@@ -270,25 +270,64 @@ export class ContractSpec {
270
270
}
271
271
}
272
272
if ( Array . isArray ( val ) ) {
273
- if ( xdr . ScSpecType . scSpecTypeVec ( ) . value === value ) {
274
- let vec = ty . value ( ) as xdr . ScSpecTypeVec ;
275
- let elementType = vec . elementType ( ) ;
276
- return xdr . ScVal . scvVec (
277
- val . map ( ( v ) => this . nativeToScVal ( v , elementType ) )
278
- ) ;
279
- } else if ( xdr . ScSpecType . scSpecTypeTuple ( ) . value === value ) {
280
- let tup = ty . value ( ) as xdr . ScSpecTypeTuple ;
281
- let valTypes = tup . valueTypes ( ) ;
282
- if ( val . length !== valTypes . length ) {
283
- throw new TypeError (
284
- `Tuple expects ${ valTypes . length } values, but ${ val . length } were provided`
273
+ // if (xdr.ScSpecType.scSpecTypeVec().value === value) {
274
+ // let vec = ty.value() as xdr.ScSpecTypeVec;
275
+ // let elementType = vec.elementType();
276
+ // return xdr.ScVal.scvVec(
277
+ // val.map((v) => this.nativeToScVal(v, elementType))
278
+ // );
279
+ // } else if (xdr.ScSpecType.scSpecTypeTuple().value === value) {
280
+ // let tup = ty.value() as xdr.ScSpecTypeTuple;
281
+ // let valTypes = tup.valueTypes();
282
+ // if (val.length !== valTypes.length) {
283
+ // throw new TypeError(
284
+ // `Tuple expects ${valTypes.length} values, but ${val.length} were provided`
285
+ // );
286
+ // }
287
+ // return xdr.ScVal.scvVec(
288
+ // val.map((v, i) => this.nativeToScVal(v, valTypes[i]))
289
+ // );
290
+ // } else {
291
+ // throw new TypeError(`Type ${ty} was not vec, but value was Array`);
292
+ // }
293
+ // This is converted to use a switch statement with a new case for the map type
294
+ switch ( value ) {
295
+ case xdr . ScSpecType . scSpecTypeVec ( ) . value : {
296
+ let vec = ty . value ( ) as xdr . ScSpecTypeVec ;
297
+ let elementType = vec . elementType ( ) ;
298
+ return xdr . ScVal . scvVec (
299
+ val . map ( ( v ) => this . nativeToScVal ( v , elementType ) )
285
300
) ;
286
301
}
287
- return xdr . ScVal . scvVec (
288
- val . map ( ( v , i ) => this . nativeToScVal ( v , valTypes [ i ] ) )
289
- ) ;
290
- } else {
291
- throw new TypeError ( `Type ${ ty } was not vec, but value was Array` ) ;
302
+ case xdr . ScSpecType . scSpecTypeTuple ( ) . value : {
303
+ let tup = ty . value ( ) as xdr . ScSpecTypeTuple ;
304
+ let valTypes = tup . valueTypes ( ) ;
305
+ if ( val . length !== valTypes . length ) {
306
+ throw new TypeError (
307
+ `Tuple expects ${ valTypes . length } values, but ${ val . length } were provided`
308
+ ) ;
309
+ }
310
+ return xdr . ScVal . scvVec (
311
+ val . map ( ( v , i ) => this . nativeToScVal ( v , valTypes [ i ] ) )
312
+ ) ;
313
+ }
314
+ case xdr . ScSpecType . scSpecTypeMap ( ) . value : {
315
+ let map = ty . value ( ) as xdr . ScSpecTypeMap ;
316
+ let keyType = map . keyType ( ) ;
317
+ let valueType = map . valueType ( ) ;
318
+ return xdr . ScVal . scvMap (
319
+ val . map ( ( entry ) => {
320
+ let key = this . nativeToScVal ( entry [ 0 ] , keyType ) ;
321
+ let val = this . nativeToScVal ( entry [ 1 ] , valueType ) ;
322
+ return new xdr . ScMapEntry ( { key, val } ) ;
323
+ } )
324
+ ) ;
325
+ }
326
+
327
+ default :
328
+ throw new TypeError (
329
+ `Type ${ ty } was not vec, but value was Array`
330
+ ) ;
292
331
}
293
332
}
294
333
if ( val . constructor === Map ) {
@@ -518,7 +557,7 @@ export class ContractSpec {
518
557
case xdr . ScValType . scvI128 ( ) . value :
519
558
case xdr . ScValType . scvU256 ( ) . value :
520
559
case xdr . ScValType . scvI256 ( ) . value :
521
- return scValToBigInt ( scv ) as T ;
560
+ return ( scValToBigInt ( scv ) as T ) ;
522
561
523
562
case xdr . ScValType . scvVec ( ) . value : {
524
563
if ( value == xdr . ScSpecType . scSpecTypeVec ( ) . value ) {
@@ -545,12 +584,12 @@ export class ContractSpec {
545
584
let type_ = typeDef . value ( ) as xdr . ScSpecTypeMap ;
546
585
let keyType = type_ . keyType ( ) ;
547
586
let valueType = type_ . valueType ( ) ;
548
- return new Map (
549
- map . map ( ( entry ) => [
550
- this . scValToNative ( entry . key ( ) , keyType ) ,
551
- this . scValToNative ( entry . val ( ) , valueType ) ,
552
- ] )
553
- ) as T ;
587
+
588
+ let res = map . map ( ( entry ) => [
589
+ this . scValToNative ( entry . key ( ) , keyType ) ,
590
+ this . scValToNative ( entry . val ( ) , valueType ) ,
591
+ ] ) as T ;
592
+ return res ;
554
593
}
555
594
throw new TypeError (
556
595
`ScSpecType ${ t . name } was not map, but ${ JSON . stringify (
@@ -773,20 +812,6 @@ function findCase(name: string) {
773
812
}
774
813
775
814
const PRIMITIVE_DEFINITONS = {
776
- U64 : {
777
- type : "string" ,
778
- pattern : "^[0-9]+$" ,
779
- format : "bigint" ,
780
- minLength : 1 ,
781
- maxLength : 20 , // 64-bit max value has 20 digits
782
- } ,
783
- I64 : {
784
- type : "string" ,
785
- pattern : "^-?^[0-9]+$" ,
786
- format : "bigint" ,
787
- minLength : 1 ,
788
- maxLength : 21 , // Includes additional digit for the potential '-'
789
- } ,
790
815
U32 : {
791
816
type : "integer" ,
792
817
minimum : 0 ,
@@ -797,31 +822,39 @@ const PRIMITIVE_DEFINITONS = {
797
822
minimum : - 2147483648 ,
798
823
maximum : 2147483647 ,
799
824
} ,
825
+ U64 : {
826
+ type : "string" ,
827
+ pattern : "^([1-9][0-9]*|0)$" ,
828
+ minLength : 1 ,
829
+ maxLength : 20 , // 64-bit max value has 20 digits
830
+ } ,
831
+ I64 : {
832
+ type : "string" ,
833
+ pattern : "^(-?[1-9][0-9]*|0)$" ,
834
+ minLength : 1 ,
835
+ maxLength : 21 , // Includes additional digit for the potential '-'
836
+ } ,
800
837
U128 : {
801
838
type : "string" ,
802
- pattern : "^[0-9]+$" ,
803
- format : "bigint" ,
839
+ pattern : "^([1-9][0-9]*|0)$" ,
804
840
minLength : 1 ,
805
841
maxLength : 39 , // 128-bit max value has 39 digits
806
842
} ,
807
843
I128 : {
808
844
type : "string" ,
809
- pattern : "^-?[0-9]+$" ,
810
- format : "bigint" ,
845
+ pattern : "^(-?[1-9][0-9]*|0)$" ,
811
846
minLength : 1 ,
812
847
maxLength : 40 , // Includes additional digit for the potential '-'
813
848
} ,
814
849
U256 : {
815
850
type : "string" ,
816
- pattern : "^[0-9]+$" ,
817
- format : "bigint" ,
851
+ pattern : "^([1-9][0-9]*|0)$" ,
818
852
minLength : 1 ,
819
853
maxLength : 78 , // 256-bit max value has 78 digits
820
854
} ,
821
855
I256 : {
822
856
type : "string" ,
823
- pattern : "^-?[0-9]+$" ,
824
- format : "bigint" ,
857
+ pattern : "^(-?[1-9][0-9]*|0)$" ,
825
858
minLength : 1 ,
826
859
maxLength : 79 , // Includes additional digit for the potential '-'
827
860
} ,
@@ -945,13 +978,15 @@ function typeRef(typeDef: xdr.ScSpecTypeDef): object {
945
978
}
946
979
case xdr . ScSpecType . scSpecTypeMap ( ) . value : {
947
980
let map = typeDef . value ( ) as xdr . ScSpecTypeMap ;
948
- let ref = typeRef ( map . valueType ( ) ) ;
981
+ let items = [ typeRef ( map . keyType ( ) ) , typeRef ( map . valueType ( ) ) ] ;
949
982
return {
950
- type : "object" ,
951
- patternProperties : {
952
- "^[a-zA-Z0-9]+$" : ref ,
983
+ type : "array" ,
984
+ items : {
985
+ type : "array" ,
986
+ items,
987
+ minItems : 2 ,
988
+ maxItems : 2 ,
953
989
} ,
954
- additionalProperties : false ,
955
990
} ;
956
991
}
957
992
case xdr . ScSpecType . scSpecTypeTuple ( ) . value : {
0 commit comments