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
pub mod user_accounts;
use base64;
use libp2p::{
floodsub::Topic,
identity::{ed25519, Keypair},
PeerId,
};
use prost::Message;
use state;
use crate::connections::{internet::Internet, lan::Lan};
use crate::rpc::Rpc;
use crate::storage::configuration::Configuration;
use crate::utilities::qaul_id::QaulId;
use user_accounts::UserAccounts;
static NODE: state::Storage<Node> = state::Storage::new();
pub mod proto {
include!("qaul.rpc.node.rs");
}
pub struct Node {
id: PeerId,
keys: Keypair,
topic: Topic,
}
impl Node {
pub fn init() {
UserAccounts::init();
{
if !Configuration::is_node_initialized() {
log::trace!("Create a new node.");
Self::new();
} else {
log::trace!("Setup node from configuration.");
Self::from_config();
}
}
}
fn new() {
let keys_ed25519 = ed25519::Keypair::generate();
let keys = Keypair::Ed25519(keys_ed25519.clone());
let id = PeerId::from(keys.public());
let topic = Topic::new("pages");
let node = Node { id, keys, topic };
{
let mut config = Configuration::get_mut();
config.node.keys = base64::encode(keys_ed25519.encode());
config.node.id = id.to_string();
config.node.initialized = 1;
}
Configuration::save();
log::trace!("Peer Id: {}", node.id.clone());
NODE.set(node);
}
fn from_config() {
let config = Configuration::get();
let mut basedecode = base64::decode(&config.node.keys).unwrap();
let keys = Keypair::Ed25519(ed25519::Keypair::decode(&mut basedecode).unwrap());
let id = PeerId::from(keys.public());
let topic = Topic::new("pages");
if id.to_string() == config.node.id {
log::trace!("id's match {}", config.node.id);
} else {
log::error!("------------------------------------");
log::error!("ERROR: id's are not equal");
log::error!("{} {}", id.to_string(), config.node.id);
log::error!("------------------------------------");
}
let node = Node { id, keys, topic };
NODE.set(node);
}
pub fn get_id() -> PeerId {
let node = NODE.get();
node.id.clone()
}
pub fn get_small_id() -> Vec<u8> {
let node = NODE.get();
QaulId::to_small(node.id)
}
pub fn get_id_string() -> String {
let node = NODE.get();
node.id.to_string()
}
pub fn get_keys<'a>() -> &'a Keypair {
let node = NODE.get();
&node.keys
}
pub fn get_topic() -> Topic {
let node = NODE.get();
node.topic.clone()
}
pub fn rpc(data: Vec<u8>, lan: Option<&mut Lan>, internet: Option<&mut Internet>) {
match proto::Node::decode(&data[..]) {
Ok(node) => {
match node.message {
Some(proto::node::Message::GetNodeInfo(_)) => {
Rpc::increase_message_counter();
let mut addresses: Vec<String> = Vec::new();
if let Some(internet_connection) = internet {
for address in internet_connection.swarm.listeners() {
addresses.push(address.to_string());
}
for address in internet_connection.swarm.external_addresses() {
addresses.push(address.addr.to_string());
}
} else if let Some(lan_connection) = lan {
for address in lan_connection.swarm.listeners() {
addresses.push(address.to_string());
}
for address in lan_connection.swarm.external_addresses() {
addresses.push(address.addr.to_string());
}
} else {
log::error!("lan & internet swarms missing");
}
let proto_nodeinformation = proto::NodeInformation {
id_base58: Self::get_id_string(),
addresses,
};
let proto_message = proto::Node {
message: Some(proto::node::Message::Info(proto_nodeinformation)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");
Rpc::send_message(
buf,
crate::rpc::proto::Modules::Node.into(),
"".to_string(),
Vec::new(),
);
}
_ => {
log::error!("rpc message undefined");
}
}
}
Err(error) => {
log::error!("{:?}", error);
}
}
}
}