From 0ad7f948910ebbb5175f94ea1790d177c6550cce Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 22 Feb 2024 21:18:15 +0100 Subject: [PATCH] Remove calcTxHash --- crypto/txsigner.go | 121 ------------------------------ crypto/txsigner_eip155.go | 142 ++++++++++++++++-------------------- crypto/txsigner_frontier.go | 101 +++++++++++-------------- 3 files changed, 104 insertions(+), 260 deletions(-) diff --git a/crypto/txsigner.go b/crypto/txsigner.go index aa4ab9c101..7f8c3a82c1 100644 --- a/crypto/txsigner.go +++ b/crypto/txsigner.go @@ -6,7 +6,6 @@ import ( "math/big" "github.com/0xPolygon/polygon-edge/chain" - "github.com/0xPolygon/polygon-edge/helper/keccak" "github.com/0xPolygon/polygon-edge/types" "github.com/umbracle/fastrlp" ) @@ -97,123 +96,3 @@ func recoverAddress(txHash types.Hash, r, s, parity *big.Int, isHomestead bool) return types.BytesToAddress(address), nil } - -func calcTxHash(tx *types.Transaction, chainID uint64) types.Hash { - var hash []byte - - switch tx.Type() { - case types.AccessListTx: - a := arenaPool.Get() - v := a.NewArray() - - v.Set(a.NewUint(chainID)) - v.Set(a.NewUint(tx.Nonce())) - v.Set(a.NewBigInt(tx.GasPrice())) - v.Set(a.NewUint(tx.Gas())) - - if tx.To() == nil { - v.Set(a.NewNull()) - } else { - v.Set(a.NewCopyBytes((*(tx.To())).Bytes())) - } - - v.Set(a.NewBigInt(tx.Value())) - v.Set(a.NewCopyBytes(tx.Input())) - - // add accessList - accessListVV := a.NewArray() - - if tx.AccessList() != nil { - for _, accessTuple := range tx.AccessList() { - accessTupleVV := a.NewArray() - accessTupleVV.Set(a.NewCopyBytes(accessTuple.Address.Bytes())) - - storageKeysVV := a.NewArray() - for _, storageKey := range accessTuple.StorageKeys { - storageKeysVV.Set(a.NewCopyBytes(storageKey.Bytes())) - } - - accessTupleVV.Set(storageKeysVV) - accessListVV.Set(accessTupleVV) - } - } - - v.Set(accessListVV) - - hash = keccak.PrefixedKeccak256Rlp([]byte{byte(tx.Type())}, nil, v) - - arenaPool.Put(a) - - return types.BytesToHash(hash) - - case types.DynamicFeeTx, types.LegacyTx, types.StateTx: - a := arenaPool.Get() - isDynamicFeeTx := tx.Type() == types.DynamicFeeTx - - v := a.NewArray() - - if isDynamicFeeTx { - v.Set(a.NewUint(chainID)) - } - - v.Set(a.NewUint(tx.Nonce())) - - if isDynamicFeeTx { - v.Set(a.NewBigInt(tx.GasTipCap())) - v.Set(a.NewBigInt(tx.GasFeeCap())) - } else { - v.Set(a.NewBigInt(tx.GasPrice())) - } - - v.Set(a.NewUint(tx.Gas())) - - if tx.To() == nil { - v.Set(a.NewNull()) - } else { - v.Set(a.NewCopyBytes((*(tx.To())).Bytes())) - } - - v.Set(a.NewBigInt(tx.Value())) - - v.Set(a.NewCopyBytes(tx.Input())) - - if isDynamicFeeTx { - // Convert TxAccessList to RLP format and add it to the vv array. - accessListVV := a.NewArray() - - if tx.AccessList() != nil { - for _, accessTuple := range tx.AccessList() { - accessTupleVV := a.NewArray() - accessTupleVV.Set(a.NewCopyBytes(accessTuple.Address.Bytes())) - - storageKeysVV := a.NewArray() - for _, storageKey := range accessTuple.StorageKeys { - storageKeysVV.Set(a.NewCopyBytes(storageKey.Bytes())) - } - - accessTupleVV.Set(storageKeysVV) - accessListVV.Set(accessTupleVV) - } - } - - v.Set(accessListVV) - } else { - // EIP155 - if chainID != 0 { - v.Set(a.NewUint(chainID)) - v.Set(a.NewUint(0)) - v.Set(a.NewUint(0)) - } - } - - if isDynamicFeeTx { - hash = keccak.PrefixedKeccak256Rlp([]byte{byte(tx.Type())}, nil, v) - } else { - hash = keccak.Keccak256Rlp(nil, v) - } - - arenaPool.Put(a) - } - - return types.BytesToHash(hash) -} diff --git a/crypto/txsigner_eip155.go b/crypto/txsigner_eip155.go index d9d7a1aab3..b6333795d0 100644 --- a/crypto/txsigner_eip155.go +++ b/crypto/txsigner_eip155.go @@ -2,9 +2,11 @@ package crypto import ( "crypto/ecdsa" + "errors" "math/big" "math/bits" + "github.com/0xPolygon/polygon-edge/helper/keccak" "github.com/0xPolygon/polygon-edge/types" ) @@ -39,54 +41,52 @@ func (signer *EIP155Signer) Hash(tx *types.Transaction) types.Hash { return signer.FrontierSigner.Hash(tx) } - // var hash []byte + var hash []byte - // RLP := arenaPool.Get() + RLP := arenaPool.Get() - // // RLP(-, -, -, -, -, -, -, -, -) - // hashPreimage := RLP.NewArray() + // RLP(-, -, -, -, -, -, -, -, -) + hashPreimage := RLP.NewArray() - // // RLP(nonce, -, -, -, -, -, -, -, -) - // hashPreimage.Set(RLP.NewUint(tx.Nonce())) + // RLP(nonce, -, -, -, -, -, -, -, -) + hashPreimage.Set(RLP.NewUint(tx.Nonce())) - // // RLP(nonce, gasPrice, -, -, -, -, -, -, -) - // hashPreimage.Set(RLP.NewBigInt(tx.GasPrice())) + // RLP(nonce, gasPrice, -, -, -, -, -, -, -) + hashPreimage.Set(RLP.NewBigInt(tx.GasPrice())) - // // RLP(nonce, gasPrice, gas, -, -, -, -, -, -) - // hashPreimage.Set(RLP.NewUint(tx.Gas())) + // RLP(nonce, gasPrice, gas, -, -, -, -, -, -) + hashPreimage.Set(RLP.NewUint(tx.Gas())) - // // Checking whether the transaction is a smart contract deployment - // if tx.To() == nil { - // // RLP(nonce, gasPrice, gas, to, -, -, -, -, -) - // hashPreimage.Set(RLP.NewNull()) - // } else { - // // RLP(nonce, gasPrice, gas, to, -, -, -, -, -) - // hashPreimage.Set(RLP.NewCopyBytes((*(tx.To())).Bytes())) - // } - - // // RLP(nonce, gasPrice, gas, to, value, -, -, -, -) - // hashPreimage.Set(RLP.NewBigInt(tx.Value())) + // Checking whether the transaction is a smart contract deployment + if tx.To() == nil { + // RLP(nonce, gasPrice, gas, to, -, -, -, -, -) + hashPreimage.Set(RLP.NewNull()) + } else { + // RLP(nonce, gasPrice, gas, to, -, -, -, -, -) + hashPreimage.Set(RLP.NewCopyBytes((*(tx.To())).Bytes())) + } - // // RLP(nonce, gasPrice, gas, to, value, input, -, -, -) - // hashPreimage.Set(RLP.NewCopyBytes(tx.Input())) + // RLP(nonce, gasPrice, gas, to, value, -, -, -, -) + hashPreimage.Set(RLP.NewBigInt(tx.Value())) - // // RLP(nonce, gasPrice, gas, to, value, input, chainId, -, -) - // hashPreimage.Set(RLP.NewUint(signer.chainID)) + // RLP(nonce, gasPrice, gas, to, value, input, -, -, -) + hashPreimage.Set(RLP.NewCopyBytes(tx.Input())) - // // RLP(nonce, gasPrice, gas, to, value, input, chainId, 0, -) - // hashPreimage.Set(RLP.NewUint(0)) + // RLP(nonce, gasPrice, gas, to, value, input, chainId, -, -) + hashPreimage.Set(RLP.NewUint(signer.chainID)) - // // RLP(nonce, gasPrice, gas, to, value, input, chainId, 0, 0) - // hashPreimage.Set(RLP.NewUint(0)) + // RLP(nonce, gasPrice, gas, to, value, input, chainId, 0, -) + hashPreimage.Set(RLP.NewUint(0)) - // // keccak256(RLP(nonce, gasPrice, gas, to, value, input)) - // hash = keccak.Keccak256Rlp(nil, hashPreimage) + // RLP(nonce, gasPrice, gas, to, value, input, chainId, 0, 0) + hashPreimage.Set(RLP.NewUint(0)) - // arenaPool.Put(RLP) + // keccak256(RLP(nonce, gasPrice, gas, to, value, input)) + hash = keccak.Keccak256Rlp(nil, hashPreimage) - // return types.BytesToHash(hash) + arenaPool.Put(RLP) - return calcTxHash(tx, signer.chainID) + return types.BytesToHash(hash) } // Sender returns the sender of the transaction @@ -97,48 +97,40 @@ func (signer *EIP155Signer) Sender(tx *types.Transaction) (types.Address, error) protected := true - bigV := big.NewInt(0) - v, r, s := tx.RawSignatureValues() // Checking one of the values is enought since they are inseparable - // if v == nil { - // return types.Address{}, errors.New("Sender method: Unknown signature") - // } - - if v != nil { - bigV.SetBytes(v.Bytes()) + if v == nil { + return types.Address{}, errors.New("Sender method: Unknown signature") } + bigV := big.NewInt(0).SetBytes(v.Bytes()) + if vv := bigV.Uint64(); bits.Len(uint(vv)) <= 8 { protected = vv != 27 && vv != 28 } if !protected { - return signer.FrontierSigner.Sender(tx) + return signer.HomesteadSigner.Sender(tx) } // Reverse the V calculation to find the parity of the Y coordinate // v = CHAIN_ID * 2 + 35 + {0, 1} -> {0, 1} = v - 35 - CHAIN_ID * 2 - // a := big.NewInt(0) - // b := big.NewInt(0) - // parity := big.NewInt(0) + a := big.NewInt(0) + b := big.NewInt(0) + parity := big.NewInt(0) - // // a = v - 35 - // a.Sub(v, big35) + // a = v - 35 + a.Sub(v, big35) - // // b = CHAIN_ID * 2 - // b.Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) + // b = CHAIN_ID * 2 + b.Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) - // // parity = a - b - // parity.Sub(a, b) + // parity = a - b + parity.Sub(a, b) - mulOperand := big.NewInt(0).Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) - bigV.Sub(bigV, mulOperand) - bigV.Sub(bigV, big35) - - return recoverAddress(signer.Hash(tx), r, s, bigV, true) + return recoverAddress(signer.Hash(tx), r, s, parity, true) } // SingTx takes the original transaction as input and returns its signed version @@ -159,9 +151,9 @@ func (signer *EIP155Signer) SignTx(tx *types.Transaction, privateKey *ecdsa.Priv r := new(big.Int).SetBytes(signature[:32]) s := new(big.Int).SetBytes(signature[32:64]) - // if s.Cmp(secp256k1NHalf) > 0 { - // return nil, errors.New("SignTx method: S must be inclusively lower than secp256k1n/2") - // } + if s.Cmp(secp256k1NHalf) > 0 { + return nil, errors.New("SignTx method: S must be inclusively lower than secp256k1n/2") + } v := new(big.Int).SetBytes(signer.calculateV(signature[64])) @@ -173,27 +165,19 @@ func (signer *EIP155Signer) SignTx(tx *types.Transaction, privateKey *ecdsa.Priv // Private method calculateV returns the V value for the EIP-155 transactions // // V is calculated by the formula: {0, 1} + CHAIN_ID * 2 + 35 where {0, 1} denotes the parity of the Y coordinate -// func (signer *EIP155Signer) calculateV(parity byte) []byte { -// a := big.NewInt(0) -// b := big.NewInt(0) -// result := big.NewInt(0) - -// // a = {0, 1} + 35 -// a.Add(big.NewInt(int64(parity)), big35) +func (signer *EIP155Signer) calculateV(parity byte) []byte { + a := big.NewInt(0) + b := big.NewInt(0) + result := big.NewInt(0) -// // b = CHAIN_ID * 2 -// b.Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) + // a = {0, 1} + 35 + a.Add(big.NewInt(int64(parity)), big35) -// // result = a + b -// result.Add(a, b) + // b = CHAIN_ID * 2 + b.Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) -// return result.Bytes() -// } + // result = a + b + result.Add(a, b) -func (signer *EIP155Signer) calculateV(parity byte) []byte { - reference := big.NewInt(int64(parity)) - reference.Add(reference, big35) - mulOperand := big.NewInt(0).Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) - reference.Add(reference, mulOperand) - return reference.Bytes() + return result.Bytes() } diff --git a/crypto/txsigner_frontier.go b/crypto/txsigner_frontier.go index 7298836141..b42280984f 100644 --- a/crypto/txsigner_frontier.go +++ b/crypto/txsigner_frontier.go @@ -5,6 +5,7 @@ import ( "errors" "math/big" + "github.com/0xPolygon/polygon-edge/helper/keccak" "github.com/0xPolygon/polygon-edge/types" ) @@ -31,45 +32,43 @@ func (signer *FrontierSigner) Hash(tx *types.Transaction) types.Hash { return types.ZeroHash } - // var hash []byte + var hash []byte - // RLP := arenaPool.Get() + RLP := arenaPool.Get() - // // RLP(-, -, -, -, -, -) - // hashPreimage := RLP.NewArray() + // RLP(-, -, -, -, -, -) + hashPreimage := RLP.NewArray() - // // RLP(nonce, -, -, -, -, -) - // hashPreimage.Set(RLP.NewUint(tx.Nonce())) + // RLP(nonce, -, -, -, -, -) + hashPreimage.Set(RLP.NewUint(tx.Nonce())) - // // RLP(nonce, gasPrice, -, -, -, -) - // hashPreimage.Set(RLP.NewBigInt(tx.GasPrice())) + // RLP(nonce, gasPrice, -, -, -, -) + hashPreimage.Set(RLP.NewBigInt(tx.GasPrice())) - // // RLP(nonce, gasPrice, gas, -, -, -) - // hashPreimage.Set(RLP.NewUint(tx.Gas())) + // RLP(nonce, gasPrice, gas, -, -, -) + hashPreimage.Set(RLP.NewUint(tx.Gas())) - // // Checking whether the transaction is a smart contract deployment - // if tx.To() == nil { - // // RLP(nonce, gasPrice, gas, to, -, -) - // hashPreimage.Set(RLP.NewNull()) - // } else { - // // RLP(nonce, gasPrice, gas, to, -, -) - // hashPreimage.Set(RLP.NewCopyBytes((*(tx.To())).Bytes())) - // } - - // // RLP(nonce, gasPrice, gas, to, value, -) - // hashPreimage.Set(RLP.NewBigInt(tx.Value())) + // Checking whether the transaction is a smart contract deployment + if tx.To() == nil { + // RLP(nonce, gasPrice, gas, to, -, -) + hashPreimage.Set(RLP.NewNull()) + } else { + // RLP(nonce, gasPrice, gas, to, -, -) + hashPreimage.Set(RLP.NewCopyBytes((*(tx.To())).Bytes())) + } - // // RLP(nonce, gasPrice, gas, to, value, input) - // hashPreimage.Set(RLP.NewCopyBytes(tx.Input())) + // RLP(nonce, gasPrice, gas, to, value, -) + hashPreimage.Set(RLP.NewBigInt(tx.Value())) - // // keccak256(RLP(nonce, gasPrice, gas, to, value, input)) - // hash = keccak.Keccak256Rlp(nil, hashPreimage) + // RLP(nonce, gasPrice, gas, to, value, input) + hashPreimage.Set(RLP.NewCopyBytes(tx.Input())) - // arenaPool.Put(RLP) + // keccak256(RLP(nonce, gasPrice, gas, to, value, input)) + hash = keccak.Keccak256Rlp(nil, hashPreimage) - // return types.BytesToHash(hash) + arenaPool.Put(RLP) - return calcTxHash(tx, 0) + return types.BytesToHash(hash) } // Sender returns the sender of the transaction @@ -78,30 +77,19 @@ func (signer *FrontierSigner) Sender(tx *types.Transaction) (types.Address, erro return types.Address{}, errors.New("Sender method: Unknown transaction type") } - // v, r, s := tx.RawSignatureValues() - - // // Checking one of the values is enought since they are inseparable - // // if v == nil { - // // return types.Address{}, errors.New("Sender method: Unknown signature") - // // } - - // // Reverse the V calculation to find the parity of the Y coordinate - // // v = {0, 1} + 27 -> {0, 1} = v - 27 - - // parity := big.NewInt(0).Sub(v, big27) - - // return recoverAddress(signer.Hash(tx), r, s, parity, false) - - refV := big.NewInt(0) - v, r, s := tx.RawSignatureValues() - if v != nil { - refV.SetBytes(v.Bytes()) + + // Checking one of the values is enought since they are inseparable + if v == nil { + return types.Address{}, errors.New("Sender method: Unknown signature") } - refV.Sub(refV, big27) + // Reverse the V calculation to find the parity of the Y coordinate + // v = {0, 1} + 27 -> {0, 1} = v - 27 - return recoverAddress(signer.Hash(tx), r, s, refV, false) + parity := big.NewInt(0).Sub(v, big27) + + return recoverAddress(signer.Hash(tx), r, s, parity, false) } // SingTx takes the original transaction as input and returns its signed version @@ -131,18 +119,11 @@ func (signer *FrontierSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.Pr // Private method calculateV returns the V value for the pre-EIP-155 transactions // // V is calculated by the formula: {0, 1} + 27 where {0, 1} denotes the parity of the Y coordinate -// func (signer *FrontierSigner) calculateV(parity byte) []byte { -// result := big.NewInt(0) - -// // result = {0, 1} + 27 -// result.Add(big.NewInt(int64(parity)), big27) - -// return result.Bytes() -// } - func (signer *FrontierSigner) calculateV(parity byte) []byte { - reference := big.NewInt(int64(parity)) - reference.Add(reference, big27) + result := big.NewInt(0) + + // result = {0, 1} + 27 + result.Add(big.NewInt(int64(parity)), big27) - return reference.Bytes() + return result.Bytes() }