From ff69f12966b86a534125659551890f4741dc6ade Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Tue, 31 Oct 2023 18:08:27 -0400 Subject: [PATCH] added blockchain commands --- blockchain/blockchain.go | 5 ++--- blockchain/keystore.go | 18 +++++++++++------- blockchain/keystore_test.go | 2 +- mobile/blockchain.go | 7 +++---- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 2fd7ffad..33f2ea95 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -177,14 +177,14 @@ func (bl *FxBlockchain) PlugSeedIfNeeded(ctx context.Context, action string, req seed, err := bl.keyStorer.LoadKey(ctx) if err != nil { log.Errorw("seed is empty", "err", err) - seed = []byte{} + seed = "" } return struct { ReqInterface Seed string }{ ReqInterface: req, - Seed: string(seed), + Seed: seed, } default: return req @@ -217,7 +217,6 @@ func (bl *FxBlockchain) serve(w http.ResponseWriter, r *http.Request) { bl.handleAction(http.MethodPost, actionPoolCreate, from, w, r) }, actionPoolJoin: func(from peer.ID, w http.ResponseWriter, r *http.Request) { - //TODO: We should check if from owns the blox bl.handleAction(http.MethodPost, actionPoolJoin, from, w, r) }, actionPoolCancelJoin: func(from peer.ID, w http.ResponseWriter, r *http.Request) { diff --git a/blockchain/keystore.go b/blockchain/keystore.go index a3874b76..58375fa9 100644 --- a/blockchain/keystore.go +++ b/blockchain/keystore.go @@ -7,8 +7,8 @@ import ( // Implementations for this interface should be responsible for saving/loading a single key. type KeyStorer interface { - SaveKey(ctx context.Context, key []byte) error - LoadKey(ctx context.Context) ([]byte, error) + SaveKey(ctx context.Context, key string) error + LoadKey(ctx context.Context) (string, error) } type SimpleKeyStorer struct { @@ -17,13 +17,17 @@ type SimpleKeyStorer struct { func NewSimpleKeyStorer() *SimpleKeyStorer { // Saving the db in the local dir - return &SimpleKeyStorer{dbPath: "."} + return &SimpleKeyStorer{dbPath: "/internal/.secrets"} } -func (s *SimpleKeyStorer) SaveKey(ctx context.Context, key []byte) error { - return os.WriteFile(s.dbPath+"/key.db", key, 0400) +func (s *SimpleKeyStorer) SaveKey(ctx context.Context, key string) error { + return os.WriteFile(s.dbPath+"/secret_seed.txt", []byte(key), 0400) } -func (s *SimpleKeyStorer) LoadKey(ctx context.Context) ([]byte, error) { - return os.ReadFile(s.dbPath + "/key.db") +func (s *SimpleKeyStorer) LoadKey(ctx context.Context) (string, error) { + data, err := os.ReadFile(s.dbPath + "/secret_seed.txt") + if err != nil { + return "", err + } + return string(data), nil } diff --git a/blockchain/keystore_test.go b/blockchain/keystore_test.go index ee3a4c9e..36ee4872 100644 --- a/blockchain/keystore_test.go +++ b/blockchain/keystore_test.go @@ -7,7 +7,7 @@ import ( func TestSimpleKeyStore(t *testing.T) { keyStore := NewSimpleKeyStorer() - err := keyStore.SaveKey(context.Background(), []byte("dummy")) + err := keyStore.SaveKey(context.Background(), "dummy") if err != nil { t.Errorf("while save key: %v", err) } diff --git a/mobile/blockchain.go b/mobile/blockchain.go index 82555114..3d413199 100644 --- a/mobile/blockchain.go +++ b/mobile/blockchain.go @@ -53,8 +53,7 @@ func (c *Client) PoolCreate(seed string, poolName string) ([]byte, error) { // PoolJoin requests blox at Config.BloxAddr to join a pool with the id. // the addr must be a valid multiaddr that includes peer ID. // Note that this call is only allowed on a user's own blox -// TODO: This still needs rethink as someone should not be able to put another person PeerID in request -func (c *Client) PoolJoin(seed string, poolID int) ([]byte, error) { +func (c *Client) PoolJoin(poolID int) ([]byte, error) { ctx := context.TODO() return c.bl.PoolJoin(ctx, c.bloxPid, blockchain.PoolJoinRequest{PoolID: poolID, PeerID: c.bloxPid.String()}) } @@ -63,7 +62,7 @@ func (c *Client) PoolJoin(seed string, poolID int) ([]byte, error) { // the addr must be a valid multiaddr that includes peer ID. // Note that this call is only allowed on a user's own blox // TODO: This still needs rethink as someone should not be able to put another person PeerID in request -func (c *Client) PoolCancelJoin(seed string, poolID int) ([]byte, error) { +func (c *Client) PoolCancelJoin(poolID int) ([]byte, error) { ctx := context.TODO() return c.bl.PoolCancelJoin(ctx, c.bloxPid, blockchain.PoolCancelJoinRequest{PoolID: poolID}) } @@ -102,7 +101,7 @@ func (c *Client) PoolVote(seed string, poolID int, account string, voteValue boo // the addr must be a valid multiaddr that includes peer ID. // Note that this call is only allowed on a user's own blox // TODO: This still needs rethink as someone should not be able to put another person PeerID in request -func (c *Client) PoolLeave(seed string, poolID int) ([]byte, error) { +func (c *Client) PoolLeave(poolID int) ([]byte, error) { ctx := context.TODO() return c.bl.PoolLeave(ctx, c.bloxPid, blockchain.PoolLeaveRequest{PoolID: poolID}) }