import { extractKeyId } from '../../claim/message' ; import { PublicKey , PublicKeyStatus } from '../../crypto' ; import RestClient from '../../network/rest/restClient' ; import { buildGetDDOTx , buildGetDocumentTx , buildGetPublicKeyStateTx } from '../../smartcontract/nativevm/ontidContractTxBuilder' ; import { DDO } from '../../transaction/ddo' ; import { hexstr2str } from '../../utils' ; param publicKeyId param url export async function retrievePublicKey ( publicKeyId , url ) { const ontId = extractOntId ( publicKeyId ) ; const keyId = extractKeyId ( publicKeyId ) ; const client = new RestClient ( url ) ; const getDocumentTx = buildGetDocumentTx ( ontId ) ; const response = await client . sendRawTransaction ( getDocumentTx . serialize ( ) , true ) ; if ( response . Result && response . Result . Result ) { try { const obj = JSON . parse ( hexstr2str ( response . Result . Result ) ) ; const publicKey = obj . publicKey . find ( ( pk ) => pk . id . split ( '#' ) [ 0 ] === ontId ) ; if ( publicKey === undefined ) { throw new Error ( 'Not found' ) ; } return new PublicKey ( publicKey . publicKeyHex ) ; } catch ( err ) { throw new Error ( err ) ; } } else { const getDDOTx = buildGetDDOTx ( ontId ) ; const response2 = await client . sendRawTransaction ( getDDOTx . serialize ( ) , true ) ; if ( response2 . Result && response2 . Result . Result ) { const ddo = DDO . deserialize ( response2 . Result . Result ) ; const publicKey = ddo . publicKeys . find ( ( pk ) => pk . id === keyId ) ; if ( publicKey === undefined ) { throw new Error ( 'Not found' ) ; } return publicKey . pk ; } else { throw new Error ( 'Not found' ) ; } } } param publicKeyId param url export async function retrievePublicKeyState ( publicKeyId , url ) { const ontId = extractOntId ( publicKeyId ) ; const keyId = extractKeyId ( publicKeyId ) ; const client = new RestClient ( url ) ; const getPublicKeyStateTx = buildGetPublicKeyStateTx ( ontId , keyId ) ; const response = await client . sendRawTransaction ( getPublicKeyStateTx . serialize ( ) , true ) ; if ( response . Result && response . Result . Result ) { return PublicKeyStatus . fromHexLabel ( response . Result . Result ) ; } else { throw new Error ( 'Not found' ) ; } } param publicKeyId export function extractOntId ( publicKeyId ) { const index = publicKeyId . indexOf ( '#keys-' ) ; if ( index === - 1 ) { throw new Error ( 'Is not a publicKeId.' ) ; } return publicKeyId . substr ( 0 , index ) ; }