import { ABIReturn , addressToScriptHash , AnyABI , ArrayABI , BooleanABI , BufferABI , common , ForwardValueABI , Hash160ABI , Hash256ABI , IntegerABI , MapABI , ObjectABI , Param , PublicKeyABI , ScriptBuilderParam , SignatureABI , StringABI , utils , VoidABI , } from '@neo-one/client-common' ; import * as args from '../args' ; import { InvalidArgumentError } from '../errors' ; export const params = { Any : ( _name , _param , _parameter ) : ScriptBuilderParam | undefined => undefined , String : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : args . assertString ( name , param ) , Address : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : common . stringToUInt160 ( addressToScriptHash ( args . assertAddress ( name , param ) ) ) , Hash160 : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : common . stringToUInt160 ( addressToScriptHash ( args . assertAddress ( name , param ) ) ) , Hash256 : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : common . stringToUInt256 ( args . assertHash256 ( name , param ) ) , PublicKey : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : common . stringToECPoint ( args . assertPublicKey ( name , param ) ) , Integer : ( name , param , parameter ) : ScriptBuilderParam | undefined => { if ( parameter . optional && param === undefined ) { return undefined ; } const value = args . assertBigNumber ( name , param ) ; return utils . bigNumberToBN ( value , parameter . decimals ) ; } , Boolean : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : args . assertBoolean ( name , param ) , Buffer : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : Buffer . from ( args . assertBuffer ( name , param ) , 'hex' ) , Signature : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : Buffer . from ( args . assertBuffer ( name , param ) , 'hex' ) , Array : ( name , param , parameter ) : ScriptBuilderParam | undefined => { if ( parameter . optional && param === undefined ) { return undefined ; } const paramArray = args . assertArray ( name , param ) ; const { value } = parameter ; const checker = params [ value . type ] as any ; return paramArray . map ( ( val ) => checker ( name , val , value ) ) ; } , Map : ( name , param , parameter ) : ScriptBuilderParam | undefined => { if ( parameter . optional && param === undefined ) { return undefined ; } const paramMap = args . assertMap ( name , param ) ; const { key , value } = parameter ; const keyChecker = params [ key . type ] as any ; const valueChecker = params [ value . type ] as any ; const output = new Map < ScriptBuilderParam | undefined , ScriptBuilderParam | undefined > ( ) ; paramMap . forEach ( ( v , k ) => { output . set ( keyChecker ( name , k , key ) , valueChecker ( name , v , value ) ) ; } ) ; return output ; } , Object : ( name , param , parameter ) : ScriptBuilderParam | undefined => { if ( parameter . optional && param === undefined ) { return undefined ; } const paramObject = args . assertObject ( name , param ) ; return Object . entries ( paramObject ) . reduce < { readonly [ key ] : ScriptBuilderParam ; } > ( ( acc , [ keyParam , val ] ) => { const key = args . assertString ( name , keyParam ) ; const value = parameter . properties [ key ] as ABIReturn | undefined ; if ( value === undefined ) { throw new Error ( 'Invalid abi' ) ; } const checker = params [ value . type ] as any ; return { ... acc , [ key ] : checker ( name , val , value ) } ; } , { } ) ; } , Void : ( name , param , _parameter ) : ScriptBuilderParam | undefined => { if ( param !== undefined ) { throw new InvalidArgumentError ( 'Void' , name , param ) ; } return param ; } , ForwardValue : ( name , param , parameter ) : ScriptBuilderParam | undefined => parameter . optional && param === undefined ? undefined : args . assertForwardValue ( name , param ) . converted } ;