From 413e02870214b8600fe7219b43ac417df97587bf Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Thu, 23 Jan 2014 10:56:20 -0500 Subject: [PATCH] Add RecoverAddressesCmd. --- cmds.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/cmds.go b/cmds.go index 4954792c..bf3eb63d 100644 --- a/cmds.go +++ b/cmds.go @@ -40,6 +40,8 @@ first command sent or you will be disconnected.`) `TODO(jrick) fillmein`) btcjson.RegisterCustomCmd("notifyspent", parseNotifySpentCmd, `TODO(jrick) fillmein`) + btcjson.RegisterCustomCmd("recoveraddresses", parseRecoverAddressesCmd, + `TODO(jrick) fillmein`) btcjson.RegisterCustomCmd("rescan", parseRescanCmd, `TODO(jrick) fillmein`) btcjson.RegisterCustomCmd("walletislocked", parseWalletIsLockedCmd, @@ -503,6 +505,99 @@ func (cmd *GetBestBlockCmd) UnmarshalJSON(b []byte) error { return nil } +// RecoverAddressesCmd is a type handling custom marshaling and +// unmarshaling of recoveraddresses JSON websocket extension +// commands. +type RecoverAddressesCmd struct { + id interface{} + Account string + N int +} + +// Enforce that RecoverAddressesCmd satisifies the btcjson.Cmd interface. +var _ btcjson.Cmd = &RecoverAddressesCmd{} + +// NewRecoverAddressesCmd creates a new RecoverAddressesCmd. +func NewRecoverAddressesCmd(id interface{}, account string, n int) *RecoverAddressesCmd { + return &RecoverAddressesCmd{ + id: id, + Account: account, + N: n, + } +} + +// parseRecoverAddressesCmd parses a RawCmd into a concrete type satisifying +// the btcjson.Cmd interface. This is used when registering the custom +// command with the btcjson parser. +func parseRecoverAddressesCmd(r *btcjson.RawCmd) (btcjson.Cmd, error) { + if len(r.Params) != 2 { + return nil, btcjson.ErrWrongNumberOfParams + } + + account, ok := r.Params[0].(string) + if !ok { + return nil, errors.New("first parameter account must be a string") + } + n, ok := r.Params[1].(float64) + if !ok { + return nil, errors.New("second parameter n must be a number") + } + return NewRecoverAddressesCmd(r.Id, account, int(n)), nil +} + +// Id satisifies the Cmd interface by returning the ID of the command. +func (cmd *RecoverAddressesCmd) Id() interface{} { + return cmd.id +} + +// SetId satisifies the Cmd interface by setting the ID of the command. +func (cmd *RecoverAddressesCmd) SetId(id interface{}) { + cmd.id = id +} + +// Method satisfies the Cmd interface by returning the RPC method. +func (cmd *RecoverAddressesCmd) Method() string { + return "recoveraddresses" +} + +// MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface. +func (cmd *RecoverAddressesCmd) MarshalJSON() ([]byte, error) { + // Fill a RawCmd and marshal. + raw := btcjson.RawCmd{ + Jsonrpc: "1.0", + Method: "recoveraddresses", + Id: cmd.id, + Params: []interface{}{ + cmd.Account, + cmd.N, + }, + } + + return json.Marshal(raw) +} + +// UnmarshalJSON unmarshals the JSON encoding of cmd into cmd. Part of +// the Cmd interface. +func (cmd *RecoverAddressesCmd) UnmarshalJSON(b []byte) error { + // Unmarshal into a RawCmd. + var r btcjson.RawCmd + if err := json.Unmarshal(b, &r); err != nil { + return err + } + + newCmd, err := parseRecoverAddressesCmd(&r) + if err != nil { + return err + } + + concreteCmd, ok := newCmd.(*RecoverAddressesCmd) + if !ok { + return btcjson.ErrInternal + } + *cmd = *concreteCmd + return nil +} + // RescanCmd is a type handling custom marshaling and // unmarshaling of rescan JSON websocket extension // commands.