Skip to content

Commit

Permalink
Merge pull request #323 from EYBlockchain/swati/eventListenerFix
Browse files Browse the repository at this point in the history
event listener fixes
  • Loading branch information
SwatiEY authored Aug 9, 2024
2 parents 0743cea + 5dbb58b commit 6b1a74e
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/boilerplate/common/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ if [[ ! $setup == 'n' ]]
then
printf "\n${GREEN}*** Starting the zokrates container ***${NC}\n"

docker-compose -f docker-compose.zapp.yml up -d zokrates
docker compose -f docker-compose.zapp.yml up -d zokrates

sleep 5

printf "\n${GREEN}*** Running setup for your ZApp... ***${NC}\n"

docker-compose -f docker-compose.zapp.yml run zapp-setup node /app/orchestration/common/zkp-setup.mjs -i ''
docker compose -f docker-compose.zapp.yml run zapp-setup node /app/orchestration/common/zkp-setup.mjs -i ''

printf "\n${GREEN}*** Setup complete! Writing verification key to db... ***${NC}\n"

docker-compose -f docker-compose.zapp.yml run zapp-setup node /app/write-vk.mjs -i ''
docker compose -f docker-compose.zapp.yml run zapp-setup node /app/write-vk.mjs -i ''

fi
printf "\n${GREEN}*** Finished! ***${NC}\n"
12 changes: 6 additions & 6 deletions src/boilerplate/common/bin/startup
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#!/bin/sh
set -e

docker-compose -f docker-compose.zapp.yml down -v
docker compose -f docker-compose.zapp.yml down -v

sleep 5

docker-compose -f docker-compose.zapp.yml up -d ganache
docker-compose -f docker-compose.zapp.yml up -d zokrates
docker compose -f docker-compose.zapp.yml up -d ganache
docker compose -f docker-compose.zapp.yml up -d zokrates

sleep 5

CONSTRUCTOR_CALL

docker-compose -f docker-compose.zapp.yml up -d deployer
docker compose -f docker-compose.zapp.yml up -d deployer

sleep 25

docker-compose -f docker-compose.zapp.yml up -d timber
docker compose -f docker-compose.zapp.yml up -d timber

sleep 10

docker-compose -f docker-compose.zapp.yml up -d zapp
docker compose -f docker-compose.zapp.yml up -d zapp

echo "All services have been started."
echo "----------------------------------------------------"
Expand Down
4 changes: 2 additions & 2 deletions src/boilerplate/common/bin/startup-double
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
set -e

docker-compose -f docker-compose.zapp-double.yml down -v
docker compose -f docker-compose.zapp-double.yml down -v

echo "Starting ganache..."

Expand All @@ -13,7 +13,7 @@ sleep 30
echo "Starting deployer and zokrates..."
CONSTRUCTOR_CALL

docker-compose -f docker-compose.zapp-double.yml up -d deployer $BUILD_ARG
docker compose -f docker-compose.zapp-double.yml up -d deployer $BUILD_ARG

