Add wallet commands to JSON-RPC client

This commit is contained in:
Andrey Beletsky 2019-09-25 17:47:12 +07:00
parent 712e346bd2
commit ac75979453
3 changed files with 116 additions and 9 deletions

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) {
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"`
}