Skip to content

Commit

Permalink
Remove calcTxHash
Browse files Browse the repository at this point in the history
  • Loading branch information
grujicf committed Feb 22, 2024
1 parent 77afcbe commit 0ad7f94
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 260 deletions.
121 changes: 0 additions & 121 deletions crypto/txsigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
142 changes: 63 additions & 79 deletions crypto/txsigner_eip155.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]))

Expand All @@ -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()
}
Loading

0 comments on commit 0ad7f94

Please sign in to comment.