@cofhe/hardhat-plugin 0.1.1 → 0.2.1

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/src/index.ts CHANGED
@@ -1,33 +1,28 @@
1
1
  /* eslint-disable no-empty-pattern */
2
2
  /* eslint-disable turbo/no-undeclared-env-vars */
3
- /* eslint-disable no-unused-vars */
4
3
  import chalk from 'chalk';
5
4
  import { type PublicClient, type WalletClient } from 'viem';
6
5
  import { extendConfig, extendEnvironment, task, types } from 'hardhat/config';
7
6
  import { TASK_TEST, TASK_NODE } from 'hardhat/builtin-tasks/task-names';
8
7
  import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers';
9
- import { type CofhesdkClient, type CofhesdkConfig, type CofhesdkInputConfig, type Result } from '@cofhe/sdk';
8
+ import {
9
+ type CofhesdkClient,
10
+ type CofhesdkConfig,
11
+ type CofhesdkInputConfig,
12
+ MOCKS_ZK_VERIFIER_SIGNER_ADDRESS,
13
+ } from '@cofhe/sdk';
10
14
  import { createCofhesdkClient, createCofhesdkConfig } from '@cofhe/sdk/node';
11
15
  import { HardhatSignerAdapter } from '@cofhe/sdk/adapters';
12
16
 
13
17
  import { localcofheFundAccount } from './fund.js';
14
- import {
15
- MOCKS_ZK_VERIFIER_SIGNER_ADDRESS,
16
- TASK_COFHE_MOCKS_DEPLOY,
17
- TASK_COFHE_MOCKS_SET_LOG_OPS,
18
- TASK_COFHE_USE_FAUCET,
19
- } from './consts.js';
18
+ import { TASK_COFHE_MOCKS_DEPLOY, TASK_COFHE_MOCKS_SET_LOG_OPS, TASK_COFHE_USE_FAUCET } from './consts.js';
20
19
  import { deployMocks, type DeployMocksArgs } from './deploy.js';
21
20
  import { mock_setLoggingEnabled, mock_withLogs } from './logging.js';
22
- import { mock_expectPlaintext } from './utils.js';
21
+ import { getFixedMockContract, mock_expectPlaintext } from './utils.js';
23
22
  import { mock_getPlaintext } from './utils.js';
