Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interface Executor #859

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/algob/src/internal/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
58 changes: 58 additions & 0 deletions packages/web/src/lib/executor.ts
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Execute single transaction or group of transactions (atomic transaction)
* Executes (creates, signs and sends) a 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> | TxnReceipt | Promise<TxnReceipt[]> | 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<SignedTransaction>;

/**
* 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<SignedTransaction[]>;

/**
* 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> | TxnReceipt[];
}
16 changes: 10 additions & 6 deletions packages/web/src/lib/myalgowallet-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,7 +68,7 @@ interface MyAlgoConnect {
signLogicSig(logic: Uint8Array | Base64, address: Address): Promise<Uint8Array>;
}

export class MyAlgoWalletSession {
export class MyAlgoWalletSession implements Executor {
connector!: MyAlgoConnect;
private readonly algodClient: algosdk.Algodv2;
accounts: Accounts[] = [];
Expand Down Expand Up @@ -100,7 +100,7 @@ export class MyAlgoWalletSession {
*/
async signLogic(logic: string | Uint8Array, address: string): Promise<Uint8Array> {
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);
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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(
Expand Down
22 changes: 13 additions & 9 deletions packages/web/src/lib/wallectconnect-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions packages/web/src/lib/web-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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),
Expand Down
9 changes: 0 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down