Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement analyze methods send transaction accounts sign transaction send private transaction #166

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
fb817db
eth_accounts method and the corresponding UT
novosandara Mar 26, 2024
0aa156e
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Mar 26, 2024
dc6c98e
UT fix
novosandara Mar 27, 2024
75ececc
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
Stefan-Ethernal Mar 27, 2024
9d88475
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Mar 27, 2024
c7d258b
eth_sendTransaction method and the corresponding tests (UT and e2e)
novosandara Mar 27, 2024
b94d134
eth_sendTransaction e2e fix
novosandara Mar 28, 2024
56aab5d
lint fix
novosandara Mar 28, 2024
99c1586
e2e fix
novosandara Mar 28, 2024
28367a3
eth_signTransaction method and the corresponding tests (UT and e2e)
novosandara Mar 28, 2024
bac326e
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Mar 28, 2024
09984a1
e2e Test fix
novosandara Mar 30, 2024
efdd9f7
eth_signTransaction e2e fix
novosandara Mar 30, 2024
8cc0850
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Mar 31, 2024
37f7791
e2e fix
novosandara Mar 31, 2024
b87af15
eth_signTransaction e2e fix
novosandara Mar 31, 2024
745a644
nonce too low, fix
novosandara Mar 31, 2024
dda1364
tidying up the code
novosandara Mar 31, 2024
28df87f
e2e test
novosandara Mar 31, 2024
6bff572
e2e fix
novosandara Mar 31, 2024
1478a40
e2e fix
novosandara Apr 1, 2024
f789b1b
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Apr 1, 2024
d3b5859
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Apr 8, 2024
00b2eb5
CR fix
novosandara Apr 8, 2024
4157d05
CR fix and method eth_sign
novosandara Apr 10, 2024
3a6c420
eth_sign, CR fix
novosandara Apr 14, 2024
08d9c7c
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Apr 14, 2024
0a686bb
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Apr 18, 2024
9ac3ba8
tidying up the code
novosandara Apr 19, 2024
02f138c
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
Stefan-Ethernal Apr 23, 2024
b0a5af1
reorgs
goran-ethernal Apr 23, 2024
0eb93b6
test and lint fix
goran-ethernal Apr 24, 2024
304e9b1
eth_getProof
novosandara Apr 24, 2024
8fc8913
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
novosandara Apr 24, 2024
0672025
fix GetProof
novosandara Apr 25, 2024
c6c7d9e
lint fix
novosandara Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion consensus/polybft/wallet/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func (a *Account) Save(secretsManager secrets.SecretsManager) (err error) {
}

func (a *Account) GetEcdsaPrivateKey() (*ecdsa.PrivateKey, error) {
ecdsaRaw, err := a.Ecdsa.MarshallPrivateKey()
return AdaptECDSAPrivKey(a.Ecdsa)
}

