/
main.go
123 lines (103 loc) · 2.76 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"strings"
"github.com/moncho/warpwallet/bitcoin"
"github.com/moncho/warpwallet/qr"
"github.com/moncho/warpwallet/terminal"
"github.com/moncho/warpwallet/warp"
)
var quiet bool
func main() {
flag.BoolVar(&quiet, "quiet", false, "just print the private key")
flag.BoolVar(&quiet, "q", false, "just print the private key (shorthand)")
flag.Parse()
piped := seedsFromPipe()
pass, salt := promptSeeds(piped)
if pass == "" || salt == "" {
fmt.Println("A pass and some salt are required")
os.Exit(-1)
}
key, err := warp.NewSeed(pass, salt)
if err != nil {
fmt.Printf("Could not generate wallet seed from %s, %s: %s", pass, salt, err.Error())
os.Exit(-1)
}
wif, pubAddress := generate(key)
if wif == "" || pubAddress == "" {
os.Exit(-1)
}
var wifQR bytes.Buffer
if err := qr.Copy(&wifQR, strings.NewReader(wif)); err != nil {
fmt.Printf("Could not generate QR code for WIF: %s", err.Error())
os.Exit(-1)
}
var pubAddressQR bytes.Buffer
if err := qr.Copy(&pubAddressQR, strings.NewReader(pubAddress)); err != nil {
fmt.Printf("Could not generate QR code for pubAddress: %s", err.Error())
os.Exit(-1)
}
if quiet {
fmt.Println(wif)
} else {
print(wif, wifQR.String(), pubAddress, pubAddressQR.String())
}
}
func print(wif, wifQR, pubAddress, pubAddressQR string) {
fmt.Printf("\n%s %s\t\t %s %s\n\n",
terminal.White("Public bitcoin address:"),
terminal.Yellow(pubAddress),
terminal.White("Private key (don't share):"),
terminal.Red(wif))
pub := strings.Split(pubAddressQR, "\n")
wif2 := strings.Split(wifQR, "\n")
for i, line := range pub {
fmt.Printf("%s\t\t%s\n", string(line), string(wif2[i]))
}
}
func promptSeeds(piped bool) (string, string) {
reader := bufio.NewReader(os.Stdin)
if !piped {
fmt.Print(
terminal.White("Passphrase: "))
}
pass, _ := reader.ReadString('\n')
pass = strings.Trim(pass, "\n")
if !piped {
fmt.Print(
terminal.White("Your email [as a salt]: "))
}
salt, _ := reader.ReadString('\n')
salt = strings.Trim(salt, "\n")
return pass, salt
}
func generate(key []byte) (string, string) {
priv, err := bitcoin.NewBitcoinPrivKey(key)
if err != nil {
fmt.Printf("Could not generate wallet private key: %s", err.Error())
return "", ""
}
wif, err := bitcoin.PrivateToWIF(*priv)
if err != nil {
fmt.Printf("Could not generate WIF from private key: %s", err.Error())
return "", ""
}
address, err := bitcoin.ToBTCAddress(priv.PublicKey)
if err != nil {
fmt.Printf("Could not generate compressed Bitcoin address from public key: %s", err.Error())
return "", ""
}
return wif, address
}
func seedsFromPipe() bool {
stat, _ := os.Stdin.Stat()
piped := false
if (stat.Mode() & os.ModeCharDevice) == 0 {
piped = true
}
return piped
}