From c2b111649098fcc48aeb356f512bb97ac0ff4200 Mon Sep 17 00:00:00 2001 From: jojobyte <184880+jojobyte@users.noreply.github.com> Date: Sat, 20 Jan 2024 21:53:14 -0700 Subject: [PATCH] feat(data): :sparkles: encrypt alias & contact data --- README.md | 7 +- src/components/contacts-list.js | 10 +- src/helpers/utils.js | 58 ++++----- src/helpers/wallet.js | 222 ++++++++++++++++++++++++-------- src/main.js | 193 ++++++++++++++------------- src/rigs/add-contact.js | 52 ++++---- src/rigs/confirm-delete.js | 9 +- src/rigs/edit-contact.js | 25 +++- src/rigs/edit-profile.js | 20 +-- src/rigs/send-confirm.js | 74 ++++------- src/rigs/send-or-request.js | 8 +- 11 files changed, 383 insertions(+), 295 deletions(-) diff --git a/README.md b/README.md index df555cb..f88ef77 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,11 @@ A Graphical User Interface (GUI) for - [x] Request - [x] Send - [x] Modify CrypticStorage.js Keystore code to be Xchain compatible -- [ ] Encrypt wallet data in LocalStorage / IndexedDB +- [x] Encrypt wallet data in LocalStorage / IndexedDB - [x] Recovery Phrase - - [ ] Contact Data -- [ ] Backup Wallet & Contacts + - [x] Aliases Data + - [x] Contact Data +- [ ] Backup/Restore Wallet & Contacts ### Stage 2 - [ ] Transactions view diff --git a/src/components/contacts-list.js b/src/components/contacts-list.js index 404ef53..3d28e6f 100644 --- a/src/components/contacts-list.js +++ b/src/components/contacts-list.js @@ -102,11 +102,11 @@ const initialState = { // }, handleClick: state => event => { event.preventDefault() - console.log( - 'handle contacts click', - event, - state, - ) + // console.log( + // 'handle contacts click', + // event, + // state, + // ) }, handleContactsChange: (newState, oldState) => { if (newState.contacts !== oldState.contacts) { diff --git a/src/helpers/utils.js b/src/helpers/utils.js index 83b8989..d4e8c66 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -601,10 +601,6 @@ export function generatePaymentRequestURI( let addr = state.wallet?.address || '' let claims = [] - // dash:XmPNH5bMkwwc1kjVGwxWxTs86C3ZsgjdSX?amount=0.0001 - // dash:XktddCruaWEMDqqiciagWPXCfqc1jfY6zf?amount=0.0001 - // dash:XtXnZkocKvrqGDxW7mhBvoR6yCAD99mnau?amount=0.0001 - if (state.userInfo) { let filteredInfo = Array.from( Object.entries(state.userInfo) @@ -658,41 +654,37 @@ export function generatePaymentRequestURI( return res } -// export function generatePaymentRequestURI(state) { -// let shareUri = `dash:${state.wallet?.address || ''}?` -// let shareParams = [] - -// if (state.amount > 0) { -// shareParams.push(`amount=${state.amount}`) -// } - -// if (state.label) { -// shareParams.push(`label=${state.label}`) -// } +export async function getStoreData( + store, + callback, + iterableCallback = res => async (v, k, i) => res.push(v) +) { + let result = [] -// if (state.message) { -// shareParams.push(`message=${state.message}`) -// } + return await store.keys().then(async function(keys) { + for (let k of keys) { + let v = await store.getItem(k) + await iterableCallback(result)(v, k) + } -// if (shareParams.length > 0) { -// shareUri += `?${shareParams.join('&')}` -// } + callback?.(result) -// return shareUri -// } + return result + }).catch(function(err) { + console.error('getStoreData', err) + return null + }); +} -export async function loadStore(store, callback) { - // let storeLen = await store.length() +export async function loadStore( + store, + callback, + iterableCallback = res => v => res.push(v) +) { let result = [] - return await store.iterate((v, k, i) => { - result.push(v) - - // if (i === storeLen) { - // return result - // } - }) - .then(() => callback(result)) + return await store.iterate(iterableCallback(result)) + .then(() => callback?.(result)) .catch(err => { console.error('loadStore', err) return null diff --git a/src/helpers/wallet.js b/src/helpers/wallet.js index 6e5214f..b0d2601 100644 --- a/src/helpers/wallet.js +++ b/src/helpers/wallet.js @@ -114,8 +114,7 @@ export async function findAllInStore(targStore, query = {}) { }) } -export async function loadWalletsForAlias(alias) { - let $alias = await store.aliases.getItem(alias) +export async function loadWalletsForAlias($alias) { $alias.$wallets = {} if ($alias?.wallets) { @@ -220,15 +219,16 @@ export function getKeystoreData(keystore) { } } -export async function decryptKeystore( +export async function setupCryptic( encryptionPassword, keystore, ) { + const ks = getKeystoreData(keystore) const { - ciphertext, cipherLength, cipherAlgorithm, + cipherLength, cipherAlgorithm, derivationAlgorithm, hashingAlgorithm, iv, - mac, iterations, salt, numBits, - } = getKeystoreData(keystore) + iterations, salt, + } = ks Cryptic.setConfig({ cipherAlgorithm, @@ -243,32 +243,157 @@ export async function decryptKeystore( salt, ); - const derivedBytes = await cryptic.deriveBits(numBits, salt) + return { + Cryptic, + cryptic, + ks, + } +} + +export async function encryptData( + encryptionPassword, + keystore, + data, +) { + const { cryptic, ks } = await setupCryptic( + encryptionPassword, + keystore, + ) + + return await cryptic.encrypt(data, ks.iv); +} + +export async function decryptData( + encryptionPassword, + keystore, + data, +) { + const { cryptic, ks } = await setupCryptic( + encryptionPassword, + keystore, + ) + + return await cryptic.decrypt(data, ks.iv) +} + +export async function storedData( + encryptionPassword, keystore, +) { + const SD = {} + + SD.decryptData = async function(data) { + if (data && 'string' === typeof data && data.length > 0) { + data = JSON.parse(await decryptData( + encryptionPassword, + keystore, + data + )) + } + + return data + } + + SD.decryptItem = async function(targetStore, item,) { + let data = await targetStore.getItem( + item, + ) + + data = await SD.decryptData(data) + + return data + } + + /** + * + * @param {*} targetStore + * @param {*} item + * @param {*} data + * @param {*} extend + * @returns {Promise<[String,Object]>} + */ + SD.encryptData = async function( + targetStore, item, data = {}, extend = true + ) { + let encryptedData = '' + let storedData = {} + let jsonData = {} + if (extend) { + // storedData = await targetStore.getItem( + // item, + // ) + storedData = await SD.decryptItem( + targetStore, + item + ) + } + + if (data) { + jsonData = { + ...storedData, + ...data, + } + encryptedData = await encryptData( + encryptionPassword, + keystore, + JSON.stringify(jsonData) + ) + } + + return [ + encryptedData, + jsonData, + ] + } + + SD.encryptItem = async function( + targetStore, item, data = {}, extend = true + ) { + let encryptedData = '' + let encryptedResult = '' + let result = {} + + if (data || extend) { + let d = await SD.encryptData(targetStore, item, data, extend) + encryptedResult = d[0] + result = d[1] + encryptedData = await targetStore.setItem( + item, + encryptedResult + ) + } + + return result || data || encryptedData + // return encryptedData + } + + return SD +} + +export async function decryptKeystore( + encryptionPassword, + keystore, +) { + const { Cryptic, cryptic, ks } = await setupCryptic( + encryptionPassword, + keystore, + ) + + const derivedBytes = await cryptic.deriveBits(ks.numBits, ks.salt) const bMAC = blake256([ ...new Uint8Array(derivedBytes.slice(16, 32)), - ...Cryptic.toBytes(ciphertext), + ...Cryptic.toBytes(ks.ciphertext), ]) const kMAC = Cryptic.toHex(keccak_256(new Uint8Array([ ...new Uint8Array(derivedBytes.slice(16, 32)), - ...Cryptic.toBytes(ciphertext), + ...Cryptic.toBytes(ks.ciphertext), ]))); - // console.log( - // 'decryptPhrase mac === bMAC', - // { - // mac, - // bMAC, - // kMAC, - // }, - // mac === bMAC - // ) - - if (mac && ![bMAC, kMAC].includes(mac)) { + if (ks.mac && ![bMAC, kMAC].includes(ks.mac)) { throw new Error('Invalid password') } - return await cryptic.decrypt(ciphertext, iv) + return await cryptic.decrypt(ks.ciphertext, ks.iv) } export function genKeystore( @@ -305,43 +430,27 @@ export async function encryptKeystore( encryptionPassword, recoveryPhrase, ) { - let ks = genKeystore() - const { - cipherLength, cipherAlgorithm, - derivationAlgorithm, hashingAlgorithm, - iterations, iv, salt, numBits, - } = getKeystoreData(ks) - - Cryptic.setConfig({ - cipherAlgorithm, - cipherLength, - hashingAlgorithm, - derivationAlgorithm, - iterations, - }) - - const cryptic = Cryptic.create( + let keystore = genKeystore() + const { Cryptic, cryptic, ks } = await setupCryptic( encryptionPassword, - salt, - ); + keystore, + ) - // const keyMaterial = await cryptic.keyMaterial - // const derivedKey = await cryptic.derivedKey - const derivedBytes = await cryptic.deriveBits(numBits, salt) - const encryptedPhrase = await cryptic.encrypt(recoveryPhrase, iv); + const derivedBytes = await cryptic.deriveBits(ks.numBits, ks.salt) + const encryptedPhrase = await cryptic.encrypt(recoveryPhrase, ks.iv); - ks.crypto.ciphertext = encryptedPhrase + keystore.crypto.ciphertext = encryptedPhrase const bMAC = blake256([ ...new Uint8Array(derivedBytes.slice(16, 32)), - ...Cryptic.toBytes(ks.crypto.ciphertext), + ...Cryptic.toBytes(keystore.crypto.ciphertext), ]) const kMAC = Cryptic.toHex(keccak_256(new Uint8Array([ ...new Uint8Array(derivedBytes.slice(16, 32)), - ...Cryptic.toBytes(ks.crypto.ciphertext), + ...Cryptic.toBytes(keystore.crypto.ciphertext), ]))); - ks.crypto.mac = bMAC + keystore.crypto.mac = bMAC // console.log( // 'encrypted keystore', @@ -358,7 +467,7 @@ export async function encryptKeystore( // }, // ) - return ks + return keystore } export async function initWallet( @@ -410,16 +519,23 @@ export async function initWallet( id, accountIndex, addressIndex: addrs?.finalAddressIndex || addressIndex, - keystore: keystore || await encryptKeystore(encryptionPassword, recoveryPhrase), + keystore: keystore || await encryptKeystore( + encryptionPassword, + recoveryPhrase + ), } ) let storedAlias = await store.aliases.setItem( `${alias}`, - { - wallets, - info, - } + await encryptData( + encryptionPassword, + storeWallet.keystore, + JSON.stringify({ + wallets, + info, + }) + ) ) // console.log( diff --git a/src/main.js b/src/main.js index cd60cf6..b8dd14e 100644 --- a/src/main.js +++ b/src/main.js @@ -20,11 +20,15 @@ import { batchAddressGenerate, updateAllFunds, decryptKeystore, + loadWallets, loadWalletsForAlias, store, createTx, sendTx, getAddrsWithFunds, + // encryptData, + // decryptData, + storedData, } from './helpers/wallet.js' import setupNav from './components/nav.js' @@ -71,15 +75,11 @@ let appState = envoy( sentTransactions: {}, account: {}, }, - // (state, oldState) => { - // if (state.contacts !== oldState.contacts) { - // console.log( - // 'state.contacts !== oldState.contacts on push', - // oldState.contacts, - // state.contacts, - // ) - // } - // } +) +let appTools = envoy( + { + storedData: {}, + }, ) let userInfo = envoy( { @@ -87,18 +87,21 @@ let userInfo = envoy( }, async (state, oldState, prop) => { if (state[prop] !== oldState[prop]) { - let $aliases = await store.aliases.getItem( + let decryptedAlias = await appTools.storedData.decryptItem( + store.aliases, appState.selectedAlias, ) - store.aliases.setItem( + appTools.storedData.encryptItem( + store.aliases, appState.selectedAlias, { - ...$aliases, + ...decryptedAlias, info: { - ...$aliases.info, + ...decryptedAlias.info, [prop]: state[prop], }, - } + }, + false, ) } } @@ -144,11 +147,11 @@ let contactsList = await setupContactsList( events: { handleClick: state => async event => { event.preventDefault() - console.warn( - 'handle contacts click', - event.target, - state, - ) + // console.log( + // 'handle contacts click', + // event.target, + // state, + // ) let contactArticle = event.target?.closest('article') @@ -157,7 +160,8 @@ let contactsList = await setupContactsList( contactArticle !== null ) { let contactID = contactArticle.dataset.id - let contactData = await store.contacts.getItem( + let contactData = await appTools.storedData.decryptItem( + store.contacts, contactID, ) let contactAccountID = Object.values(contactData.incoming)?.[0]?.accountIndex @@ -269,8 +273,8 @@ let contactsList = await setupContactsList( ) let { createdAt, ...contactAcct } = newAccount - newContact = await store.contacts.setItem( - // shareAccount.id, + newContact = await appTools.storedData.encryptItem( + store.contacts, shareAccount.xkeyId, { createdAt, @@ -279,7 +283,8 @@ let contactsList = await setupContactsList( ...contactAcct, } } - } + }, + false, ) appState.contacts.push(newContact) @@ -288,11 +293,6 @@ let contactsList = await setupContactsList( appState.contacts.sort(sortContactsByAlias) ) - // await loadStore( - // store.contacts, - // res => contactsList.render(res) - // ) - console.log( 'share qr new contact', newContact, @@ -316,25 +316,33 @@ let contactsList = await setupContactsList( ) async function getUserInfo() { - if (appState.selectedAlias) { - console.log( - 'getUserInfo selectedAlias', - appState.selectedAlias, - // appState + let w = await store.wallets.getItem(appState.selectedWallet) + let ks = w?.keystore + + if (appState.selectedAlias && ks) { + appTools.storedData = await storedData( + appState.encryptionPassword, + ks, ) - let { $wallets, ...$userInfo } = await loadWalletsForAlias( - appState.selectedAlias + // console.log( + // 'getUserInfo selectedAlias', + // appState.selectedAlias, + // ) + await appTools.storedData?.decryptItem( + store.aliases, + appState.selectedAlias, ) - wallets = $wallets - - Object.entries(($userInfo?.info || {})) - .forEach( - ([k,v]) => userInfo[k] = v + .then(async $alias => { + let { $wallets, ...$userInfo } = await loadWalletsForAlias( + $alias ) - // userInfo = { - // // ...OIDC_CLAIMS, - // ...($userInfo?.info || {}), - // } + wallets = $wallets + + Object.entries(($userInfo?.info || {})) + .forEach( + ([k,v]) => userInfo[k] = v + ) + }) } } @@ -401,24 +409,24 @@ async function main() { appDialogs.addContact = addContactRig({ setupDialog, updateAllFunds, - appDialogs, appState, store, walletFunds, + appDialogs, appState, appTools, store, walletFunds, mainApp, wallet, userInfo, contactsList, }) appDialogs.confirmDelete = confirmDeleteRig({ - mainApp, setupDialog, appDialogs, appState, + mainApp, setupDialog, appDialogs, appState, appTools, store, userInfo, contactsList, }) appDialogs.editContact = editContactRig({ setupDialog, updateAllFunds, - appDialogs, appState, store, walletFunds, + appDialogs, appState, appTools, store, walletFunds, mainApp, wallet, userInfo, contactsList, }) appDialogs.editProfile = editProfileRig({ mainApp, setupDialog, store, - appState, bodyNav, + appState, appTools, bodyNav, }) appDialogs.scanContact = scanContactRig({ @@ -426,12 +434,12 @@ async function main() { }) appDialogs.sendOrRequest = sendOrRequestRig({ - mainApp, setupDialog, appDialogs, appState, store, + mainApp, setupDialog, appDialogs, appState, appTools, store, wallet, account: appState.account, deriveWalletData, createTx, getAddrsWithFunds, }) appDialogs.sendConfirm = sendConfirmRig({ - mainApp, setupDialog, appDialogs, appState, + mainApp, setupDialog, appDialogs, appState, appTools, deriveWalletData, createTx, sendTx, getAddrsWithFunds, store, userInfo, contactsList, }) @@ -495,19 +503,41 @@ async function main() { } }) - let ks_phrase = wallets?.[appState.selectedWallet] - ?.keystore?.crypto?.ciphertext || '' - let ks_iv = wallets?.[appState.selectedWallet] - ?.keystore?.crypto?.cipherparams?.iv || '' - let ks_salt = wallets?.[appState.selectedWallet] - ?.keystore?.crypto?.kdfparams?.salt || '' + let ks = wallets?.[appState.selectedWallet] + ?.keystore + let ks_phrase = ks?.crypto?.ciphertext || '' + let ks_iv = ks?.crypto?.cipherparams?.iv || '' + let ks_salt = ks?.crypto?.kdfparams?.salt || '' - if (appState.encryptionPassword) { + if (appState.encryptionPassword && ks) { try { appState.phrase = await decryptKeystore( appState.encryptionPassword, - wallets?.[appState.selectedWallet]?.keystore, + ks, ) + + // const { encryptItem, encryptData, decryptItem } = + // appTools.storedData = await storedData( + // appState.encryptionPassword, + // ks, + // ) + + // let $alias = await store.aliases.getItem( + // `${appState.selectedAlias}` + // ) + + // let decryptedAlias = await appTools.storedData.decryptItem( + // store.aliases, + // `${appState.selectedAlias}`, + // ) + + // let encryptedAlias = await appTools.storedData.encryptData( + // store.aliases, + // `${appState.selectedAlias}`, + // ) + // console.log('alias foo', { + // $alias, decryptedAlias, encryptedAlias + // }) } catch(err) { console.error( '[fail] unable to decrypt recovery phrase', @@ -574,7 +604,7 @@ async function main() { }) mainFtr.render() - await loadStore( + loadStore( store.contacts, res => { if (res) { @@ -585,6 +615,11 @@ async function main() { userInfo, }) } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) + // appTools.storedData.decryptData(v) + // .then(ev => res.push(ev)) } ) @@ -604,34 +639,6 @@ async function main() { event.preventDefault() event.stopPropagation() - let shareAccount - - if (appState.phrase) { - console.log( - 'share qr current wallet', - accountIndex, - wallet?.xkeyId, - wallet, - ) - // if (!wallet) { - // wallet = await deriveWalletData(appState.phrase) - // } - - // accountIndex += 1 - - // shareAccount = await deriveWalletData( - // appState.phrase, - // accountIndex - // ) - - // console.log( - // 'share qr derived wallet', - // accountIndex, - // shareAccount?.xkeyId, - // shareAccount, - // ) - } - await getUserInfo() appDialogs.editProfile.render( @@ -676,18 +683,6 @@ async function main() { onMessage: async function (evname, data) { let updates = {} let txUpdates = {} - // console.log('onMessage check for', addr, evname, data) - // let result; - // try { - // result = await find(evname, data); - // } catch (e) { - // reject(e); - // return; - // } - - // if (result) { - // resolve(result); - // } if (![ // "tx", diff --git a/src/rigs/add-contact.js b/src/rigs/add-contact.js index 92bb959..271e1cf 100644 --- a/src/rigs/add-contact.js +++ b/src/rigs/add-contact.js @@ -23,7 +23,7 @@ export let addContactRig = (function (globals) { 'use strict'; let { - setupDialog, appDialogs, appState, store, + setupDialog, appDialogs, appState, appTools, store, mainApp, wallet, userInfo, contactsList, updateAllFunds, walletFunds, } = globals; @@ -53,10 +53,11 @@ export let addContactRig = (function (globals) { contact[localName] = fieldValue } - let newContact = await store.contacts.setItem( - // state.wallet.id, - state.wallet.xkeyId, - contact, + let newContact = await appTools.storedData.encryptItem( + store.contacts, + state.wallet.xkeyId, + contact, + false, ) state.contact = newContact @@ -74,6 +75,9 @@ export let addContactRig = (function (globals) { userInfo, }) } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) } ) }, 1000) @@ -265,8 +269,8 @@ export let addContactRig = (function (globals) { xkey, ) - let newContact = await store.contacts.setItem( - // state.wallet.id, + let newContact = await appTools.storedData.encryptItem( + store.contacts, state.wallet.xkeyId, { ...state.contact, @@ -288,7 +292,8 @@ export let addContactRig = (function (globals) { }, alias: preferred_username, uri: event.target.value, - } + }, + false, ) loadStore( @@ -302,6 +307,9 @@ export let addContactRig = (function (globals) { userInfo, }) } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) } ) @@ -452,12 +460,12 @@ export let addContactRig = (function (globals) { return; } - let storedContact = await store.contacts.getItem( + let storedContact = await appTools.storedData.decryptItem( + store.contacts, state.wallet.xkeyId, ) - - let pairedContact = await store.contacts.setItem( - // state.wallet.id, + let pairedContact = appTools.storedData.encryptItem( + store.contacts, state.wallet.xkeyId, { ...storedContact, @@ -470,22 +478,10 @@ export let addContactRig = (function (globals) { }, uri: event.target.contactAddr.value, alias: event.target.contactAlias.value, - } + }, + false, ) - // let contactExists = appState.contacts.findIndex( - // c => c.info?.preferred_username === pairedContact.info?.preferred_username - // ) - // if (contactExists > -1) { - // appState.contacts[contactExists] = pairedContact - // } else { - // appState.contacts.push(pairedContact) - // } - - // appState.contacts.sort(sortContactsByAlias); - - // contactsList.render(appState.contacts) - loadStore( store.contacts, res => { @@ -494,7 +490,6 @@ export let addContactRig = (function (globals) { updateAllFunds(state.wallet, walletFunds) .then(funds => { - // walletFunds.balance = funds console.log('updateAllFunds then funds', funds) }) .catch(err => console.error('catch updateAllFunds', err, state.wallet)) @@ -504,6 +499,9 @@ export let addContactRig = (function (globals) { userInfo, }) } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) } ) diff --git a/src/rigs/confirm-delete.js b/src/rigs/confirm-delete.js index ffe8220..97c3f9b 100644 --- a/src/rigs/confirm-delete.js +++ b/src/rigs/confirm-delete.js @@ -9,7 +9,7 @@ export let confirmDeleteRig = (function (globals) { 'use strict'; let { - mainApp, setupDialog, appDialogs, appState, + mainApp, setupDialog, appDialogs, appState, appTools, store, userInfo, contactsList, } = globals @@ -89,10 +89,6 @@ export let confirmDeleteRig = (function (globals) { let fde = formDataEntries(event) - // let storedContact = await store.contacts.getItem( - // state.shareAccount.xkeyId, - // ) - if (fde?.intent === 'delete') { let removedContact = await store.contacts.removeItem( state.shareAccount.xkeyId, @@ -111,6 +107,9 @@ export let confirmDeleteRig = (function (globals) { userInfo, }) } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) } ) diff --git a/src/rigs/edit-contact.js b/src/rigs/edit-contact.js index 2944f80..d6befc5 100644 --- a/src/rigs/edit-contact.js +++ b/src/rigs/edit-contact.js @@ -24,7 +24,7 @@ export let editContactRig = (function (globals) { 'use strict'; let { - setupDialog, appDialogs, appState, store, + setupDialog, appDialogs, appState, appTools, store, mainApp, wallet, userInfo, contactsList, updateAllFunds, walletFunds, } = globals; @@ -54,9 +54,11 @@ export let editContactRig = (function (globals) { contact[localName] = fieldValue } - let modifyContact = await store.contacts.setItem( + let modifyContact = await appTools.storedData.encryptItem( + store.contacts, state.shareAccount.xkeyId, contact, + false, ) state.contact = modifyContact @@ -74,6 +76,9 @@ export let editContactRig = (function (globals) { userInfo, }) } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) } ) }, 1000) @@ -246,8 +251,8 @@ export let editContactRig = (function (globals) { xkey, ) - let modifyContact = await store.contacts.setItem( - // state.wallet.id, + let modifyContact = await appTools.storedData.encryptItem( + store.contacts, state.shareAccount.xkeyId, { ...state.contact, @@ -269,7 +274,8 @@ export let editContactRig = (function (globals) { }, alias: preferred_username, uri: event.target.value, - } + }, + false, ) loadStore( @@ -283,6 +289,9 @@ export let editContactRig = (function (globals) { userInfo, }) } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) } ) @@ -311,7 +320,8 @@ export let editContactRig = (function (globals) { return state.elements.dialog.close('cancel') } - let storedContact = await store.contacts.getItem( + let storedContact = await appTools.storedData.decryptItem( + store.contacts, state.shareAccount.xkeyId, ) let contactCard = state.elements?.dialog?.querySelector( @@ -347,7 +357,8 @@ export let editContactRig = (function (globals) { let fde = formDataEntries(event) - let storedContact = await store.contacts.getItem( + let storedContact = await appTools.storedData.decryptItem( + store.contacts, state.shareAccount.xkeyId, ) diff --git a/src/rigs/edit-profile.js b/src/rigs/edit-profile.js index 35dce08..15e4b19 100644 --- a/src/rigs/edit-profile.js +++ b/src/rigs/edit-profile.js @@ -18,7 +18,7 @@ export let editProfileRig = (function (globals) { let { mainApp, setupDialog, store, - appState, bodyNav, + appState, appTools, bodyNav, } = globals; let editProfile = setupDialog( @@ -185,14 +185,6 @@ export let editProfileRig = (function (globals) { let fde = formDataEntries(event) - // if (!String(fde.profileName)?.trim()) { - // event.target.profileName.setCustomValidity( - // 'Name must be Alphanumeric' - // ) - // event.target.reportValidity() - // return; - // } - if (!String(fde.profileAlias)?.trim()) { event.target.profileAlias.setCustomValidity( 'An alias is required' @@ -201,7 +193,8 @@ export let editProfileRig = (function (globals) { return; } - let storedAlias = await store.aliases.getItem( + let storedAlias = await appTools.storedData.decryptItem( + store.aliases, appState.selectedAlias, ) let removedAlias @@ -214,8 +207,8 @@ export let editProfileRig = (function (globals) { localStorage.selectedAlias = fde.profileAlias } - let updatedAlias = await store.aliases.setItem( - // state.wallet.id, + let updatedAlias = await appTools.storedData.encryptItem( + store.aliases, appState.selectedAlias, { ...storedAlias, @@ -224,7 +217,8 @@ export let editProfileRig = (function (globals) { name: String(fde.profileName), preferred_username: String(fde.profileAlias), }, - } + }, + false, ) state.userInfo.name = String(fde.profileName) diff --git a/src/rigs/send-confirm.js b/src/rigs/send-confirm.js index 1037b39..5fa58dc 100644 --- a/src/rigs/send-confirm.js +++ b/src/rigs/send-confirm.js @@ -9,7 +9,7 @@ export let sendConfirmRig = (function (globals) { 'use strict'; let { - mainApp, setupDialog, appDialogs, appState, + mainApp, setupDialog, appDialogs, appState, appTools, sendTx, store, userInfo, contactsList, } = globals @@ -113,21 +113,9 @@ export let sendConfirmRig = (function (globals) { handleSubmit: state => async event => { event.preventDefault() event.stopPropagation() - // event.target.to.setCustomValidity( - // '' - // ) - // event.target.to.reportValidity() let fde = formDataEntries(event) - // if (fde.intent === 'send' && !fde.to) { - // event.target.to.setCustomValidity( - // 'You must specify a contact or address to send to' - // ) - // event.target.to.reportValidity() - // return; - // } - let outWallet, inWallet, address, addressIndex, txRes let sendWallet = {} @@ -139,11 +127,6 @@ export let sendConfirmRig = (function (globals) { if (fde.intent === 'send') { if (outWallet) { addressIndex = (outWallet?.addressIndex || 0) + 1 - // sendWallet = await deriveWalletData( - // outWallet?.xpub, - // 0, - // addressIndex, - // ) } if (state.tx) { @@ -153,24 +136,29 @@ export let sendConfirmRig = (function (globals) { } if (txRes && addressIndex !== undefined) { - store.contacts - .getItem(inWallet.xkeyId) - .then(async c => { - await store.contacts.setItem( - inWallet.xkeyId, - { - ...c, - outgoing: { - ...c.outgoing, - [outWallet.xkeyId]: { - ...c.outgoing[outWallet.xkeyId], - addressIndex, - } + appTools.storedData.decryptItem( + store.contacts, + inWallet.xkeyId, + ).then(async c => { + await appTools.storedData.encryptItem( + store.contacts, + inWallet.xkeyId, + { + ...c, + outgoing: { + ...c.outgoing, + [outWallet.xkeyId]: { + ...c.outgoing[outWallet.xkeyId], + addressIndex, } } - ) + }, + false, + ) - loadStore(store.contacts, res => { + loadStore( + store.contacts, + res => { if (res) { appState.contacts = res @@ -179,8 +167,12 @@ export let sendConfirmRig = (function (globals) { userInfo, }) } - }) - }) + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) + } + ) + }) } appState.sentTransactions = { @@ -197,18 +189,6 @@ export let sendConfirmRig = (function (globals) { `Ð ${state.amount || 0}`, state.contact, txRes, - // { - // xkeyId: outWallet?.xkeyId, - // addressKeyId: outWallet?.addressKeyId, - // addressIndex: outWallet?.addressIndex, - // address: outWallet?.address, - // }, - // { - // xkeyId: sendWallet?.xkeyId, - // addressKeyId: sendWallet?.addressKeyId, - // addressIndex: sendWallet?.addressIndex, - // address, - // }, ) } diff --git a/src/rigs/send-or-request.js b/src/rigs/send-or-request.js index 8824e5f..9759c34 100644 --- a/src/rigs/send-or-request.js +++ b/src/rigs/send-or-request.js @@ -9,7 +9,7 @@ export let sendOrRequestRig = (function (globals) { 'use strict'; let { - mainApp, setupDialog, appDialogs, appState, store, + mainApp, setupDialog, appDialogs, appState, appTools, store, createTx, deriveWalletData, getAddrsWithFunds, wallet, wallets, accounts, } = globals @@ -352,7 +352,8 @@ export let sendOrRequestRig = (function (globals) { inWallet.addressIndex, ) - await store.contacts.setItem( + await appTools.storedData.encryptItem( + store.contacts, inWallet.xkeyId, { ...contact, @@ -364,7 +365,8 @@ export let sendOrRequestRig = (function (globals) { addressIndex: receiveWallet.addressIndex, } }, - } + }, + false, ) inWallet.address = receiveWallet.address