forked from Bit-Nation/panthalassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
115 lines (86 loc) · 2.13 KB
/
utils.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
package panthalassa
import (
"strings"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
keyStore "github.com/Bit-Nation/panthalassa/keyStore"
mnemonic "github.com/Bit-Nation/panthalassa/mnemonic"
profile "github.com/Bit-Nation/panthalassa/profile"
bip39 "github.com/tyler-smith/go-bip39"
)
//Creates an new set of encrypted account key's
func NewAccountKeys(pw, pwConfirm string) (string, error) {
//Create mnemonic
mn, err := mnemonic.New()
if err != nil {
return "", err
}
//Create KeyStore
ks, err := keyStore.NewFromMnemonic(mn)
if err != nil {
return "", err
}
km := keyManager.CreateFromKeyStore(ks)
// export store
store, err := km.Export(pw, pwConfirm)
if err != nil {
return "", err
}
rawStore, err := store.Marshal()
if err != nil {
return "", err
}
return string(rawStore), nil
}
//Create new account store from mnemonic
//This can e.g. be used in case you need to recover your account
func NewAccountKeysFromMnemonic(mne, pw, pwConfirm string) (string, error) {
//Create mnemonic
mn, err := mnemonic.FromString(mne)
if err != nil {
return "", err
}
//Create key store from mnemonic
ks, err := keyStore.NewFromMnemonic(mn)
if err != nil {
return "", err
}
//Create keyManager
km := keyManager.CreateFromKeyStore(ks)
store, err := km.Export(pw, pwConfirm)
if err != nil {
return "", err
}
rawStore, err := store.Marshal()
if err != nil {
return "", err
}
return string(rawStore), nil
}
//Check if mnemonic is valid
func IsValidMnemonic(mne string) bool {
words := strings.Split(mne, " ")
if len(words) != 24 {
return false
}
return bip39.IsMnemonicValid(mne)
}
// sign profile
func SignProfileStandAlone(name, location, image, keyManagerStore, password string) (string, error) {
store, err := keyManager.UnmarshalStore([]byte(keyManagerStore))
if err != nil {
return "", err
}
p, err := profile.SignWithKeyManagerStore(name, location, image, store, password)
if err != nil {
return "", err
}
_, err = p.SignaturesValid()
if err != nil {
return "", err
}
rawProfile, err := p.Marshal()
if err != nil {
return "", err
}
return string(rawProfile), nil
}