decred.org/dcrwallet/v3@v3.1.0/rpc/client/dcrd/notifications.go (about)

     1  // Copyright (c) 2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package dcrd
     6  
     7  import (
     8  	"encoding/json"
     9  
    10  	"decred.org/dcrwallet/v3/errors"
    11  	"github.com/decred/dcrd/chaincfg/chainhash"
    12  	"github.com/decred/dcrd/wire"
    13  )
    14  
    15  func unmarshalArray(j json.RawMessage, params ...interface{}) error {
    16  	err := json.Unmarshal(j, &params)
    17  	if err != nil {
    18  		return errors.E(errors.Encoding, err)
    19  	}
    20  	return nil
    21  }
    22  
    23  // WinningTickets extracts the parameters from a winningtickets JSON-RPC
    24  // notification.
    25  func WinningTickets(params json.RawMessage) (block *chainhash.Hash, height int32, winners []*chainhash.Hash, err error) {
    26  	// Parameters (array):
    27  	// 0: block hash (reversed hex string)
    28  	// 1: block height (number)
    29  	// 2: object with ticket hashes in values
    30  	ticketObj := make(map[string]*hash)
    31  	hash := new(hash)
    32  	err = unmarshalArray(params, hash, &height, &ticketObj)
    33  	if err != nil {
    34  		return
    35  	}
    36  	winners = make([]*chainhash.Hash, 0, len(ticketObj))
    37  	for _, h := range ticketObj {
    38  		winners = append(winners, h.Hash)
    39  	}
    40  	return hash.Hash, height, winners, nil
    41  }
    42  
    43  // BlockConnected extracts the parameters from a blockconnected JSON-RPC
    44  // notification.
    45  func BlockConnected(params json.RawMessage) (header *wire.BlockHeader, relevant []*wire.MsgTx, err error) {
    46  	// Parameters (array):
    47  	// 0: block header
    48  	// 1: array of relevant hex-encoded transactions
    49  	header = new(wire.BlockHeader)
    50  	txs := new(transactions)
    51  	err = unmarshalArray(params, unhex(header), txs)
    52  	if err != nil {
    53  		return
    54  	}
    55  	return header, txs.Transactions, nil
    56  }
    57  
    58  // RelevantTxAccepted extracts the parameters from a relevanttxaccepted JSON-RPC
    59  // notification.
    60  func RelevantTxAccepted(params json.RawMessage) (tx *wire.MsgTx, err error) {
    61  	// Parameters (array):
    62  	// 0: relevant hex-encoded transaction
    63  	tx = new(wire.MsgTx)
    64  	err = unmarshalArray(params, unhex(tx))
    65  	return
    66  }
    67  
    68  // TSpend extracts the parameters from a tspend JSON-RPC notification.
    69  func TSpend(params json.RawMessage) (tx *wire.MsgTx, err error) {
    70  	// Parameters (array):
    71  	// 0: relevant hex-encoded transaction
    72  	tx = new(wire.MsgTx)
    73  	err = unmarshalArray(params, unhex(tx))
    74  	return
    75  }