Merge pull request #1933 from yaslama/transactions-addresses

Add addresses to transactions returned by SubscribeTransactions
This commit is contained in:
Wilmer Paulino 2019-07-10 02:34:31 -07:00 committed by GitHub
commit f819002b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 14 deletions

@ -556,6 +556,7 @@ func minedTransactionsToDetails(
// for an unconfirmed transaction to a transaction detail.
func unminedTransactionsToDetail(
summary base.TransactionSummary,
chainParams *chaincfg.Params,
) (*lnwallet.TransactionDetail, error) {
wireTx := &wire.MsgTx{}
@ -565,10 +566,22 @@ func unminedTransactionsToDetail(
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: *summary.Hash,
TotalFees: int64(summary.Fee),
Timestamp: summary.Timestamp,
DestAddresses: destAddresses,
RawTx: summary.Transaction,
}
@ -618,7 +631,7 @@ func (b *BtcWallet) ListTransactionDetails() ([]*lnwallet.TransactionDetail, err
txDetails = append(txDetails, details...)
}
for _, tx := range txns.UnminedTransactions {
detail, err := unminedTransactionsToDetail(tx)
detail, err := unminedTransactionsToDetail(tx, b.netParams)
if err != nil {
return nil, err
}
@ -705,7 +718,9 @@ out:
// notifications for any newly unconfirmed transactions.
go func() {
for _, tx := range txNtfn.UnminedTransactions {
detail, err := unminedTransactionsToDetail(tx)
detail, err := unminedTransactionsToDetail(
tx, t.w.ChainParams(),
)
if err != nil {
continue
}

@ -1181,9 +1181,11 @@ func testListTransactionDetails(miner *rpctest.Harness,
// Next create a transaction paying to an output which isn't under the
// wallet's control.
b := txscript.NewScriptBuilder()
b.AddOp(txscript.OP_0)
outputScript, err := b.Script()
minerAddr, err := miner.NewAddress()
if err != nil {
t.Fatalf("unable to generate address: %v", err)
}
outputScript, err := txscript.PayToAddrScript(minerAddr)
if err != nil {
t.Fatalf("unable to make output script: %v", err)
}
@ -1227,6 +1229,20 @@ func testListTransactionDetails(miner *rpctest.Harness,
t.Fatalf("num confs incorrect, got %v expected %v",
txDetail.NumConfirmations, 0)
}
// We test that each txDetail has destination addresses. This ensures
// that even when we have 0 confirmation transactions, the destination
// addresses are returned.
var match bool
for _, addr := range txDetail.DestAddresses {
if addr.String() == minerAddr.String() {
match = true
break
}
}
if !match {
t.Fatalf("minerAddr: %v should have been a dest addr", minerAddr)
}
}
if !mempoolTxFound {
t.Fatalf("unable to find mempool tx in tx details!")

@ -3576,6 +3576,10 @@ func (r *rpcServer) SubscribeTransactions(req *lnrpc.GetTransactionsRequest,
for {
select {
case tx := <-txClient.ConfirmedTransactions():
destAddresses := make([]string, 0, len(tx.DestAddresses))
for _, destAddress := range tx.DestAddresses {
destAddresses = append(destAddresses, destAddress.EncodeAddress())
}
detail := &lnrpc.Transaction{
TxHash: tx.Hash.String(),
Amount: int64(tx.Value),
@ -3583,6 +3587,7 @@ func (r *rpcServer) SubscribeTransactions(req *lnrpc.GetTransactionsRequest,
BlockHash: tx.BlockHash.String(),
TimeStamp: tx.Timestamp,
TotalFees: tx.TotalFees,
DestAddresses: destAddresses,
RawTxHex: hex.EncodeToString(tx.RawTx),
}
if err := updateStream.Send(detail); err != nil {
@ -3590,11 +3595,16 @@ func (r *rpcServer) SubscribeTransactions(req *lnrpc.GetTransactionsRequest,
}
case tx := <-txClient.UnconfirmedTransactions():
var destAddresses []string
for _, destAddress := range tx.DestAddresses {
destAddresses = append(destAddresses, destAddress.EncodeAddress())
}
detail := &lnrpc.Transaction{
TxHash: tx.Hash.String(),
Amount: int64(tx.Value),
TimeStamp: tx.Timestamp,
TotalFees: tx.TotalFees,
DestAddresses: destAddresses,
RawTxHex: hex.EncodeToString(tx.RawTx),
}
if err := updateStream.Send(detail); err != nil {