forked from algorand/js-algorand-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.ABI.ts
More file actions
577 lines (550 loc) · 16.8 KB
/
10.ABI.ts
File metadata and controls
577 lines (550 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* eslint-env mocha */
import assert from 'assert';
import {
ABIMethod,
AtomicTransactionComposer,
AtomicTransactionComposerStatus,
MultisigMetadata,
generateAccount,
makeBasicAccountTransactionSigner,
makeMultiSigAccountTransactionSigner,
makePaymentTxnWithSuggestedParams,
} from '../src';
import {
ABIAddressType,
ABIArrayDynamicType,
ABIArrayStaticType,
ABIBoolType,
ABIByteType,
ABIStringType,
ABITupleType,
ABIType,
ABIUfixedType,
ABIUintType,
ABIValue,
} from '../src/abi/abi_type';
import { decodeAddress } from '../src/encoding/address';
describe('ABI type checking', () => {
it('should create the correct type from the string', () => {
for (let i = 8; i < 513; i += 8) {
let expected = new ABIUintType(i);
let actual = ABIType.from(`uint${i}`);
assert.deepStrictEqual(actual, expected);
for (let j = 1; j < 161; j++) {
expected = new ABIUfixedType(i, j);
actual = ABIType.from(`ufixed${i}x${j}`);
assert.deepStrictEqual(actual, expected);
}
}
const testCases = [
['address', new ABIAddressType()],
['bool', new ABIBoolType()],
['byte', new ABIByteType()],
['string', new ABIStringType()],
['uint32[]', new ABIArrayDynamicType(new ABIUintType(32))],
[
'byte[][]',
new ABIArrayDynamicType(new ABIArrayDynamicType(new ABIByteType())),
],
['ufixed256x64[]', new ABIArrayDynamicType(new ABIUfixedType(256, 64))],
[
'ufixed128x10[100]',
new ABIArrayStaticType(new ABIUfixedType(128, 10), 100),
],
[
'bool[256][100]',
new ABIArrayStaticType(
new ABIArrayStaticType(new ABIBoolType(), 256),
100
),
],
['()', new ABITupleType([])],
[
'(uint16,(byte,address[10]))',
new ABITupleType([
new ABIUintType(16),
new ABITupleType([
new ABIByteType(),
new ABIArrayStaticType(new ABIAddressType(), 10),
]),
]),
],
[
'(uint256,(byte,address[10]),(),bool)',
new ABITupleType([
new ABIUintType(256),
new ABITupleType([
new ABIByteType(),
new ABIArrayStaticType(new ABIAddressType(), 10),
]),
new ABITupleType([]),
new ABIBoolType(),
]),
],
[
'(ufixed256x16,((string),bool,(address,uint8)))',
new ABITupleType([
new ABIUfixedType(256, 16),
new ABITupleType([
new ABITupleType([new ABIStringType()]),
new ABIBoolType(),
new ABITupleType([new ABIAddressType(), new ABIUintType(8)]),
]),
]),
],
];
for (const testCase of testCases) {
const actual = ABIType.from(testCase[0] as string);
assert.deepStrictEqual(actual, testCase[1]);
}
});
it('should fail for an invalid bit size or precision', () => {
const invalidSizes = [-1, 0, 9, 513, 1024];
const invalidPrecisions = [-1, 0, 161];
for (const size of invalidSizes) {
assert.throws(() => new ABIUintType(size));
assert.throws(() => new ABIUfixedType(size, 10));
}
for (const precision of invalidPrecisions) {
assert.throws(() => new ABIUfixedType(8, precision));
}
});
it('should fail for an invalid type string', () => {
const testCases = [
// uint
'uint 8',
'uint8 ',
'uint123x345',
'uint!8',
'uint[32]',
'uint-893',
'uint#120\\',
// ufixed
'ufixed000000000016x0000010',
'ufixed123x345',
'ufixed 128 x 100',
'ufixed64x10 ',
'ufixed!8x2 ',
'ufixed[32]x16',
'ufixed-64x+100',
'ufixed16x+12',
// dynamic array
'byte[] ',
'[][][]',
'stuff[]',
// static array
'bool[01]',
'byte[10 ]',
'uint64[0x21]',
// tuple
'(ufixed128x10))',
'(,uint128,byte[])',
'(address,ufixed64x5,)',
'(byte[16],somethingwrong)',
'( )',
'((uint32)',
'(byte,,byte)',
'((byte),,(byte))',
];
for (const testCase of testCases) {
assert.throws(() => ABIType.from(testCase));
}
});
it('should properly return whether the type is dynamic', () => {
const testCases = [
[new ABIUintType(8).isDynamic(), false],
[new ABIUfixedType(16, 10).isDynamic(), false],
[new ABIByteType().isDynamic(), false],
[new ABIBoolType().isDynamic(), false],
[new ABIAddressType().isDynamic(), false],
[new ABIStringType().isDynamic(), true],
[new ABIArrayDynamicType(new ABIBoolType()).isDynamic(), true],
[
new ABIArrayDynamicType(
new ABIArrayDynamicType(new ABIByteType())
).isDynamic(),
true,
],
[ABIType.from('(string[100])').isDynamic(), true],
[ABIType.from('(address,bool,uint256)').isDynamic(), false],
[ABIType.from('(uint8,(byte[10]))').isDynamic(), false],
[ABIType.from('(string,uint256)').isDynamic(), true],
[ABIType.from('(bool,(ufixed16x10[],(byte,address)))').isDynamic(), true],
[
ABIType.from('(bool,(uint256,(byte,address,string)))').isDynamic(),
true,
],
];
for (const testCase of testCases) {
const actual = testCase[0];
const expected = testCase[1];
assert.deepStrictEqual(actual, expected);
}
});
it('should properly return the byte length of the type', () => {
const testCases = [
[new ABIAddressType().byteLen(), 32],
[new ABIByteType().byteLen(), 1],
[new ABIBoolType().byteLen(), 1],
[new ABIUintType(64).byteLen(), 8],
[new ABIUfixedType(256, 50).byteLen(), 32],
[ABIType.from('bool[81]').byteLen(), 11],
[ABIType.from('bool[80]').byteLen(), 10],
[ABIType.from('bool[88]').byteLen(), 11],
[ABIType.from('address[5]').byteLen(), 160],
[ABIType.from('uint16[20]').byteLen(), 40],
[ABIType.from('ufixed64x20[10]').byteLen(), 80],
// [ABIType.from('(address,byte,ufixed16x20)').byteLen(), 35],
[
ABIType.from(
'((bool,address[10]),(bool,bool,bool),uint8[20])'
).byteLen(),
342,
],
[ABIType.from('(bool,bool)').byteLen(), 1],
[ABIType.from(`(${'bool,'.repeat(6)}uint8)`).byteLen(), 2],
[
ABIType.from(
`(${'bool,'.repeat(10)}uint8,${'bool,'.repeat(10)}byte)`
).byteLen(),
6,
],
];
for (const testCase of testCases) {
const actual = testCase[0];
const expected = testCase[1];
assert.deepStrictEqual(actual, expected);
}
// Dynamic types should not have a byte length
assert.throws(() => new ABIStringType().byteLen());
assert.throws(() => new ABIArrayDynamicType(new ABIBoolType()).byteLen());
});
});
describe('ABI encoding', () => {
type TestCase<T> = {
abiType: ABIType;
input: T;
expectedEncoding: Uint8Array;
};
function newTestCase<T>(a: ABIType, b: T, c: Uint8Array): TestCase<T> {
return {
abiType: a,
input: b,
expectedEncoding: c,
};
}
[
newTestCase(new ABIUintType(8), BigInt(0), new Uint8Array([0])),
newTestCase(new ABIUintType(16), BigInt(3), new Uint8Array([0, 3])),
newTestCase(
new ABIUintType(64),
256,
new Uint8Array([0, 0, 0, 0, 0, 0, 1, 0])
),
newTestCase(new ABIUfixedType(8, 30), BigInt(255), new Uint8Array([255])),
newTestCase(new ABIUfixedType(32, 10), 33, new Uint8Array([0, 0, 0, 33])),
newTestCase(
new ABIAddressType(),
'MO2H6ZU47Q36GJ6GVHUKGEBEQINN7ZWVACMWZQGIYUOE3RBSRVYHV4ACJI',
decodeAddress(
'MO2H6ZU47Q36GJ6GVHUKGEBEQINN7ZWVACMWZQGIYUOE3RBSRVYHV4ACJI'
).publicKey
),
newTestCase(
new ABIStringType(),
'What’s new',
new Uint8Array([
0,
12,
87,
104,
97,
116,
226,
128,
153,
115,
32,
110,
101,
119,
])
),
newTestCase(
new ABIStringType(),
'😅🔨',
new Uint8Array([0, 8, 240, 159, 152, 133, 240, 159, 148, 168])
),
newTestCase(new ABIByteType(), 10, new Uint8Array([10])),
newTestCase(new ABIByteType(), 255, new Uint8Array([255])),
newTestCase(new ABIBoolType(), true, new Uint8Array([128])),
newTestCase(new ABIBoolType(), false, new Uint8Array([0])),
newTestCase(
new ABIStringType(),
'asdf',
new Uint8Array([0, 4, 97, 115, 100, 102])
),
newTestCase(
new ABIArrayStaticType(new ABIBoolType(), 3),
[true, true, false],
new Uint8Array([192])
),
newTestCase(
new ABIArrayStaticType(new ABIBoolType(), 8),
[false, true, false, false, false, false, false, false],
new Uint8Array([64])
),
newTestCase(
new ABIArrayStaticType(new ABIBoolType(), 8),
[true, true, true, true, true, true, true, true],
new Uint8Array([255])
),
newTestCase(
new ABIArrayStaticType(new ABIBoolType(), 9),
[true, false, false, true, false, false, true, false, true],
new Uint8Array([146, 128])
),
newTestCase(
new ABIArrayStaticType(new ABIUintType(64), 3),
[BigInt(1), BigInt(2), 3], // Deliberately mix BigInt and int
new Uint8Array([
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
2,
0,
0,
0,
0,
0,
0,
0,
3,
])
),
newTestCase(
new ABIArrayDynamicType(new ABIBoolType()),
[],
new Uint8Array([0, 0])
),
newTestCase(
new ABIArrayDynamicType(new ABIBoolType()),
[true, true, false],
new Uint8Array([0, 3, 192])
),
newTestCase(
new ABIArrayDynamicType(new ABIBoolType()),
[false, true, false, false, false, false, false, false],
new Uint8Array([0, 8, 64])
),
newTestCase(
new ABIArrayDynamicType(new ABIBoolType()),
[true, false, false, true, false, false, true, false, true],
new Uint8Array([0, 9, 146, 128])
),
newTestCase(ABIType.from('()'), [], new Uint8Array([])),
// 2^6 + 2^5 = 64 + 32 = 96
newTestCase(
ABIType.from('(bool,bool,bool)'),
[false, true, true],
new Uint8Array([96])
),
newTestCase(
ABIType.from('(bool[3])'),
[[false, true, true]],
new Uint8Array([96])
),
newTestCase(
ABIType.from('(bool[])'),
[[false, true, true]],
new Uint8Array([0, 2, 0, 3, 96])
),
newTestCase(
ABIType.from('(bool[2],bool[])'),
[
[true, true],
[true, true],
],
new Uint8Array([192, 0, 3, 0, 2, 192])
),
newTestCase(
ABIType.from('(bool[],bool[])'),
[[], []],
new Uint8Array([0, 4, 0, 6, 0, 0, 0, 0])
),
newTestCase(
ABIType.from('(string,bool,bool,bool,bool,string)'),
['AB', true, false, true, false, 'DE'],
new Uint8Array([0, 5, 160, 0, 9, 0, 2, 65, 66, 0, 2, 68, 69])
),
newTestCase(
new ABITupleType([new ABIUintType(8), new ABIUintType(16)]),
[1, 2],
new Uint8Array([1, 0, 2])
),
].forEach((testCase) => {
it(`should round-trip ${testCase.abiType}, ${testCase.input}`, () => {
const encoded = testCase.abiType.encode(testCase.input);
assert.deepStrictEqual(encoded, testCase.expectedEncoding);
const decoded = testCase.abiType.decode(encoded);
// Converts any numeric type to BigInt for strict equality comparisons.
// The conversion is required because there's no type information
// available to convert a decoded BigInt back to its original number
// form. Converting from number to BigInt is always _safe_.
function numericAsBigInt(d: ABIValue): ABIValue {
if (typeof d === 'number') {
return BigInt(d);
}
if (d instanceof Array) {
return (d as ABIValue[]).map(numericAsBigInt);
}
return d;
}
// Returns true when the provided ABIType decodes to BigInt.
function decodeReturnsBigInt(t: ABIType): boolean {
if (t instanceof ABIUintType || t instanceof ABIUfixedType) {
return true;
}
if (t instanceof ABITupleType) {
return t.childTypes.map(decodeReturnsBigInt).includes(true);
}
if (t instanceof ABIArrayStaticType) {
return decodeReturnsBigInt(t.childType);
}
if (t instanceof ABIArrayDynamicType) {
return decodeReturnsBigInt(t.childType);
}
return false;
}
if (decodeReturnsBigInt(testCase.abiType)) {
// If casting to BigInt changes the test case input, then it implies
// the _unchanged_ test case input != `decoded`.
//
// The sanity check confirms that transforming the expected value is
// necessary.
if (testCase.input !== numericAsBigInt(testCase.input)) {
assert.notDeepStrictEqual(decoded, testCase.input);
}
assert.deepStrictEqual(decoded, numericAsBigInt(testCase.input));
} else {
assert.deepStrictEqual(decoded, testCase.input);
}
});
});
it('should fail for bad values during encoding', () => {
assert.throws(() => new ABIUintType(8).encode(BigInt(-1)));
assert.throws(() => new ABIUintType(512).encode(BigInt(2 ** 512)));
assert.throws(() => new ABIUfixedType(512, 10).encode(BigInt(-1)));
assert.throws(() => new ABIByteType().encode(-1));
assert.throws(() => new ABIByteType().encode(256));
assert.throws(() => new ABIAddressType().encode('BADADDRESS'));
assert.throws(() =>
new ABIArrayStaticType(new ABIBoolType(), 3).encode([true])
);
assert.throws(() =>
new ABIArrayStaticType(new ABIStringType(), 1).encode([true])
);
assert.throws(() =>
new ABIArrayStaticType(new ABIUintType(256), 1).encode(['hello'])
);
assert.throws(() =>
new ABIArrayDynamicType(new ABIAddressType()).encode([false])
);
assert.throws(() =>
new ABITupleType([new ABIBoolType(), new ABIUfixedType(128, 20)]).encode([
BigInt(3),
true,
])
);
});
it('should properly accept foreign array objects in the ATC addMethodCall', () => {
const composer = new AtomicTransactionComposer();
const method = ABIMethod.fromSignature('add(application)uint8');
const account = generateAccount();
const sender = 'DN7MBMCL5JQ3PFUQS7TMX5AH4EEKOBJVDUF4TCV6WERATKFLQF4MQUPZTA';
const sp = {
fee: 1000,
firstRound: 1,
lastRound: 1001,
genesisID: 'gi',
genesisHash: 'gh',
};
const foreignAcct =
'E4VCHISDQPLIZWMALIGNPK2B2TERPDMR64MZJXE3UL75MUDXZMADX5OWXM';
// Create method call using ATC.
// The foreign apps array argument should be packed before the method argument.
composer.addMethodCall({
appID: 7,
method,
sender,
suggestedParams: sp,
methodArgs: [2],
appAccounts: [foreignAcct],
appForeignApps: [1],
appForeignAssets: [124],
signer: makeBasicAccountTransactionSigner(account),
});
assert.deepStrictEqual(
composer.getStatus(),
AtomicTransactionComposerStatus.BUILDING
);
assert.deepStrictEqual(composer.count(), 1);
// The built group should have one txn.
const txns = composer.buildGroup();
// eslint-disable-next-line prefer-destructuring
const txn = txns[0].txn;
// Assert that foreign objects were passed in and ordering was correct.
assert.deepStrictEqual(txn.appForeignApps?.length, 2);
assert.deepStrictEqual(txn.appForeignApps[0], 1);
assert.deepStrictEqual(txn.appForeignApps[1], 2);
assert.deepStrictEqual(txn.appForeignAssets?.length, 1);
assert.deepStrictEqual(txn.appForeignAssets[0], 124);
assert.deepStrictEqual(txn.appAccounts?.length, 1);
assert.deepStrictEqual(txn.appAccounts[0], decodeAddress(foreignAcct));
});
it('should accept at least one signature in the multisig', () => {
const account1 = generateAccount();
const account2 = generateAccount();
// Create a multisig signer
const msig: MultisigMetadata = {
version: 1,
threshold: 1,
addrs: [account1.addr, account2.addr],
};
const sks: Uint8Array[] = [account1.sk];
const signer = makeMultiSigAccountTransactionSigner(msig, sks);
// Create a transaction
const suggestedParams = {
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisID: '',
firstRound: 0,
lastRound: 1000,
fee: 1000,
flatFee: true,
};
const actualTxn = makePaymentTxnWithSuggestedParams(
account1.addr,
account2.addr,
1000,
undefined,
undefined,
suggestedParams
);
// A multisig with 1 signature should be accepted
signer([actualTxn], [0]).then((signedTxns) => {
assert.deepStrictEqual(signedTxns.length, 1);
});
});
});