Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 132 additions & 158 deletions apitest.js

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions src/boilerplate/common/commitment-storage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,13 @@ export async function joinCommitments(

const signed = await web3.eth.accounts.signTransaction(txParams, key);

const sendTxn = await web3.eth.sendSignedTransaction(signed.rawTransaction);
const tx = await web3.eth.sendSignedTransaction(signed.rawTransaction);

let tx = await instance.getPastEvents('allEvents', {
fromBlock: sendTxn?.blockNumber || 0,
toBlock: sendTxn?.blockNumber || 'latest',
await instance.getPastEvents('allEvents', {
fromBlock: tx?.blockNumber || 0,
toBlock: tx?.blockNumber || 'latest',
});

tx = tx[0];

await markNullified(generalise(commitments[0]._id), secretKey.hex(32));
await markNullified(generalise(commitments[1]._id), secretKey.hex(32));
await storeCommitment({
Expand Down Expand Up @@ -676,15 +674,13 @@ export async function splitCommitments(

const signed = await web3.eth.accounts.signTransaction(txParams, key);

const sendTxn = await web3.eth.sendSignedTransaction(signed.rawTransaction);
const tx = await web3.eth.sendSignedTransaction(signed.rawTransaction);

let tx = await instance.getPastEvents('allEvents', {
fromBlock: sendTxn?.blockNumber || 0,
toBlock: sendTxn?.blockNumber || 'latest',
await instance.getPastEvents('allEvents', {
fromBlock: tx?.blockNumber || 0,
toBlock: tx?.blockNumber || 'latest',
});

tx = tx[0];

await markNullified(generalise(commitment._id), secretKey.hex(32));

await storeCommitment({
Expand Down
4 changes: 2 additions & 2 deletions src/boilerplate/common/contract.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export async function registerKey(
};
const key = config.web3.key;
const signed = await web3.eth.accounts.signTransaction(txParams, key);
const sendTxn = await web3.eth.sendSignedTransaction(signed.rawTransaction);
const tx = await web3.eth.sendSignedTransaction(signed.rawTransaction);
}
const keyJson = {
secretKey: secretKey.integer,
Expand All @@ -156,4 +156,4 @@ export async function registerKey(
fs.writeFileSync(keyDb, JSON.stringify(keyJson, null, 4));

return publicKey;
}
}
14 changes: 7 additions & 7 deletions src/boilerplate/common/generic-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe('FUNCTION_NAME', function () {
await startEventFilter('CONTRACT_NAME');
// this calls your function! It returns the tx from the shield contract
// you can replace the values below - numbers are randomly generated
const { tx , encEvent, encBackupEvent } = await FUNCTION_NAME.FUNCTION_NAME(FUNCTION_SIG_1);
const { tx , newLeavesEvent, encEvent, encBackupEvent } = await FUNCTION_NAME.FUNCTION_NAME(FUNCTION_SIG_1);
// prints the tx
console.log(tx);
// reassigns leafIndex to the index of the first commitment added by this function
if (tx.event) {
leafIndex = tx.returnValues[0];
if (newLeavesEvent) {
leafIndex = newLeavesEvent.returnValues.minLeafIndex ?? newLeavesEvent.returnValues[0];
// prints the new leaves (commitments) added by this function call
console.log(`Merkle tree event returnValues:`);
console.log(tx.returnValues[0]);
console.log(newLeavesEvent.returnValues);
}
if (encEvent && encEvent[0].event) {
encryption.msgs = encEvent[0].returnValues[0];
Expand Down Expand Up @@ -71,10 +71,10 @@ describe('FUNCTION_NAME', function () {
it('should call FUNCTION_NAME again', async () => {
try {
// this calls your function a second time for incremental cases
const { tx } = await FUNCTION_NAME.FUNCTION_NAME(FUNCTION_SIG_2);
if (tx.event) {
const { tx, newLeavesEvent } = await FUNCTION_NAME.FUNCTION_NAME(FUNCTION_SIG_2);
if (newLeavesEvent) {
console.log(`Merkle tree event returnValues:`);
console.log(tx.returnValues[0]);
console.log(newLeavesEvent.returnValues);
}
} catch (err) {
logger.error(err);
Expand Down
24 changes: 6 additions & 18 deletions src/boilerplate/common/services/generic-api_services.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ import { getAllCommitments, getCommitmentsByState } from "./common/commitment-st
import ServiceManager from './common/serviceManager.mjs'
import web3 from "./common/web3.mjs";

/**
Welcome to your zApp's integration test!
Depending on how your functions interact and the range of inputs they expect, the below may need to be changed.
e.g. Your input contract has two functions, add() and minus(). minus() cannot be called before an initial add() - the compiler won't know this! You'll need to rearrange the below.
e.g. The function add() only takes numbers greater than 100. The compiler won't know this, so you'll need to change the call to add() below.
The transpiler automatically fills in any ZKP inputs for you and provides some dummy values for the original zol function.
NOTE: if any non-secret functions need to be called first, the transpiler won't know! You'll need to add those calls below.
NOTE: if you'd like to keep track of your commitments, check out ./common/db/preimage. Remember to delete this file if you'd like to start fresh with a newly deployed contract.
*/
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
let leafIndex;
let encryption = {};
// eslint-disable-next-line func-names

Expand All @@ -34,23 +24,21 @@ export class ServiceManager{
await this.FUNCTION_NAME.init();
}

async service_FUNCTION_NAME (req, res, next){
async service_FUNCTION_NAME (req, res, next){
try {
await startEventFilter('CONTRACT_NAME');
const FUNCTION_SIG;
const { tx , encEvent, encBackupEvent, _RESPONSE_} = await this.FUNCTION_NAME.FUNCTION_NAME(FUNCTION_SIG);
const { tx , newLeavesEvent, encEvent, encBackupEvent, _RESPONSE_} = await this.FUNCTION_NAME.FUNCTION_NAME(FUNCTION_SIG);
// prints the tx
console.log(tx);
const txSerialized = serializeBigInt(tx);
const newLeavesEventSerialized = serializeBigInt(newLeavesEvent);
const encEventSerialized = serializeBigInt(encEvent);
const encBackupEventSerialized = serializeBigInt(encBackupEvent);
res.send({ tx: txSerialized, encEvent: encEventSerialized, encBackupEvent: encBackupEventSerialized, _RESPONSE_ });
// reassigns leafIndex to the index of the first commitment added by this function
if (tx.event) {
leafIndex = tx.returnValues[0];
// prints the new leaves (commitments) added by this function call
res.send({ tx: txSerialized, newLeavesEvent: newLeavesEventSerialized, encEvent: encEventSerialized, encBackupEvent: encBackupEventSerialized, _RESPONSE_ });
if (newLeavesEvent) {
console.log(`Merkle tree event returnValues:`);
console.log(tx.returnValues);
console.log(newLeavesEvent.returnValues);
}
if (encEvent.event) {
encryption.msgs = encEvent[0].returnValues[0];
Expand Down
10 changes: 0 additions & 10 deletions src/boilerplate/common/services/generic-read-only-api_services.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ import { getAllCommitments, getCommitmentsByState } from "./common/commitment-st
import ServiceManager from './common/serviceManager.mjs'
import web3 from "./common/web3.mjs";

/**
Welcome to your zApp's integration test!
Depending on how your functions interact and the range of inputs they expect, the below may need to be changed.
e.g. Your input contract has two functions, add() and minus(). minus() cannot be called before an initial add() - the compiler won't know this! You'll need to rearrange the below.
e.g. The function add() only takes numbers greater than 100. The compiler won't know this, so you'll need to change the call to add() below.
The transpiler automatically fills in any ZKP inputs for you and provides some dummy values for the original zol function.
NOTE: if any non-secret functions need to be called first, the transpiler won't know! You'll need to add those calls below.
NOTE: if you'd like to keep track of your commitments, check out ./common/db/preimage. Remember to delete this file if you'd like to start fresh with a newly deployed contract.
*/
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
let leafIndex;
let encryption = {};
// eslint-disable-next-line func-names

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import fs from "fs";
import logger from "./common/logger.mjs";
import web3 from "./common/web3.mjs";
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
let leafIndex;
let encryption = {};
// eslint-disable-next-line func-names
async service_FUNCTION_NAME (req, res, next){
Expand All @@ -16,9 +15,4 @@ let encryption = {};
console.log(tx);
const txSerialized = serializeBigInt(tx);
res.send({tx: txSerialized, _RESPONSE_});

if (tx.event) {
console.log(tx.returnValues);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,6 @@ integrationApiServicesBoilerplate = {
minus() cannot be called before an initial add(). */

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
let leafIndex;
let encryption = {};
// eslint-disable-next-line func-names

Expand Down
24 changes: 13 additions & 11 deletions src/boilerplate/orchestration/javascript/raw/toOrchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
rtnparams?.push( ` ${param.replace('_change', '').replace('_newCommitmentValue', '')}_newCommitmentValue : ${param}.integer `);
});
if (params) params[params.length - 1] += `,`;
let txReturns = "tx, encEvent, encBackupEvent,";
let txReturns = "tx, newLeavesEvent, encEvent, encBackupEvent,";
if (node.stateMutability === 'view'){
txReturns = "";
}
Expand Down Expand Up @@ -743,7 +743,7 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
}
if (node.isConstructor)
lines.push(
`\nfs.writeFileSync("/app/orchestration/common/db/constructorTx.json", JSON.stringify(tx, null, 4));`,
`\nfs.writeFileSync("/app/orchestration/common/db/constructorTx.json", JSON.stringify(constructorInput, null, 4));`,
);
return {
statements: [
Expand Down Expand Up @@ -1047,7 +1047,7 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
\nBackupData.forEach((element) => {
element.cipherText = element.cipherText.map(ct => generalise(ct).hex(32));
});
\nconst tx = { proofInput: [{customInputs: [${returnInputs}], newNullifiers: ${params[0][0]} commitmentRoot:${params[0][1]} checkNullifiers: ${params[0][3]} newCommitments: ${params[0][2]} cipherText:${params[0][4]} encKeys: ${params[0][5]}}, proof, BackupData], nullifiers: ${params[0][1]} ${publicInputs}};`
\nconst constructorInput = { proofInput: [{customInputs: [${returnInputs}], newNullifiers: ${params[0][0]} commitmentRoot:${params[0][1]} checkNullifiers: ${params[0][3]} newCommitments: ${params[0][2]} cipherText:${params[0][4]} encKeys: ${params[0][5]}}, proof, BackupData], nullifiers: ${params[0][1]} ${publicInputs}};`
]
}
}
Expand All @@ -1071,8 +1071,8 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
};
}
let checkLeaves =
`\n tx = tx[0];\n
\n if (!tx) {
`\n newLeavesEvent = newLeavesEvents[0];\n
\n if (!newLeavesEvent) {
throw new Error( 'Tx failed - the commitment was not accepted on-chain, or the contract is not deployed.');
} \n`;
if (!node.newCommitmentsRequired) {
Expand All @@ -1094,18 +1094,19 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
};
\n const key = config.web3.key;
\n const signed = await web3.eth.accounts.signTransaction(txParams, key);
\n const sendTxn = await web3.eth.sendSignedTransaction(signed.rawTransaction);
\n let tx = await instance.getPastEvents("NewLeaves", {fromBlock: sendTxn?.blockNumber || 0, toBlock: sendTxn?.blockNumber || 'latest'});
\n const tx = await web3.eth.sendSignedTransaction(signed.rawTransaction);
\n const newLeavesEvents = await instance.getPastEvents("NewLeaves", {fromBlock: tx?.blockNumber || 0, toBlock: tx?.blockNumber || 'latest'});
\n let newLeavesEvent = null;
${checkLeaves}
let encEvent = '';
\n try {
\n encEvent = await instance.getPastEvents("EncryptedData", {fromBlock: sendTxn?.blockNumber || 0, toBlock: sendTxn?.blockNumber || 'latest'});
\n encEvent = await instance.getPastEvents("EncryptedData", {fromBlock: tx?.blockNumber || 0, toBlock: tx?.blockNumber || 'latest'});
\n } catch (err) {
\n console.log('No encrypted event');
\n}
\nlet encBackupEvent = '';
\n try {
\n encBackupEvent = await instance.getPastEvents("EncryptedBackupData", {fromBlock: sendTxn?.blockNumber || 0, toBlock: sendTxn?.blockNumber || 'latest'});
\n encBackupEvent = await instance.getPastEvents("EncryptedBackupData", {fromBlock: tx?.blockNumber || 0, toBlock: tx?.blockNumber || 'latest'});
\n } catch (err) {
\n console.log('No encrypted backup event');
\n}`,
Expand Down Expand Up @@ -1138,11 +1139,11 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
return {
statements: [
`\n\n// Save transaction for the constructor:
\nconst tx = { ${lines}};
\nconst constructorInput = { ${lines}};
\n if (!fs.existsSync("/app/orchestration/common/db")) {
\n fs.mkdirSync("/app/orchestration/common/db", { recursive: true });
\n }
\nfs.writeFileSync("/app/orchestration/common/db/constructorTx.json", JSON.stringify(tx, null, 4));`
\nfs.writeFileSync("/app/orchestration/common/db/constructorTx.json", JSON.stringify(constructorInput, null, 4));`
],
};
}
Expand Down Expand Up @@ -1196,6 +1197,7 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
\nconst key = config.web3.key;
\nconst signed = await web3.eth.accounts.signTransaction(txParams, key);
\nconst tx = await web3.eth.sendSignedTransaction(signed.rawTransaction);
\nconst newLeavesEvent = null;
\nconst encEvent = {};
\nconst encBackupEvent ={};
`
Expand Down
1 change: 0 additions & 1 deletion src/transformers/visitors/checks/unsupportedVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const disallowedVariableNames = new Set([
'txParams',
'tx',
'signed',
'sendTxn',
'encEvent',
'encBackupEvent',
'publicReturns',
Expand Down
Loading