@algorandfoundation/algorand-typescript 0.0.1-alpha.1 → 0.0.1-alpha.11

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.
package/arc4/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ export { w as Address, t as Bool, s as Byte, C as Contract, D as DynamicArray, n as OnCompleteAction, v as StaticArray, S as Str, T as Tuple, r as UFixedNxM, q as UintN, g as abimethod, o as baremethod } from '../index-DffKkMrz.js';
2
+ import '../execution-context-BznYSiE4.js';
3
+ import 'node:buffer';
4
+ import 'node:util';
5
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
@@ -0,0 +1,20 @@
1
+ const ctxMgr = {
2
+ set instance(ctx) {
3
+ const instance = global.puyaTsExecutionContext;
4
+ if (instance != undefined)
5
+ throw new Error('Execution context has already been set');
6
+ global.puyaTsExecutionContext = ctx;
7
+ },
8
+ get instance() {
9
+ const instance = global.puyaTsExecutionContext;
10
+ if (instance == undefined)
11
+ throw new Error('No execution context has been set');
12
+ return instance;
13
+ },
14
+ reset() {
15
+ global.puyaTsExecutionContext = undefined;
16
+ },
17
+ };
18
+
19
+ export { ctxMgr as c };
20
+ //# sourceMappingURL=execution-context-BznYSiE4.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution-context-BznYSiE4.js","sources":["../src/execution-context.ts"],"sourcesContent":["import { Contract, gtxn, itxn } from '.'\nimport { AbiMethodConfig, BareMethodConfig } from './arc4'\nimport { OpsNamespace } from './op-types'\nimport { bytes, uint64 } from './primitives'\nimport { Account, Application, Asset } from './reference'\n\nexport type ExecutionContext = {\n log(value: bytes): void\n application(id?: uint64): Application\n asset(id?: uint64): Asset\n account(address?: bytes): Account\n op: Partial<OpsNamespace>\n abiMetadata: {\n captureMethodConfig<T extends Contract>(contract: T, methodName: string, config?: AbiMethodConfig<T> | BareMethodConfig): void\n }\n gtxn: {\n Transaction: typeof gtxn.Transaction\n PaymentTxn: typeof gtxn.PaymentTxn\n KeyRegistrationTxn: typeof gtxn.KeyRegistrationTxn\n AssetConfigTxn: typeof gtxn.AssetConfigTxn\n AssetTransferTxn: typeof gtxn.AssetTransferTxn\n AssetFreezeTxn: typeof gtxn.AssetFreezeTxn\n ApplicationTxn: typeof gtxn.ApplicationTxn\n }\n itxn: {\n submitGroup: typeof itxn.submitGroup\n payment: typeof itxn.payment\n keyRegistration: typeof itxn.keyRegistration\n assetConfig: typeof itxn.assetConfig\n assetTransfer: typeof itxn.assetTransfer\n assetFreeze: typeof itxn.assetFreeze\n applicationCall: typeof itxn.applicationCall\n }\n}\n\ndeclare global {\n // eslint-disable-next-line no-var\n var puyaTsExecutionContext: ExecutionContext | undefined\n}\n\nexport const ctxMgr = {\n set instance(ctx: ExecutionContext) {\n const instance = global.puyaTsExecutionContext\n if (instance != undefined) throw new Error('Execution context has already been set')\n global.puyaTsExecutionContext = ctx\n },\n get instance() {\n const instance = global.puyaTsExecutionContext\n if (instance == undefined) throw new Error('No execution context has been set')\n return instance\n },\n reset() {\n global.puyaTsExecutionContext = undefined\n },\n}\n"],"names":[],"mappings":"AAwCa,MAAA,MAAM,GAAG;IACpB,IAAI,QAAQ,CAAC,GAAqB,EAAA;AAChC,QAAA,MAAM,QAAQ,GAAG,MAAM,CAAC,sBAAsB,CAAA;QAC9C,IAAI,QAAQ,IAAI,SAAS;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAA;AACpF,QAAA,MAAM,CAAC,sBAAsB,GAAG,GAAG,CAAA;KACpC;AACD,IAAA,IAAI,QAAQ,GAAA;AACV,QAAA,MAAM,QAAQ,GAAG,MAAM,CAAC,sBAAsB,CAAA;QAC9C,IAAI,QAAQ,IAAI,SAAS;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAA;AAC/E,QAAA,OAAO,QAAQ,CAAA;KAChB;IACD,KAAK,GAAA;AACH,QAAA,MAAM,CAAC,sBAAsB,GAAG,SAAS,CAAA;KAC1C;;;;;"}
@@ -1,4 +1,6 @@
1
1
  export declare const uint8ArrayToBigInt: (v: Uint8Array) => bigint;
