algosdk 1.11.0 → 1.13.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/README.md +17 -6
  2. package/dist/browser/algosdk.min.js +1 -1
  3. package/dist/browser/algosdk.min.js.map +1 -1
  4. package/dist/cjs/src/abi/abi_type.js +693 -0
  5. package/dist/cjs/src/abi/abi_type.js.map +1 -0
  6. package/dist/cjs/src/abi/contract.js +27 -0
  7. package/dist/cjs/src/abi/contract.js.map +1 -0
  8. package/dist/cjs/src/abi/index.js +19 -0
  9. package/dist/cjs/src/abi/index.js.map +1 -0
  10. package/dist/cjs/src/abi/interface.js +23 -0
  11. package/dist/cjs/src/abi/interface.js.map +1 -0
  12. package/dist/cjs/src/abi/method.js +114 -0
  13. package/dist/cjs/src/abi/method.js.map +1 -0
  14. package/dist/cjs/src/abi/reference.js +25 -0
  15. package/dist/cjs/src/abi/reference.js.map +1 -0
  16. package/dist/cjs/src/abi/transaction.js +52 -0
  17. package/dist/cjs/src/abi/transaction.js.map +1 -0
  18. package/dist/cjs/src/client/baseHTTPClient.js +3 -0
  19. package/dist/cjs/src/client/baseHTTPClient.js.map +1 -0
  20. package/dist/cjs/src/client/client.js +165 -104
  21. package/dist/cjs/src/client/client.js.map +1 -1
  22. package/dist/cjs/src/client/urlTokenBaseHTTPClient.js +138 -0
  23. package/dist/cjs/src/client/urlTokenBaseHTTPClient.js.map +1 -0
  24. package/dist/cjs/src/client/v2/algod/algod.js +7 -2
  25. package/dist/cjs/src/client/v2/algod/algod.js.map +1 -1
  26. package/dist/cjs/src/client/v2/algod/models/types.js +18 -12
  27. package/dist/cjs/src/client/v2/algod/models/types.js.map +1 -1
  28. package/dist/cjs/src/client/v2/indexer/indexer.js +15 -2
  29. package/dist/cjs/src/client/v2/indexer/indexer.js.map +1 -1
  30. package/dist/cjs/src/client/v2/indexer/lookupApplicationLogs.js +48 -0
  31. package/dist/cjs/src/client/v2/indexer/lookupApplicationLogs.js.map +1 -0
  32. package/dist/cjs/src/client/v2/serviceClient.js +17 -8
  33. package/dist/cjs/src/client/v2/serviceClient.js.map +1 -1
  34. package/dist/cjs/src/composer.js +439 -0
  35. package/dist/cjs/src/composer.js.map +1 -0
  36. package/dist/cjs/src/encoding/address.js +14 -1
  37. package/dist/cjs/src/encoding/address.js.map +1 -1
  38. package/dist/cjs/src/encoding/bigint.js +38 -0
  39. package/dist/cjs/src/encoding/bigint.js.map +1 -0
  40. package/dist/cjs/src/logic/langspec.json +392 -77
  41. package/dist/cjs/src/logicsig.js +2 -2
  42. package/dist/cjs/src/logicsig.js.map +1 -1
  43. package/dist/cjs/src/main.js +17 -91
  44. package/dist/cjs/src/main.js.map +1 -1
  45. package/dist/cjs/src/makeTxn.js +29 -1
  46. package/dist/cjs/src/makeTxn.js.map +1 -1
  47. package/dist/cjs/src/multisig.js +77 -2
  48. package/dist/cjs/src/multisig.js.map +1 -1
  49. package/dist/cjs/src/signer.js +66 -0
  50. package/dist/cjs/src/signer.js.map +1 -0
  51. package/dist/cjs/src/transaction.js +53 -10
  52. package/dist/cjs/src/transaction.js.map +1 -1
  53. package/dist/cjs/src/types/intDecoding.js +0 -2
  54. package/dist/cjs/src/types/intDecoding.js.map +1 -1
  55. package/dist/cjs/src/types/transactions/base.js +0 -1
  56. package/dist/cjs/src/types/transactions/base.js.map +1 -1
  57. package/dist/cjs/src/wait.js +53 -0
  58. package/dist/cjs/src/wait.js.map +1 -0
  59. package/dist/esm/src/abi/abi_type.js +680 -0
  60. package/dist/esm/src/abi/abi_type.js.map +1 -0
  61. package/dist/esm/src/abi/contract.js +23 -0
  62. package/dist/esm/src/abi/contract.js.map +1 -0
  63. package/dist/esm/src/abi/index.js +7 -0
  64. package/dist/esm/src/abi/index.js.map +1 -0
  65. package/dist/esm/src/abi/interface.js +19 -0
  66. package/dist/esm/src/abi/interface.js.map +1 -0
  67. package/dist/esm/src/abi/method.js +110 -0
  68. package/dist/esm/src/abi/method.js.map +1 -0
  69. package/dist/esm/src/abi/reference.js +21 -0
  70. package/dist/esm/src/abi/reference.js.map +1 -0
  71. package/dist/esm/src/abi/transaction.js +47 -0
  72. package/dist/esm/src/abi/transaction.js.map +1 -0
  73. package/dist/esm/src/client/baseHTTPClient.js +2 -0
  74. package/dist/esm/src/client/baseHTTPClient.js.map +1 -0
  75. package/dist/esm/src/client/client.js +165 -101
  76. package/dist/esm/src/client/client.js.map +1 -1
  77. package/dist/esm/src/client/urlTokenBaseHTTPClient.js +112 -0
  78. package/dist/esm/src/client/urlTokenBaseHTTPClient.js.map +1 -0
  79. package/dist/esm/src/client/v2/algod/algod.js +7 -2
  80. package/dist/esm/src/client/v2/algod/algod.js.map +1 -1
  81. package/dist/esm/src/client/v2/algod/models/types.js +18 -12
  82. package/dist/esm/src/client/v2/algod/models/types.js.map +1 -1
  83. package/dist/esm/src/client/v2/indexer/indexer.js +15 -2
  84. package/dist/esm/src/client/v2/indexer/indexer.js.map +1 -1
  85. package/dist/esm/src/client/v2/indexer/lookupApplicationLogs.js +42 -0
  86. package/dist/esm/src/client/v2/indexer/lookupApplicationLogs.js.map +1 -0
  87. package/dist/esm/src/client/v2/serviceClient.js +17 -8
  88. package/dist/esm/src/client/v2/serviceClient.js.map +1 -1
  89. package/dist/esm/src/composer.js +435 -0
  90. package/dist/esm/src/composer.js.map +1 -0
  91. package/dist/esm/src/encoding/address.js +12 -0
  92. package/dist/esm/src/encoding/address.js.map +1 -1
  93. package/dist/esm/src/encoding/bigint.js +33 -0
  94. package/dist/esm/src/encoding/bigint.js.map +1 -0
  95. package/dist/esm/src/logic/langspec.json +392 -77
  96. package/dist/esm/src/logicsig.js +2 -2
  97. package/dist/esm/src/logicsig.js.map +1 -1
  98. package/dist/esm/src/main.js +7 -85
  99. package/dist/esm/src/main.js.map +1 -1
  100. package/dist/esm/src/makeTxn.js +27 -0
  101. package/dist/esm/src/makeTxn.js.map +1 -1
  102. package/dist/esm/src/multisig.js +73 -1
  103. package/dist/esm/src/multisig.js.map +1 -1
  104. package/dist/esm/src/signer.js +59 -0
  105. package/dist/esm/src/signer.js.map +1 -0
  106. package/dist/esm/src/transaction.js +53 -10
  107. package/dist/esm/src/transaction.js.map +1 -1
  108. package/dist/esm/src/types/intDecoding.js +0 -2
  109. package/dist/esm/src/types/intDecoding.js.map +1 -1
  110. package/dist/esm/src/types/transactions/base.js +0 -1
  111. package/dist/esm/src/types/transactions/base.js.map +1 -1
  112. package/dist/esm/src/wait.js +49 -0
  113. package/dist/esm/src/wait.js.map +1 -0
  114. package/dist/types/src/abi/abi_type.d.ts +102 -0
  115. package/dist/types/src/abi/contract.d.ts +21 -0
  116. package/dist/types/src/abi/index.d.ts +6 -0
  117. package/dist/types/src/abi/interface.d.ts +13 -0
  118. package/dist/types/src/abi/method.d.ts +39 -0
  119. package/dist/types/src/abi/reference.d.ts +15 -0
  120. package/dist/types/src/abi/transaction.d.ts +33 -0
  121. package/dist/types/src/client/baseHTTPClient.d.ts +41 -0
  122. package/dist/types/src/client/client.d.ts +67 -31
  123. package/dist/types/src/client/kmd.d.ts +1 -1
  124. package/dist/types/src/client/urlTokenBaseHTTPClient.d.ts +44 -0
  125. package/dist/types/src/client/v2/algod/algod.d.ts +8 -2
  126. package/dist/types/src/client/v2/algod/models/types.d.ts +29 -14
  127. package/dist/types/src/client/v2/indexer/indexer.d.ts +14 -2
  128. package/dist/types/src/client/v2/indexer/lookupApplicationLogs.d.ts +20 -0
  129. package/dist/types/src/client/v2/serviceClient.d.ts +4 -2
  130. package/dist/types/src/composer.d.ts +160 -0
  131. package/dist/types/src/encoding/address.d.ts +6 -0
  132. package/dist/types/src/encoding/bigint.d.ts +14 -0
  133. package/dist/types/src/main.d.ts +8 -48
  134. package/dist/types/src/makeTxn.d.ts +18 -1
  135. package/dist/types/src/multisig.d.ts +39 -1
  136. package/dist/types/src/signer.d.ts +40 -0
  137. package/dist/types/src/wait.d.ts +11 -0
  138. package/package.json +6 -4
