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
// // Copyright 2024 MaidSafe.net limited.
// //
// // This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// // Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// // under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// // KIND, either express or implied. Please review the Licences for the specific language governing
// // permissions and limitations relating to use of the SAFE Network Software.
// use crdts::merkle_reg::{Hash, MerkleReg, Node};
// use std::collections::HashMap;
// use std::io;
// // TODO: use autonomi API here
// // use sn_client::{acc_packet::load_account_wallet_or_create_with_mnemonic, Client, WalletClient};
// use sn_registers::{Entry, Permissions, RegisterAddress};
// use xor_name::XorName;
// use bls::SecretKey;
// use clap::Parser;
// use color_eyre::{
// eyre::{eyre, Result, WrapErr},
// Help,
// };
// #[derive(Parser, Debug)]
// #[clap(name = "register inspect cli")]
// struct Opt {
// // Create register and give it a nickname (first user)
// #[clap(long, default_value = "")]
// reg_nickname: String,
// // Get existing register with given network address (any other user)
// #[clap(long, default_value = "", conflicts_with = "reg_nickname")]
// reg_address: String,
// }
// #[tokio::main]
// async fn main() -> Result<()> {
// let opt = Opt::parse();
// let mut reg_nickname = opt.reg_nickname;
// let reg_address_string = opt.reg_address;
// // let's build a random secret key to sign our Register ops
// let signer = SecretKey::random();
// println!("Starting SAFE client...");
// let client = Client::new(signer, None, None, None).await?;
// println!("SAFE client signer public key: {:?}", client.signer_pk());
// // The address of the register to be displayed
// let mut meta = XorName::from_content(reg_nickname.as_bytes());
// let reg_address = if !reg_nickname.is_empty() {
// meta = XorName::from_content(reg_nickname.as_bytes());
// RegisterAddress::new(meta, client.signer_pk())
// } else {
// reg_nickname = format!("{reg_address_string:<6}...");
// RegisterAddress::from_hex(®_address_string)
// .wrap_err("cannot parse hex register address")?
// };
// // Loading a local wallet (for ClientRegister::sync()).
// // The wallet can have ZERO balance in this example,
// // but the ClientRegister::sync() API requires a wallet and will
// // create the register if not found even though we don't want that.
// //
// // The only want to avoid unwanted creation of a Register seems to
// // be to supply an empty wallet.
// // TODO Follow the issue about this: https://github.com/maidsafe/safe_network/issues/1308
// let root_dir = dirs_next::data_dir()
// .ok_or_else(|| eyre!("could not obtain data directory path".to_string()))?
// .join("safe")
// .join("client");
// let wallet = load_account_wallet_or_create_with_mnemonic(&root_dir, None)
// .wrap_err(format!"Unable to read wallet file in {root_dir:?}"))
// .suggestion(
// "If you have an old wallet file, it may no longer be compatible. Try removing it",
// )?;
// let mut wallet_client = WalletClient::new(client.clone(), wallet);
// println!("Retrieving Register '{reg_nickname}' from SAFE");
// let mut reg_replica = match client.get_register(reg_address).await {
// Ok(register) => {
// println!(
// "Register '{reg_nickname}' found at {:?}!",
// register.address(),
// );
// register
// }
// Err(_) => {
// println!("Register '{reg_nickname}' not found, creating it at {reg_address}");
// let (register, _cost, _royalties_fees) = client
// .create_and_pay_for_register(
// meta,
// &mut wallet_client,
// true,
// Permissions::new_anyone_can_write(),
// )
// .await?;
// register
// }
// };
// println!("Register address: {:?}", reg_replica.address().to_hex());
// println!("Register owned by: {:?}", reg_replica.owner());
// println!("Register permissions: {:?}", reg_replica.permissions());
// // Repeatedly display of the register structure on command
// loop {
// println!();
// println!(
// "Current total number of items in Register: {}",
// reg_replica.size()
// );
// println!("Latest value (more than one if concurrent writes were made):");
// println!("--------------");
// for (_, entry) in reg_replica.read().into_iter() {
// println!("{}", String::from_utf8(entry)?);
// }
// println!("--------------");
// if prompt_user() {
// return Ok(());
// }
// // Sync with network after a delay
// println!("Syncing with SAFE...");
// reg_replica.sync(&mut wallet_client, true, None).await?;
// let merkle_reg = reg_replica.merkle_reg();
// let content = merkle_reg.read();
// println!("synced!");
// // Show the Register structure
// // Index nodes to make it easier to see where a
// // node appears multiple times in the output.
// // Note: it isn't related to the order of insertion
// // which is hard to determine.
// let mut index: usize = 0;
// let mut node_ordering: HashMap<Hash, usize> = HashMap::new();
// for (_hash, node) in content.hashes_and_nodes() {
// index_node_and_descendants(node, &mut index, &mut node_ordering, merkle_reg);
// }
// println!("======================");
// println!("Root (Latest) Node(s):");
// for node in content.nodes() {
// let _ = print_node(0, node, &node_ordering);
// }
// println!("======================");
// println!("Register Structure:");
// println!("(In general, earlier nodes are more indented)");
// let mut indents = 0;
// for (_hash, node) in content.hashes_and_nodes() {
// print_node_and_descendants(&mut indents, node, &node_ordering, merkle_reg);
// }
// println!("======================");
// }
// }
// fn index_node_and_descendants(
// node: &Node<Entry>,
// index: &mut usize,
// node_ordering: &mut HashMap<Hash, usize>,
// merkle_reg: &MerkleReg<Entry>,
// ) {
// let node_hash = node.hash();
// if node_ordering.get(&node_hash).is_none() {
// node_ordering.insert(node_hash, *index);
// *index += 1;
// }
// for child_hash in node.children.iter() {
// if let Some(child_node) = merkle_reg.node(*child_hash) {
// index_node_and_descendants(child_node, index, node_ordering, merkle_reg);
// } else {
// println!("ERROR looking up hash of child");
// }
// }
// }
// fn print_node_and_descendants(
// indents: &mut usize,
// node: &Node<Entry>,
// node_ordering: &HashMap<Hash, usize>,
// merkle_reg: &MerkleReg<Entry>,
// ) {
// let _ = print_node(*indents, node, node_ordering);
// *indents += 1;
// for child_hash in node.children.iter() {
// if let Some(child_node) = merkle_reg.node(*child_hash) {
// print_node_and_descendants(indents, child_node, node_ordering, merkle_reg);
// }
// }
// *indents -= 1;
// }
// fn print_node(
// indents: usize,
// node: &Node<Entry>,
// node_ordering: &HashMap<Hash, usize>,
// ) -> Result<()> {
// let order = match node_ordering.get(&node.hash()) {
// Some(order) => format!("{order}"),
// None => String::new(),
// };
// let indentation = " ".repeat(indents);
// println!(
// "{indentation}[{:>2}] Node({:?}..) Entry({:?})",
// order,
// hex::encode(&node.hash()[0..3]),
// String::from_utf8(node.value.clone())?
// );
// Ok(())
// }
// fn prompt_user() -> bool {
// let mut input_text = String::new();
// println!();
// println!("Enter a blank line to print the latest register structure (or 'Q' <Enter> to quit)");
// io::stdin()
// .read_line(&mut input_text)
// .expect("Failed to read text from stdin");
// let string = input_text.trim().to_string();
// string.contains('Q') || string.contains('q')
// }