2014-01-30 16:14:02 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This file implements the websocket client connection to a bitcoin RPC
|
|
|
|
// server.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go.net/websocket"
|
2014-02-24 20:35:30 +01:00
|
|
|
"encoding/hex"
|
2014-01-30 16:14:02 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"github.com/conformal/btcjson"
|
2014-02-24 20:35:30 +01:00
|
|
|
"github.com/conformal/btcutil"
|
2014-01-30 16:14:02 +01:00
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"github.com/conformal/btcws"
|
2014-03-18 04:36:31 +01:00
|
|
|
"io"
|
2014-01-30 16:14:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServerConn is an interface representing a client connection to a bitcoin RPC
|
|
|
|
// server.
|
|
|
|
type ServerConn interface {
|
|
|
|
// SendRequest sends a bitcoin RPC request, returning a channel to
|
|
|
|
// read the reply. A channel is used so both synchronous and
|
|
|
|
// asynchronous RPC can be supported.
|
2014-04-09 18:07:09 +02:00
|
|
|
SendRequest(request *ServerRequest) chan RawRPCResponse
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrBtcdDisconnected describes an error where an operation cannot
|
|
|
|
// successfully complete due to btcwallet not being connected to
|
|
|
|
// btcd.
|
|
|
|
var ErrBtcdDisconnected = btcjson.Error{
|
|
|
|
Code: -1,
|
|
|
|
Message: "btcd disconnected",
|
|
|
|
}
|
|
|
|
|
2014-04-09 18:07:09 +02:00
|
|
|
// ErrBtcdDisconnectedRaw is the raw JSON encoding of ErrBtcdDisconnected.
|
|
|
|
var ErrBtcdDisconnectedRaw = json.RawMessage(`{"code":-1,"message":"btcd disconnected"}`)
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// BtcdRPCConn is a type managing a client connection to a btcd RPC server
|
|
|
|
// over websockets.
|
|
|
|
type BtcdRPCConn struct {
|
|
|
|
ws *websocket.Conn
|
|
|
|
addRequest chan *AddRPCRequest
|
|
|
|
closed chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that BtcdRPCConn can be used as an RPCConn.
|
|
|
|
var _ ServerConn = &BtcdRPCConn{}
|
|
|
|
|
|
|
|
// NewBtcdRPCConn creates a new RPC connection from a btcd websocket
|
|
|
|
// connection to btcd.
|
|
|
|
func NewBtcdRPCConn(ws *websocket.Conn) *BtcdRPCConn {
|
|
|
|
conn := &BtcdRPCConn{
|
|
|
|
ws: ws,
|
|
|
|
addRequest: make(chan *AddRPCRequest),
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendRequest sends an RPC request and returns a channel to read the response's
|
|
|
|
// result and error. Part of the RPCConn interface.
|
2014-04-09 18:07:09 +02:00
|
|
|
func (btcd *BtcdRPCConn) SendRequest(request *ServerRequest) chan RawRPCResponse {
|
2014-01-30 16:14:02 +01:00
|
|
|
select {
|
|
|
|
case <-btcd.closed:
|
|
|
|
// The connection has closed, so instead of adding and sending
|
|
|
|
// a request, return a channel that just replies with the
|
|
|
|
// error for a disconnected btcd.
|
2014-04-09 18:07:09 +02:00
|
|
|
responseChan := make(chan RawRPCResponse, 1)
|
|
|
|
responseChan <- RawRPCResponse{Error: &ErrBtcdDisconnectedRaw}
|
2014-01-30 16:14:02 +01:00
|
|
|
return responseChan
|
|
|
|
|
|
|
|
default:
|
|
|
|
addRequest := &AddRPCRequest{
|
|
|
|
Request: request,
|
2014-04-09 18:07:09 +02:00
|
|
|
ResponseChan: make(chan chan RawRPCResponse, 1),
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
btcd.addRequest <- addRequest
|
|
|
|
return <-addRequest.ResponseChan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connected returns whether the connection remains established to the RPC
|
|
|
|
// server.
|
|
|
|
//
|
|
|
|
// This function probably should be removed, as any checks for confirming
|
|
|
|
// the connection are no longer valid after the check and may result in
|
|
|
|
// races.
|
|
|
|
func (btcd *BtcdRPCConn) Connected() bool {
|
|
|
|
select {
|
|
|
|
case <-btcd.closed:
|
|
|
|
return false
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 19:03:23 +01:00
|
|
|
// Close forces closing the current btcd connection.
|
|
|
|
func (btcd *BtcdRPCConn) Close() {
|
|
|
|
select {
|
|
|
|
case <-btcd.closed:
|
|
|
|
default:
|
|
|
|
close(btcd.closed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// AddRPCRequest is used to add an RPCRequest to the pool of requests
|
|
|
|
// being manaaged by a btcd RPC connection.
|
|
|
|
type AddRPCRequest struct {
|
|
|
|
Request *ServerRequest
|
2014-04-09 18:07:09 +02:00
|
|
|
ResponseChan chan chan RawRPCResponse
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// send performs the actual send of the marshaled request over the btcd
|
|
|
|
// websocket connection.
|
|
|
|
func (btcd *BtcdRPCConn) send(rpcrequest *ServerRequest) error {
|
|
|
|
// btcjson.Cmds define their own MarshalJSON which returns an error
|
|
|
|
// to satisify the json.Marshaler interface, but will never error.
|
|
|
|
mrequest, _ := rpcrequest.request.MarshalJSON()
|
|
|
|
return websocket.Message.Send(btcd.ws, mrequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the goroutines required to send RPC requests and listen for
|
|
|
|
// replies.
|
|
|
|
func (btcd *BtcdRPCConn) Start() {
|
|
|
|
done := btcd.closed
|
2014-04-09 18:07:09 +02:00
|
|
|
responses := make(chan RawRPCResponse)
|
2014-01-30 16:14:02 +01:00
|
|
|
|
|
|
|
// Maintain a map of JSON IDs to RPCRequests currently being waited on.
|
|
|
|
go func() {
|
|
|
|
m := make(map[uint64]*ServerRequest)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case addrequest := <-btcd.addRequest:
|
|
|
|
rpcrequest := addrequest.Request
|
|
|
|
m[rpcrequest.request.Id().(uint64)] = rpcrequest
|
|
|
|
|
|
|
|
if err := btcd.send(rpcrequest); err != nil {
|
|
|
|
// Connection lost.
|
2014-03-17 22:00:04 +01:00
|
|
|
log.Infof("Cannot complete btcd websocket send: %v",
|
|
|
|
err)
|
2014-01-30 16:14:02 +01:00
|
|
|
btcd.ws.Close()
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrequest.ResponseChan <- rpcrequest.response
|
|
|
|
|
2014-04-09 18:07:09 +02:00
|
|
|
case rawResponse, ok := <-responses:
|
2014-03-17 22:00:04 +01:00
|
|
|
if !ok {
|
|
|
|
responses = nil
|
|
|
|
close(done)
|
|
|
|
break
|
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
rpcrequest, ok := m[*rawResponse.Id]
|
2014-01-30 16:14:02 +01:00
|
|
|
if !ok {
|
|
|
|
log.Warnf("Received unexpected btcd response")
|
|
|
|
continue
|
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
delete(m, *rawResponse.Id)
|
|
|
|
|
|
|
|
rpcrequest.response <- rawResponse
|
|
|
|
|
|
|
|
/*
|
|
|
|
//rpcrequest.result
|
|
|
|
var jsonErr *btcjson.Error
|
|
|
|
if rawResponse.result != nil {
|
|
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(*rawResponse.result), &result)
|
|
|
|
err := json.Unmarshal([]byte(*rawResponse.error), &error)
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-04-09 18:07:09 +02:00
|
|
|
rawResult := recvResponse
|
|
|
|
rawError := recvResponse
|
2014-01-30 16:14:02 +01:00
|
|
|
response := &ServerResponse{
|
2014-04-09 18:07:09 +02:00
|
|
|
result: r.Result,
|
|
|
|
err: jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
rpcrequest.response <- response
|
2014-04-09 18:07:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
// If no result var was set, create and send
|
|
|
|
// send the response unmarshaled by the json
|
|
|
|
// package.
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-04-09 18:07:09 +02:00
|
|
|
|
|
|
|
if rpcrequest.result == nil {
|
|
|
|
response := &ServerResponse{
|
|
|
|
result: recvResponse.reply.Result,
|
|
|
|
err: recvResponse.reply.Error,
|
|
|
|
}
|
|
|
|
rpcrequest.response <- response
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// A return var was set, so unmarshal again
|
|
|
|
// into the var before sending the response.
|
|
|
|
*/
|
2014-01-30 16:14:02 +01:00
|
|
|
|
|
|
|
case <-done:
|
2014-04-09 18:07:09 +02:00
|
|
|
resp := RawRPCResponse{Error: &ErrBtcdDisconnectedRaw}
|
2014-01-30 16:14:02 +01:00
|
|
|
for _, request := range m {
|
2014-04-09 18:07:09 +02:00
|
|
|
request.response <- resp
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Listen for replies/notifications from btcd, and decide how to handle them.
|
|
|
|
go func() {
|
|
|
|
// Idea: instead of reading btcd messages from just one websocket
|
|
|
|
// connection, maybe use two so the same connection isn't used
|
|
|
|
// for both notifications and responses? Should make handling
|
|
|
|
// must faster as unnecessary unmarshal attempts could be avoided.
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m string
|
|
|
|
if err := websocket.Message.Receive(btcd.ws, &m); err != nil {
|
2014-03-18 04:36:31 +01:00
|
|
|
// Log warning if btcd did not disconnect.
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Infof("Cannot receive btcd websocket message: %v",
|
|
|
|
err)
|
|
|
|
}
|
2014-03-17 22:00:04 +01:00
|
|
|
btcd.ws.Close()
|
|
|
|
close(responses)
|
2014-01-30 16:14:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try notifications (requests with nil ids) first.
|
|
|
|
n, err := unmarshalNotification(m)
|
|
|
|
if err == nil {
|
|
|
|
svrNtfns <- n
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be a response.
|
|
|
|
r, err := unmarshalResponse(m)
|
|
|
|
if err == nil {
|
|
|
|
responses <- r
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not sure what was received but it isn't correct.
|
|
|
|
log.Warnf("Received invalid message from btcd")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-04-09 18:07:09 +02:00
|
|
|
// unmarshalResponse attempts to unmarshal a marshaled JSON-RPC response.
|
|
|
|
func unmarshalResponse(s string) (RawRPCResponse, error) {
|
|
|
|
var r RawRPCResponse
|
2014-01-30 16:14:02 +01:00
|
|
|
if err := json.Unmarshal([]byte(s), &r); err != nil {
|
2014-04-09 18:07:09 +02:00
|
|
|
return r, err
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a valid ID.
|
|
|
|
if r.Id == nil {
|
2014-04-09 18:07:09 +02:00
|
|
|
return r, errors.New("id is null")
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
return r, nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalNotification attempts to unmarshal a marshaled JSON-RPC
|
|
|
|
// notification (Request with a nil or no ID).
|
|
|
|
func unmarshalNotification(s string) (btcjson.Cmd, error) {
|
|
|
|
req, err := btcjson.ParseMarshaledCmd([]byte(s))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Id() != nil {
|
|
|
|
return nil, errors.New("id is non-nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBestBlock gets both the block height and hash of the best block
|
|
|
|
// in the main chain.
|
2014-04-10 23:43:11 +02:00
|
|
|
func GetBestBlock(rpc ServerConn) (*btcws.GetBestBlockResult, *btcjson.Error) {
|
2014-01-30 16:14:02 +01:00
|
|
|
cmd := btcws.NewGetBestBlockCmd(<-NewJSONID)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
|
2014-04-10 23:43:11 +02:00
|
|
|
var resultData btcws.GetBestBlockResult
|
2014-04-09 18:07:09 +02:00
|
|
|
_, jsonErr := response.FinishUnmarshal(&resultData)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return nil, jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
return &resultData, nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlock requests details about a block with the given hash.
|
|
|
|
func GetBlock(rpc ServerConn, blockHash string) (*btcjson.BlockResult, *btcjson.Error) {
|
|
|
|
// NewGetBlockCmd cannot fail with no optargs, so omit the check.
|
|
|
|
cmd, _ := btcjson.NewGetBlockCmd(<-NewJSONID, blockHash)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
|
|
|
|
var resultData btcjson.BlockResult
|
|
|
|
_, jsonErr := response.FinishUnmarshal(&resultData)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return nil, jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
return &resultData, nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentNet requests the network a bitcoin RPC server is running on.
|
|
|
|
func GetCurrentNet(rpc ServerConn) (btcwire.BitcoinNet, *btcjson.Error) {
|
|
|
|
cmd := btcws.NewGetCurrentNetCmd(<-NewJSONID)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
|
|
|
|
var resultData uint32
|
|
|
|
_, jsonErr := response.FinishUnmarshal(&resultData)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return 0, jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
return btcwire.BitcoinNet(resultData), nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBlocks requests blockconnected and blockdisconnected notifications.
|
|
|
|
func NotifyBlocks(rpc ServerConn) *btcjson.Error {
|
|
|
|
cmd := btcws.NewNotifyBlocksCmd(<-NewJSONID)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
_, jsonErr := response.FinishUnmarshal(nil)
|
|
|
|
return jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 02:20:00 +02:00
|
|
|
// NotifyReceived requests notifications for new transactions that spend
|
2014-01-30 16:14:02 +01:00
|
|
|
// to any of the addresses in addrs.
|
2014-04-15 02:20:00 +02:00
|
|
|
func NotifyReceived(rpc ServerConn, addrs []string) *btcjson.Error {
|
|
|
|
cmd := btcws.NewNotifyReceivedCmd(<-NewJSONID, addrs)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
_, jsonErr := response.FinishUnmarshal(nil)
|
|
|
|
return jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifySpent requests notifications for when a transaction is processed which
|
|
|
|
// spends op.
|
2014-05-06 15:38:23 +02:00
|
|
|
func NotifySpent(rpc ServerConn, outpoints []*btcwire.OutPoint) *btcjson.Error {
|
|
|
|
ops := make([]btcws.OutPoint, 0, len(outpoints))
|
|
|
|
for _, op := range outpoints {
|
|
|
|
ops = append(ops, *btcws.NewOutPointFromWire(op))
|
|
|
|
}
|
|
|
|
cmd := btcws.NewNotifySpentCmd(<-NewJSONID, ops)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
_, jsonErr := response.FinishUnmarshal(nil)
|
|
|
|
return jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rescan requests a blockchain rescan for transactions to any number of
|
|
|
|
// addresses and notifications to inform wallet about such transactions.
|
2014-03-21 21:36:42 +01:00
|
|
|
func Rescan(rpc ServerConn, beginBlock int32, addrs []string,
|
|
|
|
outpoints []*btcwire.OutPoint) *btcjson.Error {
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// NewRescanCmd cannot fail with no optargs, so omit the check.
|
2014-04-11 04:01:23 +02:00
|
|
|
ops := make([]btcws.OutPoint, len(outpoints))
|
|
|
|
for i := range outpoints {
|
|
|
|
ops[i] = *btcws.NewOutPointFromWire(outpoints[i])
|
|
|
|
}
|
|
|
|
cmd, _ := btcws.NewRescanCmd(<-NewJSONID, beginBlock, addrs, ops)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
_, jsonErr := response.FinishUnmarshal(nil)
|
|
|
|
return jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendRawTransaction sends a hex-encoded transaction for relay.
|
|
|
|
func SendRawTransaction(rpc ServerConn, hextx string) (txid string, error *btcjson.Error) {
|
|
|
|
// NewSendRawTransactionCmd cannot fail, so omit the check.
|
|
|
|
cmd, _ := btcjson.NewSendRawTransactionCmd(<-NewJSONID, hextx)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
|
|
|
|
var resultData string
|
|
|
|
_, jsonErr := response.FinishUnmarshal(&resultData)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return "", jsonErr
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
return resultData, nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-02-24 20:35:30 +01:00
|
|
|
|
|
|
|
// GetRawTransaction sends the non-verbose version of a getrawtransaction
|
|
|
|
// request to receive the serialized transaction referenced by txsha. If
|
|
|
|
// successful, the transaction is decoded and returned as a btcutil.Tx.
|
|
|
|
func GetRawTransaction(rpc ServerConn, txsha *btcwire.ShaHash) (*btcutil.Tx, *btcjson.Error) {
|
|
|
|
// NewGetRawTransactionCmd cannot fail with no optargs.
|
|
|
|
cmd, _ := btcjson.NewGetRawTransactionCmd(<-NewJSONID, txsha.String())
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
|
|
|
|
var resultData string
|
|
|
|
_, jsonErr := response.FinishUnmarshal(&resultData)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return nil, jsonErr
|
2014-02-24 20:35:30 +01:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
serializedTx, err := hex.DecodeString(resultData)
|
2014-02-24 20:35:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, &btcjson.ErrDecodeHexString
|
|
|
|
}
|
|
|
|
utx, err := btcutil.NewTxFromBytes(serializedTx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &btcjson.ErrDeserialization
|
|
|
|
}
|
|
|
|
return utx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerboseGetRawTransaction sends the verbose version of a getrawtransaction
|
|
|
|
// request to receive details about a transaction.
|
|
|
|
func VerboseGetRawTransaction(rpc ServerConn, txsha *btcwire.ShaHash) (*btcjson.TxRawResult, *btcjson.Error) {
|
|
|
|
// NewGetRawTransactionCmd cannot fail with a single optarg.
|
|
|
|
cmd, _ := btcjson.NewGetRawTransactionCmd(<-NewJSONID, txsha.String(), 1)
|
2014-04-09 18:07:09 +02:00
|
|
|
response := <-rpc.SendRequest(NewServerRequest(cmd))
|
|
|
|
|
|
|
|
var resultData btcjson.TxRawResult
|
|
|
|
_, jsonErr := response.FinishUnmarshal(&resultData)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return nil, jsonErr
|
2014-02-24 20:35:30 +01:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
return &resultData, nil
|
2014-02-24 20:35:30 +01:00
|
|
|
}
|