Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LegalService.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Media;
8using System.Windows.Media.Imaging;
14
16{
18 {
19 private ContractsClient contractsClient;
20
21 private LegalService(TreeNode Parent, string JID, string Name, string Node, Dictionary<string, bool> Features)
22 : base(Parent, JID, Name, Node, Features)
23 {
24 }
25
26 public static async Task<LegalService> Create(TreeNode Parent, string JID, string Name, string Node, Dictionary<string, bool> Features)
27 {
28 LegalService Result = new LegalService(Parent, JID, Name, Node, Features);
29
30 Result.contractsClient = new ContractsClient(Result.Account.Client, JID);
31 Result.contractsClient.SetKeySettingsInstance(string.Empty, true);
32
33 await Result.contractsClient.LoadKeys(true);
34 await Result.contractsClient.EnableE2eEncryption(true, false);
35
36 Result.contractsClient.IdentityUpdated += Result.ContractsClient_IdentityUpdated;
37
38 return Result;
39 }
40
41 public ContractsClient ContractsClient => this.contractsClient;
42
43 public override void Dispose()
44 {
45 this.contractsClient?.Dispose();
46 this.contractsClient = null;
47
48 base.Dispose();
49 }
50
51 public override ImageSource ImageResource => XmppAccountNode.legal;
52
53 public override string ToolTip
54 {
55 get
56 {
57 return "Legal Services";
58 }
59 }
60
61 public override void AddContexMenuItems(ref string CurrentGroup, ContextMenu Menu)
62 {
63 base.AddContexMenuItems(ref CurrentGroup, Menu);
64
65 MenuItem Item;
66
67 this.GroupSeparator(ref CurrentGroup, "Database", Menu);
68
69 Menu.Items.Add(Item = new MenuItem()
70 {
71 Header = "_Register Legal Identity...",
72 IsEnabled = true,
73 Icon = new Image()
74 {
75 Source = new BitmapImage(new Uri("../Graphics/Places-user-identity-icon_16.png", UriKind.Relative)),
76 Width = 16,
77 Height = 16
78 }
79 });
80
81 Item.Click += this.RegisterLegalIdentity_Click;
82
83 Menu.Items.Add(Item = new MenuItem()
84 {
85 Header = "M_y legal identities...",
86 IsEnabled = true
87 });
88
89 Item.Click += this.MyLegalIdentities_Click;
90
91 Menu.Items.Add(Item = new MenuItem()
92 {
93 Header = "_Obsolete legal identities...",
94 IsEnabled = true
95 });
96
97 Item.Click += this.ObsoleteLegalIdentities_Click;
98
99 Menu.Items.Add(Item = new MenuItem()
100 {
101 Header = "Report legal identities as _compromized...",
102 IsEnabled = true
103 });
104
105 Item.Click += this.CompromizedLegalIdentities_Click;
106 }
107
108 private async void RegisterLegalIdentity_Click(object Sender, RoutedEventArgs e)
109 {
110 try
111 {
113 {
114 Owner = MainWindow.currentInstance
115 };
116 bool? Result = Form.ShowDialog();
117 string s;
118
119 if (Result.HasValue && Result.Value)
120 {
121 List<Property> Properties = new List<Property>();
122
123 if (!string.IsNullOrEmpty(s = Form.FirstName.Text))
124 Properties.Add(new Property("FIRST", s));
125
126 if (!string.IsNullOrEmpty(s = Form.MiddleNames.Text))
127 Properties.Add(new Property("MIDDLE", s));
128
129 if (!string.IsNullOrEmpty(s = Form.LastName.Text))
130 Properties.Add(new Property("LAST", s));
131
132 if (!string.IsNullOrEmpty(s = Form.PersonalNumber.Text))
133 Properties.Add(new Property("PNR", s));
134
135 if (!string.IsNullOrEmpty(s = Form.Address.Text))
136 Properties.Add(new Property("ADDR", s));
137
138 if (!string.IsNullOrEmpty(s = Form.Address2.Text))
139 Properties.Add(new Property("ADDR2", s));
140
141 if (!string.IsNullOrEmpty(s = Form.PostalCode.Text))
142 Properties.Add(new Property("ZIP", s));
143
144 if (!string.IsNullOrEmpty(s = Form.Area.Text))
145 Properties.Add(new Property("AREA", s));
146
147 if (!string.IsNullOrEmpty(s = Form.City.Text))
148 Properties.Add(new Property("CITY", s));
149
150 if (!string.IsNullOrEmpty(s = Form.Region.Text))
151 Properties.Add(new Property("REGION", s));
152
153 if (!string.IsNullOrEmpty(s = Form.Country.Text))
154 Properties.Add(new Property("COUNTRY", s));
155
156 await this.contractsClient.GenerateNewKeys();
157 await this.contractsClient.Apply(Properties.ToArray(), (sender2, e2) =>
158 {
159 if (!e2.Ok)
160 MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to register legal identity." : e2.ErrorText);
161
162 return Task.CompletedTask;
163
164 }, null);
165 }
166 }
167 catch (Exception ex)
168 {
169 MainWindow.ErrorBox(ex.Message);
170 }
171 }
172
173 private Task ContractsClient_IdentityUpdated(object Sender, LegalIdentityEventArgs e)
174 {
175 StringBuilder Markdown = new StringBuilder();
176
177 Markdown.AppendLine("Legal identity updated:");
178 Markdown.AppendLine();
179 Output(XmppClient.GetBareJID(e.To), Markdown, e.Identity.GetTags());
180
181 MainWindow.UpdateGui(async () =>
182 {
183 await MainWindow.currentInstance.ChatMessage(XmppClient.GetBareJID(e.From), XmppClient.GetBareJID(e.To),
184 Markdown.ToString(), string.Empty, true, DateTime.Now);
185 });
186
187 return Task.CompletedTask;
188 }
189
190 internal static void Output(string JID, StringBuilder Markdown, KeyValuePair<string, object>[] Tags)
191 {
192 Markdown.AppendLine("| Legal Identity ||");
193 Markdown.AppendLine("|:------|:--------|");
194 Markdown.Append("| JID | ");
195 Markdown.Append(MarkdownDocument.Encode(JID));
196 Markdown.AppendLine(" |");
197
198 foreach (KeyValuePair<string, object> P in Tags)
199 {
200 string s = P.Key;
201
202 switch (s)
203 {
204 case "FIRST": s = "First Name"; break;
205 case "MIDDLE": s = "Middle Name(s)"; break;
206 case "LAST": s = "Last Name"; break;
207 case "PNR": s = "Personal Number"; break;
208 case "ADDR": s = "Address"; break;
209 case "ADDR2": s = "Address, row 2"; break;
210 case "ZIP": s = "Postal Code (ZIP)"; break;
211 case "AREA": s = "Area"; break;
212 case "CITY": s = "City"; break;
213 case "REGION": s = "Region (State)"; break;
214 case "COUNTRY": s = "Country"; break;
215 }
216
217 Markdown.Append("| ");
218 Markdown.Append(MarkdownDocument.Encode(s).Replace("\r\n", "\n").Replace("\n", "<br/>").Replace("\r", "<br/>"));
219 Markdown.Append(" | ");
220 Markdown.Append(MarkdownDocument.Encode(P.Value.ToString()).Replace("\r\n", "\n").Replace("\n", "<br/>").Replace("\r", "<br/>"));
221 Markdown.AppendLine(" |");
222 }
223 }
224
225 private void MyLegalIdentities_Click(object Sender, RoutedEventArgs e)
226 {
227 this.contractsClient.GetLegalIdentities((sender2, e2) =>
228 {
229 if (e2.Ok)
230 {
231 if (e2.Identities is null || e2.Identities.Length == 0)
232 MainWindow.MessageBox("No legal identities are regitered.", "Identities", MessageBoxButton.OK, MessageBoxImage.Information);
233 else
234 {
235 foreach (LegalIdentity Identity in e2.Identities)
236 {
237 StringBuilder Markdown = new StringBuilder();
238
239 Output(XmppClient.GetBareJID(e2.To), Markdown, Identity.GetTags());
240
241 MainWindow.UpdateGui(() =>
242 {
243 return MainWindow.currentInstance.ChatMessage(XmppClient.GetBareJID(e2.From), XmppClient.GetBareJID(e2.To),
244 Markdown.ToString(), string.Empty, true, DateTime.Now);
245 });
246 }
247 }
248 }
249 else
250 MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to get list of identities." : e2.ErrorText);
251
252 return Task.CompletedTask;
253
254 }, null);
255 }
256
257 private void ObsoleteLegalIdentities_Click(object Sender, RoutedEventArgs e)
258 {
259 if (MessageBox.Show(MainWindow.currentInstance, "Are you sure you want to obsolete registered legal identities?",
260 "Confirmation", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes) != MessageBoxResult.Yes)
261 {
262 return;
263 }
264
265 this.contractsClient.GetLegalIdentities((sender2, e2) =>
266 {
267 if (e2.Ok)
268 {
269 if (e2.Identities is null || e2.Identities.Length == 0)
270 MainWindow.MessageBox("No legal identities are regitered.", "Identities", MessageBoxButton.OK, MessageBoxImage.Information);
271 else
272 {
273 int Nr = 0;
274
275 foreach (LegalIdentity Identity in e2.Identities)
276 {
277 if (Identity.State == IdentityState.Approved || Identity.State == IdentityState.Created)
278 {
279 this.contractsClient.ObsoleteLegalIdentity(Identity.Id, null, null);
280 Nr++;
281 }
282 }
283
284 if (Nr == 0)
285 MainWindow.MessageBox("No legal identities found to obsolete.", "Identities", MessageBoxButton.OK, MessageBoxImage.Information);
286 }
287 }
288 else
289 MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to get list of identities." : e2.ErrorText);
290
291 return Task.CompletedTask;
292
293 }, null);
294 }
295
296 private void CompromizedLegalIdentities_Click(object Sender, RoutedEventArgs e)
297 {
298 if (MessageBox.Show(MainWindow.currentInstance, "Are you sure you want to report your registered legal identities as compromized?",
299 "Confirmation", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes) != MessageBoxResult.Yes)
300 {
301 return;
302 }
303
304 this.contractsClient.GetLegalIdentities((sender2, e2) =>
305 {
306 if (e2.Ok)
307 {
308 if (e2.Identities is null || e2.Identities.Length == 0)
309 MainWindow.MessageBox("No legal identities are regitered.", "Identities", MessageBoxButton.OK, MessageBoxImage.Information);
310 else
311 {
312 int Nr = 0;
313
314 foreach (LegalIdentity Identity in e2.Identities)
315 {
316 if (Identity.State == IdentityState.Approved ||
317 Identity.State == IdentityState.Created ||
318 Identity.State == IdentityState.Obsoleted)
319 {
320 this.contractsClient.CompromisedLegalIdentity(Identity.Id, null, null);
321 Nr++;
322 }
323 }
324
325 if (Nr == 0)
326 MainWindow.MessageBox("No legal identities found to report as compromized.", "Identities", MessageBoxButton.OK, MessageBoxImage.Information);
327 }
328 }
329 else
330 MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to get list of identities." : e2.ErrorText);
331
332 return Task.CompletedTask;
333
334 }, null);
335 }
336
337 }
338}
Interaction logic for xaml
Abstract base class for tree nodes in the connection view.
Definition: TreeNode.cs:24
TreeNode Parent
Parent node. May be null if a root node.
Definition: TreeNode.cs:107
Class representing a normal XMPP account.
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
Adds support for legal identities, smart contracts and signatures to an XMPP client.
async Task GenerateNewKeys()
Generates new keys for the contracts clients.
Task< bool > LoadKeys(bool CreateIfNone)
Loads keys from the underlying persistence layer.
override void Dispose()
Disposes of the extension.
Task EnableE2eEncryption(bool UseLocalKeys)
Defines if End-to-End encryption should use the keys used by the contracts client to perform signatur...
Task GetLegalIdentities(EventHandlerAsync< LegalIdentitiesEventArgs > Callback, object State)
Gets legal identities registered with the account.
Task Apply(Property[] Properties, EventHandlerAsync< LegalIdentityEventArgs > Callback, object State)
Applies for a legal identity to be registered.
void SetKeySettingsInstance(string InstanceName, bool Locked)
Sets the key settings instance name.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
static string GetBareJID(string JID)
Gets the Bare JID from a JID, which may be a Full JID.
Definition: XmppClient.cs:6901