From dce5762f6328f88879da669a60e067ca05ca5eb6 Mon Sep 17 00:00:00 2001 From: k00b Date: Sun, 27 Oct 2024 02:43:45 -0500 Subject: [PATCH] get vault working --- api/resolvers/vault.js | 15 +- api/resolvers/wallet.js | 34 ++-- api/typeDefs/vault.js | 2 + components/device-sync.js | 10 +- components/form.js | 57 +++--- components/qr.js | 18 +- components/vault/use-vault-configurator.js | 24 ++- components/vault/use-vault.js | 28 +-- fragments/vault.js | 13 +- fragments/wallet.js | 5 +- lib/validate.js | 6 + lib/yup.js | 27 ++- pages/settings/index.js | 5 + pages/settings/passphrase/index.js | 203 +++++++++++++++++++++ pages/settings/wallets/[wallet].js | 2 +- wallets/config.js | 2 +- wallets/index.js | 47 +++-- wallets/validate.js | 48 +++-- wallets/webln/index.js | 2 +- 19 files changed, 414 insertions(+), 134 deletions(-) create mode 100644 pages/settings/passphrase/index.js diff --git a/api/resolvers/vault.js b/api/resolvers/vault.js index 5915899f..8ee4237e 100644 --- a/api/resolvers/vault.js +++ b/api/resolvers/vault.js @@ -8,10 +8,8 @@ export default { const k = await models.vault.findUnique({ where: { - userId_key_ownerId_ownerType: { - key, - userId: me.id - } + key, + userId: me.id } }) return k @@ -19,7 +17,7 @@ export default { getVaultEntries: async (parent, { keysFilter }, { me, models }, info) => { if (!me) throw new GqlAuthenticationError() - const entries = await models.vault.findMany({ + const entries = await models.vaultEntry.findMany({ where: { userId: me.id, key: keysFilter?.length @@ -54,12 +52,13 @@ export default { } for (const entry of entries) { + console.log(entry) txs.push(models.vaultEntry.update({ where: { userId_key: { userId: me.id, key: entry.key } }, - data: { value: entry.value } + data: { value: entry.value, iv: entry.iv } })) } - await models.prisma.$transaction(txs) + await models.$transaction(txs) return true }, clearVault: async (parent, args, { me, models }) => { @@ -70,7 +69,7 @@ export default { data: { vaultKeyHash: '' } })) txs.push(models.vaultEntry.deleteMany({ where: { userId: me.id } })) - await models.prisma.$transaction(txs) + await models.$transaction(txs) return true } } diff --git a/api/resolvers/wallet.js b/api/resolvers/wallet.js index 6711b20b..ecf5aacf 100644 --- a/api/resolvers/wallet.js +++ b/api/resolvers/wallet.js @@ -31,9 +31,7 @@ function injectResolvers (resolvers) { const resolverName = generateResolverName(walletDef.walletField) console.log(resolverName) resolvers.Mutation[resolverName] = async (parent, { settings, validateLightning, vaultEntries, ...data }, { me, models }) => { - // allow transformation of the data on validation (this is optional ... won't do anything if not implemented) - // TODO: our validation should be improved - const validData = await validateWallet(walletDef, { ...data, ...settings, vaultEntries }) + const validData = await validateWallet(walletDef, { ...data, ...settings, vaultEntries }, { serverSide: true }) if (validData) { Object.keys(validData).filter(key => key in data).forEach(key => { data[key] = validData[key] }) Object.keys(validData).filter(key => key in settings).forEach(key => { settings[key] = validData[key] }) @@ -700,22 +698,27 @@ async function upsertWallet ( data: { enabled, priority, - [wallet.field]: { - update: { - where: { walletId: Number(id) }, - data: walletData - } - }, + // client only wallets has no walletData + ...(Object.keys(walletData).length > 0 + ? { + [wallet.field]: { + update: { + where: { walletId: Number(id) }, + data: walletData + } + } + } + : {}), vaultEntries: { deleteMany: difference(oldVaultEntries, vaultEntries, 'key').map(({ key }) => ({ userId: me.id, key })), - create: difference(vaultEntries, oldVaultEntries, 'key').map(({ key, value }) => ({ - key, value, userId: me.id + create: difference(vaultEntries, oldVaultEntries, 'key').map(({ key, iv, value }) => ({ + key, iv, value, userId: me.id })), - update: intersectionMerge(oldVaultEntries, vaultEntries, 'key').map(({ key, value }) => ({ + update: intersectionMerge(oldVaultEntries, vaultEntries, 'key').map(({ key, iv, value }) => ({ where: { userId_key: { userId: me.id, key } }, - data: { value } + data: { value, iv } })) } }, @@ -735,9 +738,8 @@ async function upsertWallet ( priority, userId: me.id, type: wallet.type, - [wallet.field]: { - create: walletData - }, + // client only wallets has no walletData + ...(Object.keys(walletData).length > 0 ? { [wallet.field]: { create: walletData } } : {}), vaultEntries: { createMany: { data: vaultEntries.map(({ key, value }) => ({ key, value, userId: me.id })) diff --git a/api/typeDefs/vault.js b/api/typeDefs/vault.js index a1600ea9..76be6a4e 100644 --- a/api/typeDefs/vault.js +++ b/api/typeDefs/vault.js @@ -4,6 +4,7 @@ export default gql` type VaultEntry { id: ID! key: String! + iv: String! value: String! createdAt: Date! updatedAt: Date! @@ -11,6 +12,7 @@ export default gql` input VaultEntryInput { key: String! + iv: String! value: String! walletId: ID } diff --git a/components/device-sync.js b/components/device-sync.js index 2a3084d5..99b590d8 100644 --- a/components/device-sync.js +++ b/components/device-sync.js @@ -15,11 +15,11 @@ import { useApolloClient } from '@apollo/client' export default function DeviceSync () { const { me } = useMe() const apollo = useApolloClient() - const [value, setVaultKey, clearVault, disconnectVault] = useVaultConfigurator() + const { key, setVaultKey, clearVault } = useVaultConfigurator() const showModal = useShowModal() const enabled = !!me?.privates?.vaultKeyHash - const connected = !!value?.key + const connected = !!key const manage = useCallback(async () => { if (enabled && connected) { @@ -27,7 +27,7 @@ export default function DeviceSync () {

Device sync is enabled!

- Sensitive data (like wallet credentials) is now securely synced between all connected devices. + Sensitive data (like wallet credentials) are now securely synced between all connected devices.

Disconnect to prevent this device from syncing data or to reset your passphrase. @@ -38,7 +38,7 @@ export default function DeviceSync () {