This repository was archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmobile.go
350 lines (300 loc) · 7.39 KB
/
mobile.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package mobile
import (
"github.com/golang/protobuf/proto"
logging "github.com/ipfs/go-log"
mh "github.com/multiformats/go-multihash"
"github.com/textileio/go-textile/broadcast"
"github.com/textileio/go-textile/common"
"github.com/textileio/go-textile/core"
"github.com/textileio/go-textile/keypair"
"github.com/textileio/go-textile/pb"
"github.com/textileio/go-textile/wallet"
)
var log = logging.Logger("tex-mobile")
// Messenger is a push mechanism to the bridge
type Messenger interface {
Notify(event *Event)
}
// Event is sent by Messenger to the bridge (data is a protobuf,
// name is the string value of a pb.MobileEvent_Type)
type Event struct {
Name string
Type int32
Data []byte
}
// Callback is used for asyc methods
type Callback interface {
Call(err error)
}
// ProtoCallback is used for asyc methods that deliver a protobuf message
type ProtoCallback interface {
Call(msg []byte, err error)
}
// DataCallback is used for asyc methods that deliver raw data
type DataCallback interface {
Call(data []byte, media string, err error)
}
// NewWallet creates a brand new wallet and returns its recovery phrase
func NewWallet(wordCount int) (string, error) {
w, err := wallet.WalletFromWordCount(wordCount)
if err != nil {
return "", err
}
return w.RecoveryPhrase, nil
}
// WalletAccountAt derives the account at the given index
func WalletAccountAt(mnemonic string, index int, passphrase string) ([]byte, error) {
w := wallet.WalletFromMnemonic(mnemonic)
account, err := w.AccountAt(index, passphrase)
if err != nil {
return nil, err
}
return proto.Marshal(&pb.MobileWalletAccount{
Seed: account.Seed(),
Address: account.Address(),
})
}
// InitConfig is used to setup a textile node
type InitConfig struct {
Seed string
RepoPath string
BaseRepoPath string
LogToDisk bool
Debug bool
}
// MigrateConfig is used to define options during a major migration
type MigrateConfig struct {
RepoPath string
}
// RunConfig is used to define run options for a mobile node
type RunConfig struct {
RepoPath string
Debug bool
CafeOutboxHandler core.CafeOutboxHandler
}
// Mobile is the name of the framework (must match package name)
type Mobile struct {
RepoPath string
node *core.Textile
messenger Messenger
listener *broadcast.Listener
}
// Repo returns the actual location of the configured repo
func (conf InitConfig) Repo() (string, error) {
coreConf, err := conf.coreInitConfig()
if err != nil {
return "", err
}
repo, err := coreConf.Repo()
if err != nil {
return "", err
}
return repo, nil
}
// RepoExists return whether or not the configured repo already exists
func (conf InitConfig) RepoExists() (bool, error) {
coreConf, err := conf.coreInitConfig()
if err != nil {
return false, err
}
exists, err := coreConf.RepoExists()
if err != nil {
return false, err
}
return exists, nil
}
// RepoExists return whether or not the repo at repoPath exists
func RepoExists(repoPath string) bool {
return core.RepoExists(repoPath)
}
// AccountRepoExists return whether or not the repo at repoPath exists
func AccountRepoExists(baseRepoPath string, accountAddress string) bool {
return core.AccountRepoExists(baseRepoPath, accountAddress)
}
func (conf InitConfig) coreInitConfig() (core.InitConfig, error) {
var accnt *keypair.Full
if len(conf.Seed) > 0 {
var err error
accnt, err = toAccount(conf.Seed)
if err != nil {
return core.InitConfig{}, err
}
}
return core.InitConfig{
Account: accnt,
RepoPath: conf.RepoPath,
BaseRepoPath: conf.BaseRepoPath,
IsMobile: true,
LogToDisk: conf.LogToDisk,
Debug: conf.Debug,
}, nil
}
func toAccount(seed string) (*keypair.Full, error) {
kp, err := keypair.Parse(seed)
if err != nil {
return nil, err
}
accnt, ok := kp.(*keypair.Full)
if !ok {
return nil, keypair.ErrInvalidKey
}
return accnt, nil
}
// InitRepo calls core InitRepo
func InitRepo(config *InitConfig) error {
coreConf, err := config.coreInitConfig()
if err != nil {
return err
}
return core.InitRepo(coreConf)
}
// MigrateRepo calls core MigrateRepo
func MigrateRepo(config *MigrateConfig) error {
return core.MigrateRepo(core.MigrateConfig{
RepoPath: config.RepoPath,
})
}
// Create a gomobile compatible wrapper around Textile
func NewTextile(config *RunConfig, messenger Messenger) (*Mobile, error) {
mobile := &Mobile{
RepoPath: config.RepoPath,
messenger: messenger,
}
node, err := core.NewTextile(core.RunConfig{
RepoPath: config.RepoPath,
CafeOutboxHandler: config.CafeOutboxHandler,
CheckMessages: mobile.checkCafeMessages,
Debug: config.Debug,
})
if err != nil {
return nil, err
}
mobile.node = node
mobile.listener = node.ThreadUpdateListener()
return mobile, nil
}
// Start the mobile node
func (m *Mobile) Start() error {
if err := m.node.Start(); err != nil {
if err == core.ErrStarted {
return nil
}
return err
}
go func() {
<-m.node.OnlineCh()
// subscribe to account updates
go func() {
for {
select {
case update, ok := <-m.node.UpdateCh():
if !ok {
return
}
m.notify(pb.MobileEventType_ACCOUNT_UPDATE, update)
}
}
}()
// subscribe to thread updates
go func() {
for {
select {
case value, ok := <-m.listener.Ch:
if !ok {
return
}
if update, ok := value.(*pb.FeedItem); ok {
m.notify(pb.MobileEventType_THREAD_UPDATE, update)
}
}
}
}()
// subscribe to notifications
go func() {
for {
select {
case note, ok := <-m.node.NotificationCh():
if !ok {
return
}
m.notify(pb.MobileEventType_NOTIFICATION, note)
}
}
}()
m.notify(pb.MobileEventType_NODE_ONLINE, nil)
}()
m.notify(pb.MobileEventType_NODE_START, nil)
return nil
}
// Stop the mobile node
func (m *Mobile) Stop(cb Callback) {
go func() {
cb.Call(m.stop())
}()
}
// stop is the sync version of Stop
func (m *Mobile) stop() error {
if err := m.node.Stop(); err != nil {
if err == core.ErrStopped {
return nil
}
return err
}
m.notify(pb.MobileEventType_NODE_STOP, nil)
return nil
}
// Online returns core Online
func (m *Mobile) Online() bool {
return m.node.Online()
}
// WaitAdd calls core WaitAdd
func (m *Mobile) WaitAdd(delta int, src string) {
m.node.WaitAdd(delta, src)
}
// WaitDone marks a wait as done in the stop wait group
func (m *Mobile) WaitDone(src string) {
m.node.WaitDone(src)
}
// Version returns common Version
func (m *Mobile) Version() string {
return "v" + common.Version
}
// GitSummary returns common GitSummary
func (m *Mobile) GitSummary() string {
return common.GitSummary
}
// Summary calls core Summary
func (m *Mobile) Summary() ([]byte, error) {
if !m.node.Started() {
return nil, core.ErrStopped
}
return proto.Marshal(m.node.Summary())
}
// onlineCh returns core OnlineCh
func (m *Mobile) onlineCh() <-chan struct{} {
return m.node.OnlineCh()
}
// blockView returns marshaled view of a block
func (m *Mobile) blockView(hash mh.Multihash) ([]byte, error) {
view, err := m.node.BlockView(hash.B58String())
if err != nil {
return nil, err
}
return proto.Marshal(view)
}
func (m *Mobile) notify(etype pb.MobileEventType, msg proto.Message) {
var data []byte
if msg != nil {
var err error
data, err = proto.Marshal(msg)
if err != nil {
log.Error(err.Error())
return
}
}
m.messenger.Notify(&Event{
Name: etype.String(),
Type: int32(etype),
Data: data,
})
}