Skip to content

Commit aede5aa

Browse files
Merge pull request #402 from EYBlockchain/hari/client-sync
fix: move initialclientsync to incomingviewingkey
2 parents d68f3b0 + 625a0c7 commit aede5aa

File tree

4 files changed

+75
-31
lines changed

4 files changed

+75
-31
lines changed

‎nightfall-client/src/event-handlers/block-proposed.mjs

+3-31
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import Timber from 'common-files/classes/timber.mjs';
44
import {
55
markNullifiedOnChain,
66
markOnChain,
7-
storeCommitment,
87
countCommitments,
98
setSiblingInfo,
109
} from '../services/commitment-storage.mjs';
1110
import getProposeBlockCalldata from '../services/process-calldata.mjs';
12-
import Secrets from '../classes/secrets.mjs';
1311
import { ivks, nsks } from '../services/keys.mjs';
1412
import { getLatestTree, saveTree, saveTransaction, saveBlock } from '../services/database.mjs';
13+
import { decryptCommitment } from '../services/commitment-sync.mjs';
1514

1615
const { ZERO } = config;
1716

@@ -38,38 +37,11 @@ async function blockProposedEventHandler(data) {
3837
// filter out non zero commitments and nullifiers
3938
const nonZeroCommitments = transaction.commitments.flat().filter(n => n !== ZERO);
4039
const nonZeroNullifiers = transaction.nullifiers.flat().filter(n => n !== ZERO);
41-
const storeCommitments = [];
4240
if (
4341
(transaction.transactionType === '1' || transaction.transactionType === '2') &&
4442
(await countCommitments(nonZeroCommitments)) === 0
45-
) {
46-
let keysTried = 1;
47-
ivks.forEach((key, i) => {
48-
// decompress the secrets first and then we will decryp t the secrets from this
49-
const decompressedSecrets = Secrets.decompressSecrets(transaction.compressedSecrets);
50-
try {
51-
const commitment = Secrets.decryptSecrets(
52-
decompressedSecrets,
53-
key,
54-
nonZeroCommitments[0],
55-
);
56-
if (Object.keys(commitment).length === 0)
57-
logger.info(
58-
`This encrypted message isn't for this recipient, keys tried = ${keysTried++}`,
59-
);
60-
else {
61-
// console.log('PUSHED', commitment, 'nsks', nsks[i]);
62-
storeCommitments.push(storeCommitment(commitment, nsks[i]));
63-
}
64-
} catch (err) {
65-
logger.info(err);
66-
logger.info(
67-
`*This encrypted message isn't for this recipient, keys tried = ${keysTried++}`,
68-
);
69-
}
70-
});
71-
}
72-
await Promise.all(storeCommitments);
43+
)
44+
await decryptCommitment(transaction, ivks, nsks);
7345
return Promise.all([
7446
markOnChain(nonZeroCommitments, block.blockNumberL2, data.blockNumber, data.transactionHash),
7547
markNullifiedOnChain(

‎nightfall-client/src/routes/incoming-viewing-key.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import express from 'express';
66
import { generalise } from 'general-number';
77
import logger from 'common-files/utils/logger.mjs';
88
import { storeMemoryKeysForDecryption } from '../services/keys.mjs';
9+
import { clientCommitmentSync } from '../services/commitment-sync.mjs';
910

1011
const router = express.Router();
1112

@@ -17,6 +18,10 @@ router.post('/', async (req, res, next) => {
1718
ivks.map(ivk => ivk.bigInt),
1819
nsks.map(nsk => nsk.bigInt),
1920
);
21+
await clientCommitmentSync(
22+
ivks.map(ivk => ivk.bigInt),
23+
nsks.map(nsk => nsk.bigInt),
24+
);
2025
res.json({ status: 'success' });
2126
} catch (err) {
2227
logger.error(err);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
commitmentsync services to decrypt commitments from transaction blockproposed events
3+
or use clientCommitmentSync to decrypt when new ivk is received.
4+
*/
5+
6+
import config from 'config';
7+
import logger from 'common-files/utils/logger.mjs';
8+
import Secrets from '../classes/secrets.mjs';
9+
import { getAllTransactions } from './database.mjs';
10+
import { countCommitments, storeCommitment } from './commitment-storage.mjs';
11+
12+
const { ZERO } = config;
13+
14+
/**
15+
decrypt commitments for a transaction given ivks and nsks.
16+
*/
17+
export async function decryptCommitment(transaction, ivk, nsk) {
18+
const nonZeroCommitments = transaction.commitments.flat().filter(n => n !== ZERO);
19+
const storeCommitments = [];
20+
ivk.forEach((key, j) => {
21+
// decompress the secrets first and then we will decryp t the secrets from this
22+
const decompressedSecrets = Secrets.decompressSecrets(transaction.compressedSecrets);
23+
logger.info(`decompressedSecrets: ${decompressedSecrets}`);
24+
try {
25+
const commitment = Secrets.decryptSecrets(decompressedSecrets, key, nonZeroCommitments[0]);
26+
if (Object.keys(commitment).length === 0)
27+
logger.info("This encrypted message isn't for this recipient");
28+
else {
29+
// console.log('PUSHED', commitment, 'nsks', nsks[i]);
30+
storeCommitments.push(storeCommitment(commitment, nsk[j]));
31+
}
32+
} catch (err) {
33+
logger.info(err);
34+
logger.info("This encrypted message isn't for this recipient");
35+
}
36+
});
37+
await Promise.all(storeCommitments).catch(function (err) {
38+
logger.info(err);
39+
});
40+
}
41+
42+
/**
43+
Called when new ivk(s) are recieved , it fetches all available commitments
44+
from commitments collection and decrypts commitments belonging to the new ivk(s).
45+
*/
46+
export async function clientCommitmentSync(ivk, nsk) {
47+
const transactions = await getAllTransactions();
48+
for (let i = 0; i < transactions.length; i++) {
49+
// filter out non zero commitments and nullifiers
50+
const nonZeroCommitments = transactions[i].commitments.flat().filter(n => n !== ZERO);
51+
if (
52+
(transactions[i].transactionType === '1' || transactions[i].transactionType === '2') &&
53+
countCommitments(nonZeroCommitments) === 0
54+
)
55+
decryptCommitment(transactions[i], ivk, nsk);
56+
}
57+
}

‎nightfall-client/src/services/database.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ export async function saveTransaction(_transaction) {
183183
throw new Error('Attempted to replay existing transaction');
184184
}
185185

186+
/*
187+
To get all transactions in the collection. This can be used
188+
to decrypt commitments when new ivk is received.
189+
*/
190+
export async function getAllTransactions() {
191+
const connection = await mongo.connection(MONGO_URL);
192+
const db = connection.db(COMMITMENTS_DB);
193+
return db.collection(TRANSACTIONS_COLLECTION).find().toArray();
194+
}
195+
186196
/*
187197
For added safety we only delete mempool: true, we should never be deleting
188198
transactions from our local db that have been spent.

0 commit comments

Comments
 (0)