Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Transactions With Multiple Signers

Icon LinkTransactions with Multiple Signers

When a transaction contains a spendable input such as a coin, it must also contain the signature of the coin owner for it to be spent. If the coin owner is also submitting the transaction, then this is straightforward. However, if an external address is required to sign the transaction, then the transaction must contain multiple signatures. Within the SDK, an account signature can be added to a transaction by calling addAccountWitnesses on the transaction request.

Consider a script that requires two signatures to be spent:

script;
 
use std::{b512::B512, ecr::ec_recover_address, tx::{tx_id, tx_witness_data}};
 
fn main(signer: b256) -> bool {
    let witness_data: B512 = tx_witness_data(1);
    let address: b256 = ec_recover_address(witness_data, tx_id()).unwrap().value;
    return address == signer;
}

In the snippet above, we use the built-in Sway function tx_witness_data() to retrieve the witness signatures and tx_id() for the transaction hash. Then, we retrieve the signing address to validate the script.

We would interact with this script in the SDK by creating a transaction request from an invocation scope. The same can be done for a contract. Consider the following script:

// #import { Script, BaseAssetId };
 
const script = new Script(bytecode, abi, sender);
const { value } = await script.functions
  .main(signer.address.toB256())
  .addTransfer(receiver.address, amountToReceiver, BaseAssetId)
  .addSigners(signer)
  .call<BN>();

The same approach can be used for a predicate by instantiating it and adding it to a transaction request. Consider the following predicate:

predicate;
 
use std::{b512::B512, ecr::ec_recover_address, tx::{tx_id, tx_witness_data}};
 
fn main(signer: b256) -> bool {
    let witness_data: B512 = tx_witness_data(1);
    let address: b256 = ec_recover_address(witness_data, tx_id()).unwrap().value;
    return address == signer;
}

We can interact with this predicate in the SDK with the following implementation:

// #import { Predicate, BaseAssetId, ScriptTransactionRequest };
 
// Create and fund the predicate
const predicate = new Predicate<[string]>({
  bytecode,
  abi,
  provider,
  inputData: [signer.address.toB256()],
});
await sender.transfer(predicate.address, 10_000, BaseAssetId);
 
// Create the transaction request
const request = new ScriptTransactionRequest({ gasPrice, gasLimit: 10_000 });
request.addCoinOutput(receiver.address, amountToReceiver, BaseAssetId);
 
// Get the predicate resources and add them and predicate data to the request
const resources = await predicate.getResourcesToSpend([
  {
    assetId: BaseAssetId,
    amount: amountToReceiver,
  },
]);
request.addPredicateResources(resources, predicate);
const parsedRequest = predicate.populateTransactionPredicateData(request);
 
// Add witnesses including the signer
parsedRequest.addWitness('0x');
await parsedRequest.addAccountWitnesses(signer);
 
// Estimate the predicate inputs
const { estimatedInputs } = await provider.getTransactionCost(parsedRequest);
parsedRequest.updatePredicateInputs(estimatedInputs);
 
// Send the transaction
const res = await provider.sendTransaction(parsedRequest);
await res.waitForResult();