forked from trent_larson/crowd-funder-for-time-pwa
chore: remove error logging for errors that are propagated
This commit is contained in:
148
src/libs/util.ts
148
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) {
|
||||||
|
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`,
|
|
||||||
);
|
|
||||||
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 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);
|
|
||||||
|
|
||||||
await platformService.updateDefaultSettings({ activeDid: identity.did });
|
|
||||||
|
|
||||||
await platformService.insertNewDidIntoSettings(identity.did);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("Failed to save new identity:", error);
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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) {
|
if (derivationPath) {
|
||||||
// Derive the DID from mnemonic and derivation path
|
// Derive the DID from mnemonic and derivation path
|
||||||
const [address, privateHex, publicHex] = deriveAddress(
|
const [address, privateHex, publicHex] = deriveAddress(
|
||||||
didOrMnemonic.trim().toLowerCase(),
|
didOrMnemonic.trim().toLowerCase(),
|
||||||
derivationPath,
|
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
|
|
||||||
const platformService = await getPlatformService();
|
|
||||||
const existingAccount = await platformService.dbQuery(
|
|
||||||
"SELECT did FROM accounts WHERE did = ?",
|
|
||||||
[didToCheck],
|
|
||||||
);
|
);
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user