@algorandfoundation/algorand-typescript-testing 1.0.0-alpha.14 → 1.0.0-alpha.16

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.
@@ -0,0 +1,3 @@
1
+ import { BaseContract, CompileContractOptions, CompiledContract, CompiledLogicSig, CompileLogicSigOptions, LogicSig } from '@algorandfoundation/algorand-typescript';
2
+ import { ConstructorFor } from '../typescript-helpers';
3
+ export declare function compileImpl(artefact: ConstructorFor<BaseContract> | ConstructorFor<LogicSig>, options?: CompileContractOptions | CompileLogicSigOptions): CompiledLogicSig | CompiledContract;
package/impl/emit.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function emitImpl<T>(typeInfoString: string, event: T | string, ...eventProps: unknown[]): void;
@@ -29,7 +29,7 @@ export declare class UFixedNxMImpl<N extends BitSize, M extends number> extends
29
29
  static getArc4TypeName: (t: TypeInfo) => string;
30
30
  }
31
31
  export declare class ByteImpl extends Byte {
32
- typeInfo: TypeInfo | string;
32
+ typeInfo: TypeInfo;
33
33
  private value;
34
34
  constructor(typeInfo: TypeInfo | string, v?: CompatForArc4Int<8>);
35
35
  get native(): uint64;
@@ -39,7 +39,7 @@ export declare class ByteImpl extends Byte {
39
39
  static getMaxBitsLength(typeInfo: TypeInfo): BitSize;
40
40
  }
41
41
  export declare class StrImpl extends Str {
42
- typeInfo: TypeInfo | string;
42
+ typeInfo: TypeInfo;
43
43
  private value;
44
44
  constructor(typeInfo: TypeInfo | string, s?: StringCompat);
45
45
  get native(): string;
@@ -48,8 +48,8 @@ export declare class StrImpl extends Str {
48
48
  static fromBytesImpl(value: internal.primitives.StubBytesCompat | Uint8Array, typeInfo: string | TypeInfo, prefix?: 'none' | 'log'): StrImpl;
49
49
  }
50
50
  export declare class BoolImpl extends Bool {
51
- typeInfo: TypeInfo | string;
52
51
  private value;
52
+ typeInfo: TypeInfo;
53
53
  constructor(typeInfo: TypeInfo | string, v?: boolean);
54
54
  get native(): boolean;
55
55
  equals(other: this): boolean;
@@ -177,6 +177,7 @@ export declare function interpretAsArc4Impl<T extends ARC4Encoded>(typeInfoStrin
177
177
  export declare const arc4Encoders: Record<string, fromBytes<DeliberateAny>>;
178
178
  export declare const getArc4Encoder: <T>(typeInfo: TypeInfo, encoders?: Record<string, fromBytes<DeliberateAny>>) => fromBytes<T>;
179
179
  export declare const getArc4TypeName: (typeInfo: TypeInfo) => string | undefined;
180
- export declare function decodeArc4Impl<T>(sourceTypeInfoString: string, bytes: internal.primitives.StubBytesCompat): T;
180
+ export declare function decodeArc4Impl<T>(sourceTypeInfoString: string, bytes: internal.primitives.StubBytesCompat, prefix?: 'none' | 'log'): T;
181
181
  export declare function encodeArc4Impl<T>(_targetTypeInfoString: string, source: T): bytes;
182
+ export declare const getArc4Encoded: (value: DeliberateAny) => ARC4Encoded;
182
183
  export {};
package/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
- import { Bytes, Uint64, Account, internal, Base64, arc4, Ecdsa, op, TransactionType, Asset, Application, BigUint, BaseContract, Contract } from '@algorandfoundation/algorand-typescript';
2
- import { a as asMaybeUint64Cls, l as lazyContext, b as asUint64, c as asBytes, t as toBytes, d as asMaybeBytesCls, e as asBigUint, B as BITS_IN_BYTE, M as MAX_BYTES_SIZE, f as asUint64Cls, g as asBytesCls, h as MAX_UINT8, i as binaryStringToBytes, U as UINT64_SIZE, j as MAX_UINT64, k as asNumber, m as MAX_BOX_SIZE, n as asUint8Array, o as conactUint8Arrays, P as PROGRAM_TAG, L as LOGIC_DATA_PREFIX, p as getObjectReference, q as getApplicationAddress, r as MIN_TXN_FEE, D as DEFAULT_ACCOUNT_MIN_BALANCE, s as DEFAULT_MAX_TXN_LIFE, Z as ZERO_ADDRESS, u as DEFAULT_ASSET_CREATE_MIN_BALANCE, v as DEFAULT_ASSET_OPT_IN_MIN_BALANCE, w as DEFAULT_GLOBAL_GENESIS_HASH, x as combineIntoMaxBytePages, y as MAX_ITEMS_IN_LOG, A as ABI_RETURN_VALUE_LOG_PREFIX, z as getRandomBytes, C as getArc4Encoder, E as AccountCls, F as ApplicationCls, G as AssetCls, H as arc4Encoders, I as AccountMap, J as getGenericTypeInfo, K as getArc4Selector, N as isContractProxy, O as getContractMethodAbiMetadata, Q as copyAbiMetadatas, R as getContractAbiMetadata, S as BytesMap, T as iterBigInt, V as Uint64Map, W as AssetHolding$1, X as asBigInt, Y as TRANSACTION_GROUP_MAX_SIZE, _ as getRandomBigInt, $ as AccountData, a0 as ApplicationData, a1 as AddressImpl, a2 as UintNImpl, a3 as getRandomNumber, a4 as MAX_UINT16, a5 as MAX_UINT32, a6 as MAX_UINT128, a7 as MAX_UINT256, a8 as MAX_UINT512, a9 as DynamicBytesImpl, aa as StrImpl, ab as captureMethodConfig, ac as DEFAULT_TEMPLATE_VAR_PREFIX } from './runtime-helpers-C5d5vNab.js';
3
- import { ec } from 'elliptic';
4
- import { sha256 as sha256$1 } from 'js-sha256';
5
- import { sha3_256 as sha3_256$1, keccak256 as keccak256$1 } from 'js-sha3';
6
- import { sha512_256 as sha512_256$1 } from 'js-sha512';
7
- import nacl from 'tweetnacl';
1
+ import { Bytes, Uint64, Account, internal, Base64, op, arc4, TransactionType, Asset, Application, BigUint, BaseContract, Contract } from '@algorandfoundation/algorand-typescript';
2
+ import { a as asMaybeUint64Cls, l as lazyContext, b as asUint64, c as asBytes, t as toBytes, d as asMaybeBytesCls, e as asBigUint, B as BITS_IN_BYTE, M as MAX_BYTES_SIZE, f as asUint64Cls, g as asBytesCls, h as testInvariant, i as MAX_UINT8, j as binaryStringToBytes, U as UINT64_SIZE, n as notImplementedError, k as MAX_UINT64, m as asNumber, o as MAX_BOX_SIZE, p as asUint8Array, q as conactUint8Arrays, r as getObjectReference, s as getApplicationAddress, u as MIN_TXN_FEE, D as DEFAULT_ACCOUNT_MIN_BALANCE, v as DEFAULT_MAX_TXN_LIFE, Z as ZERO_ADDRESS, w as DEFAULT_ASSET_CREATE_MIN_BALANCE, x as DEFAULT_ASSET_OPT_IN_MIN_BALANCE, y as DEFAULT_GLOBAL_GENESIS_HASH, E as EllipticCurve, z as ecdsaPkDecompress, A as ecdsaPkRecover, C as ecdsaVerify, F as ed25519verify, G as ed25519verifyBare, H as keccak256, I as sha256, J as sha3_256, K as sha512_256, L as vrfVerify, N as combineIntoMaxBytePages, O as MAX_ITEMS_IN_LOG, P as ABI_RETURN_VALUE_LOG_PREFIX, Q as getRandomBytes, R as getArc4Encoder, S as AccountCls, T as ApplicationCls, V as AssetCls, W as arc4Encoders, X as AccountMap, Y as getGenericTypeInfo, _ as getArc4Selector, $ as isContractProxy, a0 as getContractMethodAbiMetadata, a1 as copyAbiMetadatas, a2 as getContractAbiMetadata, a3 as BytesMap, a4 as iterBigInt, a5 as Uint64Map, a6 as AssetHolding$1, a7 as asBigInt, a8 as TRANSACTION_GROUP_MAX_SIZE, a9 as getRandomBigInt, aa as AccountData, ab as ApplicationData, ac as AddressImpl, ad as UintNImpl, ae as getRandomNumber, af as MAX_UINT16, ag as MAX_UINT32, ah as MAX_UINT128, ai as MAX_UINT256, aj as MAX_UINT512, ak as DynamicBytesImpl, al as StrImpl, am as captureMethodConfig, an as DEFAULT_TEMPLATE_VAR_PREFIX } from './runtime-helpers-qRDREJih.js';
8
3
  import { randomBytes } from 'crypto';
9
4
  import '@algorandfoundation/algorand-typescript/arc4';
10
5
  import '@algorandfoundation/puya-ts';
6
+ import 'js-sha512';
11
7
  import 'assert';
8
+ import 'elliptic';
9
+ import 'js-sha256';
10
+ import 'js-sha3';
11
+ import 'tweetnacl';
12
12
 
13
13
  const resolveAppIndex = (appIdOrIndex) => {
14
14
  const input = asUint64(appIdOrIndex);
@@ -331,20 +331,6 @@ const AssetHolding = {
331
331
  },
332
332
  };
333
333
 
334
- class NotImplementedError extends Error {
335
- constructor(feature) {
336
- super(`${feature} is not available in test context. Mock using your preferred testing framework.`);
337
- }
338
- }
339
- function notImplementedError(feature) {
340
- throw new NotImplementedError(feature);
341
- }
342
- function testInvariant(condition, message) {
343
- if (!condition) {
344
- throw new internal.errors.InternalError(message);
345
- }
346
- }
347
-
348
334
  const addw = (a, b) => {
349
335
  const uint64A = internal.primitives.Uint64Cls.fromCompat(a);
350
336
  const uint64B = internal.primitives.Uint64Cls.fromCompat(b);
@@ -773,94 +759,6 @@ const Box$1 = {
773
759
  },
774
760
  };
775
761
 
776
- const sha256 = (a) => {
777
- const bytesA = internal.primitives.BytesCls.fromCompat(a);
778
- const hashArray = sha256$1.create().update(bytesA.asUint8Array()).digest();
779
- const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
780
- return hashBytes.asAlgoTs();
781
- };
782
- const sha3_256 = (a) => {
783
- const bytesA = internal.primitives.BytesCls.fromCompat(a);
784
- const hashArray = sha3_256$1.create().update(bytesA.asUint8Array()).digest();
785
- const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
786
- return hashBytes.asAlgoTs();
787
- };
788
- const keccak256 = (a) => {
789
- const bytesA = internal.primitives.BytesCls.fromCompat(a);
790
- const hashArray = keccak256$1.create().update(bytesA.asUint8Array()).digest();
791
- const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
792
- return hashBytes.asAlgoTs();
793
- };
794
- const sha512_256 = (a) => {
795
- const bytesA = internal.primitives.BytesCls.fromCompat(a);
796
- const hashArray = sha512_256$1.create().update(bytesA.asUint8Array()).digest();
797
- const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
798
- return hashBytes.asAlgoTs();
799
- };
800
- const ed25519verifyBare = (a, b, c) => {
801
- const bytesA = internal.primitives.BytesCls.fromCompat(a);
802
- const bytesB = internal.primitives.BytesCls.fromCompat(b);
803
- const bytesC = internal.primitives.BytesCls.fromCompat(c);
804
- return nacl.sign.detached.verify(bytesA.asUint8Array(), bytesB.asUint8Array(), bytesC.asUint8Array());
805
- };
806
- const ed25519verify = (a, b, c) => {
807
- const txn = lazyContext.activeGroup.activeTransaction;
808
- const programBytes = asBytesCls(txn.onCompletion == arc4.OnCompleteAction[arc4.OnCompleteAction.ClearState] ? txn.clearStateProgram : txn.approvalProgram);
809
- const logicSig = conactUint8Arrays(asUint8Array(PROGRAM_TAG), programBytes.asUint8Array());
810
- const logicSigAddress = sha512_256$1.array(logicSig);
811
- const addressBytes = Bytes(logicSigAddress);
812
- const data = LOGIC_DATA_PREFIX.concat(addressBytes).concat(asBytes(a));
813
- return ed25519verifyBare(data, b, c);
814
- };
815
- const ecdsaVerify = (v, a, b, c, d, e) => {
816
- const dataBytes = internal.primitives.BytesCls.fromCompat(a);
817
- const sigRBytes = internal.primitives.BytesCls.fromCompat(b);
818
- const sigSBytes = internal.primitives.BytesCls.fromCompat(c);
819
- const pubkeyXBytes = internal.primitives.BytesCls.fromCompat(d);
820
- const pubkeyYBytes = internal.primitives.BytesCls.fromCompat(e);
821
- const publicKey = internal.primitives.BytesCls.fromCompat(new Uint8Array([0x04]))
822
- .concat(pubkeyXBytes)
823
- .concat(pubkeyYBytes);
824
- const ecdsa = new ec(curveMap[v]);
825
- const keyPair = ecdsa.keyFromPublic(publicKey.asUint8Array());
826
- return keyPair.verify(dataBytes.asUint8Array(), { r: sigRBytes.asUint8Array(), s: sigSBytes.asUint8Array() });
827
- };
828
- const ecdsaPkRecover = (v, a, b, c, d) => {
829
- if (v !== Ecdsa.Secp256k1) {
830
- internal.errors.internalError(`Unsupported ECDSA curve: ${v}`);
831
- }
832
- const dataBytes = internal.primitives.BytesCls.fromCompat(a);
833
- const rBytes = internal.primitives.BytesCls.fromCompat(c);
834
- const sBytes = internal.primitives.BytesCls.fromCompat(d);
835
- const recoveryId = internal.primitives.Uint64Cls.fromCompat(b);
836
- const ecdsa = new ec(curveMap[v]);
837
- const pubKey = ecdsa.recoverPubKey(dataBytes.asUint8Array(), { r: rBytes.asUint8Array(), s: sBytes.asUint8Array() }, recoveryId.asNumber());
838
- const x = pubKey.getX().toArray('be');
839
- const y = pubKey.getY().toArray('be');
840
- return [Bytes(x), Bytes(y)];
841
- };
842
- const ecdsaPkDecompress = (v, a) => {
843
- const bytesA = internal.primitives.BytesCls.fromCompat(a);
844
- const ecdsa = new ec(curveMap[v]);
845
- const keyPair = ecdsa.keyFromPublic(bytesA.asUint8Array());
846
- const pubKey = keyPair.getPublic();
847
- const x = pubKey.getX().toArray('be');
848
- const y = pubKey.getY().toArray('be');
849
- return [Bytes(new Uint8Array(x)), Bytes(new Uint8Array(y))];
850
- };
851
- const vrfVerify = (_s, _a, _b, _c) => {
852
- notImplementedError('vrfVerify');
853
- };
854
- const EllipticCurve = new Proxy({}, {
855
- get: (_target, prop) => {
856
- notImplementedError(`EllipticCurve.${prop.toString()}`);
857
- },
858
- });
859
- const curveMap = {
860
- [Ecdsa.Secp256k1]: 'secp256k1',
861
- [Ecdsa.Secp256r1]: 'p256',
862
- };
863
-
864
762
  class GlobalData {
865
763
  minTxnFee;
866
764
  minBalance;
@@ -2966,8 +2864,15 @@ class ApplicationInnerTxn extends ApplicationTransaction {
2966
2864
  /* @internal */
2967
2865
  constructor(fields) {
2968
2866
  const { appId, approvalProgram, clearStateProgram, onCompletion, appArgs, accounts, assets, apps, ...rest } = mapCommonFields(fields);
2867
+ const compiledApp = appId === undefined && approvalProgram !== undefined
2868
+ ? lazyContext.ledger.getApplicationForApprovalProgram(approvalProgram)
2869
+ : undefined;
2969
2870
  super({
2970
- appId: appId instanceof internal.primitives.Uint64Cls ? getApp(appId) : appId,
2871
+ appId: appId === undefined && compiledApp
2872
+ ? compiledApp
2873
+ : appId instanceof internal.primitives.Uint64Cls
2874
+ ? getApp(appId)
2875
+ : appId,
2971
2876
  onCompletion: typeof onCompletion === 'string'
2972
2877
  ? onCompletion
2973
2878
  : onCompletion !== undefined
@@ -2981,6 +2886,7 @@ class ApplicationInnerTxn extends ApplicationTransaction {
2981
2886
  accounts: accounts?.map((x) => x),
2982
2887
  assets: assets?.map((x) => x),
2983
2888
  apps: apps?.map((x) => x),
2889
+ createdApp: compiledApp,
2984
2890
  ...rest,
2985
2891
  });
2986
2892
  }
@@ -3664,6 +3570,27 @@ class LedgerContext {
3664
3570
  }
3665
3571
  throw internal.errors.internalError('Unknown contract, check correct testing context is active');
3666
3572
  }
3573
+ getApplicationForApprovalProgram(approvalProgram) {
3574
+ if (approvalProgram === undefined) {
3575
+ return undefined;
3576
+ }
3577
+ const entries = this.applicationDataMap.entries();
3578
+ let next = entries.next().value;
3579
+ let found = false;
3580
+ while (next && !found) {
3581
+ found = next[1].application.approvalProgram === approvalProgram;
3582
+ if (!found) {
3583
+ next = entries.next().value;
3584
+ }
3585
+ }
3586
+ if (found && next) {
3587
+ const appId = asUint64(next[0]);
3588
+ if (this.applicationDataMap.has(appId)) {
3589
+ return Application(appId);
3590
+ }
3591
+ }
3592
+ return undefined;
3593
+ }
3667
3594
  /**
3668
3595
  * Update asset holdings for account, only specified values will be updated.
3669
3596
  * Account will also be opted-in to asset
@@ -4308,6 +4235,8 @@ class TestExecutionContext {
4308
4235
  #defaultSender;
4309
4236
  #activeLogicSigArgs;
4310
4237
  #template_vars = {};
4238
+ #compiledApps = [];
4239
+ #compiledLogicSigs = [];
4311
4240
  constructor(defaultSenderAddress) {
4312
4241
  internal.ctxMgr.instance = this;
4313
4242
  this.#contractContext = new ContractContext();
@@ -4401,8 +4330,32 @@ class TestExecutionContext {
4401
4330
  this.#activeLogicSigArgs = [];
4402
4331
  }
4403
4332
  }
4404
- setTemplateVar(name, value) {
4405
- this.#template_vars[DEFAULT_TEMPLATE_VAR_PREFIX + name] = value;
4333
+ setTemplateVar(name, value, prefix) {
4334
+ this.#template_vars[(prefix ?? DEFAULT_TEMPLATE_VAR_PREFIX) + name] = value;
4335
+ }
4336
+ getCompiledApp(contract) {
4337
+ return this.#compiledApps.find(([c, _]) => c === contract);
4338
+ }
4339
+ setCompiledApp(c, appId) {
4340
+ const existing = this.getCompiledApp(c);
4341
+ if (existing) {
4342
+ existing[1] = appId;
4343
+ }
4344
+ else {
4345
+ this.#compiledApps.push([c, appId]);
4346
+ }
4347
+ }
4348
+ getCompiledLogicSig(logicsig) {
4349
+ return this.#compiledLogicSigs.find(([c, _]) => c === logicsig);
4350
+ }
4351
+ setCompiledLogicSig(c, account) {
4352
+ const existing = this.getCompiledLogicSig(c);
4353
+ if (existing) {
4354
+ existing[1] = account;
4355
+ }
4356
+ else {
4357
+ this.#compiledLogicSigs.push([c, account]);
4358
+ }
4406
4359
  }
4407
4360
  reset() {
4408
4361
  this.#contractContext = new ContractContext();
@@ -4410,6 +4363,7 @@ class TestExecutionContext {
4410
4363
  this.#txnContext = new TransactionContext();
4411
4364
  this.#activeLogicSigArgs = [];
4412
4365
  this.#template_vars = {};
4366
+ this.#compiledApps = [];
4413
4367
  internal.ctxMgr.reset();
4414
4368
  internal.ctxMgr.instance = this;
4415
4369
  }