-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.go
More file actions
105 lines (88 loc) · 2.86 KB
/
Copy pathapp.go
File metadata and controls
105 lines (88 loc) · 2.86 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
95
96
97
98
99
100
101
102
103
104
105
package main
import (
backend "MessageMesh/backend"
"MessageMesh/backend/models"
"context"
)
// App struct
type App struct {
ctx context.Context
network backend.Network
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Start the network, connect to peers and join the blockchain
a.network.ConnectToNetwork()
// Start the UI Data loop
go backend.UIDataLoop(a.network, a.ctx)
}
// Functions for the UI to get data from the network
// Get the list of peers in the network
func (a *App) GetPeerList() []string {
peers := make([]string, 0)
for _, peer := range a.network.PubSubService.PeerList() {
peers = append(peers, peer.String())
}
return peers
}
// Get the user's peer ID
func (a *App) GetUserPeerID() string {
return a.network.PubSubService.SelfID().String()
}
// Send a message to a peer (Not in use by the UI)
func (a *App) SendMessage(message string, receiver string) {
a.network.SendMessage(message, receiver)
}
// Send an encrypted message to a peer
func (a *App) SendEncryptedMessage(message string, receiver string) {
a.network.SendEncryptedMessage(message, receiver)
}
// Get the blockchain (Not in use by the UI)
func (a *App) GetBlockchain() []*models.Block {
return a.network.ConsensusService.Blockchain.Chain
}
// Get the messages from the blockchain (Not in use by the UI)
func (a *App) GetMessages() []*models.Message {
messages := make([]*models.Message, 0)
for _, block := range a.network.ConsensusService.Blockchain.Chain {
if block.BlockType == "message" {
messages = append(messages, &block.Data.(*models.MessageData).Message)
}
}
return messages
}
// Get a decrypted message from the blockchain
func (a *App) GetDecryptedMessage(message string, peerIDs []string) (string, error) {
return a.network.DecryptMessage(message, peerIDs)
}
// Get the messages from a specific peer
func (a *App) GetMessagesFromPeer(peer string) []*models.Message {
messages := make([]*models.Message, 0)
for _, block := range a.network.ConsensusService.Blockchain.Chain {
if block.BlockType == "message" {
if block.Data.(*models.MessageData).Message.Sender == peer || block.Data.(*models.MessageData).Message.Receiver == peer {
messages = append(messages, &block.Data.(*models.MessageData).Message)
}
}
}
return messages
}
// Get the accounts from the blockchain (Not in use by the UI)
func (a *App) GetAccounts() []*models.Account {
accounts := make([]*models.Account, 0)
for _, block := range a.network.ConsensusService.Blockchain.Chain {
if block.BlockType == "account" {
accounts = append(accounts, &block.Data.(*models.AccountData).Account)
}
}
return accounts
}
func (a *App) SetTopic(topic string) {
//a.network.PubSubService.Topic = topic
}