Skip to content

Transactions 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:

rust
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;
}
See code in context

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:

ts
import { BaseAssetId, Script } from 'fuels';

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>();
See code in context

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

rust
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;
}
See code in context

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

ts
import { Predicate, BaseAssetId, ScriptTransactionRequest } from 'fuels';

// 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();
See code in context