From d9d7db7c20a465144a63d7a9b95c2b46fe2ce6de Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Mon, 2 Dec 2013 14:59:09 -0500 Subject: [PATCH] Add listalltransactions command. --- cmds.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/cmds.go b/cmds.go index 7f78c136..20b1580c 100644 --- a/cmds.go +++ b/cmds.go @@ -13,13 +13,14 @@ import ( ) func init() { - btcjson.RegisterCustomCmd("getcurrentnet", parseGetCurrentNetCmd) - btcjson.RegisterCustomCmd("getbestblock", parseGetBestBlockCmd) - btcjson.RegisterCustomCmd("rescan", parseRescanCmd) - btcjson.RegisterCustomCmd("notifynewtxs", parseNotifyNewTXsCmd) - btcjson.RegisterCustomCmd("notifyspent", parseNotifySpentCmd) btcjson.RegisterCustomCmd("createencryptedwallet", parseCreateEncryptedWalletCmd) btcjson.RegisterCustomCmd("getbalances", parseGetBalancesCmd) + btcjson.RegisterCustomCmd("getbestblock", parseGetBestBlockCmd) + btcjson.RegisterCustomCmd("getcurrentnet", parseGetCurrentNetCmd) + btcjson.RegisterCustomCmd("listalltransactions", parseListAllTransactionsCmd) + btcjson.RegisterCustomCmd("notifynewtxs", parseNotifyNewTXsCmd) + btcjson.RegisterCustomCmd("notifyspent", parseNotifySpentCmd) + btcjson.RegisterCustomCmd("rescan", parseRescanCmd) btcjson.RegisterCustomCmd("walletislocked", parseWalletIsLockedCmd) } @@ -742,3 +743,103 @@ func (cmd *WalletIsLockedCmd) UnmarshalJSON(b []byte) error { *cmd = *concreteCmd return nil } + +// ListAllTransactionsCmd is a type handling custom marshaling and +// unmarshaling of walletislocked JSON websocket extension commands. +type ListAllTransactionsCmd struct { + id interface{} + Account string +} + +// Enforce that ListAllTransactionsCmd satisifies the btcjson.Cmd +// interface. +var _ btcjson.Cmd = &ListAllTransactionsCmd{} + +// NewListAllTransactionsCmd creates a new ListAllTransactionsCmd. +func NewListAllTransactionsCmd(id interface{}, + optArgs ...string) (*ListAllTransactionsCmd, error) { + + // Optional arguments set to their default values. + account := "" + + if len(optArgs) > 1 { + return nil, btcjson.ErrInvalidParams + } + + if len(optArgs) == 1 { + account = optArgs[0] + } + + return &ListAllTransactionsCmd{ + id: id, + Account: account, + }, nil +} + +// parseListAllTransactionsCmd parses a ListAllTransactionsCmd into a concrete +// type satisifying the btcjson.Cmd interface. This is used when +// registering the custom command with the btcjson parser. +func parseListAllTransactionsCmd(r *btcjson.RawCmd) (btcjson.Cmd, error) { + if len(r.Params) > 1 { + return nil, btcjson.ErrInvalidParams + } + + if len(r.Params) == 0 { + return NewListAllTransactionsCmd(r.Id) + } + + account, ok := r.Params[0].(string) + if !ok { + return nil, errors.New("account must be a string") + } + return NewListAllTransactionsCmd(r.Id, account) +} + +// Id satisifies the Cmd interface by returning the ID of the command. +func (cmd *ListAllTransactionsCmd) Id() interface{} { + return cmd.id +} + +// Method satisfies the Cmd interface by returning the RPC method. +func (cmd *ListAllTransactionsCmd) Method() string { + return "listalltransactions" +} + +// MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface. +func (cmd *ListAllTransactionsCmd) MarshalJSON() ([]byte, error) { + // Fill a RawCmd and marshal. + raw := btcjson.RawCmd{ + Jsonrpc: "1.0", + Method: "listalltransactions", + Id: cmd.id, + Params: []interface{}{}, + } + + if cmd.Account != "" { + raw.Params = append(raw.Params, cmd.Account) + } + + return json.Marshal(raw) +} + +// UnmarshalJSON unmarshals the JSON encoding of cmd into cmd. Part of +// the Cmd interface. +func (cmd *ListAllTransactionsCmd) UnmarshalJSON(b []byte) error { + // Unmarshal into a RawCmd. + var r btcjson.RawCmd + if err := json.Unmarshal(b, &r); err != nil { + return err + } + + newCmd, err := parseListAllTransactionsCmd(&r) + if err != nil { + return err + } + + concreteCmd, ok := newCmd.(*ListAllTransactionsCmd) + if !ok { + return btcjson.ErrInternal + } + *cmd = *concreteCmd + return nil +}