Signing your Transaction
There are many ways to sign a transaction prior to submitting it to the Ethereum network. Any method that produces the standard hex-encoded RLP payload representing the transaction will work with TDN. Below is an example of how to sign a transaction with ethers.js, which you may substitute with another method if you prefer.
Example transaction signing with ethers.js:
// First import Ethers.js
const ethers = require("ethers")
// NOTE: We can use Ethers' provider to get the latest nonce.
// If you are keeping track of nonce another way, this isn’t required!
const ethersProvider = ethers.getDefaultProvider("rinkeby")
// We initialize a wallet using a private key, which allows us to sign under that wallet address
const wallet = new ethers.Wallet('')
async function makeTx() {
return {
to: '0xf527A5Ee2155fAD99A5bBB23c9e52B0a11b99dD4', // replace with desired ‘to’ address
value: ethers.utils.parseEther('0.001'),
gasLimit: 41000,
// Uncomment this if you would like to use type 2 transactions
// maxPriorityFeePerGas: ethers.utils.parseUnits('5', 'gwei'),
// maxFeePerGas: ethers.utils.parseUnits('20', 'gwei'),
gasPrice: ethers.utils.parseUnits('144', 'gwei'),
nonce: await ethersProvider.getTransactionCount(wallet.address),
type: 0,
data:...// this must be a hex string prefixed with '0x'
chainId: 4, // this must match the network you pass to getDefaultProvider
}
}
async function main() {
// We now sign this transaction object. The `signedTransaction` variable
// contains the RLP-encoded hex string that goes in the request payload.
const signedTransaction = await wallet.signTransaction(await makeTx())
console.log(signedTransaction)
}
main() // run everything
For Javascript, check out a runnable Gist example.
For Go, check out the Go Ethereum Book section on creating a transaction.
Copy link