From 4c8173086e9b8656450ffe998362c27c4010e1bc Mon Sep 17 00:00:00 2001 From: moncho Date: Sun, 3 Sep 2017 11:48:06 +0200 Subject: [PATCH] Print QR codes for the public Wallet address and the private key --- main.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 775834a..42d85af 100644 --- a/main.go +++ b/main.go @@ -2,27 +2,29 @@ package main import ( "bufio" + "bytes" "fmt" "os" "strings" "github.com/moncho/warpwallet/bitcoin" + "github.com/moncho/warpwallet/qr" + "github.com/moncho/warpwallet/terminal" "github.com/moncho/warpwallet/warp" ) func main() { - stat, _ := os.Stdin.Stat() - piped := false - if (stat.Mode() & os.ModeCharDevice) == 0 { - piped = true + piped := seedsFromPipe() + pass, salt := promptSeeds(piped) + + if pass == "" || salt == "" { + fmt.Println("A pass and some salt are required") + os.Exit(-1) } - pass, salt := askForSeeds(piped) key, err := warp.NewSeed(pass, salt) - //log.Printf("Generating wallet seed from %s, %s", pass, salt) - if err != nil { fmt.Printf("Could not generate wallet seed from %s, %s: %s", pass, salt, err.Error()) os.Exit(-1) @@ -32,21 +34,50 @@ func main() { if wif == "" || pubAddress == "" { os.Exit(-1) } - fmt.Printf("Public bitcoin address: %v\n", pubAddress) - fmt.Printf("Private key (don't share): %v\n", wif) + var wifQR bytes.Buffer + if err := qr.Encode(wif, &wifQR); err != nil { + fmt.Printf("Could not generate QR code for WIF: %s", err.Error()) + os.Exit(-1) + } + var pubAddressQR bytes.Buffer + + if err := qr.Encode(pubAddress, &pubAddressQR); err != nil { + fmt.Printf("Could not generate QR code for pubAddress: %s", err.Error()) + os.Exit(-1) + } + 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 askForSeeds(piped bool) (string, string) { +func promptSeeds(piped bool) (string, string) { reader := bufio.NewReader(os.Stdin) if !piped { - fmt.Print("Passphrase: ") + fmt.Print( + terminal.White("Passphrase: ")) } pass, _ := reader.ReadString('\n') pass = strings.Trim(pass, "\n") if !piped { - fmt.Print("Your email [as a salt]: ") + fmt.Print( + terminal.White("Your email [as a salt]: ")) } salt, _ := reader.ReadString('\n') salt = strings.Trim(salt, "\n") @@ -72,3 +103,12 @@ func generate(key []byte) (string, string) { return wif, address } + +func seedsFromPipe() bool { + stat, _ := os.Stdin.Stat() + piped := false + if (stat.Mode() & os.ModeCharDevice) == 0 { + piped = true + } + return piped +}