@@ -0,0 +1,693 @@
1
+ "use strict";
2
+ /* eslint-disable no-bitwise */
3
+ /* eslint-disable no-use-before-define */
4
+ /* eslint-disable class-methods-use-this */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ABITupleType = exports.ABIArrayDynamicType = exports.ABIArrayStaticType = exports.ABIStringType = exports.ABIByteType = exports.ABIBoolType = exports.ABIAddressType = exports.ABIUfixedType = exports.ABIUintType = exports.ABIType = exports.LENGTH_ENCODE_BYTE_SIZE = exports.SINGLE_BOOL_SIZE = exports.SINGLE_BYTE_SIZE = exports.ADDR_BYTE_SIZE = exports.MAX_LEN = void 0;
7
+ /**
8
+ //ABI-Types: uint<N>: An N-bit unsigned integer (8 <= N <= 512 and N % 8 = 0).
9
+ // | byte (alias for uint8)
10
+ // | ufixed <N> x <M> (8 <= N <= 512, N % 8 = 0, and 0 < M <= 160)
11
+ // | bool
12
+ // | address (alias for byte[32])
13
+ // | <type> [<N>]
14
+ // | <type> []
15
+ // | string
16
+ // | (T1, ..., Tn)
17
+ */
18
+ const address_1 = require("../encoding/address");
19
+ const bigint_1 = require("../encoding/bigint");
20
+ const utils_1 = require("../utils/utils");
21
+ exports.MAX_LEN = 2 ** 16 - 1;
22
+ exports.ADDR_BYTE_SIZE = 32;
23
+ exports.SINGLE_BYTE_SIZE = 1;
24
+ exports.SINGLE_BOOL_SIZE = 1;
25
+ exports.LENGTH_ENCODE_BYTE_SIZE = 2;
26
+ const staticArrayRegexp = /^([a-z\d[\](),]+)\[([1-9][\d]*)]$/;
27
+ const ufixedRegexp = /^ufixed([1-9][\d]*)x([1-9][\d]*)$/;
28
+ class ABIType {
29
+ // De-serializes the ABI type from a string using the ABI specs
30
+ static from(str) {
31
+ if (str.endsWith('[]')) {
32
+ const arrayArgType = ABIType.from(str.slice(0, str.length - 2));
33
+ return new ABIArrayDynamicType(arrayArgType);
34
+ }
35
+ if (str.endsWith(']')) {
36
+ const stringMatches = str.match(staticArrayRegexp);
37
+ // Match the string itself, array element type, then array length
38
+ if (stringMatches.length !== 3) {
39
+ throw new Error(`malformed static array string: ${str}`);
40
+ }
41
+ // Parse static array using regex
42
+ const arrayLengthStr = stringMatches[2];
43
+ const arrayLength = parseInt(arrayLengthStr, 10);
44
+ if (arrayLength > exports.MAX_LEN) {
45
+ throw new Error(`array length exceeds limit ${exports.MAX_LEN}`);
46
+ }
47
+ // Parse the array element type
48
+ const arrayType = ABIType.from(stringMatches[1]);
49
+ return new ABIArrayStaticType(arrayType, arrayLength);
50
+ }
51
+ if (str.startsWith('uint')) {
52
+ // Checks if the parsed number contains only digits, no whitespaces
53
+ const digitsOnly = (string) => [...string].every((c) => '0123456789'.includes(c));
54
+ const typeSizeStr = str.slice(4, str.length);
55
+ if (!digitsOnly(typeSizeStr)) {
56
+ throw new Error(`malformed uint string: ${typeSizeStr}`);
57
+ }
58
+ const typeSize = parseInt(typeSizeStr, 10);
59
+ if (typeSize > exports.MAX_LEN) {
60
+ throw new Error(`malformed uint string: ${typeSize}`);
61
+ }
62
+ return new ABIUintType(typeSize);
63
+ }
64
+ if (str === 'byte') {
65
+ return new ABIByteType();
66
+ }
67
+ if (str.startsWith('ufixed')) {
68
+ const stringMatches = str.match(ufixedRegexp);
69
+ if (stringMatches.length !== 3) {
70
+ throw new Error(`malformed ufixed type: ${str}`);
71
+ }
72
+ const ufixedSize = parseInt(stringMatches[1], 10);
73
+ const ufixedPrecision = parseInt(stringMatches[2], 10);
74
+ return new ABIUfixedType(ufixedSize, ufixedPrecision);
75
+ }
76
+ if (str === 'bool') {
77
+ return new ABIBoolType();
78
+ }
79
+ if (str === 'address') {
80
+ return new ABIAddressType();
81
+ }
82
+ if (str === 'string') {
83
+ return new ABIStringType();
84
+ }
85
+ if (str.length >= 2 && str[0] === '(' && str[str.length - 1] === ')') {
86
+ const tupleContent = ABITupleType.parseTupleContent(str.slice(1, str.length - 1));
87
+ const tupleTypes = [];
88
+ for (let i = 0; i < tupleContent.length; i++) {
89
+ const ti = ABIType.from(tupleContent[i]);
90
+ tupleTypes.push(ti);
91
+ }
92
+ return new ABITupleType(tupleTypes);
93
+ }
94
+ throw new Error(`cannot convert a string ${str} to an ABI type`);
95
+ }
96
+ }
97
+ exports.ABIType = ABIType;
98
+ class ABIUintType extends ABIType {
99
+ constructor(size) {
100
+ super();
101
+ if (size % 8 !== 0 || size < 8 || size > 512) {
102
+ throw new Error(`unsupported uint type bitSize: ${size}`);
103
+ }
104
+ this.bitSize = size;
105
+ }
106
+ toString() {
107
+ return `uint${this.bitSize}`;
108
+ }
109
+ equals(other) {
110
+ return other instanceof ABIUintType && this.bitSize === other.bitSize;
111
+ }
112
+ isDynamic() {
113
+ return false;
114
+ }
115
+ byteLen() {
116
+ return this.bitSize / 8;
117
+ }
118
+ encode(value) {
119
+ if (typeof value !== 'bigint' && typeof value !== 'number') {
120
+ throw new Error(`Cannot encode value as uint${this.bitSize}: ${value}`);
121
+ }
122
+ if (value >= BigInt(2 ** this.bitSize) || value < BigInt(0)) {
123
+ throw new Error(`${value} is not a non-negative int or too big to fit in size uint${this.bitSize}`);
124
+ }
125
+ if (typeof value === 'number' && !Number.isSafeInteger(value)) {
126
+ throw new Error(`${value} should be converted into a BigInt before it is encoded`);
127
+ }
128
+ return bigint_1.bigIntToBytes(value, this.bitSize / 8);
129
+ }
130
+ decode(byteString) {
131
+ if (byteString.length !== this.bitSize / 8) {
132
+ throw new Error(`byte string must correspond to a uint${this.bitSize}`);
133
+ }
134
+ return bigint_1.bytesToBigInt(byteString);
135
+ }
136
+ }
137
+ exports.ABIUintType = ABIUintType;
138
+ class ABIUfixedType extends ABIType {
139
+ constructor(size, denominator) {
140
+ super();
141
+ if (size % 8 !== 0 || size < 8 || size > 512) {
142
+ throw new Error(`unsupported ufixed type bitSize: ${size}`);
143
+ }
144
+ if (denominator > 160 || denominator < 1) {
145
+ throw new Error(`unsupported ufixed type precision: ${denominator}`);
146
+ }
147
+ this.bitSize = size;
148
+ this.precision = denominator;
149
+ }
150
+ toString() {
151
+ return `ufixed${this.bitSize}x${this.precision}`;
152
+ }
153
+ equals(other) {
154
+ return (other instanceof ABIUfixedType &&
155
+ this.bitSize === other.bitSize &&
156
+ this.precision === other.precision);
157
+ }
158
+ isDynamic() {
159
+ return false;
160
+ }
161
+ byteLen() {
162
+ return this.bitSize / 8;
163
+ }
164
+ encode(value) {
165
+ if (typeof value !== 'bigint' && typeof value !== 'number') {
166
+ throw new Error(`Cannot encode value as ${this.toString()}: ${value}`);
167
+ }
168
+ if (value >= BigInt(2 ** this.bitSize) || value < BigInt(0)) {
169
+ throw new Error(`${value} is not a non-negative int or too big to fit in size ${this.toString()}`);
170
+ }
171
+ if (typeof value === 'number' && !Number.isSafeInteger(value)) {
172
+ throw new Error(`${value} should be converted into a BigInt before it is encoded`);
173
+ }
174
+ return bigint_1.bigIntToBytes(value, this.bitSize / 8);
175
+ }
176
+ decode(byteString) {
177
+ if (byteString.length !== this.bitSize / 8) {
178
+ throw new Error(`byte string must correspond to a ${this.toString()}`);
179
+ }
180
+ return bigint_1.bytesToBigInt(byteString);
181
+ }
182
+ }
183
+ exports.ABIUfixedType = ABIUfixedType;
184
+ class ABIAddressType extends ABIType {
185
+ toString() {
186
+ return 'address';
187
+ }
188
+ equals(other) {
189
+ return other instanceof ABIAddressType;
190
+ }
191
+ isDynamic() {
192
+ return false;
193
+ }
194
+ byteLen() {
195
+ return exports.ADDR_BYTE_SIZE;
196
+ }
197
+ encode(value) {
198
+ if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
199
+ throw new Error(`Cannot encode value as ${this.toString()}: ${value}`);
200
+ }
201
+ if (typeof value === 'string') {
202
+ const decodedAddress = address_1.decodeAddress(value);
203
+ return decodedAddress.publicKey;
204
+ }
205
+ // Return the address if it is already in bytes
206
+ if (value.byteLength !== 32) {
207
+ throw new Error(`byte string must be 32 bytes long for an address`);
208
+ }
209
+ return value;
210
+ }
211
+ decode(byteString) {
212
+ if (byteString.byteLength !== 32) {
213
+ throw new Error(`byte string must be 32 bytes long for an address`);
214
+ }
215
+ return address_1.encodeAddress(byteString);
216
+ }
217
+ }
218
+ exports.ABIAddressType = ABIAddressType;
219
+ class ABIBoolType extends ABIType {
220
+ toString() {
221
+ return 'bool';
222
+ }
223
+ equals(other) {
224
+ return other instanceof ABIBoolType;
225
+ }
226
+ isDynamic() {
227
+ return false;
228
+ }
229
+ byteLen() {
230
+ return exports.SINGLE_BOOL_SIZE;
231
+ }
232
+ encode(value) {
233
+ if (typeof value !== 'boolean') {
234
+ throw new Error(`Cannot encode value as bool: ${value}`);
235
+ }
236
+ if (value) {
237
+ return new Uint8Array([128]);
238
+ }
239
+ return new Uint8Array([0]);
240
+ }
241
+ decode(byteString) {
242
+ if (byteString.byteLength !== 1) {
243
+ throw new Error(`bool string must be 1 byte long`);
244
+ }
245
+ const value = byteString[0];
246
+ if (value === 128) {
247
+ return true;
248
+ }
249
+ if (value === 0) {
250
+ return false;
251
+ }
252
+ throw new Error(`boolean could not be decoded from the byte string`);
253
+ }
254
+ }
255
+ exports.ABIBoolType = ABIBoolType;
256
+ class ABIByteType extends ABIType {
257
+ toString() {
258
+ return 'byte';
259
+ }
260
+ equals(other) {
261
+ return other instanceof ABIByteType;
262
+ }
263
+ isDynamic() {
264
+ return false;
265
+ }
266
+ byteLen() {
267
+ return exports.SINGLE_BYTE_SIZE;
268
+ }
269
+ encode(value) {
270
+ if (typeof value !== 'number' && typeof value !== 'bigint') {
271
+ throw new Error(`Cannot encode value as byte: ${value}`);
272
+ }
273
+ if (typeof value === 'bigint') {
274
+ // eslint-disable-next-line no-param-reassign
275
+ value = Number(value);
276
+ }
277
+ if (value < 0 || value > 255) {
278
+ throw new Error(`${value} cannot be encoded into a byte`);
279
+ }
280
+ return new Uint8Array([value]);
281
+ }
282
+ decode(byteString) {
283
+ if (byteString.byteLength !== 1) {
284
+ throw new Error(`byte string must be 1 byte long`);
285
+ }
286
+ return byteString[0];
287
+ }
288
+ }
289
+ exports.ABIByteType = ABIByteType;
290
+ class ABIStringType extends ABIType {
291
+ toString() {
292
+ return 'string';
293
+ }
294
+ equals(other) {
295
+ return other instanceof ABIStringType;
296
+ }
297
+ isDynamic() {
298
+ return true;
299
+ }
300
+ byteLen() {
301
+ throw new Error(`${this.toString()} is a dynamic type`);
302
+ }
303
+ encode(value) {
304
+ if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
305
+ throw new Error(`Cannot encode value as string: ${value}`);
306
+ }
307
+ const encodedBytes = Buffer.from(value);
308
+ const encodedLength = bigint_1.bigIntToBytes(value.length, exports.LENGTH_ENCODE_BYTE_SIZE);
309
+ const mergedBytes = new Uint8Array(value.length + exports.LENGTH_ENCODE_BYTE_SIZE);
310
+ mergedBytes.set(encodedLength);
311
+ mergedBytes.set(encodedBytes, exports.LENGTH_ENCODE_BYTE_SIZE);
312
+ return mergedBytes;
313
+ }
314
+ decode(byteString) {
315
+ if (byteString.length < exports.LENGTH_ENCODE_BYTE_SIZE) {
316
+ throw new Error(`byte string is too short to be decoded. Actual length is ${byteString.length}, but expected at least ${exports.LENGTH_ENCODE_BYTE_SIZE}`);
317
+ }
318
+ const buf = Buffer.from(byteString);
319
+ const byteLength = buf.readUIntBE(0, exports.LENGTH_ENCODE_BYTE_SIZE);
320
+ const byteValue = byteString.slice(exports.LENGTH_ENCODE_BYTE_SIZE, byteString.length);
321
+ if (byteLength !== byteValue.length) {
322
+ throw new Error(`string length bytes do not match the actual length of string. Expected ${byteLength}, got ${byteValue.length}`);
323
+ }
324
+ return Buffer.from(byteValue).toString('utf-8');
325
+ }
326
+ }
327
+ exports.ABIStringType = ABIStringType;
328
+ class ABIArrayStaticType extends ABIType {
329
+ constructor(argType, arrayLength) {
330
+ super();
331
+ if (arrayLength < 1) {
332
+ throw new Error(`static array must have a length greater than 0: ${arrayLength}`);
333
+ }
334
+ this.childType = argType;
335
+ this.staticLength = arrayLength;
336
+ }
337
+ toString() {
338
+ return `${this.childType.toString()}[${this.staticLength}]`;
339
+ }
340
+ equals(other) {
341
+ return (other instanceof ABIArrayStaticType &&
342
+ this.staticLength === other.staticLength &&
343
+ this.childType.equals(other.childType));
344
+ }
345
+ isDynamic() {
346
+ return this.childType.isDynamic();
347
+ }
348
+ byteLen() {
349
+ if (this.childType.constructor === ABIBoolType) {
350
+ return Math.ceil(this.staticLength / 8);
351
+ }
352
+ return this.staticLength * this.childType.byteLen();
353
+ }
354
+ encode(value) {
355
+ if (!Array.isArray(value) && !(value instanceof Uint8Array)) {
356
+ throw new Error(`Cannot encode value as ${this.toString()}: ${value}`);
357
+ }
358
+ if (value.length !== this.staticLength) {
359
+ throw new Error(`Value array does not match static array length. Expected ${this.staticLength}, got ${value.length}`);
360
+ }
361
+ const convertedTuple = this.toABITupleType();
362
+ return convertedTuple.encode(value);
363
+ }
364
+ decode(byteString) {
365
+ const convertedTuple = this.toABITupleType();
366
+ return convertedTuple.decode(byteString);
367
+ }
368
+ toABITupleType() {
369
+ return new ABITupleType(Array(this.staticLength).fill(this.childType));
370
+ }
371
+ }
372
+ exports.ABIArrayStaticType = ABIArrayStaticType;
373
+ class ABIArrayDynamicType extends ABIType {
374
+ constructor(argType) {
375
+ super();
376
+ this.childType = argType;
377
+ }
378
+ toString() {
379
+ return `${this.childType.toString()}[]`;
380
+ }
381
+ equals(other) {
382
+ return (other instanceof ABIArrayDynamicType &&
383
+ this.childType.equals(other.childType));
384
+ }
385
+ isDynamic() {
386
+ return true;
387
+ }
388
+ byteLen() {
389
+ throw new Error(`${this.toString()} is a dynamic type`);
390
+ }
391
+ encode(value) {
392
+ if (!Array.isArray(value) && !(value instanceof Uint8Array)) {
393
+ throw new Error(`Cannot encode value as ${this.toString()}: ${value}`);
394
+ }
395
+ const convertedTuple = this.toABITupleType(value.length);
396
+ const encodedTuple = convertedTuple.encode(value);
397
+ const encodedLength = bigint_1.bigIntToBytes(convertedTuple.childTypes.length, exports.LENGTH_ENCODE_BYTE_SIZE);
398
+ const mergedBytes = utils_1.concatArrays(encodedLength, encodedTuple);
399
+ return mergedBytes;
400
+ }
401
+ decode(byteString) {
402
+ const buf = Buffer.from(byteString);
403
+ const byteLength = buf.readUIntBE(0, exports.LENGTH_ENCODE_BYTE_SIZE);
404
+ const convertedTuple = this.toABITupleType(byteLength);
405
+ return convertedTuple.decode(byteString.slice(exports.LENGTH_ENCODE_BYTE_SIZE, byteString.length));
406
+ }
407
+ toABITupleType(length) {
408
+ return new ABITupleType(Array(length).fill(this.childType));
409
+ }
410
+ }
411
+ exports.ABIArrayDynamicType = ABIArrayDynamicType;
412
+ class ABITupleType extends ABIType {
413
+ constructor(argTypes) {
414
+ super();
415
+ if (argTypes.length >= exports.MAX_LEN) {
416
+ throw new Error('tuple type child type number larger than maximum uint16 error');
417
+ }
418
+ this.childTypes = argTypes;
419
+ }
420
+ toString() {
421
+ const typeStrings = [];
422
+ for (let i = 0; i < this.childTypes.length; i++) {
423
+ typeStrings[i] = this.childTypes[i].toString();
424
+ }
425
+ return `(${typeStrings.join(',')})`;
426
+ }
427
+ equals(other) {
428
+ return (other instanceof ABITupleType &&
429
+ this.childTypes.length === other.childTypes.length &&
430
+ this.childTypes.every((child, index) => child.equals(other.childTypes[index])));
431
+ }
432
+ isDynamic() {
433
+ const isDynamic = (child) => child.isDynamic();
434
+ return this.childTypes.some(isDynamic);
435
+ }
436
+ byteLen() {
437
+ let size = 0;
438
+ for (let i = 0; i < this.childTypes.length; i++) {
439
+ if (this.childTypes[i].constructor === ABIBoolType) {
440
+ const after = findBoolLR(this.childTypes, i, 1);
441
+ const boolNum = after + 1;
442
+ i += after;
443
+ size += Math.trunc((boolNum + 7) / 8);
444
+ }
445
+ else {
446
+ const childByteSize = this.childTypes[i].byteLen();
447
+ size += childByteSize;
448
+ }
449
+ }
450
+ return size;
451
+ }
452
+ encode(value) {
453
+ if (!Array.isArray(value) && !(value instanceof Uint8Array)) {
454
+ throw new Error(`Cannot encode value as ${this.toString()}: ${value}`);
455
+ }
456
+ const values = Array.from(value);
457
+ if (value.length > exports.MAX_LEN) {
458
+ throw new Error('length of tuple array should not exceed a uint16');
459
+ }
460
+ const tupleTypes = this.childTypes;
461
+ const heads = [];
462
+ const tails = [];
463
+ const isDynamicIndex = new Map();
464
+ let i = 0;
465
+ while (i < tupleTypes.length) {
466
+ const tupleType = tupleTypes[i];
467
+ if (tupleType.isDynamic()) {
468
+ // Head is not pre-determined for dynamic types; store a placeholder for now
469
+ isDynamicIndex.set(heads.length, true);
470
+ heads.push(new Uint8Array([0, 0]));
471
+ tails.push(tupleType.encode(values[i]));
472
+ }
473
+ else {
474
+ if (tupleType.constructor === ABIBoolType) {
475
+ const before = findBoolLR(tupleTypes, i, -1);
476
+ let after = findBoolLR(tupleTypes, i, 1);
477
+ // Pack bytes to heads and tails
478
+ if (before % 8 !== 0) {
479
+ throw new Error('expected before index should have number of bool mod 8 equal 0');
480
+ }
481
+ after = Math.min(7, after);
482
+ const compressedInt = compressMultipleBool(values.slice(i, i + after + 1));
483
+ heads.push(bigint_1.bigIntToBytes(compressedInt, 1));
484
+ i += after;
485
+ }
486
+ else {
487
+ const encodedTupleValue = tupleType.encode(values[i]);
488
+ heads.push(encodedTupleValue);
489
+ }
490
+ isDynamicIndex.set(i, false);
491
+ tails.push(new Uint8Array());
492
+ }
493
+ i += 1;
494
+ }
495
+ // Adjust head lengths for dynamic types
496
+ let headLength = 0;
497
+ for (const headElement of heads) {
498
+ headLength += headElement.length;
499
+ }
500
+ // encode any placeholders for dynamic types
501
+ let tailLength = 0;
502
+ for (let j = 0; j < heads.length; j++) {
503
+ if (isDynamicIndex.get(j)) {
504
+ const headValue = headLength + tailLength;
505
+ if (headValue > exports.MAX_LEN) {
506
+ throw new Error(`byte length of ${headValue} should not exceed a uint16`);
507
+ }
508
+ heads[j] = bigint_1.bigIntToBytes(headValue, exports.LENGTH_ENCODE_BYTE_SIZE);
509
+ }
510
+ tailLength += tails[j].length;
511
+ }
512
+ return utils_1.concatArrays(...heads, ...tails);
513
+ }
514
+ decode(byteString) {
515
+ const tupleTypes = this.childTypes;
516
+ const dynamicSegments = [];
517
+ const valuePartition = [];
518
+ let i = 0;
519
+ let iterIndex = 0;
520
+ const buf = Buffer.from(byteString);
521
+ while (i < tupleTypes.length) {
522
+ const tupleType = tupleTypes[i];
523
+ if (tupleType.isDynamic()) {
524
+ if (byteString.slice(iterIndex, byteString.length).length <
525
+ exports.LENGTH_ENCODE_BYTE_SIZE) {
526
+ throw new Error('dynamic type in tuple is too short to be decoded');
527
+ }
528
+ const dynamicIndex = buf.readUIntBE(iterIndex, exports.LENGTH_ENCODE_BYTE_SIZE);
529
+ if (dynamicSegments.length > 0) {
530
+ dynamicSegments[dynamicSegments.length - 1].right = dynamicIndex;
531
+ // Check that right side of segment is greater than the left side
532
+ if (dynamicIndex < dynamicSegments[dynamicSegments.length - 1].left) {
533
+ throw new Error('dynamic index segment miscalculation: left is greater than right index');
534
+ }
535
+ }
536
+ // Since we do not know where the current dynamic element ends, put a placeholder and update later
537
+ const seg = {
538
+ left: dynamicIndex,
539
+ right: -1,
540
+ };
541
+ dynamicSegments.push(seg);
542
+ valuePartition.push(null);
543
+ iterIndex += exports.LENGTH_ENCODE_BYTE_SIZE;
544
+ }
545
+ else {
546
+ // eslint-disable-next-line no-lonely-if
547
+ if (tupleType.constructor === ABIBoolType) {
548
+ const before = findBoolLR(this.childTypes, i, -1);
549
+ let after = findBoolLR(this.childTypes, i, 1);
550
+ if (before % 8 !== 0) {
551
+ throw new Error('expected before bool number mod 8 === 0');
552
+ }
553
+ after = Math.min(7, after);
554
+ // Parse bool in a byte to multiple byte strings
555
+ for (let boolIndex = 0; boolIndex <= after; boolIndex++) {
556
+ const boolMask = 0x80 >> boolIndex;
557
+ if ((byteString[iterIndex] & boolMask) > 0) {
558
+ valuePartition.push(new Uint8Array([128]));
559
+ }
560
+ else {
561
+ valuePartition.push(new Uint8Array([0]));
562
+ }
563
+ }
564
+ i += after;
565
+ iterIndex += 1;
566
+ }
567
+ else {
568
+ const currLen = tupleType.byteLen();
569
+ valuePartition.push(byteString.slice(iterIndex, iterIndex + currLen));
570
+ iterIndex += currLen;
571
+ }
572
+ }
573
+ if (i !== tupleTypes.length - 1 && iterIndex >= byteString.length) {
574
+ throw new Error('input byte not enough to decode');
575
+ }
576
+ i += 1;
577
+ }
578
+ if (dynamicSegments.length > 0) {
579
+ dynamicSegments[dynamicSegments.length - 1].right = byteString.length;
580
+ iterIndex = byteString.length;
581
+ }
582
+ if (iterIndex < byteString.length) {
583
+ throw new Error('input byte not fully consumed');
584
+ }
585
+ // Check segment indices are valid
586
+ // If the dynamic segment are not consecutive and well-ordered, we return error
587
+ for (let j = 0; j < dynamicSegments.length; j++) {
588
+ const seg = dynamicSegments[j];
589
+ if (seg.left > seg.right) {
590
+ throw new Error('dynamic segment should display a [l, r] space with l <= r');
591
+ }
592
+ if (j !== dynamicSegments.length - 1 &&
593
+ seg.right !== dynamicSegments[j + 1].left) {
594
+ throw new Error('dynamic segment should be consecutive');
595
+ }
596
+ }
597
+ // Check dynamic element partitions
598
+ let segIndex = 0;
599
+ for (let j = 0; j < tupleTypes.length; j++) {
600
+ if (tupleTypes[j].isDynamic()) {
601
+ valuePartition[j] = byteString.slice(dynamicSegments[segIndex].left, dynamicSegments[segIndex].right);
602
+ segIndex += 1;
603
+ }
604
+ }
605
+ // Decode each tuple element
606
+ const returnValues = [];
607
+ for (let j = 0; j < tupleTypes.length; j++) {
608
+ const valueTi = tupleTypes[j].decode(valuePartition[j]);
609
+ returnValues.push(valueTi);
610
+ }
611
+ return returnValues;
612
+ }
613
+ static parseTupleContent(str) {
614
+ if (str.length === 0) {
615
+ return [];
616
+ }
617
+ if (str.endsWith(',') || str.startsWith(',')) {
618
+ throw new Error('tuple string should not start with comma');
619
+ }
620
+ if (str.includes(',,')) {
621
+ throw new Error('tuple string should not have consecutive commas');
622
+ }
623
+ const tupleStrings = [];
624
+ let depth = 0;
625
+ let word = '';
626
+ for (const char of str) {
627
+ word += char;
628
+ if (char === '(') {
629
+ depth += 1;
630
+ }
631
+ else if (char === ')') {
632
+ depth -= 1;
633
+ }
634
+ else if (char === ',') {
635
+ // If the comma is at depth 0, then append the word as token.
636
+ if (depth === 0) {
637
+ tupleStrings.push(word.slice(0, word.length - 1));
638
+ word = '';
639
+ }
640
+ }
641
+ }
642
+ if (word.length !== 0) {
643
+ tupleStrings.push(word);
644
+ }
645
+ if (depth !== 0) {
646
+ throw new Error('tuple string has mismatched parentheses');
647
+ }
648
+ return tupleStrings;
649
+ }
650
+ }
651
+ exports.ABITupleType = ABITupleType;
652
+ // compressMultipleBool compresses consecutive bool values into a byte in ABI tuple / array value.
653
+ function compressMultipleBool(valueList) {
654
+ let res = 0;
655
+ if (valueList.length > 8) {
656
+ throw new Error('value list passed in should be no greater than length 8');
657
+ }
658
+ for (let i = 0; i < valueList.length; i++) {
659
+ const boolVal = valueList[i];
660
+ if (typeof boolVal !== 'boolean') {
661
+ throw new Error('non-boolean values cannot be compressed into a byte');
662
+ }
663
+ if (boolVal) {
664
+ res |= 1 << (7 - i);
665
+ }
666
+ }
667
+ return res;
668
+ }
669
+ // Assume that the current index on the list of type is an ABI bool type.
670
+ // It returns the difference between the current index and the index of the furthest consecutive Bool type.
671
+ function findBoolLR(typeList, index, delta) {
672
+ let until = 0;
673
+ while (true) {
674
+ const curr = index + delta * until;
675
+ if (typeList[curr].constructor === ABIBoolType) {
676
+ if (curr !== typeList.length - 1 && delta === 1) {
677
+ until += 1;
678
+ }
679
+ else if (curr > 0 && delta === -1) {
680
+ until += 1;
681
+ }
682
+ else {
683
+ break;
684
+ }
685
+ }
686
+ else {
687
+ until -= 1;
688
+ break;
689
+ }
690
+ }
691
+ return until;
692
+ }
693
+ //# sourceMappingURL=abi_type.js.map