23
+ import type { Contract } from 'ethers';
24
+ import { hardhat } from '@cofhe/sdk/chains';
24
25
  import {
25
- expectResultError,
26
- expectResultPartialValue,
27
- expectResultSuccess,
28
- expectResultValue,
29
- } from './expectResultUtils.js';
30
- export {
31
26
  MockACLArtifact,
32
27
  MockQueryDecrypterArtifact,
33
28
  MockTaskManagerArtifact,
@@ -184,8 +179,8 @@ task(TASK_COFHE_MOCKS_SET_LOG_OPS, 'Set logging for the Mock CoFHE contracts')
184
179
 
185
180
  // MOCK UTILS
186
181
 
182
+ export * from './consts.js';
187
183
  export * from './utils.js';
188
- export * from './expectResultUtils.js';
189
184
  export * from './fund.js';
190
185
  export * from './logging.js';
191
186
  export * from './deploy.js';
@@ -217,35 +212,21 @@ declare module 'hardhat/types/runtime' {
217
212
  hardhatSignerAdapter: (
218
213
  signer: HardhatEthersSigner
219
214
  ) => Promise<{ publicClient: PublicClient; walletClient: WalletClient }>;
220
-
221
- /**
222
- * Assert that a Result type returned from a function is successful and return its value (result.success === true)
223
- * @param {Result<T>} result - The Result to check
224
- * @returns {T} The inner data of the Result (non null)
225
- */
226
- expectResultSuccess: <T>(result: Result<T> | Promise<Result<T>>) => Promise<T>;
227
-
228
215
  /**
229
- * Assert that a Result type contains an error matching the partial string (result.success === false && result.error.includes(errorPartial))
230
- * @param {Result<T>} result - The Result to check
231
- * @param {string} errorPartial - The partial error string to match
232
- */
233
- expectResultError: <T>(result: Result<T> | Promise<Result<T>>, errorPartial: string) => Promise<void>;
234
-
235
- /**
236
- * Assert that a Result type contains a specific value (result.success === true && result.data === value)
237
- * @param {Result<T>} result - The Result to check
238
- * @param {T} value - The inner data of the Result (non null)
216
+ * Connect a CoFHE SDK client with a Hardhat ethers signer
217
+ * @param {CofhesdkClient} client - The CoFHE SDK client to connect
218
+ * @param {HardhatEthersSigner} signer - The Hardhat ethers signer to use
219
+ * @returns {Promise<void>}
239
220
  */
240
- expectResultValue: <T>(result: Result<T> | Promise<Result<T>>, value: T) => Promise<T>;
241
-
221
+ connectWithHardhatSigner: (client: CofhesdkClient, signer: HardhatEthersSigner) => Promise<void>;
242
222
  /**
243
- * Assert that a Result type contains a value matching the partial object (result.success === true && result.data.includes(partial))
244
- * @param {Result<T>} result - The Result to check
245
- * @param {Partial<T>} partial - The partial object to match against
246
- * @returns {T} The inner data of the Result (non null)
223
+ * Create and connect to a batteries included client.
224
+ * Also generates a self-usage a permit for the signer.
225
+ * If customization is needed, use createCofhesdkClient and connectWithHardhatSigner.
226
+ * @param {HardhatEthersSigner} signer - The Hardhat ethers signer to use (optional - defaults to first signer)
227
+ * @returns {Promise<CofhesdkClient>} The CoFHE SDK client instance
247
228
  */
248
- expectResultPartialValue: <T>(result: Result<T> | Promise<Result<T>>, partial: Partial<T>) => Promise<T>;
229
+ createBatteriesIncludedCofhesdkClient: (signer?: HardhatEthersSigner) => Promise<CofhesdkClient>;
249
230
 
250
231
  mocks: {
251
232
  /**
@@ -318,6 +299,36 @@ declare module 'hardhat/types/runtime' {
318
299
  * @param {bigint} expectedValue - The expected plaintext value
319
300
  */
320
301
  expectPlaintext: (ctHash: bigint, expectedValue: bigint) => Promise<void>;
302
+
303
+ /**
304
+ * Get the MockTaskManager contract
305
+ * @returns {Promise<Contract>} The MockTaskManager contract
306
+ */
307
+ getMockTaskManager: () => Promise<Contract>;
308
+
309
+ /**
310
+ * Get the MockACL contract
311
+ * @returns {Promise<Contract>} The MockACL contract
312
+ */
313
+ getMockACL: () => Promise<Contract>;
314
+
315
+ /**
316
+ * Get the MockQueryDecrypter contract
317
+ * @returns {Promise<Contract>} The MockQueryDecrypter contract
318
+ */
319
+ getMockQueryDecrypter: () => Promise<Contract>;
320
+
321
+ /**
322
+ * Get the MockZkVerifier contract
323
+ * @returns {Promise<Contract>} The MockZkVerifier contract
324
+ */
325
+ getMockZkVerifier: () => Promise<Contract>;
326
+
327
+ /**
328
+ * Get the TestBed contract
329
+ * @returns {Promise<Contract>} The TestBed contract
330
+ */
331
+ getTestBed: () => Promise<Contract>;
321
332
  };
322
333
  };
323
334
  }
