Skip to content

Commit

Permalink
rpcclient: add more wallet commands
Browse files Browse the repository at this point in the history
Implement backupwallet, dumpwallet, loadwallet and unloadwallet.
  • Loading branch information
torkelrogstad committed Oct 9, 2020
1 parent 6519c04 commit 37b4510
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 5 deletions.
36 changes: 36 additions & 0 deletions btcjson/walletsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,39 @@ func NewGetWalletInfoCmd() *GetWalletInfoCmd {
return &GetWalletInfoCmd{}
}

// BackupWalletCmd defines the backupwallet JSON-RPC command
type BackupWalletCmd struct {
Destination string
}

// NewBackupWalletCmd returns a new instance which can be used to issue a
// backupwallet JSON-RPC command
func NewBackupWalletCmd(destination string) *BackupWalletCmd {
return &BackupWalletCmd{Destination: destination}
}

// UnloadWalletCmd defines the unloadwallet JSON-RPC command
type UnloadWalletCmd struct {
WalletName string
}

// NewUnloadWalletCmd returns a new instance which can be used to issue a
// unloadwallet JSON-RPC command.
func NewUnloadWalletCmd(walletName string) *UnloadWalletCmd {
return &UnloadWalletCmd{WalletName: walletName}
}

// LoadWalletCmd defines the loadwallet JSON-RPC command
type LoadWalletCmd struct {
WalletName string
}

// NewLoadWalletCmd returns a new instance which can be used to issue a
// loadwallet JSON-RPC command
func NewLoadWalletCmd(walletName string) *LoadWalletCmd {
return &LoadWalletCmd{WalletName: walletName}
}

