mirror of https://github.com/ethereum/go-ethereum
Co-authored-by: lightclient <lightclient@protonmail.com> Co-authored-by: Mario Vega <marioevz@gmail.com>
parent
e3d61e6db0
commit
731ae82944
@ -0,0 +1,75 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package types |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
var _ = (*authorizationMarshaling)(nil) |
||||
|
||||
// MarshalJSON marshals as JSON.
|
||||
func (a Authorization) MarshalJSON() ([]byte, error) { |
||||
type Authorization struct { |
||||
ChainID hexutil.Uint64 `json:"chainId" gencodec:"required"` |
||||
Address common.Address `json:"address" gencodec:"required"` |
||||
Nonce hexutil.Uint64 `json:"nonce" gencodec:"required"` |
||||
V hexutil.Uint64 `json:"v" gencodec:"required"` |
||||
R *hexutil.Big `json:"r" gencodec:"required"` |
||||
S *hexutil.Big `json:"s" gencodec:"required"` |
||||
} |
||||
var enc Authorization |
||||
enc.ChainID = hexutil.Uint64(a.ChainID) |
||||
enc.Address = a.Address |
||||
enc.Nonce = hexutil.Uint64(a.Nonce) |
||||
enc.V = hexutil.Uint64(a.V) |
||||
enc.R = (*hexutil.Big)(a.R) |
||||
enc.S = (*hexutil.Big)(a.S) |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
// UnmarshalJSON unmarshals from JSON.
|
||||
func (a *Authorization) UnmarshalJSON(input []byte) error { |
||||
type Authorization struct { |
||||
ChainID *hexutil.Uint64 `json:"chainId" gencodec:"required"` |
||||
Address *common.Address `json:"address" gencodec:"required"` |
||||
Nonce *hexutil.Uint64 `json:"nonce" gencodec:"required"` |
||||
V *hexutil.Uint64 `json:"v" gencodec:"required"` |
||||
R *hexutil.Big `json:"r" gencodec:"required"` |
||||
S *hexutil.Big `json:"s" gencodec:"required"` |
||||
} |
||||
var dec Authorization |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
if dec.ChainID == nil { |
||||
return errors.New("missing required field 'chainId' for Authorization") |
||||
} |
||||
a.ChainID = uint64(*dec.ChainID) |
||||
if dec.Address == nil { |
||||
return errors.New("missing required field 'address' for Authorization") |
||||
} |
||||
a.Address = *dec.Address |
||||
if dec.Nonce == nil { |
||||
return errors.New("missing required field 'nonce' for Authorization") |
||||
} |
||||
a.Nonce = uint64(*dec.Nonce) |
||||
if dec.V == nil { |
||||
return errors.New("missing required field 'v' for Authorization") |
||||
} |
||||
a.V = uint8(*dec.V) |
||||
if dec.R == nil { |
||||
return errors.New("missing required field 'r' for Authorization") |
||||
} |
||||
a.R = (*big.Int)(dec.R) |
||||
if dec.S == nil { |
||||
return errors.New("missing required field 's' for Authorization") |
||||
} |
||||
a.S = (*big.Int)(dec.S) |
||||
return nil |
||||
} |
@ -0,0 +1,238 @@ |
||||
// Copyright 2024 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package types |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/ecdsa" |
||||
"errors" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
"github.com/holiman/uint256" |
||||
) |
||||
|
||||
// DelegationPrefix is used by code to denote the account is delegating to
|
||||
// another account.
|
||||
var DelegationPrefix = []byte{0xef, 0x01, 0x00} |
||||
|
||||
// ParseDelegation tries to parse the address from a delegation slice.
|
||||
func ParseDelegation(b []byte) (common.Address, bool) { |
||||
if len(b) != 23 || !bytes.HasPrefix(b, DelegationPrefix) { |
||||
return common.Address{}, false |
||||
} |
||||
return common.BytesToAddress(b[len(DelegationPrefix):]), true |
||||
} |
||||
|
||||
// AddressToDelegation adds the delegation prefix to the specified address.
|
||||
func AddressToDelegation(addr common.Address) []byte { |
||||
return append(DelegationPrefix, addr.Bytes()...) |
||||
} |
||||
|
||||
// SetCodeTx implements the EIP-7702 transaction type which temporarily installs
|
||||
// the code at the signer's address.
|
||||
type SetCodeTx struct { |
||||
ChainID uint64 |
||||
Nonce uint64 |
||||
GasTipCap *uint256.Int // a.k.a. maxPriorityFeePerGas
|
||||
GasFeeCap *uint256.Int // a.k.a. maxFeePerGas
|
||||
Gas uint64 |
||||
To common.Address |
||||
Value *uint256.Int |
||||
Data []byte |
||||
AccessList AccessList |
||||
AuthList AuthorizationList |
||||
|
||||
// Signature values
|
||||
V *uint256.Int `json:"v" gencodec:"required"` |
||||
R *uint256.Int `json:"r" gencodec:"required"` |
||||
S *uint256.Int `json:"s" gencodec:"required"` |
||||
} |
||||
|
||||
//go:generate go run github.com/fjl/gencodec -type Authorization -field-override authorizationMarshaling -out gen_authorization.go
|
||||
|
||||
// Authorization is an authorization from an account to deploy code at it's
|
||||
// address.
|
||||
type Authorization struct { |
||||
ChainID uint64 `json:"chainId" gencodec:"required"` |
||||
Address common.Address `json:"address" gencodec:"required"` |
||||
Nonce uint64 `json:"nonce" gencodec:"required"` |
||||
V uint8 `json:"v" gencodec:"required"` |
||||
R *big.Int `json:"r" gencodec:"required"` |
||||
S *big.Int `json:"s" gencodec:"required"` |
||||
} |
||||
|
||||
// field type overrides for gencodec
|
||||
type authorizationMarshaling struct { |
||||
ChainID hexutil.Uint64 |
||||
Nonce hexutil.Uint64 |
||||
V hexutil.Uint64 |
||||
R *hexutil.Big |
||||
S *hexutil.Big |
||||
} |
||||
|
||||
// SignAuth signs the provided authorization.
|
||||
func SignAuth(auth *Authorization, prv *ecdsa.PrivateKey) (*Authorization, error) { |
||||
h := prefixedRlpHash( |
||||
0x05, |
||||
[]interface{}{ |
||||
auth.ChainID, |
||||
auth.Address, |
||||
auth.Nonce, |
||||
}) |
||||
|
||||
sig, err := crypto.Sign(h[:], prv) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return auth.withSignature(sig), nil |
||||
} |
||||
|
||||
// withSignature updates the signature of an Authorization to be equal the
|
||||
// decoded signature provided in sig.
|
||||
func (a *Authorization) withSignature(sig []byte) *Authorization { |
||||
r, s, _ := decodeSignature(sig) |
||||
cpy := Authorization{ |
||||
ChainID: a.ChainID, |
||||
Address: a.Address, |
||||
Nonce: a.Nonce, |
||||
V: sig[64], |
||||
R: r, |
||||
S: s, |
||||
} |
||||
return &cpy |
||||
} |
||||
|
||||
type AuthorizationList []*Authorization |
||||
|
||||
// Authority recovers the authorizing
|
||||
func (a Authorization) Authority() (common.Address, error) { |
||||
sighash := prefixedRlpHash( |
||||
0x05, |
||||
[]interface{}{ |
||||
a.ChainID, |
||||
a.Address, |
||||
a.Nonce, |
||||
}) |
||||
if !crypto.ValidateSignatureValues(a.V, a.R, a.S, true) { |
||||
return common.Address{}, ErrInvalidSig |
||||
} |
||||
// encode the signature in uncompressed format
|
||||
r, s := a.R.Bytes(), a.S.Bytes() |
||||
sig := make([]byte, crypto.SignatureLength) |
||||
copy(sig[32-len(r):32], r) |
||||
copy(sig[64-len(s):64], s) |
||||
sig[64] = a.V |
||||
// recover the public key from the signature
|
||||
pub, err := crypto.Ecrecover(sighash[:], sig) |
||||
if err != nil { |
||||
return common.Address{}, err |
||||
} |
||||
if len(pub) == 0 || pub[0] != 4 { |
||||
return common.Address{}, errors.New("invalid public key") |
||||
} |
||||
var addr common.Address |
||||
copy(addr[:], crypto.Keccak256(pub[1:])[12:]) |
||||
return addr, nil |
||||
} |
||||
|
||||
// copy creates a deep copy of the transaction data and initializes all fields.
|
||||
func (tx *SetCodeTx) copy() TxData { |
||||
cpy := &SetCodeTx{ |
||||
Nonce: tx.Nonce, |
||||
To: tx.To, |
||||
Data: common.CopyBytes(tx.Data), |
||||
Gas: tx.Gas, |
||||
// These are copied below.
|
||||
AccessList: make(AccessList, len(tx.AccessList)), |
||||
AuthList: make(AuthorizationList, len(tx.AuthList)), |
||||
Value: new(uint256.Int), |
||||
ChainID: tx.ChainID, |
||||
GasTipCap: new(uint256.Int), |
||||
GasFeeCap: new(uint256.Int), |
||||
V: new(uint256.Int), |
||||
R: new(uint256.Int), |
||||
S: new(uint256.Int), |
||||
} |
||||
copy(cpy.AccessList, tx.AccessList) |
||||
copy(cpy.AuthList, tx.AuthList) |
||||
if tx.Value != nil { |
||||
cpy.Value.Set(tx.Value) |
||||
} |
||||
if tx.GasTipCap != nil { |
||||
cpy.GasTipCap.Set(tx.GasTipCap) |
||||
} |
||||
if tx.GasFeeCap != nil { |
||||
cpy.GasFeeCap.Set(tx.GasFeeCap) |
||||
} |
||||
if tx.V != nil { |
||||
cpy.V.Set(tx.V) |
||||
} |
||||
if tx.R != nil { |
||||
cpy.R.Set(tx.R) |
||||
} |
||||
if tx.S != nil { |
||||
cpy.S.Set(tx.S) |
||||
} |
||||
return cpy |
||||
} |
||||
|
||||
// accessors for innerTx.
|
||||
func (tx *SetCodeTx) txType() byte { return SetCodeTxType } |
||||
func (tx *SetCodeTx) chainID() *big.Int { return big.NewInt(int64(tx.ChainID)) } |
||||
func (tx *SetCodeTx) accessList() AccessList { return tx.AccessList } |
||||
func (tx *SetCodeTx) data() []byte { return tx.Data } |
||||
func (tx *SetCodeTx) gas() uint64 { return tx.Gas } |
||||
func (tx *SetCodeTx) gasFeeCap() *big.Int { return tx.GasFeeCap.ToBig() } |
||||
func (tx *SetCodeTx) gasTipCap() *big.Int { return tx.GasTipCap.ToBig() } |
||||
func (tx *SetCodeTx) gasPrice() *big.Int { return tx.GasFeeCap.ToBig() } |
||||
func (tx *SetCodeTx) value() *big.Int { return tx.Value.ToBig() } |
||||
func (tx *SetCodeTx) nonce() uint64 { return tx.Nonce } |
||||
func (tx *SetCodeTx) to() *common.Address { tmp := tx.To; return &tmp } |
||||
|
||||
func (tx *SetCodeTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int { |
||||
if baseFee == nil { |
||||
return dst.Set(tx.GasFeeCap.ToBig()) |
||||
} |
||||
tip := dst.Sub(tx.GasFeeCap.ToBig(), baseFee) |
||||
if tip.Cmp(tx.GasTipCap.ToBig()) > 0 { |
||||
tip.Set(tx.GasTipCap.ToBig()) |
||||
} |
||||
return tip.Add(tip, baseFee) |
||||
} |
||||
|
||||
func (tx *SetCodeTx) rawSignatureValues() (v, r, s *big.Int) { |
||||
return tx.V.ToBig(), tx.R.ToBig(), tx.S.ToBig() |
||||
} |
||||
|
||||
func (tx *SetCodeTx) setSignatureValues(chainID, v, r, s *big.Int) { |
||||
tx.ChainID = chainID.Uint64() |
||||
tx.V.SetFromBig(v) |
||||
tx.R.SetFromBig(r) |
||||
tx.S.SetFromBig(s) |
||||
} |
||||
|
||||
func (tx *SetCodeTx) encode(b *bytes.Buffer) error { |
||||
return rlp.Encode(b, tx) |
||||
} |
||||
|
||||
func (tx *SetCodeTx) decode(input []byte) error { |
||||
return rlp.DecodeBytes(input, tx) |
||||
} |
@ -0,0 +1,70 @@ |
||||
// Copyright 2024 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package types |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
// TestParseDelegation tests a few possible delegation designator values and
|
||||
// ensures they are parsed correctly.
|
||||
func TestParseDelegation(t *testing.T) { |
||||
addr := common.Address{0x42} |
||||
for _, tt := range []struct { |
||||
val []byte |
||||
want *common.Address |
||||
}{ |
||||
{ // simple correct delegation
|
||||
val: append(DelegationPrefix, addr.Bytes()...), |
||||
want: &addr, |
||||
}, |
||||
{ // wrong address size
|
||||
val: append(DelegationPrefix, addr.Bytes()[0:19]...), |
||||
}, |
||||
{ // short address
|
||||
val: append(DelegationPrefix, 0x42), |
||||
}, |
||||
{ // long address
|
||||
val: append(append(DelegationPrefix, addr.Bytes()...), 0x42), |
||||
}, |
||||
{ // wrong prefix size
|
||||
val: append(DelegationPrefix[:2], addr.Bytes()...), |
||||
}, |
||||
{ // wrong prefix
|
||||
val: append([]byte{0xef, 0x01, 0x01}, addr.Bytes()...), |
||||
}, |
||||
{ // wrong prefix
|
||||
val: append([]byte{0xef, 0x00, 0x00}, addr.Bytes()...), |
||||
}, |
||||
{ // no prefix
|
||||
val: addr.Bytes(), |
||||
}, |
||||
{ // no address
|
||||
val: DelegationPrefix, |
||||
}, |
||||
} { |
||||
got, ok := ParseDelegation(tt.val) |
||||
if ok && tt.want == nil { |
||||
t.Fatalf("expected fail, got %s", got.Hex()) |
||||
} |
||||
if !ok && tt.want != nil { |
||||
t.Fatalf("failed to parse, want %s", tt.want.Hex()) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,74 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package tests |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/math" |
||||
) |
||||
|
||||
var _ = (*stAuthorizationMarshaling)(nil) |
||||
|
||||
// MarshalJSON marshals as JSON.
|
||||
func (s stAuthorization) MarshalJSON() ([]byte, error) { |
||||
type stAuthorization struct { |
||||
ChainID math.HexOrDecimal64 |
||||
Address common.Address `json:"address" gencodec:"required"` |
||||
Nonce math.HexOrDecimal64 `json:"nonce" gencodec:"required"` |
||||
V math.HexOrDecimal64 `json:"v" gencodec:"required"` |
||||
R *math.HexOrDecimal256 `json:"r" gencodec:"required"` |
||||
S *math.HexOrDecimal256 `json:"s" gencodec:"required"` |
||||
} |
||||
var enc stAuthorization |
||||
enc.ChainID = math.HexOrDecimal64(s.ChainID) |
||||
enc.Address = s.Address |
||||
enc.Nonce = math.HexOrDecimal64(s.Nonce) |
||||
enc.V = math.HexOrDecimal64(s.V) |
||||
enc.R = (*math.HexOrDecimal256)(s.R) |
||||
enc.S = (*math.HexOrDecimal256)(s.S) |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
// UnmarshalJSON unmarshals from JSON.
|
||||
func (s *stAuthorization) UnmarshalJSON(input []byte) error { |
||||
type stAuthorization struct { |
||||
ChainID *math.HexOrDecimal64 |
||||
Address *common.Address `json:"address" gencodec:"required"` |
||||
Nonce *math.HexOrDecimal64 `json:"nonce" gencodec:"required"` |
||||
V *math.HexOrDecimal64 `json:"v" gencodec:"required"` |
||||
R *math.HexOrDecimal256 `json:"r" gencodec:"required"` |
||||
S *math.HexOrDecimal256 `json:"s" gencodec:"required"` |
||||
} |
||||
var dec stAuthorization |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
if dec.ChainID != nil { |
||||
s.ChainID = uint64(*dec.ChainID) |
||||
} |
||||
if dec.Address == nil { |
||||
return errors.New("missing required field 'address' for stAuthorization") |
||||
} |
||||
s.Address = *dec.Address |
||||
if dec.Nonce == nil { |
||||
return errors.New("missing required field 'nonce' for stAuthorization") |
||||
} |
||||
s.Nonce = uint64(*dec.Nonce) |
||||
if dec.V == nil { |
||||
return errors.New("missing required field 'v' for stAuthorization") |
||||
} |
||||
s.V = uint8(*dec.V) |
||||
if dec.R == nil { |
||||
return errors.New("missing required field 'r' for stAuthorization") |
||||
} |
||||
s.R = (*big.Int)(dec.R) |
||||
if dec.S == nil { |
||||
return errors.New("missing required field 's' for stAuthorization") |
||||
} |
||||
s.S = (*big.Int)(dec.S) |
||||
return nil |
||||
} |
Loading…
Reference in new issue