-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathsigner_client_test.go
More file actions
203 lines (177 loc) · 4.91 KB
/
Copy pathsigner_client_test.go
File metadata and controls
203 lines (177 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package lndclient
import (
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightningnetwork/lnd/keychain"
"github.com/stretchr/testify/require"
)
// newTestPubKey returns a fresh public key for use in descriptor
// marshalling tests.
func newTestPubKey(t *testing.T) *btcec.PublicKey {
t.Helper()
priv, err := btcec.NewPrivateKey()
require.NoError(t, err)
return priv.PubKey()
}
// TestMarshallSignDescriptorsFullLocator pins the contract of the full
// descriptor marshaller used by SignOutputRawKeyLocator: the key locator
// is attached whenever either the family or the index is non-zero, and
// only the all-zero KeyLocator (the struct's zero value, indistinguishable
// from "unset") is omitted.
//
// The family-6 / index-0 case is the regression guard. A prior version of
// the helper required both components to be non-zero and therefore
// silently dropped the locator for LND's node identity key slot. Signers
// that must re-derive the key from its locator — restored-from-seed
// wallets, or family-pinned identity keys — could not resolve the key.
func TestMarshallSignDescriptorsFullLocator(t *testing.T) {
t.Parallel()
pubKey := newTestPubKey(t)
output := &wire.TxOut{PkScript: []byte{0x51}, Value: 1000}
tests := []struct {
name string
family keychain.KeyFamily
index uint32
omitPubKey bool
expectLocator bool
}{
{
name: "all-zero locator is omitted",
family: 0,
index: 0,
expectLocator: false,
},
{
name: "family-only locator is attached",
family: keychain.KeyFamilyNodeKey,
index: 0,
expectLocator: true,
},
{
name: "index-only locator is attached",
family: 0,
index: 5,
expectLocator: true,
},
{
name: "fully populated locator is attached",
family: keychain.KeyFamilyNodeKey,
index: 5,
expectLocator: true,
},
{
name: "family-only locator is attached with " +
"nil pubkey",
family: keychain.KeyFamilyNodeKey,
index: 0,
omitPubKey: true,
expectLocator: true,
},
{
name: "index-only locator is attached with " +
"nil pubkey",
family: 0,
index: 5,
omitPubKey: true,
expectLocator: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
keyDesc := keychain.KeyDescriptor{
KeyLocator: keychain.KeyLocator{
Family: tc.family,
Index: tc.index,
},
}
if !tc.omitPubKey {
keyDesc.PubKey = pubKey
}
descs := []*SignDescriptor{{
KeyDesc: keyDesc,
Output: output,
}}
rpcDescs := marshallSignDescriptors(descs, true)
require.Len(t, rpcDescs, 1)
rpcKeyDesc := rpcDescs[0].KeyDesc
require.NotNil(t, rpcKeyDesc)
if tc.omitPubKey {
require.Empty(t, rpcKeyDesc.RawKeyBytes)
} else {
require.Equal(t,
pubKey.SerializeCompressed(),
rpcKeyDesc.RawKeyBytes,
)
}
if !tc.expectLocator {
require.Nil(t, rpcKeyDesc.KeyLoc)
return
}
require.NotNil(t, rpcKeyDesc.KeyLoc)
require.Equal(t,
int32(tc.family),
rpcKeyDesc.KeyLoc.KeyFamily,
)
require.Equal(t,
int32(tc.index),
rpcKeyDesc.KeyLoc.KeyIndex,
)
})
}
}
// TestMarshallSignDescriptorsPartialOnlyOneOf verifies that the partial
// descriptor marshaller used by the legacy SignOutputRaw populates either
// the public key or the locator, but never both. Applications like Loop
// have adjusted themselves to this behavior, which is why the fix for the
// locator-drop bug lives in the full descriptor path instead.
func TestMarshallSignDescriptorsPartialOnlyOneOf(t *testing.T) {
t.Parallel()
pubKey := newTestPubKey(t)
output := &wire.TxOut{PkScript: []byte{0x51}, Value: 1000}
t.Run("pubkey present drops locator", func(t *testing.T) {
t.Parallel()
descs := []*SignDescriptor{{
KeyDesc: keychain.KeyDescriptor{
PubKey: pubKey,
KeyLocator: keychain.KeyLocator{
Family: keychain.KeyFamilyNodeKey,
Index: 5,
},
},
Output: output,
}}
rpcDescs := marshallSignDescriptors(descs, false)
require.Len(t, rpcDescs, 1)
require.Equal(t,
pubKey.SerializeCompressed(),
rpcDescs[0].KeyDesc.RawKeyBytes,
)
require.Nil(t, rpcDescs[0].KeyDesc.KeyLoc)
})
t.Run("pubkey absent uses locator", func(t *testing.T) {
t.Parallel()
descs := []*SignDescriptor{{
KeyDesc: keychain.KeyDescriptor{
KeyLocator: keychain.KeyLocator{
Family: keychain.KeyFamilyNodeKey,
Index: 5,
},
},
Output: output,
}}
rpcDescs := marshallSignDescriptors(descs, false)
require.Len(t, rpcDescs, 1)
require.Empty(t, rpcDescs[0].KeyDesc.RawKeyBytes)
require.NotNil(t, rpcDescs[0].KeyDesc.KeyLoc)
require.Equal(t,
int32(keychain.KeyFamilyNodeKey),
rpcDescs[0].KeyDesc.KeyLoc.KeyFamily,
)
require.Equal(t,
int32(5),
rpcDescs[0].KeyDesc.KeyLoc.KeyIndex,
)
})
}