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 support for TransactionSigner #44

Open
smonn opened this issue Sep 13, 2022 · 1 comment
Open

Add support for TransactionSigner #44

smonn opened this issue Sep 13, 2022 · 1 comment
Assignees
Labels
Enhancement New feature or request

Comments

@smonn
Copy link

smonn commented Sep 13, 2022

The TransactionSigner works really well with the AtomicTransactionComposer, which is really helpful when interacting with smart contracts or group/atomic transactions. The TransactionSigner interface/type is defined as:

/**
 * This type represents a function which can sign transactions from an atomic transaction group.
 * @param txnGroup - The atomic group containing transactions to be signed
 * @param indexesToSign - An array of indexes in the atomic transaction group that should be signed
 * @returns A promise which resolves an array of encoded signed transactions. The length of the
 *   array will be the same as the length of indexesToSign, and each index i in the array
 *   corresponds to the signed transaction from txnGroup[indexesToSign[i]]
 */
export declare type TransactionSigner = (txnGroup: Transaction[], indexesToSign: number[]) => Promise<Uint8Array[]>;

A basic implementation of it would look like this (but should probably live in the PeraWalletConnect type/class):

function peraWalletSigner(peraWallet: PeraWalletConnect): TransactionSigner {
  return async (txnGroup, indexesToSign) => {
    return await peraWallet.signTransaction([
      txnGroup.map((txn, index) => {
        if (indexesToSign.includes(index)) {
          return {
            txn,
          };
        }

        return {
          txn,
          signers: [],
        };
      }),
    ]);
  };
}

Usage:

const peraWallet = new PeraWalletConnect();
const client = new Algodv2(/* ... */);
const atc = new AtomicTransactionComposer();
const sender = '....' // account address from peraWallet.connect() or peraWallet.reconnectSession()
const suggestedParams = await client.getTransactionParams().do();

atc.addTransaction({
  signer: peraWalletSigner(peraWallet),
  txn: algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    suggestedParams,
    from: sender,
    amount: 0,
    to: sender,
    note: new Uint8Array(Buffer.from("Hello World", "utf-8")),
  }),
});

const result = await atc.execute(client, 5)
@mucahit mucahit self-assigned this Nov 15, 2022
@mucahit mucahit added the Enhancement New feature or request label Nov 15, 2022
@barnjamin
Copy link

+1 please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants