-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
107 lines (99 loc) · 2.95 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
package main
import(
"bufio"
"golang.org/x/crypto/ed25519"
"flag"
"fmt"
"github.com/dignifiedquire/go-basex"
bip39 "github.com/tyler-smith/go-bip39"
"encoding/hex"
"io/ioutil"
"os"
"strconv"
)
//Alphabet used for bs58 encoding
var BASE_58_CHARSET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
func main() {
//Flag to recover a key.json from existing mnemonic
recoverPtr := flag.Bool("r", false, "recover from mnemonic")
flag.Parse()
var mnemonic string
if(*recoverPtr) {
mnemonic = recoverMnemonic()
} else {
mnemonic = ""
}
key := generateKey(mnemonic)
writeKeyToFile(key)
}
//Generates an ed25519 keypair from mnemonic, if no mnemonic is given as argument, creates a new one and prints it to cli.
func generateKey(mnemonic string) ed25519.PrivateKey {
//if no mnemonic is set, create a new one
if(mnemonic == "") {
entropy, _ := bip39.NewEntropy(128)
mnemonic, _ = bip39.NewMnemonic(entropy)
fmt.Println("#############################################################################")
fmt.Println(mnemonic)
fmt.Println("#############################################################################")
}
entropy, err := bip39.EntropyFromMnemonic(mnemonic)
if err != nil {
fmt.Println("Mnenomic not valid", err)
}
key := ed25519.NewKeyFromSeed([]byte(hex.EncodeToString(entropy)))
return key
}
//Prints out the bs58 encoded address of an ed25519 keypair
func getAddress(key ed25519.PrivateKey) string {
var pubBytes [32]byte
copy(pubBytes[:], key[32:])
bs58 := basex.NewAlphabet(BASE_58_CHARSET)
enc := bs58.Encode(pubBytes[:])
return enc
}
//Simple request to input mnemonic into cli
func getMnemonicFromUser() string {
var mnemonic string
fmt.Print("Enter mnemonic to recover from: ")
reader := bufio.NewReader(os.Stdin)
mnemonic, _ = reader.ReadString('\n')
return mnemonic
}
//Gets mnemonic from user input and verifies if it is valid. If not, asks user again and prints error
func recoverMnemonic() string {
for true {
mnemonic := getMnemonicFromUser()
_, err := bip39.EntropyFromMnemonic(mnemonic)
if err != nil {
fmt.Println("Error:",err)
}
if err == nil {
return mnemonic
}
}
return ""
}
//Takes ed25519 keypair and writes it to a file called <bs58_address>.json. Format is compatible with Solana CLI.
func writeKeyToFile(key ed25519.PrivateKey) {
intArr := make([]int, len(key))
for i,j := range key {
intArr[i] = int(j)
}
var strKey string
strKey = "["
for i, n := range intArr {
str:= strconv.Itoa(n)
strKey = strKey + str
if(i < len(intArr) - 1) {
strKey = strKey + ","
}
}
strKey += "]"
address := getAddress(key)
err := ioutil.WriteFile(address + ".json", []byte(strKey), 0644)
if(err != nil) {
fmt.Println("Error writing to file:", err)
} else {
fmt.Println("Imported Account", address)
}
}