2
+ export declare const hexToUint8Array: (value: string) => Uint8Array;
3
+ export declare const base64ToUint8Array: (value: string) => Uint8Array;
2
4
  export declare const bigIntToUint8Array: (val: bigint, fixedSize?: number | "dynamic") => Uint8Array;
3
5
  export declare const utf8ToUint8Array: (value: string) => Uint8Array;
4
6
  export declare const uint8ArrayToUtf8: (value: Uint8Array) => string;
package/impl/errors.d.ts CHANGED
@@ -6,6 +6,7 @@ export declare class AvmError extends Error {
6
6
  constructor(message: string);
7
7
  }
8
8
  export declare function avmError(message: string): never;
9
+ export declare function avmInvariant(condition: unknown, message: string): asserts condition;
9
10
  /**
10
11
  * Raised when an assertion fails
11
12
  */
@@ -7,33 +7,35 @@ export declare function toExternalValue(val: biguint): bigint;
7
7
  export declare function toExternalValue(val: bytes): Uint8Array;
8
8
  export declare function toExternalValue(val: string): string;
9
9
  export declare const toBytes: (val: unknown) => bytes;
10
+ /**
11
+ * Convert a StubUint64Compat value into a 'number' if possible.
12
+ * This value may be negative
13
+ * @param v
14
+ */
15
+ export declare const getNumber: (v: StubUint64Compat) => number;
10
16
  export declare const isBytes: (v: unknown) => v is StubBytesCompat;
11
17
  export declare const isUint64: (v: unknown) => v is StubUint64Compat;
12
- export declare const isBigUint: (v: unknown) => v is biguint;
13
18
  export declare const checkUint64: (v: bigint) => bigint;
14
19
  export declare const checkBigUint: (v: bigint) => bigint;
15
20
  export declare const checkBytes: (v: Uint8Array) => Uint8Array;
16
21
  export declare abstract class AlgoTsPrimitiveCls {
17
- private readonly _type;
18
- constructor(t: string);
19
22
  static [Symbol.hasInstance](x: unknown): x is AlgoTsPrimitiveCls;
20
- abstract valueOf(): bigint | string | boolean;
23
+ abstract valueOf(): bigint | string;
21
24
  abstract toBytes(): BytesCls;
22
25
  }
23
26
  export declare class Uint64Cls extends AlgoTsPrimitiveCls {
24
- readonly value: bigint;
27
+ #private;
25
28
  constructor(value: bigint | number | string);
26
29
  static [Symbol.hasInstance](x: unknown): x is Uint64Cls;
27
30
  static fromCompat(v: StubUint64Compat): Uint64Cls;
28
- static getNumber(v: StubUint64Compat): number;
29
31
  valueOf(): bigint;
30
- toBytes(isDynamic?: boolean): BytesCls;
32
+ toBytes(): BytesCls;
31
33
  asAlgoTs(): uint64;
32
34
  asBigInt(): bigint;
33
35
  asNumber(): number;
34
36
  }
