From eceab538ce78e5d33f8c778552798529f5b9063b Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 18 Dec 2024 13:30:16 +0100 Subject: [PATCH] core/types: change parameter order of SignAuth and rename The signing key should be passed first, because it is usually in a variable, while the authorization will often be a literal. Compare a, err := types.SignAuthorization(types.SetCodeAuthorization{ ChainID: getChainID(), Nonce: getNonce(), }, key) with a, err := types.SignAuthorization(key, types.SetCodeAuthorization{ ChainID: getChainID(), Nonce: getNonce(), }) --- core/blockchain_test.go | 8 ++++---- core/types/tx_setcode.go | 18 ++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index c756374b5a..74b7c7b020 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -4273,16 +4273,16 @@ func TestEIP7702(t *testing.T) { // 1. tx -> addr1 which is delegated to 0xaaaa // 2. addr1:0xaaaa calls into addr2:0xbbbb // 3. addr2:0xbbbb writes to storage - auth1, _ := types.SignAuth(types.SetCodeAuthorization{ + auth1, _ := types.SignAuthorization(key1, types.SetCodeAuthorization{ ChainID: gspec.Config.ChainID.Uint64(), Address: aa, Nonce: 1, - }, key1) - auth2, _ := types.SignAuth(types.SetCodeAuthorization{ + }) + auth2, _ := types.SignAuthorization(key2, types.SetCodeAuthorization{ ChainID: 0, Address: bb, Nonce: 0, - }, key2) + }) _, blocks, _ := GenerateChainWithGenesis(gspec, engine, 1, func(i int, b *BlockGen) { b.SetCoinbase(aa) diff --git a/core/types/tx_setcode.go b/core/types/tx_setcode.go index 324bab8d60..92d6ac29dd 100644 --- a/core/types/tx_setcode.go +++ b/core/types/tx_setcode.go @@ -87,28 +87,22 @@ type authorizationMarshaling struct { S hexutil.U256 } -// SignAuth signs the provided authorization. -func SignAuth(auth SetCodeAuthorization, prv *ecdsa.PrivateKey) (SetCodeAuthorization, error) { +// SignAuthorization sets the signature of a code authorization. +func SignAuthorization(prv *ecdsa.PrivateKey, auth SetCodeAuthorization) (SetCodeAuthorization, error) { sighash := auth.sigHash() sig, err := crypto.Sign(sighash[:], prv) if err != nil { return SetCodeAuthorization{}, err } - return auth.withSignature(sig), nil -} - -// withSignature updates the signature of an Authorization to be equal the -// decoded signature provided in sig. -func (a *SetCodeAuthorization) withSignature(sig []byte) SetCodeAuthorization { r, s, _ := decodeSignature(sig) return SetCodeAuthorization{ - ChainID: a.ChainID, - Address: a.Address, - Nonce: a.Nonce, + ChainID: auth.ChainID, + Address: auth.Address, + Nonce: auth.Nonce, V: sig[64], R: *uint256.MustFromBig(r), S: *uint256.MustFromBig(s), - } + }, nil } func (a *SetCodeAuthorization) sigHash() common.Hash {