import { H256 , H256Value , PlatformAddress , PlatformAddressValue , U64Value } from "codechain-primitives" ; import { Transaction } from "../core/Transaction" ; import { Rpc } from "." ; export class AccountRpc { private rpc : Rpc ; hidden constructor ( rpc ) { this . rpc = rpc ; } returns public getList ( ) { return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "account_getList" , [ ] ) . then ( ( accounts ) => { try { if ( Array . isArray ( accounts ) ) { resolve ( accounts . map ( account => PlatformAddress . ensure ( account ) . toString ( ) ) ) ; } else { reject ( Error ( `Expected account_getList to return an array but it returned ${ accounts }` ) ) ; } } catch ( e ) { reject ( Error ( `Expected account_getList to return an array of PlatformAddress string, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param passphrase returns public create ( passphrase ? ) { if ( passphrase && typeof passphrase !== "string" ) { throw Error ( `Expected the first argument to be a string but given ${ passphrase }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "account_create" , [ passphrase ] ) . then ( account => { try { resolve ( PlatformAddress . ensure ( account ) . toString ( ) ) ; } catch ( e ) { reject ( Error ( `Expected account_create to return PlatformAddress string but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param secret param passphrase returns public importRaw ( secret , passphrase ? ) { if ( ! H256 . check ( secret ) ) { throw Error ( `Expected the first argument to be an H256 value but found ${ secret }` ) ; } if ( passphrase && typeof passphrase !== "string" ) { throw Error ( `Expected the second argument to be a string but found ${ passphrase }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "account_importRaw" , [ `0x${ H256 . ensure ( secret ) . value }` , passphrase ] ) . then ( account => { try { resolve ( PlatformAddress . ensure ( account ) . toString ( ) ) ; } catch ( e ) { reject ( Error ( `Expected account_importRaw to return PlatformAddress string but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param messageDigest param address param passphrase public sign ( messageDigest , address , passphrase ? ) { if ( ! H256 . check ( messageDigest ) ) { throw Error ( `Expected the first argument to be an H256 value but found ${ messageDigest }` ) ; } if ( ! PlatformAddress . check ( address ) ) { throw Error ( `Expected the second argument to be a PlatformAddress value but found ${ address }` ) ; } if ( passphrase && typeof passphrase !== "string" ) { throw Error ( `Expected the third argument to be a string but found ${ passphrase }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "account_sign" , [ `0x${ H256 . ensure ( messageDigest ) . value }` , PlatformAddress . ensure ( address ) . toString ( ) , passphrase ] ) . then ( result => { if ( typeof result === "string" && result . match ( /0x[0-9a-f]{130}/ ) ) { return resolve ( result ) ; } reject ( Error ( `Expected account_sign to return a 65 byte hexstring but it returned ${ result }` ) ) ; } ) . catch ( reject ) ; } ) ; } param params . tx param params . account param params . passphrase public async sendTransaction ( params ) { const { tx , fee , account , passphrase , blockNumber } = params ; if ( ! PlatformAddress . check ( account ) ) { throw Error ( `Expected the account param to be a PlatformAddress value but found ${ account }` ) ; } if ( passphrase && typeof passphrase !== "string" ) { throw Error ( `Expected the passphrase param to be a string but found ${ passphrase }` ) ; } if ( fee ) { tx . setFee ( fee ) ; } if ( tx . fee ( ) == null ) { const minFee = await this . rpc . chain . getMinTransactionFee ( tx . type ( ) , blockNumber ) ; if ( minFee === null ) { throw new Error ( "The fee of the tx is not specified" ) ; } tx . setFee ( minFee ) ; } const result = await this . rpc . sendRpcRequest ( "account_sendTransaction" , [ tx . toJSON ( ) , PlatformAddress . ensure ( account ) . toString ( ) , passphrase ] ) ; return { hash : H256 . ensure ( result . hash ) , seq : result . seq } ; } param address param passphrase param duration public unlock ( address , passphrase ? , duration ? ) { if ( ! PlatformAddress . check ( address ) ) { throw Error ( `Expected the first argument to be a PlatformAddress value but found ${ address }` ) ; } if ( passphrase && typeof passphrase !== "string" ) { throw Error ( `Expected the second argument to be a string but found ${ passphrase }` ) ; } if ( duration !== undefined && ( typeof duration !== "number" || ! Number . isInteger ( duration ) || duration < 0 ) ) { throw Error ( `Expected the third argument to be non-negative integer but found ${ duration }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "account_unlock" , [ PlatformAddress . ensure ( address ) . toString ( ) , passphrase || "" , duration ] ) . then ( result => { if ( result === null ) { return resolve ( null ) ; } reject ( Error ( `Expected account_unlock to return null but it returned ${ result }` ) ) ; } ) . catch ( reject ) ; } ) ; } param address param oldPassphrase param newPassphrase public changePassword ( address , oldPassphrase , newPassphrase ) { if ( oldPassphrase && typeof oldPassphrase !== "string" ) { throw Error ( `Expected the second argument to be a string but given ${ oldPassphrase }` ) ; } if ( newPassphrase && typeof newPassphrase !== "string" ) { throw Error ( `Expected the second argument to be a string but given ${ newPassphrase }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "account_changePassword" , [ address , oldPassphrase , newPassphrase ] ) . then ( result => { if ( result == null ) { return resolve ( null ) ; } reject ( Error ( `Expected account_changePassword to return null but it returned ${ result }` ) ) ; } ) . catch ( reject ) ; } ) ; } }