@cofhe/hardhat-3-plugin 0.0.0-alpha-20260409113701

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.
@@ -0,0 +1,107 @@
1
+ import type { WalletClient } from 'viem';
2
+ import type { CofheClient, CofheConfig, CofheInputConfig } from '@cofhe/sdk';
3
+ import type { DeployMocksArgs, LogMocksDeploy } from './deploy.js';
4
+ import type {
5
+ MockTaskManagerArtifact,
6
+ MockACLArtifact,
7
+ MockZkVerifierArtifact,
8
+ MockThresholdNetworkArtifact,
9
+ TestBedArtifact,
10
+ } from '@cofhe/mock-contracts';
11
+
12
+ import 'hardhat/types/network';
13
+
14
+ // ─── Per-connection CoFHE API ─────────────────────────────────────────────────
15
+
16
+ export interface CofheConnection {
17
+ /**
18
+ * Create a CoFHE configuration. Defaults to hardhat environment;
19
+ * pass overrides as needed.
20
+ */
21
+ createConfig(config?: Partial<CofheInputConfig>): Promise<CofheConfig>;
22
+
23
+ /** Create a CoFHE client instance from a config. */
24
+ createClient(config: CofheConfig): CofheClient;
25
+
26
+ /**
27
+ * Create and connect a batteries-included CoFHE client.
28
+ * If a WalletClient is provided it is used as the signer; otherwise the
29
+ * first account from the connection is used automatically.
30
+ */
31
+ createClientWithBatteries(walletClient?: WalletClient): Promise<CofheClient>;
32
+
33
+ mocks: {
34
+ /** Deploy (or re-deploy) the mock contracts. */
35
+ deployMocks(options?: DeployMocksArgs): Promise<void>;
36
+
37
+ /** Execute a block with CoFHE mock logging enabled. */
38
+ withLogs(closureName: string, closure: () => Promise<void>): Promise<void>;
39
+
40
+ /** Enable logging from CoFHE mock contracts. */
41
+ enableLogs(closureName?: string): Promise<void>;
42
+
43
+ /** Disable logging from CoFHE mock contracts. */
44
+ disableLogs(): Promise<void>;
45
+
46
+ /** Get the plaintext value stored for a ciphertext hash. */
47
+ getPlaintext(ctHash: bigint | string): Promise<bigint>;
48
+
49
+ /** Assert that a ciphertext hash represents the expected plaintext value. */
50
+ expectPlaintext(ctHash: bigint | string, expectedValue: bigint): Promise<void>;
51
+
52
+ /** MockTaskManager contract descriptor — spread into readContract / writeContract */
53
+ MockTaskManager: { address: `0x${string}`; abi: typeof MockTaskManagerArtifact.abi };
54
+
55
+ /** Resolves the MockACL descriptor from the TaskManager on-chain. */
56
+ MockACL(): Promise<{ address: `0x${string}`; abi: typeof MockACLArtifact.abi }>;
57
+
58
+ /** MockZkVerifier contract descriptor */
59
+ MockZkVerifier: { address: `0x${string}`; abi: typeof MockZkVerifierArtifact.abi };
60
+
61
+ /** MockThresholdNetwork contract descriptor */
62
+ MockThresholdNetwork: { address: `0x${string}`; abi: typeof MockThresholdNetworkArtifact.abi };
63
+
64
+ /** TestBed contract descriptor */
65
+ TestBed: { address: `0x${string}`; abi: typeof TestBedArtifact.abi };
66
+ };
67
+ }
68
+
69
+ // ─── Hardhat config augmentation ─────────────────────────────────────────────
70
+
71
+ declare module 'hardhat/types/config' {
72
+ interface HardhatUserConfig {
73
+ cofhe?: {
74
+ /** Whether to log mock FHE operations (default: true) */
75
+ logMocks?: boolean;
76
+ /** Whether to show gas usage warnings for mock operations (default: true) */
77
+ gasWarning?: boolean;
78
+ /**
79
+ * Controls deploy-mocks console output.
80
+ * - `''` — silent, no output
81
+ * - `'v'` — single summary line (default)
82
+ * - `'vv'` — full per-contract deployment logs
83
+ */
84
+ mocksDeployVerbosity?: LogMocksDeploy;
85
+ };
86
+ }
87
+
88
+ interface HardhatConfig {
89
+ cofhe: {
90
+ logMocks: boolean;
91
+ gasWarning: boolean;
92
+ mocksDeployVerbosity: LogMocksDeploy;
93
+ };
94
+ }
95
+ }
96
+
97
+ // ─── NetworkConnection augmentation ──────────────────────────────────────────
98
+
99
+ declare module 'hardhat/types/network' {
100
+ interface NetworkConnection {
101
+ /**
102
+ * CoFHE mock environment — ready to use immediately after network.connect().
103
+ * Mock contracts are deployed automatically on every connect().
104
+ */
105
+ cofhe: CofheConnection;
106
+ }
107
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,79 @@
1
+ import type { PublicClient } from 'viem';
2
+ import { TASK_MANAGER_ADDRESS, MOCKS_ZK_VERIFIER_ADDRESS } from '@cofhe/sdk';
3
+
4
+ // ─── Mock storage helpers ─────────────────────────────────────────────────────
5
+
6
+ /**
7
+ * Returns true when mock contracts are deployed (ZkVerifier has code).
8
+ */
9
+ async function isMockEnvironment(publicClient: PublicClient): Promise<boolean> {
10
+ const bytecode = await publicClient.getCode({
11
+ address: MOCKS_ZK_VERIFIER_ADDRESS,
12
+ });
13
+ return !!bytecode && bytecode.length > 2;
14
+ }
15
+
16
+ /**
17
+ * Reads the plaintext value stored for a ciphertext hash from MockTaskManager.
18
+ * Returns undefined when not running on a mock network.
19
+ */
20
+ export async function mock_getPlaintext(
21
+ publicClient: PublicClient,
22
+ ctHash: bigint | string
23
+ ): Promise<bigint | undefined> {
24
+ if (!(await isMockEnvironment(publicClient))) {
25
+ console.log('mock_getPlaintext — skipped on non-mock network');
26
+ return undefined;
27
+ }
28
+
29
+ return publicClient.readContract({
30
+ address: TASK_MANAGER_ADDRESS,
31
+ abi: [
32
+ {
33
+ name: 'mockStorage',
34
+ type: 'function',
35
+ inputs: [{ name: 'ctHash', type: 'uint256' }],
36
+ outputs: [{ type: 'uint256' }],
37
+ stateMutability: 'view',
38
+ },
39
+ ],
40
+ functionName: 'mockStorage',
41
+ args: [BigInt(ctHash)],
42
+ }) as Promise<bigint>;
43
+ }
44
+
45
+ /**
46
+ * Asserts that a ciphertext hash maps to the expected plaintext value.
47
+ */
48
+ export async function mock_expectPlaintext(
49
+ publicClient: PublicClient,
50
+ ctHash: bigint | string,
51
+ expectedValue: bigint
52
+ ): Promise<void> {
53
+ if (!(await isMockEnvironment(publicClient))) {
54
+ console.log('mock_expectPlaintext — skipped on non-mock network');
55
+ return;
56
+ }
57
+
58
+ const exists = (await publicClient.readContract({
59
+ address: TASK_MANAGER_ADDRESS,
60
+ abi: [
61
+ {
62
+ name: 'inMockStorage',
63
+ type: 'function',
64
+ inputs: [{ name: 'ctHash', type: 'uint256' }],
65
+ outputs: [{ type: 'bool' }],
66
+ stateMutability: 'view',
67
+ },
68
+ ],
69
+ functionName: 'inMockStorage',
70
+ args: [BigInt(ctHash)],
71
+ })) as boolean;
72
+
73
+ if (!exists) throw new Error(`mock_expectPlaintext: ctHash ${ctHash} not found in mock storage`);
74
+
75
+ const plaintext = await mock_getPlaintext(publicClient, ctHash);
76
+ if (plaintext !== expectedValue) {
77
+ throw new Error(`mock_expectPlaintext: expected ${expectedValue}, got ${plaintext} for ctHash ${ctHash}`);
78
+ }
79
+ }