import axios from 'axios' ; import * as bip39 from 'bip39' ; import { Account } from '../account' ; import { Claim } from '../claim/claim' ; import { HTTPS_REST_PORT , HTTPS_WS_PORT , ONT_BIP44_PATH , REST_API , TEST_NODE } from '../consts' ; import { Address , PgpSignature , PrivateKey , PublicKey } from '../crypto' ; import { ERROR_CODE } from '../error' ; import { Identity } from '../identity' ; import { NeoRpc } from '../neocore/NeoRpc' ; import { Program } from '../neocore/Program' ; import { SmartContract } from '../neocore/SmartContract' ; import RestClient from '../network/rest/restClient' ; import { WebsocketClient } from '../network/websocket/websocketClient' ; import * as scrypt from '../scrypt' ; import { ScryptParams } from '../scrypt' ; import AbiInfo from '../smartcontract/abi/abiInfo' ; import { Parameter } from '../smartcontract/abi/parameter' ; import { makeTransferTx , makeTransferTxV2 , makeWithdrawOngTx , makeWithdrawOngTxV2 , ONT_CONTRACT } from '../smartcontract/nativevm/ontAssetTxBuilder' ; import { buildAddAttributeTx , buildGetDDOTx , buildRegisterOntidTx } from '../smartcontract/nativevm/ontidContractTxBuilder' ; import { Oep8TxBuilder } from '../smartcontract/neovm/oep8TxBuilder' ; import { DDOAttribute } from '../transaction/ddo' ; import { Transaction } from '../transaction/transaction' ; import { buildRestfulParam , sendRawTxRestfulUrl , signTransaction , signTx } from '../transaction/transactionBuilder' ; import { generateMnemonic , hexstr2str , isBase64 , isHexString , now , reverseHex , sendBackResult2Native , str2hexstr , StringReader } from '../utils' ; import { Wallet } from '../wallet' ; import { Ecies } from './../crypto/Ecies' ; import { ParameterType } from './../smartcontract/abi/parameter' ; import { Oep4TxBuilder } from './../smartcontract/neovm/oep4TxBuilder' ; const { HDKey } = require ( '@ont-dev/hdkey-secp256r1' ) ; const CONTRACT_HASH = 'ceab719b8baa2310f232ee0d277c061704541cfb' ; const NEO_NODE = 'https://neonode1.ont.network:10332' ; const NEP5_ABI = '{"hash":"0x5bb169f915c916a5e30a3c13a5e0cd228ea26826","entrypoint":"Main","functions":[{"name":"Name","parameters":[],"returntype":"String"},{"name":"Symbol","parameters":[],"returntype":"String"},{"name":"Decimals","parameters":[],"returntype":"Integer"},{"name":"Main","parameters":[{"name":"operation","type":"String"},{"name":"args","type":"Array"}],"returntype":"Any"},{"name":"Init","parameters":[],"returntype":"Boolean"},{"name":"TotalSupply","parameters":[],"returntype":"Integer"},{"name":"Transfer","parameters":[{"name":"from","type":"ByteArray"},{"name":"to","type":"ByteArray"},{"name":"value","type":"Integer"}],"returntype":"Boolean"},{"name":"BalanceOf","parameters":[{"name":"address","type":"ByteArray"}],"returntype":"Integer"}],"events":[{"name":"transfer","parameters":[{"name":"arg1","type":"ByteArray"},{"name":"arg2","type":"ByteArray"},{"name":"arg3","type":"Integer"}],"returntype":"Void"}]}' ; const NEO_TRAN = 100000000 ; export class SDK { static SERVER_NODE : string = TEST_NODE ; static REST_PORT : string = HTTPS_REST_PORT ; static SOCKET_PORT : string = HTTPS_WS_PORT ; static restClient : RestClient = new RestClient ( ) ; static socketClient : WebsocketClient = new WebsocketClient ( ) ; static setServerNode ( node ) { if ( node ) { let url = '' ; if ( node . indexOf ( 'https' ) > - 1 ) { url = node . substr ( 'https://' . length ) ; } else { url = node ; } SDK . SERVER_NODE = url ; return ; } throw new Error ( 'Can not set ' + node + 'as server node' ) ; } static setRestPort ( port ) { if ( port ) { SDK . REST_PORT = port ; SDK . restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return ; } throw new Error ( 'Can not set ' + port + ' as restful port' ) ; } static setSocketPort ( port ) { if ( port ) { SDK . SOCKET_PORT = port ; SDK . socketClient = new WebsocketClient ( `wss://${ SDK . SERVER_NODE }:${ SDK . SOCKET_PORT }` ) ; return ; } throw new Error ( 'Can not set ' + port + 'as socket port' ) ; } static getDecryptError ( err ) { return { error : ERROR_CODE . Decrypto_ERROR , result : '' } ; } static transformPassword ( password ) { if ( isBase64 ( password ) ) { return Buffer . from ( password , 'base64' ) . toString ( ) ; } return password ; } static createWallet ( name , password , payer , gasPrice , gasLimit , callback ? ) { const wallet = Wallet . create ( name ) ; password = this . transformPassword ( password ) ; const privateKey = PrivateKey . random ( ) ; const identity = Identity . create ( privateKey , password , name ) ; wallet . defaultOntid = identity . ontid ; wallet . addIdentity ( identity ) ; const walletDataStr = wallet . toJson ( ) ; let obj = { error : 0 , result : walletDataStr , tx : '' } ; const publicKey = privateKey . getPublicKey ( ) ; const tx = buildRegisterOntidTx ( identity . ontid , publicKey , gasPrice , gasLimit ) ; tx . payer = new Address ( payer ) ; signTransaction ( tx , privateKey ) ; privateKey . key = '' ; password = '' ; const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { if ( res . Result . Result === '01' ) { obj . tx = tx . serialize ( ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } else { const errResult = { error : ERROR_CODE . PreExec_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( errResult ) , callback ) ; } return errResult ; } } ) . catch ( ( err ) => { obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } } ) ; } static importIdentityWithPrivateKey ( label = '' , privateKey , password , callback ? ) { privateKey = privateKey . trim ( ) ; password = this . transformPassword ( password ) ; if ( ! privateKey || privateKey . length !== 64 || ! isHexString ( privateKey ) ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } let obj ; const pri = new PrivateKey ( privateKey ) ; const identity = Identity . create ( pri , password , label ) ; obj = { error : ERROR_CODE . SUCCESS , result : identity . toJson ( ) } ; const tx = buildGetDDOTx ( identity . ontid ) ; const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { const result = res . Result ; if ( result . Result ) { } else { obj . error = ERROR_CODE . UNKNOWN_ONTID ; obj . result = '' ; } if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } ) . catch ( ( err ) => { obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } } ) ; } static importIdentityWithWif ( label = '' , wif , password , callback ? ) { wif = wif . trim ( ) ; password = this . transformPassword ( password ) ; let obj ; let pri ; try { pri = PrivateKey . deserializeWIF ( wif ) ; } catch ( err ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } const identity = Identity . create ( pri , password , label ) ; obj = { error : ERROR_CODE . SUCCESS , result : identity . toJson ( ) } ; const tx = buildGetDDOTx ( identity . ontid ) ; const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { const result = res . Result ; if ( result . Result ) { } else { obj . error = ERROR_CODE . UNKNOWN_ONTID ; obj . result = '' ; } if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } ) . catch ( ( err ) => { obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } } ) ; } static importIdentityWithWifOffChain ( label = '' , wif , password , callback ? ) { wif = wif . trim ( ) ; password = this . transformPassword ( password ) ; let obj ; let pri ; try { pri = PrivateKey . deserializeWIF ( wif ) ; } catch ( err ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } const identity = Identity . create ( pri , password , label ) ; obj = { error : ERROR_CODE . SUCCESS , result : identity . toJson ( ) } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } static importIdentityWithWallet ( label , encryptedPrivateKey , password , address , salt , callback ? ) { let obj ; let identity = new Identity ( ) ; try { const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; const addr = new Address ( address ) ; password = this . transformPassword ( password ) ; identity = Identity . importIdentity ( label , encryptedPrivateKeyObj , password , addr , salt ) ; } catch ( err ) { obj = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } obj = { error : ERROR_CODE . SUCCESS , result : identity . toJson ( ) } ; const tx = buildGetDDOTx ( identity . ontid ) ; const param = buildRestfulParam ( tx ) ; const restUrl = `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ; const url = sendRawTxRestfulUrl ( restUrl , true ) ; password = '' ; return axios . post ( url , param ) . then ( ( res ) => { const result = res . data . Result ; if ( result . Result ) { } else { obj . error = ERROR_CODE . UNKNOWN_ONTID ; obj . result = '' ; } if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } ) . catch ( ( err ) => { obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } } ) ; } static importIdentityAndCreateWallet ( label , encryptedPrivateKey , password , address , salt , callback ? ) { let identity = new Identity ( ) ; let error = { } ; let obj ; try { password = this . transformPassword ( password ) ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; const addr = new Address ( address ) ; identity = Identity . importIdentity ( label , encryptedPrivateKeyObj , password , addr , salt ) ; const wallet = Wallet . create ( identity . label ) ; wallet . defaultOntid = identity . ontid ; wallet . addIdentity ( identity ) ; const walletStr = wallet . toJson ( ) ; obj = { error : ERROR_CODE . SUCCESS , result : walletStr } ; const tx = buildGetDDOTx ( identity . ontid ) ; const param = buildRestfulParam ( tx ) ; const restUrl = `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ; const url = sendRawTxRestfulUrl ( restUrl , true ) ; return axios . post ( url , param ) . then ( ( res ) => { const result = res . data . Result ; if ( result . Result ) { } else { obj . error = ERROR_CODE . UNKNOWN_ONTID ; obj . result = '' ; } password = '' ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } ) . catch ( ( err ) => { obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } } ) ; } catch ( err ) { error = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( error ) , callback ) ; } return Promise . reject ( error ) ; } } static createIdentity ( label , password , payer , gasPrice , gasLimit , callback ? ) { const privateKey = PrivateKey . random ( ) ; password = this . transformPassword ( password ) ; const identity = Identity . create ( privateKey , password , label ) ; const result = identity . toJson ( ) ; let obj = { error : ERROR_CODE . SUCCESS , result , tx : '' } ; const publicKey = privateKey . getPublicKey ( ) ; const tx = buildRegisterOntidTx ( identity . ontid , publicKey , gasPrice , gasLimit ) ; tx . payer = new Address ( payer ) ; signTransaction ( tx , privateKey ) ; password = '' ; privateKey . key = '' ; const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { if ( res . Result . Result === '01' ) { obj . tx = tx . serialize ( ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } privateKey . key = '' ; password = '' ; return obj ; } else { const errResult = { error : ERROR_CODE . PreExec_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( errResult ) , callback ) ; } return errResult ; } } ) . catch ( ( err ) => { obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } } ) ; } static createAccount ( label , password , callback ? ) { let mnemonic = generateMnemonic ( ) ; password = this . transformPassword ( password ) ; const mnemonicHex = str2hexstr ( mnemonic ) ; const privateKey = PrivateKey . generateFromMnemonic ( mnemonic ) ; const account = Account . create ( privateKey , password , label ) ; const addr = account . address ; const salt = Buffer . from ( account . salt , 'base64' ) . toString ( 'hex' ) ; const mnemonicEnc = scrypt . encryptWithGcm ( mnemonicHex , addr , salt , password ) ; const result = account . toJson ( ) ; const obj = { error : ERROR_CODE . SUCCESS , result , mnemonicEnc } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } privateKey . key = '' ; password = '' ; mnemonic = '' ; return obj ; } static decryptMnemonicEnc ( mnemonicEnc , address , salt , password , callback ? ) { let obj ; password = this . transformPassword ( password ) ; const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; const decMneHex = scrypt . decryptWithGcm ( mnemonicEnc , addr , saltHex , password ) ; const decMne = hexstr2str ( decMneHex ) ; obj = { error : ERROR_CODE . SUCCESS , result : decMne } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } static importAccountWithWallet ( label , encryptedPrivateKey , address , salt , password , callback ? ) { let account = new Account ( ) ; password = this . transformPassword ( password ) ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const addr = new Address ( address ) ; account = Account . importAccount ( label , encryptedPrivateKeyObj , password , addr , salt ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const obj = { error : ERROR_CODE . SUCCESS , result : account . toJson ( ) } ; if ( address !== account . address . toBase58 ( ) ) { obj . error = ERROR_CODE . INVALID_ADDR , obj . result = '' ; } if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } password = '' ; return obj ; } static signSelfClaim ( context , claimData , ontid , encryptedPrivateKey , password , address , salt , callback ? ) { let privateKey ; password = this . transformPassword ( password ) ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; const restUrl = `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }${ REST_API . sendRawTx }` ; try { const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const metadata = { issuer : ontid , subject : ontid , issuedAt : now ( ) } ; const publicKeyId = ontid + '#keys-1' ; const claim = new Claim ( metadata , undefined , undefined ) ; claim . sign ( restUrl , publicKeyId , privateKey ) ; const obj = { error : 0 , result : claim } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } privateKey . key = '' ; password = '' ; return obj ; } static decryptEncryptedPrivateKey ( encryptedPrivateKey , password , address , salt , callback ? ) { password = this . transformPassword ( password ) ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; let pri ; try { const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; pri = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const obj = { error : 0 , result : pri . key } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } password = '' ; return obj ; } static getClaim ( claimId , context , issuer , subject , encryptedPrivateKey , password , address , salt , payer , gasPrice , gasLimit , callback ? ) { let privateKey ; password = this . transformPassword ( password ) ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const path = 'claim' + claimId ; const valueObj = { Type : 'JSON' , Value : { Context : context , Issuer : issuer } } ; const value = JSON . stringify ( valueObj ) ; const attr = new DDOAttribute ( ) ; attr . key = path ; attr . type = 'JSON' ; attr . value = value ; const publicKey = privateKey . getPublicKey ( ) ; const tx = buildAddAttributeTx ( subject , [ attr ] , publicKey , gasPrice , gasLimit ) ; tx . payer = new Address ( payer ) ; signTransaction ( tx , privateKey ) ; const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { if ( res . Result . Result === '01' ) { const obj = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } privateKey . key = '' ; password = '' ; return obj ; } else { const obj = { error : ERROR_CODE . PreExec_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } } ) . catch ( ( err ) => { const obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } } ) ; } static signData ( content , encryptedPrivateKey , password , address , salt , callback ? ) { let privateKey ; password = this . transformPassword ( password ) ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; let result ; try { const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const signature = privateKey . sign ( content ) ; result = signature . serializePgp ( ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } privateKey . key = '' ; password = '' ; return result ; } static getBalance ( address , callback ? ) { const addressObj = new Address ( address ) ; const request = `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }${ REST_API . getBalance }/${ addressObj . toBase58 ( ) }` ; return axios . get ( request ) . then ( ( res ) => { if ( res . data . Error === 0 ) { const result = res . data . Result ; const obj = { error : 0 , result } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } else { const obj = { error : res . data . Error , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } } ) . catch ( ( err ) => { const obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return Promise . reject ( obj ) ; } ) ; } static getBalanceV2 ( address , callback ? ) { const addressObj = new Address ( address ) ; const request = `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }${ REST_API . getBalanceV2 }/${ addressObj . toBase58 ( ) }` ; return axios . get ( request ) . then ( ( res ) => { if ( res . data . Error === 0 ) { const result = res . data . Result ; const obj = { error : 0 , result } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } else { const obj = { error : res . data . Error , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } } ) . catch ( ( err ) => { const obj = { error : ERROR_CODE . NETWORK_ERROR , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return Promise . reject ( obj ) ; } ) ; } static transferAssets ( token , from , to , value , encryptedPrivateKey , password , salt , gasPrice , gasLimit , payer , callback ? ) { let fromAddress ; let toAddress ; password = this . transformPassword ( password ) ; try { fromAddress = new Address ( from ) ; toAddress = new Address ( to ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const addr = new Address ( from ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const tx = makeTransferTx ( token , fromAddress , toAddress , value , gasPrice , gasLimit ) ; tx . payer = new Address ( payer ) ; signTransaction ( tx , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) , txHash : reverseHex ( tx . getSignContent ( ) ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static transferAssetsV2 ( token , from , to , value , encryptedPrivateKey , password , salt , gasPrice , gasLimit , payer , callback ? ) { let fromAddress ; let toAddress ; password = this . transformPassword ( password ) ; try { fromAddress = new Address ( from ) ; toAddress = new Address ( to ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const addr = new Address ( from ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const tx = makeTransferTxV2 ( token , fromAddress , toAddress , value , gasPrice , gasLimit ) ; tx . payer = new Address ( payer ) ; signTransaction ( tx , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) , txHash : reverseHex ( tx . getSignContent ( ) ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static claimOng ( address , value , encryptedPrivateKey , password , salt , gasPrice , gasLimit , payer , callback ? ) { let addressObj ; password = this . transformPassword ( password ) ; try { addressObj = new Address ( address ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addressObj , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const tx = makeWithdrawOngTx ( addressObj , addressObj , value , new Address ( payer ) , gasPrice , gasLimit ) ; signTransaction ( tx , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) , txHash : reverseHex ( tx . getSignContent ( ) ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static claimOngV2 ( address , value , encryptedPrivateKey , password , salt , gasPrice , gasLimit , payer , callback ? ) { let addressObj ; password = this . transformPassword ( password ) ; try { addressObj = new Address ( address ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addressObj , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const tx = makeWithdrawOngTxV2 ( addressObj , addressObj , value , new Address ( payer ) , gasPrice , gasLimit ) ; signTransaction ( tx , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) , txHash : reverseHex ( tx . getSignContent ( ) ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static exportIdentityToQrcode ( identityDataStr , callback ? ) { const obj = Identity . parseJson ( identityDataStr ) ; let salt = obj . controls [ 0 ] . salt ; if ( ! isBase64 ( salt ) ) { salt = Buffer . from ( salt , 'hex' ) . toString ( 'base64' ) ; } const result = { type : 'I' , label : obj . label , algorithm : 'ECDSA' , scrypt : { n : 4096 , p : 8 , r : 8 , dkLen : 64 } , key : obj . controls [ 0 ] . encryptedKey . key , salt , address : obj . controls [ 0 ] . address . toBase58 ( ) , parameters : { curve : 'secp256r1' } } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } static exportIdentityToKeystring ( identityDataStr , callback ? ) { const obj = Identity . parseJson ( identityDataStr ) ; const address = obj . controls [ 0 ] . address . toBase58 ( ) ; const salt = obj . controls [ 0 ] . salt ; const key = obj . controls [ 0 ] . encryptedKey . key ; const result = salt + address + key ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } static exportAccountToQrcode ( accountDataStr , callback ? ) { const obj = Account . parseJson ( accountDataStr ) ; const result = { type : 'A' , label : obj . label , algorithm : 'ECDSA' , scrypt : { n : 4096 , p : 8 , r : 8 , dkLen : 64 } , key : obj . encryptedKey . key , salt : obj . salt , address : obj . address . toBase58 ( ) , parameters : { curve : 'secp256r1' } } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } static exportAccountToKeystring ( accountDataStr , callback ? ) { const obj = Account . parseJson ( accountDataStr ) ; const salt = obj . salt ; const address = obj . address . toBase58 ( ) ; const key = obj . encryptedKey . key ; const result = salt + address + key ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } static importAccountMnemonic ( label , mnemonic , password , callback ? ) { mnemonic = mnemonic . trim ( ) ; password = this . transformPassword ( password ) ; if ( ! bip39 . validateMnemonic ( mnemonic ) ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } const seed = bip39 . mnemonicToSeedHex ( mnemonic ) ; const hdkey = HDKey . fromMasterSeed ( Buffer . from ( seed , 'hex' ) ) ; const pri = hdkey . derive ( ONT_BIP44_PATH ) ; const key = Buffer . from ( pri . privateKey ) . toString ( 'hex' ) ; const privateKey = new PrivateKey ( key ) ; const account = Account . create ( privateKey , password , label ) ; const result = account . toJson ( ) ; const obj = { error : ERROR_CODE . SUCCESS , result } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } privateKey . key = '' ; password = '' ; mnemonic = '' ; return obj ; } static exportWifPrivakeKey ( encryptedKey , password , address , salt , callback ? ) { if ( address . length !== 34 && address . length !== 40 ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } password = this . transformPassword ( password ) ; const encrypt = new PrivateKey ( encryptedKey ) ; const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; const privateKey = encrypt . decrypt ( password , addr , saltHex ) ; let wif = privateKey . serializeWIF ( ) ; const result = { error : ERROR_CODE . SUCCESS , result : { wif , privateKey : privateKey . key } } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; wif = '' ; password = '' ; return result ; } static importAccountWithWif ( label , wif , password , callback ? ) { let privateKey ; password = this . transformPassword ( password ) ; try { privateKey = PrivateKey . deserializeWIF ( wif ) ; } catch ( err ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } const account = Account . create ( privateKey , password , label ) ; const result = { error : ERROR_CODE . SUCCESS , result : account . toJson ( ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static importAccountWithPrivateKey ( label , privateKey , password , callback ? ) { privateKey = privateKey . trim ( ) ; password = this . transformPassword ( password ) ; if ( ! privateKey || privateKey . length !== 64 || ! isHexString ( privateKey ) ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } const pri = new PrivateKey ( privateKey ) ; const account = Account . create ( pri , password , label ) ; const result = { error : ERROR_CODE . SUCCESS , result : account . toJson ( ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey = '' ; password = '' ; return result ; } /**
     * Actually import with Qrcode
     */ static importAccountWithKeystore ( keystore , password , callback ? ) { let keyStoreObj ; password = this . transformPassword ( password ) ; try { keyStoreObj = JSON . parse ( keystore ) ; } catch ( err ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } if ( keyStoreObj . type !== 'A' ) { const obj = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; callback && sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; return obj ; } else { let account = new Account ( ) ; const encryptedPrivateKeyObj = new PrivateKey ( keyStoreObj . key ) ; try { const params = { cost : keyStoreObj . scrypt . n || 4096 , blockSize : keyStoreObj . scrypt . p || 8 , parallel : keyStoreObj . scrypt . r || 8 , size : keyStoreObj . scrypt . dkLen || 64 } ; const addr = new Address ( keyStoreObj . address ) ; account = Account . importAccount ( keyStoreObj . label , encryptedPrivateKeyObj , password , addr , keyStoreObj . salt , params ) ; const obj = { error : ERROR_CODE . SUCCESS , result : account . toJson ( ) } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } password = '' ; return obj ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } } } static getUnclaimedOng ( address , callback ? ) { const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . getAllowance ( 'ong' , new Address ( ONT_CONTRACT ) , new Address ( address ) ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : res . Result } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) . catch ( ( err ) => { const result = { error : err . Error , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) ; } static getUnclaimedOngV2 ( address , callback ? ) { const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . getAllowanceV2 ( 'ong' , new Address ( ONT_CONTRACT ) , new Address ( address ) ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : res . Result } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) . catch ( ( err ) => { const result = { error : err . Error , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) ; } static querySmartCodeEventByTxhash ( txHash , callback ? ) { const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . getSmartCodeEvent ( txHash ) . then ( ( res ) => { const obj = { error : ERROR_CODE . SUCCESS , result : res } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } ) . catch ( ( err ) => { const result = { error : err . Error , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) ; } static createSharedWallet ( requiredSignatureNum , allRelatedPks , callback ? ) { const M = parseInt ( requiredSignatureNum , 10 ) ; let pks = [ ] ; let pubs = [ ] ; let error = ERROR_CODE . SUCCESS ; try { pks = JSON . parse ( allRelatedPks ) ; pubs = pks . map ( ( p ) => PublicKey . deserializeHex ( new StringReader ( p ) ) ) ; } catch ( err ) { error = ERROR_CODE . INVALID_PARAMS ; } if ( M < 2 || pks . length < M || pks . length > 12 ) { error = ERROR_CODE . INVALID_PARAMS ; } let address = '' ; try { address = Address . fromMultiPubKeys ( M , pubs ) . toBase58 ( ) ; } catch ( err ) { error = ERROR_CODE . INVALID_PARAMS ; } if ( callback ) { const result = { error , result : address } ; sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return address ; } static adderssFromPublicKey ( publicKey , callback ? ) { const pk = PublicKey . deserializeHex ( new StringReader ( publicKey ) ) ; const address = Address . fromPubKey ( pk ) . toBase58 ( ) ; const result = { error : ERROR_CODE . SUCCESS , result : address } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return address ; } static makeMultiSignTransaction ( asset , from , to , amount , gasPrice , gasLimit , callback ? ) { let fromAddress ; let toAddress ; try { fromAddress = new Address ( from ) ; toAddress = new Address ( to ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const tx = makeTransferTx ( asset , fromAddress , toAddress , amount , gasPrice , gasLimit ) ; tx . payer = fromAddress ; const result = { error : ERROR_CODE . SUCCESS , txHash : reverseHex ( tx . getSignContent ( ) ) , txData : tx . serialize ( ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return result ; } static makeMultiSignTransactionV2 ( asset , from , to , amount , gasPrice , gasLimit , callback ? ) { let fromAddress ; let toAddress ; try { fromAddress = new Address ( from ) ; toAddress = new Address ( to ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const tx = makeTransferTxV2 ( asset , fromAddress , toAddress , amount , gasPrice , gasLimit ) ; tx . payer = fromAddress ; const result = { error : ERROR_CODE . SUCCESS , txHash : reverseHex ( tx . getSignContent ( ) ) , txData : tx . serialize ( ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return result ; } static signMultiAddrTransaction ( encryptedPrivateKey , address , salt , password , allRelatedPks , requiredSignatureNum , txDada , callback ? ) { password = this . transformPassword ( password ) ; let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const M = parseInt ( requiredSignatureNum , 10 ) ; const tx = Transaction . deserialize ( txDada ) ; const pubs = JSON . parse ( allRelatedPks ) ; const pks = pubs . map ( ( p ) => new PublicKey ( p ) ) ; signTx ( tx , M , pks , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , signedHash : tx . serialize ( ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return tx ; } /**
     * Neo transfer
     */ static neoTransfer ( from , to , value , encryptedPrivateKey , password , salt , callback ? , params ? ) { password = this . transformPassword ( password ) ; const recv = new Address ( to ) ; const addr = new Address ( from ) ; const abiInfo = AbiInfo . parseJson ( NEP5_ABI ) ; const contractAddr = new Address ( reverseHex ( CONTRACT_HASH ) ) ; const amount = parseInt ( value , 10 ) ; const func = abiInfo . getFunction ( 'Transfer' ) ; func . name = func . name . toLowerCase ( ) ; let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex , params ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const p1 = new Parameter ( 'from' , ParameterType . ByteArray , addr . serialize ( ) ) ; const p2 = new Parameter ( 'to' , ParameterType . ByteArray , recv . serialize ( ) ) ; const p3 = new Parameter ( 'value' , ParameterType . Integer , amount * NEO_TRAN ) ; func . setParamsValue ( p1 , p2 , p3 ) ; const tx = SmartContract . makeInvokeTransaction ( contractAddr , addr , func ) ; const p = new Program ( ) ; p . parameter = Program . programFromParams ( [ tx . sign ( privateKey ) ] ) ; p . code = Program . programFromPubKey ( privateKey . getPublicKey ( ) ) ; tx . scripts = [ p ] ; return NeoRpc . sendRawTransaction ( NEO_NODE , tx . serialize ( ) ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : '' } ; if ( res . result ) { result . result = reverseHex ( tx . getHash ( ) ) ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } else { result . error = ERROR_CODE . NETWORK_ERROR ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) ; } static getNeoBalance ( address , callback ? ) { const contractAddr = new Address ( reverseHex ( CONTRACT_HASH ) ) ; const addr = new Address ( address ) ; return NeoRpc . getBalance ( NEO_NODE , contractAddr , addr ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : 0 } ; if ( res . result ) { const balance = parseInt ( reverseHex ( res . result ) , 16 ) ; result . result = balance ; } callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return result ; } ) ; } static sendTransaction ( txData , callback ? ) { const restClient = new RestClient ( `https://${ SDK . SERVER_NODE }:${ SDK . REST_PORT }` ) ; return restClient . sendRawTransaction ( txData ) . then ( ( res ) => { const obj = { error : ERROR_CODE . SUCCESS , result : res } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } ) . catch ( ( err ) => { const result = { error : err . Error , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) ; } static sendTransactionWithWebsocket ( txData , callback ? ) { const socketClient = new WebsocketClient ( `ws://${ SDK . SERVER_NODE }:${ SDK . SOCKET_PORT }` ) ; return socketClient . sendRawTransaction ( txData , false , true ) . then ( ( res ) => { const obj = { error : ERROR_CODE . SUCCESS , result : res } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } ) . catch ( ( err ) => { const result = { error : err . Error , result : '' } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } ) ; } static queryOep8Balance ( contractHash , account , tokenId , callback ? ) { const contractAddr = new Address ( reverseHex ( contractHash ) ) ; const oep8 = new Oep8TxBuilder ( contractAddr ) ; const addr = new Address ( account ) ; const tx = oep8 . makeQueryBalanceOfTx ( addr , tokenId ) ; return SDK . restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : 0 } ; if ( res . Result . Result ) { result . result = parseInt ( reverseHex ( res . Result . Result ) , 16 ) ; } callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return result ; } ) ; } static queryOep8Balances ( contractHash , account , callback ? ) { const contractAddr = new Address ( reverseHex ( contractHash ) ) ; const oep8 = new Oep8TxBuilder ( contractAddr ) ; const addr = new Address ( account ) ; const tx = oep8 . makeQueryBalancesTx ( addr ) ; return SDK . restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] } ; if ( res . Result . Result ) { const vals = res . Result . Result . map ( ( v ) => v ? parseInt ( reverseHex ( v ) , 16 ) : 0 ) ; result . result = vals ; } callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return result ; } ) ; } static queryOep8TotalBalance ( contractHash , account , callback ? ) { const contractAddr = new Address ( reverseHex ( contractHash ) ) ; const oep8 = new Oep8TxBuilder ( contractAddr ) ; const addr = new Address ( account ) ; const tx = oep8 . makeQueryTotalBalanceTx ( addr ) ; return SDK . restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : 0 } ; if ( res . Result . Result ) { result . result = parseInt ( reverseHex ( res . Result . Result ) , 16 ) ; } callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return result ; } ) ; } static transferOep8 ( contractHash , from , to , value , tokenId , encryptedPrivateKey , password , salt , gasPrice , gasLimit , payer , callback ? ) { let fromAddress ; let toAddress ; let payerAddress ; password = this . transformPassword ( password ) ; try { fromAddress = new Address ( from ) ; toAddress = new Address ( to ) ; payerAddress = new Address ( payer ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; return result ; } let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const addr = new Address ( from ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; return result ; } const contractAddr = new Address ( reverseHex ( contractHash ) ) ; const oep8 = new Oep8TxBuilder ( contractAddr ) ; const tx = oep8 . makeTransferTx ( fromAddress , toAddress , tokenId , value , gasPrice , gasLimit , payerAddress ) ; signTransaction ( tx , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) , txHash : reverseHex ( tx . getSignContent ( ) ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static compoundOep8 ( contractHash , account , compoundNum , encryptedPrivateKey , password , salt , gasPrice , gasLimit , payer , callback ) { let addr ; password = this . transformPassword ( password ) ; try { addr = new Address ( account ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; return result ; } let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; return result ; } const contractAddr = new Address ( reverseHex ( contractHash ) ) ; const oep8 = new Oep8TxBuilder ( contractAddr ) ; const tx = oep8 . makeCompoundTx ( addr , compoundNum , gasPrice , gasLimit , addr ) ; signTransaction ( tx , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) , txHash : reverseHex ( tx . getSignContent ( ) ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static queryOep4Balance ( contractHash , account , callback ? ) { const contractAddr = new Address ( reverseHex ( contractHash ) ) ; const oep4 = new Oep4TxBuilder ( contractAddr ) ; const addr = new Address ( account ) ; const tx = oep4 . queryBalanceOf ( addr ) ; return SDK . restClient . sendRawTransaction ( tx . serialize ( ) , true ) . then ( ( res ) => { const result = { error : ERROR_CODE . SUCCESS , result : 0 } ; if ( res . Result . Result ) { result . result = parseInt ( reverseHex ( res . Result . Result ) , 16 ) ; } callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; return result ; } ) ; } static transferOep4 ( contractHash , from , to , value , encryptedPrivateKey , password , salt , gasPrice , gasLimit , callback ? ) { let fromAddress ; let toAddress ; password = this . transformPassword ( password ) ; try { fromAddress = new Address ( from ) ; toAddress = new Address ( to ) ; } catch ( err ) { const result = { error : ERROR_CODE . INVALID_PARAMS , result : '' } ; return result ; } let privateKey ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; try { const addr = new Address ( from ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; privateKey = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; return result ; } const contractAddr = new Address ( reverseHex ( contractHash ) ) ; const oep4 = new Oep4TxBuilder ( contractAddr ) ; const tx = oep4 . makeTransferTx ( fromAddress , toAddress , value , gasPrice , gasLimit , fromAddress ) ; signTransaction ( tx , privateKey ) ; const result = { error : ERROR_CODE . SUCCESS , result : '' , tx : tx . serialize ( ) , txHash : reverseHex ( tx . getSignContent ( ) ) } ; callback && sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; privateKey . key = '' ; password = '' ; return result ; } static eciesDecrypt ( encryptedPrivateKey , password , address , salt , cipher , callback ? ) { password = this . transformPassword ( password ) ; const encryptedPrivateKeyObj = new PrivateKey ( encryptedPrivateKey ) ; let pri ; try { const addr = new Address ( address ) ; const saltHex = Buffer . from ( salt , 'base64' ) . toString ( 'hex' ) ; pri = encryptedPrivateKeyObj . decrypt ( password , addr , saltHex ) ; } catch ( err ) { const result = this . getDecryptError ( err ) ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( result ) , callback ) ; } return result ; } const ins = new Ecies ( ) ; const cipherContent = cipher . split ( '.' ) ; ins . setKeyPair ( pri . key ) ; const plainBuffer = ins . dec ( cipherContent [ 0 ] , cipherContent [ 1 ] , cipherContent [ 2 ] , 32 ) ; const plain = plainBuffer . toString ( 'utf8' ) ; const obj = { error : 0 , result : plain } ; if ( callback ) { sendBackResult2Native ( JSON . stringify ( obj ) , callback ) ; } return obj ; } }