-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpanthalassa.go
More file actions
94 lines (77 loc) · 2.06 KB
/
panthalassa.go
File metadata and controls
94 lines (77 loc) · 2.06 KB
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 panthalassa
import (
"encoding/hex"
"fmt"
api "github.com/Bit-Nation/panthalassa/api"
chat "github.com/Bit-Nation/panthalassa/chat"
dapp "github.com/Bit-Nation/panthalassa/dapp"
dAppReg "github.com/Bit-Nation/panthalassa/dapp/registry"
db "github.com/Bit-Nation/panthalassa/db"
dyncall "github.com/Bit-Nation/panthalassa/dyncall"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
p2p "github.com/Bit-Nation/panthalassa/p2p"
storm "github.com/asdine/storm"
lp2pCrypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
)
type Panthalassa struct {
km *keyManager.KeyManager
upStream api.UpStream
api *api.API
p2p *p2p.Network
dAppReg *dAppReg.Registry
chat *chat.Chat
chatDB db.ChatStorage
db *storm.DB
dAppStorage dapp.Storage
dyncall *dyncall.Registry
}
//Stop the panthalassa instance
//this becomes interesting when we start
//to use the mesh network
func (p *Panthalassa) Stop() error {
var err error
err = p.db.Close()
err = p.p2p.Close()
err = p.chat.Close()
return err
}
//Export account with the given password
func (p *Panthalassa) Export(pw, pwConfirm string) (string, error) {
// export
store, err := p.km.Export(pw, pwConfirm)
if err != nil {
return "", err
}
// marshal key store
rawStore, err := store.Marshal()
if err != nil {
return "", err
}
return string(rawStore), nil
}
// add friend to peer store
func (p *Panthalassa) AddContact(pubKey string) error {
// decode public key
rawPubKey, err := hex.DecodeString(pubKey)
if err != nil {
return err
}
// create lp2p public key
lp2pPubKey, err := lp2pCrypto.UnmarshalEd25519PublicKey(rawPubKey)
if err != nil {
return err
}
// create ID from friend public key
id, err := peer.IDFromPublicKey(lp2pPubKey)
if err != nil {
return err
}
// add public key to peer store
err = p.p2p.Host.Peerstore().AddPubKey(id, lp2pPubKey)
if err != nil {
return err
}
logger.Info(fmt.Sprintf("added contact: %s", pubKey))
return p.p2p.Host.Peerstore().Put(id, "contact", true)
}