Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changes

- Use generic ledger type in ZOwnablePKWitnesses (#389)
- Bump compact compiler to v0.29.0 (#366)

## 0.0.1-alpha.1 (2025-12-2)
Expand Down
9 changes: 5 additions & 4 deletions contracts/src/access/test/simulators/ZOwnablePKSimulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import {
ZOwnablePKWitnesses,
} from '../../witnesses/ZOwnablePKWitnesses.js';

/**
* Type constructor args
*/
/** Type constructor args */
type ZOwnablePKArgs = readonly [
owner: Uint8Array,
instanceSalt: Uint8Array,
isInit: boolean,
];

/** Concrete ledger type extracted from the generated artifact */
type ZOwnablePKLedger = ReturnType<typeof ledger>;

/**
* Base simulator
* @dev We deliberately use `any` as the base simulator type.
Expand All @@ -47,7 +48,7 @@ const ZOwnablePKSimulatorBase: any = createSimulator<
return [owner, instanceSalt, isInit];
},
ledgerExtractor: (state) => ledger(state),
witnessesFactory: () => ZOwnablePKWitnesses(),
witnessesFactory: () => ZOwnablePKWitnesses<ZOwnablePKLedger>(),
});

/**
Expand Down
27 changes: 15 additions & 12 deletions contracts/src/access/witnesses/ZOwnablePKWitnesses.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { getRandomValues } from 'node:crypto';
import type { WitnessContext } from '@midnight-ntwrk/compact-runtime';
import type { Ledger } from '../../../artifacts/MockZOwnablePK/contract/index.js';

/**
* @description Interface defining the witness methods for ZOwnablePK operations.
* @template L - The ledger type.
* @template P - The private state type.
*/
export interface IZOwnablePKWitnesses<P> {
export interface IZOwnablePKWitnesses<L, P> {
/**
* Retrieves the secret nonce from the private state.
* @param context - The witness context containing the private state.
* @param context - The witness context containing the ledger and private state.
* @returns A tuple of the private state and the secret nonce as a Uint8Array.
*/
wit_secretNonce(context: WitnessContext<Ledger, P>): [P, Uint8Array];
wit_secretNonce(context: WitnessContext<L, P>): [P, Uint8Array];
}

/**
Expand Down Expand Up @@ -56,13 +56,16 @@ export const ZOwnablePKPrivateState = {

/**
* @description Factory function creating witness implementations for Ownable operations.
* @template L - The ledger type, supplied by the simulator.
* @returns An object implementing the Witnesses interface for ZOwnablePKPrivateState.
*/
export const ZOwnablePKWitnesses =
(): IZOwnablePKWitnesses<ZOwnablePKPrivateState> => ({
wit_secretNonce(
context: WitnessContext<Ledger, ZOwnablePKPrivateState>,
): [ZOwnablePKPrivateState, Uint8Array] {
return [context.privateState, context.privateState.secretNonce];
},
});
export const ZOwnablePKWitnesses = <L>(): IZOwnablePKWitnesses<
L,
ZOwnablePKPrivateState
> => ({
wit_secretNonce(
context: WitnessContext<L, ZOwnablePKPrivateState>,
): [ZOwnablePKPrivateState, Uint8Array] {
return [context.privateState, context.privateState.secretNonce];
},
});
16 changes: 11 additions & 5 deletions packages/simulator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { Contract, ledger } from './artifacts/MyContract/contract/index.js';
// 1. Define your contract arguments type
type MyContractArgs = readonly [owner: Uint8Array, value: bigint];

// 2. Create the simulator
// 2. Define the extracted ledger type
type MyContractLedger = ReturnType<typeof ledger>;

// 3. Create the simulator
const MySimulator = createSimulator<
MyPrivateState,
ReturnType<typeof ledger>,
Expand All @@ -31,10 +34,10 @@ const MySimulator = createSimulator<
defaultPrivateState: () => MyPrivateState.generate(),
contractArgs: (owner, value) => [owner, value],
ledgerExtractor: (state) => ledger(state),
witnessesFactory: () => MyWitnesses(),
witnessesFactory: () => MyWitnesses<MyContractLedger>(),
});

// 3. Use it!
// 4. Use it!
const sim = new MySimulator([ownerAddress, 100n], { coinPK: deployerPK });
```

Expand All @@ -52,6 +55,9 @@ import { MyContractWitnesses, MyContractPrivateState } from './MyContractWitness
// Define contract constructor arguments as a tuple type
type MyContractArgs = readonly [arg1: bigint, arg2: string];

// Define the extracted ledger type
type MyContractLedger = ReturnType<typeof ledger>;

// Create the base simulator with full type information
const MyContractSimulatorBase = createSimulator<
MyContractPrivateState, // Private state type
Expand All @@ -63,7 +69,7 @@ const MyContractSimulatorBase = createSimulator<
defaultPrivateState: () => MyContractPrivateState.generate(),
contractArgs: (arg1, arg2) => [arg1, arg2],
ledgerExtractor: (state) => ledger(state),
witnessesFactory: () => MyContractWitnesses(), // Note: Must be a function!
witnessesFactory: () => MyContractWitnesses<MyContractLedger>(), // Note: Must be a function!
});
```

Expand All @@ -76,7 +82,7 @@ If the Compact contract has no witnesses:
// Some Compact contract examples use:
export const MyContractWitnesses = {};

// But for the simulator, wrap it in a function:
// But for the simulator, wrap it in a generic function:
export const MyContractWitnesses = () => ({});
Comment on lines +85 to 86
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix wording: this is a factory function, not a generic function.

The comment says “generic function”, but () => ({}) is non-generic. Rename it to “factory function” to avoid confusion.

Suggested doc fix
-// But for the simulator, wrap it in a generic function:
+// But for the simulator, wrap it in a factory function:
 export const MyContractWitnesses = () => ({});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// But for the simulator, wrap it in a generic function:
export const MyContractWitnesses = () => ({});
// But for the simulator, wrap it in a factory function:
export const MyContractWitnesses = () => ({});
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/simulator/README.md` around lines 85 - 86, The comment incorrectly
calls the export MyContractWitnesses a "generic function"; change the wording to
call it a "factory function" instead. Update the surrounding comment where
MyContractWitnesses is referenced to read something like "but for the simulator,
wrap it in a factory function:" so the term matches the implementation (() =>
({})) and avoids implying type generics.

```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { getRandomValues } from 'node:crypto';
import type { WitnessContext } from '@midnight-ntwrk/compact-runtime';
import type { Ledger } from '../../artifacts/SampleZOwnable/contract/index.js';

/**
* @description Interface defining the witness methods for SampleZOwnable operations.
* @template L - The ledger type.
* @template P - The private state type.
*/
export interface ISampleZOwnableWitnesses<P> {
export interface ISampleZOwnableWitnesses<L, P> {
/**
* Retrieves the secret nonce from the private state.
* @param context - The witness context containing the private state.
* @param context - The witness context containing the ledger and private state.
* @returns A tuple of the private state and the secret nonce as a Uint8Array.
*/
secretNonce(context: WitnessContext<Ledger, P>): [P, Uint8Array];
secretNonce(context: WitnessContext<L, P>): [P, Uint8Array];
}

/**
Expand Down Expand Up @@ -56,13 +56,16 @@ export const SampleZOwnablePrivateState = {

/**
* @description Factory function creating witness implementations for Ownable operations.
* @template L - The ledger type, supplied by the simulator.
* @returns An object implementing the Witnesses interface for SampleZOwnablePrivateState.
*/
export const SampleZOwnableWitnesses =
(): ISampleZOwnableWitnesses<SampleZOwnablePrivateState> => ({
secretNonce(
context: WitnessContext<Ledger, SampleZOwnablePrivateState>,
): [SampleZOwnablePrivateState, Uint8Array] {
return [context.privateState, context.privateState.secretNonce];
},
});
export const SampleZOwnableWitnesses = <L>(): ISampleZOwnableWitnesses<
L,
SampleZOwnablePrivateState
> => ({
secretNonce(
context: WitnessContext<L, SampleZOwnablePrivateState>,
): [SampleZOwnablePrivateState, Uint8Array] {
return [context.privateState, context.privateState.secretNonce];
},
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getRandomValues } from 'node:crypto';
import type { WitnessContext } from '@midnight-ntwrk/compact-runtime';
import type { Ledger } from '../../artifacts/Witness/contract/index.js';

const randomBigInt = (bits: number): bigint => {
const bytes = Math.ceil(bits / 8);
Expand All @@ -16,14 +15,14 @@ const randomBigInt = (bits: number): bigint => {
return result % max;
};

export interface IWitnessWitnesses<P> {
wit_secretBytes(context: WitnessContext<Ledger, P>): [P, Uint8Array];
export interface IWitnessWitnesses<L, P> {
wit_secretBytes(context: WitnessContext<L, P>): [P, Uint8Array];
wit_secretFieldPlusArg(
context: WitnessContext<Ledger, P>,
context: WitnessContext<L, P>,
arg: bigint,
): [P, bigint];
wit_secretUintPlusArgs(
context: WitnessContext<Ledger, P>,
context: WitnessContext<L, P>,
arg1: bigint,
arg2: bigint,
): [P, bigint];
Expand All @@ -45,20 +44,23 @@ export const WitnessPrivateState = {
},
};

export const WitnessWitnesses = (): IWitnessWitnesses<WitnessPrivateState> => ({
export const WitnessWitnesses = <L>(): IWitnessWitnesses<
L,
WitnessPrivateState
> => ({
wit_secretBytes(
context: WitnessContext<Ledger, WitnessPrivateState>,
context: WitnessContext<L, WitnessPrivateState>,
): [WitnessPrivateState, Uint8Array] {
return [context.privateState, context.privateState.secretBytes];
},
wit_secretFieldPlusArg(
context: WitnessContext<Ledger, WitnessPrivateState>,
context: WitnessContext<L, WitnessPrivateState>,
arg: bigint,
): [WitnessPrivateState, bigint] {
return [context.privateState, context.privateState.secretField + arg];
},
wit_secretUintPlusArgs(
context: WitnessContext<Ledger, WitnessPrivateState>,
context: WitnessContext<L, WitnessPrivateState>,
arg1: bigint,
arg2: bigint,
): [WitnessPrivateState, bigint] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import {
SampleZOwnableWitnesses,
} from '../fixtures/sample-contracts/witnesses/SampleZOwnableWitnesses';

/**
* Type constructor args
*/
/** Type constructor args */
type SampleZOwnableArgs = readonly [
owner: Uint8Array,
instanceSalt: Uint8Array,
];

/** Concrete ledger type extracted from the generated artifact */
type SampleZOwnableLedger = ReturnType<typeof ledger>;

/**
* Base simulator
*/
Expand All @@ -36,7 +37,7 @@ const SampleZOwnableSimulatorBase = createSimulator<
return [owner, instanceSalt];
},
ledgerExtractor: (state) => ledger(state),
witnessesFactory: () => SampleZOwnableWitnesses(),
witnessesFactory: () => SampleZOwnableWitnesses<SampleZOwnableLedger>(),
});

/**
Expand Down
9 changes: 5 additions & 4 deletions packages/simulator/test/integration/WitnessSimulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {
WitnessWitnesses,
} from '../fixtures/sample-contracts/witnesses/WitnessWitnesses';

/**
* Type constructor args
*/
/** Type constructor args */
type WitnessArgs = readonly [];

/** Concrete ledger type extracted from the generated artifact */
type WitnessLedger = ReturnType<typeof ledger>;

/**
* Base simulator
*/
Expand All @@ -30,7 +31,7 @@ const WitnessSimulatorBase = createSimulator<
return [];
},
ledgerExtractor: (state) => ledger(state),
witnessesFactory: () => WitnessWitnesses(),
witnessesFactory: () => WitnessWitnesses<WitnessLedger>(),
});

/**
Expand Down
Loading