-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
215 lines (188 loc) · 4.88 KB
/
main.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
package main
import (
"crypto/hmac"
"crypto/sha512"
"fmt"
"github.com/apottere/go-vanity-wallet/utils"
"github.com/dustinxie/ecc"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/sha3"
"hash"
"math/big"
"os"
"runtime"
"strconv"
"strings"
"sync"
)
func fatal(a ...any) {
_, err := fmt.Fprintln(os.Stderr, a...)
if err != nil {
panic(err)
}
os.Exit(1)
}
type DerivationPart struct {
last bool
i uint64
serialized []byte
}
const hardenedLimit = 0x80000000
var seedSalt = []byte("mnemonic")
var masterKeySeed = []byte("Bitcoin seed")
func loop(wg *sync.WaitGroup, entropyInt int, derivation []DerivationPart) {
defer wg.Done()
entropy := utils.NewEntropyInfo(entropyInt)
// Shared vars
var err error
var mnemonic []byte
var seed []byte
var currentHash hash.Hash
var I []byte
var chainCode []byte
var privateKeyBytes []byte
var x, y *big.Int
p256k1 := ecc.P256k1()
n := p256k1.Params().N
keccak := sha3.NewLegacyKeccak256()
zero := big.NewInt(0)
zeroByte := []byte{0}
bytePrefix := []byte{0}
privateKey := new(big.Int)
newPrivateKey := new(big.Int)
publicKey := make([]byte, 64)
publicKeyLeft := publicKey[:32]
publicKeyRight := publicKey[32:]
tempIntBytes := make([]byte, 32)
skip := false
var count uint64 = 0
for {
// Generate random mnemonic
mnemonic, err = utils.RandomMnemonic(entropy)
if err != nil {
panic(err)
}
// Create seed from mnemonic
seed = pbkdf2.Key(mnemonic, seedSalt, 2048, 64, sha512.New)
currentHash = hmac.New(sha512.New, masterKeySeed)
currentHash.Write(seed)
I = currentHash.Sum(nil)
privateKeyBytes = I[:32]
privateKey.SetBytes(privateKeyBytes)
chainCode = I[32:]
// Derive private key
skip = false
for _, part := range derivation {
currentHash = hmac.New(sha512.New, chainCode)
if part.i >= hardenedLimit {
currentHash.Write(zeroByte)
currentHash.Write(privateKeyBytes)
currentHash.Write(part.serialized)
I = currentHash.Sum(nil)
} else {
x, y := p256k1.ScalarBaseMult(privateKeyBytes)
bytePrefix[0] = 2 + byte(y.Bit(0))
currentHash.Write(bytePrefix)
currentHash.Write(x.FillBytes(tempIntBytes))
currentHash.Write(part.serialized)
I = currentHash.Sum(nil)
}
newPrivateKey.SetBytes(I[:32])
newPrivateKey.Add(newPrivateKey, privateKey)
privateKey.Mod(newPrivateKey, n)
if privateKey.Cmp(zero) == 0 || privateKey.Cmp(n) != -1 {
fmt.Println("Private key is invalid, skipping!")
skip = true
break
}
privateKeyBytes = privateKey.FillBytes(tempIntBytes)
chainCode = I[32:]
}
count += 1
if skip {
continue
}
// Derive address
x, y = p256k1.ScalarBaseMult(privateKeyBytes)
x.FillBytes(publicKeyLeft)
y.FillBytes(publicKeyRight)
keccak.Reset()
keccak.Write(publicKey)
address := keccak.Sum(nil)[12:]
if address[0] == 0x1b && address[1] == 0x0 && address[2] == 0x0 {
result := fmt.Sprintf("0x%x", address)
result += "\t" + string(mnemonic)
fmt.Println(result)
}
}
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
fatal("usage: vanity-wallet <entropy> [derivation path]")
}
threadsString := args[0]
threads, err := strconv.Atoi(threadsString)
if err != nil {
fatal("invalid thread count:", threadsString)
}
if threads < 1 {
threads = runtime.NumCPU()
fmt.Printf("Threads: %d\n", threads)
}
if threads > 10 {
runtime.GOMAXPROCS(threads)
}
entropyString := args[1]
entropyInt, err := strconv.Atoi(entropyString)
if err != nil {
fatal("invalid entropy:", entropyString)
}
if entropyInt < 128 || entropyInt > 256 || entropyInt%32 != 0 {
fatal("entropy must be a multiple of 32 between 128 and 256 (inclusive)")
}
derivationString := args[2]
derivationStringArray := strings.Split(derivationString, "/")
if len(derivationStringArray) < 2 || derivationStringArray[0] != "m" {
fatal("invalid derivation path:", derivationString)
}
derivationLength := len(derivationStringArray) - 1
derivation := make([]DerivationPart, derivationLength)
for i, part := range derivationStringArray[1:] {
hardened := strings.HasSuffix(part, "'")
if hardened {
part = strings.TrimSuffix(part, "'")
}
index, err := strconv.ParseUint(part, 10, 0)
if err != nil {
fatal("invalid derivation part:", part)
}
if hardened {
index += hardenedLimit
}
serialized := make([]byte, 4)
serialized[0] = byte(index >> 24)
serialized[1] = byte(index >> 16)
serialized[2] = byte(index >> 8)
serialized[3] = byte(index)
derivation[i] = DerivationPart{
last: i == derivationLength-1,
i: index,
serialized: serialized,
}
}
derivationStringOut := ""
for i, part := range derivation {
if i > 0 {
derivationStringOut += " "
}
derivationStringOut += fmt.Sprintf("0x%X", part.serialized)
}
fmt.Println("Derivation Path:", derivationStringOut)
var wg sync.WaitGroup
for i := 0; i < threads; i++ {
wg.Add(1)
go loop(&wg, entropyInt, derivation)
}
wg.Wait()
}