Skip to content

Commit

Permalink
Fix bug: keccak256(rlpTx) for signing eip-155
Browse files Browse the repository at this point in the history
  • Loading branch information
kubo39 committed Nov 16, 2024
1 parent d668aa7 commit 547f6f8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions source/deth/rpcconnector.d
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ unittest

Transaction tx = {
to: bob,
gas: 21_000.wei,
gasPrice: 0.wei,
value: 16.wei,
data: cast(bytes) "\xdd\xdd\xdd\xdd Dlang - Fast code, fast.",
};
Expand Down Expand Up @@ -332,7 +334,10 @@ unittest
assert(conn.accounts[0] == alice);

Transaction tx = {
from: alice,
to: bob,
gas: 21_000.wei,
gasPrice: 0.wei,
value: 16.wei,
data: cast(bytes) "\xdd\xdd\xdd\xdd Dlang - Fast code, fast.",
chainid: conn.net_version.to!ulong,
Expand Down
9 changes: 5 additions & 4 deletions source/deth/wallet.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module deth.wallet;

import std.experimental.logger;
import std.exception : enforce;
import secp256k1 : secp256k1;
import deth.util : Address, Hash, bytes, convTo, Transaction, ox;
import deth.util.rlp : rlpEncode, cutBytes;
import deth.util.types;
import keccak : keccak256;
import secp256k1 : secp256k1;

/// struct to store several private keys in a single wallet
struct Wallet
Expand Down Expand Up @@ -58,8 +60,6 @@ struct Wallet
/// Returns: rlp encoded signed transaction
bytes signTransaction(const Transaction tx, Address signer = Address.init) @safe pure
{
import keccak : keccak256;
import deth.util.types;

if (!tx.from.isNull)
{
Expand All @@ -73,10 +73,10 @@ struct Wallet
auto c = addrs[signer];
bytes rlpTx = tx.serialize.rlpEncode;
debug logf("Rlp encoded tx %s", rlpTx.toHexString.ox);
auto signature = c.sign(rlpTx);
bytes rawTx;
if (tx.chainid.isNull)
{
auto signature = c.sign(rlpTx);
ulong v = 27 + signature.recid;
rawTx = rlpEncode(
tx.serialize ~ v.convTo!bytes.cutBytes
Expand All @@ -85,6 +85,7 @@ struct Wallet
else
{
/// eip 155 signing
auto signature = c.sign(keccak256(rlpTx));
ulong v = signature.recid + tx.chainid.get * 2 + 35;
rawTx = rlpEncode(tx.serialize[0 .. $ - 3] ~ [
v.convTo!bytes.cutBytes, signature.r.cutBytes,
Expand Down

0 comments on commit 547f6f8

Please sign in to comment.