Skip to content

Commit

Permalink
Merge pull request lightningnetwork#4 from LN-Zap/fix-2396
Browse files Browse the repository at this point in the history
Add output script and amount to return values when polling via RPC GetTransactions and SubscribeTransactions
  • Loading branch information
bjarnemagnussen authored Jun 8, 2021
2 parents 5cfa595 + 6caf593 commit df6a916
Show file tree
Hide file tree
Showing 8 changed files with 3,788 additions and 3,547 deletions.
7,185 changes: 3,642 additions & 3,543 deletions lnrpc/rpc.pb.go

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions lnrpc/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,17 @@ message Utxo {
int64 confirmations = 6;
}

message DestOutput {
// The pkscript in hex
string pk_script = 1;

// The value of the output coin in satoshis
int64 amount = 2;

// Denotes if the output is controlled by the internal wallet
bool is_our_address = 3;
}

message Transaction {
// The transaction hash
string tx_hash = 1;
Expand All @@ -568,11 +579,14 @@ message Transaction {
// Addresses that received funds for this transaction
repeated string dest_addresses = 8;

// Outputs that received funds for this transaction
repeated DestOutput dest_outputs = 9;

// The raw transaction hex.
string raw_tx_hex = 9;
string raw_tx_hex = 10;

// A label that was optionally set on transaction broadcast.
string label = 10;
string label = 11;
}
message GetTransactionsRequest {
/*
Expand Down
26 changes: 26 additions & 0 deletions lnrpc/rpc.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3616,6 +3616,25 @@
}
}
},
"lnrpcDestOutput": {
"type": "object",
"properties": {
"pk_script": {
"type": "string",
"title": "The pkscript in hex"
},
"amount": {
"type": "string",
"format": "int64",
"title": "The value of the output coin in satoshis"
},
"is_our_address": {
"type": "boolean",
"format": "boolean",
"title": "Denotes if the output is controlled by the internal wallet"
}
}
},
"lnrpcDisconnectPeerResponse": {
"type": "object"
},
Expand Down Expand Up @@ -5927,6 +5946,13 @@
},
"title": "Addresses that received funds for this transaction"
},
"dest_outputs": {
"type": "array",
"items": {
"$ref": "#/definitions/lnrpcDestOutput"
},
"title": "Outputs that received funds for this transaction"
},
"raw_tx_hex": {
"type": "string",
"description": "The raw transaction hex."
Expand Down
11 changes: 11 additions & 0 deletions lnrpc/rpc_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetai
destAddresses = append(destAddresses, destAddress.EncodeAddress())
}

// Re-package destination output information.
var destOutputs []*DestOutput
for _, o := range tx.DestOutputs {
destOutputs = append(destOutputs, &DestOutput{
PkScript: hex.EncodeToString(o.PkScript),
Amount: int64(o.Value),
IsOurAddress: o.IsOurAddress,
})
}

// We also get unconfirmed transactions, so BlockHash can be
// nil.
blockHash := ""
Expand All @@ -37,6 +47,7 @@ func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetai
TimeStamp: tx.Timestamp,
TotalFees: tx.TotalFees,
DestAddresses: destAddresses,
DestOutputs: destOutputs,
RawTxHex: hex.EncodeToString(tx.RawTx),
Label: tx.Label,
}
Expand Down
26 changes: 26 additions & 0 deletions lnrpc/walletrpc/walletkit.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,25 @@
"description": "- `p2wkh`: Pay to witness key hash (`WITNESS_PUBKEY_HASH` = 0)\n- `np2wkh`: Pay to nested witness key hash (`NESTED_PUBKEY_HASH` = 1)",
"title": "`AddressType` has to be one of:"
},
"lnrpcDestOutput": {
"type": "object",
"properties": {
"pk_script": {
"type": "string",
"title": "The pkscript in hex"
},
"amount": {
"type": "string",
"format": "int64",
"title": "The value of the output coin in satoshis"
},
"is_our_address": {
"type": "boolean",
"format": "boolean",
"title": "Denotes if the output is controlled by the internal wallet"
}
}
},
"lnrpcOutPoint": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -715,6 +734,13 @@
},
"title": "Addresses that received funds for this transaction"
},
"dest_outputs": {
"type": "array",
"items": {
"$ref": "#/definitions/lnrpcDestOutput"
},
"title": "Outputs that received funds for this transaction"
},
"raw_tx_hex": {
"type": "string",
"description": "The raw transaction hex."
Expand Down
38 changes: 36 additions & 2 deletions lnwallet/btcwallet/btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,19 @@ func minedTransactionsToDetails(
return nil, err
}

// isOurAddress is a map containing the output indices
// controlled by the wallet.
// Note: We make use of the information in `MyOutputs` provided
// by the `wallet.TransactionSummary` structure that holds
// information only if the output is controlled by the wallet.
isOurAddress := make(map[int]bool, len(tx.MyOutputs))
for _, o := range tx.MyOutputs {
isOurAddress[int(o.Index)] = true
}

var destAddresses []btcutil.Address
for _, txOut := range wireTx.TxOut {
var destOutputs []lnwallet.DestOutput
for i, txOut := range wireTx.TxOut {
_, outAddresses, _, err := txscript.ExtractPkScriptAddrs(
txOut.PkScript, chainParams,
)
Expand All @@ -935,6 +946,11 @@ func minedTransactionsToDetails(
}

destAddresses = append(destAddresses, outAddresses...)
destOutputs = append(destOutputs, lnwallet.DestOutput{
PkScript: txOut.PkScript,
Value: btcutil.Amount(txOut.Value),
IsOurAddress: isOurAddress[i],
})
}

txDetail := &lnwallet.TransactionDetail{
Expand All @@ -945,6 +961,7 @@ func minedTransactionsToDetails(
Timestamp: block.Timestamp,
TotalFees: int64(tx.Fee),
DestAddresses: destAddresses,
DestOutputs: destOutputs,
RawTx: tx.Transaction,
Label: tx.Label,
}
Expand Down Expand Up @@ -975,8 +992,19 @@ func unminedTransactionsToDetail(
return nil, err
}

// isOurAddress is a map containing the output indices controlled by
// the wallet.
// Note: We make use of the information in `MyOutputs` provided
// by the `wallet.TransactionSummary` structure that holds information
// only if the output is controlled by the wallet.
isOurAddress := make(map[int]bool, len(summary.MyOutputs))
for _, o := range summary.MyOutputs {
isOurAddress[int(o.Index)] = true
}

var destAddresses []btcutil.Address
for _, txOut := range wireTx.TxOut {
var destOutputs []lnwallet.DestOutput
for i, txOut := range wireTx.TxOut {
_, outAddresses, _, err :=
txscript.ExtractPkScriptAddrs(txOut.PkScript, chainParams)
if err != nil {
Expand All @@ -986,13 +1014,19 @@ func unminedTransactionsToDetail(
}

destAddresses = append(destAddresses, outAddresses...)
destOutputs = append(destOutputs, lnwallet.DestOutput{
PkScript: txOut.PkScript,
Value: btcutil.Amount(txOut.Value),
IsOurAddress: isOurAddress[i],
})
}

txDetail := &lnwallet.TransactionDetail{
Hash: *summary.Hash,
TotalFees: int64(summary.Fee),
Timestamp: summary.Timestamp,
DestAddresses: destAddresses,
DestOutputs: destOutputs,
RawTx: summary.Transaction,
Label: summary.Label,
}
Expand Down
11 changes: 11 additions & 0 deletions lnwallet/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ type Utxo struct {
wire.OutPoint
}

// DestOutput contains additional information on a destination address.
type DestOutput struct {
PkScript []byte
Value btcutil.Amount
IsOurAddress bool
}

// TransactionDetail describes a transaction with either inputs which belong to
// the wallet, or has outputs that pay to the wallet.
type TransactionDetail struct {
Expand Down Expand Up @@ -117,6 +124,10 @@ type TransactionDetail struct {
// DestAddresses are the destinations for a transaction
DestAddresses []btcutil.Address

// DestOutputs contains output data for each destination address, such
// as the output script and amount.
DestOutputs []DestOutput

// RawTx returns the raw serialized transaction.
RawTx []byte

Expand Down
20 changes: 20 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5082,6 +5082,15 @@ func (r *rpcServer) SubscribeTransactions(req *lnrpc.GetTransactionsRequest,
for _, destAddress := range tx.DestAddresses {
destAddresses = append(destAddresses, destAddress.EncodeAddress())
}

destOutputs := make([]*lnrpc.DestOutput, 0, len(tx.DestOutputs))
for _, destOutput := range tx.DestOutputs {
destOutputs = append(destOutputs, &lnrpc.DestOutput{
PkScript: hex.EncodeToString(destOutput.PkScript),
Amount: int64(destOutput.Value),
IsOurAddress: destOutput.IsOurAddress,
})
}
detail := &lnrpc.Transaction{
TxHash: tx.Hash.String(),
Amount: int64(tx.Value),
Expand All @@ -5091,6 +5100,7 @@ func (r *rpcServer) SubscribeTransactions(req *lnrpc.GetTransactionsRequest,
TimeStamp: tx.Timestamp,
TotalFees: tx.TotalFees,
DestAddresses: destAddresses,
DestOutputs: destOutputs,
RawTxHex: hex.EncodeToString(tx.RawTx),
}
if err := updateStream.Send(detail); err != nil {
Expand All @@ -5102,12 +5112,22 @@ func (r *rpcServer) SubscribeTransactions(req *lnrpc.GetTransactionsRequest,
for _, destAddress := range tx.DestAddresses {
destAddresses = append(destAddresses, destAddress.EncodeAddress())
}

destOutputs := make([]*lnrpc.DestOutput, 0, len(tx.DestOutputs))
for _, destOutput := range tx.DestOutputs {
destOutputs = append(destOutputs, &lnrpc.DestOutput{
PkScript: hex.EncodeToString(destOutput.PkScript),
Amount: int64(destOutput.Value),
IsOurAddress: destOutput.IsOurAddress,
})
}
detail := &lnrpc.Transaction{
TxHash: tx.Hash.String(),
Amount: int64(tx.Value),
TimeStamp: tx.Timestamp,
TotalFees: tx.TotalFees,
DestAddresses: destAddresses,
DestOutputs: destOutputs,
RawTxHex: hex.EncodeToString(tx.RawTx),
}
if err := updateStream.Send(detail); err != nil {
Expand Down

0 comments on commit df6a916

Please sign in to comment.