Browse Source

chore: remove error logging for errors that are propagated

pull/189/head
Trent Larson 5 days ago
parent
commit
2c7cb9333e
  1. 140
      src/libs/util.ts

140
src/libs/util.ts

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

Loading…
Cancel
Save