๐ฎโโ๏ธSignatures in Stellar: Who, how much and to what is authorized
Last updated
Last updated
If youโve been following the previous chapter from this learning path, youโll now know that signatures are the way Stellar validates a transaction (and all of the operations within it) are authorized. Now, signing a transaction with the correct signature is just the first step, today weโll take deep dive into signatures, authorization and transactions validation in Stellar and will approach Each operation in Stellar falls under a specific threshold category: low, medium, or high. You can think of thresholds as the level of authorization required to perform an specific operation for each account, which should translate to how much weight a signature requires to perform an operation. In the List of Operations provided by Stellar you can find the specific threshold for each operation, and later, in your account you can find it specified as a number between 0 and 255.
By default all operations thresholds are set to 0, but you can specify a weight for each threshold for your account using the set options operation. Keep in mind the recommendation is always to set the lower one for the Low Threshold and the highest for the High one.
Now that you understand thresholds the next thing youโll want to approach is signatures weights. By default each account has a single signer, also named as master, whose original weight is 1. But in some cases youโll want to add more than one signature, for usage purposes or for safety. In this cases you could wanna set a higher authorization level for an specific account, to enable it to perform more delicate operations and a lower one for others to perform daily operations
Note that if you set thresholds higher than the one set for your Master key and thereโs no other signer set for the related account youโll block any fund in that account, as no other account will be able to sign these transactions.
The set operations will also allow you to add several signers, for each one of them youโll need to specify a type and set a weight. If youโd like to remove a signer you can simply set the weight to 0 and submit the transaction
Once you have added signers what Stellar is gonna do when authorizing your transaction is to add up the weight for all of the signers accounts to check if that sums up to the threshold required. Once a signature threshold is met, if there are leftover signatures, the transaction will fail. For example, if your transaction requires three signatures, providing more than three signatures, even if they are all valid, will result in a failed transaction error: TX_BAD_AUTH_EXTRA
.
To enable some advanced smart contract features there are a couple of additional signature types. These signature types also have weights and can be added and removed similarly to normal signature types
Pre authorized transactions You can pre-approve a transaction by adding the hash of the following transaction as a signer. To do this, you need to prepare the transaction with the correct sequence number, get its hash, and add it as a signer. Once the transaction is applied (whether it succeeds or fails), the signer is automatically removed. If the transaction is never submitted, the signer stays on the account and must be manually removed using the Set Options operation.
Hash signatures This type of signature will allow anyone who knows x to sign the transaction. It is particularly useful in atomic cross-chain swaps which are needed for inter-blockchain protocols like lightning networks. To use it, start by creating a random 256-bit value, which we call x. The SHA256 hash of that value can be added as a signer of type hash(x). Then in order to authorize a transaction, x is added as one of the signatures of the transaction. Keep in mind that x will be known to the world as soon as a transaction is submitted to the network with x as a signature. This means anyone will be able to sign for that account with the hash(x) signer at that point. Often you want there to be additional signers so someone must have a particular secret key and know x in order to reach the weight threshold required to authorize transactions on the account.
Thereโs many cases in which a multisig account will be the best way to go. Think of a share account that should allow all users to perform basic operations such as payments or setting offers but should require the authorization of all users to perform a change in the account setting or to merge it. Or think of a company account, which should allow a mix of admins to perform payments to ensure procedures but also allow a higher member to perform the operation solo by assigning them a higher signature weight.
Conclusion
Signatures in Stellar play a crucial role in transaction validation and authorization. By understanding thresholds, signature weights, and advanced signature types like pre-authorized transactions and hash(x) signatures, you can manage account security and enable complex use cases like atomic cross-chain swaps. Carefully configuring thresholds and signers ensures both flexibility and safety, preventing issues such as locked accounts or failed transactions due to mismatched authorization levels.