From b671a1a14c014543d3f0ef52f2a81f65455c69eb Mon Sep 17 00:00:00 2001 From: sagarparker Date: Mon, 4 Mar 2024 15:21:25 +0530 Subject: [PATCH 1/2] Update for the new generator --- package-lock.json | 110 +++++++++- package.json | 3 +- .../proverControllers/proverController.ts | 207 +++++++++++------- 3 files changed, 227 insertions(+), 93 deletions(-) diff --git a/package-lock.json b/package-lock.json index 732367b..9a701a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,7 @@ "cors": "^2.8.5", "ethers": "^6.6.6", "express": "^4.18.2", - "kalypso-sdk": "1.0.17", + "kalypso-sdk": "1.0.21", "node-gzip": "^1.1.2" }, "devDependencies": { @@ -25,6 +25,7 @@ "@types/cors": "^2.8.14", "@types/express": "^4.17.17", "@types/mocha": "^10.0.1", + "@types/node-fetch": "^2.6.11", "@types/node-gzip": "^1.1.0", "dotenv": "^16.3.1", "ts-node": "^10.9.1", @@ -1986,6 +1987,16 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.0.tgz", "integrity": "sha512-najjVq5KN2vsH2U/xyh2opaSEz6cZMR2SetLIlxlj08nOcmPOemJmUK2o4kUzfLqfrWE0PIrNeE16XhYDd3nqg==" }, + "node_modules/@types/node-fetch": { + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.11.tgz", + "integrity": "sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==", + "dev": true, + "dependencies": { + "@types/node": "*", + "form-data": "^4.0.0" + } + }, "node_modules/@types/node-gzip": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@types/node-gzip/-/node-gzip-1.1.0.tgz", @@ -2241,6 +2252,12 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -2675,6 +2692,18 @@ "color-support": "bin.js" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/command-exists": { "version": "1.2.9", "resolved": "https://registry.npmjs.org/command-exists/-/command-exists-1.2.9.tgz", @@ -2834,6 +2863,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/delegates": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", @@ -3351,6 +3389,20 @@ } } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dev": true, + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -3972,15 +4024,16 @@ } }, "node_modules/kalypso-sdk": { - "version": "1.0.17", - "resolved": "https://registry.npmjs.org/kalypso-sdk/-/kalypso-sdk-1.0.17.tgz", - "integrity": "sha512-IkyZd4Zg7OOr3BAtClVlkKWkSRIupp0/ntk1aF2y71WjlE/zbvS/uAB3ZOwpvEHbRX6ZGyxJMdjRGgL0MTQhbA==", + "version": "1.0.21", + "resolved": "https://registry.npmjs.org/kalypso-sdk/-/kalypso-sdk-1.0.21.tgz", + "integrity": "sha512-OsLorw6/QzYIWTxsE7nFUwfLzpL+KL9d4wW+RenFBVG4pu76b1Bs3abvJuTbxHXLjRlzZOpwdG96htSS0Db6gg==", "dependencies": { "@nomicfoundation/hardhat-ethers": "^3.0.4", "@types/node": "^20.4.5", "bignumber.js": "^9.1.1", "eciesjs": "^0.4.4", "ethers": "^6.6.6", + "node-fetch": "^2.7.0", "pako": "^2.1.0" } }, @@ -7229,6 +7282,16 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.0.tgz", "integrity": "sha512-najjVq5KN2vsH2U/xyh2opaSEz6cZMR2SetLIlxlj08nOcmPOemJmUK2o4kUzfLqfrWE0PIrNeE16XhYDd3nqg==" }, + "@types/node-fetch": { + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.11.tgz", + "integrity": "sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==", + "dev": true, + "requires": { + "@types/node": "*", + "form-data": "^4.0.0" + } + }, "@types/node-gzip": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@types/node-gzip/-/node-gzip-1.1.0.tgz", @@ -7439,6 +7502,12 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true + }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -7781,6 +7850,15 @@ "resolved": "https://registry.npmjs.org/color-support/-/color-support-1.1.3.tgz", "integrity": "sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==" }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "requires": { + "delayed-stream": "~1.0.0" + } + }, "command-exists": { "version": "1.2.9", "resolved": "https://registry.npmjs.org/command-exists/-/command-exists-1.2.9.tgz", @@ -7896,6 +7974,12 @@ "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", "peer": true }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true + }, "delegates": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", @@ -8307,6 +8391,17 @@ "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", "peer": true }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dev": true, + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -8746,15 +8841,16 @@ } }, "kalypso-sdk": { - "version": "1.0.17", - "resolved": "https://registry.npmjs.org/kalypso-sdk/-/kalypso-sdk-1.0.17.tgz", - "integrity": "sha512-IkyZd4Zg7OOr3BAtClVlkKWkSRIupp0/ntk1aF2y71WjlE/zbvS/uAB3ZOwpvEHbRX6ZGyxJMdjRGgL0MTQhbA==", + "version": "1.0.21", + "resolved": "https://registry.npmjs.org/kalypso-sdk/-/kalypso-sdk-1.0.21.tgz", + "integrity": "sha512-OsLorw6/QzYIWTxsE7nFUwfLzpL+KL9d4wW+RenFBVG4pu76b1Bs3abvJuTbxHXLjRlzZOpwdG96htSS0Db6gg==", "requires": { "@nomicfoundation/hardhat-ethers": "^3.0.4", "@types/node": "^20.4.5", "bignumber.js": "^9.1.1", "eciesjs": "^0.4.4", "ethers": "^6.6.6", + "node-fetch": "^2.7.0", "pako": "^2.1.0" } }, diff --git a/package.json b/package.json index c51d2b4..b7fb610 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@types/cors": "^2.8.14", "@types/express": "^4.17.17", "@types/mocha": "^10.0.1", + "@types/node-fetch": "^2.6.11", "@types/node-gzip": "^1.1.0", "dotenv": "^16.3.1", "ts-node": "^10.9.1", @@ -29,7 +30,7 @@ "cors": "^2.8.5", "ethers": "^6.6.6", "express": "^4.18.2", - "kalypso-sdk": "1.0.17", + "kalypso-sdk": "1.0.21", "node-gzip": "^1.1.2" } } diff --git a/src/controllers/proverControllers/proverController.ts b/src/controllers/proverControllers/proverController.ts index cc4942f..b41c8fc 100644 --- a/src/controllers/proverControllers/proverController.ts +++ b/src/controllers/proverControllers/proverController.ts @@ -3,30 +3,48 @@ import { ethers } from "ethers"; import BigNumber from "bignumber.js"; import dotenv from "dotenv"; +type Enclave = { + url: string; + utilityUrl: string; + apikey?: string; +} + type KalspsoConfig = { - payment_token:string, - staking_token: string, - generator_registry: string, - attestation_verifier: string, - entity_registry: string, - proof_market_place: string, - transfer_verifier_wrapper: string, - zkb_verifier_wrapper: string, - priority_list: string, - input_and_proof_format: string + payment_token: string; + staking_token: string; + generator_registry: string; + attestation_verifier: string; + entity_registry: string; + proof_market_place: string; + generatorEnclave?: Enclave; + matchingEngineEnclave?: Enclave; + ivsEnclave?: Enclave; + checkInputUrl: string; } const kalypsoConfig: KalspsoConfig = { - "payment_token": "0xDa25fF5adCa83e38CAA78ACC03046C59315457f4", - "staking_token": "0xb2d1265c3B638958Fd91EefF8267b3dF0b23fF7b", - "generator_registry": "0xaDF6a4858f22C7fDfc56885E35A9D12E67553d96", - "attestation_verifier": "0x398d55904A8fFf1cEb2b639cE854fc58c0305515", - "entity_registry": "0x6CEa4be917D3f58ea19358044A43fE7195c9c047", - "proof_market_place": "0x5cc413E6eC01Cf923DcBb4E98010DF53686Dd756", - "transfer_verifier_wrapper": "0xD7996353Bb1312F160f47e2C85174A83D3Ef7Ec6", - "zkb_verifier_wrapper": "0x4c97BEa5F37B21b3BEa5E6d0316352D4b8eBA20e", - "priority_list": "0x83C7F3bd2C2313b6FBA7474134461676c0fB32cd", - "input_and_proof_format": "0xd34748D0614d9bfC7e9011FeEA3AF33651dad066" + "payment_token": "0x01d84D33CC8636F83d2bb771e184cE57d8356863", + "staking_token": "0xdb69299dDE4A00c99b885D9f8748B2AeD1Fe4Ed4", + "generator_registry": "0x6FD6ED78f6D8a04bC9DF9480B4CD4A7E37e885a4", + "attestation_verifier": "0x3aB3487269206d5f6a10725d4e477BaA3611adcA", + "entity_registry": "0xc54F0B48727902472e077Ae56b6321c4f2d77aD6", + "proof_market_place": "0x9db3AF484D362765064854f73d90312e662dB65a", + "generatorEnclave": { + "url": "http://43.205.85.160:5000", + "utilityUrl": "http://43.205.85.160:1500", + "apikey": "ea35cd66-a0af-418b-9def-f2cb036f4397 " + }, + "ivsEnclave": { + "url": "http://43.205.177.43:5000", + "utilityUrl": "http://43.205.177.43:1500", + "apikey": "949bc5d2-d2ce-4076-ad42-181f91ff8b79" + }, + "matchingEngineEnclave": { + "url": "http://13.201.131.193:5000", + "utilityUrl": "http://13.201.131.193:1500", + "apikey": "8b0936cb-036e-4654-94e5-17cb717c58fc" + }, + "checkInputUrl": "http://43.205.177.43:3030" } dotenv.config(); @@ -62,83 +80,102 @@ const createAskAndGetProof = async (createAskAndGetProofParams:createAskAndGetPr console.log("using address", await wallet.getAddress()); + const reward = new BigNumber(10).pow(18).multipliedBy(45).div(10).toFixed(0); + let abiCoder = new ethers.AbiCoder(); let inputBytes = abiCoder.encode(["uint256[5]"], [[input.root, input.nullifier, input.out_commit, input.delta, input.memo]]); - - const reward = "1000000000000000000"; - + const kalypso = new KalypsoSdk(wallet, kalypsoConfig); - + const secretString = JSON.stringify(secret); - + const latestBlock = await provider.getBlockNumber(); - - const marketId = "0x07b7d625c70be57115ab18fc435ed0253425671cb91bd6547b7defbc75f52082"; + + const marketId = "3"; const assignmentDeadline = new BigNumber(latestBlock).plus(10000000000); console.log({ latestBlock, assignmentDeadline: assignmentDeadline.toFixed(0) }); const proofGenerationTimeInBlocks = new BigNumber(10000000000); - - // Create ASK request - const askRequest = await kalypso - .MarketPlace() - .createAsk( - marketId, - inputBytes, - reward, - assignmentDeadline.toFixed(0), - proofGenerationTimeInBlocks.toFixed(0), - await wallet.getAddress(), - Buffer.from(secretString) - ); - await askRequest.wait(); - console.log("Ask Request Hash: ", askRequest.hash); - - let receipt = await provider.getTransactionReceipt(askRequest.hash); - let askId = await kalypso.MarketPlace().getAskId(receipt!); - console.log("Ask ID :",askId); + const ivsCheckEciesCheckingKey = await kalypso.MarketPlace().IvsEnclaveConnector().fetchInputVerifierPublicKeys(); - if(askId){ - return await new Promise(resolve => { - console.log("\nTrying to fetch proof...\n") - let intervalId = setInterval(async ()=>{ - let data = await kalypso.MarketPlace().getProofByAskId(askId.toString()); - if(data?.proof_generated){ - console.log(data.message); - let abiCoder = new ethers.AbiCoder(); - let proof = abiCoder.decode( - ["uint256[8]"], - data.proof, - ); - - let formated_proof = { - "a":[ - proof[0][0].toString(), - proof[0][1].toString(), - ], - "b":[ - [ - proof[0][2].toString(), - proof[0][3].toString(), - ], - [ - proof[0][4].toString(), - proof[0][5].toString(), - ] + const isGoodRequest = await kalypso.MarketPlace().checkInputsAndEncryptedSecretWithIvs( + marketId, + inputBytes, + Buffer.from(secretString), + kalypso.MarketPlace().IvsEnclaveConnector().checkInputUrl(), + ivsCheckEciesCheckingKey.data.public_key, // don't use ecies public key here + ivsCheckEciesCheckingKey.data.ecies_public_key + ); + + if (!isGoodRequest) { + throw new Error("Better not create a request, if it is not provable to prevent loss of funds"); + } + + console.log({ isGoodRequest }); + + // Create ASK request + const askRequest = await kalypso.MarketPlace().createAsk( + marketId, + inputBytes, + reward, + assignmentDeadline.toFixed(0), + proofGenerationTimeInBlocks.toFixed(0), + await wallet.getAddress(), + 0, // TODO: keep this 0 for now + Buffer.from(secretString) + ); + const tx = await askRequest.wait(); + const block_number = tx?.blockNumber; + console.log("Ask Request Hash: ", askRequest.hash, " at block", block_number); + + let receipt = await provider.getTransactionReceipt(askRequest.hash); + + let askId = await kalypso.MarketPlace().getAskId(receipt!); + console.log("Ask ID :",askId); + + + + if(askId){ + return await new Promise(resolve => { + console.log("\nTrying to fetch proof...\n") + let intervalId = setInterval(async ()=>{ + let data = await kalypso.MarketPlace().getProofByAskId(askId.toString(),block_number!); + if(data?.proof_generated){ + console.log(data.message); + let abiCoder = new ethers.AbiCoder(); + let proof = abiCoder.decode( + ["uint256[8]"], + data.proof, + ); + + let formated_proof = { + "a":[ + proof[0][0].toString(), + proof[0][1].toString(), + ], + "b":[ + [ + proof[0][2].toString(), + proof[0][3].toString(), ], - "c":[ - proof[0][6].toString(), - proof[0][7].toString(), + [ + proof[0][4].toString(), + proof[0][5].toString(), ] - } - resolve(formated_proof); - clearInterval(intervalId); - } else { - console.log(`Proof not submitted yet for askId : ${askId}.`); - } - },5000); - }); - } + ], + "c":[ + proof[0][6].toString(), + proof[0][7].toString(), + ] + } + resolve(formated_proof); + clearInterval(intervalId); + } else { + console.log(`Proof not submitted yet for askId : ${askId}.`); + } + },5000); + }); + } } catch (err) { console.log(err); From 5cf3354cbc5ff562bd4d3d72ba29722dab2f32be Mon Sep 17 00:00:00 2001 From: sagarparker Date: Tue, 5 Mar 2024 11:36:01 +0530 Subject: [PATCH 2/2] Update for the new ASK request --- package.json | 2 +- src/controllers/proverControllers/proverController.ts | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b7fb610..a59b571 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "cors": "^2.8.5", "ethers": "^6.6.6", "express": "^4.18.2", - "kalypso-sdk": "1.0.21", + "kalypso-sdk": "1.0.23", "node-gzip": "^1.1.2" } } diff --git a/src/controllers/proverControllers/proverController.ts b/src/controllers/proverControllers/proverController.ts index b41c8fc..637904e 100644 --- a/src/controllers/proverControllers/proverController.ts +++ b/src/controllers/proverControllers/proverController.ts @@ -91,7 +91,7 @@ const createAskAndGetProof = async (createAskAndGetProofParams:createAskAndGetPr const latestBlock = await provider.getBlockNumber(); - const marketId = "3"; + const marketId = "4"; const assignmentDeadline = new BigNumber(latestBlock).plus(10000000000); console.log({ latestBlock, assignmentDeadline: assignmentDeadline.toFixed(0) }); const proofGenerationTimeInBlocks = new BigNumber(10000000000); @@ -129,12 +129,10 @@ const createAskAndGetProof = async (createAskAndGetProofParams:createAskAndGetPr console.log("Ask Request Hash: ", askRequest.hash, " at block", block_number); let receipt = await provider.getTransactionReceipt(askRequest.hash); - let askId = await kalypso.MarketPlace().getAskId(receipt!); console.log("Ask ID :",askId); - if(askId){ return await new Promise(resolve => { console.log("\nTrying to fetch proof...\n") @@ -207,4 +205,4 @@ export const proveTransaction = async(req:any,res:any) => { } catch (error) { console.log(error) } -} \ No newline at end of file +}