| 
						
						
							
								
							
						
						
					 | 
					@ -20,7 +20,15 @@ import { Account, AccountEncrypted } from "../db/tables/accounts"; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { Contact } from "../db/tables/contacts"; | 
					 | 
					 | 
					import { Contact } from "../db/tables/contacts"; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import * as databaseUtil from "../db/databaseUtil"; | 
					 | 
					 | 
					import * as databaseUtil from "../db/databaseUtil"; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { DEFAULT_PASSKEY_EXPIRATION_MINUTES } from "../db/tables/settings"; | 
					 | 
					 | 
					import { DEFAULT_PASSKEY_EXPIRATION_MINUTES } from "../db/tables/settings"; | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					import { arrayBufferToBase64, base64ToArrayBuffer, deriveAddress, generateSeed, newIdentifier, simpleDecrypt, simpleEncrypt } from "../libs/crypto"; | 
					 | 
					 | 
					import { | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  arrayBufferToBase64, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  base64ToArrayBuffer, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  deriveAddress, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  generateSeed, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  newIdentifier, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  simpleDecrypt, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  simpleEncrypt, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					} from "../libs/crypto"; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import * as serverUtil from "../libs/endorserServer"; | 
					 | 
					 | 
					import * as serverUtil from "../libs/endorserServer"; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { | 
					 | 
					 | 
					import { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  containsHiddenDid, | 
					 | 
					 | 
					  containsHiddenDid, | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					@ -468,7 +476,9 @@ export interface AccountKeyInfo extends Account, KeyMeta {} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					export const retrieveAccountCount = async (): Promise<number> => { | 
					 | 
					 | 
					export const retrieveAccountCount = async (): Promise<number> => { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  let result; | 
					 | 
					 | 
					  let result; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  const platformService = PlatformServiceFactory.getInstance(); | 
					 | 
					 | 
					  const platformService = PlatformServiceFactory.getInstance(); | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					  const dbResult = await platformService.dbQuery(`SELECT COUNT(*) FROM accounts`); | 
					 | 
					 | 
					  const dbResult = await platformService.dbQuery( | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    `SELECT COUNT(*) FROM accounts`, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  ); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  result = dbResult.values[0][0] as number; | 
					 | 
					 | 
					  result = dbResult.values[0][0] as number; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  if (USE_DEXIE_DB) { | 
					 | 
					 | 
					  if (USE_DEXIE_DB) { | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					@ -523,23 +533,41 @@ export const retrieveFullyDecryptedAccount = async ( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					): Promise<AccountKeyInfo | undefined> => { | 
					 | 
					 | 
					): Promise<AccountKeyInfo | undefined> => { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  let result: AccountKeyInfo | undefined = undefined; | 
					 | 
					 | 
					  let result: AccountKeyInfo | undefined = undefined; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  const platformService = PlatformServiceFactory.getInstance(); | 
					 | 
					 | 
					  const platformService = PlatformServiceFactory.getInstance(); | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					  const dbSecrets = await platformService.dbQuery(`SELECT secretBase64 from secret`); | 
					 | 
					 | 
					  const dbSecrets = await platformService.dbQuery( | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					  if (!dbSecrets || dbSecrets.values.length === 0 || dbSecrets.values[0].length === 0) { | 
					 | 
					 | 
					    `SELECT secretBase64 from secret`, | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					    throw new Error("No secret found. We recommend you clear your data and start over."); | 
					 | 
					 | 
					  ); | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  if ( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    !dbSecrets || | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    dbSecrets.values.length === 0 || | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    dbSecrets.values[0].length === 0 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  ) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    throw new Error( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      "No secret found. We recommend you clear your data and start over.", | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    ); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  const secretBase64 = dbSecrets.values[0][0] as string; | 
					 | 
					 | 
					  const secretBase64 = dbSecrets.values[0][0] as string; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  const secret = base64ToArrayBuffer(secretBase64); | 
					 | 
					 | 
					  const secret = base64ToArrayBuffer(secretBase64); | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					  const dbAccount = await platformService.dbQuery(`SELECT * FROM accounts WHERE did = ?`, [activeDid]); | 
					 | 
					 | 
					  const dbAccount = await platformService.dbQuery( | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					  if (!dbAccount || dbAccount.values.length === 0 || dbAccount.values[0].length === 0) { | 
					 | 
					 | 
					    `SELECT * FROM accounts WHERE did = ?`, | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    [activeDid], | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  ); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  if ( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    !dbAccount || | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    dbAccount.values.length === 0 || | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    dbAccount.values[0].length === 0 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  ) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    throw new Error("Account not found."); | 
					 | 
					 | 
					    throw new Error("Account not found."); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					  const fullAccountData = databaseUtil.mapColumnsToValues(dbAccount.columns, dbAccount.values)[0] as AccountEncrypted; | 
					 | 
					 | 
					  const fullAccountData = databaseUtil.mapColumnsToValues( | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    dbAccount.columns, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    dbAccount.values, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  )[0] as AccountEncrypted; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  const identityEncr = base64ToArrayBuffer(fullAccountData.identityEncrBase64); | 
					 | 
					 | 
					  const identityEncr = base64ToArrayBuffer(fullAccountData.identityEncrBase64); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  const mnemonicEncr = base64ToArrayBuffer(fullAccountData.mnemonicEncrBase64); | 
					 | 
					 | 
					  const mnemonicEncr = base64ToArrayBuffer(fullAccountData.mnemonicEncrBase64); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  fullAccountData.identity = await simpleDecrypt(identityEncr, secret); | 
					 | 
					 | 
					  fullAccountData.identity = await simpleDecrypt(identityEncr, secret); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  fullAccountData.mnemonic = await simpleDecrypt(mnemonicEncr, secret); | 
					 | 
					 | 
					  fullAccountData.mnemonic = await simpleDecrypt(mnemonicEncr, secret); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  result = fullAccountData; | 
					 | 
					 | 
					  result = fullAccountData; | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					   | 
					 | 
					 | 
					
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					  if (USE_DEXIE_DB) { | 
					 | 
					 | 
					  if (USE_DEXIE_DB) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    // one of the few times we use accountsDBPromise directly; try to avoid more usage
 | 
					 | 
					 | 
					    // one of the few times we use accountsDBPromise directly; try to avoid more usage
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const accountsDB = await accountsDBPromise; | 
					 | 
					 | 
					    const accountsDB = await accountsDBPromise; | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					@ -578,9 +606,13 @@ export const generateSaveAndActivateIdentity = async (): Promise<string> => { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  try { | 
					 | 
					 | 
					  try { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    // add to the new sql db
 | 
					 | 
					 | 
					    // add to the new sql db
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const platformService = PlatformServiceFactory.getInstance(); | 
					 | 
					 | 
					    const platformService = PlatformServiceFactory.getInstance(); | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					    const secrets = await platformService.dbQuery(`SELECT secretBase64 FROM secret`); | 
					 | 
					 | 
					    const secrets = await platformService.dbQuery( | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      `SELECT secretBase64 FROM secret`, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    ); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    if (secrets.values.length === 0 || secrets.values[0].length === 0) { | 
					 | 
					 | 
					    if (secrets.values.length === 0 || secrets.values[0].length === 0) { | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					      throw new Error("No initial encryption supported. We recommend you clear your data and start over."); | 
					 | 
					 | 
					      throw new Error( | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        "No initial encryption supported. We recommend you clear your data and start over.", | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      ); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    } | 
					 | 
					 | 
					    } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const secretBase64 = secrets.values[0][0] as string; | 
					 | 
					 | 
					    const secretBase64 = secrets.values[0][0] as string; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const secret = base64ToArrayBuffer(secretBase64); | 
					 | 
					 | 
					    const secret = base64ToArrayBuffer(secretBase64); | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					
  |