packageDocumentation module hidden import { AbiCoder as ABICoder , ParamType , toUtf8Bytes } from './abiCoder' ; import { isObject , isArray } from '@harmony-js/utils' ; import { keccak256 , Arrayish } from '@harmony-js/crypto' ; import { jsonInterfaceMethodToString , bnToString } from './utils' ; export class AbiCoderClass { coder : ABICoder ; constructor ( coder ) { this . coder = coder ; } encodeFunctionSignature ( functionName ) { if ( isObject ( functionName ) ) { functionName = jsonInterfaceMethodToString ( functionName ) ; } const result = keccak256 ( toUtf8Bytes ( functionName ) ) ; return result . slice ( 0 , 10 ) ; } encodeEventSignature ( functionName ) { if ( isObject ( functionName ) ) { functionName = jsonInterfaceMethodToString ( functionName ) ; } const result = keccak256 ( toUtf8Bytes ( functionName ) ) ; return result ; } encodeParameter ( types , param ) { return this . encodeParameters ( [ types ] , [ param ] ) ; } encodeParameters ( types , params ) { return this . coder . encode ( types , params ) ; } encodeFunctionCall ( jsonInterface , params ) { return ( this . encodeFunctionSignature ( jsonInterface ) + this . encodeParameters ( jsonInterface . inputs , params ) . replace ( '0x' , '' ) ) ; } decodeParameter ( type , bytes ) { return this . decodeParameters ( [ type ] , bytes ) [ 0 ] ; } decodeParameters ( outputs , bytes ) { if ( isArray ( outputs ) && outputs . length === 0 ) { throw new Error ( 'Empty outputs array given!' ) ; } if ( ! bytes || bytes === '0x' || bytes === '0X' ) { throw new Error ( `Invalid bytes string given: ${ bytes }` ) ; } const result = this . coder . decode ( outputs , bytes ) ; const returnValues = { } ; let decodedValue ; if ( isArray ( result ) ) { if ( outputs . length > 1 ) { outputs . forEach ( ( output , i ) => { decodedValue = result [ i ] ; if ( decodedValue === '0x' ) { decodedValue = null ; } returnValues [ i ] = bnToString ( decodedValue ) ; if ( isObject ( output ) && output . name ) { returnValues [ output . name ] = bnToString ( decodedValue ) ; } } ) ; return returnValues ; } return bnToString ( result ) ; } if ( isObject ( outputs [ 0 ] ) && outputs [ 0 ] . name ) { returnValues [ outputs [ 0 ] . name ] = bnToString ( result ) ; } returnValues [ 0 ] = bnToString ( result ) ; return returnValues ; } decodeLog ( inputs , data = '' , topics ) { const returnValues = { } ; let topicCount = 0 ; let value ; const nonIndexedInputKeys = [ ] ; const nonIndexedInputItems = [ ] ; if ( ! isArray ( topics ) ) { topics = [ topics ] ; } inputs . forEach ( ( input , i ) => { if ( input . indexed ) { if ( input . type === 'string' ) { return ; } value = topics [ topicCount ] ; if ( this . isStaticType ( input . type ) ) { value = this . decodeParameter ( input . type , topics [ topicCount ] ) ; } returnValues [ i ] = bnToString ( value ) ; returnValues [ input . name ] = bnToString ( value ) ; topicCount ++ ; return ; } nonIndexedInputKeys . push ( i ) ; nonIndexedInputItems . push ( input ) ; } ) ; if ( data ) { const values = this . decodeParameters ( nonIndexedInputItems , data ) ; let decodedValue ; nonIndexedInputKeys . forEach ( ( itemKey , index ) => { decodedValue = values [ index ] ; returnValues [ itemKey ] = bnToString ( decodedValue ) ; returnValues [ nonIndexedInputItems [ index ] . name ] = bnToString ( decodedValue ) ; } ) ; } return returnValues ; } isStaticType ( type ) { if ( type === 'bytes' ) { return false ; } if ( type === 'string' ) { return false ; } if ( type . indexOf ( '[' ) && type . slice ( type . indexOf ( '[' ) ) . length === 2 ) { return false ; } return true ; } }