-
Notifications
You must be signed in to change notification settings - Fork 75
/
keys.go
94 lines (81 loc) · 2.55 KB
/
keys.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
package util
import (
"encoding/hex"
"fmt"
"strings"
"github.com/tyler-smith/go-bip32"
"github.com/tyler-smith/go-bip39"
)
const (
NBitsForKeypairDefault = 2048
defaultEntropy = 128
mnemonicLength = 12
)
func GenerateKey(importKey string, keyType string, seedPhrase string) (string, string, error) {
mnemonicLen := len(strings.Split(seedPhrase, ","))
mnemonic := strings.ReplaceAll(seedPhrase, ",", " ")
if importKey != "" && keyType != "" && strings.ToLower(keyType) != "secp256k1" {
return "", "", fmt.Errorf("cannot specify key type and import TRON private key at the same time")
} else if seedPhrase != "" {
if mnemonicLen != mnemonicLength {
return "", "", fmt.Errorf("The seed phrase required to generate TRON private key needs to contain 12 words. Provided mnemonic has %v words.", mnemonicLen)
}
if err := !bip39.IsMnemonicValid(mnemonic); err {
return "", "", fmt.Errorf("Entered seed phrase is not valid")
}
fmt.Println("Generating TRON key with BIP39 seed phrase...")
return GeneratePrivKeyUsingBIP39(mnemonic)
} else if (keyType == "" && importKey == "") || keyType == "BIP39" {
fmt.Println("Generating TRON key with BIP39 seed phrase...")
return GeneratePrivKeyUsingBIP39("")
} else {
return importKey, mnemonic, nil
}
}
func GeneratePrivKeyUsingBIP39(mnemonic string) (string, string, error) {
if mnemonic == "" {
entropy, err := bip39.NewEntropy(defaultEntropy)
if err != nil {
return "", "", err
}
mnemonic, err = bip39.NewMnemonic(entropy)
if err != nil {
return "", "", err
}
}
if !bip39.IsMnemonicValid(mnemonic) {
return "", "", fmt.Errorf("invalid mnemonic: %s", mnemonic)
}
// Generate a Bip32 HD wallet for the mnemonic and a user supplied password
seed := bip39.NewSeed(mnemonic, "")
masterKey, err := bip32.NewMasterKey(seed)
if err != nil {
return "", "", err
}
publicKey := masterKey.PublicKey()
childKey, err := masterKey.NewChildKey(44 + bip32.FirstHardenedChild)
if err != nil {
return "", "", err
}
childKey2, err := childKey.NewChildKey(195 + bip32.FirstHardenedChild)
if err != nil {
return "", "", err
}
childKey3, err := childKey2.NewChildKey(0 + bip32.FirstHardenedChild)
if err != nil {
return "", "", err
}
childKey4, err := childKey3.NewChildKey(0)
if err != nil {
return "", "", err
}
childKey5, err := childKey4.NewChildKey(0)
if err != nil {
return "", "", err
}
encoding := childKey5.Key
importKey := hex.EncodeToString(encoding)
// Display mnemonic and keys
fmt.Println("Master public key: ", publicKey)
return importKey, mnemonic, nil
}