Geth + web3js 发送签名交易时发件人无效
Geth + web3js invalid sender when sending signed transaction
我正在使用 https://trezor.io/ 发送已签名的 t运行 操作
我已成功发送 t运行 次操作至:
- Truffle开发网
- 罗普斯滕
现在我使用的是私有本地 go-ethereum 节点,签名 t运行saction 的调用与 truffle 和 ropsten 的代码完全相同,发送时我得到 Invalid sender
t运行行动
在对此进行一些研究时,我发现这个错误是由于没有相同的 chainId 和 networkId 而产生的,我检查了我的配置以及我如何 运行 geth 节点和 chainId 与 networkId 相同
我在我的 geth 节点的 genesis.json 中指定了链 ID 10
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"clique": {
"period": 15,
"epoch": 30000
}
}
and i 运行 networkId 为 10 的节点:
geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcport 8545 --rpcaddr '192.168.1.244' --rpccorsdomain="*" --ws --wsaddr "192.168.1.244" --wsorigins "http://192.168.1.182" --wsport 8546 --wsapi 'personal,db,eth,net,web3,txpool,miner' --rpcapi 'personal,db,eth,net,web3,txpool,miner' --bootnodes 'enode://8235e42bec82ad8944dcf65b57d25b7a970d6e94f35961a188b2dfd306c6964f2d00d078e3bf1d9ccc6664112669d7ea9c04aa45a8ab9113aa8fe8a04b088f80@127.0.0.1:30310' --networkid 10 --gasprice '1' -unlock 'd770217581e0ca1265c88c9faaff81f5038b129f' --password node1/password.txt --mine console
知道为什么会发生这种情况吗?
我正在使用 geth 1.8 和 web3 1.0-beta33
我认为这与 geth 的配置有关,因为正如我所说,我已经使用相同的代码
向 Truffle dev 和 Ropsten 发送了 t运行sactions
这是我如何发送 t运行saction(源代码)https://github.com/ethereum/web3.js/issues/1669
我已经设法解决了这个问题,
除了在 geth 配置中指定链 ID 和 运行 节点外,还需要在要签名的交易中指明它
所以,正确的做法是:
1)
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"clique": {
"period": 15,
"epoch": 30000
}
}
2)
geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcport 8545 --rpcaddr '192.168.1.244' --rpccorsdomain="*" --ws --wsaddr "192.168.1.244" --wsorigins "http://192.168.1.182" --wsport 8546 --wsapi 'personal,db,eth,net,web3,txpool,miner' --rpcapi 'personal,db,eth,net,web3,txpool,miner' --bootnodes 'enode://8235e42bec82ad8944dcf65b57d25b7a970d6e94f35961a188b2dfd306c6964f2d00d078e3bf1d9ccc6664112669d7ea9c04aa45a8ab9113aa8fe8a04b088f80@127.0.0.1:30310' --networkid 10 --gasprice '1' -unlock 'd770217581e0ca1265c88c9faaff81f5038b129f' --password node1/password.txt --mine console
3)
创建原始交易(注意 chainId)
var tx = {
nonce: count ,
gasPrice: web3.toHex(gasPriceGwei*1e9),
gasLimit: web3.toHex(gasLimit),
to: CONTRACT_ADDRESS,
value: '0x00',
data: getData,
chainId:10,
from:"0xedff546ac229317df81ef9e6cb3b67c0e6425fa7"
};
let response = await this.trezorSignTx(tx);
4) 然后签署交易(这里也要注意chainId):
trezorSignTx= async(transaction)=> {
let trezor= await this.getTrezor();
// spend one change output
var address_n = "m/44'/1'/0'/0/0"
// var address_n = [44 | 0x80000000,
// 60 | 0x80000000,
// 0 | 0x80000000 ,
// 0 ]; // same, in raw form
var nonce = transaction.nonce.substring(2); // note - it is hex, not number!!!
var gas_price = transaction.gasPrice.substring(2);
var gas_limit = transaction.gasLimit.substring(2);
var to = transaction.to.substring(2);
// var value = '01'; // in hexadecimal, in wei - this is 1 wei
var value = transaction.value.substring(2); // in hexadecimal, in wei - this is about 18 ETC
var data = transaction.data.substring(2); // some contract data
// var data = null // for no data
var chain_id = 10; // 1 for ETH, 61 for ETC
console.log(transaction);
return new Promise (function (resolve,reject) {
trezor.ethereumSignTx(
address_n,
nonce,
gas_price,
gas_limit,
to,
value,
data,
chain_id,
function (response) {
if (response.success) {
console.log('Signature V (recovery parameter):', response.v); // number
console.log('Signature R component:', response.r); // bytes
console.log('Signature S component:', response.s); // bytes
resolve(response);
} else {
console.error('Error:', response.error); // error message
resolve(null);
}
});
})
}
编辑:跨平台帖子:Send signed transactions to Ropsten or Truffle develop network with Trezor (Hardware Wallet)
签名前:
rawTx.v = Buffer.from([chainId])
rawTx.nonce = await web3.utils.toHex(web3.eth.getTransactionCount(account))
我正在使用 https://trezor.io/ 发送已签名的 t运行 操作
我已成功发送 t运行 次操作至:
- Truffle开发网
- 罗普斯滕
现在我使用的是私有本地 go-ethereum 节点,签名 t运行saction 的调用与 truffle 和 ropsten 的代码完全相同,发送时我得到 Invalid sender
t运行行动
在对此进行一些研究时,我发现这个错误是由于没有相同的 chainId 和 networkId 而产生的,我检查了我的配置以及我如何 运行 geth 节点和 chainId 与 networkId 相同
我在我的 geth 节点的 genesis.json 中指定了链 ID 10
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"clique": {
"period": 15,
"epoch": 30000
}
}
and i 运行 networkId 为 10 的节点:
geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcport 8545 --rpcaddr '192.168.1.244' --rpccorsdomain="*" --ws --wsaddr "192.168.1.244" --wsorigins "http://192.168.1.182" --wsport 8546 --wsapi 'personal,db,eth,net,web3,txpool,miner' --rpcapi 'personal,db,eth,net,web3,txpool,miner' --bootnodes 'enode://8235e42bec82ad8944dcf65b57d25b7a970d6e94f35961a188b2dfd306c6964f2d00d078e3bf1d9ccc6664112669d7ea9c04aa45a8ab9113aa8fe8a04b088f80@127.0.0.1:30310' --networkid 10 --gasprice '1' -unlock 'd770217581e0ca1265c88c9faaff81f5038b129f' --password node1/password.txt --mine console
知道为什么会发生这种情况吗?
我正在使用 geth 1.8 和 web3 1.0-beta33
我认为这与 geth 的配置有关,因为正如我所说,我已经使用相同的代码
向 Truffle dev 和 Ropsten 发送了 t运行sactions这是我如何发送 t运行saction(源代码)https://github.com/ethereum/web3.js/issues/1669
我已经设法解决了这个问题,
除了在 geth 配置中指定链 ID 和 运行 节点外,还需要在要签名的交易中指明它
所以,正确的做法是:
1)
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"clique": {
"period": 15,
"epoch": 30000
}
}
2)
geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcport 8545 --rpcaddr '192.168.1.244' --rpccorsdomain="*" --ws --wsaddr "192.168.1.244" --wsorigins "http://192.168.1.182" --wsport 8546 --wsapi 'personal,db,eth,net,web3,txpool,miner' --rpcapi 'personal,db,eth,net,web3,txpool,miner' --bootnodes 'enode://8235e42bec82ad8944dcf65b57d25b7a970d6e94f35961a188b2dfd306c6964f2d00d078e3bf1d9ccc6664112669d7ea9c04aa45a8ab9113aa8fe8a04b088f80@127.0.0.1:30310' --networkid 10 --gasprice '1' -unlock 'd770217581e0ca1265c88c9faaff81f5038b129f' --password node1/password.txt --mine console
3) 创建原始交易(注意 chainId)
var tx = {
nonce: count ,
gasPrice: web3.toHex(gasPriceGwei*1e9),
gasLimit: web3.toHex(gasLimit),
to: CONTRACT_ADDRESS,
value: '0x00',
data: getData,
chainId:10,
from:"0xedff546ac229317df81ef9e6cb3b67c0e6425fa7"
};
let response = await this.trezorSignTx(tx);
4) 然后签署交易(这里也要注意chainId):
trezorSignTx= async(transaction)=> {
let trezor= await this.getTrezor();
// spend one change output
var address_n = "m/44'/1'/0'/0/0"
// var address_n = [44 | 0x80000000,
// 60 | 0x80000000,
// 0 | 0x80000000 ,
// 0 ]; // same, in raw form
var nonce = transaction.nonce.substring(2); // note - it is hex, not number!!!
var gas_price = transaction.gasPrice.substring(2);
var gas_limit = transaction.gasLimit.substring(2);
var to = transaction.to.substring(2);
// var value = '01'; // in hexadecimal, in wei - this is 1 wei
var value = transaction.value.substring(2); // in hexadecimal, in wei - this is about 18 ETC
var data = transaction.data.substring(2); // some contract data
// var data = null // for no data
var chain_id = 10; // 1 for ETH, 61 for ETC
console.log(transaction);
return new Promise (function (resolve,reject) {
trezor.ethereumSignTx(
address_n,
nonce,
gas_price,
gas_limit,
to,
value,
data,
chain_id,
function (response) {
if (response.success) {
console.log('Signature V (recovery parameter):', response.v); // number
console.log('Signature R component:', response.r); // bytes
console.log('Signature S component:', response.s); // bytes
resolve(response);
} else {
console.error('Error:', response.error); // error message
resolve(null);
}
});
})
}
编辑:跨平台帖子:Send signed transactions to Ropsten or Truffle develop network with Trezor (Hardware Wallet)
签名前:
rawTx.v = Buffer.from([chainId])
rawTx.nonce = await web3.utils.toHex(web3.eth.getTransactionCount(account))