|
|
@ -617,66 +617,61 @@ export const retrieveAllAccountsMetadata = async (): Promise< |
|
|
|
export const DUPLICATE_ACCOUNT_ERROR = "Cannot import duplicate account."; |
|
|
|
|
|
|
|
/** |
|
|
|
* Saves a new identity to both SQL and Dexie databases |
|
|
|
* Saves a new identity to SQL database |
|
|
|
*/ |
|
|
|
export async function saveNewIdentity( |
|
|
|
identity: IIdentifier, |
|
|
|
mnemonic: string, |
|
|
|
derivationPath: string, |
|
|
|
): Promise<void> { |
|
|
|
try { |
|
|
|
// add to the new sql db
|
|
|
|
const platformService = await getPlatformService(); |
|
|
|
// add to the new sql db
|
|
|
|
const platformService = await getPlatformService(); |
|
|
|
|
|
|
|
// Check if account already exists before attempting to save
|
|
|
|
const existingAccount = await platformService.dbQuery( |
|
|
|
"SELECT did FROM accounts WHERE did = ?", |
|
|
|
[identity.did], |
|
|
|
); |
|
|
|
// Check if account already exists before attempting to save
|
|
|
|
const existingAccount = await platformService.dbQuery( |
|
|
|
"SELECT did FROM accounts WHERE did = ?", |
|
|
|
[identity.did], |
|
|
|
); |
|
|
|
|
|
|
|
if (existingAccount?.values?.length) { |
|
|
|
throw new Error( |
|
|
|
`Account with DID ${identity.did} already exists. ${DUPLICATE_ACCOUNT_ERROR}`, |
|
|
|
); |
|
|
|
} |
|
|
|
if (existingAccount?.values?.length) { |
|
|
|
throw new Error( |
|
|
|
`Account with DID ${identity.did} already exists. ${DUPLICATE_ACCOUNT_ERROR}`, |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
const secrets = await platformService.dbQuery( |
|
|
|
`SELECT secretBase64 FROM secret`, |
|
|
|
const secrets = await platformService.dbQuery( |
|
|
|
`SELECT secretBase64 FROM secret`, |
|
|
|
); |
|
|
|
if (!secrets?.values?.length || !secrets.values[0]?.length) { |
|
|
|
throw new Error( |
|
|
|
"No initial encryption supported. We recommend you clear your data and start over.", |
|
|
|
); |
|
|
|
if (!secrets?.values?.length || !secrets.values[0]?.length) { |
|
|
|
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 identityStr = JSON.stringify(identity); |
|
|
|
const encryptedIdentity = await simpleEncrypt(identityStr, secret); |
|
|
|
const encryptedMnemonic = await simpleEncrypt(mnemonic, secret); |
|
|
|
const encryptedIdentityBase64 = arrayBufferToBase64(encryptedIdentity); |
|
|
|
const encryptedMnemonicBase64 = arrayBufferToBase64(encryptedMnemonic); |
|
|
|
const secret = base64ToArrayBuffer(secretBase64); |
|
|
|
const identityStr = JSON.stringify(identity); |
|
|
|
const encryptedIdentity = await simpleEncrypt(identityStr, secret); |
|
|
|
const encryptedMnemonic = await simpleEncrypt(mnemonic, secret); |
|
|
|
const encryptedIdentityBase64 = arrayBufferToBase64(encryptedIdentity); |
|
|
|
const encryptedMnemonicBase64 = arrayBufferToBase64(encryptedMnemonic); |
|
|
|
|
|
|
|
const sql = `INSERT INTO accounts (dateCreated, derivationPath, did, identityEncrBase64, mnemonicEncrBase64, publicKeyHex)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)`;
|
|
|
|
const params = [ |
|
|
|
new Date().toISOString(), |
|
|
|
derivationPath, |
|
|
|
identity.did, |
|
|
|
encryptedIdentityBase64, |
|
|
|
encryptedMnemonicBase64, |
|
|
|
identity.keys[0].publicKeyHex, |
|
|
|
]; |
|
|
|
await platformService.dbExec(sql, params); |
|
|
|
|
|
|
|
const sql = `INSERT INTO accounts (dateCreated, derivationPath, did, identityEncrBase64, mnemonicEncrBase64, publicKeyHex)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)`;
|
|
|
|
const params = [ |
|
|
|
new Date().toISOString(), |
|
|
|
derivationPath, |
|
|
|
identity.did, |
|
|
|
encryptedIdentityBase64, |
|
|
|
encryptedMnemonicBase64, |
|
|
|
identity.keys[0].publicKeyHex, |
|
|
|
]; |
|
|
|
await platformService.dbExec(sql, params); |
|
|
|
|
|
|
|
await platformService.updateDefaultSettings({ activeDid: identity.did }); |
|
|
|
|
|
|
|
await platformService.insertNewDidIntoSettings(identity.did); |
|
|
|
} catch (error) { |
|
|
|
logger.error("Failed to save new identity:", error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
await platformService.updateDefaultSettings({ activeDid: identity.did }); |
|
|
|
|
|
|
|
await platformService.insertNewDidIntoSettings(identity.did); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -1074,39 +1069,28 @@ export async function checkForDuplicateAccount( |
|
|
|
didOrMnemonic: string, |
|
|
|
derivationPath?: string, |
|
|
|
): Promise<boolean> { |
|
|
|
try { |
|
|
|
let didToCheck: string; |
|
|
|
|
|
|
|
if (derivationPath) { |
|
|
|
// Derive the DID from mnemonic and derivation path
|
|
|
|
const [address, privateHex, publicHex] = deriveAddress( |
|
|
|
didOrMnemonic.trim().toLowerCase(), |
|
|
|
derivationPath, |
|
|
|
); |
|
|
|
|
|
|
|
const newId = newIdentifier( |
|
|
|
address, |
|
|
|
privateHex, |
|
|
|
publicHex, |
|
|
|
derivationPath, |
|
|
|
); |
|
|
|
didToCheck = newId.did; |
|
|
|
} else { |
|
|
|
// Use the provided DID directly
|
|
|
|
didToCheck = didOrMnemonic; |
|
|
|
} |
|
|
|
let didToCheck: string; |
|
|
|
|
|
|
|
// Check if an account with this DID already exists
|
|
|
|
const platformService = await getPlatformService(); |
|
|
|
const existingAccount = await platformService.dbQuery( |
|
|
|
"SELECT did FROM accounts WHERE did = ?", |
|
|
|
[didToCheck], |
|
|
|
if (derivationPath) { |
|
|
|
// Derive the DID from mnemonic and derivation path
|
|
|
|
const [address, privateHex, publicHex] = deriveAddress( |
|
|
|
didOrMnemonic.trim().toLowerCase(), |
|
|
|
derivationPath, |
|
|
|
); |
|
|
|
|
|
|
|
return (existingAccount?.values?.length ?? 0) > 0; |
|
|
|
} catch (error) { |
|
|
|
// If we can't check for duplicates, let the calling process handle the error
|
|
|
|
logger.error("Error checking for duplicate account:", error); |
|
|
|
throw error; |
|
|
|
const newId = newIdentifier(address, privateHex, publicHex, derivationPath); |
|
|
|
didToCheck = newId.did; |
|
|
|
} else { |
|
|
|
// Use the provided DID directly
|
|
|
|
didToCheck = didOrMnemonic; |
|
|
|
} |
|
|
|
|
|
|
|
// Check if an account with this DID already exists
|
|
|
|
const platformService = await getPlatformService(); |
|
|
|
const existingAccount = await platformService.dbQuery( |
|
|
|
"SELECT did FROM accounts WHERE did = ?", |
|
|
|
[didToCheck], |
|
|
|
); |
|
|
|
|
|
|
|
return (existingAccount?.values?.length ?? 0) > 0; |
|
|
|
} |
|
|
|