@maci-protocol/coordinator 0.0.0-ci.5209133
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/LICENSE +22 -0
- package/README.md +42 -0
- package/build/hardhat.config.cjs +31 -0
- package/build/hardhat.config.cjs.map +1 -0
- package/build/hardhat.config.d.cts +24 -0
- package/build/hardhat.config.d.cts.map +1 -0
- package/build/scripts/generateKeypair.d.ts +2 -0
- package/build/scripts/generateKeypair.d.ts.map +1 -0
- package/build/scripts/generateKeypair.js +25 -0
- package/build/scripts/generateKeypair.js.map +1 -0
- package/build/scripts/generateMaciKeyPair.d.ts +2 -0
- package/build/scripts/generateMaciKeyPair.d.ts.map +1 -0
- package/build/scripts/generateMaciKeyPair.js +3 -0
- package/build/scripts/generateMaciKeyPair.js.map +1 -0
- package/build/scripts/getAuthHeader.d.ts +2 -0
- package/build/scripts/getAuthHeader.d.ts.map +1 -0
- package/build/scripts/getAuthHeader.js +31 -0
- package/build/scripts/getAuthHeader.js.map +1 -0
- package/build/tests/constants.d.ts +8 -0
- package/build/tests/constants.d.ts.map +1 -0
- package/build/tests/constants.js +10 -0
- package/build/tests/constants.js.map +1 -0
- package/build/tests/e2e.aa.test.d.ts +2 -0
- package/build/tests/e2e.aa.test.d.ts.map +1 -0
- package/build/tests/e2e.aa.test.js +103 -0
- package/build/tests/e2e.aa.test.js.map +1 -0
- package/build/tests/e2e.deploy.test.d.ts +2 -0
- package/build/tests/e2e.deploy.test.d.ts.map +1 -0
- package/build/tests/e2e.deploy.test.js +234 -0
- package/build/tests/e2e.deploy.test.js.map +1 -0
- package/build/tests/utils.d.ts +14 -0
- package/build/tests/utils.d.ts.map +1 -0
- package/build/tests/utils.js +44 -0
- package/build/tests/utils.js.map +1 -0
- package/build/ts/app.module.d.ts +3 -0
- package/build/ts/app.module.d.ts.map +1 -0
- package/build/ts/app.module.js +36 -0
- package/build/ts/app.module.js.map +1 -0
- package/build/ts/auth/AccountSignatureGuard.service.d.ts +44 -0
- package/build/ts/auth/AccountSignatureGuard.service.d.ts.map +1 -0
- package/build/ts/auth/AccountSignatureGuard.service.js +96 -0
- package/build/ts/auth/AccountSignatureGuard.service.js.map +1 -0
- package/build/ts/auth/__tests__/AccountSignatureGuard.test.d.ts +2 -0
- package/build/ts/auth/__tests__/AccountSignatureGuard.test.d.ts.map +1 -0
- package/build/ts/auth/__tests__/AccountSignatureGuard.test.js +113 -0
- package/build/ts/auth/__tests__/AccountSignatureGuard.test.js.map +1 -0
- package/build/ts/common/__tests__/common.test.d.ts +2 -0
- package/build/ts/common/__tests__/common.test.d.ts.map +1 -0
- package/build/ts/common/__tests__/common.test.js +79 -0
- package/build/ts/common/__tests__/common.test.js.map +1 -0
- package/build/ts/common/accountAbstraction.d.ts +55 -0
- package/build/ts/common/accountAbstraction.d.ts.map +1 -0
- package/build/ts/common/accountAbstraction.js +110 -0
- package/build/ts/common/accountAbstraction.js.map +1 -0
- package/build/ts/common/errors.d.ts +33 -0
- package/build/ts/common/errors.d.ts.map +1 -0
- package/build/ts/common/errors.js +34 -0
- package/build/ts/common/errors.js.map +1 -0
- package/build/ts/common/index.d.ts +4 -0
- package/build/ts/common/index.d.ts.map +1 -0
- package/build/ts/common/index.js +4 -0
- package/build/ts/common/index.js.map +1 -0
- package/build/ts/common/networks.d.ts +29 -0
- package/build/ts/common/networks.d.ts.map +1 -0
- package/build/ts/common/networks.js +70 -0
- package/build/ts/common/networks.js.map +1 -0
- package/build/ts/common/types.d.ts +9 -0
- package/build/ts/common/types.d.ts.map +1 -0
- package/build/ts/common/types.js +2 -0
- package/build/ts/common/types.js.map +1 -0
- package/build/ts/crypto/__tests__/crypto.service.test.d.ts +2 -0
- package/build/ts/crypto/__tests__/crypto.service.test.d.ts.map +1 -0
- package/build/ts/crypto/__tests__/crypto.service.test.js +26 -0
- package/build/ts/crypto/__tests__/crypto.service.test.js.map +1 -0
- package/build/ts/crypto/crypto.module.d.ts +3 -0
- package/build/ts/crypto/crypto.module.d.ts.map +1 -0
- package/build/ts/crypto/crypto.module.js +18 -0
- package/build/ts/crypto/crypto.module.js.map +1 -0
- package/build/ts/crypto/crypto.service.d.ts +31 -0
- package/build/ts/crypto/crypto.service.d.ts.map +1 -0
- package/build/ts/crypto/crypto.service.js +68 -0
- package/build/ts/crypto/crypto.service.js.map +1 -0
- package/build/ts/deployer/__tests__/deployer.controller.test.d.ts +2 -0
- package/build/ts/deployer/__tests__/deployer.controller.test.d.ts.map +1 -0
- package/build/ts/deployer/__tests__/deployer.controller.test.js +84 -0
- package/build/ts/deployer/__tests__/deployer.controller.test.js.map +1 -0
- package/build/ts/deployer/__tests__/deployer.service.test.d.ts +2 -0
- package/build/ts/deployer/__tests__/deployer.service.test.d.ts.map +1 -0
- package/build/ts/deployer/__tests__/deployer.service.test.js +425 -0
- package/build/ts/deployer/__tests__/deployer.service.test.js.map +1 -0
- package/build/ts/deployer/__tests__/utils.d.ts +36 -0
- package/build/ts/deployer/__tests__/utils.d.ts.map +1 -0
- package/build/ts/deployer/__tests__/utils.js +96 -0
- package/build/ts/deployer/__tests__/utils.js.map +1 -0
- package/build/ts/deployer/constants.d.ts +13 -0
- package/build/ts/deployer/constants.d.ts.map +1 -0
- package/build/ts/deployer/constants.js +13 -0
- package/build/ts/deployer/constants.js.map +1 -0
- package/build/ts/deployer/deployer.controller.d.ts +34 -0
- package/build/ts/deployer/deployer.controller.d.ts.map +1 -0
- package/build/ts/deployer/deployer.controller.js +89 -0
- package/build/ts/deployer/deployer.controller.js.map +1 -0
- package/build/ts/deployer/deployer.module.d.ts +3 -0
- package/build/ts/deployer/deployer.module.d.ts.map +1 -0
- package/build/ts/deployer/deployer.module.js +23 -0
- package/build/ts/deployer/deployer.module.js.map +1 -0
- package/build/ts/deployer/deployer.service.d.ts +112 -0
- package/build/ts/deployer/deployer.service.d.ts.map +1 -0
- package/build/ts/deployer/deployer.service.js +628 -0
- package/build/ts/deployer/deployer.service.js.map +1 -0
- package/build/ts/deployer/dto.d.ts +46 -0
- package/build/ts/deployer/dto.d.ts.map +1 -0
- package/build/ts/deployer/dto.js +117 -0
- package/build/ts/deployer/dto.js.map +1 -0
- package/build/ts/deployer/types.d.ts +306 -0
- package/build/ts/deployer/types.d.ts.map +1 -0
- package/build/ts/deployer/types.js +2 -0
- package/build/ts/deployer/types.js.map +1 -0
- package/build/ts/deployer/utils.d.ts +8 -0
- package/build/ts/deployer/utils.d.ts.map +1 -0
- package/build/ts/deployer/utils.js +9 -0
- package/build/ts/deployer/utils.js.map +1 -0
- package/build/ts/file/__tests__/file.service.test.d.ts +2 -0
- package/build/ts/file/__tests__/file.service.test.d.ts.map +1 -0
- package/build/ts/file/__tests__/file.service.test.js +76 -0
- package/build/ts/file/__tests__/file.service.test.js.map +1 -0
- package/build/ts/file/file.module.d.ts +3 -0
- package/build/ts/file/file.module.d.ts.map +1 -0
- package/build/ts/file/file.module.js +18 -0
- package/build/ts/file/file.module.js.map +1 -0
- package/build/ts/file/file.service.d.ts +62 -0
- package/build/ts/file/file.service.d.ts.map +1 -0
- package/build/ts/file/file.service.js +115 -0
- package/build/ts/file/file.service.js.map +1 -0
- package/build/ts/file/types.d.ts +36 -0
- package/build/ts/file/types.d.ts.map +1 -0
- package/build/ts/file/types.js +2 -0
- package/build/ts/file/types.js.map +1 -0
- package/build/ts/jest/setup.d.ts +8 -0
- package/build/ts/jest/setup.d.ts.map +1 -0
- package/build/ts/jest/setup.js +2 -0
- package/build/ts/jest/setup.js.map +1 -0
- package/build/ts/jest/transform.d.ts +8 -0
- package/build/ts/jest/transform.d.ts.map +1 -0
- package/build/ts/jest/transform.js +10 -0
- package/build/ts/jest/transform.js.map +1 -0
- package/build/ts/main.d.ts +2 -0
- package/build/ts/main.d.ts.map +1 -0
- package/build/ts/main.js +46 -0
- package/build/ts/main.js.map +1 -0
- package/build/ts/proof/__tests__/proof.controller.test.d.ts +2 -0
- package/build/ts/proof/__tests__/proof.controller.test.d.ts.map +1 -0
- package/build/ts/proof/__tests__/proof.controller.test.js +89 -0
- package/build/ts/proof/__tests__/proof.controller.test.js.map +1 -0
- package/build/ts/proof/__tests__/proof.gateway.test.d.ts +2 -0
- package/build/ts/proof/__tests__/proof.gateway.test.d.ts.map +1 -0
- package/build/ts/proof/__tests__/proof.gateway.test.js +61 -0
- package/build/ts/proof/__tests__/proof.gateway.test.js.map +1 -0
- package/build/ts/proof/__tests__/proof.service.test.d.ts +2 -0
- package/build/ts/proof/__tests__/proof.service.test.d.ts.map +1 -0
- package/build/ts/proof/__tests__/proof.service.test.js +128 -0
- package/build/ts/proof/__tests__/proof.service.test.js.map +1 -0
- package/build/ts/proof/dto.d.ts +73 -0
- package/build/ts/proof/dto.d.ts.map +1 -0
- package/build/ts/proof/dto.js +255 -0
- package/build/ts/proof/dto.js.map +1 -0
- package/build/ts/proof/proof.controller.d.ts +46 -0
- package/build/ts/proof/proof.controller.d.ts.map +1 -0
- package/build/ts/proof/proof.controller.js +135 -0
- package/build/ts/proof/proof.controller.js.map +1 -0
- package/build/ts/proof/proof.gateway.d.ts +35 -0
- package/build/ts/proof/proof.gateway.d.ts.map +1 -0
- package/build/ts/proof/proof.gateway.js +93 -0
- package/build/ts/proof/proof.gateway.js.map +1 -0
- package/build/ts/proof/proof.module.d.ts +3 -0
- package/build/ts/proof/proof.module.d.ts.map +1 -0
- package/build/ts/proof/proof.module.js +24 -0
- package/build/ts/proof/proof.module.js.map +1 -0
- package/build/ts/proof/proof.service.d.ts +54 -0
- package/build/ts/proof/proof.service.d.ts.map +1 -0
- package/build/ts/proof/proof.service.js +246 -0
- package/build/ts/proof/proof.service.js.map +1 -0
- package/build/ts/proof/types.d.ts +140 -0
- package/build/ts/proof/types.d.ts.map +1 -0
- package/build/ts/proof/types.js +11 -0
- package/build/ts/proof/types.js.map +1 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.controller.test.d.ts +2 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.controller.test.d.ts.map +1 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.controller.test.js +44 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.controller.test.js.map +1 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.service.test.d.ts +2 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.service.test.d.ts.map +1 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.service.test.js +61 -0
- package/build/ts/sessionKeys/__tests__/sessionKeys.service.test.js.map +1 -0
- package/build/ts/sessionKeys/__tests__/utils.d.ts +25 -0
- package/build/ts/sessionKeys/__tests__/utils.d.ts.map +1 -0
- package/build/ts/sessionKeys/__tests__/utils.js +64 -0
- package/build/ts/sessionKeys/__tests__/utils.js.map +1 -0
- package/build/ts/sessionKeys/dto.d.ts +11 -0
- package/build/ts/sessionKeys/dto.d.ts.map +1 -0
- package/build/ts/sessionKeys/dto.js +29 -0
- package/build/ts/sessionKeys/dto.js.map +1 -0
- package/build/ts/sessionKeys/sessionKeys.controller.d.ts +26 -0
- package/build/ts/sessionKeys/sessionKeys.controller.d.ts.map +1 -0
- package/build/ts/sessionKeys/sessionKeys.controller.js +74 -0
- package/build/ts/sessionKeys/sessionKeys.controller.js.map +1 -0
- package/build/ts/sessionKeys/sessionKeys.module.d.ts +3 -0
- package/build/ts/sessionKeys/sessionKeys.module.d.ts.map +1 -0
- package/build/ts/sessionKeys/sessionKeys.module.js +23 -0
- package/build/ts/sessionKeys/sessionKeys.module.js.map +1 -0
- package/build/ts/sessionKeys/sessionKeys.service.d.ts +50 -0
- package/build/ts/sessionKeys/sessionKeys.service.d.ts.map +1 -0
- package/build/ts/sessionKeys/sessionKeys.service.js +104 -0
- package/build/ts/sessionKeys/sessionKeys.service.js.map +1 -0
- package/build/ts/sessionKeys/types.d.ts +11 -0
- package/build/ts/sessionKeys/types.d.ts.map +1 -0
- package/build/ts/sessionKeys/types.js +2 -0
- package/build/ts/sessionKeys/types.js.map +1 -0
- package/build/ts/subgraph/__tests__/subgraph.controller.test.d.ts +2 -0
- package/build/ts/subgraph/__tests__/subgraph.controller.test.d.ts.map +1 -0
- package/build/ts/subgraph/__tests__/subgraph.controller.test.js +50 -0
- package/build/ts/subgraph/__tests__/subgraph.controller.test.js.map +1 -0
- package/build/ts/subgraph/__tests__/subgraph.gateway.test.d.ts +2 -0
- package/build/ts/subgraph/__tests__/subgraph.gateway.test.d.ts.map +1 -0
- package/build/ts/subgraph/__tests__/subgraph.gateway.test.js +83 -0
- package/build/ts/subgraph/__tests__/subgraph.gateway.test.js.map +1 -0
- package/build/ts/subgraph/__tests__/subgraph.service.test.d.ts +2 -0
- package/build/ts/subgraph/__tests__/subgraph.service.test.d.ts.map +1 -0
- package/build/ts/subgraph/__tests__/subgraph.service.test.js +58 -0
- package/build/ts/subgraph/__tests__/subgraph.service.test.js.map +1 -0
- package/build/ts/subgraph/dto.d.ts +27 -0
- package/build/ts/subgraph/dto.d.ts.map +1 -0
- package/build/ts/subgraph/dto.js +83 -0
- package/build/ts/subgraph/dto.js.map +1 -0
- package/build/ts/subgraph/subgraph.controller.d.ts +24 -0
- package/build/ts/subgraph/subgraph.controller.d.ts.map +1 -0
- package/build/ts/subgraph/subgraph.controller.js +66 -0
- package/build/ts/subgraph/subgraph.controller.js.map +1 -0
- package/build/ts/subgraph/subgraph.gateway.d.ts +35 -0
- package/build/ts/subgraph/subgraph.gateway.d.ts.map +1 -0
- package/build/ts/subgraph/subgraph.gateway.js +93 -0
- package/build/ts/subgraph/subgraph.gateway.js.map +1 -0
- package/build/ts/subgraph/subgraph.module.d.ts +3 -0
- package/build/ts/subgraph/subgraph.module.d.ts.map +1 -0
- package/build/ts/subgraph/subgraph.module.js +23 -0
- package/build/ts/subgraph/subgraph.module.js.map +1 -0
- package/build/ts/subgraph/subgraph.service.d.ts +20 -0
- package/build/ts/subgraph/subgraph.service.d.ts.map +1 -0
- package/build/ts/subgraph/subgraph.service.js +106 -0
- package/build/ts/subgraph/subgraph.service.js.map +1 -0
- package/build/ts/subgraph/types.d.ts +93 -0
- package/build/ts/subgraph/types.d.ts.map +1 -0
- package/build/ts/subgraph/types.js +24 -0
- package/build/ts/subgraph/types.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +124 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// To only run this file: pnpm exec jest --testPathPattern=tests/e2e.deploy.test.ts
|
|
2
|
+
import { genRandomSalt } from "@maci-protocol/crypto";
|
|
3
|
+
import { Keypair } from "@maci-protocol/domainobjs";
|
|
4
|
+
import { joinPoll, publish, signup, sleep } from "@maci-protocol/sdk";
|
|
5
|
+
import { ValidationPipe } from "@nestjs/common";
|
|
6
|
+
import { Test } from "@nestjs/testing";
|
|
7
|
+
import dotenv from "dotenv";
|
|
8
|
+
import hardhat from "hardhat";
|
|
9
|
+
import { io } from "socket.io-client";
|
|
10
|
+
import { zeroAddress } from "viem";
|
|
11
|
+
import { AppModule } from "../ts/app.module";
|
|
12
|
+
import { ESupportedNetworks } from "../ts/common";
|
|
13
|
+
import { getPublicClient } from "../ts/common/accountAbstraction";
|
|
14
|
+
import { testMaciDeploymentConfig, testPollDeploymentConfig } from "../ts/deployer/__tests__/utils";
|
|
15
|
+
import { FileModule } from "../ts/file/file.module";
|
|
16
|
+
import { generateApproval, getKernelAccount } from "../ts/sessionKeys/__tests__/utils";
|
|
17
|
+
import { pollDuration, pollJoiningTestZkeyPath, testPollJoiningWasmPath, testPollJoiningWitnessPath, testRapidsnarkPath, zeroUint256Encoded, } from "./constants";
|
|
18
|
+
import { getAuthorizationHeader, rechargeGasIfNeeded } from "./utils";
|
|
19
|
+
dotenv.config();
|
|
20
|
+
const LOCALHOST = "http://localhost:3000";
|
|
21
|
+
const NUM_USERS = 1;
|
|
22
|
+
const voteOptions = {
|
|
23
|
+
"0": 0,
|
|
24
|
+
"1": 0,
|
|
25
|
+
};
|
|
26
|
+
describe("E2E Deployment Tests", () => {
|
|
27
|
+
let signer;
|
|
28
|
+
let encryptedHeader;
|
|
29
|
+
let coordinatorKeypair;
|
|
30
|
+
let sessionKeyAddress;
|
|
31
|
+
let approval;
|
|
32
|
+
let app;
|
|
33
|
+
let socket;
|
|
34
|
+
const publicClient = getPublicClient(ESupportedNetworks.OPTIMISM_SEPOLIA);
|
|
35
|
+
let maciAddress;
|
|
36
|
+
let pollId;
|
|
37
|
+
// set up coordinator address
|
|
38
|
+
beforeAll(async () => {
|
|
39
|
+
[signer] = await hardhat.ethers.getSigners();
|
|
40
|
+
encryptedHeader = await getAuthorizationHeader(signer);
|
|
41
|
+
coordinatorKeypair = new Keypair();
|
|
42
|
+
process.env.COORDINATOR_ADDRESSES = await signer.getAddress();
|
|
43
|
+
await rechargeGasIfNeeded(process.env.COORDINATOR_ADDRESSES, "0.007", "0.007");
|
|
44
|
+
});
|
|
45
|
+
// set up NestJS app
|
|
46
|
+
beforeAll(async () => {
|
|
47
|
+
const moduleFixture = await Test.createTestingModule({
|
|
48
|
+
imports: [AppModule, FileModule],
|
|
49
|
+
}).compile();
|
|
50
|
+
app = moduleFixture.createNestApplication();
|
|
51
|
+
app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
|
52
|
+
await app.init();
|
|
53
|
+
await app.listen(3000);
|
|
54
|
+
});
|
|
55
|
+
afterAll(async () => {
|
|
56
|
+
await app.close();
|
|
57
|
+
});
|
|
58
|
+
// set up auth header in WS connection
|
|
59
|
+
beforeEach(async () => {
|
|
60
|
+
const authorization = await getAuthorizationHeader(signer);
|
|
61
|
+
await new Promise((resolve) => {
|
|
62
|
+
app.getUrl().then((url) => {
|
|
63
|
+
socket = io(url, {
|
|
64
|
+
extraHeaders: {
|
|
65
|
+
authorization,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
socket.on("connect", () => {
|
|
69
|
+
resolve(true);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
afterEach(() => {
|
|
75
|
+
socket.disconnect();
|
|
76
|
+
});
|
|
77
|
+
// run tests
|
|
78
|
+
test("should retrieve the session key address", async () => {
|
|
79
|
+
const response = await fetch(`${LOCALHOST}/v1/session-keys/generate`, {
|
|
80
|
+
method: "GET",
|
|
81
|
+
headers: {
|
|
82
|
+
Authorization: encryptedHeader,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const body = (await response.json());
|
|
86
|
+
expect(response.status).toBe(200);
|
|
87
|
+
expect(body.sessionKeyAddress).not.toBe(zeroAddress);
|
|
88
|
+
// save them for next tests
|
|
89
|
+
sessionKeyAddress = body.sessionKeyAddress;
|
|
90
|
+
approval = await generateApproval(sessionKeyAddress);
|
|
91
|
+
const sessionKeyAccount = await getKernelAccount(sessionKeyAddress);
|
|
92
|
+
await rechargeGasIfNeeded(sessionKeyAccount.address, "0.03", "0.03");
|
|
93
|
+
});
|
|
94
|
+
test("should deploy MACI correctly", async () => {
|
|
95
|
+
const config = testMaciDeploymentConfig;
|
|
96
|
+
config.VkRegistry.args.stateTreeDepth = config.VkRegistry.args.stateTreeDepth.toString();
|
|
97
|
+
config.VkRegistry.args.intStateTreeDepth = config.VkRegistry.args.intStateTreeDepth.toString();
|
|
98
|
+
config.VkRegistry.args.voteOptionTreeDepth = config.VkRegistry.args.voteOptionTreeDepth.toString();
|
|
99
|
+
const response = await fetch(`${LOCALHOST}/v1/deploy/maci`, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: {
|
|
102
|
+
Authorization: encryptedHeader,
|
|
103
|
+
"Content-Type": "application/json",
|
|
104
|
+
},
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
approval,
|
|
107
|
+
sessionKeyAddress,
|
|
108
|
+
chain: ESupportedNetworks.OPTIMISM_SEPOLIA,
|
|
109
|
+
config,
|
|
110
|
+
}),
|
|
111
|
+
});
|
|
112
|
+
const body = (await response.json());
|
|
113
|
+
expect(response.status).toBe(201);
|
|
114
|
+
expect(body.address).not.toBe(zeroAddress);
|
|
115
|
+
// save them for next tests
|
|
116
|
+
maciAddress = body.address;
|
|
117
|
+
});
|
|
118
|
+
test("should deploy a poll correctly", async () => {
|
|
119
|
+
const config = testPollDeploymentConfig;
|
|
120
|
+
config.coordinatorPubkey = coordinatorKeypair.pubKey.serialize();
|
|
121
|
+
config.voteOptions = config.voteOptions.toString();
|
|
122
|
+
const startDate = Math.floor(Date.now() / 1000) + 10;
|
|
123
|
+
config.startDate = startDate;
|
|
124
|
+
config.endDate = startDate + pollDuration;
|
|
125
|
+
const response = await fetch(`${LOCALHOST}/v1/deploy/poll`, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
headers: {
|
|
128
|
+
Authorization: encryptedHeader,
|
|
129
|
+
"Content-Type": "application/json",
|
|
130
|
+
},
|
|
131
|
+
body: JSON.stringify({
|
|
132
|
+
approval,
|
|
133
|
+
sessionKeyAddress,
|
|
134
|
+
chain: ESupportedNetworks.OPTIMISM_SEPOLIA,
|
|
135
|
+
config,
|
|
136
|
+
}),
|
|
137
|
+
});
|
|
138
|
+
const body = (await response.json());
|
|
139
|
+
expect(response.status).toBe(201);
|
|
140
|
+
expect(body.pollId).toBeDefined();
|
|
141
|
+
// save them for next tests
|
|
142
|
+
pollId = BigInt(body.pollId);
|
|
143
|
+
});
|
|
144
|
+
test("should deploy a subgraph correctly", async () => {
|
|
145
|
+
const blockNumber = Number(await publicClient.getBlockNumber());
|
|
146
|
+
const response = await fetch(`${LOCALHOST}/v1/subgraph/deploy`, {
|
|
147
|
+
method: "POST",
|
|
148
|
+
headers: {
|
|
149
|
+
Authorization: encryptedHeader,
|
|
150
|
+
"Content-Type": "application/json",
|
|
151
|
+
},
|
|
152
|
+
body: JSON.stringify({
|
|
153
|
+
maciContractAddress: maciAddress,
|
|
154
|
+
startBlock: blockNumber,
|
|
155
|
+
network: ESupportedNetworks.OPTIMISM_SEPOLIA,
|
|
156
|
+
name: process.env.SUBGRAPH_NAME,
|
|
157
|
+
tag: `v0.0.${blockNumber}`, // different versions per test using block number
|
|
158
|
+
}),
|
|
159
|
+
});
|
|
160
|
+
const body = (await response.json());
|
|
161
|
+
const { url } = body;
|
|
162
|
+
const regex = /^https:\/\/api\.studio\.thegraph\.com\/query\/\d+\/maci-subgraph\/v0\.0\.\d+$/;
|
|
163
|
+
expect(response.status).toBe(201);
|
|
164
|
+
expect(body.url).toBeDefined();
|
|
165
|
+
expect(regex.test(url)).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
test("should allow voting on a poll", async () => {
|
|
168
|
+
for (let i = 0; i < NUM_USERS; i += 1) {
|
|
169
|
+
const keypairUser = new Keypair();
|
|
170
|
+
const pubkeyUser = keypairUser.pubKey.serialize();
|
|
171
|
+
const privkeyUser = keypairUser.privKey.serialize();
|
|
172
|
+
const vote = i % 2;
|
|
173
|
+
voteOptions[String(vote)] += 1;
|
|
174
|
+
// user signs up to MACI
|
|
175
|
+
// eslint-disable-next-line no-await-in-loop
|
|
176
|
+
await signup({
|
|
177
|
+
maciAddress,
|
|
178
|
+
maciPubKey: pubkeyUser,
|
|
179
|
+
sgData: zeroUint256Encoded,
|
|
180
|
+
signer,
|
|
181
|
+
});
|
|
182
|
+
// user joins the poll
|
|
183
|
+
// eslint-disable-next-line no-await-in-loop
|
|
184
|
+
await joinPoll({
|
|
185
|
+
maciAddress,
|
|
186
|
+
privateKey: privkeyUser,
|
|
187
|
+
stateIndex: 1n,
|
|
188
|
+
pollId: BigInt(pollId),
|
|
189
|
+
pollJoiningZkey: pollJoiningTestZkeyPath,
|
|
190
|
+
useWasm: true,
|
|
191
|
+
pollWasm: testPollJoiningWasmPath,
|
|
192
|
+
pollWitgen: testPollJoiningWitnessPath,
|
|
193
|
+
rapidsnark: testRapidsnarkPath,
|
|
194
|
+
sgDataArg: zeroUint256Encoded,
|
|
195
|
+
ivcpDataArg: zeroUint256Encoded,
|
|
196
|
+
signer,
|
|
197
|
+
});
|
|
198
|
+
// user publishes a vote
|
|
199
|
+
// eslint-disable-next-line no-await-in-loop
|
|
200
|
+
const publishData = await publish({
|
|
201
|
+
pubkey: pubkeyUser,
|
|
202
|
+
stateIndex: 1n,
|
|
203
|
+
voteOptionIndex: BigInt(vote),
|
|
204
|
+
nonce: 1n,
|
|
205
|
+
pollId: BigInt(pollId),
|
|
206
|
+
newVoteWeight: 1n,
|
|
207
|
+
maciAddress,
|
|
208
|
+
salt: genRandomSalt(),
|
|
209
|
+
privateKey: privkeyUser,
|
|
210
|
+
signer,
|
|
211
|
+
});
|
|
212
|
+
expect(publishData.hash).not.toBe(zeroAddress);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
test("should merge correctly", async () => {
|
|
216
|
+
await sleep(10000);
|
|
217
|
+
const response = await fetch(`${LOCALHOST}/v1/proof/merge`, {
|
|
218
|
+
method: "POST",
|
|
219
|
+
headers: {
|
|
220
|
+
Authorization: encryptedHeader,
|
|
221
|
+
"Content-Type": "application/json",
|
|
222
|
+
},
|
|
223
|
+
body: JSON.stringify({
|
|
224
|
+
maciContractAddress: maciAddress,
|
|
225
|
+
pollId: Number(pollId),
|
|
226
|
+
approval,
|
|
227
|
+
sessionKeyAddress,
|
|
228
|
+
chain: ESupportedNetworks.OPTIMISM_SEPOLIA,
|
|
229
|
+
}),
|
|
230
|
+
});
|
|
231
|
+
expect(response.status).toBe(201);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
//# sourceMappingURL=e2e.deploy.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"e2e.deploy.test.js","sourceRoot":"","sources":["../../tests/e2e.deploy.test.ts"],"names":[],"mappings":"AAAA,mFAAmF;AAEnF,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,cAAc,EAAyB,MAAM,gBAAgB,CAAC;AACvE,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAi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,EAAE,WAAW;gBAChC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC;gBACtB,QAAQ;gBACR,iBAAiB;gBACjB,KAAK,EAAE,kBAAkB,CAAC,gBAAgB;aAC7B,CAAC;SACjB,CAAC,CAAC;QAEH,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Signer } from "ethers";
|
|
2
|
+
import { Hex } from "viem";
|
|
3
|
+
/**
|
|
4
|
+
* Sign a message with a wallet and encrypt it using the coordinator's public key
|
|
5
|
+
* @param signer
|
|
6
|
+
* @returns Authorization header
|
|
7
|
+
*/
|
|
8
|
+
export declare const getAuthorizationHeader: (signer: Signer) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Reloads with ETH in case the smart account is out of gas (less than 0.05)
|
|
11
|
+
* @param sessionKeyAddress
|
|
12
|
+
*/
|
|
13
|
+
export declare const rechargeGasIfNeeded: (address: Hex, minimumValueOfEther: string, valueToSendOfEther: string) => Promise<void>;
|
|
14
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../tests/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAyB,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC5D,OAAO,EAAmC,GAAG,EAAoB,MAAM,MAAM,CAAC;AAY9E;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAU,QAAQ,MAAM,KAAG,OAAO,CAAC,MAAM,CAM3E,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAC9B,SAAS,GAAG,EACZ,qBAAqB,MAAM,EAC3B,oBAAoB,MAAM,KACzB,OAAO,CAAC,IAAI,CAgBd,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import dotenv from "dotenv";
|
|
2
|
+
import { getBytes, hashMessage } from "ethers";
|
|
3
|
+
import { createWalletClient, formatEther, http, parseEther } from "viem";
|
|
4
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
5
|
+
import { optimismSepolia } from "viem/chains";
|
|
6
|
+
import fs from "fs";
|
|
7
|
+
import { ESupportedNetworks } from "../ts/common";
|
|
8
|
+
import { getPublicClient } from "../ts/common/accountAbstraction";
|
|
9
|
+
import { CryptoService } from "../ts/crypto/crypto.service";
|
|
10
|
+
dotenv.config();
|
|
11
|
+
/**
|
|
12
|
+
* Sign a message with a wallet and encrypt it using the coordinator's public key
|
|
13
|
+
* @param signer
|
|
14
|
+
* @returns Authorization header
|
|
15
|
+
*/
|
|
16
|
+
export const getAuthorizationHeader = async (signer) => {
|
|
17
|
+
const cryptoService = new CryptoService();
|
|
18
|
+
const publicKey = await fs.promises.readFile(process.env.COORDINATOR_PUBLIC_KEY_PATH);
|
|
19
|
+
const signature = await signer.signMessage("message");
|
|
20
|
+
const digest = Buffer.from(getBytes(hashMessage("message"))).toString("hex");
|
|
21
|
+
return `Bearer ${cryptoService.encrypt(publicKey, `${signature}:${digest}`)}`;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Reloads with ETH in case the smart account is out of gas (less than 0.05)
|
|
25
|
+
* @param sessionKeyAddress
|
|
26
|
+
*/
|
|
27
|
+
export const rechargeGasIfNeeded = async (address, minimumValueOfEther, valueToSendOfEther) => {
|
|
28
|
+
const publicClient = getPublicClient(ESupportedNetworks.OPTIMISM_SEPOLIA);
|
|
29
|
+
const balance = await publicClient.getBalance({ address });
|
|
30
|
+
const balanceAsEther = formatEther(balance);
|
|
31
|
+
if (balanceAsEther <= minimumValueOfEther) {
|
|
32
|
+
const testAccount = privateKeyToAccount(process.env.TEST_PRIVATE_KEY);
|
|
33
|
+
const walletClient = createWalletClient({
|
|
34
|
+
chain: optimismSepolia,
|
|
35
|
+
transport: http(),
|
|
36
|
+
});
|
|
37
|
+
await walletClient.sendTransaction({
|
|
38
|
+
account: testAccount,
|
|
39
|
+
to: address,
|
|
40
|
+
value: parseEther(valueToSendOfEther),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../tests/utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAe,MAAM,QAAQ,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAO,IAAI,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAC9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,MAAM,CAAC,MAAM,EAAE,CAAC;AAEhB;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,KAAK,EAAE,MAAc,EAAmB,EAAE;IAC9E,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC;IAC1C,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,2BAA4B,CAAC,CAAC;IACvF,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC7E,OAAO,UAAU,aAAa,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,SAAS,IAAI,MAAM,EAAE,CAAC,EAAE,CAAC;AAChF,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EACtC,OAAY,EACZ,mBAA2B,EAC3B,kBAA0B,EACX,EAAE;IACjB,MAAM,YAAY,GAAG,eAAe,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC;IAC1E,MAAM,OAAO,GAAG,MAAM,YAAY,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3D,MAAM,cAAc,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IAC5C,IAAI,cAAc,IAAI,mBAAmB,EAAE,CAAC;QAC1C,MAAM,WAAW,GAAG,mBAAmB,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAwB,CAAC,CAAC;QAC9E,MAAM,YAAY,GAAG,kBAAkB,CAAC;YACtC,KAAK,EAAE,eAAe;YACtB,SAAS,EAAE,IAAI,EAAE;SAClB,CAAC,CAAC;QACH,MAAM,YAAY,CAAC,eAAe,CAAC;YACjC,OAAO,EAAE,WAAW;YACpB,EAAE,EAAE,OAAO;YACX,KAAK,EAAE,UAAU,CAAC,kBAAkB,CAAC;SACtC,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.module.d.ts","sourceRoot":"","sources":["../../ts/app.module.ts"],"names":[],"mappings":"AAUA,qBAgBa,SAAS;CAAG"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Module } from "@nestjs/common";
|
|
8
|
+
import { ThrottlerModule } from "@nestjs/throttler";
|
|
9
|
+
import { CryptoModule } from "./crypto/crypto.module";
|
|
10
|
+
import { DeployerModule } from "./deployer/deployer.module";
|
|
11
|
+
import { FileModule } from "./file/file.module";
|
|
12
|
+
import { ProofModule } from "./proof/proof.module";
|
|
13
|
+
import { SessionKeysModule } from "./sessionKeys/sessionKeys.module";
|
|
14
|
+
import { SubgraphModule } from "./subgraph/subgraph.module";
|
|
15
|
+
let AppModule = class AppModule {
|
|
16
|
+
};
|
|
17
|
+
AppModule = __decorate([
|
|
18
|
+
Module({
|
|
19
|
+
imports: [
|
|
20
|
+
ThrottlerModule.forRoot([
|
|
21
|
+
{
|
|
22
|
+
ttl: Number(process.env.TTL),
|
|
23
|
+
limit: Number(process.env.LIMIT),
|
|
24
|
+
},
|
|
25
|
+
]),
|
|
26
|
+
FileModule,
|
|
27
|
+
CryptoModule,
|
|
28
|
+
SubgraphModule,
|
|
29
|
+
ProofModule,
|
|
30
|
+
SessionKeysModule,
|
|
31
|
+
DeployerModule,
|
|
32
|
+
],
|
|
33
|
+
})
|
|
34
|
+
], AppModule);
|
|
35
|
+
export { AppModule };
|
|
36
|
+
//# sourceMappingURL=app.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.module.js","sourceRoot":"","sources":["../../ts/app.module.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAkBrD,IAAM,SAAS,GAAf,MAAM,SAAS;CAAG,CAAA;AAAZ,SAAS;IAhBrB,MAAM,CAAC;QACN,OAAO,EAAE;YACP,eAAe,CAAC,OAAO,CAAC;gBACtB;oBACE,GAAG,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;oBAC5B,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC;iBACjC;aACF,CAAC;YACF,UAAU;YACV,YAAY;YACZ,cAAc;YACd,WAAW;YACX,iBAAiB;YACjB,cAAc;SACf;KACF,CAAC;GACW,SAAS,CAAG"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type CanActivate, type ExecutionContext, type CustomDecorator } from "@nestjs/common";
|
|
2
|
+
import { Reflector } from "@nestjs/core";
|
|
3
|
+
import { CryptoService } from "../crypto/crypto.service";
|
|
4
|
+
/**
|
|
5
|
+
* Public metadata key
|
|
6
|
+
*/
|
|
7
|
+
export declare const PUBLIC_METADATA_KEY = "isPublic";
|
|
8
|
+
/**
|
|
9
|
+
* Public decorator to by-pass auth checks
|
|
10
|
+
*
|
|
11
|
+
* @returns public decorator
|
|
12
|
+
*/
|
|
13
|
+
export declare const Public: () => CustomDecorator;
|
|
14
|
+
/**
|
|
15
|
+
* AccountSignatureGuard is responsible for protecting calling controller and websocket gateway functions.
|
|
16
|
+
* If account address is not added to .env file, you will not be allowed to call any API methods.
|
|
17
|
+
* Make sure you send `Authorization: Bearer encrypt({signature}:{digest})` header where:
|
|
18
|
+
* 1. encrypt - RSA public encryption.
|
|
19
|
+
* 2. signature - eth wallet signature for any message
|
|
20
|
+
* 3. digest - hex representation of message digest
|
|
21
|
+
*
|
|
22
|
+
* ```
|
|
23
|
+
* const signature = await signer.signMessage("message");
|
|
24
|
+
* const digest = Buffer.from(getBytes(hashMessage("message"))).toString("hex");
|
|
25
|
+
* ```
|
|
26
|
+
* See tests for more details about authorization.
|
|
27
|
+
*/
|
|
28
|
+
export declare class AccountSignatureGuard implements CanActivate {
|
|
29
|
+
private readonly cryptoService;
|
|
30
|
+
private readonly reflector;
|
|
31
|
+
/**
|
|
32
|
+
* Logger
|
|
33
|
+
*/
|
|
34
|
+
private readonly logger;
|
|
35
|
+
constructor(cryptoService: CryptoService, reflector: Reflector);
|
|
36
|
+
/**
|
|
37
|
+
* This function should return a boolean, indicating whether the request is allowed or not based on message signature and digest.
|
|
38
|
+
*
|
|
39
|
+
* @param ctx - execution context
|
|
40
|
+
* @returns whether the request is allowed or not
|
|
41
|
+
*/
|
|
42
|
+
canActivate(ctx: ExecutionContext): Promise<boolean>;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=AccountSignatureGuard.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountSignatureGuard.service.d.ts","sourceRoot":"","sources":["../../../ts/auth/AccountSignatureGuard.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,WAAW,EAGhB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACrB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AASzC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD;;GAEG;AACH,eAAO,MAAM,mBAAmB,aAAa,CAAC;AAE9C;;;;GAIG;AACH,eAAO,MAAM,MAAM,QAAO,eAAyD,CAAC;AAEpF;;;;;;;;;;;;;GAaG;AACH,qBACa,qBAAsB,YAAW,WAAW;IAOrD,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAP5B;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;gBAGb,aAAa,EAAE,aAAa,EAC5B,SAAS,EAAE,SAAS;IAKvC;;;;;OAKG;IACG,WAAW,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CAqC3D"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var AccountSignatureGuard_1;
|
|
11
|
+
import { Logger, Injectable, SetMetadata, } from "@nestjs/common";
|
|
12
|
+
import { Reflector } from "@nestjs/core";
|
|
13
|
+
import { ethers } from "ethers";
|
|
14
|
+
import fs from "fs";
|
|
15
|
+
import path from "path";
|
|
16
|
+
import { CryptoService } from "../crypto/crypto.service";
|
|
17
|
+
/**
|
|
18
|
+
* Public metadata key
|
|
19
|
+
*/
|
|
20
|
+
export const PUBLIC_METADATA_KEY = "isPublic";
|
|
21
|
+
/**
|
|
22
|
+
* Public decorator to by-pass auth checks
|
|
23
|
+
*
|
|
24
|
+
* @returns public decorator
|
|
25
|
+
*/
|
|
26
|
+
export const Public = () => SetMetadata(PUBLIC_METADATA_KEY, true);
|
|
27
|
+
/**
|
|
28
|
+
* AccountSignatureGuard is responsible for protecting calling controller and websocket gateway functions.
|
|
29
|
+
* If account address is not added to .env file, you will not be allowed to call any API methods.
|
|
30
|
+
* Make sure you send `Authorization: Bearer encrypt({signature}:{digest})` header where:
|
|
31
|
+
* 1. encrypt - RSA public encryption.
|
|
32
|
+
* 2. signature - eth wallet signature for any message
|
|
33
|
+
* 3. digest - hex representation of message digest
|
|
34
|
+
*
|
|
35
|
+
* ```
|
|
36
|
+
* const signature = await signer.signMessage("message");
|
|
37
|
+
* const digest = Buffer.from(getBytes(hashMessage("message"))).toString("hex");
|
|
38
|
+
* ```
|
|
39
|
+
* See tests for more details about authorization.
|
|
40
|
+
*/
|
|
41
|
+
let AccountSignatureGuard = AccountSignatureGuard_1 = class AccountSignatureGuard {
|
|
42
|
+
cryptoService;
|
|
43
|
+
reflector;
|
|
44
|
+
/**
|
|
45
|
+
* Logger
|
|
46
|
+
*/
|
|
47
|
+
logger;
|
|
48
|
+
constructor(cryptoService, reflector) {
|
|
49
|
+
this.cryptoService = cryptoService;
|
|
50
|
+
this.reflector = reflector;
|
|
51
|
+
this.logger = new Logger(AccountSignatureGuard_1.name);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* This function should return a boolean, indicating whether the request is allowed or not based on message signature and digest.
|
|
55
|
+
*
|
|
56
|
+
* @param ctx - execution context
|
|
57
|
+
* @returns whether the request is allowed or not
|
|
58
|
+
*/
|
|
59
|
+
async canActivate(ctx) {
|
|
60
|
+
try {
|
|
61
|
+
const isPublic = this.reflector.get(PUBLIC_METADATA_KEY, ctx.getHandler());
|
|
62
|
+
if (isPublic) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
const request = ctx.switchToHttp().getRequest();
|
|
66
|
+
const socket = ctx.switchToWs().getClient();
|
|
67
|
+
const encryptedHeader = socket.handshake?.headers.authorization || request.headers?.authorization;
|
|
68
|
+
if (!encryptedHeader) {
|
|
69
|
+
this.logger.warn("No authorization header");
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const privateKey = await fs.promises.readFile(path.resolve(process.env.COORDINATOR_PRIVATE_KEY_PATH));
|
|
73
|
+
const [signature, digest] = this.cryptoService
|
|
74
|
+
.decrypt(privateKey, encryptedHeader.replace("Bearer", "").trim())
|
|
75
|
+
.split(":");
|
|
76
|
+
if (!signature || !digest) {
|
|
77
|
+
this.logger.warn("No signature or digest");
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const address = ethers.recoverAddress(Buffer.from(digest, "hex"), signature).toLowerCase();
|
|
81
|
+
const coordinatorAddress = process.env.COORDINATOR_ADDRESSES?.split(",").map((value) => value.toLowerCase()) ?? [];
|
|
82
|
+
return coordinatorAddress.includes(address);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
this.logger.error("Error", error);
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
AccountSignatureGuard = AccountSignatureGuard_1 = __decorate([
|
|
91
|
+
Injectable(),
|
|
92
|
+
__metadata("design:paramtypes", [CryptoService,
|
|
93
|
+
Reflector])
|
|
94
|
+
], AccountSignatureGuard);
|
|
95
|
+
export { AccountSignatureGuard };
|
|
96
|
+
//# sourceMappingURL=AccountSignatureGuard.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountSignatureGuard.service.js","sourceRoot":"","sources":["../../../ts/auth/AccountSignatureGuard.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EACL,MAAM,EAEN,UAAU,EACV,WAAW,GAGZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAKxB,O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0,0 +1,113 @@
|
|
|
1
|
+
import dotenv from "dotenv";
|
|
2
|
+
import { getBytes, hashMessage } from "ethers";
|
|
3
|
+
import hardhat from "hardhat";
|
|
4
|
+
import { AccountSignatureGuard, PUBLIC_METADATA_KEY, Public } from "../AccountSignatureGuard.service";
|
|
5
|
+
dotenv.config();
|
|
6
|
+
jest.mock("../../crypto/crypto.service", () => ({
|
|
7
|
+
CryptoService: {
|
|
8
|
+
getInstance: jest.fn(),
|
|
9
|
+
},
|
|
10
|
+
}));
|
|
11
|
+
describe("AccountSignatureGuard", () => {
|
|
12
|
+
const mockRequest = {
|
|
13
|
+
headers: { authorization: "data" },
|
|
14
|
+
};
|
|
15
|
+
const mockContext = {
|
|
16
|
+
getHandler: jest.fn(),
|
|
17
|
+
switchToHttp: jest.fn().mockReturnValue({
|
|
18
|
+
getRequest: jest.fn(() => mockRequest),
|
|
19
|
+
}),
|
|
20
|
+
switchToWs: jest.fn().mockReturnValue({
|
|
21
|
+
getClient: jest.fn(() => ({ handshake: mockRequest })),
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
const mockSignature = "0xc0436b6fbd5ff883fe88367f081d0780706b5c29fbfde8db2c1d607510f9095a73b3bc9b94a1588d561eaf49d195b134e3f92c36449c017ecf92c5e4d84a32131c";
|
|
25
|
+
const mockDigest = "7f6c0e5c497ded52462ec18daeb1c94cefa11cd6949ebdb7074b2a32cac13fba";
|
|
26
|
+
const mockCryptoService = {
|
|
27
|
+
decrypt: jest.fn(),
|
|
28
|
+
};
|
|
29
|
+
const reflector = {
|
|
30
|
+
get: jest.fn(),
|
|
31
|
+
};
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
mockCryptoService.decrypt = jest.fn(() => `${mockSignature}:${mockDigest}`);
|
|
34
|
+
reflector.get.mockReturnValue(false);
|
|
35
|
+
});
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
jest.clearAllMocks();
|
|
38
|
+
});
|
|
39
|
+
test("should create public decorator properly", () => {
|
|
40
|
+
const decorator = Public();
|
|
41
|
+
expect(decorator.KEY).toBe(PUBLIC_METADATA_KEY);
|
|
42
|
+
});
|
|
43
|
+
test("should return false if there is no Authorization header", async () => {
|
|
44
|
+
const ctx = {
|
|
45
|
+
getHandler: jest.fn(),
|
|
46
|
+
switchToHttp: jest.fn().mockReturnValue({
|
|
47
|
+
getRequest: jest.fn(() => ({
|
|
48
|
+
headers: { authorization: "" },
|
|
49
|
+
})),
|
|
50
|
+
}),
|
|
51
|
+
switchToWs: jest.fn().mockReturnValue({
|
|
52
|
+
getClient: jest.fn(() => ({ handshake: { headers: { authorization: "" } } })),
|
|
53
|
+
}),
|
|
54
|
+
};
|
|
55
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
56
|
+
const result = await guard.canActivate(ctx);
|
|
57
|
+
expect(result).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
test("should return false if there is no signature", async () => {
|
|
60
|
+
mockCryptoService.decrypt.mockReturnValue(`:${mockDigest}`);
|
|
61
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
62
|
+
const result = await guard.canActivate(mockContext);
|
|
63
|
+
expect(result).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
test("should return false if there is no digest", async () => {
|
|
66
|
+
mockCryptoService.decrypt.mockReturnValue(mockSignature);
|
|
67
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
68
|
+
const result = await guard.canActivate(mockContext);
|
|
69
|
+
expect(result).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
test("should return false if signature or digest are invalid", async () => {
|
|
72
|
+
mockCryptoService.decrypt.mockReturnValue(`signature:digest`);
|
|
73
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
74
|
+
const result = await guard.canActivate(mockContext);
|
|
75
|
+
expect(result).toBe(false);
|
|
76
|
+
});
|
|
77
|
+
test("should return false if signer is different", async () => {
|
|
78
|
+
const [, signer] = await hardhat.ethers.getSigners();
|
|
79
|
+
const signature = await signer.signMessage("message");
|
|
80
|
+
const digest = Buffer.from(getBytes(hashMessage("message"))).toString("hex");
|
|
81
|
+
mockCryptoService.decrypt.mockReturnValue(`${signature}:${digest}`);
|
|
82
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
83
|
+
const result = await guard.canActivate(mockContext);
|
|
84
|
+
expect(result).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
test("should return true if authorization is passed properly", async () => {
|
|
87
|
+
const [signer] = await hardhat.ethers.getSigners();
|
|
88
|
+
process.env.COORDINATOR_ADDRESSES = await signer.getAddress();
|
|
89
|
+
const signature = await signer.signMessage("message");
|
|
90
|
+
const digest = Buffer.from(getBytes(hashMessage("message"))).toString("hex");
|
|
91
|
+
mockCryptoService.decrypt.mockReturnValue(`${signature}:${digest}`);
|
|
92
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
93
|
+
const result = await guard.canActivate(mockContext);
|
|
94
|
+
expect(result).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
test("should return false if there is no COORDINATOR_ADDRESSES env", async () => {
|
|
97
|
+
const [signer] = await hardhat.ethers.getSigners();
|
|
98
|
+
process.env.COORDINATOR_ADDRESSES = undefined;
|
|
99
|
+
const signature = await signer.signMessage("message");
|
|
100
|
+
const digest = Buffer.from(getBytes(hashMessage("message"))).toString("hex");
|
|
101
|
+
mockCryptoService.decrypt.mockReturnValue(`${signature}:${digest}`);
|
|
102
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
103
|
+
const result = await guard.canActivate(mockContext);
|
|
104
|
+
expect(result).toBe(false);
|
|
105
|
+
});
|
|
106
|
+
test("should return true if can skip authorization", async () => {
|
|
107
|
+
reflector.get.mockReturnValue(true);
|
|
108
|
+
const guard = new AccountSignatureGuard(mockCryptoService, reflector);
|
|
109
|
+
const result = await guard.canActivate(mockContext);
|
|
110
|
+
expect(result).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
//# sourceMappingURL=AccountSignatureGuard.test.js.map
|