// AdaptECDSAPrivKey converts ecdsa private key from crypto.ECDSAKey to ecdsa.PrivateKey instance
func AdaptECDSAPrivKey(ecdsaKey *crypto.ECDSAKey) (*ecdsa.PrivateKey, error) {
ecdsaRaw, err := ecdsaKey.MarshallPrivateKey()
if err != nil {
return nil, err
}
Expand Down
42 changes: 1 addition & 41 deletions consensus/polybft/wallet/account_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wallet

import (
"fmt"
"testing"

"github.com/0xPolygon/polygon-edge/secrets"
Expand All @@ -12,7 +11,7 @@ import (
func TestAccount(t *testing.T) {
t.Parallel()

secretsManager := newSecretsManagerMock()
secretsManager := secrets.NewSecretsManagerMock()

key := generateTestAccount(t)
pubKeyMarshalled := key.Bls.PublicKey().Marshal()
Expand All @@ -33,10 +32,6 @@ func TestAccount(t *testing.T) {
assert.Equal(t, privKeyMarshalled, privKeyMarshalled1)
}

func newSecretsManagerMock() secrets.SecretsManager {
return &secretsManagerMock{cache: make(map[string][]byte)}
}

func generateTestAccount(t *testing.T) *Account {
t.Helper()

Expand All @@ -45,38 +40,3 @@ func generateTestAccount(t *testing.T) *Account {

return acc
}

type secretsManagerMock struct {
cache map[string][]byte
}

func (sm *secretsManagerMock) Setup() error {
return nil
}

func (sm *secretsManagerMock) GetSecret(name string) ([]byte, error) {
value, exists := sm.cache[name]
if !exists {
return nil, fmt.Errorf("secret does not exists for %s", name)
}

return value, nil
}

func (sm *secretsManagerMock) SetSecret(name string, value []byte) error {
sm.cache[name] = value

return nil
}

func (sm *secretsManagerMock) HasSecret(name string) bool {
_, exists := sm.cache[name]

return exists
}

func (sm *secretsManagerMock) RemoveSecret(name string) error {
delete(sm.cache, name)

return nil
}
3 changes: 3 additions & 0 deletions crypto/txsigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type TxSigner interface {
// Sender returns the sender of the transaction
Sender(*types.Transaction) (types.Address, error)

// SignCanonical this method should return the signature in 'canonical' format, with v 0 or 1.
SignCanonical(*types.Transaction, *ecdsa.PrivateKey) ([]byte, error)

// SignTx takes the original transaction as input and returns its signed version
SignTx(*types.Transaction, *ecdsa.PrivateKey) (*types.Transaction, error)

Expand Down
20 changes: 20 additions & 0 deletions crypto/txsigner_berlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ func (signer *BerlinSigner) Sender(tx *types.Transaction) (types.Address, error)
return recoverAddress(signer.Hash(tx), r, s, v, true)
}

// SignCanonical this method should return the signature in 'canonical' format, with v 0 or 1.
func (signer *BerlinSigner) SignCanonical(tx *types.Transaction, privateKey *ecdsa.PrivateKey) ([]byte, error) {
if tx.Type() == types.DynamicFeeTxType {
return nil, types.ErrTxTypeNotSupported
}

if tx.Type() != types.AccessListTxType {
return signer.EIP155Signer.SignCanonical(tx, privateKey)
}

signedTx, err := signer.SignTx(tx, privateKey)
if err != nil {
return nil, err
}

v, r, s := signedTx.RawSignatureValues()

return encodeSignature(r, s, v, true)
}

// SignTx takes the original transaction as input and returns its signed version
func (signer *BerlinSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.PrivateKey) (*types.Transaction, error) {
if tx.Type() == types.DynamicFeeTxType {
Expand Down
40 changes: 32 additions & 8 deletions crypto/txsigner_eip155.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ func (signer *EIP155Signer) Sender(tx *types.Transaction) (types.Address, error)
return types.Address{}, errors.New("failed to recover sender, because signature is unknown")
}

bigV := big.NewInt(0).SetBytes(v.Bytes())

if vv := v.Uint64(); bits.Len(uint(vv)) <= 8 {
protected = vv != 27 && vv != 28
}
Expand All @@ -109,13 +107,25 @@ func (signer *EIP155Signer) Sender(tx *types.Transaction) (types.Address, error)
return types.ZeroAddress, err
}

// 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)
parity := signer.calculateParity(v)

return recoverAddress(signer.Hash(tx), r, s, parity, true)
}

// SignCanonical this method should return the signature in 'canonical' format, with v 0 or 1.
func (signer *EIP155Signer) SignCanonical(tx *types.Transaction, privateKey *ecdsa.PrivateKey) ([]byte, error) {
if tx.Type() != types.LegacyTxType && tx.Type() != types.StateTxType {
return nil, types.ErrTxTypeNotSupported
}

signedTx, err := signer.SignTx(tx, privateKey)
if err != nil {
return nil, err
}

v, r, s := signedTx.RawSignatureValues()

return recoverAddress(signer.Hash(tx), r, s, bigV, true)
return encodeSignature(r, s, signer.calculateParity(v), true)
}

// SignTx takes the original transaction as input and returns its signed version
Expand Down Expand Up @@ -176,3 +186,17 @@ func (signer *EIP155Signer) calculateV(parity byte) []byte {

return a.Bytes()
}

// calculateParity returns the parity of the Y coordinate
// 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
func (signer *EIP155Signer) calculateParity(v *big.Int) *big.Int {
// a = V - 35
a := big.NewInt(0).Sub(v, big35)

// b = CHAIN_ID * 2
b := big.NewInt(0).Mul(big.NewInt(int64(signer.chainID)), big.NewInt(2))

// parity = a - b
return big.NewInt(0).Sub(a, b)
}
23 changes: 20 additions & 3 deletions crypto/txsigner_frontier.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,23 @@ func (signer *FrontierSigner) sender(tx *types.Transaction, isHomestead bool) (t
return types.Address{}, errors.New("failed to recover sender, because signature is unknown")
}

// 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)
parity := signer.calculateParity(v)

return recoverAddress(signer.Hash(tx), r, s, parity, isHomestead)
}

// SignCanonical this method should return the signature in 'canonical' format, with v 0 or 1.
func (signer *FrontierSigner) SignCanonical(tx *types.Transaction, privateKey *ecdsa.PrivateKey) ([]byte, error) {
signedTx, err := signer.SignTx(tx, privateKey)
if err != nil {
return nil, err
}

v, r, s := signedTx.RawSignatureValues()

return encodeSignature(r, s, signer.calculateParity(v), true)
}

// SignTx takes the original transaction as input and returns its signed version
func (signer *FrontierSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.PrivateKey) (*types.Transaction, error) {
return signer.signTxInternal(tx, privateKey)
Expand Down Expand Up @@ -145,3 +155,10 @@ func (signer *FrontierSigner) calculateV(parity byte) []byte {

return result.Bytes()
}

// calculateParity returns the parity of the Y coordinate
// Reverse the V calculation to find the parity of the Y coordinate
// v = {0, 1} + 27 -> {0, 1} = v - 27
func (signer *FrontierSigner) calculateParity(v *big.Int) *big.Int {
return big.NewInt(0).Sub(v, big27)
}
16 changes: 16 additions & 0 deletions crypto/txsigner_london.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ func (signer *LondonSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.Priv
return tx, nil
}

// SignCanonical this method should return the signature in 'canonical' format, with v 0 or 1.
func (signer *LondonSigner) SignCanonical(tx *types.Transaction, privateKey *ecdsa.PrivateKey) ([]byte, error) {
if tx.Type() != types.DynamicFeeTxType {
return signer.BerlinSigner.SignCanonical(tx, privateKey)
}

signedTx, err := signer.SignTx(tx, privateKey)
if err != nil {
return nil, err
}

v, r, s := signedTx.RawSignatureValues()

return encodeSignature(r, s, v, true)
}

func (signer *LondonSigner) SignTxWithCallback(tx *types.Transaction,
signFn func(hash types.Hash) (sig []byte, err error)) (*types.Transaction, error) {
if tx.Type() != types.DynamicFeeTxType {
Expand Down
152 changes: 152 additions & 0 deletions crypto/txsigner_london_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,158 @@ func TestLondonSignerSender(t *testing.T) {
}
}

func TestTxSigner_SignCanonical(t *testing.T) {
t.Parallel()

key, err := GenerateECDSAPrivateKey()
require.NoError(t, err, "unable to generate private key")

to := types.StringToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")

cases := []struct {
name string
txn *types.Transaction
signer TxSigner
errorExpected bool
}{
{
name: "LondonSigner - legacy tx",
txn: types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(10000)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(1)),
types.WithNonce(1),
)),
signer: NewLondonSigner(100),
},
{
name: "LondonSigner - dynamic tx",
txn: types.NewTx(types.NewDynamicFeeTx(
types.WithGasFeeCap(ethgo.Gwei(10)),
types.WithGasTipCap(ethgo.Gwei(1)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(1)),
types.WithNonce(1),
)),
signer: NewLondonSigner(100),
},
{
name: "BerlinSigner - legacy tx",
txn: types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(500)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(11)),
types.WithNonce(11),
)),
signer: NewBerlinSigner(100),
},
{
name: "BerlinSigner - dynamic tx",
txn: types.NewTx(types.NewDynamicFeeTx(
types.WithGasFeeCap(ethgo.Gwei(110)),
types.WithGasTipCap(ethgo.Gwei(121)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(21)),
types.WithNonce(1),
)),
signer: NewBerlinSigner(100),
errorExpected: true,
},
{
name: "EIP155Signer - legacy tx",
txn: types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(300)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(1311)),
types.WithNonce(1781),
)),
signer: NewEIP155Signer(100),
},
{
name: "EIP155Signer - dynamic tx",
txn: types.NewTx(types.NewDynamicFeeTx(
types.WithGasFeeCap(ethgo.Gwei(210)),
types.WithGasTipCap(ethgo.Gwei(221)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(221)),
types.WithNonce(11),
)),
signer: NewEIP155Signer(100),
errorExpected: true,
},
{
name: "HomesteadSigner - legacy tx",
txn: types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(700)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(151)),
types.WithNonce(111),
)),
signer: NewHomesteadSigner(),
},
{
name: "HomesteadSigner - dynamic tx",
txn: types.NewTx(types.NewDynamicFeeTx(
types.WithGasFeeCap(ethgo.Gwei(310)),
types.WithGasTipCap(ethgo.Gwei(321)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(321)),
types.WithNonce(111),
)),
signer: NewHomesteadSigner(),
errorExpected: true,
},
{
name: "FrontierSigner - legacy tx",
txn: types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(1200)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(121)),
types.WithNonce(113),
)),
signer: NewFrontierSigner(),
},
{
name: "FrontierSigner - dynamic tx",
txn: types.NewTx(types.NewDynamicFeeTx(
types.WithGasFeeCap(ethgo.Gwei(410)),
types.WithGasTipCap(ethgo.Gwei(521)),
types.WithGas(21000),
types.WithTo(&to),
types.WithValue(big.NewInt(421)),
types.WithNonce(1111),
)),
signer: NewFrontierSigner(),
errorExpected: true,
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

sig, err := tc.signer.SignCanonical(tc.txn, key)
if tc.errorExpected {
require.Error(t, err, "expected error")
} else {
require.NoError(t, err, "unable to sign transaction")
require.NotEmpty(t, sig)
require.Equal(t, 65, len(sig))
}
})
}
}

func Test_LondonSigner_Sender(t *testing.T) {
t.Parallel()

Expand Down
Loading
Loading