public async Task <LegalIdentity> AddLegalIdentity(RegisterIdentityModel model, params LegalIdentityAttachment[] attachments)
        {
            AssertContractsIsAvailable();
            AssertFileUploadIsAvailable();

            await contractsClient.GenerateNewKeys();

            LegalIdentity identity = await contractsClient.ApplyAsync(model.ToProperties(this.neuronService));

            foreach (var a in attachments)
            {
                HttpFileUploadEventArgs e2 = await fileUploadClient.RequestUploadSlotAsync(Path.GetFileName(a.Filename), a.ContentType, a.ContentLength);

                if (!e2.Ok)
                {
                    throw new Exception(e2.ErrorText);
                }

                await e2.PUT(a.Data, a.ContentType, (int)Constants.Timeouts.UploadFile.TotalMilliseconds);

                byte[] signature = await contractsClient.SignAsync(a.Data, SignWith.CurrentKeys);

                identity = await contractsClient.AddLegalIdAttachmentAsync(identity.Id, e2.GetUrl, signature);
            }

            return(identity);
        }
        private RegisterIdentityModel CreateRegisterModel()
        {
            string s;
            RegisterIdentityModel model = new RegisterIdentityModel();

            if (!string.IsNullOrWhiteSpace(s = this.FirstName?.Trim()))
            {
                model.FirstName = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.MiddleNames?.Trim()))
            {
                model.MiddleNames = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.LastNames?.Trim()))
            {
                model.LastNames = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.PersonalNumber?.Trim()))
            {
                model.PersonalNumber = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.Address?.Trim()))
            {
                model.Address = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.Address2?.Trim()))
            {
                model.Address2 = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.ZipCode?.Trim()))
            {
                model.ZipCode = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.Area?.Trim()))
            {
                model.Area = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.City?.Trim()))
            {
                model.City = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.Region?.Trim()))
            {
                model.Region = s;
            }

            if (!string.IsNullOrWhiteSpace(s = this.SelectedCountry?.Trim()))
            {
                model.Country = s;
            }

            return(model);
        }
        private async Task Register()
        {
            if (!(await this.ValidateInput(true)))
            {
                return;
            }

            string countryCode           = ISO_3166_1.ToCode(this.SelectedCountry);
            string pnr                   = PersonalNumber.Trim();
            string pnrBeforeValidation   = pnr;
            bool?  personalNumberIsValid = PersonalNumberSchemes.IsValid(countryCode, ref pnr, out string personalNumberFormat);

            if (personalNumberIsValid.HasValue && !personalNumberIsValid.Value)
            {
                if (string.IsNullOrWhiteSpace(personalNumberFormat))
                {
                    await this.UiDispatcher.DisplayAlert(AppResources.ErrorTitle, AppResources.PersonalNumberDoesNotMatchCountry);
                }
                else
                {
                    await this.UiDispatcher.DisplayAlert(AppResources.ErrorTitle, AppResources.PersonalNumberDoesNotMatchCountry_ExpectedFormat + personalNumberFormat);
                }
                return;
            }
            if (pnr != pnrBeforeValidation)
            {
                this.PersonalNumber = pnr;
            }

            if (string.IsNullOrWhiteSpace(this.TagProfile.LegalJid))
            {
                await this.UiDispatcher.DisplayAlert(AppResources.ErrorTitle, AppResources.OperatorDoesNotSupportLegalIdentitiesAndSmartContracts);

                return;
            }

            if (string.IsNullOrWhiteSpace(this.TagProfile.RegistryJid))
            {
                await this.UiDispatcher.DisplayAlert(AppResources.ErrorTitle, AppResources.OperatorDoesNotSupportThingRegistries);

                return;
            }

            if (string.IsNullOrWhiteSpace(this.TagProfile.ProvisioningJid))
            {
                await this.UiDispatcher.DisplayAlert(AppResources.ErrorTitle, AppResources.OperatorDoesNotSupportProvisioningAndDecisionSupportForThings);

                return;
            }

            if (!this.NeuronService.IsOnline)
            {
                await this.UiDispatcher.DisplayAlert(AppResources.ErrorTitle, AppResources.NotConnectedToTheOperator);

                return;
            }

            SetIsBusy(RegisterCommand, TakePhotoCommand, PickPhotoCommand);

            try
            {
                RegisterIdentityModel     model  = CreateRegisterModel();
                LegalIdentityAttachment[] photos = { photo };
                (bool succeeded, LegalIdentity addedIdentity) = await this.networkService.TryRequest(() => this.NeuronService.Contracts.AddLegalIdentity(model, photos));

                if (succeeded)
                {
                    this.LegalIdentity = addedIdentity;
                    this.TagProfile.SetLegalIdentity(this.LegalIdentity);
                    UiDispatcher.BeginInvokeOnMainThread(() =>
                    {
                        SetIsDone(RegisterCommand, TakePhotoCommand, PickPhotoCommand);
                        OnStepCompleted(EventArgs.Empty);
                    });
                }
            }
            catch (Exception ex)
            {
                this.LogService.LogException(ex);
                await this.UiDispatcher.DisplayAlert(ex);
            }
            finally
            {
                BeginInvokeSetIsDone(RegisterCommand, TakePhotoCommand, PickPhotoCommand);
            }
        }