Skip to content

Commit

Permalink
wallet: add gettransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEk committed Oct 27, 2023
1 parent 07be54b commit e45bff6
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ var (
// to true.
ErrTxLabelExists = errors.New("transaction already labelled")

// ErrNoTx is returned when a transaction can not be found.
ErrNoTx = errors.New("can not find transaction")

// ErrTxUnsigned is returned when a transaction is created in the
// watch-only mode where we can select coins but not sign any inputs.
ErrTxUnsigned = errors.New("watch-only wallet, transaction not signed")
Expand Down Expand Up @@ -2464,6 +2467,56 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier,
return &res, err
}

// GetTransactionResult returns a summary of the transaction along with
// other block properties.
type GetTransactionResult struct {
Summary TransactionSummary
Height int32
BlockHash *chainhash.Hash
Confirmations int32
Timestamp int64
}

// GetTransaction returns detailed data of a transaction given its id. In addition it
// returns properties about its block.
func (w *Wallet) GetTransaction(txHash chainhash.Hash) (*GetTransactionResult,
error) {

var res GetTransactionResult
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 return an error.
if txDetail == nil {
return fmt.Errorf("%w: txid %v", ErrNoTx, txHash)
}

res = GetTransactionResult{
Summary: makeTxSummary(dbtx, w, txDetail),
Timestamp: txDetail.Block.Time.Unix(),
Confirmations: txDetail.Block.Height,
}

// If it is a confirmed transaction we set the corresponding
// block height and hash.
if txDetail.Block.Height != -1 {
res.Height = txDetail.Block.Height
res.BlockHash = &txDetail.Block.Hash
}

return nil
})
if err != nil {
return nil, err
}
return &res, err
}

// AccountResult is a single account result for the AccountsResult type.
type AccountResult struct {
waddrmgr.AccountProperties
Expand Down

0 comments on commit e45bff6

Please sign in to comment.