Skip to content

Commit

Permalink
Merge pull request #402 from EYBlockchain/hari/client-sync
Browse files Browse the repository at this point in the history
fix: move initialclientsync to incomingviewingkey
  • Loading branch information
ChaitanyaKonda authored Feb 7, 2022
2 parents d68f3b0 + 625a0c7 commit aede5aa
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 31 deletions.
34 changes: 3 additions & 31 deletions nightfall-client/src/event-handlers/block-proposed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import Timber from 'common-files/classes/timber.mjs';
import {
markNullifiedOnChain,
markOnChain,
storeCommitment,
countCommitments,
setSiblingInfo,
} 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;

Expand All @@ -38,38 +37,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
) {
let keysTried = 1;
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, keys tried = ${keysTried++}`,
);
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, keys tried = ${keysTried++}`,
);
}
});
}
await Promise.all(storeCommitments);
)
await decryptCommitment(transaction, ivks, nsks);
return Promise.all([
markOnChain(nonZeroCommitments, block.blockNumberL2, data.blockNumber, data.transactionHash),
markNullifiedOnChain(
Expand Down
5 changes: 5 additions & 0 deletions nightfall-client/src/routes/incoming-viewing-key.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import express from 'express';
import { generalise } from 'general-number';
import logger from 'common-files/utils/logger.mjs';
import { storeMemoryKeysForDecryption } from '../services/keys.mjs';
import { clientCommitmentSync } from '../services/commitment-sync.mjs';

const router = express.Router();

Expand All @@ -17,6 +18,10 @@ router.post('/', async (req, res, next) => {
ivks.map(ivk => ivk.bigInt),
nsks.map(nsk => nsk.bigInt),
);
await clientCommitmentSync(
ivks.map(ivk => ivk.bigInt),
nsks.map(nsk => nsk.bigInt),
);
res.json({ status: 'success' });
} catch (err) {
logger.error(err);
Expand Down
57 changes: 57 additions & 0 deletions nightfall-client/src/services/commitment-sync.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
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);
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);
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]);
storeCommitments.push(storeCommitment(commitment, nsk[j]));
}
} catch (err) {
logger.info(err);
logger.info("This encrypted message isn't for this recipient");
}
});
await Promise.all(storeCommitments).catch(function (err) {
logger.info(err);
});
}

/**
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();
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);
}
}
10 changes: 10 additions & 0 deletions nightfall-client/src/services/database.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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.
Expand Down

0 comments on commit aede5aa

Please sign in to comment.