forked from Bit-Nation/panthalassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobile_interface.go
241 lines (180 loc) · 4.91 KB
/
mobile_interface.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package panthalassa
import (
"encoding/json"
"errors"
deviceApi "github.com/Bit-Nation/panthalassa/api/device"
chat "github.com/Bit-Nation/panthalassa/chat"
clientImpl "github.com/Bit-Nation/panthalassa/client"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
mesh "github.com/Bit-Nation/panthalassa/mesh"
profile "github.com/Bit-Nation/panthalassa/profile"
log "github.com/ipfs/go-log"
)
var panthalassaInstance *Panthalassa
var logger = log.Logger("panthalassa")
type UpStream interface {
Send(data string)
}
type StartConfig struct {
EncryptedKeyManager string `json:"encrypted_key_manager"`
SignedProfile string `json:"signed_profile"`
}
// create a new panthalassa instance
func start(km *keyManager.KeyManager, config StartConfig, client UpStream) error {
//Exit if instance was already created and not stopped
if panthalassaInstance != nil {
return errors.New("call stop first in order to create a new panthalassa instance")
}
//Mesh network
pk, err := km.MeshPrivateKey()
if err != nil {
return err
}
// device api
api := deviceApi.New(client)
// we don't need the rendevouz key for now
m, errReporter, err := mesh.New(pk, api, "", config.SignedProfile)
if err != nil {
return err
}
//Report error's from mesh network to current logger
go func() {
for {
select {
case err := <-errReporter:
logger.Error(err)
}
}
}()
chatKeyPair, err := km.ChatIdKeyPair()
if err != nil {
return err
}
c, err := chat.New(chatKeyPair, km, clientImpl.New(api, km))
if err != nil {
return err
}
//Create panthalassa instance
panthalassaInstance = &Panthalassa{
km: km,
upStream: client,
deviceApi: api,
mesh: m,
chat: &c,
}
return nil
}
// start panthalassa
func Start(config string, password string, client UpStream) error {
// unmarshal config
var c StartConfig
if err := json.Unmarshal([]byte(config), &c); err != nil {
return err
}
store, err := keyManager.UnmarshalStore([]byte(c.EncryptedKeyManager))
if err != nil {
return err
}
// open key manager with password
km, err := keyManager.OpenWithPassword(store, password)
if err != nil {
return err
}
return start(km, c, client)
}
// create a new panthalassa instance with the mnemonic
func StartFromMnemonic(config string, mnemonic string, client UpStream) error {
// unmarshal config
var c StartConfig
if err := json.Unmarshal([]byte(config), &c); err != nil {
return err
}
store, err := keyManager.UnmarshalStore([]byte(c.EncryptedKeyManager))
if err != nil {
return err
}
// create key manager
km, err := keyManager.OpenWithMnemonic(store, mnemonic)
if err != nil {
return err
}
// create panthalassa instance
return start(km, c, client)
}
//Eth Private key
func EthPrivateKey() (string, error) {
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa")
}
return panthalassaInstance.km.GetEthereumPrivateKey()
}
func EthAddress() (string, error) {
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa")
}
return panthalassaInstance.km.GetEthereumAddress()
}
func SendResponse(id string, data string) error {
if panthalassaInstance == nil {
return errors.New("you have to start panthalassa")
}
return panthalassaInstance.deviceApi.Receive(id, data)
}
//Export the current account store with given password
func ExportAccountStore(pw, pwConfirm string) (string, error) {
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa")
}
return panthalassaInstance.Export(pw, pwConfirm)
}
func IdentityPublicKey() (string, error) {
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa")
}
return panthalassaInstance.km.IdentityPublicKey()
}
func GetMnemonic() (string, error) {
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa")
}
return panthalassaInstance.km.GetMnemonic().String(), nil
}
func SignProfile(name, location, image string) (string, error) {
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa")
}
p, err := profile.SignProfile(name, location, image, *panthalassaInstance.km)
if err != nil {
return "", err
}
rawProfile, err := p.Marshal()
if err != nil {
return "", err
}
return string(rawProfile), nil
}
//Stop panthalassa
func Stop() error {
//Exit if not started
if panthalassaInstance == nil {
return errors.New("you have to start panthalassa in order to stop it")
}
//Stop panthalassa
err := panthalassaInstance.Stop()
if err != nil {
//Reset singleton
panthalassaInstance = nil
return err
}
//Reset singleton
panthalassaInstance = nil
return nil
}
// fetch the identity public key of the
func GetIdentityPublicKey() (string, error) {
//Exit if not started
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa first")
}
return panthalassaInstance.km.IdentityPublicKey()
}