@ideem/zsm-client-sdk
Version:
ZSM makes 2FA easy and invisible for everyone, all the time, using advanced cryptography like MPC to establish cryptographic proof of the origin of any transaction or login attempt, while eliminating opportunities for social engineering. ZSM has no relian
52 lines (48 loc) • 3.04 kB
JavaScript
/**
* @file IdentityIndexing.js
* @description This module provides functions to bind a user identifier (UID) to an identity ID (IID) in IndexedDB and to retrieve the IID associated with a given UID.
* @author Jason Jurusz for Ideem
* @version 1.0.0
* @exports uid2iid, iid4uid
* @todo Migrate this code into the WebAssembly module.
*/
/**
* @name uid2iid
* @description Binds a user identifier (UID) to an identity ID (IID) in IndexedDB.
* @param {string} uid The user identifier (UID) to bind.
* @param {string} iid The identity ID (IID) to bind to the UID.
* @throws {Error} If unable to bind UID to IID or if a DB Connection Error occurs.
* @returns {void}
*/
function uid2iid(uid, iid) {
const idbInstance = indexedDB.open("ideem", 1);
idbInstance.onerror = (err=idbInstance.error) => { throw new Error(`[IdentityIndexing] :: uid2iid :: Unable to bind UID to IID:\n${err}`); }
idbInstance.onsuccess = (e, db=e.target.result) => {
const zsmTbl = db.transaction("zsm", "readwrite");
const write = zsmTbl.objectStore("zsm").put(iid, uid);
write.onsuccess = () => (db.close());
write.onerror = (err=write.error) => (db.close(), console.error(`[IdentityIndexing] :: uid2iid :: Unable to bind UID to IID:\n${err}`));
};
}
/**
* @name iid4uid
* @description Finds the identity ID for a given user identifier (UID) in IndexedDB.
* @param {string} uid The user identifier (UID) to search for.
* @returns {Promise<string>} A promise that resolves to the identity ID (IID) associated with the UID, or the UID itself if not found.
* @throws {Error} If unable to search IndexedDB or if a DB Connection Error occurs.
*/
function iid4uid(uid) {
return new Promise((resolve, reject) => {
const idbInstance = indexedDB.open("ideem", 1);
idbInstance.onupgradeneeded = (e, db=e.target.result) => { if (!db.objectStoreNames.contains("zsm")) db.createObjectStore("zsm");};
idbInstance.onerror = (err=idbInstance.error) => { return reject(new Error(`[IdentityIndexing] :: iid4uid :: Unable to connect to IDB:\n${err}`)); }
idbInstance.onsuccess = (e, db=e.target.result) => {
if(!db.objectStoreNames.contains("zsm")) return resolve(uid);
const zsmTbl = db.transaction("zsm", "readonly");
const lookup = zsmTbl.objectStore("zsm").get(uid);
lookup.onsuccess = () => (db.close(), (lookup.result) ? resolve(lookup.result) : resolve(uid));
lookup.onerror = (err=lookup.error) => (db.close(), console.error(`[IdentityIndexing] :: iid4uid :: Unable to find IID for UID:\n${err}`), reject(new Error(`[IdentityIndexing] :: iid4uid :: Unable to find IID for UID:\n${err}`)));
};
});
}
export { uid2iid, iid4uid };