@@ -24,14 +24,20 @@ import {
24
24
BITS_IN_BYTE ,
25
25
UINT64_SIZE ,
26
26
} from '../constants'
27
+ import { lazyContext } from '../context-helpers/internal-context'
27
28
import { fromBytes , TypeInfo } from '../encoders'
28
29
import { DeliberateAny } from '../typescript-helpers'
29
- import { asBigUint , asBigUintCls , asBytesCls , asUint64 , asUint8Array , conactUint8Arrays , uint8ArrayToNumber } from '../util'
30
+ import { asBigInt , asBigUint , asBigUintCls , asBytesCls , asUint64 , asUint8Array , conactUint8Arrays , uint8ArrayToNumber } from '../util'
31
+ import { AccountCls } from './account'
32
+ import { ApplicationCls } from './application'
33
+ import { AssetCls } from './asset'
34
+ import { ApplicationTransaction } from './transactions'
30
35
31
36
const ABI_LENGTH_SIZE = 2
32
37
const maxBigIntValue = ( bitSize : number ) => 2n ** BigInt ( bitSize ) - 1n
33
38
const maxBytesLength = ( bitSize : number ) => Math . floor ( bitSize / BITS_IN_BYTE )
34
39
const encodeLength = ( length : number ) => new internal . primitives . BytesCls ( encodingUtil . bigIntToUint8Array ( BigInt ( length ) , ABI_LENGTH_SIZE ) )
40
+
35
41
type CompatForArc4Int < N extends BitSize > = N extends 8 | 16 | 32 | 64 ? Uint64Compat : BigUintCompat
36
42
export class UintNImpl < N extends BitSize > extends UintN < N > {
37
43
private value : Uint8Array
@@ -99,7 +105,7 @@ export class UFixedNxMImpl<N extends BitSize, M extends number> extends UFixedNx
99
105
private value : Uint8Array
100
106
private bitSize : N
101
107
private precision : M
102
- private typeInfo : TypeInfo
108
+ typeInfo : TypeInfo
103
109
104
110
constructor ( typeInfo : TypeInfo | string , v : `${number } .${number } `) {
105
111
super ( v )
@@ -163,7 +169,10 @@ export class UFixedNxMImpl<N extends BitSize, M extends number> extends UFixedNx
163
169
export class ByteImpl extends Byte {
164
170
private value : UintNImpl < 8 >
165
171
166
- constructor ( typeInfo : TypeInfo | string , v ?: CompatForArc4Int < 8 > ) {
172
+ constructor (
173
+ public typeInfo : TypeInfo | string ,
174
+ v ?: CompatForArc4Int < 8 > ,
175
+ ) {
167
176
super ( v )
168
177
this . value = new UintNImpl < 8 > ( typeInfo , v )
169
178
}
@@ -202,7 +211,10 @@ export class ByteImpl extends Byte {
202
211
export class StrImpl extends Str {
203
212
private value : Uint8Array
204
213
205
- constructor ( _typeInfo : TypeInfo | string , s ?: StringCompat ) {
214
+ constructor (
215
+ public typeInfo : TypeInfo | string ,
216
+ s ?: StringCompat ,
217
+ ) {
206
218
super ( )
207
219
const bytesValue = asBytesCls ( s ?? '' )
208
220
const bytesLength = encodeLength ( bytesValue . length . asNumber ( ) )
@@ -244,7 +256,10 @@ const FALSE_BIGINT_VALUE = 0n
244
256
export class BoolImpl extends Bool {
245
257
private value : Uint8Array
246
258
247
- constructor ( _typeInfo : TypeInfo | string , v ?: boolean ) {
259
+ constructor (
260
+ public typeInfo : TypeInfo | string ,
261
+ v ?: boolean ,
262
+ ) {
248
263
super ( v )
249
264
this . value = encodingUtil . bigIntToUint8Array ( v ? TRUE_BIGINT_VALUE : FALSE_BIGINT_VALUE , 1 )
250
265
}
@@ -320,8 +335,8 @@ const arrayProxyHandler = <TItem>() => ({
320
335
export class StaticArrayImpl < TItem extends ARC4Encoded , TLength extends number > extends StaticArray < TItem , TLength > {
321
336
private value ?: TItem [ ]
322
337
private uint8ArrayValue ?: Uint8Array
323
- private typeInfo : TypeInfo
324
338
private size : number
339
+ typeInfo : TypeInfo
325
340
genericArgs : StaticArrayGenericArgs
326
341
327
342
constructor ( typeInfo : TypeInfo | string , ...items : TItem [ ] & { length : TLength } )
@@ -383,6 +398,10 @@ export class StaticArrayImpl<TItem extends ARC4Encoded, TLength extends number>
383
398
return StaticArrayImpl . fromBytesImpl ( this . bytes , JSON . stringify ( this . typeInfo ) ) as StaticArrayImpl < TItem , TLength >
384
399
}
385
400
401
+ get native ( ) : TItem [ ] {
402
+ return this . items
403
+ }
404
+
386
405
static fromBytesImpl (
387
406
value : internal . primitives . StubBytesCompat | Uint8Array ,
388
407
typeInfo : string | TypeInfo ,
@@ -425,7 +444,7 @@ export class StaticArrayImpl<TItem extends ARC4Encoded, TLength extends number>
425
444
}
426
445
427
446
export class AddressImpl extends Address {
428
- private typeInfo : TypeInfo
447
+ typeInfo : TypeInfo
429
448
private value : StaticArrayImpl < ByteImpl , 32 >
430
449
431
450
constructor ( typeInfo : TypeInfo | string , value ?: Account | string | bytes ) {
@@ -499,7 +518,7 @@ const readLength = (value: Uint8Array): readonly [number, Uint8Array] => {
499
518
export class DynamicArrayImpl < TItem extends ARC4Encoded > extends DynamicArray < TItem > {
500
519
private value ?: TItem [ ]
501
520
private uint8ArrayValue ?: Uint8Array
502
- private typeInfo : TypeInfo
521
+ typeInfo : TypeInfo
503
522
genericArgs : DynamicArrayGenericArgs
504
523
505
524
constructor ( typeInfo : TypeInfo | string , ...items : TItem [ ] ) {
@@ -554,6 +573,10 @@ export class DynamicArrayImpl<TItem extends ARC4Encoded> extends DynamicArray<TI
554
573
return DynamicArrayImpl . fromBytesImpl ( this . bytes , JSON . stringify ( this . typeInfo ) ) as DynamicArrayImpl < TItem >
555
574
}
556
575
576
+ get native ( ) : TItem [ ] {
577
+ return this . items
578
+ }
579
+
557
580
push ( ...values : TItem [ ] ) {
558
581
const items = this . items
559
582
items . push ( ...values )
@@ -594,7 +617,7 @@ export class DynamicArrayImpl<TItem extends ARC4Encoded> extends DynamicArray<TI
594
617
export class TupleImpl < TTuple extends [ ARC4Encoded , ...ARC4Encoded [ ] ] > extends Tuple < TTuple > {
595
618
private value ?: TTuple
596
619
private uint8ArrayValue ?: Uint8Array
597
- private typeInfo : TypeInfo
620
+ typeInfo : TypeInfo
598
621
genericArgs : TypeInfo [ ]
599
622
600
623
constructor ( typeInfo : TypeInfo | string )
@@ -691,7 +714,6 @@ export class TupleImpl<TTuple extends [ARC4Encoded, ...ARC4Encoded[]]> extends T
691
714
type StructConstraint = Record < string , ARC4Encoded >
692
715
export class StructImpl < T extends StructConstraint > extends ( Struct < StructConstraint > as DeliberateAny ) {
693
716
private uint8ArrayValue ?: Uint8Array
694
- private typeInfo : TypeInfo
695
717
genericArgs : Record < string , TypeInfo >
696
718
697
719
constructor ( typeInfo : TypeInfo | string , value : T = { } as T ) {
@@ -737,6 +759,10 @@ export class StructImpl<T extends StructConstraint> extends (Struct<StructConstr
737
759
return result as T
738
760
}
739
761
762
+ get native ( ) : T {
763
+ return this . items
764
+ }
765
+
740
766
private decodeAsProperties ( ) {
741
767
if ( this . uint8ArrayValue ) {
742
768
const values = decode ( this . uint8ArrayValue , Object . values ( this . genericArgs ) )
@@ -769,7 +795,7 @@ export class StructImpl<T extends StructConstraint> extends (Struct<StructConstr
769
795
}
770
796
771
797
export class DynamicBytesImpl extends DynamicBytes {
772
- private typeInfo : TypeInfo
798
+ typeInfo : TypeInfo
773
799
private value : DynamicArrayImpl < ByteImpl >
774
800
775
801
constructor ( typeInfo : TypeInfo | string , value ?: bytes | string ) {
@@ -825,7 +851,7 @@ export class DynamicBytesImpl extends DynamicBytes {
825
851
826
852
export class StaticBytesImpl extends StaticBytes {
827
853
private value : StaticArrayImpl < ByteImpl , number >
828
- private typeInfo : TypeInfo
854
+ typeInfo : TypeInfo
829
855
830
856
constructor ( typeInfo : TypeInfo | string , value ?: bytes | string ) {
831
857
super ( value )
@@ -1161,3 +1187,85 @@ export const getArc4TypeName = (typeInfo: TypeInfo): string | undefined => {
1161
1187
else if ( typeof name === 'function' ) return name ( typeInfo )
1162
1188
return undefined
1163
1189
}
1190
+
1191
+ export function decodeArc4Impl < T > ( sourceTypeInfoString : string , bytes : internal . primitives . StubBytesCompat ) : T {
1192
+ const sourceTypeInfo = JSON . parse ( sourceTypeInfoString )
1193
+ const encoder = getArc4Encoder ( sourceTypeInfo )
1194
+ const source = encoder ( bytes , sourceTypeInfo )
1195
+
1196
+ return getNativeValue ( source ) as T
1197
+ }
1198
+
1199
+ export function encodeArc4Impl < T > ( _targetTypeInfoString : string , source : T ) : bytes {
1200
+ const arc4Encoded = getArc4Encoded ( source )
1201
+ return arc4Encoded . bytes
1202
+ }
1203
+
1204
+ const getNativeValue = ( value : DeliberateAny ) : DeliberateAny => {
1205
+ const native = ( value as DeliberateAny ) . native
1206
+ if ( Array . isArray ( native ) ) {
1207
+ return native . map ( ( item ) => getNativeValue ( item ) )
1208
+ } else if ( native instanceof internal . primitives . AlgoTsPrimitiveCls ) {
1209
+ return native
1210
+ } else if ( typeof native === 'object' ) {
1211
+ return Object . fromEntries ( Object . entries ( native ) . map ( ( [ key , value ] ) => [ key , getNativeValue ( value ) ] ) )
1212
+ }
1213
+ return native
1214
+ }
1215
+
1216
+ const getArc4Encoded = ( value : DeliberateAny ) : ARC4Encoded => {
1217
+ if ( value instanceof ARC4Encoded ) {
1218
+ return value
1219
+ }
1220
+ if ( value instanceof AccountCls ) {
1221
+ const index = ( lazyContext . activeGroup . activeTransaction as ApplicationTransaction ) . apat . indexOf ( value )
1222
+ return new UintNImpl ( { name : 'UintN<64>' , genericArgs : [ { name : '64' } ] } , asBigInt ( index ) )
1223
+ }
1224
+ if ( value instanceof AssetCls ) {
1225
+ const index = ( lazyContext . activeGroup . activeTransaction as ApplicationTransaction ) . apas . indexOf ( value )
1226
+ return new UintNImpl ( { name : 'UintN<64>' , genericArgs : [ { name : '64' } ] } , asBigInt ( index ) )
1227
+ }
1228
+ if ( value instanceof ApplicationCls ) {
1229
+ const index = ( lazyContext . activeGroup . activeTransaction as ApplicationTransaction ) . apfa . indexOf ( value )
1230
+ return new UintNImpl ( { name : 'UintN<64>' , genericArgs : [ { name : '64' } ] } , asBigInt ( index ) )
1231
+ }
1232
+ if ( typeof value === 'boolean' ) {
1233
+ return new BoolImpl ( { name : 'Bool' } , value )
1234
+ }
1235
+ if ( value instanceof internal . primitives . Uint64Cls || typeof value === 'number' ) {
1236
+ return new UintNImpl ( { name : 'UintN<64>' , genericArgs : [ { name : '64' } ] } , asBigInt ( value ) )
1237
+ }
1238
+ if ( value instanceof internal . primitives . BigUintCls ) {
1239
+ return new UintNImpl ( { name : 'UintN<512>' , genericArgs : [ { name : '512' } ] } , value . asBigInt ( ) )
1240
+ }
1241
+ if ( typeof value === 'bigint' ) {
1242
+ return new UintNImpl ( { name : 'UintN<512>' , genericArgs : [ { name : '512' } ] } , value )
1243
+ }
1244
+ if ( value instanceof internal . primitives . BytesCls ) {
1245
+ return new DynamicBytesImpl (
1246
+ { name : 'DynamicBytes' , genericArgs : { elementType : { name : 'Byte' , genericArgs : [ { name : '8' } ] } } } ,
1247
+ value . asAlgoTs ( ) ,
1248
+ )
1249
+ }
1250
+ if ( typeof value === 'string' ) {
1251
+ return new StrImpl ( { name : 'Str' } , value )
1252
+ }
1253
+ if ( Array . isArray ( value ) ) {
1254
+ const result : ARC4Encoded [ ] = value . reduce ( ( acc : ARC4Encoded [ ] , cur : DeliberateAny ) => {
1255
+ return acc . concat ( getArc4Encoded ( cur ) )
1256
+ } , [ ] )
1257
+ const genericArgs : TypeInfo [ ] = result . map ( ( x ) => ( x as DeliberateAny ) . typeInfo )
1258
+ const typeInfo = { name : `Tuple<[${ genericArgs . map ( ( x ) => x . name ) . join ( ',' ) } ]>` , genericArgs }
1259
+ return new TupleImpl ( typeInfo , ...( result as [ ] ) )
1260
+ }
1261
+ if ( typeof value === 'object' ) {
1262
+ const result = Object . values ( value ) . reduce ( ( acc : ARC4Encoded [ ] , cur : DeliberateAny ) => {
1263
+ return acc . concat ( getArc4Encoded ( cur ) )
1264
+ } , [ ] )
1265
+ const genericArgs : TypeInfo [ ] = result . map ( ( x ) => ( x as DeliberateAny ) . typeInfo )
1266
+ const typeInfo = { name : 'Struct' , genericArgs : Object . fromEntries ( Object . keys ( value ) . map ( ( x , i ) => [ x , genericArgs [ i ] ] ) ) }
1267
+ return new StructImpl ( typeInfo , Object . fromEntries ( Object . keys ( value ) . map ( ( x , i ) => [ x , result [ i ] ] ) ) )
1268
+ }
1269
+
1270
+ throw internal . errors . codeError ( `Unsupported type for encoding: ${ typeof value } ` )
1271
+ }
0 commit comments