docker compose -f docker-compose.zapp-double.yml up -d zokrates zokrates2 $BUILD_ARG
echo "Waiting for 20 seconds before starting timber..."
Expand Down
2 changes: 1 addition & 1 deletion src/boilerplate/common/boilerplate-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"retest": "docker-compose -f docker-compose.zapp.yml run zapp npx mocha --exit --require @babel/register 'orchestration/test.mjs'",
"start": "./bin/startup",
"restart": "docker-compose -f docker-compose.zapp.yml restart && docker compose -f docker-compose.zapp-double.yml logs -f -n 1000 zapp zokrates timber",
"apitest": "./bin/setup && ./bin/startup && docker-compose -f docker-compose.zapp.yml run --name apiservice -d -p 3000:3000 zapp node orchestration/api.mjs"
"apitest": "./bin/setup && ./bin/startup && docker compose -f docker-compose.zapp.yml run --name apiservice -d -p 3000:3000 zapp node orchestration/api.mjs"
},
"keywords": [
"private contract",
Expand Down
61 changes: 37 additions & 24 deletions src/boilerplate/common/commitment-storage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,46 @@ let temp_smt_tree = SMT(hlt[0]); // for temporary updates before proof generatio
// Gets the hash of a smt_tree (or subtree)
export const getHash = tree => reduceTree(poseidonConcatHash, tree);

// function to format a commitment for a mongo db and store it
export async function storeCommitment(commitment) {
const connection = await mongo.connection(MONGO_URL);
const db = connection.db(COMMITMENTS_DB);
// we'll also compute and store the nullifier hash.
const nullifierHash = commitment.secretKey
? poseidonHash([
export function formatCommitment (commitment) {
let data
try {
const nullifierHash = commitment.secretKey
? poseidonHash([
BigInt(commitment.preimage.stateVarId.hex(32)),
BigInt(commitment.secretKey.hex(32)),
BigInt(commitment.preimage.salt.hex(32)),
BigInt(commitment.preimage.salt.hex(32))
])
: '';
const preimage = generalise(commitment.preimage).all.hex(32);
preimage.value = generalise(commitment.preimage.value).all
? generalise(commitment.preimage.value).all.integer
: generalise(commitment.preimage.value).integer;
const data = {
_id: commitment.hash.hex(32),
name: commitment.name,
mappingKey: commitment.mappingKey ? commitment.mappingKey : null,
secretKey: commitment.secretKey ? commitment.secretKey.hex(32) : null,
preimage,
isNullified: commitment.isNullified,
nullifier: commitment.secretKey ? nullifierHash.hex(32) : null,
};
logger.debug(`Storing commitment ${data._id}`);
return db.collection(COMMITMENTS_COLLECTION).insertOne(data);
: ''
const preimage = generalise(commitment.preimage).all.hex(32)
preimage.value = generalise(commitment.preimage.value).all
? generalise(commitment.preimage.value).all.integer
: generalise(commitment.preimage.value).integer
data = {
_id: commitment.hash.hex(32),
name: commitment.name,
source: commitment.source,
mappingKey: commitment.mappingKey ? commitment.mappingKey : null,
secretKey: commitment.secretKey ? commitment.secretKey.hex(32) : null,
preimage,
isNullified: commitment.isNullified,
nullifier: commitment.secretKey ? nullifierHash.hex(32) : null
}
logger.debug(`Storing commitment ${data._id}`)
} catch (error) {
console.error('Error --->', error)
}
return data
}

export async function persistCommitment (data) {
const connection = await mongo.connection(MONGO_URL)
const db = connection.db(COMMITMENTS_DB)
return db.collection(COMMITMENTS_COLLECTION).insertOne(data)
}
// function to format a commitment for a mongo db and store it
export async function storeCommitment (commitment) {
const data = formatCommitment(commitment)
return persistCommitment(data)
}

// function to retrieve commitment with a specified stateVarId
Expand Down
56 changes: 54 additions & 2 deletions src/boilerplate/common/encrypted-data-listener.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ import utils from 'zkp-utils';
import config from 'config';
import { generalise } from 'general-number';
import { getContractAddress, getContractInstance, registerKey } from './common/contract.mjs';
import { storeCommitment } from './common/commitment-storage.mjs';
import { decrypt } from './common/number-theory.mjs';
import { storeCommitment, formatCommitment, persistCommitment } from './common/commitment-storage.mjs';
import { decrypt, poseidonHash, } from './common/number-theory.mjs';

const keyDb = '/app/orchestration/common/db/key.json';


function decodeCommitmentData(decrypted){
const stateVarId = generalise(decrypted[0]);
const salt = generalise(decrypted[decrypted.length - 1]);
let newCommitment;

ENCRYPTEDVARIABLE_COMMITMENT
return newCommitment;
}

export default class EncryptedDataEventListener {
constructor(web3) {
this.web3 = web3;
Expand Down Expand Up @@ -43,6 +53,48 @@ export default class EncryptedDataEventListener {
}
}

async fetchBackupData () {
await this.init()
const instance = this.instance
const eventName = 'BackupData'
const eventJsonInterface = this.instance._jsonInterface.find(
o => o.name === eventName && o.type === 'event'
)
console.log('Getting data from past events. This can take a while...')
const backupEvents = await instance.getPastEvents('BackupData', {
fromBlock: this.contractMetadata.blockNumber || 1,
topics: [eventJsonInterface.signature, this.ethAddress.hex(32)]
})
console.log('Getting nullifiers. This can take a while...')
const nullifierEvents = await instance.getPastEvents('Nullifiers', {
fromBlock: this.contractMetadata.blockNumber || 1
})
const nullifiers = nullifierEvents
.flatMap(e => e.returnValues.nullifiers)
return Promise.all(
backupEvents
.map(e => decrypt(e.returnValues.cipherText, this.publicKey, this.secretKey))
.map(decodeCommitmentData)
.filter(c => c)
.map(formatCommitment)
.map(c => {
c.isNullified = nullifiers.includes(BigInt(c.nullifier).toString())
return c
})
)
}
async saveBackupData (allCommitments) {
return allCommitments.map(async commit => {
try {
await persistCommitment(commit)
} catch (e) {
if (e.toString().includes('E11000 duplicate key')) {
logger.info('Commitment already exists. Thats fine.')
}
}
})
}

async start() {
try {
await this.init();
Expand Down
60 changes: 47 additions & 13 deletions src/codeGenerators/orchestration/files/toOrchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,44 @@ file.file = file.file.replace(/CONTRACT_NAME/g, node.contractName);
let encryptedCode = '';
let encryptedStateVarId = '';
let encryptedValue = ''
let encryptedCommitmentCode = '';
// This removes the repeated elements
const uniqueMap = new Map();
node.stateVariables.forEach(item => {
if (!uniqueMap.has(item.id) || (item.isMapping && !uniqueMap.get(item.id).isMapping)) {
uniqueMap.set(item.id, item);
}
});

node.stateVariables = Array.from(uniqueMap.values());
node.stateVariables?.forEach(
variable => {
variable.isMapping ? encryptedStateVarId = `const ${variable.name}_stateVarId = generalise(utils.mimcHash([generalise(${variable.id}).bigInt, self.ethAddress.bigInt], 'ALT_BN_254')).hex(32);`
: `const ${variable.name}_stateVarId = ${variable.id}`;
variable.isStruct ? variable.structProperty.forEach( (structProp, index) => {
encryptedValue += `
const ${structProp} = generalise(decrypted[${index+1}]);`
}) : encryptedValue += ` const value = generalise(decrypted[1]); `;
}) : '';
if(variable.isStruct) {
encryptedCommitmentCode += `
if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
${encryptedValue};
newCommitment = poseidonHash([
BigInt(stateVarId.hex(32)),
${variable.structProperty.map(structProp => `BigInt(${structProp}.hex(32))`).join(', \n')},
BigInt(self.publicKey.hex(32)),
BigInt(salt.hex(32))
]);
}`
encryptedCode += `
const newCommitment = poseidonHash([
BigInt(stateVarId.hex(32)),
${variable.structProperty.map(structProp => `BigInt(${structProp}.hex(32))`).join(', \n')},
BigInt(self.publicKey.hex(32)),
BigInt(salt.hex(32))
]);
if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
${encryptedValue};
const newCommitment = poseidonHash([
BigInt(stateVarId.hex(32)),
${variable.structProperty.map(structProp => `BigInt(${structProp}.hex(32))`).join(', \n')},
BigInt(self.publicKey.hex(32)),
BigInt(salt.hex(32))
]);
try {
await storeCommitment({
hash: newCommitment,
Expand Down Expand Up @@ -294,16 +314,29 @@ node.stateVariables?.forEach(
}`;

} else {
encryptedCode += `
const newCommitment = poseidonHash([
encryptedCommitmentCode += `
if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
const value = generalise(decrypted[1]);
newCommitment = poseidonHash([
BigInt(stateVarId.hex(32)),
BigInt(value.hex(32)),
BigInt(self.publicKey.hex(32)),
BigInt(salt.hex(32))
]);
]);
}`
encryptedCode += `
if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
const value = generalise(decrypted[1]);
const newCommitment = poseidonHash([
BigInt(stateVarId.hex(32)),
BigInt(value.hex(32)),
BigInt(self.publicKey.hex(32)),
BigInt(salt.hex(32))
]);
try {
await storeCommitment({
hash: newCommitment,
Expand Down Expand Up @@ -336,7 +369,8 @@ node.stateVariables?.forEach(

}
)
encryptedCode = encryptedStateVarId + encryptedValue + encryptedCode;
encryptedCode = encryptedStateVarId + encryptedCode;
file.file = file.file.replace(/ENCRYPTEDVARIABLE_COMMITMENT/g, encryptedCommitmentCode);
file.file = file.file.replace(/ENCRYPTEDVARIABLE_CODE/g, encryptedCode);
return file.file;
}
Expand Down
12 changes: 7 additions & 5 deletions test/contracts/SimpleStruct2.zol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.0;
contract Receipt {

secret mapping (address => Rct) private total;
secret mapping (address => uint256) private sum;

struct Rct {
uint256 amount;
Expand All @@ -13,11 +14,12 @@ contract Receipt {

secret Rct public cmt;

function add(secret Rct memory myrct) public {
unknown total[msg.sender].amount += myrct.amount;
unknown total[msg.sender].tax += myrct.tax;
unknown cmt.amount += myrct.amount;
unknown cmt.tax += myrct.tax;
function add(secret Rct memory myrct, secret address user) public {
encrypt unknown total[user].amount += myrct.amount;
encrypt unknown total[user].tax += myrct.tax;
encrypt unknown cmt.amount += myrct.amount;
encrypt unknown cmt.tax += myrct.tax;
encrypt unknown sum[user] += 1;
}

function remove(secret Rct memory myrct) public {
Expand Down

0 comments on commit 6b1a74e

Please sign in to comment.