From e4a6228752df5f9109b8c428e9970da069e4a3f2 Mon Sep 17 00:00:00 2001 From: nakagawa Date: Fri, 26 Jan 2018 15:20:33 +0900 Subject: [PATCH] rpcclient: Add decodescript support. --- rpcclient/rawtransactions.go | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/rpcclient/rawtransactions.go b/rpcclient/rawtransactions.go index 3bd6e5d3..e886f225 100644 --- a/rpcclient/rawtransactions.go +++ b/rpcclient/rawtransactions.go @@ -624,3 +624,41 @@ func (c *Client) SearchRawTransactionsVerbose(address btcutil.Address, skip, return c.SearchRawTransactionsVerboseAsync(address, skip, count, includePrevOut, reverse, &filterAddrs).Receive() } + +// FutureDecodeScriptResult is a future promise to deliver the result +// of a DecodeScriptAsync RPC invocation (or an applicable error). +type FutureDecodeScriptResult chan *response + +// Receive waits for the response promised by the future and returns information +// about a script given its serialized bytes. +func (r FutureDecodeScriptResult) Receive() (*btcjson.DecodeScriptResult, error) { + res, err := receiveFuture(r) + if err != nil { + return nil, err + } + + // Unmarshal result as a decodescript result object. + var decodeScriptResult btcjson.DecodeScriptResult + err = json.Unmarshal(res, &decodeScriptResult) + if err != nil { + return nil, err + } + + return &decodeScriptResult, nil +} + +// DecodeScriptAsync 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 DecodeScript for the blocking version and more details. +func (c *Client) DecodeScriptAsync(serializedScript []byte) FutureDecodeScriptResult { + scriptHex := hex.EncodeToString(serializedScript) + cmd := btcjson.NewDecodeScriptCmd(scriptHex) + return c.sendCmd(cmd) +} + +// DecodeScript returns information about a script given its serialized bytes. +func (c *Client) DecodeScript(serializedScript []byte) (*btcjson.DecodeScriptResult, error) { + return c.DecodeScriptAsync(serializedScript).Receive() +}