From 273141507e37d1720ab9adc426b2c1f9c7b31ccf Mon Sep 17 00:00:00 2001 From: inphi Date: Wed, 8 Feb 2023 08:46:26 -0500 Subject: [PATCH] Fix EIP-4844 Signer hash Ensures that the EIP-4844 Signer (DankSigner) includes the appropriate chainID to the signing hash. --- core/types/data_blob_tx.go | 6 +++- core/types/transaction_signing.go | 4 ++- core/types/transaction_signing_test.go | 47 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/core/types/data_blob_tx.go b/core/types/data_blob_tx.go index b9fe8f720bb0..31a048b149fa 100644 --- a/core/types/data_blob_tx.go +++ b/core/types/data_blob_tx.go @@ -295,6 +295,10 @@ func (tx *BlobTxMessage) FixedLength() uint64 { return 0 } +func (tx *BlobTxMessage) setChainID(chainID *big.Int) { + (*uint256.Int)(&tx.ChainID).SetFromBig(chainID) +} + func (stx *SignedBlobTx) ByteLength() uint64 { return codec.ContainerLength(&stx.Message, &stx.Signature) } @@ -378,7 +382,7 @@ func (stx *SignedBlobTx) rawSignatureValues() (v, r, s *big.Int) { } func (stx *SignedBlobTx) setSignatureValues(chainID, v, r, s *big.Int) { - (*uint256.Int)(&stx.Message.ChainID).SetFromBig(chainID) + stx.Message.setChainID(chainID) stx.Signature.V = Uint8View(v.Uint64()) (*uint256.Int)(&stx.Signature.R).SetFromBig(r) (*uint256.Int)(&stx.Signature.S).SetFromBig(s) diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index c1a6afa63791..01e130df5ac8 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -229,7 +229,9 @@ func (s dankSigner) Hash(tx *Transaction) common.Hash { if tx.Type() != BlobTxType { return s.londonSigner.Hash(tx) } - return prefixedSSZHash(BlobTxType, &tx.inner.(*SignedBlobTx).Message) + messageSigning := tx.inner.(*SignedBlobTx).Message + messageSigning.setChainID(s.chainId) + return prefixedSSZHash(BlobTxType, &messageSigning) } type londonSigner struct{ eip2930Signer } diff --git a/core/types/transaction_signing_test.go b/core/types/transaction_signing_test.go index 2a9ceb09529f..6b207461e1da 100644 --- a/core/types/transaction_signing_test.go +++ b/core/types/transaction_signing_test.go @@ -24,6 +24,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" + "github.com/holiman/uint256" + "github.com/protolambda/ztyp/view" ) func TestEIP155Signing(t *testing.T) { @@ -77,6 +79,51 @@ func TestEIP155ChainId(t *testing.T) { } } +func TestEIP4844Signing(t *testing.T) { + key, _ := crypto.GenerateKey() + addr := crypto.PubkeyToAddress(key.PublicKey) + + signer := NewDankSigner(big.NewInt(18)) + txdata := &SignedBlobTx{ + Message: BlobTxMessage{ + Nonce: view.Uint64View(0), + Gas: view.Uint64View(123457), + To: AddressOptionalSSZ{Address: (*AddressSSZ)(&addr)}, + GasTipCap: view.Uint256View(*uint256.NewInt(42)), + GasFeeCap: view.Uint256View(*uint256.NewInt(10)), + MaxFeePerDataGas: view.Uint256View(*uint256.NewInt(10)), + Value: view.Uint256View(*uint256.NewInt(10)), + BlobVersionedHashes: VersionedHashesView{common.HexToHash("0x010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c444014")}, + }, + } + // This is the identity point serialised + var kzgProof KZGProof = [48]byte{192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + wrapData := &BlobTxWrapData{ + BlobKzgs: BlobKzgs{KZGCommitment{0: 0xc0}}, + Blobs: Blobs{Blob{}}, + KzgAggregatedProof: kzgProof, + } + tx := NewTx(txdata, WithTxWrapData(wrapData)) + tx, err := SignTx(tx, signer, key) + if err != nil { + t.Fatal(err) + } + if !tx.Protected() { + t.Fatal("expected tx to be protected") + } + + if tx.ChainId().Cmp(signer.ChainID()) != 0 { + t.Error("expected chainId to be", signer.ChainID(), "got", tx.ChainId()) + } + sender, err := Sender(signer, tx) + if err != nil { + t.Fatal(err) + } + if sender != addr { + t.Error("expected sender to be", addr, "got", sender) + } +} + func TestEIP155SigningVitalik(t *testing.T) { // Test vectors come from http://vitalik.ca/files/eip155_testvec.txt for i, test := range []struct {