Skip to content

Commit

Permalink
core/types: don't modify signature V when reading large chainID (#30157)
Browse files Browse the repository at this point in the history
  • Loading branch information
danyalprout authored Jul 15, 2024
1 parent 169aa91 commit a0d2613
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/types/transaction_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,6 @@ func deriveChainId(v *big.Int) *big.Int {
}
return new(big.Int).SetUint64((v - 35) / 2)
}
v.Sub(v, big.NewInt(35))
return v.Rsh(v, 1)
vCopy := new(big.Int).Sub(v, big.NewInt(35))
return vCopy.Rsh(vCopy, 1)
}
35 changes: 35 additions & 0 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,41 @@ func TestTransactionCoding(t *testing.T) {
}
}

func TestLegacyTransaction_ConsistentV_LargeChainIds(t *testing.T) {
chainId := new(big.Int).SetUint64(13317435930671861669)

txdata := &LegacyTx{
Nonce: 1,
Gas: 1,
GasPrice: big.NewInt(2),
Data: []byte("abcdef"),
}

key, err := crypto.GenerateKey()
if err != nil {
t.Fatalf("could not generate key: %v", err)
}

tx, err := SignNewTx(key, NewEIP2930Signer(chainId), txdata)
if err != nil {
t.Fatalf("could not sign transaction: %v", err)
}

// Make a copy of the initial V value
preV, _, _ := tx.RawSignatureValues()
preV = new(big.Int).Set(preV)

if tx.ChainId().Cmp(chainId) != 0 {
t.Fatalf("wrong chain id: %v", tx.ChainId())
}

v, _, _ := tx.RawSignatureValues()

if v.Cmp(preV) != 0 {
t.Fatalf("wrong v value: %v", v)
}
}

func encodeDecodeJSON(tx *Transaction) (*Transaction, error) {
data, err := json.Marshal(tx)
if err != nil {
Expand Down

0 comments on commit a0d2613

Please sign in to comment.