-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
168 lines (130 loc) · 3.22 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package panthalassa
import (
"encoding/base64"
"encoding/hex"
"errors"
"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"
ethc "github.com/ethereum/go-ethereum/crypto"
proto "github.com/golang/protobuf/proto"
log "github.com/ipfs/go-log"
ps "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
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
}
pp, err := p.ToProtobuf()
if err != nil {
return "", err
}
rawProfile, err := proto.Marshal(pp)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(rawProfile), nil
}
// converts an ethereum public key to address
func EthPubToAddress(pub string) (string, error) {
pubRaw, err := hex.DecodeString(pub)
if err != nil {
return "", err
}
pubKey, err := ethc.DecompressPubkey(pubRaw)
if err != nil {
return "", err
}
addr := ethc.PubkeyToAddress(*pubKey)
return addr.String(), nil
}
func SetLogger(level string) error {
return log.SetLogLevel("*", level)
}
func ConnectLogger(address string) error {
if panthalassaInstance == nil {
return errors.New("you have to start panthalassa first")
}
// multi address
addr, err := ma.NewMultiaddr(address)
if err != nil {
return err
}
// peer info
pi, err := ps.InfoFromP2pAddr(addr)
if err != nil {
return err
}
return panthalassaInstance.p2p.ConnectLogger(*pi)
}