4
4
"fmt"
5
5
6
6
"github.com/btcsuite/btcd/btcutil"
7
+ "github.com/btcsuite/btcd/txscript"
7
8
"github.com/lightningnetwork/lnd/lnrpc"
9
+ "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
8
10
"github.com/lightningnetwork/lnd/lntest"
9
11
"github.com/lightningnetwork/lnd/lntest/wait"
10
12
"github.com/stretchr/testify/require"
@@ -15,7 +17,7 @@ var coopCloseWithExternalTestCases = []*lntest.TestCase{
15
17
Name : "set P2WPKH delivery address at open" ,
16
18
TestFunc : func (ht * lntest.HarnessTest ) {
17
19
testCoopCloseWithExternalDelivery (
18
- ht , true ,
20
+ ht , true , false , false ,
19
21
lnrpc .AddressType_UNUSED_WITNESS_PUBKEY_HASH ,
20
22
)
21
23
},
@@ -24,7 +26,7 @@ var coopCloseWithExternalTestCases = []*lntest.TestCase{
24
26
Name : "set P2WPKH delivery address at close" ,
25
27
TestFunc : func (ht * lntest.HarnessTest ) {
26
28
testCoopCloseWithExternalDelivery (
27
- ht , false ,
29
+ ht , false , false , false ,
28
30
lnrpc .AddressType_UNUSED_WITNESS_PUBKEY_HASH ,
29
31
)
30
32
},
@@ -33,7 +35,7 @@ var coopCloseWithExternalTestCases = []*lntest.TestCase{
33
35
Name : "set P2TR delivery address at open" ,
34
36
TestFunc : func (ht * lntest.HarnessTest ) {
35
37
testCoopCloseWithExternalDelivery (
36
- ht , true ,
38
+ ht , true , false , false ,
37
39
lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
38
40
)
39
41
},
@@ -42,7 +44,61 @@ var coopCloseWithExternalTestCases = []*lntest.TestCase{
42
44
Name : "set P2TR delivery address at close" ,
43
45
TestFunc : func (ht * lntest.HarnessTest ) {
44
46
testCoopCloseWithExternalDelivery (
45
- ht , false ,
47
+ ht , false , false , false ,
48
+ lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
49
+ )
50
+ },
51
+ },
52
+ {
53
+ Name : "set imported P2TR address (ImportTapscript) at open" ,
54
+ TestFunc : func (ht * lntest.HarnessTest ) {
55
+ testCoopCloseWithExternalDelivery (
56
+ ht , true , true , false ,
57
+ lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
58
+ )
59
+ },
60
+ },
61
+ {
62
+ Name : "set imported P2TR address (ImportTapscript) at close" ,
63
+ TestFunc : func (ht * lntest.HarnessTest ) {
64
+ testCoopCloseWithExternalDelivery (
65
+ ht , false , true , false ,
66
+ lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
67
+ )
68
+ },
69
+ },
70
+ {
71
+ Name : "set imported P2WPKH address at open" ,
72
+ TestFunc : func (ht * lntest.HarnessTest ) {
73
+ testCoopCloseWithExternalDelivery (
74
+ ht , true , false , true ,
75
+ lnrpc .AddressType_UNUSED_WITNESS_PUBKEY_HASH ,
76
+ )
77
+ },
78
+ },
79
+ {
80
+ Name : "set imported P2WPKH address at close" ,
81
+ TestFunc : func (ht * lntest.HarnessTest ) {
82
+ testCoopCloseWithExternalDelivery (
83
+ ht , false , false , true ,
84
+ lnrpc .AddressType_UNUSED_WITNESS_PUBKEY_HASH ,
85
+ )
86
+ },
87
+ },
88
+ {
89
+ Name : "set imported P2TR address (ImportPublicKey) at open" ,
90
+ TestFunc : func (ht * lntest.HarnessTest ) {
91
+ testCoopCloseWithExternalDelivery (
92
+ ht , true , false , true ,
93
+ lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
94
+ )
95
+ },
96
+ },
97
+ {
98
+ Name : "set imported P2TR address (ImportPublicKey) at close" ,
99
+ TestFunc : func (ht * lntest.HarnessTest ) {
100
+ testCoopCloseWithExternalDelivery (
101
+ ht , false , false , true ,
46
102
lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY ,
47
103
)
48
104
},
@@ -53,20 +109,102 @@ var coopCloseWithExternalTestCases = []*lntest.TestCase{
53
109
// balance irrespective of whether the delivery address is in LND's wallet or
54
110
// not. Some users set this value to be an address in a different wallet and
55
111
// this should not affect our ability to accurately report the settled balance.
112
+ //
113
+ // If importTapscript is set, it imports a Taproot script and internal key to
114
+ // alice LND using ImportTapscript to make sure it doesn't interfere with
115
+ // delivery address. See https://github.com/lightninglabs/loop/issues/923
116
+ // If importPubkey is set, the address is imported using ImportPublicKey.
56
117
func testCoopCloseWithExternalDelivery (ht * lntest.HarnessTest ,
57
- upfrontShutdown bool , deliveryAddressType lnrpc.AddressType ) {
118
+ upfrontShutdown , importTapscript , importPubkey bool ,
119
+ deliveryAddressType lnrpc.AddressType ) {
58
120
59
121
alice := ht .NewNodeWithCoins ("Alice" , nil )
60
122
bob := ht .NewNodeWithCoins ("bob" , nil )
61
123
ht .ConnectNodes (alice , bob )
62
124
125
+ // Make fake taproot internal public key (equal to the final).
126
+ // This is only used if importTapscript or importPubkey is set.
127
+ taprootPubkey := [32 ]byte {1 , 2 , 3 }
128
+ if upfrontShutdown {
129
+ // Make new address for second sub-test not to import
130
+ // the same address twice causing an error.
131
+ taprootPubkey [3 ] = 1
132
+ }
133
+ pkScriptBytes := append (
134
+ []byte {txscript .OP_1 , txscript .OP_DATA_32 },
135
+ taprootPubkey [:]... ,
136
+ )
137
+ pkScript , err := txscript .ParsePkScript (pkScriptBytes )
138
+ require .NoError (ht , err )
139
+ taprootAddress , err := pkScript .Address (harnessNetParams )
140
+ require .NoError (ht , err )
141
+
142
+ var addr string
143
+ switch {
144
+ // Use ImportTapscript.
145
+ case importTapscript :
146
+ addr = taprootAddress .String ()
147
+
148
+ // Import the taproot address to LND.
149
+ req := & walletrpc.ImportTapscriptRequest {
150
+ InternalPublicKey : taprootPubkey [:],
151
+ Script : & walletrpc.ImportTapscriptRequest_FullKeyOnly {
152
+ FullKeyOnly : true ,
153
+ },
154
+ }
155
+ res := alice .RPC .ImportTapscript (req )
156
+ require .Equal (ht , addr , res .P2TrAddress )
157
+
158
+ // Use ImportPublicKey.
159
+ case importPubkey :
160
+ var (
161
+ address btcutil.Address
162
+ pubKey []byte
163
+ addressType walletrpc.AddressType
164
+ )
165
+ switch deliveryAddressType {
166
+ case lnrpc .AddressType_UNUSED_WITNESS_PUBKEY_HASH :
167
+ // Make fake public key hash.
168
+ pk := [33 ]byte {2 , 3 , 4 }
169
+ if upfrontShutdown {
170
+ // Make new address for second sub-test.
171
+ pk [1 ]++
172
+ }
173
+ address , err = btcutil .NewAddressWitnessPubKeyHash (
174
+ btcutil .Hash160 (pk [:]), harnessNetParams ,
175
+ )
176
+ require .NoError (ht , err )
177
+ pubKey = pk [:]
178
+ addressType = walletrpc .AddressType_WITNESS_PUBKEY_HASH
179
+
180
+ case lnrpc .AddressType_UNUSED_TAPROOT_PUBKEY :
181
+ address = taprootAddress
182
+ pubKey = taprootPubkey [:]
183
+ addressType = walletrpc .AddressType_TAPROOT_PUBKEY
184
+
185
+ default :
186
+ ht .Fatalf ("not allowed address type: %v" ,
187
+ deliveryAddressType )
188
+ }
189
+
190
+ addr = address .String ()
191
+
192
+ // Import the address to LND.
193
+ alice .RPC .ImportPublicKey (& walletrpc.ImportPublicKeyRequest {
194
+ PublicKey : pubKey ,
195
+ AddressType : addressType ,
196
+ })
197
+
63
198
// Here we generate a final delivery address in bob's wallet but set by
64
199
// alice. We do this to ensure that the address is not in alice's LND
65
200
// wallet. We already correctly track settled balances when the address
66
201
// is in the LND wallet.
67
- addr := bob .RPC .NewAddress (& lnrpc.NewAddressRequest {
68
- Type : deliveryAddressType ,
69
- })
202
+ default :
203
+ res := bob .RPC .NewAddress (& lnrpc.NewAddressRequest {
204
+ Type : deliveryAddressType ,
205
+ })
206
+ addr = res .Address
207
+ }
70
208
71
209
// Prepare for channel open.
72
210
openParams := lntest.OpenChannelParams {
@@ -76,7 +214,7 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest,
76
214
// If we are testing the case where we set it on open then we'll set the
77
215
// upfront shutdown script in the channel open parameters.
78
216
if upfrontShutdown {
79
- openParams .CloseAddress = addr . Address
217
+ openParams .CloseAddress = addr
80
218
}
81
219
82
220
// Open the channel!
@@ -91,14 +229,14 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest,
91
229
// If we are testing the case where we set the delivery address on
92
230
// channel close then we will set it in the channel close parameters.
93
231
if ! upfrontShutdown {
94
- closeParams .DeliveryAddress = addr . Address
232
+ closeParams .DeliveryAddress = addr
95
233
}
96
234
97
235
// Close the channel!
98
236
closeClient := alice .RPC .CloseChannel (& closeParams )
99
237
100
238
// Assert that we got a channel update when we get a closing txid.
101
- _ , err : = closeClient .Recv ()
239
+ _ , err = closeClient .Recv ()
102
240
require .NoError (ht , err )
103
241
104
242
// Mine the closing transaction.
0 commit comments