Can't send transactions for chains with large id
Hi there. I have issues for this blockchain:
Basecamp (testnet). Chain id: 123420001114
When I try to call a contract function, I get this error:
Invalid v 2026866392 value for chain ID 123420001114. Invalid chain ID? (Eth::Chain::ReplayProtectionError)
Here is a snippet of my code. I use it with my contract but you can take another one, if you want, it doesn't really matter.
abi = [{
"name": "claimCustom",
"type": "function",
"inputs": [
{"name": "accountAddress", "type": "address", "internalType": "address"},
{"name": "amount", "type": "uint256", "internalType": "uint256"}
],
"outputs": [],
"stateMutability": "nonpayable"
}]
contract = Eth::Contract.from_abi(
name: 'Dummy',
address: '0x38feA0959656FF4009a8d807aDaa5247dbFEB590',
abi: JSON.parse(abi.to_json)
)
args = [
contract,
'claimCustom',
'0x0000000000000000000000000000000000000000',
1,
]
kwargs = {sender_key: Eth::Key.new, gas_limit: 100000}
client = Eth::Client.create('https://rpc.basecamp.t.raas.gelato.cloud')
client.transact(*args, **kwargs)
It raises Invalid v 2026866391 value for chain ID 123420001114. Invalid chain ID? (Eth::Chain::ReplayProtectionError)
My thoughts on the issue.
As far as I understand, this error appears because of the large chain id.
When we call to_v here
https://github.com/q9f/eth.rb/blob/ad012f1f5a1e4c0ab20fa809c3526e1fd722086f/lib/eth/key.rb#L76
v is 246840002263
And then the lib tries to [v].pack("N") here
https://github.com/q9f/eth.rb/blob/ad012f1f5a1e4c0ab20fa809c3526e1fd722086f/lib/eth/key.rb#L78
where N is the uint32 (max value is 4294967296). But v is larger than max uint32, so pack trims the bytes, rendering invalid signature.
When I replace [v].pack("N") with [v].pack("Q").reverse (N - is uint32 big endian, Q - is uint64 native endian, so I reverse the bytes order. Q> also should work for ruby > 1.9.4), the transaction broadcasts successfully.
Well, what are your thoughts on this? Seems like this is the solution but I dont really know if Q is safe to use here.