35
37
  export declare class BigUintCls extends AlgoTsPrimitiveCls {
36
- readonly value: bigint;
38
+ #private;
37
39
  constructor(value: bigint);
38
40
  valueOf(): bigint;
39
41
  toBytes(): BytesCls;
@@ -49,7 +51,7 @@ export declare class BytesCls extends AlgoTsPrimitiveCls {
49
51
  get length(): Uint64Cls;
50
52
  toBytes(): BytesCls;
51
53
  at(i: StubUint64Compat): BytesCls;
52
- slice(start: StubUint64Compat, end: StubUint64Compat): BytesCls;
54
+ slice(start: undefined | StubUint64Compat, end: undefined | StubUint64Compat): BytesCls;
53
55
  concat(other: StubBytesCompat): BytesCls;
54
56
  bitwiseAnd(other: StubBytesCompat): BytesCls;
55
57
  bitwiseOr(other: StubBytesCompat): BytesCls;
@@ -59,7 +61,7 @@ export declare class BytesCls extends AlgoTsPrimitiveCls {
59
61
  private bitwiseOp;
60
62
  valueOf(): string;
61
63
  static [Symbol.hasInstance](x: unknown): x is BytesCls;
62
- static fromCompat(v: StubBytesCompat | undefined): BytesCls;
64
+ static fromCompat(v: StubBytesCompat | Uint8Array | undefined): BytesCls;
63
65
  static fromInterpolation(template: TemplateStringsArray, replacements: StubBytesCompat[]): BytesCls;
64
66
  static fromHex(hex: string): BytesCls;
65
67
  static fromBase64(b64: string): BytesCls;
@@ -71,7 +73,10 @@ export declare class BytesCls extends AlgoTsPrimitiveCls {
71
73
  asUint8Array(): Uint8Array;
72
74
  }
73
75
  export declare const arrayUtil: {
76
+ arrayAt(arrayLike: Uint8Array, index: StubUint64Compat): Uint8Array;
74
77
  arrayAt<T>(arrayLike: T[], index: StubUint64Compat): T;
75
- arraySlice(arrayLike: Uint8Array, start: StubUint64Compat, end: StubUint64Compat): Uint8Array;
76
- arraySlice<T>(arrayLike: T[], start: StubUint64Compat, end: StubUint64Compat): T[];
78
+ arrayAt<T>(arrayLike: T[] | Uint8Array, index: StubUint64Compat): T | Uint8Array;
79
+ arraySlice(arrayLike: Uint8Array, start: undefined | StubUint64Compat, end: undefined | StubUint64Compat): Uint8Array;
80
+ arraySlice<T>(arrayLike: T[], start: undefined | StubUint64Compat, end: undefined | StubUint64Compat): T[];
81
+ arraySlice<T>(arrayLike: Uint8Array | T[], start: undefined | StubUint64Compat, end: undefined | StubUint64Compat): Uint8Array | T[];
77
82
  };
@@ -1,5 +1,67 @@
1
+ import { c as ctxMgr } from './execution-context-BznYSiE4.js';
2
+ import { Buffer } from 'node:buffer';
1
3
  import { TextDecoder } from 'node:util';
2
4
 
5
+ /**
6
+ * Raised when an `err` op is encountered, or when the testing VM is asked to do something that would cause
7
+ * the AVM to fail.
8
+ */
9
+ class AvmError extends Error {
10
+ constructor(message) {
11
+ super(message);
12
+ }
13
+ }
14
+ function avmError(message) {
15
+ throw new AvmError(message);
16
+ }
17
+ function avmInvariant(condition, message) {
18
+ if (!condition) {
19
+ throw new AvmError(message);
20
+ }
21
+ }
22
+ /**
23
+ * Raised when an assertion fails
24
+ */
25
+ class AssertError extends AvmError {
26
+ constructor(message) {
27
+ super(message);
28
+ }
29
+ }
30
+ /**
31
+ * Raised when testing code errors
32
+ */
33
+ class InternalError extends Error {
34
+ constructor(message, options) {
35
+ super(message, options);
36
+ }
37
+ }
38
+ function internalError(message) {
39
+ throw new InternalError(message);
40
+ }
41
+ /**
42
+ * Raised when unsupported user code is encountered
43
+ */
44
+ class CodeError extends Error {
45
+ constructor(message, options) {
46
+ super(message, options);
47
+ }
48
+ }
49
+ function codeError(message) {
50
+ throw new CodeError(message);
51
+ }
52
+
53
+ var errors = /*#__PURE__*/Object.freeze({
54
+ __proto__: null,
55
+ AssertError: AssertError,
56
+ AvmError: AvmError,
57
+ CodeError: CodeError,
58
+ InternalError: InternalError,
59
+ avmError: avmError,
60
+ avmInvariant: avmInvariant,
61
+ codeError: codeError,
62
+ internalError: internalError
63
+ });
64
+
3
65
  const BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.split('');
4
66
  const CHAR_TO_NUM = BASE32_ALPHABET.reduce((acc, cur, index) => ((acc[cur] = index), acc), {});
5
67
  const base32ToUint8Array = (value) => {
@@ -42,7 +104,7 @@ const uint8ArrayToBase32 = (value) => {
42
104
  if (allBytes.length < 1)
43
105
  break;
44
106
  base32str += BASE32_ALPHABET[a >>> 3];
45
- base32str += BASE32_ALPHABET[((a << 2) | ((b || 0) >>> 6)) & 32];
107
+ base32str += BASE32_ALPHABET[((a << 2) | ((b || 0) >>> 6)) & 31];
46
108
  if (allBytes.length < 2)
47
109
  break;
48
110
  base32str += BASE32_ALPHABET[(b >>> 1) & 31];
@@ -62,60 +124,6 @@ const uint8ArrayToBase32 = (value) => {
62
124
  return base32str;
63
125
  };
64
126
 
65
- /**
66
- * Raised when an `err` op is encountered, or when the testing VM is asked to do something that would cause
67
- * the AVM to fail.
68
- */
69
- class AvmError extends Error {
70
- constructor(message) {
71
- super(message);
72
- }
73
- }
74
- function avmError(message) {
75
- throw new AvmError(message);
76
- }
77
- /**
78
- * Raised when an assertion fails
79
- */
80
- class AssertError extends AvmError {
81
- constructor(message) {
82
- super(message);
83
- }
84
- }
85
- /**
86
- * Raised when testing code errors
87
- */
88
- class InternalError extends Error {
89
- constructor(message, options) {
90
- super(message, options);
91
- }
92
- }
93
- function internalError(message) {
94
- throw new InternalError(message);
95
- }
96
- /**
97
- * Raised when unsupported user code is encountered
98
- */
99
- class CodeError extends Error {
100
- constructor(message, options) {
101
- super(message, options);
102
- }
103
- }
104
- function codeError(message) {
105
- throw new CodeError(message);
106
- }
107
-
108
- var errors = /*#__PURE__*/Object.freeze({
109
- __proto__: null,
110
- AssertError: AssertError,
111
- AvmError: AvmError,
112
- CodeError: CodeError,
113
- InternalError: InternalError,
114
- avmError: avmError,
115
- codeError: codeError,
116
- internalError: internalError
117
- });
118
-
119
127
  const uint8ArrayToBigInt = (v) => {
120
128
  // Assume big-endian
121
129
  return Array.from(v)
@@ -123,6 +131,16 @@ const uint8ArrayToBigInt = (v) => {
123
131
  .map((byte_value, i) => BigInt(byte_value) << BigInt(i * 8))
124
132
  .reduce((a, b) => a + b, 0n);
125
133
  };
134
+ const hexToUint8Array = (value) => {
135
+ if (value.length % 2 !== 0) {
136
+ // TODO: Verify AVM behaviour is to fail
137
+ throw new AvmError('Hex string must have even number of characters');
138
+ }
139
+ return Uint8Array.from(Buffer.from(value, 'hex'));
140
+ };
141
+ const base64ToUint8Array = (value) => {
142
+ return Uint8Array.from(Buffer.from(value, 'base64'));
143
+ };
126
144
  const bigIntToUint8Array = (val, fixedSize = 'dynamic') => {
127
145
  if (val === 0n && fixedSize === 'dynamic') {
128
146
  return new Uint8Array(0);
@@ -160,7 +178,9 @@ const uint8ArrayToBase64Url = (value) => Buffer.from(value).toString('base64url'
160
178
 
161
179
  var encodingUtil = /*#__PURE__*/Object.freeze({
162
180
  __proto__: null,
181
+ base64ToUint8Array: base64ToUint8Array,
163
182
  bigIntToUint8Array: bigIntToUint8Array,
183
+ hexToUint8Array: hexToUint8Array,
164
184
  uint8ArrayToBase32: uint8ArrayToBase32,
165
185
  uint8ArrayToBase64: uint8ArrayToBase64,
166
186
  uint8ArrayToBase64Url: uint8ArrayToBase64Url,
@@ -210,6 +230,24 @@ const toBytes = (val) => {
210
230
  internalError(`Unsupported arg type ${nameOfType(val)}`);
211
231
  }
212
232
  };
233
+ /**
234
+ * Convert a StubUint64Compat value into a 'number' if possible.
235
+ * This value may be negative
236
+ * @param v
237
+ */
238
+ const getNumber = (v) => {
239
+ if (typeof v == 'boolean')
240
+ return v ? 1 : 0;
241
+ if (typeof v == 'number')
242
+ return v;
243
+ if (typeof v == 'bigint') {
244
+ avmInvariant(v <= BigInt(Number.MAX_SAFE_INTEGER) && v >= BigInt(Number.MIN_SAFE_INTEGER), 'value cannot be safely converted to a number');
245
+ return Number(v);
246
+ }
247
+ if (v instanceof Uint64Cls)
248
+ return v.asNumber();
249
+ internalError(`Cannot convert ${v} to number`);
250
+ };
213
251
  const isBytes = (v) => {
214
252
  if (typeof v === 'string')
215
253
  return true;
@@ -226,19 +264,16 @@ const isUint64 = (v) => {
226
264
  return true;
227
265
  return v instanceof Uint64Cls;
228
266
  };
229
- const isBigUint = (v) => {
230
- return v instanceof BigUintCls;
231
- };
232
267
  const checkUint64 = (v) => {
233
268
  const u64 = BigInt.asUintN(64, v);
234
269
  if (u64 !== v)
235
- throw new AvmError(`Uint64 over or underflow`);
270
+ throw new AvmError(`Uint64 overflow or underflow`);
236
271
  return u64;
237
272
  };
238
273
  const checkBigUint = (v) => {
239
274
  const uBig = BigInt.asUintN(64 * 8, v);
240
275
  if (uBig !== v)
241
- throw new AvmError(`BigUint over or underflow`);
276
+ throw new AvmError(`BigUint overflow or underflow`);
242
277
  return uBig;
243
278
  };
244
279
  const checkBytes = (v) => {
@@ -246,24 +281,44 @@ const checkBytes = (v) => {
246
281
  throw new AvmError(`Bytes length ${v.length} exceeds maximum length ${MAX_BYTES_SIZE}`);
247
282
  return v;
248
283
  };
249
- class AlgoTsPrimitiveCls {
250
- _type = AlgoTsPrimitiveCls.name;
251
- constructor(t) {
252
- this._type = `${AlgoTsPrimitiveCls.name}.${t}`;
284
+ /**
285
+ * Verifies that an object is an instance of a type based on its name rather than reference equality.
286
+ *
287
+ * This is useful in scenarios where a module loader has loaded a module twice and hence two instances of a
288
+ * type do not have reference equality on their constructors.
289
+ * @param subject The object to check
290
+ * @param typeCtor The ctor of the type
291
+ */
292
+ function isInstanceOfTypeByName(subject, typeCtor) {
293
+ if (subject === null || typeof subject !== 'object')
294
+ return false;
295
+ let ctor = subject.constructor;
296
+ while (typeof ctor === 'function') {
297
+ if (ctor.name === typeCtor.name)
298
+ return true;
299
+ ctor = Object.getPrototypeOf(ctor);
253
300
  }
301
+ return false;
302
+ }
303
+ class AlgoTsPrimitiveCls {
254
304
  static [Symbol.hasInstance](x) {
255
- return x instanceof Object && '_type' in x && x['_type'].startsWith(AlgoTsPrimitiveCls.name);
305
+ return isInstanceOfTypeByName(x, AlgoTsPrimitiveCls);
256
306
  }
257
307
  }
258
308
  class Uint64Cls extends AlgoTsPrimitiveCls {
259
- value;
309
+ #value;
260
310
  constructor(value) {
261
- super(Uint64Cls.name);
262
- this.value = BigInt(value);
263
- checkUint64(this.value);
311
+ super();
312
+ this.#value = BigInt(value);
313
+ checkUint64(this.#value);
314
+ Object.defineProperty(this, 'uint64', {
315
+ value: this.#value.toString(),
316
+ writable: false,
317
+ enumerable: true,
318
+ });
264
319
  }
265
320
  static [Symbol.hasInstance](x) {
266
- return x instanceof Object && '_type' in x && x['_type'].endsWith(Uint64Cls.name);
321
+ return isInstanceOfTypeByName(x, Uint64Cls);
267
322
  }
268
323
  static fromCompat(v) {
269
324
  if (typeof v == 'boolean')
@@ -276,54 +331,56 @@ class Uint64Cls extends AlgoTsPrimitiveCls {
276
331
  return v;
277
332
  internalError(`Cannot convert ${v} to uint64`);
278
333
  }
279
- static getNumber(v) {
280
- return Uint64Cls.fromCompat(v).asNumber();
281
- }
282
334
  valueOf() {
283
- return this.value;
335
+ return this.#value;
284
336
  }
285
- toBytes(isDynamic = false) {
286
- return new BytesCls(bigIntToUint8Array(this.value, isDynamic ? 'dynamic' : 8));
337
+ toBytes() {
338
+ return new BytesCls(bigIntToUint8Array(this.#value, 8));
287
339
  }
288
340
  asAlgoTs() {
289
341
  return this;
290
342
  }
291
343
  asBigInt() {
292
- return this.value;
344
+ return this.#value;
293
345
  }
294
346
  asNumber() {
295
- if (this.value > Number.MAX_SAFE_INTEGER) {
347
+ if (this.#value > Number.MAX_SAFE_INTEGER) {
296
348
  throw new AvmError('value cannot be safely converted to a number');
297
349
  }
298
- return Number(this.value);
350
+ return Number(this.#value);
299
351
  }
300
352
  }
301
353
  class BigUintCls extends AlgoTsPrimitiveCls {
302
- value;
354
+ #value;
303
355
  constructor(value) {
304
- super(BigUintCls.name);
305
- this.value = value;
356
+ super();
357
+ this.#value = value;
358
+ Object.defineProperty(this, 'biguint', {
359
+ value: value.toString(),
360
+ writable: false,
361
+ enumerable: true,
362
+ });
306
363
  }
307
364
  valueOf() {
308
- return this.value;
365
+ return this.#value;
309
366
  }
310
367
  toBytes() {
311
- return new BytesCls(bigIntToUint8Array(this.value));
368
+ return new BytesCls(bigIntToUint8Array(this.#value));
312
369
  }
313
370
  asAlgoTs() {
314
371
  return this;
315
372
  }
316
373
  asBigInt() {
317
- return this.value;
374
+ return this.#value;
318
375
  }
319
376
  asNumber() {
320
- if (this.value > Number.MAX_SAFE_INTEGER) {
377
+ if (this.#value > Number.MAX_SAFE_INTEGER) {
321
378
  throw new AvmError('value cannot be safely converted to a number');
322
379
  }
323
- return Number(this.value);
380
+ return Number(this.#value);
324
381
  }
325
382
  static [Symbol.hasInstance](x) {
326
- return x instanceof Object && '_type' in x && x['_type'].endsWith(BigUintCls.name);
383
+ return isInstanceOfTypeByName(x, BigUintCls);
327
384
  }
328
385
  static fromCompat(v) {
329
386
  if (typeof v == 'boolean')
@@ -333,7 +390,7 @@ class BigUintCls extends AlgoTsPrimitiveCls {
333
390
  if (typeof v == 'bigint')
334
391
  return new BigUintCls(v);
335
392
  if (v instanceof Uint64Cls)
336
- return new BigUintCls(v.value);
393
+ return new BigUintCls(v.valueOf());
337
394
  if (v instanceof BytesCls)
338
395
  return v.toBigUint();
339
396
  if (v instanceof BigUintCls)
@@ -344,9 +401,15 @@ class BigUintCls extends AlgoTsPrimitiveCls {
344
401
  class BytesCls extends AlgoTsPrimitiveCls {
345
402
  #v;
346
403
  constructor(v) {
347
- super(BytesCls.name);
404
+ super();
348
405
  this.#v = v;
349
406
  checkBytes(this.#v);
407
+ // Add an enumerable property for debugging code to show
408
+ Object.defineProperty(this, 'bytes', {
409
+ value: uint8ArrayToHex(this.#v),
410
+ writable: false,
411
+ enumerable: true,
412
+ });
350
413
  }
351
414
  get length() {
352
415
  return new Uint64Cls(this.#v.length);
@@ -355,13 +418,10 @@ class BytesCls extends AlgoTsPrimitiveCls {
355
418
  return this;
356
419
  }
357
420
  at(i) {
358
- const start = Uint64Cls.fromCompat(i).asNumber();
359
- return new BytesCls(this.#v.slice(start, start + 1));
421
+ return new BytesCls(arrayUtil.arrayAt(this.#v, i));
360
422
  }
361
423
  slice(start, end) {
362
- const startNumber = start instanceof Uint64Cls ? start.asNumber() : start;
363
- const endNumber = end instanceof Uint64Cls ? end.asNumber() : end;
364
- const sliced = arrayUtil.arraySlice(this.#v, startNumber, endNumber);
424
+ const sliced = arrayUtil.arraySlice(this.#v, start, end);
365
425
  return new BytesCls(sliced);
366
426
  }
367
427
  concat(other) {
@@ -408,10 +468,10 @@ class BytesCls extends AlgoTsPrimitiveCls {
408
468
  return new BytesCls(result);
409
469
  }
410
470
  valueOf() {
411
- return uint8ArrayToUtf8(this.#v);
471
+ return uint8ArrayToHex(this.#v);
412
472
  }
413
473
  static [Symbol.hasInstance](x) {
414
- return x instanceof Object && '_type' in x && x['_type'].endsWith(BytesCls.name);
474
+ return isInstanceOfTypeByName(x, BytesCls);
415
475
  }
416
476
  static fromCompat(v) {
417
477
  if (v === undefined)
@@ -436,10 +496,10 @@ class BytesCls extends AlgoTsPrimitiveCls {
436
496
  .reduce((a, b) => a.concat(b));
437
497
  }
438
498
  static fromHex(hex) {
439
- return new BytesCls(Uint8Array.from(Buffer.from(hex, 'hex')));
499
+ return new BytesCls(hexToUint8Array(hex));
440
500
  }
441
501
  static fromBase64(b64) {
442
- return new BytesCls(Uint8Array.from(Buffer.from(b64, 'base64')));
502
+ return new BytesCls(base64ToUint8Array(b64));
443
503
  }
444
504
  static fromBase32(b32) {
445
505
  return new BytesCls(base32ToUint8Array(b32));
@@ -451,7 +511,7 @@ class BytesCls extends AlgoTsPrimitiveCls {
451
511
  return new BigUintCls(uint8ArrayToBigInt(this.#v));
452
512
  }
453
513
  toString() {
454
- return this.valueOf();
514
+ return uint8ArrayToUtf8(this.#v);
455
515
  }
456
516
  asAlgoTs() {
457
517
  return this;
@@ -460,12 +520,25 @@ class BytesCls extends AlgoTsPrimitiveCls {
460
520
  return this.#v;
461
521
  }
462
522
  }
463
- const arrayUtil = new (class {
523
+ const arrayUtil = new (class ArrayUtil {
464
524
  arrayAt(arrayLike, index) {
465
- return arrayLike.at(Uint64Cls.fromCompat(index).asNumber()) ?? avmError('Index out of bounds');
525
+ const indexNum = getNumber(index);
526
+ if (arrayLike instanceof Uint8Array) {
527
+ const res = arrayLike.slice(indexNum, indexNum === -1 ? undefined : indexNum + 1);
528
+ avmInvariant(res.length, 'Index out of bounds');
529
+ return res;
530
+ }
531
+ return arrayLike.at(indexNum) ?? avmError('Index out of bounds');
466
532
  }
467
533
  arraySlice(arrayLike, start, end) {
468
- return arrayLike.slice(Uint64Cls.getNumber(start), Uint64Cls.getNumber(end));
534
+ const startNum = start === undefined ? undefined : getNumber(start);
535
+ const endNum = end === undefined ? undefined : getNumber(end);
536
+ if (arrayLike instanceof Uint8Array) {
537
+ return arrayLike.slice(startNum, endNum);
538
+ }
539
+ else {
540
+ return arrayLike.slice(startNum, endNum);
541
+ }
469
542
  }
470
543
  })();
471
544
 
@@ -479,7 +552,7 @@ var primitives = /*#__PURE__*/Object.freeze({
479
552
  checkBigUint: checkBigUint,
480
553
  checkBytes: checkBytes,
481
554
  checkUint64: checkUint64,
482
- isBigUint: isBigUint,
555
+ getNumber: getNumber,
483
556
  isBytes: isBytes,
484
557
  isUint64: isUint64,
485
558
  toBytes: toBytes,
@@ -490,12 +563,30 @@ function Uint64(v) {
490
563
  return Uint64Cls.fromCompat(v).asAlgoTs();
491
564
  }
492
565
  function BigUint(v) {
566
+ if (typeof v === 'string')
567
+ v = BigInt(v);
568
+ else if (v === undefined)
569
+ v = 0n;
493
570
  return BigUintCls.fromCompat(v).asAlgoTs();
494
571
  }
495
572
  function Bytes(value, ...replacements) {
496
573
  if (isTemplateStringsArray(value)) {
497
574
  return BytesCls.fromInterpolation(value, replacements).asAlgoTs();
498
575
  }
576
+ else if (typeof value === 'bigint' || value instanceof BigUintCls) {
577
+ return BigUintCls.fromCompat(value).toBytes().asAlgoTs();
578
+ }
579
+ else if (typeof value === 'number' || value instanceof Uint64Cls) {
580
+ return Uint64Cls.fromCompat(value).toBytes().asAlgoTs();
581
+ }
582
+ else if (typeof value === 'object' && Symbol.iterator in value) {
583
+ const valueItems = Array.from(value).map((v) => getNumber(v));
584
+ const invalidValue = valueItems.find((v) => v < 0 && v > 255);
585
+ if (invalidValue) {
586
+ throw new CodeError(`Cannot convert ${invalidValue} to a byte`);
587
+ }
588
+ return new BytesCls(new Uint8Array(value)).asAlgoTs();
589
+ }
499
590
  else {
500
591
  return BytesCls.fromCompat(value).asAlgoTs();
501
592
  }
@@ -525,24 +616,6 @@ function isTemplateStringsArray(v) {
525
616
  return Boolean(v) && Array.isArray(v) && typeof v[0] === 'string';
526
617
  }
527
618
 
528
- const ctxMgr = {
529
- set instance(ctx) {
530
- const instance = global.puyaTsExecutionContext;
531
- if (instance != undefined)
532
- throw new Error('Execution context has already been set');
533
- global.puyaTsExecutionContext = ctx;
534
- },
535
- get instance() {
536
- const instance = global.puyaTsExecutionContext;
537
- if (instance == undefined)
538
- throw new Error('No execution context has been set');
539
- return instance;
540
- },
541
- reset() {
542
- global.puyaTsExecutionContext = undefined;
543
- },
544
- };
545
-
546
619
  function log(...args) {
547
620
  ctxMgr.instance.log(args.map(toBytes).reduce((left, right) => left.concat(right)));
548
621
  }
@@ -737,5 +810,5 @@ var index = /*#__PURE__*/Object.freeze({
737
810
  baremethod: baremethod
738
811
  });
739
812
 
740
- export { AssertError as A, BytesCls as B, Contract as C, DynamicArray as D, OpUpFeeSource as O, Str as S, Tuple as T, Uint64 as U, errors as a, err as b, ctxMgr as c, assert as d, encodingUtil as e, assertMatch as f, ensureBudget as g, abimethod as h, index as i, BaseContract as j, BigUint as k, log as l, match as m, Bytes as n, OnCompleteAction as o, primitives as p, baremethod as q, UintN as r, UFixedNxM as s, Byte as t, urange as u, Bool as v, StaticArray as w, Address as x };
741
- //# sourceMappingURL=index-7MfWJL4Q.js.map
813
+ export { AssertError as A, BytesCls as B, Contract as C, DynamicArray as D, OpUpFeeSource as O, Str as S, Tuple as T, Uint64 as U, errors as a, err as b, assert as c, assertMatch as d, encodingUtil as e, ensureBudget as f, abimethod as g, BaseContract as h, index as i, BigUint as j, Bytes as k, log as l, match as m, OnCompleteAction as n, baremethod as o, primitives as p, UintN as q, UFixedNxM as r, Byte as s, Bool as t, urange as u, StaticArray as v, Address as w };
814
+ //# sourceMappingURL=index-DffKkMrz.js.map