-
Notifications
You must be signed in to change notification settings - Fork 32
/
hdkeychain.go
640 lines (538 loc) · 17 KB
/
hdkeychain.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package lnd
import (
"crypto/sha256"
"fmt"
"strconv"
"strings"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/shachain"
)
const (
HardenedKeyStart = uint32(hdkeychain.HardenedKeyStart)
WalletDefaultDerivationPath = "m/84'/0'/0'"
WalletBIP49DerivationPath = "m/49'/0'/0'"
WalletBIP86DerivationPath = "m/86'/0'/0'"
LndDerivationPath = "m/1017'/%d'/%d'"
AddressDeriveFromWallet = "fromseed"
)
type AddrType int
const (
AddrTypeP2WKH AddrType = iota
AddrTypeP2WSH
AddrTypeP2TR
)
func DeriveChildren(key *hdkeychain.ExtendedKey, path []uint32) (
*hdkeychain.ExtendedKey, error) {
var currentKey = key
for idx, pathPart := range path {
derivedKey, err := currentKey.DeriveNonStandard(pathPart)
if err != nil {
return nil, err
}
// There's this special case in lnd's wallet (btcwallet) where
// the coin type and account keys are always serialized as a
// string and encrypted, which actually fixes the key padding
// issue that makes the difference between DeriveNonStandard and
// Derive. To replicate lnd's behavior exactly, we need to
// serialize and de-serialize the extended key at the coin type
// and account level (depth = 2 or depth = 3). This does not
// apply to the default account (id = 0) because that is always
// derived directly.
depth := derivedKey.Depth()
keyID := pathPart - hdkeychain.HardenedKeyStart
nextID := uint32(0)
if depth == 2 && len(path) > 2 {
nextID = path[idx+1] - hdkeychain.HardenedKeyStart
}
if (depth == 2 && nextID != 0) || (depth == 3 && keyID != 0) {
currentKey, err = hdkeychain.NewKeyFromString(
derivedKey.String(),
)
if err != nil {
return nil, err
}
} else {
currentKey = derivedKey
}
}
return currentKey, nil
}
func ParsePath(path string) ([]uint32, error) {
path = strings.TrimSpace(path)
if len(path) == 0 {
return nil, fmt.Errorf("path cannot be empty")
}
if !strings.HasPrefix(path, "m/") {
return nil, fmt.Errorf("path must start with m/")
}
parts := strings.Split(path, "/")
indices := make([]uint32, len(parts)-1)
for i := 1; i < len(parts); i++ {
index := uint32(0)
part := parts[i]
if strings.Contains(parts[i], "'") {
index += HardenedKeyStart
part = strings.TrimRight(parts[i], "'")
}
parsed, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("could not parse part \"%s\": "+
"%v", part, err)
}
indices[i-1] = index + uint32(parsed)
}
return indices, nil
}
func HardenedKey(key uint32) uint32 {
return key + HardenedKeyStart
}
// DeriveKey derives the public key and private key in the WIF format for a
// given key path of the extended key.
func DeriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
params *chaincfg.Params) (*hdkeychain.ExtendedKey, *btcec.PublicKey,
*btcutil.WIF, error) {
parsedPath, err := ParsePath(path)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not parse derivation "+
"path: %w", err)
}
derivedKey, err := DeriveChildren(extendedKey, parsedPath)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not derive children: "+
"%w", err)
}
pubKey, err := derivedKey.ECPubKey()
if err != nil {
return nil, nil, nil, fmt.Errorf("could not derive public "+
"key: %w", err)
}
privKey, err := derivedKey.ECPrivKey()
if err != nil {
return nil, nil, nil, fmt.Errorf("could not derive private "+
"key: %w", err)
}
wif, err := btcutil.NewWIF(privKey, params, true)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not encode WIF: %w",
err)
}
return derivedKey, pubKey, wif, nil
}
func PrivKeyFromPath(extendedKey *hdkeychain.ExtendedKey,
path []uint32) (*btcec.PrivateKey, error) {
derivedKey, err := DeriveChildren(extendedKey, path)
if err != nil {
return nil, fmt.Errorf("could not derive children: %w", err)
}
privKey, err := derivedKey.ECPrivKey()
if err != nil {
return nil, fmt.Errorf("could not derive private key: %w", err)
}
return privKey, nil
}
func ShaChainFromPath(extendedKey *hdkeychain.ExtendedKey, path []uint32,
multiSigPubKey *btcec.PublicKey) (*shachain.RevocationProducer, error) {
privKey, err := PrivKeyFromPath(extendedKey, path)
if err != nil {
return nil, err
}
// This is the legacy way where we just used the private key as the
// revocation root directly.
if multiSigPubKey == nil {
revRoot, err := chainhash.NewHash(privKey.Serialize())
if err != nil {
return nil, fmt.Errorf("could not create revocation "+
"root hash: %w", err)
}
return shachain.NewRevocationProducer(*revRoot), nil
}
// Perform an ECDH operation between the private key described in
// nextRevocationKeyDesc and our public multisig key. The result will be
// used to seed the revocation producer.
revRoot, err := ECDH(privKey, multiSigPubKey)
if err != nil {
return nil, err
}
// Once we have the root, we can then generate our shachain producer
// and from that generate the per-commitment point.
return shachain.NewRevocationProducer(revRoot), nil
}
func IdentityPath(params *chaincfg.Params) string {
return fmt.Sprintf(
LndDerivationPath+"/0/0", params.HDCoinType,
keychain.KeyFamilyNodeKey,
)
}
func MultisigPath(params *chaincfg.Params, index int) string {
return fmt.Sprintf(
LndDerivationPath+"/0/%d", params.HDCoinType,
keychain.KeyFamilyMultiSig, index,
)
}
func AllDerivationPaths(params *chaincfg.Params) ([]string, [][]uint32, error) {
mkPath := func(f keychain.KeyFamily) string {
return fmt.Sprintf(
LndDerivationPath, params.HDCoinType, uint32(f),
)
}
pathStrings := []string{
WalletBIP49DerivationPath,
WalletDefaultDerivationPath,
WalletBIP86DerivationPath,
mkPath(keychain.KeyFamilyPaymentBase),
}
paths := make([][]uint32, len(pathStrings))
for idx, path := range pathStrings {
p, err := ParsePath(path)
if err != nil {
return nil, nil, err
}
paths[idx] = p
}
return pathStrings, paths, nil
}
// DecodeAddressHash returns the public key or script hash encoded in a native
// bech32 encoded SegWit address and whether it's a script hash or not.
func DecodeAddressHash(addr string, chainParams *chaincfg.Params) ([]byte, bool,
error) {
targetAddr, err := ParseAddress(addr, chainParams)
if err != nil {
return nil, false, err
}
// Must be a bech32 native SegWit address.
var (
isScriptHash = false
targetHash []byte
)
switch targetAddr.(type) {
case *btcutil.AddressWitnessPubKeyHash:
targetHash = targetAddr.ScriptAddress()
case *btcutil.AddressWitnessScriptHash:
isScriptHash = true
targetHash = targetAddr.ScriptAddress()
default:
return nil, false, fmt.Errorf("address: must be a bech32 " +
"P2WPKH or P2WSH address")
}
return targetHash, isScriptHash, nil
}
// ParseAddress attempts to parse the given address string into a native address
// for the given network.
func ParseAddress(addr string, chainParams *chaincfg.Params) (btcutil.Address,
error) {
// First parse address to get targetHash from it later.
targetAddr, err := btcutil.DecodeAddress(addr, chainParams)
if err != nil {
return nil, fmt.Errorf("unable to decode address %s: %w", addr,
err)
}
// Make the check on the decoded address according to the active
// network (testnet or mainnet only).
if !targetAddr.IsForNet(chainParams) {
return nil, fmt.Errorf("address: %v is not valid for this "+
"network: %v", targetAddr.String(), chainParams.Name,
)
}
return targetAddr, nil
}
func GetWitnessAddrScript(addr btcutil.Address,
chainParams *chaincfg.Params) ([]byte, error) {
if !addr.IsForNet(chainParams) {
return nil, fmt.Errorf("address %v is not for net %v", addr,
chainParams.Name)
}
return txscript.PayToAddrScript(addr)
}
// GetP2WPKHScript creates a P2WKH output script from an address. If the address
// is not a P2WKH address, an error is returned.
func GetP2WPKHScript(addr string, chainParams *chaincfg.Params) ([]byte,
error) {
targetPubKeyHash, isScriptHash, err := DecodeAddressHash(
addr, chainParams,
)
if err != nil {
return nil, err
}
if isScriptHash {
return nil, fmt.Errorf("address %s is not a P2WKH address",
addr)
}
builder := txscript.NewScriptBuilder()
builder.AddOp(txscript.OP_0)
builder.AddData(targetPubKeyHash)
return builder.Script()
}
// GetP2WSHScript creates a P2WSH output script from an address. If the address
// is not a P2WSH address, an error is returned.
func GetP2WSHScript(addr string, chainParams *chaincfg.Params) ([]byte,
error) {
targetScriptHash, isScriptHash, err := DecodeAddressHash(
addr, chainParams,
)
if err != nil {
return nil, err
}
if !isScriptHash {
return nil, fmt.Errorf("address %s is not a P2WSH address",
addr)
}
builder := txscript.NewScriptBuilder()
builder.AddOp(txscript.OP_0)
builder.AddData(targetScriptHash)
return builder.Script()
}
func P2PKHAddr(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressPubKeyHash, error) {
hash160 := btcutil.Hash160(pubKey.SerializeCompressed())
addrP2PKH, err := btcutil.NewAddressPubKeyHash(hash160, params)
if err != nil {
return nil, fmt.Errorf("could not create address: %w", err)
}
return addrP2PKH, nil
}
func P2WKHAddr(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressWitnessPubKeyHash, error) {
hash160 := btcutil.Hash160(pubKey.SerializeCompressed())
return btcutil.NewAddressWitnessPubKeyHash(hash160, params)
}
func NP2WKHAddr(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressScriptHash, error) {
hash160 := btcutil.Hash160(pubKey.SerializeCompressed())
addrP2WKH, err := btcutil.NewAddressWitnessPubKeyHash(hash160, params)
if err != nil {
return nil, fmt.Errorf("could not create address: %w", err)
}
script, err := txscript.PayToAddrScript(addrP2WKH)
if err != nil {
return nil, fmt.Errorf("could not create script: %w", err)
}
return btcutil.NewAddressScriptHash(script, params)
}
func P2TRAddr(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressTaproot, error) {
taprootKey := txscript.ComputeTaprootKeyNoScript(pubKey)
return btcutil.NewAddressTaproot(
schnorr.SerializePubKey(taprootKey), params,
)
}
func P2AnchorStaticRemote(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressWitnessScriptHash, []byte,
error) {
commitScript, err := input.CommitScriptToRemoteConfirmed(pubKey)
if err != nil {
return nil, nil, fmt.Errorf("could not create script: %w", err)
}
scriptHash := sha256.Sum256(commitScript)
p2wsh, err := btcutil.NewAddressWitnessScriptHash(scriptHash[:], params)
return p2wsh, commitScript, err
}
func P2TaprootStaticRemote(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressTaproot,
*input.CommitScriptTree, error) {
scriptTree, err := input.NewRemoteCommitScriptTree(pubKey)
if err != nil {
return nil, nil, fmt.Errorf("could not create script: %w", err)
}
addr, err := btcutil.NewAddressTaproot(
schnorr.SerializePubKey(scriptTree.TaprootKey), params,
)
return addr, scriptTree, err
}
func CheckAddress(addr string, chainParams *chaincfg.Params, allowDerive bool,
hint string, allowedTypes ...AddrType) error {
// We generally always want an address to be specified. If one should
// be derived from the wallet automatically, the user should specify
// "derive" as the address.
if addr == "" {
return fmt.Errorf("%s address cannot be empty", hint)
}
// If we're allowed to derive an address from the wallet, we can skip
// the rest of the checks.
if allowDerive && addr == AddressDeriveFromWallet {
return nil
}
parsedAddr, err := ParseAddress(addr, chainParams)
if err != nil {
return fmt.Errorf("%s address is invalid: %w", hint, err)
}
if !matchAddrType(parsedAddr, allowedTypes...) {
return fmt.Errorf("%s address is of wrong type, allowed "+
"types: %s", hint, addrTypesToString(allowedTypes))
}
return nil
}
func PrepareWalletAddress(addr string, chainParams *chaincfg.Params,
estimator *input.TxWeightEstimator, rootKey *hdkeychain.ExtendedKey,
hint string) ([]byte, error) {
// We already checked if deriving a new address is allowed in a previous
// step, so we can just go ahead and do it now if requested.
if addr == AddressDeriveFromWallet {
// To maximize compatibility and recoverability, we always
// derive the very first P2WKH address from the wallet.
// This corresponds to the derivation path: m/84'/0'/0'/0/0.
derivedKey, err := DeriveChildren(rootKey, []uint32{
HardenedKeyStart + waddrmgr.KeyScopeBIP0084.Purpose,
HardenedKeyStart + chainParams.HDCoinType,
HardenedKeyStart + 0, 0, 0,
})
if err != nil {
return nil, err
}
derivedPubKey, err := derivedKey.ECPubKey()
if err != nil {
return nil, err
}
p2wkhAddr, err := P2WKHAddr(derivedPubKey, chainParams)
if err != nil {
return nil, err
}
return txscript.PayToAddrScript(p2wkhAddr)
}
parsedAddr, err := ParseAddress(addr, chainParams)
if err != nil {
return nil, fmt.Errorf("%s address is invalid: %w", hint, err)
}
// Exit early if we don't need to estimate the weight.
if estimator == nil {
return txscript.PayToAddrScript(parsedAddr)
}
// These are the three address types that we support in general. We
// should have checked that we get the correct type in a previous step.
switch parsedAddr.(type) {
case *btcutil.AddressWitnessPubKeyHash:
estimator.AddP2WKHOutput()
case *btcutil.AddressWitnessScriptHash:
estimator.AddP2WSHOutput()
case *btcutil.AddressTaproot:
estimator.AddP2TROutput()
default:
return nil, fmt.Errorf("%s address is of wrong type", hint)
}
return txscript.PayToAddrScript(parsedAddr)
}
func matchAddrType(addr btcutil.Address, allowedTypes ...AddrType) bool {
contains := func(allowedTypes []AddrType, addrType AddrType) bool {
for _, allowedType := range allowedTypes {
if allowedType == addrType {
return true
}
}
return false
}
switch addr.(type) {
case *btcutil.AddressWitnessPubKeyHash:
return contains(allowedTypes, AddrTypeP2WKH)
case *btcutil.AddressWitnessScriptHash:
return contains(allowedTypes, AddrTypeP2WSH)
case *btcutil.AddressTaproot:
return contains(allowedTypes, AddrTypeP2TR)
default:
return false
}
}
func addrTypesToString(allowedTypes []AddrType) string {
var types []string
for _, allowedType := range allowedTypes {
switch allowedType {
case AddrTypeP2WKH:
types = append(types, "P2WKH")
case AddrTypeP2WSH:
types = append(types, "P2WSH")
case AddrTypeP2TR:
types = append(types, "P2TR")
}
}
return strings.Join(types, ", ")
}
type HDKeyRing struct {
ExtendedKey *hdkeychain.ExtendedKey
ChainParams *chaincfg.Params
}
func (r *HDKeyRing) DeriveNextKey(_ keychain.KeyFamily) (
keychain.KeyDescriptor, error) {
return keychain.KeyDescriptor{}, nil
}
func (r *HDKeyRing) DeriveKey(keyLoc keychain.KeyLocator) (
keychain.KeyDescriptor, error) {
var empty = keychain.KeyDescriptor{}
derivedKey, err := DeriveChildren(r.ExtendedKey, []uint32{
HardenedKeyStart + uint32(keychain.BIP0043Purpose),
HardenedKeyStart + r.ChainParams.HDCoinType,
HardenedKeyStart + uint32(keyLoc.Family),
0,
keyLoc.Index,
})
if err != nil {
return empty, err
}
derivedPubKey, err := derivedKey.ECPubKey()
if err != nil {
return empty, err
}
return keychain.KeyDescriptor{
KeyLocator: keychain.KeyLocator{
Family: keyLoc.Family,
Index: keyLoc.Index,
},
PubKey: derivedPubKey,
}, nil
}
// CheckDescriptor checks if a key descriptor is correct by making sure that we
// can derive the key that it describes.
func (r *HDKeyRing) CheckDescriptor(
keyDesc keychain.KeyDescriptor) error {
// A check doesn't make sense if there is no public key set.
if keyDesc.PubKey == nil {
return fmt.Errorf("no public key provided to check")
}
// Performance fix, derive static path only once.
familyKey, err := DeriveChildren(r.ExtendedKey, []uint32{
HardenedKeyStart + uint32(keychain.BIP0043Purpose),
HardenedKeyStart + r.ChainParams.HDCoinType,
HardenedKeyStart + uint32(keyDesc.Family),
0,
})
if err != nil {
return err
}
// Scan the same key range as lnd would do on channel restore.
for i := 0; i < keychain.MaxKeyRangeScan; i++ {
child, err := DeriveChildren(familyKey, []uint32{uint32(i)})
if err != nil {
return err
}
pubKey, err := child.ECPubKey()
if err != nil {
return err
}
if !pubKey.IsEqual(keyDesc.PubKey) {
continue
}
// If we found the key, we can abort and signal success.
return nil
}
// We scanned the max range and didn't find a key. It's very likely not
// derivable with the given information.
return keychain.ErrCannotDerivePrivKey
}
// NodePubKey returns the public key that represents an lnd node's public
// network identity.
func (r *HDKeyRing) NodePubKey() (*btcec.PublicKey, error) {
keyDesc, err := r.DeriveKey(keychain.KeyLocator{
Family: keychain.KeyFamilyNodeKey,
Index: 0,
})
if err != nil {
return nil, err
}
return keyDesc.PubKey, nil
}