Skip to content

Commit

Permalink
Created new node HTTP API route (with auth): GET /wallet/seed. (not c…
Browse files Browse the repository at this point in the history
…ompatible with scala node)
  • Loading branch information
nickeskov committed Jul 2, 2021
1 parent fc452a7 commit 32bc576
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"encoding/json"
"github.com/mr-tron/base58"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -96,6 +97,18 @@ func (a *App) LoadKeys(apiKey string, password []byte) error {
return a.services.Wallet.Load(password)
}

// WalletSeeds returns wallet seeds in base58 encoding.
func (a *App) WalletSeeds() []string {
seeds := a.services.Wallet.Seeds()

seeds58 := make([]string, 0, len(seeds))
for _, seed := range seeds {
seed58 := base58.Encode(seed)
seeds58 = append(seeds58, seed58)
}
return seeds58
}

func (a *App) Accounts() ([]account, error) {
seeds := a.services.Wallet.Seeds()

Expand Down
20 changes: 20 additions & 0 deletions pkg/api/node_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,26 @@ func (a *NodeApi) sendSelfInterrupt(w http.ResponseWriter, _ *http.Request) erro
return nil
}

func (a *NodeApi) walletSeed(w http.ResponseWriter, _ *http.Request) error {
type seed struct {
Seed string `json:"seed"`
}

// TODO(nickeskov): This works not like in scala node.
// Scala node don't have multiple wallets, it have only one wallet.

seeds58 := a.app.WalletSeeds()
seeds := make([]seed, 0, len(seeds58))
for _, seed58 := range seeds58 {
seeds = append(seeds, seed{Seed: seed58})
}

if err := trySendJson(w, seeds); err != nil {
return errors.Wrap(err, "walletSeed")
}
return nil
}

// tryParseJson receives reader and out params. out MUST be a pointer
func tryParseJson(r io.Reader, out interface{}) error {
// TODO(nickeskov): check empty reader
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ func (a *NodeApi) routes(opts *RunOptions) (chi.Router, error) {
rAuth.Post("/stop", wrapper(a.sendSelfInterrupt))
})

r.Route("/wallet", func(r chi.Router) {
rAuth := r.With(checkAuthMiddleware)

rAuth.Get("/seed", wrapper(a.walletSeed))
})

// enable or disable history sync
//r.Get("/debug/sync/{enabled:\\d+}", a.DebugSyncEnabled)
})
Expand Down

0 comments on commit 32bc576

Please sign in to comment.