Skip to content

Commit

Permalink
Feature: MultiSig support in executeTx of webmode (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeena840 authored Sep 15, 2022
1 parent 2895711 commit 72798a9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ from `algosdk` and sends it to the network.

#### @algo-builder/web
- Added `appendSignMultisigTransaction` function to `WebMode` for appending signature to multisig transaction in the algosigner.
- Added `MultiSignature` support in `executeTx` method for `AlgoSigner`.

### Bug Fixes

Expand Down
28 changes: 18 additions & 10 deletions packages/web/src/lib/web-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
SignType,
TransactionAndSign,
TxParams,
SignWithMultisig
} from "../types";
import { WAIT_ROUNDS } from "./constants";
import { log } from "./logger";
Expand Down Expand Up @@ -156,14 +157,14 @@ export class WebMode {
* return an object containing a blob attribute encoded in base64
*/
async appendSignMultisigTransaction(txns: WalletTransaction[], signers: string[]): Promise<JsonPayload> {
const result:JsonPayload = {};
const result: JsonPayload = {};
for (let i = 0; i < txns.length; ++i) {
const txn = txns[i];
const partialTxn = algosdk.decodeObj(
this.algoSigner.encoding.base64ToMsgpack(txn.txn)) as EncodedSignedTransaction;
this.algoSigner.encoding.base64ToMsgpack(txn.txn)) as EncodedSignedTransaction;
if (partialTxn.txn === undefined || partialTxn.msig === undefined) {
throw new Error("Input transaction must be multisigature transaction signed with at least 1 signature");
}
}
const txnToBeSign = algosdk.Transaction.from_obj_for_encoding(partialTxn.txn);
const txnToBeSign_Uint8Array = algosdk.encodeObj(txnToBeSign.get_obj_for_encoding());
const txnToBeSign_Base64 = this.algoSigner.encoding.msgpackToBase64(txnToBeSign_Uint8Array);
Expand Down Expand Up @@ -242,19 +243,26 @@ export class WebMode {

// with logic signature we don't need signers.
const toBeSignedTxns = base64Txs.map((txn: string, txnId: number) => {
return execParams[txnId].sign === SignType.LogicSignature
? { txn: txn, signers: [] } // logic signature
: { txn: txn, authAddr: execParams[txnId].fromAccount?.addr }; // set signer
switch (execParams[txnId].sign) {
case SignType.LogicSignature:
return { txn: txn, signers: [] } // logic signature
case SignType.MultiSignature: {
const msig: SignWithMultisig = execParams[txnId] as SignWithMultisig;
return { txn: txn, msig: msig.mparams }; // multi singature
}
default:
return { txn: txn, authAddr: execParams[txnId].fromAccount?.addr }; // set signer
}
});

const signedTxn = await this.signTransaction(toBeSignedTxns);

// sign smart signature transaction
for (const [txnId, txn] of txns.entries()) {
const singer: Sign = execParams[txnId];
if (singer.sign === SignType.LogicSignature) {
singer.lsig.lsig.args = singer.args ? singer.args : [];
const lsigTxn = algosdk.signLogicSigTransaction(txn, singer.lsig);
const signer: Sign = execParams[txnId];
if (signer.sign === SignType.LogicSignature) {
signer.lsig.lsig.args = signer.args ? signer.args : [];
const lsigTxn = algosdk.signLogicSigTransaction(txn, signer.lsig);
signedTxn[txnId] = {
blob: this.algoSigner.encoding.msgpackToBase64(lsigTxn.blob),
txId: lsigTxn.txID,
Expand Down
20 changes: 14 additions & 6 deletions packages/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type ExecParams =
export enum SignType {
SecretKey,
LogicSignature,
MultiSignature
}

export enum TransactionType {
Expand Down Expand Up @@ -158,9 +159,16 @@ interface SignWithLsig {
args?: Uint8Array[];
}

export interface SignWithMultisig {
sign: SignType.MultiSignature;
mparams: WalletMultisigMetadata;
fromAccount?: AccountSDK;
fromAccountAddr: AccountAddress;
}

export type Lsig = SignWithLsig;

export type Sign = SignWithSk | SignWithLsig;
export type Sign = SignWithSk | SignWithLsig | SignWithMultisig;

export type BasicParams = Sign & {
payFlags: TxParams;
Expand Down Expand Up @@ -235,11 +243,11 @@ export type UpdateAppParam = BasicParams &
export type AppCallsParam = BasicParams &
AppOptionalFlags & {
type:
| TransactionType.CallApp
| TransactionType.ClearApp
| TransactionType.CloseApp
| TransactionType.DeleteApp
| TransactionType.OptInToApp;
| TransactionType.CallApp
| TransactionType.ClearApp
| TransactionType.CloseApp
| TransactionType.DeleteApp
| TransactionType.OptInToApp;
appID: number;
};

Expand Down

0 comments on commit 72798a9

Please sign in to comment.