From 3e9a005d6cef073dad3d6bfe69cb3293001a1c41 Mon Sep 17 00:00:00 2001 From: Dai Long Date: Thu, 8 Dec 2022 01:30:16 +0800 Subject: [PATCH 1/2] Add interface Executor --- packages/algob/src/internal/deployer.ts | 6 +-- packages/runtime/src/runtime.ts | 2 +- packages/web/src/index.ts | 1 + packages/web/src/lib/executor.ts | 58 +++++++++++++++++++++ packages/web/src/lib/myalgowallet-mode.ts | 16 +++--- packages/web/src/lib/wallectconnect-mode.ts | 22 ++++---- packages/web/src/lib/web-mode.ts | 14 +++-- 7 files changed, 95 insertions(+), 24 deletions(-) create mode 100644 packages/web/src/lib/executor.ts diff --git a/packages/algob/src/internal/deployer.ts b/packages/algob/src/internal/deployer.ts index 2ba23b3c3..3fcc01542 100644 --- a/packages/algob/src/internal/deployer.ts +++ b/packages/algob/src/internal/deployer.ts @@ -4,7 +4,7 @@ import { types as rtypes, validateOptInAccNames, } from "@algo-builder/runtime"; -import { BuilderError, ERRORS, types as wtypes, utils } from "@algo-builder/web"; +import { BuilderError, ERRORS, Executor, types as wtypes, utils } from "@algo-builder/web"; import { mkTransaction } from "@algo-builder/web/build/lib/txn"; import type { Account, @@ -599,7 +599,7 @@ class DeployerBasicMode { /** * This class is what user interacts with in deploy task */ -export class DeployerDeployMode extends DeployerBasicMode implements Deployer { +export class DeployerDeployMode extends DeployerBasicMode implements Deployer, Executor { get isDeployMode(): boolean { return true; } @@ -990,7 +990,7 @@ export class DeployerDeployMode extends DeployerBasicMode implements Deployer { /** * This class is what user interacts with in run task mode */ -export class DeployerRunMode extends DeployerBasicMode implements Deployer { +export class DeployerRunMode extends DeployerBasicMode implements Deployer, Executor { get isDeployMode(): boolean { return false; } diff --git a/packages/runtime/src/runtime.ts b/packages/runtime/src/runtime.ts index e9f20a0a7..8aa0e9b2e 100644 --- a/packages/runtime/src/runtime.ts +++ b/packages/runtime/src/runtime.ts @@ -1,4 +1,4 @@ -import { parsing, tx as webTx, types } from "@algo-builder/web"; +import { Executor, parsing, tx as webTx, types } from "@algo-builder/web"; import { runtimeGenesisHash } from "@algo-builder/web/build/lib/constants"; import algosdk, { Account as AccountSDK, diff --git a/packages/web/src/index.ts b/packages/web/src/index.ts index 6d20fead3..0ec26cb8f 100644 --- a/packages/web/src/index.ts +++ b/packages/web/src/index.ts @@ -11,6 +11,7 @@ export { WebMode } from "./lib/web-mode"; export { WallectConnectSession } from "./lib/wallectconnect-mode"; export { MyAlgoWalletSession } from "./lib/myalgowallet-mode"; export { getSuggestedParams, mkTxParams } from "./lib/api"; +export { Executor } from "./lib/executor"; export { mainnetURL, testnetURL, diff --git a/packages/web/src/lib/executor.ts b/packages/web/src/lib/executor.ts new file mode 100644 index 000000000..fd3a75b67 --- /dev/null +++ b/packages/web/src/lib/executor.ts @@ -0,0 +1,58 @@ +import algosdk, { Account, SignedTransaction, Transaction } from "algosdk"; + +import { ExecParams, Sign, TransactionAndSign, TxnReceipt } from "../types"; + +export interface Executor { + /** + * Execute single transaction or group of transactions (atomic transaction) + * @param execParams transaction parameters or atomic transaction parameters + */ + executeTx( + transactions: ExecParams[] | TransactionAndSign[] | algosdk.SignedTransaction[], + debugStack?: number + ): Promise | TxnReceipt | Promise | TxnReceipt[]; + + /** + * Creates an algosdk.Transaction object based on execParams and suggestedParams + * @param execParams execParams containing all txn info + * @param txParams suggestedParams object + * @returns array of algosdk.Transaction objects + */ + makeTx(execParams: ExecParams[], txParams: algosdk.SuggestedParams): Transaction[]; + + /** + * Signes a Transaction object with the provided account + * @param transaction transaction object. + * @param signer account object that signes the transaction + * @returns SignedTransaction + */ + signTx( + transaction: algosdk.Transaction, + signer?: Account | Sign + ): SignedTransaction | Promise; + + /** + * Creates an algosdk.Transaction object based on execParams and suggestedParams + * and signs it using provided signer account + * @param execParams execParams containing all txn info + * @param txParams suggestedParams object + * @param signer account object that signes the transaction + * @returns array of algosdk.SignedTransaction objects + */ + makeAndSignTx( + execParams: ExecParams[], + txParams: algosdk.SuggestedParams, + signer?: Account | Sign + ): SignedTransaction[] | Promise; + + /** + * Sends signedTransaction and waits for the response + * @param transactions array of signedTransaction objects. + * @param rounds number of rounds to wait for response + * @returns TxnReceipt which includes confirmed txn response along with txID + */ + sendTxAndWait( + transactions: SignedTransaction[], + rounds?: number + ): Promise | TxnReceipt[]; +} diff --git a/packages/web/src/lib/myalgowallet-mode.ts b/packages/web/src/lib/myalgowallet-mode.ts index cfef5d248..787a06619 100644 --- a/packages/web/src/lib/myalgowallet-mode.ts +++ b/packages/web/src/lib/myalgowallet-mode.ts @@ -22,9 +22,9 @@ import { } from "../types"; import { algoexplorerAlgod } from "./api"; import { WAIT_ROUNDS } from "./constants"; +import { Executor } from "./executor"; import { error, log } from "./logger"; import { mkTransaction } from "./txn"; - interface MyAlgoConnect { /** * @async @@ -68,7 +68,7 @@ interface MyAlgoConnect { signLogicSig(logic: Uint8Array | Base64, address: Address): Promise; } -export class MyAlgoWalletSession { +export class MyAlgoWalletSession implements Executor { connector!: MyAlgoConnect; private readonly algodClient: algosdk.Algodv2; accounts: Accounts[] = []; @@ -100,7 +100,7 @@ export class MyAlgoWalletSession { */ async signLogic(logic: string | Uint8Array, address: string): Promise { try { - return await this.connector.signLogicSig(logic, address) + return await this.connector.signLogicSig(logic, address); } catch (err) { error(err); throw new Error("Error while signing teal program" + err); @@ -114,12 +114,15 @@ export class MyAlgoWalletSession { * @returns Returns txID and blob object * for more info: https://developer.algorand.org/docs/get-details/dapps/smart-contracts/smartsigs/modes/#contract-account */ - signLogicSigTx(transaction: Transaction, logicSig: algosdk.LogicSigAccount): { txID: string, blob: Uint8Array } { + signLogicSigTx( + transaction: Transaction, + logicSig: algosdk.LogicSigAccount + ): { txID: string; blob: Uint8Array } { try { - return algosdk.signLogicSigTransaction(transaction, logicSig) + return algosdk.signLogicSigTransaction(transaction, logicSig); } catch (err) { error(err); - throw err + throw err; } } @@ -147,6 +150,7 @@ export class MyAlgoWalletSession { * https://connect.myalgo.com/docs/interactive-examples/PaymentTransaction * Sign a single transaction from a my algo wallet session * @param txn { SDK transaction object, shouldSign, signers, msig } object + * @param signOptions Sign Transaction Options * @returns raw signed txn */ async signTransaction( diff --git a/packages/web/src/lib/wallectconnect-mode.ts b/packages/web/src/lib/wallectconnect-mode.ts index 5fffdb501..42fc50e54 100644 --- a/packages/web/src/lib/wallectconnect-mode.ts +++ b/packages/web/src/lib/wallectconnect-mode.ts @@ -20,10 +20,11 @@ import { } from "../types"; import { algoexplorerAlgod, mkTxParams } from "./api"; import { ALGORAND_SIGN_TRANSACTION_REQUEST, WAIT_ROUNDS } from "./constants"; +import { Executor } from "./executor"; import { error, log, warn } from "./logger"; import { mkTransaction } from "./txn"; -export class WallectConnectSession { +export class WallectConnectSession implements Executor { readonly connector: WalletConnect; private readonly algodClient: algosdk.Algodv2; wcAccounts: string[]; @@ -164,12 +165,15 @@ export class WallectConnectSession { * @returns Returns txID and blob object * for more info: https://developer.algorand.org/docs/get-details/dapps/smart-contracts/smartsigs/modes/#contract-account */ - signLogicSigTx(transaction: Transaction, logicSig: algosdk.LogicSigAccount): { txID: string, blob: Uint8Array } { + signLogicSigTx( + transaction: Transaction, + logicSig: algosdk.LogicSigAccount + ): { txID: string; blob: Uint8Array } { try { - return algosdk.signLogicSigTransaction(transaction, logicSig) + return algosdk.signLogicSigTransaction(transaction, logicSig); } catch (err) { error(err); - throw err + throw err; } } @@ -298,11 +302,11 @@ export class WallectConnectSession { return txn.sign === SignType.LogicSignature ? { txn: txns[index], shouldSign: false } // logic signature : { - txn: txns[index], - shouldSign: true, - signers: - execParams[index].fromAccount?.addr || execParams[index].fromAccountAddr, - }; // to be signed + txn: txns[index], + shouldSign: true, + signers: + execParams[index].fromAccount?.addr || execParams[index].fromAccountAddr, + }; // to be signed } ); // only shouldSign txn are to be signed diff --git a/packages/web/src/lib/web-mode.ts b/packages/web/src/lib/web-mode.ts index ce4eb8990..1e1a7c8fc 100644 --- a/packages/web/src/lib/web-mode.ts +++ b/packages/web/src/lib/web-mode.ts @@ -19,13 +19,14 @@ import { TxParams, } from "../types"; import { WAIT_ROUNDS } from "./constants"; +import { Executor } from "./executor"; import { error, log } from "./logger"; import { mkTransaction } from "./txn"; const CONFIRMED_ROUND = "confirmed-round"; const LAST_ROUND = "last-round"; -export class WebMode { +export class WebMode implements Executor { algoSigner: AlgoSigner; chainName: string; @@ -241,12 +242,15 @@ export class WebMode { * @returns Returns txID and blob object * for more info: https://developer.algorand.org/docs/get-details/dapps/smart-contracts/smartsigs/modes/#contract-account */ - signLogicSigTx(transaction: Transaction, logicSig: algosdk.LogicSigAccount): { txID: string, blob: Uint8Array } { + signLogicSigTx( + transaction: Transaction, + logicSig: algosdk.LogicSigAccount + ): { txID: string; blob: Uint8Array } { try { - return algosdk.signLogicSigTransaction(transaction, logicSig) + return algosdk.signLogicSigTransaction(transaction, logicSig); } catch (err) { error(err); - throw err + throw err; } } @@ -314,7 +318,7 @@ export class WebMode { const signer: Sign = execParams[index]; if (signer.sign === SignType.LogicSignature) { signer.lsig.lsig.args = signer.args ? signer.args : []; - const lsigTxn = this.signLogicSigTx(txn, signer.lsig) + const lsigTxn = this.signLogicSigTx(txn, signer.lsig); if (!Array.isArray(signedTxn)) signedTxn = []; // only logic signature txn are provided signedTxn.splice(index, 0, { blob: this.algoSigner.encoding.msgpackToBase64(lsigTxn.blob), From 82f152e7172833844a24f82bb2fd851647715e57 Mon Sep 17 00:00:00 2001 From: Dai Long Date: Thu, 8 Dec 2022 01:39:24 +0800 Subject: [PATCH 2/2] merge develop to branch --- yarn.lock | 9 --------- 1 file changed, 9 deletions(-) diff --git a/yarn.lock b/yarn.lock index 0b3240bba..d7cb0183f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5535,15 +5535,6 @@ __metadata: languageName: node linkType: hard -"qs@npm:^6.9.4": - version: 6.11.0 - resolution: "qs@npm:6.11.0" - dependencies: - side-channel: ^1.0.4 - checksum: 6e1f29dd5385f7488ec74ac7b6c92f4d09a90408882d0c208414a34dd33badc1a621019d4c799a3df15ab9b1d0292f97c1dd71dc7c045e69f81a8064e5af7297 - languageName: node - linkType: hard - "query-string@npm:6.13.5": version: 6.13.5 resolution: "query-string@npm:6.13.5"