-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
172 lines (145 loc) · 5.19 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/tyler-smith/go-bip39"
)
func main() {
firstWordsPtr := flag.String("firstWords", "", "REQUIRED: First words of mnemonic (23 words by default)")
testnetBoolPtr := flag.Bool("testnet", false, "Use testnet (default is mainnet)")
verbosityBoolPtr := flag.Bool("verbose", false, "Verbose printout (default is quiet)")
checksumIntPtr := flag.Int("checksum", 0, "EXPERTS ONLY: Which checksum word to append, using a 0-index.")
// https://stackoverflow.com/questions/53082613/set-the-order-for-output-by-flag-printdefaults
flag.Usage = func() {
flagSet := flag.CommandLine
fmt.Println("HumanRNG - Don't Trust Your Random Number Generator\n")
fmt.Println("Usage:")
fmt.Println(" go run *.go -firstWords=\"add bag cat ...\"\n")
order := []string{"firstWords", "testnet", "verbose", "checksum"}
for _, name := range order {
flag := flagSet.Lookup(name)
fmt.Printf("-%s\n", flag.Name)
fmt.Printf(" %s\n", flag.Usage)
}
}
flag.Parse()
if *checksumIntPtr > 0 {
fmt.Println("WARNING!")
fmt.Println("You have selected a checksum # greater than 0.")
fmt.Println("This should only be attempted by experts users.")
fmt.Println("If you do not know what you're doing, DO NOT CONTINUE.")
fmt.Println(strings.Repeat("-", 80))
}
if *verbosityBoolPtr == true {
fmt.Println("Input Flags (DEBUG ONLY):")
fmt.Println("checksum word number to pick:", *checksumIntPtr)
fmt.Println("testnet:", *testnetBoolPtr)
fmt.Println("mnemonicFirstWords:", *firstWordsPtr)
fmt.Println(strings.Repeat("-", 80))
}
firstWords := strings.TrimSpace(*firstWordsPtr)
if len(firstWords) == 0 {
fmt.Println("ERROR: You didn't supply supply firstWords of your mnemonic\n")
flag.Usage()
os.Exit(1)
}
invalidMnemonicWords := GetInvalidMnemonicWords(firstWords)
if len(invalidMnemonicWords) > 0 {
errStr := fmt.Sprintf("%s", invalidMnemonicWords)
fmt.Println("Invalid BIP39 Mnemonic Word(s): " + errStr)
os.Exit(1)
}
validChecksums, err := FindAllChecksumWords(firstWords)
if err != nil {
fmt.Println("Could not find valid checksum words")
os.Exit(1)
}
// Append checksum word (default to 0th word)
if *checksumIntPtr > len(validChecksums)-1 {
fmt.Printf("Not enough valid checksum words to append (0-indexed) result #%d \n", *checksumIntPtr)
os.Exit(1)
}
checksumWordToUse := validChecksums[*checksumIntPtr]
mnemonic := firstWords + " " + checksumWordToUse
passphrase := ""
seed := bip39.NewSeed(mnemonic, passphrase)
// p2wsh version bytes
var networkName string
var network chaincfg.Params
var derivationPath string
// https://github.com/satoshilabs/slips/blob/master/slip-0132.md
pubkeyBytesToUse := [4]byte{}
privkeyBytesToUse := [4]byte{}
if *testnetBoolPtr == true {
network = chaincfg.TestNet3Params
networkName = "testnet3"
derivationPath = "m/48'/1'/0'/2'"
privkeyBytesToUse = [4]byte{0x02, 0x57, 0x50, 0x48} // Vpriv
pubkeyBytesToUse = [4]byte{0x02, 0x57, 0x54, 0x83} // Vpub
} else {
network = chaincfg.MainNetParams
networkName = "mainnet"
derivationPath = "m/48'/0'/0'/2'"
privkeyBytesToUse = [4]byte{0x02, 0xaa, 0x7a, 0x99} // Zpriv
pubkeyBytesToUse = [4]byte{0x02, 0xaa, 0x7e, 0xd3} // Zpub
}
derivationPathSpecter := strings.Replace(
strings.ReplaceAll(derivationPath, "'", "h"),
"m/", "", 1,
)
masterXpriv, err := hdkeychain.NewMaster(seed, &network)
if err != nil {
fmt.Println("Couldn't create seed", err)
os.Exit(1)
}
// TODO: this should be handled by the btcd library (when supported) and not my custom code
xfp, err := RootXPrivToFingerprint(masterXpriv)
if err != nil {
fmt.Println("Error calculating fingerpint", err)
os.Exit(1)
}
childXpriv, err := DeriveChildKeyFromPath(masterXpriv, derivationPath)
if err != nil {
fmt.Println("Error deriving child private key", err)
os.Exit(1)
}
childXpub, err := childXpriv.Neuter()
if err != nil {
fmt.Println("Error deriving child public key", err)
os.Exit(1)
}
childZpriv, err := Slip132Encode(childXpriv, privkeyBytesToUse)
if err != nil {
fmt.Println("Error encoding SLIP132 version bytes on private key")
os.Exit(1)
}
childZpub, err := Slip132Encode(childXpub, pubkeyBytesToUse)
if err != nil {
fmt.Println("Error encoding SLIP132 version bytes on public key")
os.Exit(1)
}
// Output
fmt.Println("SECRET INFO:")
fmt.Println("Full mnemonic (with checksum word): ", mnemonic)
fmt.Println("Full mnemonic length (# words): ", len(strings.Split(mnemonic, " ")))
fmt.Println(strings.Repeat("-", 80))
fmt.Println("PUBLIC INFO:")
fmt.Println("SLIP132 Extended Pubkey:", childZpub)
fmt.Println("Root Fingerprint:", xfp)
fmt.Println("Network:", networkName)
fmt.Println("Derivation Path:", derivationPath)
fmt.Println("Specter-Desktop Input Format:")
fmt.Printf(" [%s/%s]%s\n", xfp, derivationPathSpecter, childZpub)
fmt.Println(strings.Repeat("-", 80))
if *verbosityBoolPtr == true {
fmt.Println(" Advanced Details:")
fmt.Println(" child xpub:", childXpub)
fmt.Println(" child xpriv:", childXpriv)
fmt.Println(" child xpriv (SLIP132 encoded):", childZpriv)
fmt.Println(" ", len(validChecksums), "valid checksums:", validChecksums)
}
}