From a9d8c9e0d3a2be1eafa29708ea36a5d399a6722c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 3 Mar 2020 20:43:57 +0300 Subject: [PATCH] rpc/client: implement GetRawTransaction, fix #586 --- pkg/rpc/client/doc.go | 2 +- pkg/rpc/client/rpc.go | 50 ++++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/pkg/rpc/client/doc.go b/pkg/rpc/client/doc.go index 916420095..1df4f0682 100644 --- a/pkg/rpc/client/doc.go +++ b/pkg/rpc/client/doc.go @@ -21,6 +21,7 @@ Supported methods getaccountstate getblock getclaimable + getrawtransaction getunspents invoke invokefunction @@ -47,7 +48,6 @@ Unsupported methods getnewaddress getpeers getrawmempool - getrawtransaction getstorage gettransactionheight gettxout diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index 166271a26..beeef25cf 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -93,6 +93,43 @@ func (c *Client) GetClaimable(address string) (*result.ClaimableInfo, error) { return resp, nil } +// GetRawTransaction returns a transaction by hash. +func (c *Client) GetRawTransaction(hash util.Uint256) (*transaction.Transaction, error) { + var ( + params = request.NewRawParams(hash.StringLE()) + resp string + err error + ) + if err = c.performRequest("getrawtransaction", params, &resp); err != nil { + return nil, err + } + txBytes, err := hex.DecodeString(resp) + if err != nil { + return nil, err + } + r := io.NewBinReaderFromBuf(txBytes) + tx := new(transaction.Transaction) + tx.DecodeBinary(r) + if r.Err != nil { + return nil, r.Err + } + return tx, nil +} + +// GetRawTransactionVerbose returns a transaction wrapper with additional +// metadata by transaction's hash. +func (c *Client) GetRawTransactionVerbose(hash util.Uint256) (*result.TransactionOutputRaw, error) { + var ( + params = request.NewRawParams(hash.StringLE(), 1) + resp = &result.TransactionOutputRaw{} + err error + ) + if err = c.performRequest("getrawtransaction", params, &resp); err != nil { + return nil, err + } + return resp, nil +} + // GetUnspents returns UTXOs for the given NEO account. func (c *Client) GetUnspents(address string) (*result.Unspents, error) { var ( @@ -145,19 +182,6 @@ func (c *Client) Invoke(script string, params []smartcontract.Parameter) (*resul return resp, nil } -// getRawTransaction queries a transaction by hash. -// missing output wrapper at the moment, thus commented out -// func (c *Client) getRawTransaction(hash string, verbose bool) (*response, error) { -// var ( -// params = request.NewRawParams(hash, verbose) -// resp = &response{} -// ) -// if err := c.performRequest("getrawtransaction", params, resp); err != nil { -// return nil, err -// } -// return resp, nil -// } - // SendRawTransaction broadcasts a transaction over the NEO network. // The given hex string needs to be signed with a keypair. // When the result of the response object is true, the TX has successfully