forked from muun/recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address_generator.go
129 lines (108 loc) · 3.56 KB
/
address_generator.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
package main
import (
"fmt"
"log"
"github.com/muun/libwallet"
"github.com/muun/recovery/utils"
)
type AddressGenerator struct {
addressCount int
userKey *libwallet.HDPrivateKey
muunKey *libwallet.HDPrivateKey
generateContacts bool
}
func NewAddressGenerator(userKey, muunKey *libwallet.HDPrivateKey, generateContacts bool) *AddressGenerator {
return &AddressGenerator{
addressCount: 0,
userKey: userKey,
muunKey: muunKey,
generateContacts: generateContacts,
}
}
// Stream returns a channel that emits all addresses generated.
func (g *AddressGenerator) Stream() chan libwallet.MuunAddress {
ch := make(chan libwallet.MuunAddress)
go func() {
g.generate(ch)
utils.NewLogger("ADDR").Printf("Addresses %v\n", g.addressCount)
close(ch)
}()
return ch
}
func (g *AddressGenerator) generate(consumer chan libwallet.MuunAddress) {
g.generateChangeAddrs(consumer)
g.generateExternalAddrs(consumer)
if g.generateContacts {
g.generateContactAddrs(consumer, 100)
}
}
func (g *AddressGenerator) generateChangeAddrs(consumer chan libwallet.MuunAddress) {
const changePath = "m/1'/1'/0"
changeUserKey, _ := g.userKey.DeriveTo(changePath)
changeMuunKey, _ := g.muunKey.DeriveTo(changePath)
g.deriveTree(consumer, changeUserKey, changeMuunKey, 2500, "change")
}
func (g *AddressGenerator) generateExternalAddrs(consumer chan libwallet.MuunAddress) {
const externalPath = "m/1'/1'/1"
externalUserKey, _ := g.userKey.DeriveTo(externalPath)
externalMuunKey, _ := g.muunKey.DeriveTo(externalPath)
g.deriveTree(consumer, externalUserKey, externalMuunKey, 2500, "external")
}
func (g *AddressGenerator) generateContactAddrs(consumer chan libwallet.MuunAddress, numContacts int64) {
const addressPath = "m/1'/1'/2"
contactUserKey, _ := g.userKey.DeriveTo(addressPath)
contactMuunKey, _ := g.muunKey.DeriveTo(addressPath)
for i := int64(0); i <= numContacts; i++ {
partialContactUserKey, _ := contactUserKey.DerivedAt(i, false)
partialMuunUserKey, _ := contactMuunKey.DerivedAt(i, false)
branch := fmt.Sprintf("contacts-%v", i)
g.deriveTree(consumer, partialContactUserKey, partialMuunUserKey, 200, branch)
}
}
func (g *AddressGenerator) deriveTree(
consumer chan libwallet.MuunAddress,
rootUserKey, rootMuunKey *libwallet.HDPrivateKey,
count int64,
name string,
) {
for i := int64(0); i <= count; i++ {
userKey, err := rootUserKey.DerivedAt(i, false)
if err != nil {
log.Printf("skipping child %v for %v due to %v", i, name, err)
continue
}
muunKey, err := rootMuunKey.DerivedAt(i, false)
if err != nil {
log.Printf("skipping child %v for %v due to %v", i, name, err)
continue
}
addrV2, err := libwallet.CreateAddressV2(userKey.PublicKey(), muunKey.PublicKey())
if err == nil {
consumer <- addrV2
g.addressCount++
} else {
log.Printf("failed to generate %v v2 for %v due to %v", name, i, err)
}
addrV3, err := libwallet.CreateAddressV3(userKey.PublicKey(), muunKey.PublicKey())
if err == nil {
consumer <- addrV3
g.addressCount++
} else {
log.Printf("failed to generate %v v3 for %v due to %v", name, i, err)
}
addrV4, err := libwallet.CreateAddressV4(userKey.PublicKey(), muunKey.PublicKey())
if err == nil {
consumer <- addrV4
g.addressCount++
} else {
log.Printf("failed to generate %v v4 for %v due to %v", name, i, err)
}
addrV5, err := libwallet.CreateAddressV5(userKey.PublicKey(), muunKey.PublicKey())
if err == nil {
consumer <- addrV5
g.addressCount++
} else {
log.Printf("failed to generate %v v5 for %v due to %v", name, i, err)
}
}
}