2017-01-25 03:39:03 +01:00
|
|
|
// Copyright (c) 2014-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2017 The Decred developers
|
2014-05-07 18:14:39 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-07-22 04:53:54 +02:00
|
|
|
package rpcclient
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2014-06-10 02:49:41 +02:00
|
|
|
"encoding/json"
|
2014-05-07 18:14:39 +02:00
|
|
|
"errors"
|
2014-06-10 02:49:41 +02:00
|
|
|
"fmt"
|
2014-07-17 20:37:54 +02:00
|
|
|
"time"
|
2014-07-03 02:33:54 +02:00
|
|
|
|
2018-05-15 05:44:11 +02:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-15 21:11:47 +02:00
|
|
|
// ErrWebsocketsRequired is an error to describe the condition where the
|
|
|
|
// caller is trying to use a websocket-only feature, such as requesting
|
|
|
|
// notifications or other websocket requests when the client is
|
|
|
|
// configured to run in HTTP POST mode.
|
|
|
|
ErrWebsocketsRequired = errors.New("a websocket connection is required " +
|
|
|
|
"to use this feature")
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
2017-10-29 20:56:03 +01:00
|
|
|
// notificationState is used to track the current state of successfully
|
2014-05-10 06:44:43 +02:00
|
|
|
// registered notification so the state can be automatically re-established on
|
|
|
|
// reconnect.
|
|
|
|
type notificationState struct {
|
|
|
|
notifyBlocks bool
|
|
|
|
notifyNewTx bool
|
|
|
|
notifyNewTxVerbose bool
|
|
|
|
notifyReceived map[string]struct{}
|
2015-01-01 23:34:07 +01:00
|
|
|
notifySpent map[btcjson.OutPoint]struct{}
|
2014-05-10 06:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a deep copy of the receiver.
|
|
|
|
func (s *notificationState) Copy() *notificationState {
|
2016-08-17 19:39:34 +02:00
|
|
|
var stateCopy notificationState
|
|
|
|
stateCopy.notifyBlocks = s.notifyBlocks
|
|
|
|
stateCopy.notifyNewTx = s.notifyNewTx
|
|
|
|
stateCopy.notifyNewTxVerbose = s.notifyNewTxVerbose
|
2014-05-10 06:44:43 +02:00
|
|
|
stateCopy.notifyReceived = make(map[string]struct{})
|
|
|
|
for addr := range s.notifyReceived {
|
|
|
|
stateCopy.notifyReceived[addr] = struct{}{}
|
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
stateCopy.notifySpent = make(map[btcjson.OutPoint]struct{})
|
2014-05-10 06:44:43 +02:00
|
|
|
for op := range s.notifySpent {
|
|
|
|
stateCopy.notifySpent[op] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stateCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
// newNotificationState returns a new notification state ready to be populated.
|
|
|
|
func newNotificationState() *notificationState {
|
|
|
|
return ¬ificationState{
|
|
|
|
notifyReceived: make(map[string]struct{}),
|
2015-01-01 23:34:07 +01:00
|
|
|
notifySpent: make(map[btcjson.OutPoint]struct{}),
|
2014-05-10 06:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// newNilFutureResult returns a new future result channel that already has the
|
|
|
|
// result waiting on the channel with the reply set to nil. This is useful
|
|
|
|
// to ignore things such as notifications when the caller didn't specify any
|
|
|
|
// notification handlers.
|
2014-06-10 02:49:41 +02:00
|
|
|
func newNilFutureResult() chan *response {
|
|
|
|
responseChan := make(chan *response, 1)
|
|
|
|
responseChan <- &response{result: nil, err: nil}
|
2014-05-07 18:14:39 +02:00
|
|
|
return responseChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotificationHandlers defines callback function pointers to invoke with
|
|
|
|
// notifications. Since all of the functions are nil by default, all
|
|
|
|
// notifications are effectively ignored until their handlers are set to a
|
|
|
|
// concrete callback.
|
|
|
|
//
|
2014-06-04 21:58:31 +02:00
|
|
|
// NOTE: Unless otherwise documented, these handlers must NOT directly call any
|
|
|
|
// blocking calls on the client instance since the input reader goroutine blocks
|
|
|
|
// until the callback has completed. Doing so will result in a deadlock
|
|
|
|
// situation.
|
2014-05-07 18:14:39 +02:00
|
|
|
type NotificationHandlers struct {
|
2014-06-04 21:58:31 +02:00
|
|
|
// OnClientConnected is invoked when the client connects or reconnects
|
|
|
|
// to the RPC server. This callback is run async with the rest of the
|
|
|
|
// notification handlers, and is safe for blocking client requests.
|
|
|
|
OnClientConnected func()
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnBlockConnected is invoked when a block is connected to the longest
|
|
|
|
// (best) chain. It will only be invoked if a preceding call to
|
|
|
|
// NotifyBlocks has been made to register for the notification and the
|
|
|
|
// function is non-nil.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use OnFilteredBlockConnected instead.
|
2016-08-08 21:16:04 +02:00
|
|
|
OnBlockConnected func(hash *chainhash.Hash, height int32, t time.Time)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2017-01-25 03:39:03 +01:00
|
|
|
// OnFilteredBlockConnected is invoked when a block is connected to the
|
|
|
|
// longest (best) chain. It will only be invoked if a preceding call to
|
|
|
|
// NotifyBlocks has been made to register for the notification and the
|
|
|
|
// function is non-nil. Its parameters differ from OnBlockConnected: it
|
|
|
|
// receives the block's height, header, and relevant transactions.
|
|
|
|
OnFilteredBlockConnected func(height int32, header *wire.BlockHeader,
|
|
|
|
txs []*btcutil.Tx)
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnBlockDisconnected is invoked when a block is disconnected from the
|
|
|
|
// longest (best) chain. It will only be invoked if a preceding call to
|
|
|
|
// NotifyBlocks has been made to register for the notification and the
|
|
|
|
// function is non-nil.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use OnFilteredBlockDisconnected instead.
|
2016-08-08 21:16:04 +02:00
|
|
|
OnBlockDisconnected func(hash *chainhash.Hash, height int32, t time.Time)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2017-01-25 03:39:03 +01:00
|
|
|
// OnFilteredBlockDisconnected is invoked when a block is disconnected
|
|
|
|
// from the longest (best) chain. It will only be invoked if a
|
|
|
|
// preceding NotifyBlocks has been made to register for the notification
|
|
|
|
// and the call to function is non-nil. Its parameters differ from
|
|
|
|
// OnBlockDisconnected: it receives the block's height and header.
|
|
|
|
OnFilteredBlockDisconnected func(height int32, header *wire.BlockHeader)
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnRecvTx is invoked when a transaction that receives funds to a
|
|
|
|
// registered address is received into the memory pool and also
|
|
|
|
// connected to the longest (best) chain. It will only be invoked if a
|
|
|
|
// preceding call to NotifyReceived, Rescan, or RescanEndHeight has been
|
|
|
|
// made to register for the notification and the function is non-nil.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use OnRelevantTxAccepted instead.
|
2015-01-01 23:34:07 +01:00
|
|
|
OnRecvTx func(transaction *btcutil.Tx, details *btcjson.BlockDetails)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnRedeemingTx is invoked when a transaction that spends a registered
|
|
|
|
// outpoint is received into the memory pool and also connected to the
|
|
|
|
// longest (best) chain. It will only be invoked if a preceding call to
|
|
|
|
// NotifySpent, Rescan, or RescanEndHeight has been made to register for
|
|
|
|
// the notification and the function is non-nil.
|
|
|
|
//
|
|
|
|
// NOTE: The NotifyReceived will automatically register notifications
|
|
|
|
// for the outpoints that are now "owned" as a result of receiving
|
|
|
|
// funds to the registered addresses. This means it is possible for
|
|
|
|
// this to invoked indirectly as the result of a NotifyReceived call.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use OnRelevantTxAccepted instead.
|
2015-01-01 23:34:07 +01:00
|
|
|
OnRedeemingTx func(transaction *btcutil.Tx, details *btcjson.BlockDetails)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2017-01-25 03:39:03 +01:00
|
|
|
// OnRelevantTxAccepted is invoked when an unmined transaction passes
|
|
|
|
// the client's transaction filter.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
OnRelevantTxAccepted func(transaction []byte)
|
|
|
|
|
2014-06-16 16:42:29 +02:00
|
|
|
// OnRescanFinished is invoked after a rescan finishes due to a previous
|
|
|
|
// call to Rescan or RescanEndHeight. Finished rescans should be
|
|
|
|
// signaled on this notification, rather than relying on the return
|
|
|
|
// result of a rescan request, due to how btcd may send various rescan
|
|
|
|
// notifications after the rescan request has already returned.
|
2017-01-25 03:44:20 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Not used with RescanBlocks.
|
2016-08-08 21:16:04 +02:00
|
|
|
OnRescanFinished func(hash *chainhash.Hash, height int32, blkTime time.Time)
|
2014-06-16 16:42:29 +02:00
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnRescanProgress is invoked periodically when a rescan is underway.
|
|
|
|
// It will only be invoked if a preceding call to Rescan or
|
|
|
|
// RescanEndHeight has been made and the function is non-nil.
|
2017-01-25 03:44:20 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Not used with RescanBlocks.
|
2016-08-08 21:16:04 +02:00
|
|
|
OnRescanProgress func(hash *chainhash.Hash, height int32, blkTime time.Time)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnTxAccepted is invoked when a transaction is accepted into the
|
|
|
|
// memory pool. It will only be invoked if a preceding call to
|
|
|
|
// NotifyNewTransactions with the verbose flag set to false has been
|
|
|
|
// made to register for the notification and the function is non-nil.
|
2016-08-08 21:16:04 +02:00
|
|
|
OnTxAccepted func(hash *chainhash.Hash, amount btcutil.Amount)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnTxAccepted is invoked when a transaction is accepted into the
|
|
|
|
// memory pool. It will only be invoked if a preceding call to
|
|
|
|
// NotifyNewTransactions with the verbose flag set to true has been
|
|
|
|
// made to register for the notification and the function is non-nil.
|
|
|
|
OnTxAcceptedVerbose func(txDetails *btcjson.TxRawResult)
|
|
|
|
|
|
|
|
// OnBtcdConnected is invoked when a wallet connects or disconnects from
|
|
|
|
// btcd.
|
|
|
|
//
|
|
|
|
// This will only be available when client is connected to a wallet
|
|
|
|
// server such as btcwallet.
|
|
|
|
OnBtcdConnected func(connected bool)
|
|
|
|
|
|
|
|
// OnAccountBalance is invoked with account balance updates.
|
|
|
|
//
|
|
|
|
// This will only be available when speaking to a wallet server
|
|
|
|
// such as btcwallet.
|
|
|
|
OnAccountBalance func(account string, balance btcutil.Amount, confirmed bool)
|
|
|
|
|
|
|
|
// OnWalletLockState is invoked when a wallet is locked or unlocked.
|
|
|
|
//
|
|
|
|
// This will only be available when client is connected to a wallet
|
|
|
|
// server such as btcwallet.
|
|
|
|
OnWalletLockState func(locked bool)
|
|
|
|
|
|
|
|
// OnUnknownNotification is invoked when an unrecognized notification
|
|
|
|
// is received. This typically means the notification handling code
|
|
|
|
// for this package needs to be updated for a new notification type or
|
|
|
|
// the caller is using a custom notification this package does not know
|
|
|
|
// about.
|
2014-06-10 02:49:41 +02:00
|
|
|
OnUnknownNotification func(method string, params []json.RawMessage)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleNotification examines the passed notification type, performs
|
|
|
|
// conversions to get the raw notification types into higher level types and
|
|
|
|
// delivers the notification to the appropriate On<X> handler registered with
|
|
|
|
// the client.
|
2014-06-10 02:49:41 +02:00
|
|
|
func (c *Client) handleNotification(ntfn *rawNotification) {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Ignore the notification if the client is not interested in any
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
switch ntfn.Method {
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnBlockConnected
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.BlockConnectedNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnBlockConnected == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
blockHash, blockHeight, blockTime, err := parseChainNtfnParams(ntfn.Params)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Warnf("Received invalid block connected "+
|
|
|
|
"notification: %v", err)
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
c.ntfnHandlers.OnBlockConnected(blockHash, blockHeight, blockTime)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2017-01-25 03:39:03 +01:00
|
|
|
// OnFilteredBlockConnected
|
|
|
|
case btcjson.FilteredBlockConnectedNtfnMethod:
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnFilteredBlockConnected == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blockHeight, blockHeader, transactions, err :=
|
|
|
|
parseFilteredBlockConnectedParams(ntfn.Params)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid filtered block "+
|
|
|
|
"connected notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ntfnHandlers.OnFilteredBlockConnected(blockHeight,
|
|
|
|
blockHeader, transactions)
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnBlockDisconnected
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.BlockDisconnectedNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnBlockDisconnected == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
blockHash, blockHeight, blockTime, err := parseChainNtfnParams(ntfn.Params)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Warnf("Received invalid block connected "+
|
|
|
|
"notification: %v", err)
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
c.ntfnHandlers.OnBlockDisconnected(blockHash, blockHeight, blockTime)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2017-01-25 03:39:03 +01:00
|
|
|
// OnFilteredBlockDisconnected
|
|
|
|
case btcjson.FilteredBlockDisconnectedNtfnMethod:
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnFilteredBlockDisconnected == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blockHeight, blockHeader, err :=
|
|
|
|
parseFilteredBlockDisconnectedParams(ntfn.Params)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid filtered block "+
|
|
|
|
"disconnected notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ntfnHandlers.OnFilteredBlockDisconnected(blockHeight,
|
|
|
|
blockHeader)
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnRecvTx
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.RecvTxNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnRecvTx == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
tx, block, err := parseChainTxNtfnParams(ntfn.Params)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Warnf("Received invalid recvtx notification: %v",
|
2014-05-07 18:14:39 +02:00
|
|
|
err)
|
2014-06-10 02:49:41 +02:00
|
|
|
return
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
c.ntfnHandlers.OnRecvTx(tx, block)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnRedeemingTx
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.RedeemingTxNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnRedeemingTx == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
tx, block, err := parseChainTxNtfnParams(ntfn.Params)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Warnf("Received invalid redeemingtx "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
c.ntfnHandlers.OnRedeemingTx(tx, block)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2017-01-25 03:39:03 +01:00
|
|
|
// OnRelevantTxAccepted
|
|
|
|
case btcjson.RelevantTxAcceptedNtfnMethod:
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnRelevantTxAccepted == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction, err := parseRelevantTxAcceptedParams(ntfn.Params)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid relevanttxaccepted "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ntfnHandlers.OnRelevantTxAccepted(transaction)
|
|
|
|
|
2014-06-16 16:42:29 +02:00
|
|
|
// OnRescanFinished
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.RescanFinishedNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-06-16 16:42:29 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnRescanFinished == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
hash, height, blkTime, err := parseRescanProgressParams(ntfn.Params)
|
2014-06-16 16:42:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid rescanfinished "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
c.ntfnHandlers.OnRescanFinished(hash, height, blkTime)
|
2014-06-16 16:42:29 +02:00
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// OnRescanProgress
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.RescanProgressNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnRescanProgress == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
hash, height, blkTime, err := parseRescanProgressParams(ntfn.Params)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid rescanprogress "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
c.ntfnHandlers.OnRescanProgress(hash, height, blkTime)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnTxAccepted
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.TxAcceptedNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnTxAccepted == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
hash, amt, err := parseTxAcceptedNtfnParams(ntfn.Params)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Warnf("Received invalid tx accepted "+
|
|
|
|
"notification: %v", err)
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
c.ntfnHandlers.OnTxAccepted(hash, amt)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnTxAcceptedVerbose
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.TxAcceptedVerboseNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnTxAcceptedVerbose == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
rawTx, err := parseTxAcceptedVerboseNtfnParams(ntfn.Params)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid tx accepted verbose "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ntfnHandlers.OnTxAcceptedVerbose(rawTx)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnBtcdConnected
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.BtcdConnectedNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnBtcdConnected == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
connected, err := parseBtcdConnectedNtfnParams(ntfn.Params)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid btcd connected "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ntfnHandlers.OnBtcdConnected(connected)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnAccountBalance
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.AccountBalanceNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnAccountBalance == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
account, bal, conf, err := parseAccountBalanceNtfnParams(ntfn.Params)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Warnf("Received invalid account balance "+
|
|
|
|
"notification: %v", err)
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
c.ntfnHandlers.OnAccountBalance(account, bal, conf)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnWalletLockState
|
2015-01-01 23:34:07 +01:00
|
|
|
case btcjson.WalletLockStateNtfnMethod:
|
2014-07-08 14:40:53 +02:00
|
|
|
// Ignore the notification if the client is not interested in
|
2014-05-07 18:14:39 +02:00
|
|
|
// it.
|
|
|
|
if c.ntfnHandlers.OnWalletLockState == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// The account name is not notified, so the return value is
|
|
|
|
// discarded.
|
|
|
|
_, locked, err := parseWalletLockStateNtfnParams(ntfn.Params)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Received invalid wallet lock state "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ntfnHandlers.OnWalletLockState(locked)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// OnUnknownNotification
|
|
|
|
default:
|
|
|
|
if c.ntfnHandlers.OnUnknownNotification == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
c.ntfnHandlers.OnUnknownNotification(ntfn.Method, ntfn.Params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrongNumParams is an error type describing an unparseable JSON-RPC
|
|
|
|
// notificiation due to an incorrect number of parameters for the
|
|
|
|
// expected notification type. The value is the number of parameters
|
|
|
|
// of the invalid notification.
|
|
|
|
type wrongNumParams int
|
|
|
|
|
|
|
|
// Error satisifies the builtin error interface.
|
|
|
|
func (e wrongNumParams) Error() string {
|
|
|
|
return fmt.Sprintf("wrong number of parameters (%d)", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseChainNtfnParams parses out the block hash and height from the parameters
|
|
|
|
// of blockconnected and blockdisconnected notifications.
|
2016-08-08 21:16:04 +02:00
|
|
|
func parseChainNtfnParams(params []json.RawMessage) (*chainhash.Hash,
|
2015-06-18 18:14:42 +02:00
|
|
|
int32, time.Time, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
|
2015-06-18 18:14:42 +02:00
|
|
|
if len(params) != 3 {
|
|
|
|
return nil, 0, time.Time{}, wrongNumParams(len(params))
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as a string.
|
2016-08-08 21:16:04 +02:00
|
|
|
var blockHashStr string
|
|
|
|
err := json.Unmarshal(params[0], &blockHashStr)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
2015-06-18 18:14:42 +02:00
|
|
|
return nil, 0, time.Time{}, err
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal second parameter as an integer.
|
|
|
|
var blockHeight int32
|
|
|
|
err = json.Unmarshal(params[1], &blockHeight)
|
|
|
|
if err != nil {
|
2015-06-18 18:14:42 +02:00
|
|
|
return nil, 0, time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal third parameter as unix time.
|
|
|
|
var blockTimeUnix int64
|
|
|
|
err = json.Unmarshal(params[2], &blockTimeUnix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, time.Time{}, err
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
// Create hash from block hash string.
|
|
|
|
blockHash, err := chainhash.NewHashFromStr(blockHashStr)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
2015-06-18 18:14:42 +02:00
|
|
|
return nil, 0, time.Time{}, err
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-18 18:14:42 +02:00
|
|
|
// Create time.Time from unix time.
|
|
|
|
blockTime := time.Unix(blockTimeUnix, 0)
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
return blockHash, blockHeight, blockTime, nil
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 03:39:03 +01:00
|
|
|
// parseFilteredBlockConnectedParams parses out the parameters included in a
|
|
|
|
// filteredblockconnected notification.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient
|
|
|
|
// and requires a websocket connection.
|
|
|
|
func parseFilteredBlockConnectedParams(params []json.RawMessage) (int32,
|
|
|
|
*wire.BlockHeader, []*btcutil.Tx, error) {
|
|
|
|
|
|
|
|
if len(params) < 3 {
|
|
|
|
return 0, nil, nil, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as an integer.
|
|
|
|
var blockHeight int32
|
|
|
|
err := json.Unmarshal(params[0], &blockHeight)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal second parameter as a slice of bytes.
|
|
|
|
blockHeaderBytes, err := parseHexParam(params[1])
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize block header from slice of bytes.
|
|
|
|
var blockHeader wire.BlockHeader
|
|
|
|
err = blockHeader.Deserialize(bytes.NewReader(blockHeaderBytes))
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal third parameter as a slice of hex-encoded strings.
|
|
|
|
var hexTransactions []string
|
|
|
|
err = json.Unmarshal(params[2], &hexTransactions)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create slice of transactions from slice of strings by hex-decoding.
|
|
|
|
transactions := make([]*btcutil.Tx, len(hexTransactions))
|
|
|
|
for i, hexTx := range hexTransactions {
|
|
|
|
transaction, err := hex.DecodeString(hexTx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
transactions[i], err = btcutil.NewTxFromBytes(transaction)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockHeight, &blockHeader, transactions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseFilteredBlockDisconnectedParams parses out the parameters included in a
|
|
|
|
// filteredblockdisconnected notification.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient
|
|
|
|
// and requires a websocket connection.
|
|
|
|
func parseFilteredBlockDisconnectedParams(params []json.RawMessage) (int32,
|
|
|
|
*wire.BlockHeader, error) {
|
|
|
|
if len(params) < 2 {
|
|
|
|
return 0, nil, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as an integer.
|
|
|
|
var blockHeight int32
|
|
|
|
err := json.Unmarshal(params[0], &blockHeight)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal second parmeter as a slice of bytes.
|
|
|
|
blockHeaderBytes, err := parseHexParam(params[1])
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize block header from slice of bytes.
|
|
|
|
var blockHeader wire.BlockHeader
|
|
|
|
err = blockHeader.Deserialize(bytes.NewReader(blockHeaderBytes))
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockHeight, &blockHeader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseHexParam(param json.RawMessage) ([]byte, error) {
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(param, &s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hex.DecodeString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseRelevantTxAcceptedParams parses out the parameter included in a
|
|
|
|
// relevanttxaccepted notification.
|
|
|
|
func parseRelevantTxAcceptedParams(params []json.RawMessage) (transaction []byte, err error) {
|
|
|
|
if len(params) < 1 {
|
|
|
|
return nil, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseHexParam(params[0])
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// parseChainTxNtfnParams parses out the transaction and optional details about
|
|
|
|
// the block it's mined in from the parameters of recvtx and redeemingtx
|
|
|
|
// notifications.
|
|
|
|
func parseChainTxNtfnParams(params []json.RawMessage) (*btcutil.Tx,
|
2015-01-01 23:34:07 +01:00
|
|
|
*btcjson.BlockDetails, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
|
|
|
|
if len(params) == 0 || len(params) > 2 {
|
|
|
|
return nil, nil, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as a string.
|
|
|
|
var txHex string
|
|
|
|
err := json.Unmarshal(params[0], &txHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If present, unmarshal second optional parameter as the block details
|
|
|
|
// JSON object.
|
2015-01-01 23:34:07 +01:00
|
|
|
var block *btcjson.BlockDetails
|
2014-06-10 02:49:41 +02:00
|
|
|
if len(params) > 1 {
|
|
|
|
err = json.Unmarshal(params[1], &block)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hex decode and deserialize the transaction.
|
|
|
|
serializedTx, err := hex.DecodeString(txHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-02-05 22:50:03 +01:00
|
|
|
var msgTx wire.MsgTx
|
2014-06-10 02:49:41 +02:00
|
|
|
err = msgTx.Deserialize(bytes.NewReader(serializedTx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Change recvtx and redeemingtx callback signatures to use
|
2016-08-08 21:16:04 +02:00
|
|
|
// nicer types for details about the block (block hash as a
|
|
|
|
// chainhash.Hash, block time as a time.Time, etc.).
|
2014-06-10 02:49:41 +02:00
|
|
|
return btcutil.NewTx(&msgTx), block, nil
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
// parseRescanProgressParams parses out the height of the last rescanned block
|
2014-06-16 16:42:29 +02:00
|
|
|
// from the parameters of rescanfinished and rescanprogress notifications.
|
2016-08-08 21:16:04 +02:00
|
|
|
func parseRescanProgressParams(params []json.RawMessage) (*chainhash.Hash, int32, time.Time, error) {
|
2014-07-17 20:37:54 +02:00
|
|
|
if len(params) != 3 {
|
|
|
|
return nil, 0, time.Time{}, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as an string.
|
|
|
|
var hashStr string
|
|
|
|
err := json.Unmarshal(params[0], &hashStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, time.Time{}, err
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
// Unmarshal second parameter as an integer.
|
2014-06-10 02:49:41 +02:00
|
|
|
var height int32
|
2014-07-17 20:37:54 +02:00
|
|
|
err = json.Unmarshal(params[1], &height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal third parameter as an integer.
|
|
|
|
var blkTime int64
|
|
|
|
err = json.Unmarshal(params[2], &blkTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode string encoding of block hash.
|
2016-08-08 21:16:04 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(hashStr)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
2014-07-17 20:37:54 +02:00
|
|
|
return nil, 0, time.Time{}, err
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
return hash, height, time.Unix(blkTime, 0), nil
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseTxAcceptedNtfnParams parses out the transaction hash and total amount
|
|
|
|
// from the parameters of a txaccepted notification.
|
2016-08-08 21:16:04 +02:00
|
|
|
func parseTxAcceptedNtfnParams(params []json.RawMessage) (*chainhash.Hash,
|
2014-06-10 02:49:41 +02:00
|
|
|
btcutil.Amount, error) {
|
|
|
|
|
|
|
|
if len(params) != 2 {
|
|
|
|
return nil, 0, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as a string.
|
2016-08-08 21:16:04 +02:00
|
|
|
var txHashStr string
|
|
|
|
err := json.Unmarshal(params[0], &txHashStr)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2015-04-17 20:20:39 +02:00
|
|
|
// Unmarshal second parameter as a floating point number.
|
|
|
|
var famt float64
|
|
|
|
err = json.Unmarshal(params[1], &famt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds check amount.
|
|
|
|
amt, err := btcutil.NewAmount(famt)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode string encoding of transaction sha.
|
2016-08-08 21:16:04 +02:00
|
|
|
txHash, err := chainhash.NewHashFromStr(txHashStr)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:11:20 +01:00
|
|
|
return txHash, amt, nil
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseTxAcceptedVerboseNtfnParams parses out details about a raw transaction
|
|
|
|
// from the parameters of a txacceptedverbose notification.
|
|
|
|
func parseTxAcceptedVerboseNtfnParams(params []json.RawMessage) (*btcjson.TxRawResult,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
if len(params) != 1 {
|
|
|
|
return nil, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as a raw transaction result object.
|
|
|
|
var rawTx btcjson.TxRawResult
|
|
|
|
err := json.Unmarshal(params[0], &rawTx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
// TODO: change txacceptedverbose notification callbacks to use nicer
|
2014-06-10 02:49:41 +02:00
|
|
|
// types for all details about the transaction (i.e. decoding hashes
|
|
|
|
// from their string encoding).
|
|
|
|
return &rawTx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseBtcdConnectedNtfnParams parses out the connection status of btcd
|
|
|
|
// and btcwallet from the parameters of a btcdconnected notification.
|
|
|
|
func parseBtcdConnectedNtfnParams(params []json.RawMessage) (bool, error) {
|
|
|
|
if len(params) != 1 {
|
|
|
|
return false, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as a boolean.
|
|
|
|
var connected bool
|
|
|
|
err := json.Unmarshal(params[0], &connected)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return connected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAccountBalanceNtfnParams parses out the account name, total balance,
|
|
|
|
// and whether or not the balance is confirmed or unconfirmed from the
|
|
|
|
// parameters of an accountbalance notification.
|
|
|
|
func parseAccountBalanceNtfnParams(params []json.RawMessage) (account string,
|
|
|
|
balance btcutil.Amount, confirmed bool, err error) {
|
|
|
|
|
|
|
|
if len(params) != 3 {
|
|
|
|
return "", 0, false, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as a string.
|
|
|
|
err = json.Unmarshal(params[0], &account)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal second parameter as a floating point number.
|
|
|
|
var fbal float64
|
|
|
|
err = json.Unmarshal(params[1], &fbal)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal third parameter as a boolean.
|
|
|
|
err = json.Unmarshal(params[2], &confirmed)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, false, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2014-06-10 02:49:41 +02:00
|
|
|
|
|
|
|
// Bounds check amount.
|
2015-01-01 23:34:07 +01:00
|
|
|
bal, err := btcutil.NewAmount(fbal)
|
2014-06-10 02:49:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", 0, false, err
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:11:20 +01:00
|
|
|
return account, bal, confirmed, nil
|
2014-06-10 02:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseWalletLockStateNtfnParams parses out the account name and locked
|
|
|
|
// state of an account from the parameters of a walletlockstate notification.
|
|
|
|
func parseWalletLockStateNtfnParams(params []json.RawMessage) (account string,
|
|
|
|
locked bool, err error) {
|
|
|
|
|
|
|
|
if len(params) != 2 {
|
|
|
|
return "", false, wrongNumParams(len(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal first parameter as a string.
|
|
|
|
err = json.Unmarshal(params[0], &account)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal second parameter as a boolean.
|
|
|
|
err = json.Unmarshal(params[1], &locked)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, locked, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FutureNotifyBlocksResult is a future promise to deliver the result of a
|
|
|
|
// NotifyBlocksAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureNotifyBlocksResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error
|
|
|
|
// if the registration was not successful.
|
|
|
|
func (r FutureNotifyBlocksResult) Receive() error {
|
|
|
|
_, err := receiveFuture(r)
|
2016-11-16 21:52:02 +01:00
|
|
|
return err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBlocksAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See NotifyBlocks for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
|
|
|
func (c *Client) NotifyBlocksAsync() FutureNotifyBlocksResult {
|
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewNotifyBlocksCmd()
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBlocks registers the client to receive notifications when blocks are
|
|
|
|
// connected and disconnected from the main chain. The notifications are
|
|
|
|
// delivered to the notification handlers associated with the client. Calling
|
|
|
|
// this function has no effect if there are no notification handlers and will
|
|
|
|
// result in an error if the client is configured to run in HTTP POST mode.
|
|
|
|
//
|
|
|
|
// The notifications delivered as a result of this call will be via one of
|
|
|
|
// OnBlockConnected or OnBlockDisconnected.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
|
|
|
func (c *Client) NotifyBlocks() error {
|
|
|
|
return c.NotifyBlocksAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureNotifySpentResult is a future promise to deliver the result of a
|
|
|
|
// NotifySpentAsync RPC invocation (or an applicable error).
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use FutureLoadTxFilterResult instead.
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureNotifySpentResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error
|
|
|
|
// if the registration was not successful.
|
|
|
|
func (r FutureNotifySpentResult) Receive() error {
|
|
|
|
_, err := receiveFuture(r)
|
2016-11-16 21:52:02 +01:00
|
|
|
return err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 06:44:43 +02:00
|
|
|
// notifySpentInternal is the same as notifySpentAsync except it accepts
|
|
|
|
// the converted outpoints as a parameter so the client can more efficiently
|
|
|
|
// recreate the previous notification state on reconnect.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) notifySpentInternal(outpoints []btcjson.OutPoint) FutureNotifySpentResult {
|
2014-05-10 06:44:43 +02:00
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-10 06:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewNotifySpentCmd(outpoints)
|
2014-05-10 06:44:43 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
2015-04-23 20:55:35 +02:00
|
|
|
// newOutPointFromWire constructs the btcjson representation of a transaction
|
|
|
|
// outpoint from the wire type.
|
|
|
|
func newOutPointFromWire(op *wire.OutPoint) btcjson.OutPoint {
|
|
|
|
return btcjson.OutPoint{
|
|
|
|
Hash: op.Hash.String(),
|
|
|
|
Index: op.Index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// NotifySpentAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See NotifySpent for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use LoadTxFilterAsync instead.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) NotifySpentAsync(outpoints []*wire.OutPoint) FutureNotifySpentResult {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
ops := make([]btcjson.OutPoint, 0, len(outpoints))
|
2014-05-06 19:26:38 +02:00
|
|
|
for _, outpoint := range outpoints {
|
2015-04-23 20:55:35 +02:00
|
|
|
ops = append(ops, newOutPointFromWire(outpoint))
|
2014-05-06 19:26:38 +02:00
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewNotifySpentCmd(ops)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifySpent registers the client to receive notifications when the passed
|
2014-05-06 19:26:38 +02:00
|
|
|
// transaction outputs are spent. The notifications are delivered to the
|
2014-05-07 18:14:39 +02:00
|
|
|
// notification handlers associated with the client. Calling this function has
|
|
|
|
// no effect if there are no notification handlers and will result in an error
|
|
|
|
// if the client is configured to run in HTTP POST mode.
|
|
|
|
//
|
|
|
|
// The notifications delivered as a result of this call will be via
|
|
|
|
// OnRedeemingTx.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use LoadTxFilter instead.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) NotifySpent(outpoints []*wire.OutPoint) error {
|
2014-05-06 19:26:38 +02:00
|
|
|
return c.NotifySpentAsync(outpoints).Receive()
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FutureNotifyNewTransactionsResult is a future promise to deliver the result
|
|
|
|
// of a NotifyNewTransactionsAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureNotifyNewTransactionsResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error
|
|
|
|
// if the registration was not successful.
|
|
|
|
func (r FutureNotifyNewTransactionsResult) Receive() error {
|
|
|
|
_, err := receiveFuture(r)
|
2016-11-16 21:52:02 +01:00
|
|
|
return err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyNewTransactionsAsync returns an instance of a type that can be used to
|
|
|
|
// get the result of the RPC at some future time by invoking the Receive
|
|
|
|
// function on the returned instance.
|
|
|
|
//
|
|
|
|
// See NotifyNewTransactionsAsync for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
|
|
|
func (c *Client) NotifyNewTransactionsAsync(verbose bool) FutureNotifyNewTransactionsResult {
|
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewNotifyNewTransactionsCmd(&verbose)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyNewTransactions registers the client to receive notifications every
|
|
|
|
// time a new transaction is accepted to the memory pool. The notifications are
|
|
|
|
// delivered to the notification handlers associated with the client. Calling
|
|
|
|
// this function has no effect if there are no notification handlers and will
|
|
|
|
// result in an error if the client is configured to run in HTTP POST mode.
|
|
|
|
//
|
|
|
|
// The notifications delivered as a result of this call will be via one of
|
|
|
|
// OnTxAccepted (when verbose is false) or OnTxAcceptedVerbose (when verbose is
|
|
|
|
// true).
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
|
|
|
func (c *Client) NotifyNewTransactions(verbose bool) error {
|
|
|
|
return c.NotifyNewTransactionsAsync(verbose).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureNotifyReceivedResult is a future promise to deliver the result of a
|
|
|
|
// NotifyReceivedAsync RPC invocation (or an applicable error).
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use FutureLoadTxFilterResult instead.
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureNotifyReceivedResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error
|
|
|
|
// if the registration was not successful.
|
|
|
|
func (r FutureNotifyReceivedResult) Receive() error {
|
|
|
|
_, err := receiveFuture(r)
|
2016-11-16 21:52:02 +01:00
|
|
|
return err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 06:44:43 +02:00
|
|
|
// notifyReceivedInternal is the same as notifyReceivedAsync except it accepts
|
|
|
|
// the converted addresses as a parameter so the client can more efficiently
|
|
|
|
// recreate the previous notification state on reconnect.
|
|
|
|
func (c *Client) notifyReceivedInternal(addresses []string) FutureNotifyReceivedResult {
|
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-10 06:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert addresses to strings.
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewNotifyReceivedCmd(addresses)
|
2014-05-10 06:44:43 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// NotifyReceivedAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See NotifyReceived for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use LoadTxFilterAsync instead.
|
2014-05-07 18:14:39 +02:00
|
|
|
func (c *Client) NotifyReceivedAsync(addresses []btcutil.Address) FutureNotifyReceivedResult {
|
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert addresses to strings.
|
|
|
|
addrs := make([]string, 0, len(addresses))
|
|
|
|
for _, addr := range addresses {
|
2014-06-13 05:50:47 +02:00
|
|
|
addrs = append(addrs, addr.String())
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewNotifyReceivedCmd(addrs)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyReceived registers the client to receive notifications every time a
|
|
|
|
// new transaction which pays to one of the passed addresses is accepted to
|
|
|
|
// memory pool or in a block connected to the block chain. In addition, when
|
|
|
|
// one of these transactions is detected, the client is also automatically
|
|
|
|
// registered for notifications when the new transaction outpoints the address
|
|
|
|
// now has available are spent (See NotifySpent). The notifications are
|
|
|
|
// delivered to the notification handlers associated with the client. Calling
|
|
|
|
// this function has no effect if there are no notification handlers and will
|
|
|
|
// result in an error if the client is configured to run in HTTP POST mode.
|
|
|
|
//
|
|
|
|
// The notifications delivered as a result of this call will be via one of
|
|
|
|
// *OnRecvTx (for transactions that receive funds to one of the passed
|
|
|
|
// addresses) or OnRedeemingTx (for transactions which spend from one
|
|
|
|
// of the outpoints which are automatically registered upon receipt of funds to
|
|
|
|
// the address).
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:39:03 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use LoadTxFilter instead.
|
2014-05-07 18:14:39 +02:00
|
|
|
func (c *Client) NotifyReceived(addresses []btcutil.Address) error {
|
|
|
|
return c.NotifyReceivedAsync(addresses).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureRescanResult is a future promise to deliver the result of a RescanAsync
|
|
|
|
// or RescanEndHeightAsync RPC invocation (or an applicable error).
|
2017-01-25 03:44:20 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use FutureRescanBlocksResult instead.
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureRescanResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error
|
|
|
|
// if the rescan was not successful.
|
|
|
|
func (r FutureRescanResult) Receive() error {
|
|
|
|
_, err := receiveFuture(r)
|
2016-11-16 21:52:02 +01:00
|
|
|
return err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RescanAsync returns an instance of a type that can be used to get the result
|
|
|
|
// of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See Rescan for the blocking version and more details.
|
|
|
|
//
|
2014-06-11 20:23:21 +02:00
|
|
|
// NOTE: Rescan requests are not issued on client reconnect and must be
|
|
|
|
// performed manually (ideally with a new start height based on the last
|
|
|
|
// rescan progress notification). See the OnClientConnected notification
|
|
|
|
// callback for a good callsite to reissue rescan requests on connect and
|
|
|
|
// reconnect.
|
|
|
|
//
|
2014-05-07 18:14:39 +02:00
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:44:20 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use RescanBlocksAsync instead.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) RescanAsync(startBlock *chainhash.Hash,
|
2014-07-17 20:37:54 +02:00
|
|
|
addresses []btcutil.Address,
|
2015-02-05 22:50:03 +01:00
|
|
|
outpoints []*wire.OutPoint) FutureRescanResult {
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
// Convert block hashes to strings.
|
2016-08-08 21:16:04 +02:00
|
|
|
var startBlockHashStr string
|
2014-07-17 20:37:54 +02:00
|
|
|
if startBlock != nil {
|
2016-08-08 21:16:04 +02:00
|
|
|
startBlockHashStr = startBlock.String()
|
2014-07-17 20:37:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// Convert addresses to strings.
|
|
|
|
addrs := make([]string, 0, len(addresses))
|
|
|
|
for _, addr := range addresses {
|
2014-06-13 05:50:47 +02:00
|
|
|
addrs = append(addrs, addr.String())
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert outpoints.
|
2015-01-01 23:34:07 +01:00
|
|
|
ops := make([]btcjson.OutPoint, 0, len(outpoints))
|
2014-05-07 18:14:39 +02:00
|
|
|
for _, op := range outpoints {
|
2015-04-23 20:55:35 +02:00
|
|
|
ops = append(ops, newOutPointFromWire(op))
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
cmd := btcjson.NewRescanCmd(startBlockHashStr, addrs, ops, nil)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
// Rescan rescans the block chain starting from the provided starting block to
|
|
|
|
// the end of the longest chain for transactions that pay to the passed
|
|
|
|
// addresses and transactions which spend the passed outpoints.
|
2014-05-07 18:14:39 +02:00
|
|
|
//
|
|
|
|
// The notifications of found transactions are delivered to the notification
|
|
|
|
// handlers associated with client and this call will not return until the
|
|
|
|
// rescan has completed. Calling this function has no effect if there are no
|
|
|
|
// notification handlers and will result in an error if the client is configured
|
|
|
|
// to run in HTTP POST mode.
|
|
|
|
//
|
|
|
|
// The notifications delivered as a result of this call will be via one of
|
|
|
|
// OnRedeemingTx (for transactions which spend from the one of the
|
|
|
|
// passed outpoints), OnRecvTx (for transactions that receive funds
|
|
|
|
// to one of the passed addresses), and OnRescanProgress (for rescan progress
|
|
|
|
// updates).
|
|
|
|
//
|
2014-07-17 20:37:54 +02:00
|
|
|
// See RescanEndBlock to also specify an ending block to finish the rescan
|
|
|
|
// without continuing through the best block on the main chain.
|
2014-05-07 18:14:39 +02:00
|
|
|
//
|
2014-06-11 20:23:21 +02:00
|
|
|
// NOTE: Rescan requests are not issued on client reconnect and must be
|
|
|
|
// performed manually (ideally with a new start height based on the last
|
|
|
|
// rescan progress notification). See the OnClientConnected notification
|
|
|
|
// callback for a good callsite to reissue rescan requests on connect and
|
|
|
|
// reconnect.
|
|
|
|
//
|
2014-05-07 18:14:39 +02:00
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:44:20 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use RescanBlocks instead.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) Rescan(startBlock *chainhash.Hash,
|
2014-07-17 20:37:54 +02:00
|
|
|
addresses []btcutil.Address,
|
2015-02-05 22:50:03 +01:00
|
|
|
outpoints []*wire.OutPoint) error {
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
return c.RescanAsync(startBlock, addresses, outpoints).Receive()
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
// RescanEndBlockAsync returns an instance of a type that can be used to get
|
2014-05-07 18:14:39 +02:00
|
|
|
// the result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
2014-07-17 20:37:54 +02:00
|
|
|
// See RescanEndBlock for the blocking version and more details.
|
2014-05-07 18:14:39 +02:00
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:44:20 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use RescanBlocksAsync instead.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) RescanEndBlockAsync(startBlock *chainhash.Hash,
|
2015-02-05 22:50:03 +01:00
|
|
|
addresses []btcutil.Address, outpoints []*wire.OutPoint,
|
2016-08-08 21:16:04 +02:00
|
|
|
endBlock *chainhash.Hash) FutureRescanResult {
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Not supported in HTTP POST mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2015-09-15 21:11:47 +02:00
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the notification if the client is not interested in
|
|
|
|
// notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return newNilFutureResult()
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
// Convert block hashes to strings.
|
2016-08-08 21:16:04 +02:00
|
|
|
var startBlockHashStr, endBlockHashStr string
|
2014-07-17 20:37:54 +02:00
|
|
|
if startBlock != nil {
|
2016-08-08 21:16:04 +02:00
|
|
|
startBlockHashStr = startBlock.String()
|
2014-07-17 20:37:54 +02:00
|
|
|
}
|
|
|
|
if endBlock != nil {
|
2016-08-08 21:16:04 +02:00
|
|
|
endBlockHashStr = endBlock.String()
|
2014-07-17 20:37:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// Convert addresses to strings.
|
|
|
|
addrs := make([]string, 0, len(addresses))
|
|
|
|
for _, addr := range addresses {
|
2014-06-13 05:50:47 +02:00
|
|
|
addrs = append(addrs, addr.String())
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert outpoints.
|
2015-01-01 23:34:07 +01:00
|
|
|
ops := make([]btcjson.OutPoint, 0, len(outpoints))
|
2014-05-07 18:14:39 +02:00
|
|
|
for _, op := range outpoints {
|
2015-04-23 20:55:35 +02:00
|
|
|
ops = append(ops, newOutPointFromWire(op))
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
cmd := btcjson.NewRescanCmd(startBlockHashStr, addrs, ops,
|
|
|
|
&endBlockHashStr)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:09:18 +01:00
|
|
|
// RescanEndHeight rescans the block chain starting from the provided starting
|
2014-07-17 20:37:54 +02:00
|
|
|
// block up to the provided ending block for transactions that pay to the
|
|
|
|
// passed addresses and transactions which spend the passed outpoints.
|
2014-05-07 18:14:39 +02:00
|
|
|
//
|
|
|
|
// The notifications of found transactions are delivered to the notification
|
|
|
|
// handlers associated with client and this call will not return until the
|
|
|
|
// rescan has completed. Calling this function has no effect if there are no
|
|
|
|
// notification handlers and will result in an error if the client is configured
|
|
|
|
// to run in HTTP POST mode.
|
|
|
|
//
|
|
|
|
// The notifications delivered as a result of this call will be via one of
|
|
|
|
// OnRedeemingTx (for transactions which spend from the one of the
|
|
|
|
// passed outpoints), OnRecvTx (for transactions that receive funds
|
|
|
|
// to one of the passed addresses), and OnRescanProgress (for rescan progress
|
|
|
|
// updates).
|
|
|
|
//
|
|
|
|
// See Rescan to also perform a rescan through current end of the longest chain.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension and requires a websocket connection.
|
2017-01-25 03:44:20 +01:00
|
|
|
//
|
2018-09-13 23:13:06 +02:00
|
|
|
// Deprecated: Use RescanBlocks instead.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) RescanEndHeight(startBlock *chainhash.Hash,
|
2015-02-05 22:50:03 +01:00
|
|
|
addresses []btcutil.Address, outpoints []*wire.OutPoint,
|
2016-08-08 21:16:04 +02:00
|
|
|
endBlock *chainhash.Hash) error {
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2014-07-17 20:37:54 +02:00
|
|
|
return c.RescanEndBlockAsync(startBlock, addresses, outpoints,
|
|
|
|
endBlock).Receive()
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2017-01-25 03:39:03 +01:00
|
|
|
|
|
|
|
// FutureLoadTxFilterResult is a future promise to deliver the result
|
|
|
|
// of a LoadTxFilterAsync RPC invocation (or an applicable error).
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient
|
|
|
|
// and requires a websocket connection.
|
|
|
|
type FutureLoadTxFilterResult chan *response
|
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error
|
|
|
|
// if the registration was not successful.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient
|
|
|
|
// and requires a websocket connection.
|
|
|
|
func (r FutureLoadTxFilterResult) Receive() error {
|
|
|
|
_, err := receiveFuture(r)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTxFilterAsync returns an instance of a type that can be used to
|
|
|
|
// get the result of the RPC at some future time by invoking the Receive
|
|
|
|
// function on the returned instance.
|
|
|
|
//
|
|
|
|
// See LoadTxFilter for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient
|
|
|
|
// and requires a websocket connection.
|
|
|
|
func (c *Client) LoadTxFilterAsync(reload bool, addresses []btcutil.Address,
|
|
|
|
outPoints []wire.OutPoint) FutureLoadTxFilterResult {
|
|
|
|
|
|
|
|
addrStrs := make([]string, len(addresses))
|
|
|
|
for i, a := range addresses {
|
|
|
|
addrStrs[i] = a.EncodeAddress()
|
|
|
|
}
|
|
|
|
outPointObjects := make([]btcjson.OutPoint, len(outPoints))
|
|
|
|
for i := range outPoints {
|
|
|
|
outPointObjects[i] = btcjson.OutPoint{
|
|
|
|
Hash: outPoints[i].Hash.String(),
|
|
|
|
Index: outPoints[i].Index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := btcjson.NewLoadTxFilterCmd(reload, addrStrs, outPointObjects)
|
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTxFilter loads, reloads, or adds data to a websocket client's transaction
|
|
|
|
// filter. The filter is consistently updated based on inspected transactions
|
|
|
|
// during mempool acceptance, block acceptance, and for all rescanned blocks.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient
|
|
|
|
// and requires a websocket connection.
|
|
|
|
func (c *Client) LoadTxFilter(reload bool, addresses []btcutil.Address, outPoints []wire.OutPoint) error {
|
|
|
|
return c.LoadTxFilterAsync(reload, addresses, outPoints).Receive()
|
|
|
|
}
|