From 6ae7d275ab43166b6422606d1ea5d68a94fbbec4 Mon Sep 17 00:00:00 2001 From: Micah Lerner Date: Wed, 6 Dec 2017 09:19:37 -0800 Subject: [PATCH] lnwallet: Add destination addresses to listchaintxns --- lnwallet/btcwallet/btcwallet.go | 58 ++++++++++++++++++++++++--------- lnwallet/interface.go | 3 ++ rpcserver.go | 6 ++++ 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/lnwallet/btcwallet/btcwallet.go b/lnwallet/btcwallet/btcwallet.go index 6c53db07f..a89bc71ef 100644 --- a/lnwallet/btcwallet/btcwallet.go +++ b/lnwallet/btcwallet/btcwallet.go @@ -395,13 +395,10 @@ func (b *BtcWallet) PublishTransaction(tx *wire.MsgTx) error { // extractBalanceDelta extracts the net balance delta from the PoV of the // wallet given a TransactionSummary. -func extractBalanceDelta(txSummary base.TransactionSummary) (btcutil.Amount, error) { - tx := wire.NewMsgTx(1) - txReader := bytes.NewReader(txSummary.Transaction) - if err := tx.Deserialize(txReader); err != nil { - return -1, nil - } - +func extractBalanceDelta( + txSummary base.TransactionSummary, + tx *wire.MsgTx, +) (btcutil.Amount, error) { // For each input we debit the wallet's outflow for this transaction, // and for each output we credit the wallet's inflow for this // transaction. @@ -418,11 +415,32 @@ func extractBalanceDelta(txSummary base.TransactionSummary) (btcutil.Amount, err // minedTransactionsToDetails is a helper function which converts a summary // information about mined transactions to a TransactionDetail. -func minedTransactionsToDetails(currentHeight int32, - block base.Block) ([]*lnwallet.TransactionDetail, error) { +func minedTransactionsToDetails( + currentHeight int32, + block base.Block, + chainParams *chaincfg.Params, +) ([]*lnwallet.TransactionDetail, error) { details := make([]*lnwallet.TransactionDetail, 0, len(block.Transactions)) for _, tx := range block.Transactions { + wireTx := &wire.MsgTx{} + txReader := bytes.NewReader(tx.Transaction) + + if err := wireTx.Deserialize(txReader); err != nil { + return nil, err + } + + var destAddresses []btcutil.Address + for _, txOut := range wireTx.TxOut { + _, outAddresses, _, err := + txscript.ExtractPkScriptAddrs(txOut.PkScript, chainParams) + if err != nil { + return nil, err + } + + destAddresses = append(destAddresses, outAddresses...) + } + txDetail := &lnwallet.TransactionDetail{ Hash: *tx.Hash, NumConfirmations: currentHeight - block.Height + 1, @@ -430,9 +448,10 @@ func minedTransactionsToDetails(currentHeight int32, BlockHeight: block.Height, Timestamp: block.Timestamp, TotalFees: int64(tx.Fee), + DestAddresses: destAddresses, } - balanceDelta, err := extractBalanceDelta(tx) + balanceDelta, err := extractBalanceDelta(tx, wireTx) if err != nil { return nil, err } @@ -444,16 +463,25 @@ func minedTransactionsToDetails(currentHeight int32, return details, nil } -// unminedTransactionsToDetail is a helper funciton which converts a summary +// unminedTransactionsToDetail is a helper function which converts a summary // for a unconfirmed transaction to a transaction detail. -func unminedTransactionsToDetail(summary base.TransactionSummary) (*lnwallet.TransactionDetail, error) { +func unminedTransactionsToDetail( + summary base.TransactionSummary, +) (*lnwallet.TransactionDetail, error) { + wireTx := &wire.MsgTx{} + txReader := bytes.NewReader(summary.Transaction) + + if err := wireTx.Deserialize(txReader); err != nil { + return nil, err + } + txDetail := &lnwallet.TransactionDetail{ Hash: *summary.Hash, TotalFees: int64(summary.Fee), Timestamp: summary.Timestamp, } - balanceDelta, err := extractBalanceDelta(summary) + balanceDelta, err := extractBalanceDelta(summary, wireTx) if err != nil { return nil, err } @@ -487,7 +515,7 @@ func (b *BtcWallet) ListTransactionDetails() ([]*lnwallet.TransactionDetail, err // TransactionDetail which re-packages the data returned by the base // wallet. for _, blockPackage := range txns.MinedTransactions { - details, err := minedTransactionsToDetails(currentHeight, blockPackage) + details, err := minedTransactionsToDetails(currentHeight, blockPackage, b.netParams) if err != nil { return nil, err } @@ -562,7 +590,7 @@ out: // notifications for any newly confirmed transactions. go func() { for _, block := range txNtfn.AttachedBlocks { - details, err := minedTransactionsToDetails(currentHeight, block) + details, err := minedTransactionsToDetails(currentHeight, block, t.w.ChainParams()) if err != nil { continue } diff --git a/lnwallet/interface.go b/lnwallet/interface.go index af86ebf30..efc3a26c5 100644 --- a/lnwallet/interface.go +++ b/lnwallet/interface.go @@ -79,6 +79,9 @@ type TransactionDetail struct { // TotalFees is the total fee in satoshis paid by this transaction. TotalFees int64 + + // DestAddresses are the destinations for a transaction + DestAddresses []btcutil.Address } // TransactionSubscription is an interface which describes an object capable of diff --git a/rpcserver.go b/rpcserver.go index f6c857833..c8d518f9c 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2349,6 +2349,11 @@ func (r *rpcServer) GetTransactions(ctx context.Context, Transactions: make([]*lnrpc.Transaction, len(transactions)), } for i, tx := range transactions { + var destAddresses []string + for _, destAddress := range tx.DestAddresses { + destAddresses = append(destAddresses, destAddress.EncodeAddress()) + } + txDetails.Transactions[i] = &lnrpc.Transaction{ TxHash: tx.Hash.String(), Amount: int64(tx.Value), @@ -2357,6 +2362,7 @@ func (r *rpcServer) GetTransactions(ctx context.Context, BlockHeight: tx.BlockHeight, TimeStamp: tx.Timestamp, TotalFees: tx.TotalFees, + DestAddresses: destAddresses, } }