From ca14ce27b5f8a6c2ef0e7550bc053a0d430da189 Mon Sep 17 00:00:00 2001 From: dwebchapey Date: Mon, 17 Jan 2022 12:22:03 +0530 Subject: [PATCH 1/4] fix: enqueue initialClientSync in ivk route --- nightfall-client/src/routes/incoming-viewing-key.mjs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nightfall-client/src/routes/incoming-viewing-key.mjs b/nightfall-client/src/routes/incoming-viewing-key.mjs index 3bc89de16..a31b95ebd 100644 --- a/nightfall-client/src/routes/incoming-viewing-key.mjs +++ b/nightfall-client/src/routes/incoming-viewing-key.mjs @@ -5,6 +5,8 @@ Route for setting the decryption key that will be used to decrypt secrets during import express from 'express'; import { generalise } from 'general-number'; import logger from 'common-files/utils/logger.mjs'; +import { enqueueEvent } from 'common-files/utils/event-queue.mjs'; +import { initialClientSync } from '../services/state-sync.mjs'; import { storeMemoryKeysForDecryption } from '../services/keys.mjs'; const router = express.Router(); @@ -17,6 +19,7 @@ router.post('/', async (req, res, next) => { ivks.map(ivk => ivk.bigInt), nsks.map(nsk => nsk.bigInt), ); + enqueueEvent(initialClientSync, 0); res.json({ status: 'success' }); } catch (err) { logger.error(err); From 42f76f2d11f136e4d94c546ea3c941271aeff254 Mon Sep 17 00:00:00 2001 From: dwebchapey Date: Thu, 3 Feb 2022 02:28:11 +0530 Subject: [PATCH 2/4] fix: decrypt commitments from txns on new ivk --- .../src/event-handlers/block-proposed.mjs | 29 +--------- .../src/routes/incoming-viewing-key.mjs | 8 ++- .../src/services/commitment-sync.mjs | 56 +++++++++++++++++++ nightfall-client/src/services/database.mjs | 10 ++++ 4 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 nightfall-client/src/services/commitment-sync.mjs diff --git a/nightfall-client/src/event-handlers/block-proposed.mjs b/nightfall-client/src/event-handlers/block-proposed.mjs index 9ee33d818..90f51f5c3 100644 --- a/nightfall-client/src/event-handlers/block-proposed.mjs +++ b/nightfall-client/src/event-handlers/block-proposed.mjs @@ -4,15 +4,14 @@ import Timber from 'common-files/classes/timber.mjs'; import { markNullifiedOnChain, markOnChain, - storeCommitment, countCommitments, setSiblingInfo, countTransactionHashes, } from '../services/commitment-storage.mjs'; import getProposeBlockCalldata from '../services/process-calldata.mjs'; -import Secrets from '../classes/secrets.mjs'; import { ivks, nsks } from '../services/keys.mjs'; import { getLatestTree, saveTree, saveTransaction, saveBlock } from '../services/database.mjs'; +import { decryptCommitment } from '../services/commitment-sync.mjs'; const { ZERO } = config; @@ -36,33 +35,11 @@ async function blockProposedEventHandler(data) { // filter out non zero commitments and nullifiers const nonZeroCommitments = transaction.commitments.flat().filter(n => n !== ZERO); const nonZeroNullifiers = transaction.nullifiers.flat().filter(n => n !== ZERO); - const storeCommitments = []; if ( (transaction.transactionType === '1' || transaction.transactionType === '2') && (await countCommitments(nonZeroCommitments)) === 0 - ) { - ivks.forEach((key, i) => { - // decompress the secrets first and then we will decryp t the secrets from this - const decompressedSecrets = Secrets.decompressSecrets(transaction.compressedSecrets); - try { - const commitment = Secrets.decryptSecrets( - decompressedSecrets, - key, - nonZeroCommitments[0], - ); - if (Object.keys(commitment).length === 0) - logger.info("This encrypted message isn't for this recipient"); - else { - // console.log('PUSHED', commitment, 'nsks', nsks[i]); - storeCommitments.push(storeCommitment(commitment, nsks[i])); - } - } catch (err) { - logger.info(err); - logger.info("This encrypted message isn't for this recipient"); - } - }); - } - await Promise.all(storeCommitments); + ) + await decryptCommitment(transaction, ivks, nsks); return Promise.all([ markOnChain(nonZeroCommitments, block.blockNumberL2, data.blockNumber, data.transactionHash), markNullifiedOnChain( diff --git a/nightfall-client/src/routes/incoming-viewing-key.mjs b/nightfall-client/src/routes/incoming-viewing-key.mjs index a31b95ebd..4e725f488 100644 --- a/nightfall-client/src/routes/incoming-viewing-key.mjs +++ b/nightfall-client/src/routes/incoming-viewing-key.mjs @@ -5,9 +5,8 @@ Route for setting the decryption key that will be used to decrypt secrets during import express from 'express'; import { generalise } from 'general-number'; import logger from 'common-files/utils/logger.mjs'; -import { enqueueEvent } from 'common-files/utils/event-queue.mjs'; -import { initialClientSync } from '../services/state-sync.mjs'; import { storeMemoryKeysForDecryption } from '../services/keys.mjs'; +import { clientCommitmentSync } from '../services/commitment-sync.mjs'; const router = express.Router(); @@ -19,7 +18,10 @@ router.post('/', async (req, res, next) => { ivks.map(ivk => ivk.bigInt), nsks.map(nsk => nsk.bigInt), ); - enqueueEvent(initialClientSync, 0); + await clientCommitmentSync( + ivks.map(ivk => ivk.bigInt), + nsks.map(nsk => nsk.bigInt), + ); res.json({ status: 'success' }); } catch (err) { logger.error(err); diff --git a/nightfall-client/src/services/commitment-sync.mjs b/nightfall-client/src/services/commitment-sync.mjs new file mode 100644 index 000000000..51f4b9c2c --- /dev/null +++ b/nightfall-client/src/services/commitment-sync.mjs @@ -0,0 +1,56 @@ +/** +commitmentsync services to decrypt commitments from transaction blockproposed events +or use clientCommitmentSync to decrypt when new ivk is received. +*/ + +import config from 'config'; +import logger from 'common-files/utils/logger.mjs'; +import Secrets from '../classes/secrets.mjs'; +import { getAllTransactions } from './database.mjs'; +import { countCommitments, storeCommitment } from './commitment-storage.mjs'; + +const { ZERO } = config; + +/** +decrypt commitments for a transaction given ivks and nsks. +*/ +export async function decryptCommitment(transaction, ivk, nsk) { + const nonZeroCommitments = transaction.commitments.flat().filter(n => n !== ZERO); + ivk.forEach((key, j) => { + // decompress the secrets first and then we will decryp t the secrets from this + const decompressedSecrets = Secrets.decompressSecrets(transaction.compressedSecrets); + logger.info(`decompressedSecrets: ${decompressedSecrets}`); + try { + const commitment = Secrets.decryptSecrets(decompressedSecrets, key, nonZeroCommitments[0]); + if (Object.keys(commitment).length === 0) + logger.info("This encrypted message isn't for this recipient"); + else { + // console.log('PUSHED', commitment, 'nsks', nsks[i]); + logger.info('This is encrpted and stored'); + storeCommitment(commitment, nsk[j]); + } + } catch (err) { + logger.info(err); + logger.info('catch error'); + } + }); +} + +/** +Called when new ivk(s) are recieved , it fetches all available commitments +from commitments collection and decrypts commitments belonging to the new ivk(s). +*/ +export async function clientCommitmentSync(ivk, nsk) { + const transactions = await getAllTransactions(); + if (typeof transactions !== 'undefined') { + for (let i = 0; i < transactions.length; i++) { + // filter out non zero commitments and nullifiers + const nonZeroCommitments = transactions[i].commitments.flat().filter(n => n !== ZERO); + if ( + (transactions[i].transactionType === '1' || transactions[i].transactionType === '2') && + countCommitments(nonZeroCommitments) === 0 + ) + decryptCommitment(transactions[i], ivk, nsk); + } + } +} diff --git a/nightfall-client/src/services/database.mjs b/nightfall-client/src/services/database.mjs index 8cb2169f5..7741172d2 100644 --- a/nightfall-client/src/services/database.mjs +++ b/nightfall-client/src/services/database.mjs @@ -176,6 +176,16 @@ export async function saveTransaction(_transaction) { throw new Error('Attempted to replay existing transaction'); } +/* +To get all transactions in the collection. This can be used +to decrypt commitments when new ivk is received. +*/ +export async function getAllTransactions() { + const connection = await mongo.connection(MONGO_URL); + const db = connection.db(COMMITMENTS_DB); + return db.collection(TRANSACTIONS_COLLECTION).find().toArray(); +} + /* For added safety we only delete mempool: true, we should never be deleting transactions from our local db that have been spent. From c899a4fecca89519c090c81751b8e1a8eba73f10 Mon Sep 17 00:00:00 2001 From: dwebchapey Date: Thu, 3 Feb 2022 15:12:16 +0530 Subject: [PATCH 3/4] fix: store commitment in promise --- nightfall-client/src/services/commitment-sync.mjs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nightfall-client/src/services/commitment-sync.mjs b/nightfall-client/src/services/commitment-sync.mjs index 51f4b9c2c..b6203e617 100644 --- a/nightfall-client/src/services/commitment-sync.mjs +++ b/nightfall-client/src/services/commitment-sync.mjs @@ -16,6 +16,7 @@ decrypt commitments for a transaction given ivks and nsks. */ export async function decryptCommitment(transaction, ivk, nsk) { const nonZeroCommitments = transaction.commitments.flat().filter(n => n !== ZERO); + const storeCommitments = []; ivk.forEach((key, j) => { // decompress the secrets first and then we will decryp t the secrets from this const decompressedSecrets = Secrets.decompressSecrets(transaction.compressedSecrets); @@ -26,14 +27,16 @@ export async function decryptCommitment(transaction, ivk, nsk) { logger.info("This encrypted message isn't for this recipient"); else { // console.log('PUSHED', commitment, 'nsks', nsks[i]); - logger.info('This is encrpted and stored'); - storeCommitment(commitment, nsk[j]); + storeCommitments.push(storeCommitment(commitment, nsk[j])); } } catch (err) { logger.info(err); - logger.info('catch error'); + logger.info("This encrypted message isn't for this recipient"); } }); + await Promise.all(storeCommitments).catch(function (err) { + logger.info(err); + }); } /** From 9ec34b6db472179a2e5184a8cf3d035c4b0a4765 Mon Sep 17 00:00:00 2001 From: dwebchapey Date: Thu, 3 Feb 2022 22:26:27 +0530 Subject: [PATCH 4/4] fix: remove check of getAllTransactions --- .../src/services/commitment-sync.mjs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/nightfall-client/src/services/commitment-sync.mjs b/nightfall-client/src/services/commitment-sync.mjs index b6203e617..8b6414abf 100644 --- a/nightfall-client/src/services/commitment-sync.mjs +++ b/nightfall-client/src/services/commitment-sync.mjs @@ -45,15 +45,13 @@ from commitments collection and decrypts commitments belonging to the new ivk(s) */ export async function clientCommitmentSync(ivk, nsk) { const transactions = await getAllTransactions(); - if (typeof transactions !== 'undefined') { - for (let i = 0; i < transactions.length; i++) { - // filter out non zero commitments and nullifiers - const nonZeroCommitments = transactions[i].commitments.flat().filter(n => n !== ZERO); - if ( - (transactions[i].transactionType === '1' || transactions[i].transactionType === '2') && - countCommitments(nonZeroCommitments) === 0 - ) - decryptCommitment(transactions[i], ivk, nsk); - } + for (let i = 0; i < transactions.length; i++) { + // filter out non zero commitments and nullifiers + const nonZeroCommitments = transactions[i].commitments.flat().filter(n => n !== ZERO); + if ( + (transactions[i].transactionType === '1' || transactions[i].transactionType === '2') && + countCommitments(nonZeroCommitments) === 0 + ) + decryptCommitment(transactions[i], ivk, nsk); } }