core/types: change Authorization signature value to uint256

This commit is contained in:
Felix Lange 2024-12-02 12:07:30 +01:00 committed by lightclient
parent b8fb4d68de
commit 1506e06dca
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E
4 changed files with 36 additions and 41 deletions

View file

@ -5,10 +5,10 @@ package types
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/holiman/uint256"
) )
var _ = (*authorizationMarshaling)(nil) var _ = (*authorizationMarshaling)(nil)
@ -20,16 +20,16 @@ func (a Authorization) MarshalJSON() ([]byte, error) {
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
Nonce hexutil.Uint64 `json:"nonce" gencodec:"required"` Nonce hexutil.Uint64 `json:"nonce" gencodec:"required"`
V hexutil.Uint64 `json:"v" gencodec:"required"` V hexutil.Uint64 `json:"v" gencodec:"required"`
R *hexutil.Big `json:"r" gencodec:"required"` R uint256.Int `json:"r" gencodec:"required"`
S *hexutil.Big `json:"s" gencodec:"required"` S uint256.Int `json:"s" gencodec:"required"`
} }
var enc Authorization var enc Authorization
enc.ChainID = hexutil.Uint64(a.ChainID) enc.ChainID = hexutil.Uint64(a.ChainID)
enc.Address = a.Address enc.Address = a.Address
enc.Nonce = hexutil.Uint64(a.Nonce) enc.Nonce = hexutil.Uint64(a.Nonce)
enc.V = hexutil.Uint64(a.V) enc.V = hexutil.Uint64(a.V)
enc.R = (*hexutil.Big)(a.R) enc.R = a.R
enc.S = (*hexutil.Big)(a.S) enc.S = a.S
return json.Marshal(&enc) return json.Marshal(&enc)
} }
@ -40,8 +40,8 @@ func (a *Authorization) UnmarshalJSON(input []byte) error {
Address *common.Address `json:"address" gencodec:"required"` Address *common.Address `json:"address" gencodec:"required"`
Nonce *hexutil.Uint64 `json:"nonce" gencodec:"required"` Nonce *hexutil.Uint64 `json:"nonce" gencodec:"required"`
V *hexutil.Uint64 `json:"v" gencodec:"required"` V *hexutil.Uint64 `json:"v" gencodec:"required"`
R *hexutil.Big `json:"r" gencodec:"required"` R *uint256.Int `json:"r" gencodec:"required"`
S *hexutil.Big `json:"s" gencodec:"required"` S *uint256.Int `json:"s" gencodec:"required"`
} }
var dec Authorization var dec Authorization
if err := json.Unmarshal(input, &dec); err != nil { if err := json.Unmarshal(input, &dec); err != nil {
@ -66,10 +66,10 @@ func (a *Authorization) UnmarshalJSON(input []byte) error {
if dec.R == nil { if dec.R == nil {
return errors.New("missing required field 'r' for Authorization") return errors.New("missing required field 'r' for Authorization")
} }
a.R = (*big.Int)(dec.R) a.R = *dec.R
if dec.S == nil { if dec.S == nil {
return errors.New("missing required field 's' for Authorization") return errors.New("missing required field 's' for Authorization")
} }
a.S = (*big.Int)(dec.S) a.S = *dec.S
return nil return nil
} }

View file

@ -75,8 +75,8 @@ type Authorization struct {
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
Nonce uint64 `json:"nonce" gencodec:"required"` Nonce uint64 `json:"nonce" gencodec:"required"`
V uint8 `json:"v" gencodec:"required"` V uint8 `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"` R uint256.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"` S uint256.Int `json:"s" gencodec:"required"`
} }
// field type overrides for gencodec // field type overrides for gencodec
@ -84,8 +84,6 @@ type authorizationMarshaling struct {
ChainID hexutil.Uint64 ChainID hexutil.Uint64
Nonce hexutil.Uint64 Nonce hexutil.Uint64
V hexutil.Uint64 V hexutil.Uint64
R *hexutil.Big
S *hexutil.Big
} }
// SignAuth signs the provided authorization. // SignAuth signs the provided authorization.
@ -114,12 +112,12 @@ func (a *Authorization) withSignature(sig []byte) Authorization {
Address: a.Address, Address: a.Address,
Nonce: a.Nonce, Nonce: a.Nonce,
V: sig[64], V: sig[64],
R: r, R: *uint256.MustFromBig(r),
S: s, S: *uint256.MustFromBig(s),
} }
} }
// Authority recovers the authorizing // Authority recovers the the authorizing account of an authorization.
func (a Authorization) Authority() (common.Address, error) { func (a Authorization) Authority() (common.Address, error) {
sighash := prefixedRlpHash( sighash := prefixedRlpHash(
0x05, 0x05,
@ -128,17 +126,16 @@ func (a Authorization) Authority() (common.Address, error) {
a.Address, a.Address,
a.Nonce, a.Nonce,
}) })
if !crypto.ValidateSignatureValues(a.V, a.R, a.S, true) { if !crypto.ValidateSignatureValues(a.V, a.R.ToBig(), a.S.ToBig(), true) {
return common.Address{}, ErrInvalidSig return common.Address{}, ErrInvalidSig
} }
// encode the signature in uncompressed format // encode the signature in uncompressed format
r, s := a.R.Bytes(), a.S.Bytes() var sig [crypto.SignatureLength]byte
sig := make([]byte, crypto.SignatureLength) a.R.SetBytes32(sig[:32])
copy(sig[32-len(r):32], r) a.R.SetBytes32(sig[32:64])
copy(sig[64-len(s):64], s)
sig[64] = a.V sig[64] = a.V
// recover the public key from the signature // recover the public key from the signature
pub, err := crypto.Ecrecover(sighash[:], sig) pub, err := crypto.Ecrecover(sighash[:], sig[:])
if err != nil { if err != nil {
return common.Address{}, err return common.Address{}, err
} }

View file

@ -5,10 +5,10 @@ package tests
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/holiman/uint256"
) )
var _ = (*stAuthorizationMarshaling)(nil) var _ = (*stAuthorizationMarshaling)(nil)
@ -17,19 +17,19 @@ var _ = (*stAuthorizationMarshaling)(nil)
func (s stAuthorization) MarshalJSON() ([]byte, error) { func (s stAuthorization) MarshalJSON() ([]byte, error) {
type stAuthorization struct { type stAuthorization struct {
ChainID math.HexOrDecimal64 ChainID math.HexOrDecimal64
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
Nonce math.HexOrDecimal64 `json:"nonce" gencodec:"required"` Nonce math.HexOrDecimal64 `json:"nonce" gencodec:"required"`
V math.HexOrDecimal64 `json:"v" gencodec:"required"` V math.HexOrDecimal64 `json:"v" gencodec:"required"`
R *math.HexOrDecimal256 `json:"r" gencodec:"required"` R uint256.Int `json:"r" gencodec:"required"`
S *math.HexOrDecimal256 `json:"s" gencodec:"required"` S uint256.Int `json:"s" gencodec:"required"`
} }
var enc stAuthorization var enc stAuthorization
enc.ChainID = math.HexOrDecimal64(s.ChainID) enc.ChainID = math.HexOrDecimal64(s.ChainID)
enc.Address = s.Address enc.Address = s.Address
enc.Nonce = math.HexOrDecimal64(s.Nonce) enc.Nonce = math.HexOrDecimal64(s.Nonce)
enc.V = math.HexOrDecimal64(s.V) enc.V = math.HexOrDecimal64(s.V)
enc.R = (*math.HexOrDecimal256)(s.R) enc.R = s.R
enc.S = (*math.HexOrDecimal256)(s.S) enc.S = s.S
return json.Marshal(&enc) return json.Marshal(&enc)
} }
@ -37,11 +37,11 @@ func (s stAuthorization) MarshalJSON() ([]byte, error) {
func (s *stAuthorization) UnmarshalJSON(input []byte) error { func (s *stAuthorization) UnmarshalJSON(input []byte) error {
type stAuthorization struct { type stAuthorization struct {
ChainID *math.HexOrDecimal64 ChainID *math.HexOrDecimal64
Address *common.Address `json:"address" gencodec:"required"` Address *common.Address `json:"address" gencodec:"required"`
Nonce *math.HexOrDecimal64 `json:"nonce" gencodec:"required"` Nonce *math.HexOrDecimal64 `json:"nonce" gencodec:"required"`
V *math.HexOrDecimal64 `json:"v" gencodec:"required"` V *math.HexOrDecimal64 `json:"v" gencodec:"required"`
R *math.HexOrDecimal256 `json:"r" gencodec:"required"` R *uint256.Int `json:"r" gencodec:"required"`
S *math.HexOrDecimal256 `json:"s" gencodec:"required"` S *uint256.Int `json:"s" gencodec:"required"`
} }
var dec stAuthorization var dec stAuthorization
if err := json.Unmarshal(input, &dec); err != nil { if err := json.Unmarshal(input, &dec); err != nil {
@ -65,10 +65,10 @@ func (s *stAuthorization) UnmarshalJSON(input []byte) error {
if dec.R == nil { if dec.R == nil {
return errors.New("missing required field 'r' for stAuthorization") return errors.New("missing required field 'r' for stAuthorization")
} }
s.R = (*big.Int)(dec.R) s.R = *dec.R
if dec.S == nil { if dec.S == nil {
return errors.New("missing required field 's' for stAuthorization") return errors.New("missing required field 's' for stAuthorization")
} }
s.S = (*big.Int)(dec.S) s.S = *dec.S
return nil return nil
} }

View file

@ -144,8 +144,8 @@ type stAuthorization struct {
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
Nonce uint64 `json:"nonce" gencodec:"required"` Nonce uint64 `json:"nonce" gencodec:"required"`
V uint8 `json:"v" gencodec:"required"` V uint8 `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"` R uint256.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"` S uint256.Int `json:"s" gencodec:"required"`
} }
// field type overrides for gencodec // field type overrides for gencodec
@ -153,8 +153,6 @@ type stAuthorizationMarshaling struct {
ChainID math.HexOrDecimal64 ChainID math.HexOrDecimal64
Nonce math.HexOrDecimal64 Nonce math.HexOrDecimal64
V math.HexOrDecimal64 V math.HexOrDecimal64
R *math.HexOrDecimal256
S *math.HexOrDecimal256
} }
// GetChainConfig takes a fork definition and returns a chain config. // GetChainConfig takes a fork definition and returns a chain config.