mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-11 01:11:02 +02:00
Merge pull request #6321 from priyanshiiit/walletrpc
lnrpc+lnwallet: add previous_outpoints to listchaintxns
This commit is contained in:
commit
0df880139f
@ -10,6 +10,13 @@
|
|||||||
* [Add minor comment](https://github.com/lightningnetwork/lnd/pull/6559) on
|
* [Add minor comment](https://github.com/lightningnetwork/lnd/pull/6559) on
|
||||||
subscribe/cancel/lookup invoice parameter encoding.
|
subscribe/cancel/lookup invoice parameter encoding.
|
||||||
|
|
||||||
|
## RPC Server
|
||||||
|
|
||||||
|
* [Add previous_outpoints to listchaintxns](https://github.com/lightningnetwork/lnd/pull/6321)
|
||||||
|
|
||||||
|
|
||||||
# Contributors (Alphabetical Order)
|
# Contributors (Alphabetical Order)
|
||||||
|
|
||||||
* Elle Mouton
|
* Elle Mouton
|
||||||
* ErikEk
|
* ErikEk
|
||||||
|
* Priyansh Rastogi
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -686,7 +686,11 @@ message Transaction {
|
|||||||
|
|
||||||
// A label that was optionally set on transaction broadcast.
|
// A label that was optionally set on transaction broadcast.
|
||||||
string label = 10;
|
string label = 10;
|
||||||
|
|
||||||
|
// PreviousOutpoints/Inputs of this transaction.
|
||||||
|
repeated PreviousOutPoint previous_outpoints = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
message GetTransactionsRequest {
|
message GetTransactionsRequest {
|
||||||
/*
|
/*
|
||||||
The height from which to list transactions, inclusive. If this value is
|
The height from which to list transactions, inclusive. If this value is
|
||||||
@ -1007,6 +1011,15 @@ message OutPoint {
|
|||||||
uint32 output_index = 3;
|
uint32 output_index = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message PreviousOutPoint {
|
||||||
|
// The outpoint in format txid:n.
|
||||||
|
string outpoint = 1;
|
||||||
|
|
||||||
|
// Denotes if the outpoint is controlled by the internal wallet.
|
||||||
|
// The flag will only detect p2wkh, np2wkh and p2tr inputs as its own.
|
||||||
|
bool is_our_output = 2;
|
||||||
|
}
|
||||||
|
|
||||||
message LightningAddress {
|
message LightningAddress {
|
||||||
// The identity pubkey of the Lightning node.
|
// The identity pubkey of the Lightning node.
|
||||||
string pubkey = 1;
|
string pubkey = 1;
|
||||||
|
@ -5879,6 +5879,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"lnrpcPreviousOutPoint": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"outpoint": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The outpoint in format txid:n."
|
||||||
|
},
|
||||||
|
"is_our_output": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Denotes if the outpoint is controlled by the internal wallet.\nThe flag will only detect p2wkh, np2wkh and p2tr inputs as its own."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"lnrpcPsbtShim": {
|
"lnrpcPsbtShim": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@ -6501,6 +6514,13 @@
|
|||||||
"label": {
|
"label": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "A label that was optionally set on transaction broadcast."
|
"description": "A label that was optionally set on transaction broadcast."
|
||||||
|
},
|
||||||
|
"previous_outpoints": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/lnrpcPreviousOutPoint"
|
||||||
|
},
|
||||||
|
"description": "PreviousOutpoints/Inputs of this transaction."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -45,6 +45,14 @@ func RPCTransaction(tx *lnwallet.TransactionDetail) *Transaction {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previousOutpoints := make([]*PreviousOutPoint, len(tx.PreviousOutpoints))
|
||||||
|
for idx, previousOutPoint := range tx.PreviousOutpoints {
|
||||||
|
previousOutpoints[idx] = &PreviousOutPoint{
|
||||||
|
Outpoint: previousOutPoint.OutPoint,
|
||||||
|
IsOurOutput: previousOutPoint.IsOurOutput,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// We also get unconfirmed transactions, so BlockHash can be nil.
|
// We also get unconfirmed transactions, so BlockHash can be nil.
|
||||||
blockHash := ""
|
blockHash := ""
|
||||||
if tx.BlockHash != nil {
|
if tx.BlockHash != nil {
|
||||||
@ -63,6 +71,7 @@ func RPCTransaction(tx *lnwallet.TransactionDetail) *Transaction {
|
|||||||
OutputDetails: outputDetails,
|
OutputDetails: outputDetails,
|
||||||
RawTxHex: hex.EncodeToString(tx.RawTx),
|
RawTxHex: hex.EncodeToString(tx.RawTx),
|
||||||
Label: tx.Label,
|
Label: tx.Label,
|
||||||
|
PreviousOutpoints: previousOutpoints,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -769,6 +769,19 @@
|
|||||||
],
|
],
|
||||||
"default": "SCRIPT_TYPE_PUBKEY_HASH"
|
"default": "SCRIPT_TYPE_PUBKEY_HASH"
|
||||||
},
|
},
|
||||||
|
"lnrpcPreviousOutPoint": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"outpoint": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The outpoint in format txid:n."
|
||||||
|
},
|
||||||
|
"is_our_output": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Denotes if the outpoint is controlled by the internal wallet.\nThe flag will only detect p2wkh, np2wkh and p2tr inputs as its own."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"lnrpcTransaction": {
|
"lnrpcTransaction": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@ -826,6 +839,13 @@
|
|||||||
"label": {
|
"label": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "A label that was optionally set on transaction broadcast."
|
"description": "A label that was optionally set on transaction broadcast."
|
||||||
|
},
|
||||||
|
"previous_outpoints": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/lnrpcPreviousOutPoint"
|
||||||
|
},
|
||||||
|
"description": "PreviousOutpoints/Inputs of this transaction."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -199,6 +199,18 @@ func testOnchainFundRecovery(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
fn(node)
|
fn(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the previous outpoints are set correctly.
|
||||||
|
req := &lnrpc.GetTransactionsRequest{
|
||||||
|
StartHeight: 0,
|
||||||
|
EndHeight: -1,
|
||||||
|
}
|
||||||
|
txDetails, err := node.GetTransactions(ctxb, req)
|
||||||
|
require.NoError(t.t, err)
|
||||||
|
|
||||||
|
for _, tx := range txDetails.Transactions {
|
||||||
|
require.Greater(t.t, len(tx.PreviousOutpoints), 0)
|
||||||
|
}
|
||||||
|
|
||||||
// Lastly, shutdown this Carol so we can move on to the next
|
// Lastly, shutdown this Carol so we can move on to the next
|
||||||
// restoration.
|
// restoration.
|
||||||
shutdownAndAssert(net, t, node)
|
shutdownAndAssert(net, t, node)
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/btcsuite/btcwallet/chain"
|
"github.com/btcsuite/btcwallet/chain"
|
||||||
"github.com/btcsuite/btcwallet/waddrmgr"
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
||||||
|
"github.com/btcsuite/btcwallet/wallet"
|
||||||
base "github.com/btcsuite/btcwallet/wallet"
|
base "github.com/btcsuite/btcwallet/wallet"
|
||||||
"github.com/btcsuite/btcwallet/wallet/txauthor"
|
"github.com/btcsuite/btcwallet/wallet/txauthor"
|
||||||
"github.com/btcsuite/btcwallet/wallet/txrules"
|
"github.com/btcsuite/btcwallet/wallet/txrules"
|
||||||
@ -1104,6 +1105,32 @@ func extractBalanceDelta(
|
|||||||
return balanceDelta, nil
|
return balanceDelta, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getPreviousOutpoints is a helper function which gets the previous
|
||||||
|
// outpoints of a transaction.
|
||||||
|
func getPreviousOutpoints(wireTx *wire.MsgTx,
|
||||||
|
myInputs []wallet.TransactionSummaryInput) []lnwallet.PreviousOutPoint {
|
||||||
|
|
||||||
|
// isOurOutput is a map containing the output indices
|
||||||
|
// controlled by the wallet.
|
||||||
|
// Note: We make use of the information in `myInputs` provided
|
||||||
|
// by the `wallet.TransactionSummary` structure that holds
|
||||||
|
// information only if the input/previous_output is controlled by the wallet.
|
||||||
|
isOurOutput := make(map[uint32]bool, len(myInputs))
|
||||||
|
for _, myInput := range myInputs {
|
||||||
|
isOurOutput[myInput.Index] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
previousOutpoints := make([]lnwallet.PreviousOutPoint, len(wireTx.TxIn))
|
||||||
|
for idx, txIn := range wireTx.TxIn {
|
||||||
|
previousOutpoints[idx] = lnwallet.PreviousOutPoint{
|
||||||
|
OutPoint: txIn.PreviousOutPoint.String(),
|
||||||
|
IsOurOutput: isOurOutput[uint32(idx)],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return previousOutpoints
|
||||||
|
}
|
||||||
|
|
||||||
// minedTransactionsToDetails is a helper function which converts a summary
|
// minedTransactionsToDetails is a helper function which converts a summary
|
||||||
// information about mined transactions to a TransactionDetail.
|
// information about mined transactions to a TransactionDetail.
|
||||||
func minedTransactionsToDetails(
|
func minedTransactionsToDetails(
|
||||||
@ -1152,6 +1179,8 @@ func minedTransactionsToDetails(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previousOutpoints := getPreviousOutpoints(wireTx, tx.MyInputs)
|
||||||
|
|
||||||
txDetail := &lnwallet.TransactionDetail{
|
txDetail := &lnwallet.TransactionDetail{
|
||||||
Hash: *tx.Hash,
|
Hash: *tx.Hash,
|
||||||
NumConfirmations: currentHeight - block.Height + 1,
|
NumConfirmations: currentHeight - block.Height + 1,
|
||||||
@ -1162,6 +1191,7 @@ func minedTransactionsToDetails(
|
|||||||
OutputDetails: outputDetails,
|
OutputDetails: outputDetails,
|
||||||
RawTx: tx.Transaction,
|
RawTx: tx.Transaction,
|
||||||
Label: tx.Label,
|
Label: tx.Label,
|
||||||
|
PreviousOutpoints: previousOutpoints,
|
||||||
}
|
}
|
||||||
|
|
||||||
balanceDelta, err := extractBalanceDelta(tx, wireTx)
|
balanceDelta, err := extractBalanceDelta(tx, wireTx)
|
||||||
@ -1221,6 +1251,8 @@ func unminedTransactionsToDetail(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previousOutpoints := getPreviousOutpoints(wireTx, summary.MyInputs)
|
||||||
|
|
||||||
txDetail := &lnwallet.TransactionDetail{
|
txDetail := &lnwallet.TransactionDetail{
|
||||||
Hash: *summary.Hash,
|
Hash: *summary.Hash,
|
||||||
TotalFees: int64(summary.Fee),
|
TotalFees: int64(summary.Fee),
|
||||||
@ -1228,6 +1260,7 @@ func unminedTransactionsToDetail(
|
|||||||
OutputDetails: outputDetails,
|
OutputDetails: outputDetails,
|
||||||
RawTx: summary.Transaction,
|
RawTx: summary.Transaction,
|
||||||
Label: summary.Label,
|
Label: summary.Label,
|
||||||
|
PreviousOutpoints: previousOutpoints,
|
||||||
}
|
}
|
||||||
|
|
||||||
balanceDelta, err := extractBalanceDelta(summary, wireTx)
|
balanceDelta, err := extractBalanceDelta(summary, wireTx)
|
||||||
|
134
lnwallet/btcwallet/btcwallet_test.go
Normal file
134
lnwallet/btcwallet/btcwallet_test.go
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
package btcwallet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
"github.com/btcsuite/btcwallet/wallet"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
type previousOutpointsTest struct {
|
||||||
|
name string
|
||||||
|
tx *wire.MsgTx
|
||||||
|
myInputs []wallet.TransactionSummaryInput
|
||||||
|
expRes []lnwallet.PreviousOutPoint
|
||||||
|
}
|
||||||
|
|
||||||
|
var previousOutpointsTests = []previousOutpointsTest{{
|
||||||
|
name: "both outpoints are wallet controlled",
|
||||||
|
tx: &wire.MsgTx{
|
||||||
|
TxIn: []*wire.TxIn{{
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 0},
|
||||||
|
}, {
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 1},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
myInputs: []wallet.TransactionSummaryInput{{
|
||||||
|
Index: 0,
|
||||||
|
}, {
|
||||||
|
Index: 1,
|
||||||
|
}},
|
||||||
|
expRes: []lnwallet.PreviousOutPoint{{
|
||||||
|
OutPoint: wire.OutPoint{Index: 0}.String(),
|
||||||
|
IsOurOutput: true,
|
||||||
|
}, {
|
||||||
|
OutPoint: wire.OutPoint{Index: 1}.String(),
|
||||||
|
IsOurOutput: true,
|
||||||
|
}},
|
||||||
|
}, {
|
||||||
|
name: "only one outpoint is wallet controlled",
|
||||||
|
tx: &wire.MsgTx{
|
||||||
|
TxIn: []*wire.TxIn{{
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 0},
|
||||||
|
}, {
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 1},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
myInputs: []wallet.TransactionSummaryInput{{
|
||||||
|
Index: 0,
|
||||||
|
}, {
|
||||||
|
Index: 2,
|
||||||
|
}},
|
||||||
|
expRes: []lnwallet.PreviousOutPoint{{
|
||||||
|
OutPoint: wire.OutPoint{Index: 0}.String(),
|
||||||
|
IsOurOutput: true,
|
||||||
|
}, {
|
||||||
|
OutPoint: wire.OutPoint{Index: 1}.String(),
|
||||||
|
IsOurOutput: false,
|
||||||
|
}},
|
||||||
|
}, {
|
||||||
|
name: "no outpoint is wallet controlled",
|
||||||
|
tx: &wire.MsgTx{
|
||||||
|
TxIn: []*wire.TxIn{{
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 0},
|
||||||
|
}, {
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 1},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
myInputs: []wallet.TransactionSummaryInput{{
|
||||||
|
Index: 2,
|
||||||
|
}, {
|
||||||
|
Index: 3,
|
||||||
|
}},
|
||||||
|
expRes: []lnwallet.PreviousOutPoint{{
|
||||||
|
OutPoint: wire.OutPoint{Index: 0}.String(),
|
||||||
|
IsOurOutput: false,
|
||||||
|
}, {
|
||||||
|
OutPoint: wire.OutPoint{Index: 1}.String(),
|
||||||
|
IsOurOutput: false,
|
||||||
|
}},
|
||||||
|
}, {
|
||||||
|
name: "tx is empty",
|
||||||
|
tx: &wire.MsgTx{
|
||||||
|
TxIn: []*wire.TxIn{},
|
||||||
|
},
|
||||||
|
myInputs: []wallet.TransactionSummaryInput{{
|
||||||
|
Index: 2,
|
||||||
|
}, {
|
||||||
|
Index: 3,
|
||||||
|
}},
|
||||||
|
expRes: []lnwallet.PreviousOutPoint{},
|
||||||
|
}, {
|
||||||
|
name: "wallet controlled input set is empty",
|
||||||
|
tx: &wire.MsgTx{
|
||||||
|
TxIn: []*wire.TxIn{{
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 0},
|
||||||
|
}, {
|
||||||
|
PreviousOutPoint: wire.OutPoint{Index: 1},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
myInputs: []wallet.TransactionSummaryInput{},
|
||||||
|
expRes: []lnwallet.PreviousOutPoint{{
|
||||||
|
OutPoint: wire.OutPoint{Index: 0}.String(),
|
||||||
|
IsOurOutput: false,
|
||||||
|
}, {
|
||||||
|
OutPoint: wire.OutPoint{Index: 1}.String(),
|
||||||
|
IsOurOutput: false,
|
||||||
|
}},
|
||||||
|
}}
|
||||||
|
|
||||||
|
// TestPreviousOutpoints tests if we are able to get the previous
|
||||||
|
// outpoints correctly.
|
||||||
|
func TestPreviousOutpoints(t *testing.T) {
|
||||||
|
for _, test := range previousOutpointsTests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
respOutpoints := getPreviousOutpoints(
|
||||||
|
test.tx, test.myInputs,
|
||||||
|
)
|
||||||
|
|
||||||
|
for idx, respOutpoint := range respOutpoints {
|
||||||
|
expRes := test.expRes[idx]
|
||||||
|
require.Equal(
|
||||||
|
t, expRes.OutPoint,
|
||||||
|
respOutpoint.OutPoint,
|
||||||
|
)
|
||||||
|
require.Equal(
|
||||||
|
t, expRes.IsOurOutput,
|
||||||
|
respOutpoint.IsOurOutput,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -98,6 +98,17 @@ type OutputDetail struct {
|
|||||||
IsOurAddress bool
|
IsOurAddress bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PreviousOutPoint contains information about the previous outpoint.
|
||||||
|
type PreviousOutPoint struct {
|
||||||
|
// OutPoint is the transaction out point in the format txid:n.
|
||||||
|
OutPoint string
|
||||||
|
|
||||||
|
// IsOurOutput denotes if the previous output is controlled by the
|
||||||
|
// internal wallet. The flag will only detect p2wkh, np2wkh and p2tr
|
||||||
|
// inputs as its own.
|
||||||
|
IsOurOutput bool
|
||||||
|
}
|
||||||
|
|
||||||
// TransactionDetail describes a transaction with either inputs which belong to
|
// TransactionDetail describes a transaction with either inputs which belong to
|
||||||
// the wallet, or has outputs that pay to the wallet.
|
// the wallet, or has outputs that pay to the wallet.
|
||||||
type TransactionDetail struct {
|
type TransactionDetail struct {
|
||||||
@ -141,6 +152,9 @@ type TransactionDetail struct {
|
|||||||
|
|
||||||
// Label is an optional transaction label.
|
// Label is an optional transaction label.
|
||||||
Label string
|
Label string
|
||||||
|
|
||||||
|
// PreviousOutpoints are the inputs for a transaction.
|
||||||
|
PreviousOutpoints []PreviousOutPoint
|
||||||
}
|
}
|
||||||
|
|
||||||
// TransactionSubscription is an interface which describes an object capable of
|
// TransactionSubscription is an interface which describes an object capable of
|
||||||
|
Loading…
x
Reference in New Issue
Block a user