@@ -262,11 +262,27 @@ function alignSize(size) {
262
262
}
263
263
264
264
function pack ( coders , values ) {
265
+ if ( Array . isArray ( values ) ) {
266
+ if ( coders . length !== values . length ) {
267
+ throwError ( 'types/values mismatch' , { type : type , values : values } ) ;
268
+ }
269
+
270
+ } else if ( values && typeof ( values ) === 'object' ) {
271
+ var arrayValues = [ ] ;
272
+ coders . forEach ( function ( coder ) {
273
+ arrayValues . push ( values [ coder . localName ] ) ;
274
+ } ) ;
275
+ values = arrayValues ;
276
+
277
+ } else {
278
+ throwError ( 'invalid value' , { type : 'tuple' , values : values } ) ;
279
+ }
280
+
265
281
var parts = [ ] ;
266
282
267
283
coders . forEach ( function ( coder , index ) {
268
284
parts . push ( { dynamic : coder . dynamic , value : coder . encode ( values [ index ] ) } ) ;
269
- } )
285
+ } ) ;
270
286
271
287
var staticSize = 0 , dynamicSize = 0 ;
272
288
parts . forEach ( function ( part , index ) {
@@ -414,17 +430,6 @@ function coderTuple(coders, localName) {
414
430
name : 'tuple' ,
415
431
type : type ,
416
432
encode : function ( value ) {
417
- if ( Array . isArray ( value ) ) {
418
- if ( coders . length !== value . length ) {
419
- throwError ( 'types/values mismatch' , { type : type , values : values } ) ;
420
- }
421
-
422
- // @TODO : If receiving an object, and we have names, create the array
423
-
424
- } else {
425
- throwError ( 'invalid value' , { type : types , values : values } ) ;
426
- }
427
-
428
433
return pack ( coders , value ) ;
429
434
} ,
430
435
decode : function ( data , offset ) {
@@ -572,29 +577,27 @@ function Interface(abi) {
572
577
switch ( method . type ) {
573
578
case 'constructor' :
574
579
var func = ( function ( ) {
575
- // @TODO : Move to parseParams
576
- var inputTypes = getKeys ( method . inputs , 'type' ) ;
580
+ var inputParams = parseParams ( method . inputs ) ;
577
581
var func = function ( bytecode ) {
578
582
if ( ! utils . isHexString ( bytecode ) ) {
579
- throwError ( 'invalid bytecode' , { input : bytecode } ) ;
583
+ throwError ( 'invalid bytecode' , { input : bytecode } ) ;
580
584
}
581
585
582
586
var params = Array . prototype . slice . call ( arguments , 1 ) ;
583
- if ( params . length < inputTypes . length ) {
587
+ if ( params . length < inputParams . types . length ) {
584
588
throwError ( 'missing parameter' ) ;
585
- } else if ( params . length > inputTypes . length ) {
589
+ } else if ( params . length > inputParams . types . length ) {
586
590
throwError ( 'too many parameters' ) ;
587
591
}
588
592
589
593
var result = {
590
- bytecode : bytecode + Interface . encodeParams ( inputTypes , params ) . substring ( 2 ) ,
594
+ bytecode : bytecode + Interface . encodeParams ( inputParams . names , inputParams . types , params ) . substring ( 2 ) ,
591
595
}
592
596
593
597
return populateDescription ( new DeployDescription ( ) , result ) ;
594
598
}
595
599
596
- // @TODO : Move to parseParams
597
- defineFrozen ( func , 'inputs' , getKeys ( method . inputs , 'name' ) ) ;
600
+ defineFrozen ( func , 'inputs' , inputParams ) ;
598
601
599
602
return func ;
600
603
} ) ( ) ;
@@ -608,7 +611,6 @@ function Interface(abi) {
608
611
var inputParams = parseParams ( method . inputs ) ;
609
612
var outputParams = parseParams ( method . outputs ) ;
610
613
611
- var inputTypes = inputParams . types ;
612
614
if ( method . constant ) {
613
615
var outputTypes = outputParams . types ;
614
616
var outputNames = outputParams . names ;
@@ -628,13 +630,13 @@ function Interface(abi) {
628
630
629
631
var params = Array . prototype . slice . call ( arguments , 0 ) ;
630
632
631
- if ( params . length < inputTypes . length ) {
633
+ if ( params . length < inputParams . types . length ) {
632
634
throwError ( 'missing parameter' ) ;
633
- } else if ( params . length > inputTypes . length ) {
635
+ } else if ( params . length > inputParams . types . length ) {
634
636
throwError ( 'too many parameters' ) ;
635
637
}
636
638
637
- result . data = sighash + Interface . encodeParams ( inputTypes , params ) . substring ( 2 ) ;
639
+ result . data = sighash + Interface . encodeParams ( inputParams . names , inputParams . types , params ) . substring ( 2 ) ;
638
640
if ( method . constant ) {
639
641
result . parse = function ( data ) {
640
642
return Interface . decodeParams (
@@ -649,9 +651,8 @@ function Interface(abi) {
649
651
return populateDescription ( new TransactionDescription ( ) , result ) ;
650
652
}
651
653
652
- // @TODO : Move the paraseParams
653
- defineFrozen ( func , 'inputs' , getKeys ( method . inputs , 'name' ) ) ;
654
- defineFrozen ( func , 'outputs' , getKeys ( method . outputs , 'name' ) ) ;
654
+ defineFrozen ( func , 'inputs' , inputParams ) ;
655
+ defineFrozen ( func , 'outputs' , outputParams ) ;
655
656
656
657
utils . defineProperty ( func , 'signature' , signature ) ;
657
658
utils . defineProperty ( func , 'sighash' , sighash ) ;
@@ -803,12 +804,20 @@ function Interface(abi) {
803
804
}
804
805
805
806
806
- utils . defineProperty ( Interface , 'encodeParams' , function ( types , values ) {
807
+ utils . defineProperty ( Interface , 'encodeParams' , function ( names , types , values ) {
808
+
809
+ // Names is optional, so shift over all the parameters if not provided
810
+ if ( arguments . length < 3 ) {
811
+ values = types ;
812
+ types = names ;
813
+ names = null ;
814
+ }
815
+
807
816
if ( types . length !== values . length ) { throwError ( 'types/values mismatch' , { types : types , values : values } ) ; }
808
817
809
818
var coders = [ ] ;
810
- types . forEach ( function ( type ) {
811
- coders . push ( getParamCoder ( type ) ) ;
819
+ types . forEach ( function ( type , index ) {
820
+ coders . push ( getParamCoder ( type , ( names ? names [ index ] : undefined ) ) ) ;
812
821
} ) ;
813
822
814
823
return utils . hexlify ( coderTuple ( coders ) . encode ( values ) ) ;
0 commit comments