Understanding the Transaction Nonce
Mainsail transactions use a sequential nonce to protect against double-spending , long-range attacks, key-leakage as a result of signature reuse, and side-channel attacks associated with random nonces.
A sequential nonce effectively counts each outgoing transaction from a given wallet. This means that the first transaction from a wallet must have a nonce of 1, the second transaction must have a nonce of 2, and so on.
This ensures that the data contained within a particular transaction will always be unique and thus results in a distinct hash that will necessarily produce a unique signature.
key facts:
- The 1st transaction from a wallet must have a nonce of 1.
- The nonce must increment sequentially for every subsequent transaction being sent. (e.g. 2, 3, 4, 5, 6, 7, …)
- A nonce cannot be reused.
- A nonce cannot be skipped. (a transaction with a nonce of 5 originating from a wallet with a nonce of 3 will be rejected.)
funky stuff here:
- The 1st transaction from a wallet must have a nonce of 1.
- The nonce must increment sequentially for every subsequent transaction being sent.
- e.g. 2, 3, 4, 5, 6, 7, …
- A nonce cannot be skipped.
- a transaction with a nonce of 5 originating from a wallet with a nonce of 3 will be rejected.
- A nonce cannot be reused.
How To Get Nonce Value For An Address?
A sequential nonce depends on the amount of transaction a specific wallet has sent. You can find the current nonce for a wallet by utilizing the Public API, more specifically the wallet endpoint. The wallet endpoint returns the wallet details, including the current wallets nonce field, like below:
1{ 2 "data": { 3 "address": "D8rr7B1d6TL6pf14LgMz4sKp1VBMs6YUYD", 4 "publicKey": "03df6cd794a7d404db4f1b25816d8976d0e72c5177d17ac9b19a92703b62cdbbbc", 5 "nonce": "123", // THIS IS WALLETS CURRENT NONCE VALUE 6 "balance": "7919999400", 7 "attributes": { 8 ... 9 ...10 ...11 },12 "lockedBalance": "0",13 "isDelegate": false,14 "isResigned": false15 }16}
How To Set Nonce Value?
When you create a new transaction for that wallet, you will use the current nonce and add 1 to it to get to the new nonce value.
Information
If you retrieved a nonce value of 123 for a wallet, the next transaction will have to use nonce 124, (current_nonce + 1). The API will increase the nonce value once a transaction has been forged for the wallet.
Sending Multiple Transactions
You have to keep track locally of the next nonce value in case you intend to send multiple transactions in a single block.
For example, we have a wallet with nonce 123 and want to send 3 transactions to be forged in the next block. These transactions will require nonce values 124, 125 and 126 respectively, and you will have to set the values, before creating transactions.
After the block is forged, the API will report the current nonce of the wallet to be 126.