Skip to content

Commit

Permalink
code reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Feb 23, 2024
1 parent 749dbe7 commit e516072
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 65 deletions.
2 changes: 1 addition & 1 deletion crypto/txsigner_berlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
26 changes: 8 additions & 18 deletions crypto/txsigner_eip155.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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)

Expand Down
11 changes: 8 additions & 3 deletions crypto/txsigner_frontier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
39 changes: 3 additions & 36 deletions crypto/txsigner_homestead.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
}
7 changes: 0 additions & 7 deletions crypto/txsigner_london.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit e516072

Please sign in to comment.