mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
core/types: add sigHash to txdata
This commit is contained in:
parent
a650a400a0
commit
8b5ebf47c7
8 changed files with 90 additions and 53 deletions
|
|
@ -100,6 +100,9 @@ type TxData interface {
|
|||
|
||||
encode(*bytes.Buffer) error
|
||||
decode([]byte) error
|
||||
|
||||
// sigHash returns the hash of the transaction that is ought to be signed
|
||||
sigHash(*big.Int) common.Hash
|
||||
}
|
||||
|
||||
// EncodeRLP implements rlp.Encoder
|
||||
|
|
|
|||
|
|
@ -301,21 +301,7 @@ func (s cancunSigner) Hash(tx *Transaction) common.Hash {
|
|||
if tx.Type() != BlobTxType {
|
||||
return s.londonSigner.Hash(tx)
|
||||
}
|
||||
return prefixedRlpHash(
|
||||
tx.Type(),
|
||||
[]interface{}{
|
||||
s.chainId,
|
||||
tx.Nonce(),
|
||||
tx.GasTipCap(),
|
||||
tx.GasFeeCap(),
|
||||
tx.Gas(),
|
||||
tx.To(),
|
||||
tx.Value(),
|
||||
tx.Data(),
|
||||
tx.AccessList(),
|
||||
tx.BlobGasFeeCap(),
|
||||
tx.BlobHashes(),
|
||||
})
|
||||
return tx.inner.sigHash(s.chainId)
|
||||
}
|
||||
|
||||
type londonSigner struct{ eip2930Signer }
|
||||
|
|
@ -366,23 +352,10 @@ func (s londonSigner) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
|
|||
// Hash returns the hash to be signed by the sender.
|
||||
// It does not uniquely identify the transaction.
|
||||
func (s londonSigner) Hash(tx *Transaction) common.Hash {
|
||||
inner, ok := tx.inner.(*DynamicFeeTx)
|
||||
if !ok {
|
||||
if tx.Type() != DynamicFeeTxType {
|
||||
return s.eip2930Signer.Hash(tx)
|
||||
}
|
||||
return prefixedRlpHash(
|
||||
inner.txType(),
|
||||
[]interface{}{
|
||||
s.chainId,
|
||||
inner.nonce(),
|
||||
inner.gasTipCap(),
|
||||
inner.gasFeeCap(),
|
||||
inner.gas(),
|
||||
inner.to(),
|
||||
inner.value(),
|
||||
inner.data(),
|
||||
inner.accessList(),
|
||||
})
|
||||
return tx.inner.sigHash(s.chainId)
|
||||
}
|
||||
|
||||
type eip2930Signer struct{ EIP155Signer }
|
||||
|
|
@ -445,18 +418,7 @@ func (s eip2930Signer) Hash(tx *Transaction) common.Hash {
|
|||
case LegacyTxType:
|
||||
return s.EIP155Signer.Hash(tx)
|
||||
case AccessListTxType:
|
||||
return prefixedRlpHash(
|
||||
tx.Type(),
|
||||
[]interface{}{
|
||||
s.chainId,
|
||||
tx.Nonce(),
|
||||
tx.GasPrice(),
|
||||
tx.Gas(),
|
||||
tx.To(),
|
||||
tx.Value(),
|
||||
tx.Data(),
|
||||
tx.AccessList(),
|
||||
})
|
||||
return tx.inner.sigHash(s.chainId)
|
||||
default:
|
||||
// This _should_ not happen, but in case someone sends in a bad
|
||||
// json struct via RPC, it's probably more prudent to return an
|
||||
|
|
@ -526,15 +488,7 @@ func (s EIP155Signer) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
|
|||
// Hash returns the hash to be signed by the sender.
|
||||
// It does not uniquely identify the transaction.
|
||||
func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
|
||||
return rlpHash([]interface{}{
|
||||
tx.Nonce(),
|
||||
tx.GasPrice(),
|
||||
tx.Gas(),
|
||||
tx.To(),
|
||||
tx.Value(),
|
||||
tx.Data(),
|
||||
s.chainId, uint(0), uint(0),
|
||||
})
|
||||
return tx.inner.sigHash(s.chainId)
|
||||
}
|
||||
|
||||
// HomesteadSigner implements Signer interface using the
|
||||
|
|
@ -598,7 +552,7 @@ func (fs FrontierSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v *
|
|||
// Hash returns the hash to be signed by the sender.
|
||||
// It does not uniquely identify the transaction.
|
||||
func (fs FrontierSigner) Hash(tx *Transaction) common.Hash {
|
||||
return rlpHash([]interface{}{
|
||||
return rlpHash([]any{
|
||||
tx.Nonce(),
|
||||
tx.GasPrice(),
|
||||
tx.Gas(),
|
||||
|
|
|
|||
|
|
@ -594,4 +594,5 @@ func BenchmarkHash(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
// BenchmarkHash-8 541969 2320 ns/op 240 B/op 6 allocs/op
|
||||
// BenchmarkHash-8 440082 2639 ns/op 384 B/op 13 allocs/op
|
||||
// BenchmarkHash-8 493566 2033 ns/op 240 B/op 6 allocs/op
|
||||
|
|
|
|||
|
|
@ -127,3 +127,18 @@ func (tx *AccessListTx) encode(b *bytes.Buffer) error {
|
|||
func (tx *AccessListTx) decode(input []byte) error {
|
||||
return rlp.DecodeBytes(input, tx)
|
||||
}
|
||||
|
||||
func (tx *AccessListTx) sigHash(chainID *big.Int) common.Hash {
|
||||
return prefixedRlpHash(
|
||||
AccessListTxType,
|
||||
[]any{
|
||||
chainID,
|
||||
tx.Nonce,
|
||||
tx.GasPrice,
|
||||
tx.Gas,
|
||||
tx.To,
|
||||
tx.Value,
|
||||
tx.Data,
|
||||
tx.AccessList,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -259,3 +259,21 @@ func (tx *BlobTx) decode(input []byte) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tx *BlobTx) sigHash(chainID *big.Int) common.Hash {
|
||||
return prefixedRlpHash(
|
||||
BlobTxType,
|
||||
[]any{
|
||||
chainID,
|
||||
tx.Nonce,
|
||||
tx.GasTipCap,
|
||||
tx.GasFeeCap,
|
||||
tx.Gas,
|
||||
tx.To,
|
||||
tx.Value,
|
||||
tx.Data,
|
||||
tx.AccessList,
|
||||
tx.BlobFeeCap,
|
||||
tx.BlobHashes,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,3 +123,19 @@ func (tx *DynamicFeeTx) encode(b *bytes.Buffer) error {
|
|||
func (tx *DynamicFeeTx) decode(input []byte) error {
|
||||
return rlp.DecodeBytes(input, tx)
|
||||
}
|
||||
|
||||
func (tx *DynamicFeeTx) sigHash(chainID *big.Int) common.Hash {
|
||||
return prefixedRlpHash(
|
||||
DynamicFeeTxType,
|
||||
[]any{
|
||||
chainID,
|
||||
tx.Nonce,
|
||||
tx.GasTipCap,
|
||||
tx.GasFeeCap,
|
||||
tx.Gas,
|
||||
tx.To,
|
||||
tx.Value,
|
||||
tx.Data,
|
||||
tx.AccessList,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,3 +123,16 @@ func (tx *LegacyTx) encode(*bytes.Buffer) error {
|
|||
func (tx *LegacyTx) decode([]byte) error {
|
||||
panic("decode called on LegacyTx)")
|
||||
}
|
||||
|
||||
// OBS: This is the post-frontier hash, the pre-frontier does not contain a chainID
|
||||
func (tx *LegacyTx) sigHash(chainID *big.Int) common.Hash {
|
||||
return rlpHash([]any{
|
||||
tx.Nonce,
|
||||
tx.GasPrice,
|
||||
tx.Gas,
|
||||
tx.To,
|
||||
tx.Value,
|
||||
tx.Data,
|
||||
chainID, uint(0), uint(0),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -223,3 +223,20 @@ func (tx *SetCodeTx) encode(b *bytes.Buffer) error {
|
|||
func (tx *SetCodeTx) decode(input []byte) error {
|
||||
return rlp.DecodeBytes(input, tx)
|
||||
}
|
||||
|
||||
func (tx *SetCodeTx) sigHash(chainID *big.Int) common.Hash {
|
||||
return prefixedRlpHash(
|
||||
SetCodeTxType,
|
||||
[]any{
|
||||
chainID,
|
||||
tx.Nonce,
|
||||
tx.GasTipCap,
|
||||
tx.GasFeeCap,
|
||||
tx.Gas,
|
||||
tx.To,
|
||||
tx.Value,
|
||||
tx.Data,
|
||||
tx.AccessList,
|
||||
tx.AuthList,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue