mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
core/types: improve authority signature handling
This commit is contained in:
parent
e137c473f0
commit
bb182a6429
1 changed files with 14 additions and 20 deletions
|
|
@ -68,8 +68,7 @@ type SetCodeTx struct {
|
||||||
|
|
||||||
//go:generate go run github.com/fjl/gencodec -type Authorization -field-override authorizationMarshaling -out gen_authorization.go
|
//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
|
// Authorization is an authorization from an account to deploy code at its address.
|
||||||
// address.
|
|
||||||
type Authorization struct {
|
type Authorization struct {
|
||||||
ChainID uint64 `json:"chainId" gencodec:"required"`
|
ChainID uint64 `json:"chainId" gencodec:"required"`
|
||||||
Address common.Address `json:"address" gencodec:"required"`
|
Address common.Address `json:"address" gencodec:"required"`
|
||||||
|
|
@ -88,15 +87,8 @@ type authorizationMarshaling struct {
|
||||||
|
|
||||||
// SignAuth signs the provided authorization.
|
// SignAuth signs the provided authorization.
|
||||||
func SignAuth(auth Authorization, prv *ecdsa.PrivateKey) (Authorization, error) {
|
func SignAuth(auth Authorization, prv *ecdsa.PrivateKey) (Authorization, error) {
|
||||||
h := prefixedRlpHash(
|
sighash := auth.sigHash()
|
||||||
0x05,
|
sig, err := crypto.Sign(sighash[:], prv)
|
||||||
[]interface{}{
|
|
||||||
auth.ChainID,
|
|
||||||
auth.Address,
|
|
||||||
auth.Nonce,
|
|
||||||
})
|
|
||||||
|
|
||||||
sig, err := crypto.Sign(h[:], prv)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Authorization{}, err
|
return Authorization{}, err
|
||||||
}
|
}
|
||||||
|
|
@ -117,22 +109,24 @@ func (a *Authorization) withSignature(sig []byte) Authorization {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Authorization) sigHash() common.Hash {
|
||||||
|
return prefixedRlpHash(0x05, []any{
|
||||||
|
a.ChainID,
|
||||||
|
a.Address,
|
||||||
|
a.Nonce,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Authority recovers the the authorizing account of an authorization.
|
// 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 := a.sigHash()
|
||||||
0x05,
|
|
||||||
[]interface{}{
|
|
||||||
a.ChainID,
|
|
||||||
a.Address,
|
|
||||||
a.Nonce,
|
|
||||||
})
|
|
||||||
if !crypto.ValidateSignatureValues(a.V, a.R.ToBig(), a.S.ToBig(), 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
|
||||||
var sig [crypto.SignatureLength]byte
|
var sig [crypto.SignatureLength]byte
|
||||||
a.R.SetBytes32(sig[:32])
|
a.R.WriteToSlice(sig[:32])
|
||||||
a.R.SetBytes32(sig[32:64])
|
a.S.WriteToSlice(sig[32:64])
|
||||||
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[:])
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue