From d5102a9cf67331f03515b6e5c3c91eeded317935 Mon Sep 17 00:00:00 2001 From: Andrey Beletsky Date: Wed, 25 Sep 2019 18:00:37 +0700 Subject: [PATCH] Add WalletList command --- extras/jsonrpc/daemon.go | 21 +++++++++++++++------ extras/jsonrpc/daemon_test.go | 29 +++++++++++++++++++++++++++++ extras/jsonrpc/daemon_types.go | 4 +++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/extras/jsonrpc/daemon.go b/extras/jsonrpc/daemon.go index 1f283a1..c1ee397 100644 --- a/extras/jsonrpc/daemon.go +++ b/extras/jsonrpc/daemon.go @@ -608,8 +608,8 @@ type WalletCreateOpts struct { SingleKey bool } -func (d *Client) WalletCreate(id string, opts *WalletCreateOpts) (*WalletCommandResponse, error) { - response := new(WalletCommandResponse) +func (d *Client) WalletCreate(id string, opts *WalletCreateOpts) (*Wallet, error) { + response := new(Wallet) if opts == nil { opts = &WalletCreateOpts{} } @@ -622,12 +622,21 @@ func (d *Client) WalletCreate(id string, opts *WalletCreateOpts) (*WalletCommand return response, d.call(response, "wallet_create", params) } -func (d *Client) WalletAdd(id string) (*WalletCommandResponse, error) { - response := new(WalletCommandResponse) +func (d *Client) WalletAdd(id string) (*Wallet, error) { + response := new(Wallet) return response, d.call(response, "wallet_add", map[string]interface{}{"wallet_id": id}) } -func (d *Client) WalletRemove(id string) (*WalletCommandResponse, error) { - response := new(WalletCommandResponse) +func (d *Client) WalletList(id string) (*WalletList, error) { + response := new(WalletList) + params := map[string]interface{}{} + if id != "" { + params["wallet_id"] = id + } + return response, d.call(response, "wallet_list", params) +} + +func (d *Client) WalletRemove(id string) (*Wallet, error) { + response := new(Wallet) return response, d.call(response, "wallet_remove", map[string]interface{}{"wallet_id": id}) } diff --git a/extras/jsonrpc/daemon_test.go b/extras/jsonrpc/daemon_test.go index 600f250..bd7d316 100644 --- a/extras/jsonrpc/daemon_test.go +++ b/extras/jsonrpc/daemon_test.go @@ -549,6 +549,35 @@ func TestClient_WalletCreateWithOpts(t *testing.T) { } } +func TestClient_WalletList(t *testing.T) { + d := NewClient("") + + id := "lbry#wallet#id:" + fmt.Sprintf("%d", rand.Int()) + wList, err := d.WalletList(id) + if err == nil { + t.Fatalf("wallet %v was unexpectedly found", id) + } + if err.Error() != fmt.Sprintf("Error in daemon: Couldn't find wallet: %v.", id) { + t.Fatal(err) + } + + _, err = d.WalletCreate(id, &WalletCreateOpts{CreateAccount: true, SingleKey: true}) + if err != nil { + t.Fatal(err) + } + + wList, err = d.WalletList(id) + if err != nil { + t.Fatal(err) + } + if len(*wList) < 1 { + t.Fatal("wallet list is empty") + } + if (*wList)[0].ID != id { + t.Fatalf("wallet ID mismatch, expected %q, got %q", id, (*wList)[0].ID) + } +} + func TestClient_WalletRemoveWalletAdd(t *testing.T) { d := NewClient("") diff --git a/extras/jsonrpc/daemon_types.go b/extras/jsonrpc/daemon_types.go index 926f072..91c15a7 100644 --- a/extras/jsonrpc/daemon_types.go +++ b/extras/jsonrpc/daemon_types.go @@ -539,7 +539,9 @@ type NumClaimsInChannelResponse map[string]struct { type ClaimShowResponse *Claim -type WalletCommandResponse struct { +type Wallet struct { ID string `json:"id"` Name string `json:"name"` } + +type WalletList []Wallet