Skip to content

Commit

Permalink
added blockchain commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Oct 31, 2023
1 parent 25aa2ca commit ff69f12
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
5 changes: 2 additions & 3 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 11 additions & 7 deletions blockchain/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion blockchain/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions mobile/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
}
Expand All @@ -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})
}
Expand Down Expand Up @@ -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})
}
Expand Down

0 comments on commit ff69f12

Please sign in to comment.