From b293d11cd36ea00d7b1e2cc0c9fecb0cf672d21a Mon Sep 17 00:00:00 2001 From: Carmen Cabrera Date: Thu, 5 Oct 2023 05:48:11 -0400 Subject: [PATCH] move keystore functionalities to account pkg --- account/account.go | 4 ++-- account/account_test.go | 17 ++++++++--------- {curve => account}/keystore.go | 6 ++++-- 3 files changed, 14 insertions(+), 13 deletions(-) rename {curve => account}/keystore.go (93%) diff --git a/account/account.go b/account/account.go index 16daf378..a0f4c01a 100644 --- a/account/account.go +++ b/account/account.go @@ -45,10 +45,10 @@ type Account struct { ChainId *felt.Felt AccountAddress *felt.Felt publicKey string - ks curve.Keystore + ks Keystore } -func NewAccount(provider rpc.RpcProvider, accountAddress *felt.Felt, publicKey string, keystore curve.Keystore) (*Account, error) { +func NewAccount(provider rpc.RpcProvider, accountAddress *felt.Felt, publicKey string, keystore Keystore) (*Account, error) { account := &Account{ provider: provider, AccountAddress: accountAddress, diff --git a/account/account_test.go b/account/account_test.go index dc643ea9..c55a46de 100644 --- a/account/account_test.go +++ b/account/account_test.go @@ -12,7 +12,6 @@ import ( "github.com/joho/godotenv" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/starknet.go/account" - "github.com/NethermindEth/starknet.go/curve" "github.com/NethermindEth/starknet.go/mocks" "github.com/NethermindEth/starknet.go/rpc" "github.com/NethermindEth/starknet.go/test" @@ -128,7 +127,7 @@ func TestTransactionHashInvoke(t *testing.T) { for _, test := range testSet { t.Run("Transaction hash", func(t *testing.T) { - ks := curve.NewMemKeystore() + ks := account.NewMemKeystore() if test.SetKS { privKeyBI, ok := new(big.Int).SetString(test.PrivKey.String(), 0) require.True(t, ok) @@ -219,7 +218,7 @@ func TestChainIdMOCK(t *testing.T) { for _, test := range testSet { mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainID, nil) - account, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", curve.NewMemKeystore()) + account, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", account.NewMemKeystore()) require.NoError(t, err) require.Equal(t, account.ChainId.String(), test.ExpectedID) } @@ -250,7 +249,7 @@ func TestChainId(t *testing.T) { require.NoError(t, err, "Error in rpc.NewClient") provider := rpc.NewProvider(client) - account, err := account.NewAccount(provider, &felt.Zero, "pubkey", curve.NewMemKeystore()) + account, err := account.NewAccount(provider, &felt.Zero, "pubkey", account.NewMemKeystore()) require.NoError(t, err) require.Equal(t, account.ChainId.String(), test.ExpectedID) } @@ -291,7 +290,7 @@ func TestSignMOCK(t *testing.T) { for _, test := range testSet { privKeyBI, ok := new(big.Int).SetString(test.PrivKey.String(), 0) require.True(t, ok) - ks := curve.NewMemKeystore() + ks := account.NewMemKeystore() ks.Put(test.Address.String(), privKeyBI) mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainId, nil) @@ -382,7 +381,7 @@ func TestAddInvoke(t *testing.T) { provider := rpc.NewProvider(client) // Set up ks - ks := curve.NewMemKeystore() + ks := account.NewMemKeystore() if test.SetKS { fakePrivKeyBI, ok := new(big.Int).SetString(test.PrivKey.String(), 0) require.True(t, ok) @@ -419,7 +418,7 @@ func TestAddDeployAccountDevnet(t *testing.T) { fakeUserPub := utils.TestHexToFelt(t, fakeUser.PublicKey) // Set up ks - ks := curve.NewMemKeystore() + ks := account.NewMemKeystore() fakePrivKeyBI, ok := new(big.Int).SetString(fakeUser.PrivateKey, 0) require.True(t, ok) ks.Put(fakeUser.PublicKey, fakePrivKeyBI) @@ -465,7 +464,7 @@ func TestTransactionHashDeployAccountTestnet(t *testing.T) { ExpectedHash := utils.TestHexToFelt(t, "0x5b6b5927cd70ad7a80efdbe898244525871875c76540b239f6730118598b9cb") ExpectedPrecomputeAddr := utils.TestHexToFelt(t, "0x88d0038623a89bf853c70ea68b1062ccf32b094d1d7e5f924cda8404dc73e1") - ks := curve.NewMemKeystore() + ks := account.NewMemKeystore() fakePrivKeyBI, ok := new(big.Int).SetString(PrivKey.String(), 0) require.True(t, ok) ks.Put(PubKey.String(), fakePrivKeyBI) @@ -509,7 +508,7 @@ func TestTransactionHashDeclare(t *testing.T) { require.NoError(t, err, "Error in rpc.NewClient") provider := rpc.NewProvider(client) - acnt, err := account.NewAccount(provider, &felt.Zero, "", curve.NewMemKeystore()) + acnt, err := account.NewAccount(provider, &felt.Zero, "", account.NewMemKeystore()) require.NoError(t, err) tx := rpc.DeclareTxnV2{ diff --git a/curve/keystore.go b/account/keystore.go similarity index 93% rename from curve/keystore.go rename to account/keystore.go index d187ace5..536f6c22 100644 --- a/curve/keystore.go +++ b/account/keystore.go @@ -1,4 +1,4 @@ -package curve +package account import ( "context" @@ -6,6 +6,8 @@ import ( "fmt" "math/big" "sync" + + "github.com/NethermindEth/starknet.go/curve" ) type Keystore interface { @@ -68,7 +70,7 @@ func sign(ctx context.Context, msgHash *big.Int, key *big.Int) (x *big.Int, y *b err = ctx.Err() default: - x, y, err = Curve.Sign(msgHash, key) + x, y, err = curve.Curve.Sign(msgHash, key) } return x, y, err }