@midnight-ntwrk/wallet-sdk-capabilities 3.2.0-rc.0 → 3.3.0

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.
@@ -1,5 +1,5 @@
1
- import { CounterOffer, TransactionCostModel } from './CounterOffer.js';
2
- import { CoinRecipe, Imbalances, TokenType, TokenValue } from './Imbalances.js';
1
+ import { CounterOffer, type TransactionCostModel } from './CounterOffer.js';
2
+ import type { CoinRecipe, Imbalances, TokenType, TokenValue } from './Imbalances.js';
3
3
  export declare class InsufficientFundsError extends Error {
4
4
  readonly tokenType: TokenType;
5
5
  constructor(tokenType: TokenType);
@@ -1,4 +1,4 @@
1
- import { CoinRecipe, Imbalance, Imbalances, TokenType } from './Imbalances.js';
1
+ import { type CoinRecipe, type Imbalance, Imbalances, type TokenType } from './Imbalances.js';
2
2
  export interface TransactionCostModel {
3
3
  inputFeeOverhead: bigint;
4
4
  outputFeeOverhead: bigint;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './balancer/index.js';
2
2
  export * from './pendingTransactions/index.js';
3
3
  export * from './proving/index.js';
4
+ export * from './simulation/index.js';
4
5
  export * from './submission/index.js';
package/dist/index.js CHANGED
@@ -13,4 +13,5 @@
13
13
  export * from './balancer/index.js';
14
14
  export * from './pendingTransactions/index.js';
15
15
  export * from './proving/index.js';
16
+ export * from './simulation/index.js';
16
17
  export * from './submission/index.js';
@@ -1,4 +1,4 @@
1
- import { DateTime, Either, ParseResult, Schema } from 'effect';
1
+ import { type DateTime, Either, type ParseResult, Schema } from 'effect';
2
2
  export type TransactionTrait<TTransaction> = {
3
3
  ids: (tx: TTransaction) => readonly string[];
4
4
  firstId: (tx: TTransaction) => string;
@@ -1,7 +1,7 @@
1
- import { Clock, Effect, ParseResult, Scope, Stream } from 'effect';
1
+ import { type Clock, Effect, type ParseResult, Scope, Stream } from 'effect';
2
2
  import * as PendingTransactions from './pendingTransactions.js';
3
- import * as rx from 'rxjs';
4
- import { QueryClient } from '@midnight-ntwrk/wallet-sdk-indexer-client/effect';
3
+ import type * as rx from 'rxjs';
4
+ import { type QueryClient } from '@midnight-ntwrk/wallet-sdk-indexer-client/effect';
5
5
  export type PendingTransactionsService<TTransaction> = {
6
6
  start: () => Promise<void>;
7
7
  stop: () => Promise<void>;
@@ -1,8 +1,8 @@
1
1
  import * as ledger from '@midnight-ntwrk/ledger-v8';
2
2
  import type { KeyMaterialProvider } from '@midnight-ntwrk/zkir-v2';
3
- import { InvalidProtocolSchemeError } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
3
+ import { type InvalidProtocolSchemeError } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
4
4
  import { Effect } from 'effect';
5
- declare const ProvingError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
5
+ declare const ProvingError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
6
6
  readonly _tag: "Wallet.Proving";
7
7
  } & Readonly<A>;
8
8
  export declare class ProvingError extends ProvingError_base<{
@@ -12,7 +12,7 @@
12
12
  // limitations under the License.
13
13
  import * as ledger from '@midnight-ntwrk/ledger-v8';
14
14
  import { HttpProverClient, WasmProver } from '@midnight-ntwrk/wallet-sdk-prover-client/effect';
15
- import { ClientError, ServerError } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
15
+ import { ClientError, ServerError, } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
16
16
  import { Data, Effect, pipe } from 'effect';
17
17
  export class ProvingError extends Data.TaggedError('Wallet.Proving') {
18
18
  }
@@ -0,0 +1,180 @@
1
+ /**
2
+ * Unified Simulator for wallet testing.
3
+ *
4
+ * This module provides a simulated ledger environment for testing wallet functionality without requiring a real
5
+ * blockchain node. It supports:
6
+ *
7
+ * - Optional genesis mints for pre-funded accounts (shielded, unshielded, or Night tokens)
8
+ * - Transaction submission with configurable strictness
9
+ * - Night token rewards via rewardNight()
10
+ * - Time advancement for TTL and time-sensitive tests
11
+ */
12
+ import { type Array as Arr, Effect, type Scope, Stream, SubscriptionRef } from 'effect';
13
+ import { type ProofErasedTransaction, type SignatureVerifyingKey } from '@midnight-ntwrk/ledger-v8';
14
+ import { LedgerOps } from '@midnight-ntwrk/wallet-sdk-utilities';
15
+ import { NetworkId } from '@midnight-ntwrk/wallet-sdk-abstractions';
16
+ import { type Block, type BlockProducer, type FullnessSpec, type GenesisMint, type SimulatorState, type StrictnessConfig } from './SimulatorState.js';
17
+ export type { SimulatorState, Block, BlockTransaction, PendingTransaction, BlockInfo, BlockProductionRequest, BlockProducer, FullnessSpec, GenesisMint, StrictnessConfig, } from './SimulatorState.js';
18
+ export { getLastBlock, getCurrentBlockNumber, getBlockByNumber, getLastBlockResults, getLastBlockEvents, hasPendingTransactions, getCurrentTime, applyTransaction, defaultStrictness, genesisStrictness, createStrictness, } from './SimulatorState.js';
19
+ /**
20
+ * Default block producer: produces a block for each state change with non-empty mempool.
21
+ *
22
+ * By default, uses post-genesis strictness (balancing, signatures, limits enforced). This ensures realistic simulation
23
+ * where transactions must be properly balanced (pay fees).
24
+ *
25
+ * @param fullness - Static fullness (0-1) or callback based on state
26
+ * @param strictness - Strictness config (defaults to defaultStrictness)
27
+ */
28
+ export declare const immediateBlockProducer: (fullness?: FullnessSpec, strictness?: StrictnessConfig) => BlockProducer;
29
+ /** Simulator initialization configuration. */
30
+ export type SimulatorConfig = Readonly<{
31
+ /**
32
+ * Pre-funded accounts to create at genesis. When provided, creates a genesis block with transactions minting tokens
33
+ * to recipients. When omitted, the simulator starts with an empty ledger.
34
+ */
35
+ genesisMints?: Arr.NonEmptyArray<GenesisMint>;
36
+ /** Network identifier. Defaults to Undeployed. */
37
+ networkId?: NetworkId.NetworkId;
38
+ /** Custom block producer. Defaults to immediateBlockProducer(). */
39
+ blockProducer?: BlockProducer;
40
+ }>;
41
+ /**
42
+ * Unified simulator for wallet testing.
43
+ *
44
+ * Provides a simulated ledger environment for testing wallet functionality without a real blockchain. Optionally
45
+ * pre-funds accounts via genesis mints.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // Empty ledger (useful for dust/Night token testing via rewardNight)
50
+ * const simulator = yield* Simulator.init({});
51
+ *
52
+ * // Pre-funded accounts (useful for token transfer testing)
53
+ * const simulator = yield* Simulator.init({
54
+ * genesisMints: [{ amount: 1000n, tokenType, shieldedRecipient: secretKeys }],
55
+ * });
56
+ * ```;
57
+ */
58
+ export declare class Simulator {
59
+ #private;
60
+ /**
61
+ * Initialize a new simulator.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * // Empty ledger - use rewardNight() for Night tokens
66
+ * const simulator = yield* Simulator.init({});
67
+ *
68
+ * // Pre-funded accounts for token transfer testing
69
+ * const simulator = yield* Simulator.init({
70
+ * genesisMints: [{ amount: 1000n, tokenType, shieldedRecipient: secretKeys }],
71
+ * });
72
+ *
73
+ * // With custom network ID
74
+ * const simulator = yield* Simulator.init({
75
+ * networkId: NetworkId.Preview,
76
+ * genesisMints: [...],
77
+ * });
78
+ * ```;
79
+ *
80
+ * @param config - Configuration options (all optional)
81
+ * @returns Effect that produces a Simulator instance
82
+ */
83
+ static init(config?: SimulatorConfig): Effect.Effect<Simulator, never, Scope.Scope>;
84
+ /** Initialize simulator with blank ledger state. */
85
+ private static initBlank;
86
+ /**
87
+ * Initialize simulator with genesis mints (pre-funded accounts). Supports shielded and unshielded token mints. Night
88
+ * tokens are auto-detected by comparing tokenType with nativeToken().raw.
89
+ */
90
+ private static initWithGenesis;
91
+ /** Create a Simulator from an initial state with proper stream setup. */
92
+ private static fromState;
93
+ /** Observable stream of simulator state changes. */
94
+ readonly state$: Stream.Stream<SimulatorState>;
95
+ constructor(stateRef: SubscriptionRef.SubscriptionRef<SimulatorState>, state$: Stream.Stream<SimulatorState>);
96
+ /** Get the current simulator state. */
97
+ getLatestState(): Effect.Effect<SimulatorState>;
98
+ /**
99
+ * Distribute Night tokens to a recipient and submit claim transaction to mempool. Used for testing dust token
100
+ * generation.
101
+ *
102
+ * This method:
103
+ *
104
+ * 1. Modifies the ledger to make Night tokens claimable
105
+ * 2. Creates and submits a ClaimRewardsTransaction to the mempool
106
+ * 3. The block producer will process the transaction
107
+ *
108
+ * @param verifyingKey - Signature verifying key (recipient address is derived from it)
109
+ * @param amount - Amount of Night tokens to distribute
110
+ */
111
+ rewardNight(verifyingKey: SignatureVerifyingKey, amount: bigint): Effect.Effect<Block, LedgerOps.LedgerError>;
112
+ /**
113
+ * Submit a transaction and wait for it to be included in a block.
114
+ *
115
+ * This method adds the transaction to the mempool and blocks until the block producer includes it in a block. Use
116
+ * this when you need confirmation that the transaction was processed.
117
+ *
118
+ * For fire-and-forget scenarios where you don't need to wait for block inclusion, use `submitAndForget` instead.
119
+ *
120
+ * @param tx - Transaction to submit (proofs erased)
121
+ * @param options - Optional submission options
122
+ * @param options.strictness - Override well-formedness strictness
123
+ * @returns The block containing the transaction
124
+ */
125
+ submitTransaction(tx: ProofErasedTransaction, options?: {
126
+ strictness?: StrictnessConfig;
127
+ }): Effect.Effect<Block, LedgerOps.LedgerError>;
128
+ /**
129
+ * Submit a transaction without waiting for block inclusion.
130
+ *
131
+ * This method adds the transaction to the mempool and returns immediately. The block producer will process it
132
+ * asynchronously. Use this for fire-and-forget scenarios or when testing custom block producers with batched
133
+ * transactions.
134
+ *
135
+ * To wait for block inclusion, use `submitTransaction` instead.
136
+ *
137
+ * @param tx - Transaction to submit (proofs erased)
138
+ * @param options - Optional options
139
+ * @param options.strictness - Override well-formedness strictness
140
+ */
141
+ submitAndForget(tx: ProofErasedTransaction, options?: {
142
+ strictness?: StrictnessConfig;
143
+ }): Effect.Effect<void>;
144
+ /**
145
+ * Fast-forward the simulator time by the given number of seconds. Does not produce a block - only advances the
146
+ * internal clock. Useful for testing time-sensitive functionality like TTL.
147
+ *
148
+ * @param seconds - Number of seconds to advance (must be positive)
149
+ */
150
+ fastForward(seconds: bigint): Effect.Effect<void>;
151
+ /**
152
+ * Query the simulator state with a custom function. This is a generic query mechanism that allows extracting any
153
+ * information from the current state without modifying it.
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * // Query fee prices
158
+ * const feePrices = yield* simulator.query(state => state.ledger.parameters.feePrices);
159
+ *
160
+ * // Use composable state accessors
161
+ * const blockNumber = yield* simulator.query(getCurrentBlockNumber);
162
+ * const lastBlock = yield* simulator.query(getLastBlock);
163
+ * const events = yield* simulator.query(getLastBlockEvents);
164
+ *
165
+ * // Query UTXOs for an address
166
+ * const utxos = yield* simulator.query(state => Array.from(state.ledger.utxo.filter(address)));
167
+ *
168
+ * // Complex query returning multiple values
169
+ * const info = yield* simulator.query(state => ({
170
+ * networkId: state.networkId,
171
+ * blockNumber: getCurrentBlockNumber(state),
172
+ * feePrices: state.ledger.parameters.feePrices,
173
+ * }));
174
+ * ```;
175
+ *
176
+ * @param fn - Function that receives the current state and returns a result
177
+ * @returns The result of applying the function to the current state
178
+ */
179
+ query<T>(fn: (state: SimulatorState) => T): Effect.Effect<T>;
180
+ }