diff --git a/crypto/txsigner_berlin.go b/crypto/txsigner_berlin.go index 45a0809e29..617ab08a4b 100644 --- a/crypto/txsigner_berlin.go +++ b/crypto/txsigner_berlin.go @@ -101,7 +101,7 @@ func (signer *BerlinSigner) Sender(tx *types.Transaction) (types.Address, error) v, r, s := tx.RawSignatureValues() - // Checking one of the values is enought since they are inseparable + // Checking one of the values is enough since they are inseparable if v == nil { return types.Address{}, errors.New("Sender method: Unknown signature") } diff --git a/crypto/txsigner_eip155.go b/crypto/txsigner_eip155.go index 815cfc2434..3c485416d4 100644 --- a/crypto/txsigner_eip155.go +++ b/crypto/txsigner_eip155.go @@ -97,7 +97,7 @@ func (signer *EIP155Signer) Sender(tx *types.Transaction) (types.Address, error) bigV := big.NewInt(0).SetBytes(v.Bytes()) - if vv := bigV.Uint64(); bits.Len(uint(vv)) <= 8 { + if vv := v.Uint64(); bits.Len(uint(vv)) <= 8 { protected = vv != 27 && vv != 28 } @@ -107,21 +107,11 @@ func (signer *EIP155Signer) Sender(tx *types.Transaction) (types.Address, error) // 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 + mulOperand := big.NewInt(0).Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) + bigV.Sub(bigV, mulOperand) + bigV.Sub(bigV, big35) - a := big.NewInt(0) - b := big.NewInt(0) - parity := big.NewInt(0) - - // a = v - 35 - a.Sub(v, big35) - - // b = CHAIN_ID * 2 - b.Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) - - // parity = a - b - parity.Sub(a, b) - - return recoverAddress(signer.Hash(tx), r, s, parity, true) + return recoverAddress(signer.Hash(tx), r, s, bigV, true) } // SingTx takes the original transaction as input and returns its signed version @@ -139,8 +129,8 @@ func (signer *EIP155Signer) SignTx(tx *types.Transaction, privateKey *ecdsa.Priv return nil, err } - r := new(big.Int).SetBytes(signature[:types.HashLength]) - s := new(big.Int).SetBytes(signature[types.HashLength : 2*types.HashLength]) + 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") @@ -162,7 +152,7 @@ func (signer *EIP155Signer) calculateV(parity byte) []byte { a.Add(a, big35) // b = CHAIN_ID * 2 - b := new(big.Int).Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) + b := big.NewInt(0).Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2)) a.Add(a, b) diff --git a/crypto/txsigner_frontier.go b/crypto/txsigner_frontier.go index 8ad93843a6..ace21941c5 100644 --- a/crypto/txsigner_frontier.go +++ b/crypto/txsigner_frontier.go @@ -66,6 +66,11 @@ func (signer *FrontierSigner) Hash(tx *types.Transaction) types.Hash { // Sender returns the sender of the transaction func (signer *FrontierSigner) Sender(tx *types.Transaction) (types.Address, error) { + return signer.sender(tx, false) +} + +// sender returns the sender of the transaction +func (signer *FrontierSigner) sender(tx *types.Transaction, isHomestead bool) (types.Address, error) { if tx.Type() != types.LegacyTxType && tx.Type() != types.StateTxType { return types.ZeroAddress, types.ErrTxTypeNotSupported } @@ -81,7 +86,7 @@ func (signer *FrontierSigner) Sender(tx *types.Transaction) (types.Address, erro // v = {0, 1} + 27 -> {0, 1} = v - 27 parity := big.NewInt(0).Sub(v, big27) - return recoverAddress(signer.Hash(tx), r, s, parity, false) + return recoverAddress(signer.Hash(tx), r, s, parity, isHomestead) } // SingTx takes the original transaction as input and returns its signed version @@ -99,8 +104,8 @@ func (signer *FrontierSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.Pr return nil, err } - r := new(big.Int).SetBytes(signature[:types.HashLength]) - s := new(big.Int).SetBytes(signature[types.HashLength : 2*types.HashLength]) + r := new(big.Int).SetBytes(signature[:32]) + s := new(big.Int).SetBytes(signature[32:64]) v := new(big.Int).SetBytes(signer.calculateV(signature[64])) tx.SetSignatureValues(v, r, s) diff --git a/crypto/txsigner_homestead.go b/crypto/txsigner_homestead.go index 93edf8c6d7..e4ec1696af 100644 --- a/crypto/txsigner_homestead.go +++ b/crypto/txsigner_homestead.go @@ -37,28 +37,7 @@ func (signer *HomesteadSigner) Hash(tx *types.Transaction) types.Hash { // Sender returns the sender of the transaction func (signer *HomesteadSigner) Sender(tx *types.Transaction) (types.Address, error) { - if tx.Type() != types.LegacyTxType && tx.Type() != types.StateTxType { - return types.ZeroAddress, types.ErrTxTypeNotSupported - } - - if tx.Type() != types.LegacyTxType { - return types.ZeroAddress, types.ErrTxTypeNotSupported - } - - 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) - - // The only difference compared to FrontierSinger.Sender() method is that the `isHomestead` flag is set to true - // `isHomestead` flag denotes that the S value must be inclusively lower than the half of the secp256k1 curve order - return recoverAddress(signer.Hash(tx), r, s, parity, true) + return signer.sender(tx, true) } // SingTx takes the original transaction as input and returns its signed version @@ -76,8 +55,8 @@ func (signer *HomesteadSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.P return nil, err } - r := new(big.Int).SetBytes(signature[:types.HashLength]) - s := new(big.Int).SetBytes(signature[types.HashLength : 2*types.HashLength]) + r := new(big.Int).SetBytes(signature[:32]) + s := new(big.Int).SetBytes(signature[32:64]) // Homestead hard-fork introduced the rule that the S value // must be inclusively lower than the half of the secp256k1 curve order @@ -92,15 +71,3 @@ func (signer *HomesteadSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.P return tx, nil } - -// 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 *HomesteadSigner) calculateV(parity byte) []byte { - result := big.NewInt(0) - - // result = {0, 1} + 27 - result.Add(big.NewInt(int64(parity)), big27) - - return result.Bytes() -} diff --git a/crypto/txsigner_london.go b/crypto/txsigner_london.go index 71ec8ddcfe..b33a489f24 100644 --- a/crypto/txsigner_london.go +++ b/crypto/txsigner_london.go @@ -139,10 +139,3 @@ func (signer *LondonSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.Priv return tx, nil } - -// Private method calculateV returns the V value for the EIP-1559 transactions -// -// V represents the parity of the Y coordinate -func (signer *LondonSigner) calculateV(parity byte) []byte { - return big.NewInt(int64(parity)).Bytes() -}