Skip to content

Commit

Permalink
Merge pull request ethereum#101 from mdehoog/eip4844-tests
Browse files Browse the repository at this point in the history
Fix EIP-4844 Signer hash
  • Loading branch information
Inphi authored Feb 8, 2023
2 parents 6cc08ec + 2731415 commit dc1f4a3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
6 changes: 5 additions & 1 deletion core/types/data_blob_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion core/types/transaction_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
47 changes: 47 additions & 0 deletions core/types/transaction_signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit dc1f4a3

Please sign in to comment.