Skip to content

Commit

Permalink
move keystore functionalities to account pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
cicr99 committed Oct 5, 2023
1 parent fd18018 commit b293d11
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 8 additions & 9 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 4 additions & 2 deletions curve/keystore.go → account/keystore.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package curve
package account

import (
"context"
"errors"
"fmt"
"math/big"
"sync"

"github.com/NethermindEth/starknet.go/curve"
)

type Keystore interface {
Expand Down Expand Up @@ -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
}

0 comments on commit b293d11

Please sign in to comment.