5
5
"testing"
6
6
7
7
"github.com/btcsuite/btcd/btcutil"
8
+ "github.com/btcsuite/btcd/txscript"
8
9
"github.com/lightningnetwork/lnd/lnrpc"
10
+ "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
9
11
"github.com/lightningnetwork/lnd/lntest"
10
12
"github.com/lightningnetwork/lnd/lntest/node"
11
13
"github.com/lightningnetwork/lnd/lntest/wait"
@@ -19,7 +21,7 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
19
21
20
22
ok := ht .Run ("set P2WPKH delivery address at open" , func (t * testing.T ) {
21
23
testCoopCloseWithExternalDeliveryImpl (
22
- ht , alice , bob , true ,
24
+ ht , alice , bob , true , false ,
23
25
lnrpc .AddressType_UNUSED_WITNESS_PUBKEY_HASH ,
24
26
)
25
27
})
@@ -30,7 +32,7 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
30
32
31
33
ok = ht .Run ("set P2WPKH delivery address at close" , func (t * testing.T ) {
32
34
testCoopCloseWithExternalDeliveryImpl (
33
- ht , alice , bob , false ,
35
+ ht , alice , bob , false , false ,
34
36
lnrpc .AddressType_UNUSED_WITNESS_PUBKEY_HASH ,
35
37
)
36
38
})
@@ -41,7 +43,7 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
41
43
42
44
ok = ht .Run ("set P2TR delivery address at open" , func (t * testing.T ) {
43
45
testCoopCloseWithExternalDeliveryImpl (
44
- ht , alice , bob , true ,
46
+ ht , alice , bob , true , false ,
45
47
lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
46
48
)
47
49
})
@@ -50,9 +52,31 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
50
52
return
51
53
}
52
54
53
- ht .Run ("set P2TR delivery address at close" , func (t * testing.T ) {
55
+ ok = ht .Run ("set P2TR delivery address at close" , func (t * testing.T ) {
54
56
testCoopCloseWithExternalDeliveryImpl (
55
- ht , alice , bob , false ,
57
+ ht , alice , bob , false , false ,
58
+ lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
59
+ )
60
+ })
61
+ // Abort the test if failed.
62
+ if ! ok {
63
+ return
64
+ }
65
+
66
+ ok = ht .Run ("set imported P2TR address at open" , func (t * testing.T ) {
67
+ testCoopCloseWithExternalDeliveryImpl (
68
+ ht , alice , bob , true , true ,
69
+ lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
70
+ )
71
+ })
72
+ // Abort the test if failed.
73
+ if ! ok {
74
+ return
75
+ }
76
+
77
+ ht .Run ("set imported P2TR address at close" , func (t * testing.T ) {
78
+ testCoopCloseWithExternalDeliveryImpl (
79
+ ht , alice , bob , false , true ,
56
80
lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
57
81
)
58
82
})
@@ -62,17 +86,52 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
62
86
// balance irrespective of whether the delivery address is in LND's wallet or
63
87
// not. Some users set this value to be an address in a different wallet and
64
88
// this should not affect our ability to accurately report the settled balance.
89
+ //
90
+ // If importTapscript is set, it imports imports a Taproot script and internal
91
+ // key to alice LND to make sure it doesn't interfere with channel delivery
92
+ // address. See https://github.com/lightninglabs/loop/issues/923
65
93
func testCoopCloseWithExternalDeliveryImpl (ht * lntest.HarnessTest ,
66
- alice , bob * node.HarnessNode , upfrontShutdown bool ,
94
+ alice , bob * node.HarnessNode , upfrontShutdown , importTapscript bool ,
67
95
deliveryAddressType lnrpc.AddressType ) {
68
96
69
- // Here we generate a final delivery address in bob's wallet but set by
70
- // alice. We do this to ensure that the address is not in alice's LND
71
- // wallet. We already correctly track settled balances when the address
72
- // is in the LND wallet.
73
- addr := bob .RPC .NewAddress (& lnrpc.NewAddressRequest {
74
- Type : deliveryAddressType ,
75
- })
97
+ var addr string
98
+ if importTapscript {
99
+ // Make fake taproot internal public key (equal to the final).
100
+ taprootPubkey := [32 ]byte {1 , 2 , 3 }
101
+ if upfrontShutdown {
102
+ // Make new address for second sub-test not to import
103
+ // the same address twice causing an error.
104
+ taprootPubkey [3 ] = 1
105
+ }
106
+ pkScriptBytes := append (
107
+ []byte {txscript .OP_1 , txscript .OP_DATA_32 },
108
+ taprootPubkey [:]... ,
109
+ )
110
+ pkScript , err := txscript .ParsePkScript (pkScriptBytes )
111
+ require .NoError (ht , err )
112
+ address , err := pkScript .Address (harnessNetParams )
113
+ require .NoError (ht , err )
114
+ addr = address .String ()
115
+
116
+ // Import the taproot address to LND.
117
+ req := & walletrpc.ImportTapscriptRequest {
118
+ InternalPublicKey : taprootPubkey [:],
119
+ Script : & walletrpc.ImportTapscriptRequest_FullKeyOnly {
120
+ FullKeyOnly : true ,
121
+ },
122
+ }
123
+ res := alice .RPC .ImportTapscript (req )
124
+ require .Equal (ht , addr , res .P2TrAddress )
125
+ } else {
126
+ // Here we generate a final delivery address in bob's wallet but
127
+ // set by alice. We do this to ensure that the address is not in
128
+ // alice's LND wallet. We already correctly track settled
129
+ // balances when the address is in the LND wallet.
130
+ res := bob .RPC .NewAddress (& lnrpc.NewAddressRequest {
131
+ Type : deliveryAddressType ,
132
+ })
133
+ addr = res .Address
134
+ }
76
135
77
136
// Prepare for channel open.
78
137
openParams := lntest.OpenChannelParams {
@@ -82,7 +141,7 @@ func testCoopCloseWithExternalDeliveryImpl(ht *lntest.HarnessTest,
82
141
// If we are testing the case where we set it on open then we'll set the
83
142
// upfront shutdown script in the channel open parameters.
84
143
if upfrontShutdown {
85
- openParams .CloseAddress = addr . Address
144
+ openParams .CloseAddress = addr
86
145
}
87
146
88
147
// Open the channel!
@@ -97,7 +156,7 @@ func testCoopCloseWithExternalDeliveryImpl(ht *lntest.HarnessTest,
97
156
// If we are testing the case where we set the delivery address on
98
157
// channel close then we will set it in the channel close parameters.
99
158
if ! upfrontShutdown {
100
- closeParams .DeliveryAddress = addr . Address
159
+ closeParams .DeliveryAddress = addr
101
160
}
102
161
103
162
// Close the channel!
0 commit comments