From fe351af375d15e784c250ddcc7895168813eea78 Mon Sep 17 00:00:00 2001 From: ErikEk Date: Sat, 23 Sep 2023 17:47:20 +0200 Subject: [PATCH] wallet: add gettransaction --- wallet/wallet.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/wallet/wallet.go b/wallet/wallet.go index 0d0de5b28a..98a898633a 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -2464,6 +2464,73 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier, return &res, err } +// GetTransactionResult returns a transaction in the UnminedTransaction +// field if it is unmined, or in the MinedTransaction field as a Block +// structure for a mined transaction. +type GetTransactionResult struct { + MinedTransaction Block + UnminedTransaction TransactionSummary +} + +// GetTransaction returns data of any transaction given its id. A mined +// transaction is returned in a Block structure which records properties about +// the block. +func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult, + error) { + + var ( + bestHeight int32 + blockHash *chainhash.Hash + summary TransactionSummary + ) + err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error { + txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey) + + txDetail, err := w.TxStore.TxDetails(txmgrNs, txHash) + if err != nil { + return err + } + + // If the transaction was not found we can exit early. + if txDetail == nil { + return nil + } + + // Otherwise, we create a summary of the transaction details. + summary = makeTxSummary(dbtx, w, txDetail) + + // If it is a confirmed transaction we set the corresponding + // block height and hash. + if txDetail.Block.Height != -1 { + bestHeight = txDetail.Block.Height + blockHash = &txDetail.Block.Hash + } + + return nil + }) + if err != nil { + return nil, err + } + + var res GetTransactionResult + + // Add the transaction details either as confirmed or + // unconfirmed to the response. + switch blockHash { + case nil: + res.UnminedTransaction = summary + default: + res.MinedTransaction = Block{ + Hash: blockHash, + Height: bestHeight, + Timestamp: summary.Timestamp, + Transactions: []TransactionSummary{summary}, + } + } + + return &res, nil +} + // AccountResult is a single account result for the AccountsResult type. type AccountResult struct { waddrmgr.AccountProperties