/// <summary> /// Imports a SQRL identity and stores it in the database. /// </summary> /// <param name="identity">The <c>SQRLIdentity</c> to be imported.</param> /// <param name="setAsCurrentIdentity">If set to <c>true</c>, the imported identity will be /// set as the currently active identity after adding it to the database.</param> public void ImportIdentity(SQRLIdentity identity, bool setAsCurrentIdentity = true) { if (identity.Block0 == null) { throw new InvalidOperationException("The identity does not contain a type 0 block!"); } if (HasIdentity(identity.Block0.UniqueIdentifier.ToHex())) { throw new InvalidOperationException("The identity already exists in the database!"); } Identity newIdRec = new Identity(); newIdRec.Name = identity.IdentityName; newIdRec.UniqueId = identity.Block0.UniqueIdentifier.ToHex(); newIdRec.GenesisId = identity.Block0.GenesisIdentifier.ToHex(); // Serialize the identity for storing it in the database. // We could use identity.ToByteArray() here, but then we would // lose extra information not covered by the S4 format, such // as identity name, file path etc. newIdRec.DataBytes = SerializeIdentity(identity); _db.Identities.Add(newIdRec); _db.SaveChanges(); if (setAsCurrentIdentity) { SetCurrentIdentity(newIdRec.UniqueId); } // Finally, fire the IdentityCountChanged event IdentityCountChanged?.Invoke(this, new IdentityCountChangedEventArgs(this.IdentityCount)); }
/// <summary> /// Deletes the currently active <c>SQRLIdentity</c> from the database. /// </summary> public void DeleteCurrentIdentity() { _db.Identities.Remove(_currentIdentityDB); _db.SaveChanges(); if (_db.Identities.Count() >= 1) { Identity id = _db.Identities.First(); SetCurrentIdentity(id.UniqueId); } else { SetCurrentIdentity(null); } // Fire the IdentityCountChanged event IdentityCountChanged?.Invoke(this, new IdentityCountChangedEventArgs(this.IdentityCount)); }