// ImportPrivKeyCmd defines the importprivkey JSON-RPC command.
type ImportPrivKeyCmd struct {
PrivKey string
Expand Down Expand Up @@ -1030,6 +1063,7 @@ func init() {

MustRegisterCmd("addmultisigaddress", (*AddMultisigAddressCmd)(nil), flags)
MustRegisterCmd("addwitnessaddress", (*AddWitnessAddressCmd)(nil), flags)
MustRegisterCmd("backupwallet", (*BackupWalletCmd)(nil), flags)
MustRegisterCmd("createmultisig", (*CreateMultisigCmd)(nil), flags)
MustRegisterCmd("dumpprivkey", (*DumpPrivKeyCmd)(nil), flags)
MustRegisterCmd("encryptwallet", (*EncryptWalletCmd)(nil), flags)
Expand Down Expand Up @@ -1059,6 +1093,7 @@ func init() {
MustRegisterCmd("listsinceblock", (*ListSinceBlockCmd)(nil), flags)
MustRegisterCmd("listtransactions", (*ListTransactionsCmd)(nil), flags)
MustRegisterCmd("listunspent", (*ListUnspentCmd)(nil), flags)
MustRegisterCmd("loadwallet", (*LoadWalletCmd)(nil), flags)
MustRegisterCmd("lockunspent", (*LockUnspentCmd)(nil), flags)
MustRegisterCmd("move", (*MoveCmd)(nil), flags)
MustRegisterCmd("sendfrom", (*SendFromCmd)(nil), flags)
Expand All @@ -1069,6 +1104,7 @@ func init() {
MustRegisterCmd("signmessage", (*SignMessageCmd)(nil), flags)
MustRegisterCmd("signrawtransaction", (*SignRawTransactionCmd)(nil), flags)
MustRegisterCmd("signrawtransactionwithwallet", (*SignRawTransactionWithWalletCmd)(nil), flags)
MustRegisterCmd("unloadwallet", (*UnloadWalletCmd)(nil), flags)
MustRegisterCmd("walletlock", (*WalletLockCmd)(nil), flags)
MustRegisterCmd("walletpassphrase", (*WalletPassphraseCmd)(nil), flags)
MustRegisterCmd("walletpassphrasechange", (*WalletPassphraseChangeCmd)(nil), flags)
Expand Down
33 changes: 33 additions & 0 deletions btcjson/walletsvrcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ func TestWalletSvrCmds(t *testing.T) {
Address: "1address",
},
},
{
name: "backupwallet",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("backupwallet", "backup.dat")
},
staticCmd: func() interface{} {
return btcjson.NewBackupWalletCmd("backup.dat")
},
marshalled: `{"jsonrpc":"1.0","method":"backupwallet","params":["backup.dat"],"id":1}`,
unmarshalled: &btcjson.BackupWalletCmd{Destination: "backup.dat"},
},
{
name: "loadwallet",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("loadwallet", "wallet.dat")
},
staticCmd: func() interface{} {
return btcjson.NewLoadWalletCmd("wallet.dat")
},
marshalled: `{"jsonrpc":"1.0","method":"loadwallet","params":["wallet.dat"],"id":1}`,
unmarshalled: &btcjson.LoadWalletCmd{WalletName: "wallet.dat"},
},
{
name: "unloadwallet",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("unloadwallet", "wallet.dat")
},
staticCmd: func() interface{} {
return btcjson.NewUnloadWalletCmd("wallet.dat")
},
marshalled: `{"jsonrpc":"1.0","method":"unloadwallet","params":["wallet.dat"],"id":1}`,
unmarshalled: &btcjson.UnloadWalletCmd{WalletName: "wallet.dat"},
},
{
name: "createmultisig",
newCmd: func() (interface{}, error) {
Expand Down
126 changes: 121 additions & 5 deletions rpcclient/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2608,12 +2608,128 @@ func (c *Client) GetWalletInfo() (*btcjson.GetWalletInfoResult, error) {
return c.GetWalletInfoAsync().Receive()
}

// FutureBackupWalletResult is a future promise to deliver the result of an
// BackupWalletAsync RPC invocation (or an applicable error)
type FutureBackupWalletResult chan *response

// Receive waits for the response promised by the future
func (r FutureBackupWalletResult) Receive() error {
_, err := receiveFuture(r)
return err
}

// BackupWalletAsync returns an instance of a type that can be used to get the result
// of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See BackupWallet for the blocking version and more details.
func (c *Client) BackupWalletAsync(destination string) FutureBackupWalletResult {
return c.sendCmd(btcjson.NewBackupWalletCmd(destination))
}

// BackupWallet safely copies current wallet file to destination, which can
// be a directory or a path with filename
func (c *Client) BackupWallet(destination string) error {
return c.BackupWalletAsync(destination).Receive()
}

// FutureDumpWalletResult is a future promise to deliver the result of an
// DumpWallet RPC invocation (or an applicable error)
type FutureDumpWalletResult chan *response

// Receive waits for the response promised by the future
func (r FutureDumpWalletResult) Receive() error {
_, err := receiveFuture(r)
return err
}

// DumpWalletAsync returns an instance of a type that can be used to get the result
// of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See DumpWalletAsync for the blocking version and more details.
func (c *Client) DumpWalletAsync(destination string) FutureDumpWalletResult {
return c.sendCmd(btcjson.NewDumpWalletCmd(destination))
}

// DumpWallet dumps all wallet keys in a human-readable format to a server-side file.
func (c *Client) DumpWallet(destination string) error {
return c.DumpWalletAsync(destination).Receive()
}

// FutureImportWalletResult is a future promise to deliver the result of an
// ImportWalletAsync RPC invocation (or an applicable error)
type FutureImportWalletResult chan *response

// Receive waits for the response promised by the future
func (r FutureImportWalletResult) Receive() error {
_, err := receiveFuture(r)
return err
}

// ImportWalletAsync returns an instance of a type that can be used to get the result
// of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ImportWallet for the blocking version and more details.
func (c *Client) ImportWalletAsync(filename string) FutureImportWalletResult {
return c.sendCmd(btcjson.NewImportWalletCmd(filename))
}

// ImportWallet imports keys from a wallet dump file (see DumpWallet).
func (c *Client) ImportWallet(filename string) error {
return c.ImportWalletAsync(filename).Receive()
}

// FutureUnloadWalletResult is a future promise to deliver the result of an
// UnloadWalletAsync RPC invocation (or an applicable error)
type FutureUnloadWalletResult chan *response

// Receive waits for the response promised by the future
func (r FutureUnloadWalletResult) Receive() error {
_, err := receiveFuture(r)
return err
}

// UnloadWalletAsync returns an instance of a type that can be used to get the result
// of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See UnloadWallet for the blocking version and more details.
func (c *Client) UnloadWalletAsync(walletName string) FutureUnloadWalletResult {
return c.sendCmd(btcjson.NewUnloadWalletCmd(walletName))
}

// UnloadWallet unloads the referenced wallet
func (c *Client) UnloadWallet(walletName string) error {
return c.UnloadWalletAsync(walletName).Receive()
}

// FutureLoadWalletResult is a future promise to deliver the result of an
// LoadWalletAsync RPC invocation (or an applicable error)
type FutureLoadWalletResult chan *response

// Receive waits for the response promised by the future
func (r FutureLoadWalletResult) Receive() error {
_, err := receiveFuture(r)
return err
}

// LoadWalletAsync returns an instance of a type that can be used to get the result
// of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See LoadWallet for the blocking version and more details.
func (c *Client) LoadWalletAsync(walletName string) FutureLoadWalletResult {
return c.sendCmd(btcjson.NewLoadWalletCmd(walletName))
}

// LoadWallet loads a wallet from a wallet file or directory.
func (c *Client) LoadWallet(walletName string) error {
return c.LoadWalletAsync(walletName).Receive()
}

// TODO(davec): Implement
// backupwallet (NYI in btcwallet)
// encryptwallet (Won't be supported by btcwallet since it's always encrypted)
// listaddressgroupings (NYI in btcwallet)
// listreceivedbyaccount (NYI in btcwallet)

// DUMP
// importwallet (NYI in btcwallet)
// dumpwallet (NYI in btcwallet)

0 comments on commit 37b4510

Please sign in to comment.