Skip to content

Commit

Permalink
Hash extensions for EIP-155 signing
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
  • Loading branch information
peterbroadhurst committed Apr 30, 2022
1 parent b7c0f86 commit a41fc19
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions pkg/ethsigner/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func (t *Transaction) BuildLegacy() rlp.List {
return rlpList
}

func (t *Transaction) AddEIP155HashValues(rlpList rlp.List, chainID int64) rlp.List {
// These values go into the hash of the transaction
rlpList = append(rlpList, rlp.WrapInt(big.NewInt(chainID)))
rlpList = append(rlpList, rlp.WrapInt(big.NewInt(0)))
rlpList = append(rlpList, rlp.WrapInt(big.NewInt(0)))
return rlpList
}

func (t *Transaction) Build1559(chainID int64) rlp.List {
rlpList := make(rlp.List, 0, 9)
rlpList = append(rlpList, rlp.WrapInt(big.NewInt(chainID)))
Expand Down Expand Up @@ -96,16 +104,18 @@ func (t *Transaction) SignLegacyOriginal(signer *secp256k1.KeyPair) ([]byte, err
func (t *Transaction) SignLegacyEIP155(signer *secp256k1.KeyPair, chainID int64) ([]byte, error) {
rlpList := t.BuildLegacy()

txData := rlpList.Encode()
sig, err := signer.Sign(txData)
rlpList = t.AddEIP155HashValues(rlpList, chainID)

hashData := rlpList.Encode()
sig, err := signer.Sign(hashData)
if err != nil {
return nil, err
}

// Use the EIP-155 V value, of (2*ChainID + 35 + Y-parity)
sig.UpdateEIP155(chainID)

rlpList = t.addSignature(rlpList, sig)
rlpList = t.addSignature(rlpList[0:6] /* we don't include the chainID+0+0 hash values in the payload */, sig)
return rlpList.Encode(), nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ethsigner/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestSignAutoEIP155(t *testing.T) {
foundSig.R.SetBytes([]byte(rlpList.(rlp.List)[7].(rlp.Data)))
foundSig.S.SetBytes([]byte(rlpList.(rlp.List)[8].(rlp.Data)))

expectedUnsigned := txn.BuildLegacy().Encode()
expectedUnsigned := txn.AddEIP155HashValues(txn.BuildLegacy(), 1001).Encode()
addr, err := foundSig.Recover(expectedUnsigned, 1001)
assert.NoError(t, err)
assert.Equal(t, keypair.Address.String(), addr.String())
Expand Down

0 comments on commit a41fc19

Please sign in to comment.