From 714de3f3c74d7719cd95ceb8a9c68cb7cf74c832 Mon Sep 17 00:00:00 2001 From: Torkel Rogstad Date: Thu, 2 Apr 2020 11:01:56 +0200 Subject: [PATCH] rpcclient: serialize nil inputs to empty list --- btcjson/chainsvrcmds.go | 9 +++++++-- btcjson/chainsvrcmds_test.go | 15 +++++++++++++++ rpcclient/rawtransactions.go | 3 ++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/btcjson/chainsvrcmds.go b/btcjson/chainsvrcmds.go index 2f3069fd..1345f8f7 100644 --- a/btcjson/chainsvrcmds.go +++ b/btcjson/chainsvrcmds.go @@ -63,10 +63,15 @@ type CreateRawTransactionCmd struct { // NewCreateRawTransactionCmd returns a new instance which can be used to issue // a createrawtransaction JSON-RPC command. // -// Amounts are in BTC. +// Amounts are in BTC. Passing in nil and the empty slice as inputs is equivalent, +// both gets interpreted as the empty slice. func NewCreateRawTransactionCmd(inputs []TransactionInput, amounts map[string]float64, lockTime *int64) *CreateRawTransactionCmd { - + // to make sure we're serializing this to the empty list and not null, we + // explicitly initialize the list + if inputs == nil { + inputs = []TransactionInput{} + } return &CreateRawTransactionCmd{ Inputs: inputs, Amounts: amounts, diff --git a/btcjson/chainsvrcmds_test.go b/btcjson/chainsvrcmds_test.go index 8f3fd455..dd997ce5 100644 --- a/btcjson/chainsvrcmds_test.go +++ b/btcjson/chainsvrcmds_test.go @@ -60,6 +60,21 @@ func TestChainSvrCmds(t *testing.T) { Amounts: map[string]float64{"456": .0123}, }, }, + { + name: "createrawtransaction - no inputs", + newCmd: func() (interface{}, error) { + return btcjson.NewCmd("createrawtransaction", `[]`, `{"456":0.0123}`) + }, + staticCmd: func() interface{} { + amounts := map[string]float64{"456": .0123} + return btcjson.NewCreateRawTransactionCmd(nil, amounts, nil) + }, + marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[],{"456":0.0123}],"id":1}`, + unmarshalled: &btcjson.CreateRawTransactionCmd{ + Inputs: []btcjson.TransactionInput{}, + Amounts: map[string]float64{"456": .0123}, + }, + }, { name: "createrawtransaction optional", newCmd: func() (interface{}, error) { diff --git a/rpcclient/rawtransactions.go b/rpcclient/rawtransactions.go index 23754d96..63a08664 100644 --- a/rpcclient/rawtransactions.go +++ b/rpcclient/rawtransactions.go @@ -261,7 +261,8 @@ func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput, } // CreateRawTransaction returns a new transaction spending the provided inputs -// and sending to the provided addresses. +// and sending to the provided addresses. If the inputs are either nil or an +// empty slice, it is interpreted as an empty slice. func (c *Client) CreateRawTransaction(inputs []btcjson.TransactionInput, amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) (*wire.MsgTx, error) {