forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 24
/
dcrlnd_util.go
70 lines (62 loc) · 1.76 KB
/
dcrlnd_util.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
package lnwallet
import (
"github.com/decred/dcrd/hdkeychain/v3"
"github.com/decred/dcrd/txscript/v4/stdaddr"
)
// DeriveAddrsFromExtPub derives a number of internal and external addresses from
// an xpub key.
func DeriveAddrsFromExtPub(xpub *hdkeychain.ExtendedKey, addrParams stdaddr.AddressParamsV0, count int) ([]stdaddr.Address, []stdaddr.Address, error) {
const (
externalBranch uint32 = 0
internalBranch uint32 = 1
)
extKey, err := xpub.Child(externalBranch)
if err != nil {
return nil, nil, err
}
intKey, err := xpub.Child(internalBranch)
if err != nil {
return nil, nil, err
}
var intIdx, extIdx uint32
var intAddrs, extAddrs []stdaddr.Address
for i := 0; i < count; i++ {
// Derive internal address. Loop because sometimes the address
// is invalid.
var addr stdaddr.Address
child, err := intKey.Child(intIdx)
for ; err != nil; intIdx += 1 {
child, err = intKey.Child(intIdx)
if err != nil {
continue
}
addr, err = stdaddr.NewAddressPubKeyEcdsaSecp256k1V0Raw(child.SerializedPubKey(), addrParams)
if err != nil {
continue
}
}
intAddrs = append(intAddrs, addr)
// Derive external address. Loop because sometimes the address
// is invalid.
child, err = extKey.Child(extIdx)
for ; err != nil; extIdx += 1 {
child, err = extKey.Child(extIdx)
if err != nil {
continue
}
addr, err = stdaddr.NewAddressPubKeyEcdsaSecp256k1V0Raw(child.SerializedPubKey(), addrParams)
if err != nil {
continue
}
}
extAddrs = append(extAddrs, addr)
}
return intAddrs, extAddrs, nil
}
// cacheCommitmentTxHash fills the cachedTxHash of a *commitment for logging.
func cacheCommitmentTxHash(commit *commitment) *commitment {
if commit != nil && commit.txn != nil {
commit.txn.CachedTxHash()
}
return commit
}