2013-11-08 18:41:04 +01:00
|
|
|
// Copyright (c) 2013 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"github.com/conformal/btcjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-12-13 16:58:30 +01:00
|
|
|
// ErrNotANtfn describes an error where a JSON-RPC Request
|
|
|
|
// object cannot be successfully parsed as a notification
|
|
|
|
// due to having an ID.
|
|
|
|
ErrNotANtfn = errors.New("notifications may not have IDs")
|
2013-11-08 18:41:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-12-13 16:58:30 +01:00
|
|
|
// AccountBalanceNtfnMethod is the method of the btcwallet
|
|
|
|
// accountbalance notification.
|
|
|
|
AccountBalanceNtfnMethod = "accountbalance"
|
|
|
|
|
|
|
|
// BlockConnectedNtfnMethod is the method of the btcd
|
|
|
|
// blockconnected notification.
|
|
|
|
BlockConnectedNtfnMethod = "blockconnected"
|
|
|
|
|
|
|
|
// BlockDisconnectedNtfnMethod is the method of the btcd
|
|
|
|
// blockdisconnected notification.
|
|
|
|
BlockDisconnectedNtfnMethod = "blockdisconnected"
|
2013-11-08 18:41:04 +01:00
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// BtcdConnectedNtfnMethod is the method of the btcwallet
|
|
|
|
// btcdconnected notification.
|
|
|
|
BtcdConnectedNtfnMethod = "btcdconnected"
|
|
|
|
|
2014-01-08 21:23:59 +01:00
|
|
|
// ProcessedTxNtfnMethod is the method of the btcd
|
|
|
|
// processedtx notification.
|
2014-01-09 17:51:51 +01:00
|
|
|
ProcessedTxNtfnMethod = "processedtx"
|
2014-01-08 17:28:38 +01:00
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// TxMinedNtfnMethod is the method of the btcd txmined
|
2013-11-08 18:41:04 +01:00
|
|
|
// notification.
|
2013-12-13 16:58:30 +01:00
|
|
|
TxMinedNtfnMethod = "txmined"
|
2013-11-08 18:41:04 +01:00
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// TxNtfnMethod is the method of the btcwallet newtx
|
|
|
|
// notification.
|
|
|
|
TxNtfnMethod = "newtx"
|
2013-12-02 23:32:17 +01:00
|
|
|
|
2014-01-09 03:29:19 +01:00
|
|
|
// TxSpentNtfnMethod is the method of the btcd txspent
|
|
|
|
// notification.
|
|
|
|
TxSpentNtfnMethod = "txspent"
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// WalletLockStateNtfnMethod is the method of the btcwallet
|
|
|
|
// walletlockstate notification.
|
|
|
|
WalletLockStateNtfnMethod = "walletlockstate"
|
2013-11-08 18:41:04 +01:00
|
|
|
)
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Register notifications with btcjson.
|
|
|
|
func init() {
|
|
|
|
btcjson.RegisterCustomCmd(AccountBalanceNtfnMethod, parseAccountBalanceNtfn)
|
|
|
|
btcjson.RegisterCustomCmd(BlockConnectedNtfnMethod, parseBlockConnectedNtfn)
|
|
|
|
btcjson.RegisterCustomCmd(BlockDisconnectedNtfnMethod, parseBlockDisconnectedNtfn)
|
|
|
|
btcjson.RegisterCustomCmd(BtcdConnectedNtfnMethod, parseBtcdConnectedNtfn)
|
2014-01-08 21:23:59 +01:00
|
|
|
btcjson.RegisterCustomCmd(ProcessedTxNtfnMethod, parseProcessedTxNtfn)
|
2013-12-13 16:58:30 +01:00
|
|
|
btcjson.RegisterCustomCmd(TxMinedNtfnMethod, parseTxMinedNtfn)
|
2014-01-09 03:29:19 +01:00
|
|
|
btcjson.RegisterCustomCmd(TxSpentNtfnMethod, parseTxSpentNtfn)
|
2013-12-13 16:58:30 +01:00
|
|
|
btcjson.RegisterCustomCmd(TxNtfnMethod, parseTxNtfn)
|
|
|
|
btcjson.RegisterCustomCmd(WalletLockStateNtfnMethod, parseWalletLockStateNtfn)
|
|
|
|
}
|
2013-11-08 18:41:04 +01:00
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// AccountBalanceNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of accountbalance JSON websocket notifications.
|
|
|
|
type AccountBalanceNtfn struct {
|
|
|
|
Account string
|
|
|
|
Balance float64
|
|
|
|
Confirmed bool // Whether Balance is confirmed or unconfirmed.
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Enforce that AccountBalanceNtfn satisifes the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &AccountBalanceNtfn{}
|
|
|
|
|
|
|
|
// NewAccountBalanceNtfn creates a new AccountBalanceNtfn.
|
|
|
|
func NewAccountBalanceNtfn(account string, balance float64,
|
|
|
|
confirmed bool) *AccountBalanceNtfn {
|
|
|
|
|
|
|
|
return &AccountBalanceNtfn{
|
|
|
|
Account: account,
|
|
|
|
Balance: balance,
|
|
|
|
Confirmed: confirmed,
|
|
|
|
}
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// parseAccountBalanceNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseAccountBalanceNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 3 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
account, ok := r.Params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first parameter account must be a string")
|
|
|
|
}
|
|
|
|
balance, ok := r.Params[1].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter balance must be a number")
|
|
|
|
}
|
|
|
|
confirmed, ok := r.Params[2].(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("third parameter confirmed must be a boolean")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewAccountBalanceNtfn(account, balance, confirmed), nil
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
|
|
|
func (n *AccountBalanceNtfn) Id() interface{} {
|
|
|
|
return nil
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 15:44:11 +01:00
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *AccountBalanceNtfn) SetId(id interface{}) {}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *AccountBalanceNtfn) Method() string {
|
|
|
|
return AccountBalanceNtfnMethod
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
func (n *AccountBalanceNtfn) MarshalJSON() ([]byte, error) {
|
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.Account,
|
|
|
|
n.Balance,
|
|
|
|
n.Confirmed,
|
|
|
|
},
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
return json.Marshal(ntfn)
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
|
|
|
func (n *AccountBalanceNtfn) UnmarshalJSON(b []byte) error {
|
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newNtfn, err := parseAccountBalanceNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
concreteNtfn, ok := newNtfn.(*AccountBalanceNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockConnectedNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of blockconnected JSON websocket notifications.
|
|
|
|
type BlockConnectedNtfn struct {
|
2013-12-13 16:58:30 +01:00
|
|
|
Hash string
|
|
|
|
Height int32
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Enforce that BlockConnectedNtfn satisfies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &BlockConnectedNtfn{}
|
2013-11-08 18:41:04 +01:00
|
|
|
|
|
|
|
// NewBlockConnectedNtfn creates a new BlockConnectedNtfn.
|
|
|
|
func NewBlockConnectedNtfn(hash string, height int32) *BlockConnectedNtfn {
|
|
|
|
return &BlockConnectedNtfn{
|
|
|
|
Hash: hash,
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// parseBlockConnectedNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseBlockConnectedNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 2 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, ok := r.Params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first parameter hash must be a string")
|
|
|
|
}
|
|
|
|
fheight, ok := r.Params[1].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter height must be a number")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewBlockConnectedNtfn(hash, int32(fheight)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2013-11-08 18:41:04 +01:00
|
|
|
func (n *BlockConnectedNtfn) Id() interface{} {
|
2013-12-13 16:58:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-08 15:44:11 +01:00
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *BlockConnectedNtfn) SetId(id interface{}) {}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *BlockConnectedNtfn) Method() string {
|
|
|
|
return BlockConnectedNtfnMethod
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
2013-11-08 18:41:04 +01:00
|
|
|
// interface.
|
|
|
|
func (n *BlockConnectedNtfn) MarshalJSON() ([]byte, error) {
|
2013-12-13 16:58:30 +01:00
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.Hash,
|
|
|
|
n.Height,
|
|
|
|
},
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
return json.Marshal(ntfn)
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
2013-12-13 16:58:30 +01:00
|
|
|
// the btcjson.Cmd interface.
|
2013-11-08 18:41:04 +01:00
|
|
|
func (n *BlockConnectedNtfn) UnmarshalJSON(b []byte) error {
|
2013-12-13 16:58:30 +01:00
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
2013-11-08 18:41:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
newNtfn, err := parseBlockConnectedNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
concreteNtfn, ok := newNtfn.(*BlockConnectedNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
2013-11-08 18:41:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockDisconnectedNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of blockdisconnected JSON websocket notifications.
|
|
|
|
type BlockDisconnectedNtfn struct {
|
2013-12-13 16:58:30 +01:00
|
|
|
Hash string
|
|
|
|
Height int32
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Enforce that BlockDisconnectedNtfn satisfies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &BlockDisconnectedNtfn{}
|
2013-11-08 18:41:04 +01:00
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// NewBlockDisconnectedNtfn creates a new BlockDisconnectedNtfn.
|
2013-11-08 18:41:04 +01:00
|
|
|
func NewBlockDisconnectedNtfn(hash string, height int32) *BlockDisconnectedNtfn {
|
|
|
|
return &BlockDisconnectedNtfn{
|
|
|
|
Hash: hash,
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// parseBlockDisconnectedNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseBlockDisconnectedNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 2 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, ok := r.Params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first parameter hash must be a string")
|
|
|
|
}
|
|
|
|
fheight, ok := r.Params[1].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter height must be a number")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewBlockDisconnectedNtfn(hash, int32(fheight)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2013-11-08 18:41:04 +01:00
|
|
|
func (n *BlockDisconnectedNtfn) Id() interface{} {
|
2013-12-13 16:58:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-08 15:44:11 +01:00
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *BlockDisconnectedNtfn) SetId(id interface{}) {}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *BlockDisconnectedNtfn) Method() string {
|
|
|
|
return BlockDisconnectedNtfnMethod
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
2013-11-08 18:41:04 +01:00
|
|
|
// interface.
|
|
|
|
func (n *BlockDisconnectedNtfn) MarshalJSON() ([]byte, error) {
|
2013-12-13 16:58:30 +01:00
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.Hash,
|
|
|
|
n.Height,
|
|
|
|
},
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
return json.Marshal(ntfn)
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
2013-12-13 16:58:30 +01:00
|
|
|
// the btcjson.Cmd interface.
|
2013-11-08 18:41:04 +01:00
|
|
|
func (n *BlockDisconnectedNtfn) UnmarshalJSON(b []byte) error {
|
2013-12-13 16:58:30 +01:00
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
|
|
|
|
newNtfn, err := parseBlockDisconnectedNtfn(&r)
|
|
|
|
if err != nil {
|
2013-11-08 18:41:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
concreteNtfn, ok := newNtfn.(*BlockDisconnectedNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-08 18:41:04 +01:00
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// BtcdConnectedNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of btcdconnected JSON websocket notifications.
|
|
|
|
type BtcdConnectedNtfn struct {
|
|
|
|
Connected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce that BtcdConnectedNtfn satisifies the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
var _ btcjson.Cmd = &BtcdConnectedNtfn{}
|
|
|
|
|
|
|
|
// NewBtcdConnectedNtfn creates a new BtcdConnectedNtfn.
|
|
|
|
func NewBtcdConnectedNtfn(connected bool) *BtcdConnectedNtfn {
|
|
|
|
return &BtcdConnectedNtfn{connected}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseBtcdConnectedNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseBtcdConnectedNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 1 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
connected, ok := r.Params[0].(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first parameter connected is not a boolean")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewBtcdConnectedNtfn(connected), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
|
|
|
func (n *BtcdConnectedNtfn) Id() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-08 15:44:11 +01:00
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *BtcdConnectedNtfn) SetId(id interface{}) {}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *BtcdConnectedNtfn) Method() string {
|
|
|
|
return BtcdConnectedNtfnMethod
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
func (n *BtcdConnectedNtfn) MarshalJSON() ([]byte, error) {
|
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.Connected,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return json.Marshal(ntfn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
|
|
|
func (n *BtcdConnectedNtfn) UnmarshalJSON(b []byte) error {
|
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newNtfn, err := parseTxMinedNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
concreteNtfn, ok := newNtfn.(*BtcdConnectedNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
2013-11-08 18:41:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:23:59 +01:00
|
|
|
// ProcessedTxNtfn is a type handling custom marshaling and unmarshaling
|
|
|
|
// of processedtx JSON websocket notifications.
|
|
|
|
type ProcessedTxNtfn struct {
|
2014-01-08 17:28:38 +01:00
|
|
|
Receiver string
|
|
|
|
Amount int64
|
|
|
|
TxID string
|
|
|
|
TxOutIndex uint32
|
|
|
|
PkScript string
|
|
|
|
BlockHash string
|
|
|
|
BlockHeight int32
|
|
|
|
BlockIndex int
|
|
|
|
BlockTime int64
|
|
|
|
Spent bool
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:23:59 +01:00
|
|
|
// Enforce that ProcessedTxNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &ProcessedTxNtfn{}
|
2014-01-08 17:28:38 +01:00
|
|
|
|
2014-01-08 21:23:59 +01:00
|
|
|
// parseProcessedTxNtfn parses a RawCmd into a concrete type satisifying
|
2014-01-08 17:28:38 +01:00
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
2014-01-08 21:23:59 +01:00
|
|
|
func parseProcessedTxNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
2014-01-08 17:28:38 +01:00
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 10 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver, ok := r.Params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first parameter receiver must be a string")
|
|
|
|
}
|
|
|
|
famount, ok := r.Params[1].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter amount must be a number")
|
|
|
|
}
|
|
|
|
amount := int64(famount)
|
|
|
|
txid, ok := r.Params[2].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("third parameter txid must be a string")
|
|
|
|
}
|
|
|
|
fTxOutIdx, ok := r.Params[3].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("fourth parameter txoutidx must be a number")
|
|
|
|
}
|
|
|
|
txOutIdx := uint32(fTxOutIdx)
|
|
|
|
pkScript, ok := r.Params[4].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("fifth parameter pkScript must be a string")
|
|
|
|
}
|
|
|
|
blockHash := r.Params[5].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("sixth parameter blockHash must be a string")
|
|
|
|
}
|
|
|
|
fBlockHeight, ok := r.Params[6].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("seventh parameter blockHeight must be a number")
|
|
|
|
}
|
|
|
|
blockHeight := int32(fBlockHeight)
|
|
|
|
fBlockIndex, ok := r.Params[7].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("eighth parameter blockIndex must be a number")
|
|
|
|
}
|
|
|
|
blockIndex := int(fBlockIndex)
|
|
|
|
fBlockTime, ok := r.Params[8].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("ninth parameter blockTime must be a number")
|
|
|
|
}
|
|
|
|
blockTime := int64(fBlockTime)
|
|
|
|
spent, ok := r.Params[9].(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("tenth parameter spent must be a bool")
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:23:59 +01:00
|
|
|
cmd := &ProcessedTxNtfn{
|
2014-01-08 17:28:38 +01:00
|
|
|
Receiver: receiver,
|
|
|
|
Amount: amount,
|
|
|
|
TxID: txid,
|
|
|
|
TxOutIndex: txOutIdx,
|
|
|
|
PkScript: pkScript,
|
|
|
|
BlockHash: blockHash,
|
|
|
|
BlockHeight: blockHeight,
|
|
|
|
BlockIndex: blockIndex,
|
|
|
|
BlockTime: blockTime,
|
|
|
|
Spent: spent,
|
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2014-01-08 21:23:59 +01:00
|
|
|
func (n *ProcessedTxNtfn) Id() interface{} {
|
2014-01-08 17:28:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
2014-01-08 21:23:59 +01:00
|
|
|
func (n *ProcessedTxNtfn) SetId(id interface{}) {}
|
2014-01-08 17:28:38 +01:00
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
2014-01-08 21:23:59 +01:00
|
|
|
func (n *ProcessedTxNtfn) Method() string {
|
|
|
|
return ProcessedTxNtfnMethod
|
2014-01-08 17:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
2014-01-08 21:23:59 +01:00
|
|
|
func (n *ProcessedTxNtfn) MarshalJSON() ([]byte, error) {
|
2014-01-08 17:28:38 +01:00
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.Receiver,
|
|
|
|
n.Amount,
|
|
|
|
n.TxID,
|
|
|
|
n.TxOutIndex,
|
|
|
|
n.PkScript,
|
|
|
|
n.BlockHash,
|
|
|
|
n.BlockHeight,
|
|
|
|
n.BlockIndex,
|
|
|
|
n.BlockTime,
|
|
|
|
n.Spent,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return json.Marshal(ntfn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
2014-01-08 21:23:59 +01:00
|
|
|
func (n *ProcessedTxNtfn) UnmarshalJSON(b []byte) error {
|
2014-01-08 17:28:38 +01:00
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:23:59 +01:00
|
|
|
newNtfn, err := parseProcessedTxNtfn(&r)
|
2014-01-08 17:28:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:23:59 +01:00
|
|
|
concreteNtfn, ok := newNtfn.(*ProcessedTxNtfn)
|
2014-01-08 17:28:38 +01:00
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:41:04 +01:00
|
|
|
// TxMinedNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of txmined JSON websocket notifications.
|
|
|
|
type TxMinedNtfn struct {
|
2013-12-13 16:58:30 +01:00
|
|
|
TxID string
|
|
|
|
BlockHash string
|
|
|
|
BlockHeight int32
|
|
|
|
BlockTime int64
|
|
|
|
Index int
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Enforce that TxMinedNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &TxMinedNtfn{}
|
2013-11-08 18:41:04 +01:00
|
|
|
|
|
|
|
// NewTxMinedNtfn creates a new TxMinedNtfn.
|
2013-11-25 18:54:01 +01:00
|
|
|
func NewTxMinedNtfn(txid, blockhash string, blockheight int32,
|
|
|
|
blocktime int64, index int) *TxMinedNtfn {
|
|
|
|
|
|
|
|
return &TxMinedNtfn{
|
|
|
|
TxID: txid,
|
|
|
|
BlockHash: blockhash,
|
|
|
|
BlockHeight: blockheight,
|
|
|
|
BlockTime: blocktime,
|
|
|
|
Index: index,
|
|
|
|
}
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// parseTxMinedNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseTxMinedNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 5 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
txid, ok := r.Params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first parameter txid must be a string")
|
|
|
|
}
|
|
|
|
blockhash, ok := r.Params[1].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter blockhash must be a string")
|
|
|
|
}
|
|
|
|
fblockheight, ok := r.Params[2].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("third parameter blockheight must be a number")
|
|
|
|
}
|
|
|
|
fblocktime, ok := r.Params[3].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("fourth parameter blocktime must be a number")
|
|
|
|
}
|
|
|
|
findex, ok := r.Params[4].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("fifth parameter index must be a number")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewTxMinedNtfn(txid, blockhash, int32(fblockheight),
|
|
|
|
int64(fblocktime), int(findex)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2013-11-08 18:41:04 +01:00
|
|
|
func (n *TxMinedNtfn) Id() interface{} {
|
2013-12-13 16:58:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-08 15:44:11 +01:00
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *TxMinedNtfn) SetId(id interface{}) {}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *TxMinedNtfn) Method() string {
|
|
|
|
return TxMinedNtfnMethod
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
2013-11-08 18:41:04 +01:00
|
|
|
// interface.
|
|
|
|
func (n *TxMinedNtfn) MarshalJSON() ([]byte, error) {
|
2013-12-13 16:58:30 +01:00
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.TxID,
|
|
|
|
n.BlockHash,
|
|
|
|
n.BlockHeight,
|
|
|
|
n.BlockTime,
|
|
|
|
n.Index,
|
|
|
|
},
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
return json.Marshal(ntfn)
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
2013-12-13 16:58:30 +01:00
|
|
|
// the btcjson.Cmd interface.
|
2013-11-08 18:41:04 +01:00
|
|
|
func (n *TxMinedNtfn) UnmarshalJSON(b []byte) error {
|
2013-12-13 16:58:30 +01:00
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
2013-11-08 18:41:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
newNtfn, err := parseTxMinedNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
concreteNtfn, ok := newNtfn.(*TxMinedNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
2013-11-08 18:41:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
2013-12-02 23:32:17 +01:00
|
|
|
|
|
|
|
// TxNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of newtx JSON websocket notifications.
|
|
|
|
type TxNtfn struct {
|
2013-12-13 16:58:30 +01:00
|
|
|
Account string
|
|
|
|
Details map[string]interface{}
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-01-09 03:29:19 +01:00
|
|
|
// TxSpentNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of txspent JSON websocket notifications.
|
|
|
|
type TxSpentNtfn struct {
|
|
|
|
SpentTxId string
|
|
|
|
SpentTxOutIndex int
|
|
|
|
SpendingTx string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce that TxSpentNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &TxSpentNtfn{}
|
|
|
|
|
|
|
|
// NewTxSpentNtfn creates a new TxSpentNtfn.
|
|
|
|
func NewTxSpentNtfn(txid string, txOutIndex int, spendingTx string) *TxSpentNtfn {
|
|
|
|
return &TxSpentNtfn{
|
|
|
|
SpentTxId: txid,
|
|
|
|
SpentTxOutIndex: txOutIndex,
|
|
|
|
SpendingTx: spendingTx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTxSpentNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseTxSpentNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 3 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
txid, ok := r.Params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first parameter txid must be a string")
|
|
|
|
}
|
|
|
|
findex, ok := r.Params[1].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter index must be a number")
|
|
|
|
}
|
|
|
|
index := int(findex)
|
|
|
|
spendingTx, ok := r.Params[2].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("third parameter spendingTx must be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewTxSpentNtfn(txid, index, spendingTx), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
|
|
|
func (n *TxSpentNtfn) Id() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *TxSpentNtfn) SetId(id interface{}) {}
|
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *TxSpentNtfn) Method() string {
|
|
|
|
return TxSpentNtfnMethod
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
func (n *TxSpentNtfn) MarshalJSON() ([]byte, error) {
|
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.SpentTxId,
|
|
|
|
n.SpentTxOutIndex,
|
|
|
|
n.SpendingTx,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return json.Marshal(ntfn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
|
|
|
func (n *TxSpentNtfn) UnmarshalJSON(b []byte) error {
|
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newNtfn, err := parseTxSpentNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
concreteNtfn, ok := newNtfn.(*TxSpentNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Enforce that TxNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &TxNtfn{}
|
2013-12-02 23:32:17 +01:00
|
|
|
|
|
|
|
// NewTxNtfn creates a new TxNtfn.
|
|
|
|
func NewTxNtfn(account string, details map[string]interface{}) *TxNtfn {
|
|
|
|
return &TxNtfn{
|
|
|
|
Account: account,
|
|
|
|
Details: details,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// parseTxNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseTxNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
details, ok := r.Params[1].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter details must be a JSON object")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewTxNtfn(account, details), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2013-12-02 23:32:17 +01:00
|
|
|
func (n *TxNtfn) Id() interface{} {
|
2013-12-13 16:58:30 +01:00
|
|
|
return nil
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 15:44:11 +01:00
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *TxNtfn) SetId(id interface{}) {}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *TxNtfn) Method() string {
|
|
|
|
return TxNtfnMethod
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
2013-12-02 23:32:17 +01:00
|
|
|
// interface.
|
|
|
|
func (n *TxNtfn) MarshalJSON() ([]byte, error) {
|
2013-12-13 16:58:30 +01:00
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.Account,
|
|
|
|
n.Details,
|
|
|
|
},
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
return json.Marshal(ntfn)
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
2013-12-13 16:58:30 +01:00
|
|
|
// the btcjson.Cmd interface.
|
2013-12-02 23:32:17 +01:00
|
|
|
func (n *TxNtfn) UnmarshalJSON(b []byte) error {
|
2013-12-13 16:58:30 +01:00
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newNtfn, err := parseTxNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
concreteNtfn, ok := newNtfn.(*TxNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalletLockStateNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of walletlockstate JSON websocket notifications.
|
|
|
|
type WalletLockStateNtfn struct {
|
|
|
|
Account string
|
|
|
|
Locked bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce that WalletLockStateNtfnMethod satisifies the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
var _ btcjson.Cmd = &WalletLockStateNtfn{}
|
|
|
|
|
|
|
|
// NewWalletLockStateNtfn creates a new WalletLockStateNtfn.
|
|
|
|
func NewWalletLockStateNtfn(account string,
|
|
|
|
locked bool) *WalletLockStateNtfn {
|
|
|
|
|
|
|
|
return &WalletLockStateNtfn{
|
|
|
|
Account: account,
|
|
|
|
Locked: locked,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWalletLockStateNtfn parses a RawCmd into a concrete type
|
|
|
|
// satisifying the btcjson.Cmd interface. This is used when registering
|
|
|
|
// the notification with the btcjson parser.
|
|
|
|
func parseWalletLockStateNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
locked, ok := r.Params[1].(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("second parameter locked must be a boolean")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewWalletLockStateNtfn(account, locked), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
|
|
|
func (n *WalletLockStateNtfn) Id() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-08 15:44:11 +01:00
|
|
|
// SetId is implemented to satisify the btcjson.Cmd interface. The
|
|
|
|
// notification id is not modified.
|
|
|
|
func (n *WalletLockStateNtfn) SetId(id interface{}) {}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *WalletLockStateNtfn) Method() string {
|
|
|
|
return WalletLockStateNtfnMethod
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
func (n *WalletLockStateNtfn) MarshalJSON() ([]byte, error) {
|
|
|
|
ntfn := btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Method: n.Method(),
|
|
|
|
Params: []interface{}{
|
|
|
|
n.Account,
|
|
|
|
n.Locked,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return json.Marshal(ntfn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
|
|
|
func (n *WalletLockStateNtfn) UnmarshalJSON(b []byte) error {
|
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
2013-12-02 23:32:17 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
newNtfn, err := parseWalletLockStateNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
concreteNtfn, ok := newNtfn.(*WalletLockStateNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
2013-12-02 23:32:17 +01:00
|
|
|
return nil
|
|
|
|
}
|