Skip to content

Commit f5ca16c

Browse files
committed
itest: test imported p2tr address in coop close
Safeguard against lightninglabs/loop#923
1 parent 2e72e51 commit f5ca16c

File tree

1 file changed

+74
-15
lines changed

1 file changed

+74
-15
lines changed

itest/lnd_coop_close_external_delivery_test.go

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"testing"
66

77
"github.com/btcsuite/btcd/btcutil"
8+
"github.com/btcsuite/btcd/txscript"
89
"github.com/lightningnetwork/lnd/lnrpc"
10+
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
911
"github.com/lightningnetwork/lnd/lntest"
1012
"github.com/lightningnetwork/lnd/lntest/node"
1113
"github.com/lightningnetwork/lnd/lntest/wait"
@@ -19,7 +21,7 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
1921

2022
ok := ht.Run("set P2WPKH delivery address at open", func(t *testing.T) {
2123
testCoopCloseWithExternalDeliveryImpl(
22-
ht, alice, bob, true,
24+
ht, alice, bob, true, false,
2325
lnrpc.AddressType_UNUSED_WITNESS_PUBKEY_HASH,
2426
)
2527
})
@@ -30,7 +32,7 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
3032

3133
ok = ht.Run("set P2WPKH delivery address at close", func(t *testing.T) {
3234
testCoopCloseWithExternalDeliveryImpl(
33-
ht, alice, bob, false,
35+
ht, alice, bob, false, false,
3436
lnrpc.AddressType_UNUSED_WITNESS_PUBKEY_HASH,
3537
)
3638
})
@@ -41,7 +43,7 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
4143

4244
ok = ht.Run("set P2TR delivery address at open", func(t *testing.T) {
4345
testCoopCloseWithExternalDeliveryImpl(
44-
ht, alice, bob, true,
46+
ht, alice, bob, true, false,
4547
lnrpc.AddressType_UNUSED_TAPROOT_PUBKEY,
4648
)
4749
})
@@ -50,9 +52,31 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
5052
return
5153
}
5254

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) {
5456
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,
5680
lnrpc.AddressType_UNUSED_TAPROOT_PUBKEY,
5781
)
5882
})
@@ -62,17 +86,52 @@ func testCoopCloseWithExternalDelivery(ht *lntest.HarnessTest) {
6286
// balance irrespective of whether the delivery address is in LND's wallet or
6387
// not. Some users set this value to be an address in a different wallet and
6488
// this should not affect our ability to accurately report the settled balance.
89+
//
90+
// If importTapscript is set, it imports a Taproot script and internal key to
91+
// alice LND to make sure it doesn't interfere with channel delivery address.
92+
// See https://github.com/lightninglabs/loop/issues/923
6593
func testCoopCloseWithExternalDeliveryImpl(ht *lntest.HarnessTest,
66-
alice, bob *node.HarnessNode, upfrontShutdown bool,
94+
alice, bob *node.HarnessNode, upfrontShutdown, importTapscript bool,
6795
deliveryAddressType lnrpc.AddressType) {
6896

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+
}
76135

77136
// Prepare for channel open.
78137
openParams := lntest.OpenChannelParams{
@@ -82,7 +141,7 @@ func testCoopCloseWithExternalDeliveryImpl(ht *lntest.HarnessTest,
82141
// If we are testing the case where we set it on open then we'll set the
83142
// upfront shutdown script in the channel open parameters.
84143
if upfrontShutdown {
85-
openParams.CloseAddress = addr.Address
144+
openParams.CloseAddress = addr
86145
}
87146

88147
// Open the channel!
@@ -97,7 +156,7 @@ func testCoopCloseWithExternalDeliveryImpl(ht *lntest.HarnessTest,
97156
// If we are testing the case where we set the delivery address on
98157
// channel close then we will set it in the channel close parameters.
99158
if !upfrontShutdown {
100-
closeParams.DeliveryAddress = addr.Address
159+
closeParams.DeliveryAddress = addr
101160
}
102161

103162
// Close the channel!

0 commit comments

Comments
 (0)