Add wallet commands to JSON-RPC client #73
|
@ -137,11 +137,17 @@ func (d *Client) SetRPCTimeout(timeout time.Duration) {
|
||||||
//============================================
|
//============================================
|
||||||
// NEW SDK
|
// NEW SDK
|
||||||
//============================================
|
//============================================
|
||||||
|
|
||||||
func (d *Client) AccountList() (*AccountListResponse, error) {
|
func (d *Client) AccountList() (*AccountListResponse, error) {
|
||||||
response := new(AccountListResponse)
|
response := new(AccountListResponse)
|
||||||
return response, d.call(response, "account_list", map[string]interface{}{})
|
return response, d.call(response, "account_list", map[string]interface{}{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Client) AccountListForWallet(walletID string) (*AccountListResponse, error) {
|
||||||
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.
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. 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 spent a few minutes adding a 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) {
|
func (d *Client) SingleAccountList(accountID string) (*Account, error) {
|
||||||
response := new(Account)
|
response := new(Account)
|
||||||
return response, d.call(response, "account_list", map[string]interface{}{"account_id": accountID})
|
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"
|
structs.DefaultTagName = "json"
|
||||||
return response, d.call(response, "account_add", structs.Map(args))
|
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})
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package jsonrpc
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -21,6 +22,12 @@ func prettyPrint(i interface{}) {
|
||||||
fmt.Println(string(s))
|
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) {
|
func TestClient_AccountFund(t *testing.T) {
|
||||||
d := NewClient("")
|
d := NewClient("")
|
||||||
accounts, err := d.AccountList()
|
accounts, err := d.AccountList()
|
||||||
|
@ -59,17 +66,20 @@ func TestClient_AccountList(t *testing.T) {
|
||||||
|
|
||||||
func TestClient_SingleAccountList(t *testing.T) {
|
func TestClient_SingleAccountList(t *testing.T) {
|
||||||
d := NewClient("")
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
account, err := d.SingleAccountList(createdAccount.ID)
|
account, err := d.SingleAccountList(createdAccount.ID)
|
||||||
|
prettyPrint(*createdAccount)
|
||||||
|
prettyPrint(*account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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) {
|
func TestClient_AccountBalance(t *testing.T) {
|
||||||
|
@ -432,7 +442,7 @@ func TestClient_AccountSet(t *testing.T) {
|
||||||
|
|
||||||
func TestClient_AccountCreate(t *testing.T) {
|
func TestClient_AccountCreate(t *testing.T) {
|
||||||
d := NewClient("")
|
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)
|
account, err := d.AccountCreate(name, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -467,7 +477,8 @@ func TestClient_AccountAdd(t *testing.T) {
|
||||||
|
|
||||||
func TestClient_AccountRemove(t *testing.T) {
|
func TestClient_AccountRemove(t *testing.T) {
|
||||||
d := NewClient("")
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
return
|
return
|
||||||
|
@ -487,9 +498,7 @@ func TestClient_AccountRemove(t *testing.T) {
|
||||||
prettyPrint(*removedAccount)
|
prettyPrint(*removedAccount)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
t.Fatal(err)
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
t.Error("account was not removed")
|
t.Error("account was not removed")
|
||||||
prettyPrint(*account)
|
prettyPrint(*account)
|
||||||
|
@ -506,3 +515,60 @@ func TestClient_ChannelExport(t *testing.T) {
|
||||||
}
|
}
|
||||||
t.Log("Export:", *response)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -538,3 +538,8 @@ type NumClaimsInChannelResponse map[string]struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClaimShowResponse *Claim
|
type ClaimShowResponse *Claim
|
||||||
|
|
||||||
|
type WalletCommandResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
i think we should just add an optional param to the current account functions. just like I did for AccountAdd yesterday