I am using ethers to get a signed request. I have a signature for the address 0x518171C334AD3adc1cF990A884D67963201B675F
.
It is, c4b71f516c36468022d61b0e22016c6bc69bc2c99666fb7e63e3d04841c82ff53d5c687ebe43b290d03caa6766e8e957192faa6b360f4102b536c54c3318304f1b
.
When I use ethers.js, to serialize my transaction with my signature I get a transaction which is something like
0xf86a808477359400825208943e245df5a4de41e65cecd1f98b96ca06c3d319f087470de4df8200008029a0c4b71f516c36468022d61b0e22016c6bc69bc2c99666fb7e63e3d04841c82ff5a03d5c687ebe43b290d03caa6766e8e957192faa6b360f4102b536c54c3318304f
As it is an ECDSA signature, it keeps on changing every time I generate it. And every time I try to decode the serialized transaction I get a different from address.
This means that there must be something wrong in how I am using the function.
This is the code I am working with, with one of the signatures that I have generated (mentioned above)
const { utils } = require('ethers');
const bn = require('bignumber.js');
const { serializeTransaction, parseEther } = utils;
const tx2 = require('ethereumjs-tx');
async function serialize1() {
const wei = parseEther('0.02');
console.log(wei);
const gasPrice = '0x' + (2000000000).toString(16);
console.log(gasPrice);
const tx = {
gasPrice: gasPrice,
gasLimit: 21000,
to: '0x3e245df5a4de41e65cecd1f98b96ca06c3d319f0',
value: wei,
nonce: 0,
data: '',
chainId: 3,
};
const serializedTransaction = await serializeTransaction(tx);
console.log(serializedTransaction);
const signature = 'c4b71f516c36468022d61b0e22016c6bc69bc2c99666fb7e63e3d04841c82ff53d5c687ebe43b290d03caa6766e8e957192faa6b360f4102b536c54c3318304f1b';
console.log('---------------------------------------------');
const signedSerialized = await serializeTransaction(tx, Buffer.from(signature, 'hex'));
console.log(signedSerialized);
}
serialize1();
When I decode this transaction, I get
{
"nonce": 0,
"gasPrice": 2000000000,
"gasLimit": 21000,
"to": "0x3e245df5a4de41e65cecd1f98b96ca06c3d319f0",
"value": 20000000000000000,
"data": "",
"from": "0x0d67ffdd9b1d8858582d903bf0093e22b9da682e",
"r": "c4b71f516c36468022d61b0e22016c6bc69bc2c99666fb7e63e3d04841c82ff5",
"v": "29",
"s": "3d5c687ebe43b290d03caa6766e8e957192faa6b360f4102b536c54c3318304f"
}
Which is clearly not the from address I want. Am I using the serialize function in the correct way? Is the encoding that I am sending the signature in correct? Any other way I can use this signature? Any other package?
Any help will be greatly appreciated!!!
Best Answer
You don't sign a request with a signature, you sign a transaction. You cannot generate generic signatures to be associated to transactions later on, you need to have access the complete object you want to sign - the raw transaction in your case - before the process works correctly.
Each library and wallet has its own signing implementation. With ethers.js in particular, you can use the
Signer
object to sign a transaction. From the [ethers.js official documentation][1]:signer.signTransaction( transactionRequest ) ⇒ Promise< string< DataHexString > >
Signer usually refers to an external wallet - i.e MetaMask - not controlled by your ÐApp.
If instead you control the wallet with your app - so not a ÐApp anymore ;) - below some examples of signing transactions and messages with ethers.js and a wallet. You can find them inside the [Wallet official documentation][2] also: