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"
|
2014-07-03 02:44:43 +02:00
|
|
|
|
2015-01-17 06:54:00 +01:00
|
|
|
"github.com/btcsuite/btcjson"
|
2013-11-08 18:41:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// TxAcceptedNtfnMethod is the method of the btcd txaccepted
|
2014-02-17 19:45:06 +01:00
|
|
|
// notification
|
2014-04-15 07:02:44 +02:00
|
|
|
TxAcceptedNtfnMethod = "txaccepted"
|
2014-02-17 19:45:06 +01:00
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// TxAcceptedVerboseNtfnMethod is the method of the btcd
|
|
|
|
// txacceptedverbose notifications.
|
|
|
|
TxAcceptedVerboseNtfnMethod = "txacceptedverbose"
|
2014-02-17 19:45:06 +01:00
|
|
|
|
2013-12-13 16:58:30 +01:00
|
|
|
// 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-02-17 19:45:06 +01:00
|
|
|
// RecvTxNtfnMethod is the method of the btcd recvtx notification.
|
|
|
|
RecvTxNtfnMethod = "recvtx"
|
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-02-17 19:45:06 +01:00
|
|
|
// RedeemingTxNtfnMethod is the method of the btcd redeemingtx
|
2014-01-09 03:29:19 +01:00
|
|
|
// notification.
|
2014-02-17 19:45:06 +01:00
|
|
|
RedeemingTxNtfnMethod = "redeemingtx"
|
2014-01-09 03:29:19 +01:00
|
|
|
|
2014-06-16 16:37:56 +02:00
|
|
|
// RescanFinishedNtfnMethod is the method of the btcd rescanfinished
|
|
|
|
// notification.
|
|
|
|
RescanFinishedNtfnMethod = "rescanfinished"
|
|
|
|
|
2014-03-24 19:04:55 +01:00
|
|
|
// RescanProgressNtfnMethod is the method of the btcd rescanprogress
|
|
|
|
// notification.
|
|
|
|
RescanProgressNtfnMethod = "rescanprogress"
|
|
|
|
|
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() {
|
2014-01-15 22:51:21 +01:00
|
|
|
btcjson.RegisterCustomCmd(AccountBalanceNtfnMethod,
|
2014-04-11 21:00:22 +02:00
|
|
|
parseAccountBalanceNtfn, nil, `TODO(jrick) fillmein`)
|
2014-01-15 22:51:21 +01:00
|
|
|
btcjson.RegisterCustomCmd(BlockConnectedNtfnMethod,
|
2014-04-11 21:00:22 +02:00
|
|
|
parseBlockConnectedNtfn, nil, `TODO(jrick) fillmein`)
|
2014-01-15 22:51:21 +01:00
|
|
|
btcjson.RegisterCustomCmd(BlockDisconnectedNtfnMethod,
|
2014-04-11 21:00:22 +02:00
|
|
|
parseBlockDisconnectedNtfn, nil, `TODO(jrick) fillmein`)
|
2014-01-15 22:51:21 +01:00
|
|
|
btcjson.RegisterCustomCmd(BtcdConnectedNtfnMethod,
|
2014-04-11 21:00:22 +02:00
|
|
|
parseBtcdConnectedNtfn, nil, `TODO(jrick) fillmein`)
|
2014-02-17 19:45:06 +01:00
|
|
|
btcjson.RegisterCustomCmd(RecvTxNtfnMethod,
|
2014-04-11 21:00:22 +02:00
|
|
|
parseRecvTxNtfn, nil, `TODO(jrick) fillmein`)
|
2014-06-16 16:37:56 +02:00
|
|
|
btcjson.RegisterCustomCmd(RescanFinishedNtfnMethod,
|
|
|
|
parseRescanFinishedNtfn, nil, `TODO(jrick) fillmein`)
|
2014-03-24 19:04:55 +01:00
|
|
|
btcjson.RegisterCustomCmd(RescanProgressNtfnMethod,
|
2014-04-11 21:00:22 +02:00
|
|
|
parseRescanProgressNtfn, nil, `TODO(jrick) fillmein`)
|
2014-02-17 19:45:06 +01:00
|
|
|
btcjson.RegisterCustomCmd(RedeemingTxNtfnMethod, parseRedeemingTxNtfn,
|
2014-04-11 21:00:22 +02:00
|
|
|
nil, `TODO(jrick) fillmein`)
|
|
|
|
btcjson.RegisterCustomCmd(TxNtfnMethod, parseTxNtfn, nil,
|
2014-01-15 22:51:21 +01:00
|
|
|
`TODO(jrick) fillmein`)
|
|
|
|
btcjson.RegisterCustomCmd(WalletLockStateNtfnMethod,
|
2014-04-11 21:00:22 +02:00
|
|
|
parseWalletLockStateNtfn, nil, `TODO(jrick) fillmein`)
|
2014-04-15 07:02:44 +02:00
|
|
|
btcjson.RegisterCustomCmd(TxAcceptedNtfnMethod, parseTxAcceptedNtfn, nil,
|
2014-04-11 21:00:22 +02:00
|
|
|
`TODO(flam) fillmein`)
|
2014-04-15 07:02:44 +02:00
|
|
|
btcjson.RegisterCustomCmd(TxAcceptedVerboseNtfnMethod,
|
|
|
|
parseTxAcceptedVerboseNtfn, nil, `TODO(flam) fillmein`)
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2013-11-08 18:41:04 +01:00
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// BlockDetails describes details of a tx in a block.
|
|
|
|
type BlockDetails struct {
|
2014-04-10 21:33:21 +02:00
|
|
|
Height int32 `json:"height"`
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
Index int `json:"index"`
|
|
|
|
Time int64 `json:"time"`
|
2014-02-17 19:45:06 +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
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var account string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &account); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'account' must be a string: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
var balance float64
|
|
|
|
if err := json.Unmarshal(r.Params[1], &balance); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'balance' must be a number: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
var confirmed bool
|
|
|
|
if err := json.Unmarshal(r.Params[2], &confirmed); err != nil {
|
|
|
|
return nil, errors.New("third parameter 'confirmed' must be a bool: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.Account,
|
|
|
|
n.Balance,
|
|
|
|
n.Confirmed,
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(raw)
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var hash string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &hash); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'hash' must be a string: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
var height int32
|
|
|
|
if err := json.Unmarshal(r.Params[1], &height); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'height' must be a 32-bit integer: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
return NewBlockConnectedNtfn(hash, height), nil
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.Hash,
|
|
|
|
n.Height,
|
|
|
|
}
|
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
return json.Marshal(raw)
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var hash string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &hash); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'hash' must be a string: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
var height int32
|
|
|
|
if err := json.Unmarshal(r.Params[1], &height); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'height' must be a 32-bit integer: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
return NewBlockDisconnectedNtfn(hash, height), nil
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.Hash,
|
|
|
|
n.Height,
|
|
|
|
}
|
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
return json.Marshal(raw)
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var connected bool
|
|
|
|
if err := json.Unmarshal(r.Params[0], &connected); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'connected' must be a bool: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NewBtcdConnectedNtfn(connected), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
|
|
|
func (n *BtcdConnectedNtfn) Id() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.Connected,
|
|
|
|
}
|
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
return json.Marshal(raw)
|
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 *BtcdConnectedNtfn) UnmarshalJSON(b []byte) error {
|
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
newNtfn, err := parseBtcdConnectedNtfn(&r)
|
2013-12-13 16:58:30 +01:00
|
|
|
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-02-17 19:45:06 +01:00
|
|
|
// RecvTxNtfn is a type handling custom marshaling and unmarshaling
|
|
|
|
// of recvtx JSON websocket notifications.
|
|
|
|
type RecvTxNtfn struct {
|
|
|
|
HexTx string
|
|
|
|
Block *BlockDetails
|
2014-01-08 17:28:38 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// Enforce that RecvTxNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &RecvTxNtfn{}
|
2014-01-08 17:28:38 +01:00
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// NewRecvTxNtfn creates a new RecvTxNtfn.
|
|
|
|
func NewRecvTxNtfn(hextx string, block *BlockDetails) *RecvTxNtfn {
|
|
|
|
return &RecvTxNtfn{
|
|
|
|
HexTx: hextx,
|
|
|
|
Block: block,
|
2014-01-08 17:28:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// parsRecvTxNtfn parses a RawCmd into a concrete type satisifying the
|
|
|
|
// btcjson.Cmd interface. This is used when registering the notification with
|
|
|
|
// the btcjson parser.
|
|
|
|
func parseRecvTxNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
2014-01-08 17:28:38 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// Must have one or two parameters.
|
|
|
|
if len(r.Params) == 0 || len(r.Params) > 2 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
2014-01-08 17:28:38 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var hextx string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &hextx); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'hextx' must be a " +
|
|
|
|
"string: " + err.Error())
|
2014-01-08 17:28:38 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var blockDetails *BlockDetails
|
2014-02-17 19:45:06 +01:00
|
|
|
if len(r.Params) > 1 {
|
2014-04-10 21:33:21 +02:00
|
|
|
if err := json.Unmarshal(r.Params[1], &blockDetails); err != nil {
|
|
|
|
return nil, errors.New("second optional parameter " +
|
|
|
|
"'details' must be a JSON oject of block " +
|
|
|
|
"details: " + err.Error())
|
2014-02-17 19:45:06 +01:00
|
|
|
}
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
return NewRecvTxNtfn(hextx, blockDetails), nil
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RecvTxNtfn) Id() interface{} {
|
2013-12-13 16:58:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RecvTxNtfn) Method() string {
|
|
|
|
return RecvTxNtfnMethod
|
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.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RecvTxNtfn) MarshalJSON() ([]byte, error) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := make([]interface{}, 1, 2)
|
|
|
|
params[0] = n.HexTx
|
2014-02-17 19:45:06 +01:00
|
|
|
if n.Block != nil {
|
2014-04-10 21:33:21 +02:00
|
|
|
params = append(params, n.Block)
|
2014-02-17 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
return json.Marshal(raw)
|
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.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RecvTxNtfn) 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
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
newNtfn, err := parseRecvTxNtfn(&r)
|
2013-12-13 16:58:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-11-08 18:41:04 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
concreteNtfn, ok := newNtfn.(*RecvTxNtfn)
|
2013-12-13 16:58:30 +01:00
|
|
|
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
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// RedeemingTxNtfn is a type handling custom marshaling and unmarshaling
|
|
|
|
// of redeemingtx JSON websocket notifications.
|
|
|
|
type RedeemingTxNtfn struct {
|
|
|
|
HexTx string
|
|
|
|
Block *BlockDetails
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// Enforce that RedeemingTxNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &RedeemingTxNtfn{}
|
2014-01-09 03:29:19 +01:00
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// NewRedeemingTxNtfn creates a new RedeemingTxNtfn.
|
|
|
|
func NewRedeemingTxNtfn(hextx string, block *BlockDetails) *RedeemingTxNtfn {
|
|
|
|
return &RedeemingTxNtfn{
|
|
|
|
HexTx: hextx,
|
|
|
|
Block: block,
|
2014-01-09 03:29:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// parseRedeemingTxNtfn parses a RawCmd into a concrete type satisifying the
|
|
|
|
// btcjson.Cmd interface. This is used when registering the notification with
|
|
|
|
// the btcjson parser.
|
|
|
|
func parseRedeemingTxNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
2014-01-09 03:29:19 +01:00
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// Must have one or two parameters.
|
|
|
|
if len(r.Params) == 0 || len(r.Params) > 2 {
|
2014-01-09 03:29:19 +01:00
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var hextx string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &hextx); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'hextx' must be a " +
|
|
|
|
"string: " + err.Error())
|
2014-01-09 03:29:19 +01:00
|
|
|
}
|
2014-02-17 19:45:06 +01:00
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var blockDetails *BlockDetails
|
2014-02-17 19:45:06 +01:00
|
|
|
if len(r.Params) > 1 {
|
2014-04-10 21:33:21 +02:00
|
|
|
if err := json.Unmarshal(r.Params[1], &blockDetails); err != nil {
|
|
|
|
return nil, errors.New("second optional parameter " +
|
|
|
|
"'details' must be a JSON oject of block " +
|
|
|
|
"details: " + err.Error())
|
2014-02-17 19:45:06 +01:00
|
|
|
}
|
2014-01-09 03:29:19 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
return NewRedeemingTxNtfn(hextx, blockDetails), nil
|
2014-01-09 03:29:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RedeemingTxNtfn) Id() interface{} {
|
2014-01-09 03:29:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RedeemingTxNtfn) Method() string {
|
|
|
|
return RedeemingTxNtfnMethod
|
2014-01-09 03:29:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RedeemingTxNtfn) MarshalJSON() ([]byte, error) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := make([]interface{}, 1, 2)
|
|
|
|
params[0] = n.HexTx
|
2014-02-17 19:45:06 +01:00
|
|
|
if n.Block != nil {
|
2014-04-10 21:33:21 +02:00
|
|
|
params = append(params, n.Block)
|
2014-02-17 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-01-09 03:29:19 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
return json.Marshal(raw)
|
2014-01-09 03:29:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
2014-02-17 19:45:06 +01:00
|
|
|
func (n *RedeemingTxNtfn) UnmarshalJSON(b []byte) error {
|
2014-01-09 03:29:19 +01:00
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
newNtfn, err := parseRedeemingTxNtfn(&r)
|
2014-01-09 03:29:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
concreteNtfn, ok := newNtfn.(*RedeemingTxNtfn)
|
2014-03-24 19:04:55 +01:00
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-16 16:37:56 +02:00
|
|
|
// RescanFinishedNtfn is type handling custom marshaling and
|
|
|
|
// unmarshaling of rescanfinished JSON websocket notifications.
|
|
|
|
type RescanFinishedNtfn struct {
|
2014-07-17 03:35:11 +02:00
|
|
|
Hash string
|
|
|
|
Height int32
|
|
|
|
Time int64
|
2014-06-16 16:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce that RescanFinishedNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &RescanFinishedNtfn{}
|
|
|
|
|
|
|
|
// NewRescanFinishedNtfn creates a new RescanFinshedNtfn.
|
2014-07-17 03:35:11 +02:00
|
|
|
func NewRescanFinishedNtfn(hash string, height int32, time int64) *RescanFinishedNtfn {
|
|
|
|
return &RescanFinishedNtfn{hash, height, time}
|
2014-06-16 16:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseRescanFinishedNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseRescanFinishedNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:35:11 +02:00
|
|
|
if len(r.Params) != 3 {
|
2014-06-16 16:37:56 +02:00
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:35:11 +02:00
|
|
|
var hash string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &hash); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'hash' must be a " +
|
|
|
|
"string: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var height int32
|
|
|
|
if err := json.Unmarshal(r.Params[1], &height); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'height' must be a " +
|
2014-06-16 16:37:56 +02:00
|
|
|
"32-bit integer: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:35:11 +02:00
|
|
|
var time int64
|
|
|
|
if err := json.Unmarshal(r.Params[2], &time); err != nil {
|
|
|
|
return nil, errors.New("third parameter 'time' must be a " +
|
|
|
|
"64-bit integer: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewRescanFinishedNtfn(hash, height, time), nil
|
2014-06-16 16:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
|
|
|
func (n *RescanFinishedNtfn) Id() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *RescanFinishedNtfn) Method() string {
|
|
|
|
return RescanFinishedNtfnMethod
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
func (n *RescanFinishedNtfn) MarshalJSON() ([]byte, error) {
|
|
|
|
params := []interface{}{
|
2014-07-17 03:35:11 +02:00
|
|
|
n.Hash,
|
|
|
|
n.Height,
|
|
|
|
n.Time,
|
2014-06-16 16:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
|
|
|
func (n *RescanFinishedNtfn) UnmarshalJSON(b []byte) error {
|
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newNtfn, err := parseRescanFinishedNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
concreteNtfn, ok := newNtfn.(*RescanFinishedNtfn)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-24 19:04:55 +01:00
|
|
|
// RescanProgressNtfn is type handling custom marshaling and
|
|
|
|
// unmarshaling of rescanprogress JSON websocket notifications.
|
|
|
|
type RescanProgressNtfn struct {
|
2014-07-17 03:35:11 +02:00
|
|
|
Hash string
|
|
|
|
Height int32
|
|
|
|
Time int64
|
2014-03-24 19:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce that RescanProgressNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &RescanProgressNtfn{}
|
|
|
|
|
|
|
|
// NewRescanProgressNtfn creates a new RescanProgressNtfn.
|
2014-07-17 03:35:11 +02:00
|
|
|
func NewRescanProgressNtfn(hash string, height int32, time int64) *RescanProgressNtfn {
|
|
|
|
return &RescanProgressNtfn{hash, height, time}
|
2014-03-24 19:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseRescanProgressNtfn parses a RawCmd into a concrete type satisifying
|
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
|
|
|
func parseRescanProgressNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:35:11 +02:00
|
|
|
if len(r.Params) != 3 {
|
2014-03-24 19:04:55 +01:00
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:35:11 +02:00
|
|
|
var hash string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &hash); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'hash' must be a " +
|
|
|
|
"string: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var height int32
|
|
|
|
if err := json.Unmarshal(r.Params[1], &height); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'height' must be a " +
|
2014-04-10 21:33:21 +02:00
|
|
|
"32-bit integer: " + err.Error())
|
2014-03-24 19:04:55 +01:00
|
|
|
}
|
|
|
|
|
2014-07-17 03:35:11 +02:00
|
|
|
var time int64
|
|
|
|
if err := json.Unmarshal(r.Params[2], &time); err != nil {
|
|
|
|
return nil, errors.New("third parameter 'time' must be a " +
|
|
|
|
"64-bit integer: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewRescanProgressNtfn(hash, height, time), nil
|
2014-03-24 19:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
|
|
|
func (n *RescanProgressNtfn) Id() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
|
|
|
func (n *RescanProgressNtfn) Method() string {
|
|
|
|
return RescanProgressNtfnMethod
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
|
|
|
func (n *RescanProgressNtfn) MarshalJSON() ([]byte, error) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
2014-07-17 03:35:11 +02:00
|
|
|
n.Hash,
|
|
|
|
n.Height,
|
|
|
|
n.Time,
|
2014-03-24 19:04:55 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(raw)
|
2014-03-24 19:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
|
|
|
func (n *RescanProgressNtfn) UnmarshalJSON(b []byte) error {
|
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newNtfn, err := parseRescanProgressNtfn(&r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
concreteNtfn, ok := newNtfn.(*RescanProgressNtfn)
|
2014-01-09 03:29:19 +01:00
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:45:06 +01:00
|
|
|
// TxNtfn is a type handling custom marshaling and
|
|
|
|
// unmarshaling of newtx JSON websocket notifications.
|
|
|
|
type TxNtfn struct {
|
|
|
|
Account string
|
2014-04-09 04:30:55 +02:00
|
|
|
Details *btcjson.ListTransactionsResult
|
2014-02-17 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2014-04-09 04:30:55 +02:00
|
|
|
func NewTxNtfn(account string, details *btcjson.ListTransactionsResult) *TxNtfn {
|
2013-12-02 23:32:17 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var account string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &account); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'account' must be a " +
|
|
|
|
"string: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
2014-04-09 04:30:55 +02:00
|
|
|
var details btcjson.ListTransactionsResult
|
2014-04-10 21:33:21 +02:00
|
|
|
if err := json.Unmarshal(r.Params[1], &details); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'details' must be a " +
|
|
|
|
"JSON object of transaction details: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 04:30:55 +02:00
|
|
|
return NewTxNtfn(account, &details), nil
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
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) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.Account,
|
|
|
|
n.Details,
|
2013-12-02 23:32:17 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(raw)
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var account string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &account); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'account' must be a " +
|
|
|
|
"string: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
var locked bool
|
|
|
|
if err := json.Unmarshal(r.Params[1], &locked); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'locked' must be a " +
|
|
|
|
"bool: " + err.Error())
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.Account,
|
|
|
|
n.Locked,
|
2013-12-13 16:58:30 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(raw)
|
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 *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
|
|
|
|
}
|
2014-02-08 23:03:22 +01:00
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// TxAcceptedNtfn is a type handling custom marshaling and
|
2014-02-08 23:03:22 +01:00
|
|
|
// unmarshaling of txmined JSON websocket notifications.
|
2014-04-15 07:02:44 +02:00
|
|
|
type TxAcceptedNtfn struct {
|
2014-02-09 19:43:02 +01:00
|
|
|
TxID string `json:"txid"`
|
|
|
|
Amount int64 `json:"amount"`
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// Enforce that TxAcceptedNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &TxAcceptedNtfn{}
|
2014-02-08 23:03:22 +01:00
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// NewTxAcceptedNtfn creates a new TxAcceptedNtfn.
|
|
|
|
func NewTxAcceptedNtfn(txid string, amount int64) *TxAcceptedNtfn {
|
|
|
|
return &TxAcceptedNtfn{
|
2014-02-08 23:03:22 +01:00
|
|
|
TxID: txid,
|
|
|
|
Amount: amount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// parseTxAcceptedNtfn parses a RawCmd into a concrete type satisifying
|
2014-02-08 23:03:22 +01:00
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
2014-04-15 07:02:44 +02:00
|
|
|
func parseTxAcceptedNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
2014-02-08 23:03:22 +01:00
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 2 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:33:21 +02:00
|
|
|
var txid string
|
|
|
|
if err := json.Unmarshal(r.Params[0], &txid); err != nil {
|
|
|
|
return nil, errors.New("first parameter 'txid' must be a " +
|
|
|
|
"string: " + err.Error())
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
var amount int64
|
|
|
|
if err := json.Unmarshal(r.Params[1], &amount); err != nil {
|
|
|
|
return nil, errors.New("second parameter 'amount' must be an " +
|
|
|
|
"integer: " + err.Error())
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
return NewTxAcceptedNtfn(txid, amount), nil
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedNtfn) Id() interface{} {
|
2014-02-08 23:03:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedNtfn) Method() string {
|
|
|
|
return TxAcceptedNtfnMethod
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedNtfn) MarshalJSON() ([]byte, error) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.TxID,
|
|
|
|
n.Amount,
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(raw)
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedNtfn) UnmarshalJSON(b []byte) error {
|
2014-02-08 23:03:22 +01:00
|
|
|
// Unmarshal into a RawCmd.
|
|
|
|
var r btcjson.RawCmd
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
newNtfn, err := parseTxAcceptedNtfn(&r)
|
2014-02-08 23:03:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
concreteNtfn, ok := newNtfn.(*TxAcceptedNtfn)
|
2014-02-08 23:03:22 +01:00
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// TxAcceptedVerboseNtfn is a type handling custom marshaling and
|
2014-02-08 23:03:22 +01:00
|
|
|
// unmarshaling of txmined JSON websocket notifications.
|
2014-04-15 07:02:44 +02:00
|
|
|
type TxAcceptedVerboseNtfn struct {
|
2014-02-09 19:43:02 +01:00
|
|
|
RawTx *btcjson.TxRawResult `json:"rawtx"`
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// Enforce that TxAcceptedNtfn satisifies the btcjson.Cmd interface.
|
|
|
|
var _ btcjson.Cmd = &TxAcceptedVerboseNtfn{}
|
2014-02-08 23:03:22 +01:00
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// NewTxAcceptedVerboseNtfn creates a new TxAcceptedVerboseNtfn.
|
|
|
|
func NewTxAcceptedVerboseNtfn(rawTx *btcjson.TxRawResult) *TxAcceptedVerboseNtfn {
|
|
|
|
return &TxAcceptedVerboseNtfn{
|
2014-02-08 23:03:22 +01:00
|
|
|
RawTx: rawTx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
// parseTxAcceptedVerboseNtfn parses a RawCmd into a concrete type satisifying
|
2014-04-11 16:16:58 +02:00
|
|
|
// the btcjson.Cmd interface. This is used when registering the notification
|
|
|
|
// with the btcjson parser.
|
2014-04-15 07:02:44 +02:00
|
|
|
func parseTxAcceptedVerboseNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
|
2014-04-11 16:16:58 +02:00
|
|
|
if r.Id != nil {
|
|
|
|
return nil, ErrNotANtfn
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Params) != 1 {
|
|
|
|
return nil, btcjson.ErrWrongNumberOfParams
|
|
|
|
}
|
|
|
|
|
|
|
|
var rawTx *btcjson.TxRawResult
|
|
|
|
if err := json.Unmarshal(r.Params[0], &rawTx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
return NewTxAcceptedVerboseNtfn(rawTx), nil
|
2014-04-11 16:16:58 +02:00
|
|
|
}
|
|
|
|
|
2014-02-08 23:03:22 +01:00
|
|
|
// Id satisifies the btcjson.Cmd interface by returning nil for a
|
|
|
|
// notification ID.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedVerboseNtfn) Id() interface{} {
|
2014-02-08 23:03:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method satisifies the btcjson.Cmd interface by returning the method
|
|
|
|
// of the notification.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedVerboseNtfn) Method() string {
|
|
|
|
return TxAcceptedVerboseNtfnMethod
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of n. Part of the btcjson.Cmd
|
|
|
|
// interface.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedVerboseNtfn) MarshalJSON() ([]byte, error) {
|
2014-04-10 21:33:21 +02:00
|
|
|
params := []interface{}{
|
|
|
|
n.RawTx,
|
|
|
|
}
|
|
|
|
|
|
|
|
// No ID for notifications.
|
|
|
|
raw, err := btcjson.NewRawCmd(nil, n.Method(), params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
2014-04-10 21:33:21 +02:00
|
|
|
return json.Marshal(raw)
|
2014-02-08 23:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the JSON encoding of n into n. Part of
|
|
|
|
// the btcjson.Cmd interface.
|
2014-04-15 07:02:44 +02:00
|
|
|
func (n *TxAcceptedVerboseNtfn) UnmarshalJSON(b []byte) error {
|
2014-04-10 21:33:21 +02:00
|
|
|
var r btcjson.RawCmd
|
2014-02-08 23:03:22 +01:00
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
newNtfn, err := parseTxAcceptedVerboseNtfn(&r)
|
2014-04-11 16:16:58 +02:00
|
|
|
if err != nil {
|
2014-02-08 23:03:22 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:02:44 +02:00
|
|
|
concreteNtfn, ok := newNtfn.(*TxAcceptedVerboseNtfn)
|
2014-04-11 16:16:58 +02:00
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
*n = *concreteNtfn
|
2014-02-08 23:03:22 +01:00
|
|
|
return nil
|
|
|
|
}
|