Add wallet commands to JSON-RPC client #73

Merged
anbsky merged 8 commits from feature/wallet into master 2019-10-08 08:08:57 +02:00
3 changed files with 116 additions and 9 deletions
Showing only changes of commit ac75979453 - Show all commits

View file

@ -137,11 +137,17 @@ func (d *Client) SetRPCTimeout(timeout time.Duration) {
//============================================
// NEW SDK
//============================================
func (d *Client) AccountList() (*AccountListResponse, error) {
response := new(AccountListResponse)
return response, d.call(response, "account_list", map[string]interface{}{})
}
func (d *Client) AccountListForWallet(walletID string) (*AccountListResponse, error) {
nikooo777 commented 2019-09-25 16:18:04 +02:00 (Migrated from github.com)
Review

i think we should just add an optional param to the current account functions. just like I did for AccountAdd yesterday

i think we should just add an optional param to the current account functions. just like I did for AccountAdd yesterday
anbsky commented 2019-09-25 18:44:58 +02:00 (Migrated from github.com)
Review

That's how we ended up with v2 of lbry.go already — introducing backwards incompatible changes way too liberally. I don't think we should do that.

P.S. That's another example where opts would make things easier by not breaking backwards compatibility.

That's how we ended up with v2 of lbry.go already — introducing backwards incompatible changes way too liberally. I don't think we should do that. P.S. That's another example where opts would make things easier by not breaking backwards compatibility.
nikooo777 commented 2019-09-30 16:04:40 +02:00 (Migrated from github.com)
Review

I think we shouldn't worry too much about backward compatibility. If one doesn't want to stay up to date with lbry.go/sdk then they can lock themselves into a commit that supports such old versions.
We don't have the time to support multiple versions, only the current one. I still feel like it should not be split out and rather updated

I think we shouldn't worry too much about backward compatibility. If one doesn't want to stay up to date with lbry.go/sdk then they can lock themselves into a commit that supports such old versions. We don't have the time to support multiple versions, only the current one. I still feel like it should not be split out and rather updated
anbsky commented 2019-10-03 20:28:31 +02:00 (Migrated from github.com)
Review

I spent a few minutes adding a nil parameter to AccoutList in calls around the test code that don't know about wallet_id and it felt quite ugly to me. Barring all the uncertainties of designing an API ahead of client code, I'd hazard a guess the majority of clients will be requiring the argument-less version of account_list and we should keep it that.

I spent a few minutes adding a `nil` parameter to `AccoutList` in calls around the test code that don't know about `wallet_id` and it felt quite ugly to me. Barring all the uncertainties of designing an API ahead of client code, I'd hazard a guess the majority of clients will be requiring the argument-less version of `account_list` and we should keep it that.
response := new(AccountListResponse)
return response, d.call(response, "account_list", map[string]interface{}{"wallet_id": walletID})
}
func (d *Client) SingleAccountList(accountID string) (*Account, error) {
response := new(Account)
return response, d.call(response, "account_list", map[string]interface{}{"account_id": accountID})
@ -595,3 +601,33 @@ func (d *Client) AccountAdd(accountName string, seed *string, privateKey *string
structs.DefaultTagName = "json"
return response, d.call(response, "account_add", structs.Map(args))
}
type WalletCreateOpts struct {
SkipOnStartup bool
CreateAccount bool
SingleKey bool
}
func (d *Client) WalletCreate(id string, opts *WalletCreateOpts) (*WalletCommandResponse, error) {
response := new(WalletCommandResponse)
if opts == nil {
opts = &WalletCreateOpts{}
}
params := map[string]interface{}{
"wallet_id": id,
"skip_on_startup": opts.SkipOnStartup,
"create_account": opts.CreateAccount,
"single_key": opts.SingleKey,
}
return response, d.call(response, "wallet_create", params)
}
func (d *Client) WalletAdd(id string) (*WalletCommandResponse, error) {
response := new(WalletCommandResponse)
return response, d.call(response, "wallet_add", map[string]interface{}{"wallet_id": id})
}
func (d *Client) WalletRemove(id string) (*WalletCommandResponse, error) {
response := new(WalletCommandResponse)
return response, d.call(response, "wallet_remove", map[string]interface{}{"wallet_id": id})
}

View file

@ -3,6 +3,7 @@ package jsonrpc
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
@ -21,6 +22,12 @@ func prettyPrint(i interface{}) {
fmt.Println(string(s))
}
func TestMain(m *testing.M) {
rand.Seed(time.Now().UnixNano())
code := m.Run()
os.Exit(code)
}
func TestClient_AccountFund(t *testing.T) {
d := NewClient("")
accounts, err := d.AccountList()
@ -59,17 +66,20 @@ func TestClient_AccountList(t *testing.T) {
func TestClient_SingleAccountList(t *testing.T) {
d := NewClient("")
createdAccount, err := d.AccountCreate("test"+fmt.Sprintf("%d", time.Now().Unix())+"@lbry.com", false)
name := "test" + fmt.Sprintf("%d", rand.Int()) + "@lbry.com"
createdAccount, err := d.AccountCreate(name, false)
if err != nil {
t.Fatal(err)
return
}
account, err := d.SingleAccountList(createdAccount.ID)
prettyPrint(*createdAccount)
prettyPrint(*account)
if err != nil {
t.Fatal(err)
return
}
prettyPrint(*account)
if account.Name != name {
t.Fatalf("account name mismatch: %v != %v", account.Name, name)
}
}
func TestClient_AccountBalance(t *testing.T) {
@ -432,7 +442,7 @@ func TestClient_AccountSet(t *testing.T) {
func TestClient_AccountCreate(t *testing.T) {
d := NewClient("")
name := "test" + fmt.Sprintf("%d", time.Now().Unix()) + "@lbry.com"
name := "lbry#user#id:" + fmt.Sprintf("%d", rand.Int())
account, err := d.AccountCreate(name, false)
if err != nil {
t.Fatal(err)
@ -467,7 +477,8 @@ func TestClient_AccountAdd(t *testing.T) {
func TestClient_AccountRemove(t *testing.T) {
d := NewClient("")
createdAccount, err := d.AccountCreate("test"+fmt.Sprintf("%d", time.Now().Unix())+"@lbry.com", false)
name := "lbry#user#id:" + fmt.Sprintf("%d", rand.Int())
createdAccount, err := d.AccountCreate(name, false)
if err != nil {
t.Fatal(err)
return
@ -487,9 +498,7 @@ func TestClient_AccountRemove(t *testing.T) {
prettyPrint(*removedAccount)
return
}
t.Error(err)
return
t.Fatal(err)
}
t.Error("account was not removed")
prettyPrint(*account)
@ -506,3 +515,60 @@ func TestClient_ChannelExport(t *testing.T) {
}
t.Log("Export:", *response)
}
func TestClient_WalletCreate(t *testing.T) {
d := NewClient("")
id := "lbry#wallet#id:" + fmt.Sprintf("%d", rand.Int())
wallet, err := d.WalletCreate(id, nil)
if err != nil {
t.Fatal(err)
}
if wallet.ID != id {
prettyPrint(*wallet)
t.Fatalf("wallet ID mismatch, expected %q, got %q", id, wallet.Name)
}
}
func TestClient_WalletCreateWithOpts(t *testing.T) {
d := NewClient("")
id := "lbry#wallet#id:" + fmt.Sprintf("%d", rand.Int())
wallet, err := d.WalletCreate(id, &WalletCreateOpts{CreateAccount: true, SingleKey: true})
if err != nil {
t.Fatal(err)
}
accounts, err := d.AccountListForWallet(id)
if err != nil {
t.Fatal(err)
}
prettyPrint(wallet)
prettyPrint(accounts)
if accounts.LBCMainnet[0].Name == "" {
t.Fatalf("account name is empty")
}
}
func TestClient_WalletRemoveWalletAdd(t *testing.T) {
d := NewClient("")
id := "lbry#wallet#id:" + fmt.Sprintf("%d", rand.Int())
wallet, err := d.WalletCreate(id, nil)
if err != nil {
t.Fatal(err)
}
_, err = d.WalletRemove(id)
if err != nil {
t.Fatal(err)
}
addedWallet, err := d.WalletAdd(id)
if err != nil {
t.Fatal(err)
}
if addedWallet.ID != wallet.ID {
prettyPrint(*addedWallet)
t.Fatalf("wallet ID mismatch, expected %q, got %q", wallet.ID, addedWallet.Name)
}
}

View file

@ -538,3 +538,8 @@ type NumClaimsInChannelResponse map[string]struct {
}
type ClaimShowResponse *Claim
type WalletCommandResponse struct {
ID string `json:"id"`
Name string `json:"name"`
}