@@ -12,6 +12,11 @@ const regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
1212
1313const Zeros = "0000000000000000000000000000000000000000000000000000000000000000" ;
1414
15+ import { Logger } from "@ethersproject/logger" ;
16+ import { version } from "./_version" ;
17+ const logger = new Logger ( version ) ;
18+
19+
1520function _pack ( type : string , value : any , isArray ?: boolean ) : Uint8Array {
1621 switch ( type ) {
1722 case "address" :
@@ -33,7 +38,7 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
3338 let size = parseInt ( match [ 2 ] || "256" )
3439
3540 if ( ( match [ 2 ] && String ( size ) !== match [ 2 ] ) || ( size % 8 !== 0 ) || size === 0 || size > 256 ) {
36- throw new Error ( "invalid number type - " + type ) ;
41+ logger . throwArgumentError ( "invalid number type" , "type" , type )
3742 }
3843
3944 if ( isArray ) { size = 256 ; }
@@ -48,9 +53,11 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
4853 const size = parseInt ( match [ 1 ] ) ;
4954
5055 if ( String ( size ) !== match [ 1 ] || size === 0 || size > 32 ) {
51- throw new Error ( "invalid bytes type - " + type ) ;
56+ logger . throwArgumentError ( "invalid bytes type" , "type" , type )
57+ }
58+ if ( arrayify ( value ) . byteLength !== size ) {
59+ logger . throwArgumentError ( `invalid value for ${ type } ` , "value" , value )
5260 }
53- if ( arrayify ( value ) . byteLength !== size ) { throw new Error ( "invalid value for " + type ) ; }
5461 if ( isArray ) { return arrayify ( ( value + Zeros ) . substring ( 0 , 66 ) ) ; }
5562 return value ;
5663 }
@@ -59,21 +66,25 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
5966 if ( match && Array . isArray ( value ) ) {
6067 const baseType = match [ 1 ] ;
6168 const count = parseInt ( match [ 2 ] || String ( value . length ) ) ;
62- if ( count != value . length ) { throw new Error ( "invalid value for " + type ) ; }
69+ if ( count != value . length ) {
70+ logger . throwArgumentError ( `invalid array length for ${ type } ` , "value" , value )
71+ }
6372 const result : Array < Uint8Array > = [ ] ;
6473 value . forEach ( function ( value ) {
6574 result . push ( _pack ( baseType , value , true ) ) ;
6675 } ) ;
6776 return concat ( result ) ;
6877 }
6978
70- throw new Error ( "invalid type - " + type ) ;
79+ return logger . throwArgumentError ( "invalid type" , "type" , type )
7180}
7281
7382// @TODO : Array Enum
7483
7584export function pack ( types : ReadonlyArray < string > , values : ReadonlyArray < any > ) {
76- if ( types . length != values . length ) { throw new Error ( "type/value count mismatch" ) ; }
85+ if ( types . length != values . length ) {
86+ logger . throwArgumentError ( "wrong number of values; expected ${ types.length }" , "values" , values )
87+ }
7788 const tight : Array < Uint8Array > = [ ] ;
7889 types . forEach ( function ( type , index ) {
7990 tight . push ( _pack ( type , values [ index ] ) ) ;
0 commit comments