11using System.Globalization;
12using System.Reflection;
27 public ContractOrchestratorService()
31 public override Task Load(
bool IsResuming, CancellationToken CancellationToken)
33 if (this.
BeginLoad(IsResuming, CancellationToken))
35 ServiceRef.XmppService.ConnectionStateChanged += this.Contracts_ConnectionStateChanged;
36 ServiceRef.XmppService.PetitionForPeerReviewIdReceived += this.Contracts_PetitionForPeerReviewIdReceived;
37 ServiceRef.XmppService.PetitionForIdentityReceived += this.Contracts_PetitionForIdentityReceived;
38 ServiceRef.XmppService.PetitionForSignatureReceived += this.Contracts_PetitionForSignatureReceived;
39 ServiceRef.XmppService.PetitionedIdentityResponseReceived += this.Contracts_PetitionedIdentityResponseReceived;
40 ServiceRef.XmppService.PetitionedPeerReviewIdResponseReceived += this.Contracts_PetitionedPeerReviewResponseReceived;
41 ServiceRef.XmppService.SignaturePetitionResponseReceived += this.Contracts_SignaturePetitionResponseReceived;
42 ServiceRef.XmppService.ContractProposalReceived += this.Contracts_ContractProposalRecieved;
47 return Task.CompletedTask;
50 public override Task Unload()
54 ServiceRef.XmppService.ConnectionStateChanged -= this.Contracts_ConnectionStateChanged;
55 ServiceRef.XmppService.PetitionForPeerReviewIdReceived -= this.Contracts_PetitionForPeerReviewIdReceived;
56 ServiceRef.XmppService.PetitionForIdentityReceived -= this.Contracts_PetitionForIdentityReceived;
57 ServiceRef.XmppService.PetitionForSignatureReceived -= this.Contracts_PetitionForSignatureReceived;
58 ServiceRef.XmppService.PetitionedIdentityResponseReceived -= this.Contracts_PetitionedIdentityResponseReceived;
59 ServiceRef.XmppService.PetitionedPeerReviewIdResponseReceived -= this.Contracts_PetitionedPeerReviewResponseReceived;
60 ServiceRef.XmppService.SignaturePetitionResponseReceived -= this.Contracts_SignaturePetitionResponseReceived;
61 ServiceRef.XmppService.ContractProposalReceived -= this.Contracts_ContractProposalRecieved;
66 return Task.CompletedTask;
69 #region Event Handlers
71 private async Task Contracts_PetitionForPeerReviewIdReceived(
object? Sender, SignaturePetitionEventArgs e)
77 if (Identity?.Properties is not
null)
120 e.PetitionId, e.RequestorFullJid,
false);
126 PetitionPeerReviewNavigationArgs Args =
new(Identity, e.RequestorFullJid, e.SignatoryIdentityId, e.PetitionId,
127 e.Purpose, e.ContentToSign);
138 private async Task Contracts_PetitionForIdentityReceived(
object? Sender, LegalIdentityPetitionEventArgs e)
150 if (!Succeeded || LegalId is
null)
156 if (Identity is
null)
159 new KeyValuePair<string, object?>(
"Type", this.GetType().Name),
160 new KeyValuePair<string, object?>(
"Method", nameof(Contracts_PetitionForIdentityReceived)));
171 e.RequestedIdentityId, e.PetitionId, e.RequestorFullJid,
false);
176 Identity = e.RequestorIdentity;
178 if (Identity is not
null)
181 Identity, e.RequestorFullJid, e.RequestedIdentityId, e.PetitionId, e.Purpose));
191 private async Task Contracts_PetitionForSignatureReceived(
object? Sender, SignaturePetitionEventArgs e)
202 if (!Succeeded || LegalId is
null)
208 if (Identity is
null)
211 new KeyValuePair<string, object?>(
"Type", this.GetType().Name),
212 new KeyValuePair<string, object?>(
"Method", nameof(Contracts_PetitionForSignatureReceived)));
222 e.SignatoryIdentityId, e.ContentToSign, [], e.PetitionId, e.RequestorFullJid,
false);
227 Identity = e.RequestorIdentity;
229 if (Identity is not
null)
235 Identity, e.RequestorFullJid, e.SignatoryIdentityId, e.ContentToSign, e.PetitionId, e.Purpose));
245 private async Task Contracts_PetitionedIdentityResponseReceived(
object? Sender, LegalIdentityPetitionResponseEventArgs e)
251 if (!e.Response || Identity is
null)
267 private async Task Contracts_PetitionedPeerReviewResponseReceived(
object? Sender, SignaturePetitionResponseEventArgs e)
272 if (ReviewedIdentity is
null)
286 if (ReviewerIdentity is
null)
291 StringBuilder Xml =
new();
292 ReviewedIdentity.
Serialize(Xml,
true,
true,
true,
true,
true,
true,
true);
293 string s = Xml.ToString();
294 byte[] Data = Encoding.UTF8.GetBytes(s);
307 if (!Result.HasValue || !Result.Value)
311 ServiceRef.
Localizer[nameof(AppResources.APeerYouRequestedToReviewHasBeenRejectedDueToSignatureError)],
346 private async Task Contracts_ContractProposalRecieved(
object? sender, ContractProposalEventArgs e)
353 Contract,
false, e.Role, e.MessageText));
361 private Task Contracts_ConnectionStateChanged(
object _,
XmppState NewState)
369 Task _2 = Task.Run(async () =>
374 await ReDownloadLegalIdentity();
389 return Task.CompletedTask;
394 protected static async Task ReDownloadLegalIdentity()
422 if (Identity is not
null)
424 MainThread.BeginInvokeOnMainThread(async () =>
426 string? userMessage =
null;
427 bool gotoRegistrationPage =
false;
431 userMessage = ServiceRef.Localizer[nameof(AppResources.YourLegalIdentityHasBeenCompromised)];
432 await ServiceRef.TagProfile.CompromiseLegalIdentity(Identity);
433 gotoRegistrationPage = true;
437 userMessage = ServiceRef.Localizer[nameof(AppResources.YourLegalIdentityHasBeenObsoleted)];
438 await ServiceRef.TagProfile.RevokeLegalIdentity(Identity);
439 gotoRegistrationPage = true;
443 bool Response = await ServiceRef.UiService.DisplayAlert(
444 ServiceRef.Localizer[nameof(AppResources.WarningTitle)],
445 ServiceRef.Localizer[nameof(AppResources.UnableToGetAccessToYourPrivateKeys)],
446 ServiceRef.Localizer[nameof(AppResources.Continue)],
447 ServiceRef.Localizer[nameof(AppResources.Repair)]);
450 await ServiceRef.TagProfile.SetLegalIdentity(Identity, true);
455 File.WriteAllText(Path.Combine(ServiceRef.StorageService.DataFolder,
"Start.txt"),
456 DateTime.Now.AddHours(1).Ticks.ToString(CultureInfo.InvariantCulture));
460 ServiceRef.LogService.LogException(ex);
470 if (gotoRegistrationPage)
475 if (!
string.IsNullOrWhiteSpace(userMessage))
480 MainThread.BeginInvokeOnMainThread(async () =>
496 public async Task OpenLegalIdentity(
string LegalId,
string Purpose)
501 MainThread.BeginInvokeOnMainThread(async () =>
512 MainThread.BeginInvokeOnMainThread(async () =>
536 public async Task<LegalIdentity?> TryGetLegalIdentity(
string LegalId,
string Purpose)
565 public async Task OpenContract(
string ContractId,
string Purpose, Dictionary<CaseInsensitiveString, object>? ParameterValues)
585 MainThread.BeginInvokeOnMainThread(async () =>
593 ContractId = Contract.ContractId
604 CreationAttributesEventArgs creationAttr = await
ServiceRef.
XmppService.GetNeuroFeatureCreationAttributes();
605 ParameterValues ??= [];
609 NewContractNavigationArgs e =
new(
Contract, ParameterValues);
615 ViewContractNavigationArgs e =
new(
Contract,
false);
627 MainThread.BeginInvokeOnMainThread(async () =>
650 public async Task TagSignature(
string Request)
652 int i = Request.IndexOf(
',');
655 throw new InvalidOperationException(
ServiceRef.
Localizer[nameof(AppResources.InvalidTagSignatureId)]);
657 string JID = System.Web.HttpUtility.UrlDecode(Request[..i]);
658 string Key = Request[(i + 1)..];
661 ??
throw new InvalidOperationException(
ServiceRef.
Localizer[nameof(AppResources.NoLegalIdSelected)]);
664 throw new InvalidOperationException(
ServiceRef.
Localizer[nameof(AppResources.LegalIdNotApproved)]);
671 StringBuilder Xml =
new();
673 Xml.Append(
"<ql xmlns='https://tagroot.io/schema/Signature' key='");
675 Xml.Append(
"' legalId='");
682 throw new InvalidOperationException(
ServiceRef.
Localizer[nameof(AppResources.AppNotConnected)]);
688 private async Task Contracts_SignaturePetitionResponseReceived(
object? Sender, SignaturePetitionResponseEventArgs e)
694 if (!e.Response || Identity is
null)
The Application class, representing an instance of the Neuro-Access app.
static Task SetRegistrationPageAsync()
Switches the application to the on-boarding experience.
static Task< bool > AuthenticateUser(AuthenticationPurpose Purpose, bool Force=false)
Authenticates the user using the configured authentication method.
Machine-readable names in contracts.
const string PaymentInstructionsNamespace
Namespace for payment instructions
static readonly TimeSpan XmppInit
XMPP Init timeout
static readonly TimeSpan XmppConnect
XMPP Connect timeout
XMPP Protocol Properties.
const string PersonalNumber
Personal number
const string OrgAddress2
Organization Address line 2
const string DeviceId
Device ID
const string Country
Country
const string OrgArea
Organization Area
const string Region
Region
const string OrgRegion
Organization Region
const string Nationality
Nationality
const string ZipCode
Zip Code
const string BirthYear
Birth Year
const string OrgCity
Organization City
const string OrgRole
Organization Role
const string Phone
Phone number
const string EMail
e-Mail address
const string OrgZipCode
Organization Zip Code
const string MiddleNames
Middle names
const string BirthDay
Birth Day
const string OrgCountry
Organization Country
const string OrgDepartment
Organization Department
const string Address2
Address line 2
const string Jid
Jabber ID
const string OrgAddress
Organization Address line 1
const string Address
Address line 1
const string LastNames
Last names
const string OrgNumber
Organization number
const string BirthMonth
Birth Month
const string FirstName
First name
const string Gender
Gender
const string OrgName
Organization name
A set of never changing property constants and helpful values.
Contains a local reference to a contract that the user has created or signed.
bool ContractLoaded
If a local copy of the contract is available.
async Task SetContract(Contract Contract)
Sets a parsed contract.
DateTime Updated
When object was last updated.
bool BeginLoad(bool IsResuming, CancellationToken CancellationToken)
Sets the IsLoading flag if the service isn't already loading.
void EndLoad(bool isLoaded)
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
bool IsResuming
If App is resuming service.
bool BeginUnload()
Sets the IsLoading flag if the service isn't already unloading.
void EndUnload()
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
Base class that references services in the app.
static ILogService LogService
Log service.
static INetworkService NetworkService
Network service.
static IUiService UiService
Service serializing and managing UI-related tasks.
static ITagProfile TagProfile
TAG Profile service.
static IStringLocalizer Localizer
Localization service
static IXmppService XmppService
The XMPP service for XMPP communication.
A page that allows the user to create a new contract.
A page that displays a specific contract.
A page to display when the user wants to view an identity.
A page to display when the user is asked to petition an identity.
A page to display when the user is asked to review an identity application.
Holds navigation parameters specific to views displaying a petition of a signature.
A page to display when the user is asked to petition a signature.
const string NamespaceNeuroFeatures
Namespace for Neuro-Features.
Helps with common XML-related tasks.
static string Encode(string s)
Encodes a string for use in XML.
Contains the definition of a contract
DateTime Updated
When the contract was last updated
ContractParts PartsMode
How parts are defined in the smart contract.
ContractState State
Contract state
string ContractId
Contract identity
string ForMachinesNamespace
Namespace used by the root node of the machine-readable contents of the contract (ForMachines).
IdentityState State
Current state of identity
string Id
ID of the legal identity
void Serialize(StringBuilder Xml, bool IncludeNamespace, bool IncludeIdAttribute, bool IncludeClientSignature, bool IncludeAttachments, bool IncludeStatus, bool IncludeServerSignature, bool IncludeAttachmentReferences)
Serializes the identity to XML
Property[] Properties
Properties detailing the legal identity.
string Name
Name of property
Abstract base class of signatures
The requesting entity does not possess the necessary permissions to perform an action that only certa...
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static async Task Update(object Object)
Updates an object in the database.
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
This filter selects objects that have a named field equal to a given value.
Orchestrates operations on contracts upon receiving certain events, like approving or rejecting other...
Task DisplayException(Exception Exception, string? Title=null)
Displays an alert/message box to the user.
Task GoToAsync(string Route, BackMethod BackMethod=BackMethod.Inherited, string? UniqueId=null)
Navigates the AppShell to the specified route, with page arguments to match.
Task< bool > DisplayAlert(string Title, string Message, string? Accept=null, string? Cancel=null)
Displays an alert/message box to the user.
BackMethod
Navigation Back Method
AuthenticationPurpose
Purpose for requesting the user to authenticate itself.
IdentityState
Lists recognized legal identity states.
SignWith
Options on what keys to use when signing data.
ContractParts
How the parts of the contract are defined.
ContractState
Recognized contract states
XmppState
State of XMPP connection.