@@ -333,6 +344,7 @@ extendEnvironment((hre) => {
333
344
 
334
345
  // Inject zkv wallet client into config
335
346
  const configWithZkvWalletClient = {
347
+ environment: 'hardhat' as const,
336
348
  ...config,
337
349
  _internal: {
338
350
  ...config._internal,
@@ -348,21 +360,35 @@ extendEnvironment((hre) => {
348
360
  hardhatSignerAdapter: async (signer: HardhatEthersSigner) => {
349
361
  return HardhatSignerAdapter(signer);
350
362
  },
351
- expectResultSuccess: async <T>(result: Result<T> | Promise<Result<T>>) => {
352
- const awaitedResult = await result;
353
- return expectResultSuccess(awaitedResult);
354
- },
355
- expectResultError: async <T>(result: Result<T> | Promise<Result<T>>, errorPartial: string) => {
356
- const awaitedResult = await result;
357
- return expectResultError(awaitedResult, errorPartial);
363
+ connectWithHardhatSigner: async (client: CofhesdkClient, signer: HardhatEthersSigner) => {
364
+ const { publicClient, walletClient } = await HardhatSignerAdapter(signer);
365
+ return client.connect(publicClient, walletClient);
358
366
  },
359
- expectResultValue: async <T>(result: Result<T> | Promise<Result<T>>, value: T) => {
360
- const awaitedResult = await result;
361
- return expectResultValue(awaitedResult, value);
362
- },
363
- expectResultPartialValue: async <T>(result: Result<T> | Promise<Result<T>>, partial: Partial<T>) => {
364
- const awaitedResult = await result;
365
- return expectResultPartialValue(awaitedResult, partial);
367
+ createBatteriesIncludedCofhesdkClient: async (signer?: HardhatEthersSigner) => {
368
+ // Get signer if not provided
369
+ if (!signer) {
370
+ [signer] = await hre.ethers.getSigners();
371
+ }
372
+
373
+ // Create config
374
+ const config = await hre.cofhesdk.createCofhesdkConfig({
375
+ environment: 'hardhat',
376
+ supportedChains: [hardhat],
377
+ });
378
+
379
+ // Create client
380
+ const client = hre.cofhesdk.createCofhesdkClient(config);
381
+
382
+ // Connect client
383
+ await hre.cofhesdk.connectWithHardhatSigner(client, signer);
384
+
385
+ // Create self-usage permit
386
+ await client.permits.createSelf({
387
+ issuer: signer.address,
388
+ });
389
+
390
+ // Return client
391
+ return client;
366
392
  },
367
393
  mocks: {
368
394
  withLogs: async (closureName: string, closure: () => Promise<void>) => {
@@ -385,6 +411,15 @@ extendEnvironment((hre) => {
385
411
  const [signer] = await hre.ethers.getSigners();
386
412
  return mock_expectPlaintext(signer.provider, ctHash, expectedValue);
387
413
  },
414
+ getMockTaskManager: async () => getFixedMockContract(hre, MockTaskManagerArtifact),
415
+ getMockACL: async () => {
416
+ const taskManager = await getFixedMockContract(hre, MockTaskManagerArtifact);
417
+ const aclAddress = await taskManager.acl();
418
+ return hre.ethers.getContractAt(MockACLArtifact.abi, aclAddress);
419
+ },
420
+ getMockQueryDecrypter: async () => getFixedMockContract(hre, MockQueryDecrypterArtifact),
421
+ getMockZkVerifier: async () => getFixedMockContract(hre, MockZkVerifierArtifact),
422
+ getTestBed: async () => getFixedMockContract(hre, TestBedArtifact),
388
423
  },
389
424
  };
390
425
  });
package/src/logging.ts CHANGED
@@ -1,23 +1,17 @@
1
1
  import chalk from 'chalk';
2
2
  import { type HardhatRuntimeEnvironment } from 'hardhat/types';
3
- import { TASK_MANAGER_ADDRESS } from './consts';
3
+ import { getFixedMockContract } from './utils';
4
4
  import { MockTaskManagerArtifact } from '@cofhe/mock-contracts';
5
5
 
6
- const getDeployedMockTaskManager = async (hre: HardhatRuntimeEnvironment) => {
7
- // Fetch the deployed MockTaskManager
8
- const taskManager = await hre.ethers.getContractAt(MockTaskManagerArtifact.abi, TASK_MANAGER_ADDRESS);
9
-
10
- return taskManager;
11
- };
12
-
13
- const getLoggingEnabled = async (hre: HardhatRuntimeEnvironment) => {
14
- const taskManager = await getDeployedMockTaskManager(hre);
15
- return await taskManager.logOps();
6
+ const getLoggingEnabled = async (hre: HardhatRuntimeEnvironment): Promise<boolean> => {
7
+ const taskManager = await getFixedMockContract(hre, MockTaskManagerArtifact);
8
+ return taskManager.logOps();
16
9
  };
17
10
 
18
11
  const setLoggingEnabled = async (hre: HardhatRuntimeEnvironment, enabled: boolean) => {
19
- const taskManager = await getDeployedMockTaskManager(hre);
20
- await taskManager.setLogOps(enabled);
12
+ const taskManager = await getFixedMockContract(hre, MockTaskManagerArtifact);
13
+ const tx = await taskManager.setLogOps(enabled);
14
+ await tx.wait();
21
15
  };
22
16
 
23
17
  // prettier-ignore
package/src/utils.ts CHANGED
@@ -1,7 +1,38 @@
1
- import { TASK_MANAGER_ADDRESS, MOCKS_ZK_VERIFIER_ADDRESS } from './consts.js';
1
+ import { TASK_MANAGER_ADDRESS, MOCKS_ZK_VERIFIER_ADDRESS } from '@cofhe/sdk';
2
2
  import { expect } from 'chai';
3
- import { ethers } from 'ethers';
3
+ import { Contract, ethers } from 'ethers';
4
4
  import { type HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider';
5
+ import type { MockArtifact } from '@cofhe/mock-contracts';
6
+ import type { HardhatRuntimeEnvironment } from 'hardhat/types';
7
+
8
+ // Deployment utils
9
+
10
+ /// Deploys a mock contract from a pre-built artifact from the mock-contracts package
11
+ /// If the mock contract should be deployed to a fixed address, `hardhat_setCode` op is used to set the code at the fixed address
12
+ /// Otherwise, we deploy the contract using ethers.js to a non-fixed address
13
+ export const deployMockContractFromArtifact = async (hre: HardhatRuntimeEnvironment, artifact: MockArtifact) => {
14
+ // Use hardhat_setCode to deploy to fixed address
15
+ if (artifact.isFixed) {
16
+ await hre.network.provider.send('hardhat_setCode', [artifact.fixedAddress, artifact.deployedBytecode]);
17
+ return getFixedMockContract(hre, artifact);
18
+ }
19
+
20
+ // Use ethers.js to deploy to variable address
21
+ const [signer] = await hre.ethers.getSigners();
22
+ const factory = new hre.ethers.ContractFactory(artifact.abi, artifact.bytecode, signer);
23
+ const contract = await factory.deploy(/* constructor args */);
24
+ await contract.waitForDeployment();
25
+ return contract as Contract;
26
+ };
27
+
28
+ export const getFixedMockContract = async (hre: HardhatRuntimeEnvironment, artifact: MockArtifact) => {
29
+ if (!artifact.isFixed) {
30
+ throw new Error('Artifact is not fixed');
31
+ }
32
+ return await hre.ethers.getContractAt(artifact.abi, artifact.fixedAddress);
33
+ };
34
+
35
+ // Testing utils
5
36
 
6
37
  const mock_checkIsTestnet = async (fnName: string, provider: HardhatEthersProvider | ethers.JsonRpcProvider) => {
7
38
  // Testnet is checked by testing if MockZkVerifier is deployed
@@ -1,24 +0,0 @@
1
- import { type Result } from '@cofhe/sdk';
2
- import { expect } from 'chai';
3
-
4
- export const expectResultError = <T>(result: Result<T>, errorPartial: string) => {
5
- expect(result.success).to.eq(false, 'Result should be an error');
6
- expect(result.error).to.include(errorPartial, `Error should contain error partial: ${errorPartial}`);
7
- };
8
-
9
- export const expectResultSuccess = <T>(result: Result<T>): T => {
10
- expect(result.success).to.eq(true, 'Result should be a success');
11
- return result.data!;
12
- };
13
-
14
- export const expectResultValue = <T>(result: Result<T>, value: T): T => {
15
- expect(result.success).to.eq(true, 'Result should be a success');
16
- expect(result.data).to.eq(value, `Result should have the expected value ${value}`);
17
- return result.data!;
18
- };
19
-
20
- export const expectResultPartialValue = <T>(result: Result<T>, partial: Partial<T>): T => {
21
- expect(result.success).to.eq(true, 'Result should be a success');
22
- expect(result.data).to.include(partial, `Result should have the expected partial ${partial}`);
23
- return result.data!;
24
- };