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:
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:
We can interact with this predicate in the SDK with the following implementation:
// #import { Predicate, BaseAssetId, ScriptTransactionRequest };// Create and fund the predicateconst predicate =newPredicate<[string]>({ bytecode, abi, provider, inputData: [signer.address.toB256()],});await sender.transfer(predicate.address, 10_000, BaseAssetId);// Create the transaction requestconst request =newScriptTransactionRequest({ gasPrice, gasLimit:10_000 });request.addCoinOutput(receiver.address, amountToReceiver, BaseAssetId);// Get the predicate resources and add them and predicate data to the requestconst resources =await predicate.getResourcesToSpend([ { assetId: BaseAssetId, amount: amountToReceiver, },]);request.addPredicateResources(resources, predicate);const parsedRequest = predicate.populateTransactionPredicateData(request);// Add witnesses including the signerparsedRequest.addWitness('0x');await parsedRequest.addAccountWitnesses(signer);// Estimate the predicate inputsconst { estimatedInputs } =await provider.getTransactionCost(parsedRequest);parsedRequest.updatePredicateInputs(estimatedInputs);// Send the transactionconst res =await provider.sendTransaction(parsedRequest);await res.waitForResult();