voltaire-effect 1.0.0 → 1.0.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.
@@ -0,0 +1,146 @@
1
+ import * as effect_Cause from 'effect/Cause';
2
+ import * as effect_Types from 'effect/Types';
3
+ import { KzgBlobType, KzgCommitmentType, KzgProofType } from '@tevm/voltaire';
4
+ import * as Context from 'effect/Context';
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Layer from 'effect/Layer';
7
+
8
+ declare const KZGError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
9
+ readonly _tag: "KZGError";
10
+ } & Readonly<A>;
11
+ /**
12
+ * Error thrown when a KZG operation fails.
13
+ *
14
+ * @description
15
+ * Contains the operation that failed, error code, message, and optional cause.
16
+ *
17
+ * Common failure reasons:
18
+ * - Trusted setup not loaded (SETUP_NOT_LOADED)
19
+ * - Invalid blob size or format (INVALID_BLOB)
20
+ * - Invalid commitment format (INVALID_COMMITMENT)
21
+ * - Invalid proof format (INVALID_PROOF)
22
+ * - Verification failure
23
+ *
24
+ * @since 0.0.1
25
+ */
26
+ declare class KZGError extends KZGError_base<{
27
+ /** Error code for programmatic handling */
28
+ readonly code: "SETUP_NOT_LOADED" | "INVALID_BLOB" | "INVALID_COMMITMENT" | "INVALID_PROOF" | "OPERATION_FAILED";
29
+ /** The KZG operation that failed */
30
+ readonly operation: "blobToKzgCommitment" | "computeBlobKzgProof" | "verifyBlobKzgProof" | "loadTrustedSetup" | "isInitialized";
31
+ /** Human-readable error message */
32
+ readonly message: string;
33
+ /** Underlying error that caused this failure */
34
+ readonly cause?: unknown;
35
+ }> {
36
+ }
37
+ /**
38
+ * Shape interface for KZG commitment service operations.
39
+ *
40
+ * @description
41
+ * Defines the contract for KZG implementations. Operations require the trusted
42
+ * setup to be loaded first via loadTrustedSetup().
43
+ *
44
+ * @since 0.0.1
45
+ */
46
+ interface KZGServiceShape {
47
+ /**
48
+ * Computes a KZG commitment for a blob.
49
+ * @param blob - The 128KB blob data
50
+ * @returns Effect containing the 48-byte commitment, or KZGError if operation fails
51
+ */
52
+ readonly blobToKzgCommitment: (blob: KzgBlobType) => Effect.Effect<KzgCommitmentType, KZGError>;
53
+ /**
54
+ * Computes a KZG proof for a blob and commitment.
55
+ * @param blob - The 128KB blob data
56
+ * @param commitment - The 48-byte commitment
57
+ * @returns Effect containing the 48-byte proof, or KZGError if operation fails
58
+ */
59
+ readonly computeBlobKzgProof: (blob: KzgBlobType, commitment: KzgCommitmentType) => Effect.Effect<KzgProofType, KZGError>;
60
+ /**
61
+ * Verifies a KZG proof against a blob and commitment.
62
+ * @param blob - The 128KB blob data
63
+ * @param commitment - The 48-byte commitment
64
+ * @param proof - The 48-byte proof
65
+ * @returns Effect containing true if proof is valid, or KZGError if operation fails
66
+ */
67
+ readonly verifyBlobKzgProof: (blob: KzgBlobType, commitment: KzgCommitmentType, proof: KzgProofType) => Effect.Effect<boolean, KZGError>;
68
+ /**
69
+ * Loads the trusted setup for KZG operations.
70
+ * @returns Effect that completes when setup is loaded, or KZGError if loading fails
71
+ */
72
+ readonly loadTrustedSetup: () => Effect.Effect<void, KZGError>;
73
+ /**
74
+ * Checks if the trusted setup has been initialized.
75
+ * @returns Effect containing true if initialized, or KZGError if check fails
76
+ */
77
+ readonly isInitialized: () => Effect.Effect<boolean, KZGError>;
78
+ }
79
+ declare const KZGService_base: Context.TagClass<KZGService, "KZGService", KZGServiceShape>;
80
+ /**
81
+ * KZG polynomial commitment service for Effect-based applications.
82
+ * Implements EIP-4844 blob commitments for Ethereum proto-danksharding.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * import { KZGService, KZGLive } from 'voltaire-effect/crypto'
87
+ * import * as Effect from 'effect/Effect'
88
+ *
89
+ * const program = Effect.gen(function* () {
90
+ * const kzg = yield* KZGService
91
+ * yield* kzg.loadTrustedSetup()
92
+ * const commitment = yield* kzg.blobToKzgCommitment(blob)
93
+ * const proof = yield* kzg.computeBlobKzgProof(blob, commitment)
94
+ * return yield* kzg.verifyBlobKzgProof(blob, commitment, proof)
95
+ * }).pipe(Effect.provide(KZGLive))
96
+ * ```
97
+ * @since 0.0.1
98
+ */
99
+ declare class KZGService extends KZGService_base {
100
+ }
101
+ /**
102
+ * Production layer for KZGService using native KZG implementation.
103
+ *
104
+ * @description
105
+ * Provides real KZG operations using the c-kzg-4844 library with Ethereum's
106
+ * trusted setup. The trusted setup must be loaded before other operations.
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * import { KZGService, KZGLive } from 'voltaire-effect/crypto/KZG'
111
+ * import * as Effect from 'effect/Effect'
112
+ *
113
+ * const program = Effect.gen(function* () {
114
+ * const kzg = yield* KZGService
115
+ * yield* kzg.loadTrustedSetup()
116
+ * const commitment = yield* kzg.blobToKzgCommitment(blob)
117
+ * return commitment
118
+ * }).pipe(Effect.provide(KZGLive))
119
+ * ```
120
+ *
121
+ * @since 0.0.1
122
+ * @see {@link KZGTest} for unit testing
123
+ */
124
+ declare const KZGLive: Layer.Layer<KZGService, never, never>;
125
+ /**
126
+ * Test layer for KZGService returning deterministic mock values.
127
+ *
128
+ * @description
129
+ * Provides mock implementations for unit testing. Returns zero-filled
130
+ * arrays for commitments/proofs and always verifies as true.
131
+ * Use when testing application logic without cryptographic overhead.
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * import { KZGService, KZGTest, blobToKzgCommitment } from 'voltaire-effect/crypto/KZG'
136
+ * import * as Effect from 'effect/Effect'
137
+ *
138
+ * const testProgram = blobToKzgCommitment(blob).pipe(Effect.provide(KZGTest))
139
+ * // Returns Uint8Array(48) filled with zeros
140
+ * ```
141
+ *
142
+ * @since 0.0.1
143
+ */
144
+ declare const KZGTest: Layer.Layer<KZGService, never, never>;
145
+
146
+ export { KZGService as K, KZGError as a, KZGLive as b, type KZGServiceShape as c, KZGTest as d };
@@ -0,0 +1,319 @@
1
+ import { Keccak256Hash } from '@tevm/voltaire';
2
+ import * as Context from 'effect/Context';
3
+ import * as Effect from 'effect/Effect';
4
+ import * as Layer from 'effect/Layer';
5
+ import * as effect_Cause from 'effect/Cause';
6
+ import * as effect_Types from 'effect/Types';
7
+
8
+ /**
9
+ * @fileoverview Keccak-256 service definition and layer implementations for Effect.
10
+ *
11
+ * @description
12
+ * Provides the KeccakService Effect Tag and both production (KeccakLive) and
13
+ * test (KeccakTest) layer implementations. The service pattern enables
14
+ * dependency injection for testability and flexibility.
15
+ *
16
+ * @module Keccak256/KeccakService
17
+ * @since 0.0.1
18
+ */
19
+
20
+ /**
21
+ * Shape interface for the Keccak-256 hashing service.
22
+ *
23
+ * @description
24
+ * Defines the contract for Keccak-256 hash implementations.
25
+ * Used as the service shape for {@link KeccakService}.
26
+ *
27
+ * @since 0.0.1
28
+ */
29
+ interface KeccakServiceShape {
30
+ /**
31
+ * Computes the Keccak-256 hash of input data.
32
+ *
33
+ * @param {Uint8Array} data - The input bytes to hash (any length)
34
+ * @returns {Effect.Effect<Keccak256Hash>} Effect containing the 32-byte hash
35
+ */
36
+ readonly hash: (data: Uint8Array) => Effect.Effect<Keccak256Hash>;
37
+ }
38
+ declare const KeccakService_base: Context.TagClass<KeccakService, "KeccakService", KeccakServiceShape>;
39
+ /**
40
+ * Keccak-256 hashing service for Effect-based applications.
41
+ *
42
+ * @description
43
+ * An Effect Context.Tag that provides the standard Ethereum hashing algorithm
44
+ * used for addresses, signatures, state roots, and more. The service pattern
45
+ * enables swapping implementations for testing or alternative backends.
46
+ *
47
+ * The service exposes a single `hash` method that takes arbitrary bytes and
48
+ * produces a 32-byte Keccak-256 hash.
49
+ *
50
+ * @example Basic usage with Effect.gen
51
+ * ```typescript
52
+ * import { KeccakService, KeccakLive } from 'voltaire-effect/crypto/Keccak256'
53
+ * import * as Effect from 'effect/Effect'
54
+ *
55
+ * const program = Effect.gen(function* () {
56
+ * const keccak = yield* KeccakService
57
+ * return yield* keccak.hash(new Uint8Array([1, 2, 3]))
58
+ * }).pipe(Effect.provide(KeccakLive))
59
+ *
60
+ * const hash = await Effect.runPromise(program)
61
+ * ```
62
+ *
63
+ * @example Chaining multiple hashes
64
+ * ```typescript
65
+ * import { KeccakService, KeccakLive } from 'voltaire-effect/crypto/Keccak256'
66
+ * import * as Effect from 'effect/Effect'
67
+ *
68
+ * const doubleHash = Effect.gen(function* () {
69
+ * const keccak = yield* KeccakService
70
+ * const first = yield* keccak.hash(new Uint8Array([1, 2, 3]))
71
+ * return yield* keccak.hash(first)
72
+ * }).pipe(Effect.provide(KeccakLive))
73
+ * ```
74
+ *
75
+ * @example Composing with other services
76
+ * ```typescript
77
+ * import { KeccakService, KeccakLive } from 'voltaire-effect/crypto/Keccak256'
78
+ * import { Secp256k1Service, Secp256k1Live } from 'voltaire-effect/crypto/Secp256k1'
79
+ * import * as Effect from 'effect/Effect'
80
+ * import * as Layer from 'effect/Layer'
81
+ *
82
+ * const program = Effect.gen(function* () {
83
+ * const keccak = yield* KeccakService
84
+ * const secp = yield* Secp256k1Service
85
+ * const msgHash = yield* keccak.hash(message)
86
+ * return yield* secp.sign(msgHash, privateKey)
87
+ * }).pipe(Effect.provide(Layer.merge(KeccakLive, Secp256k1Live)))
88
+ * ```
89
+ *
90
+ * @see {@link KeccakLive} - Production implementation
91
+ * @see {@link KeccakTest} - Test implementation
92
+ * @see {@link hash} - Standalone hash function
93
+ * @since 0.0.1
94
+ */
95
+ declare class KeccakService extends KeccakService_base {
96
+ }
97
+ /**
98
+ * Production layer for KeccakService using native Keccak-256 implementation.
99
+ *
100
+ * @description
101
+ * Provides the real Keccak-256 hash implementation from the voltaire library.
102
+ * Use this layer in production applications for cryptographically secure hashing.
103
+ *
104
+ * The underlying implementation uses optimized native code (Zig/Rust) for
105
+ * high-performance hashing.
106
+ *
107
+ * @example Providing the live layer
108
+ * ```typescript
109
+ * import { KeccakService, KeccakLive } from 'voltaire-effect/crypto/Keccak256'
110
+ * import * as Effect from 'effect/Effect'
111
+ *
112
+ * const program = Effect.gen(function* () {
113
+ * const keccak = yield* KeccakService
114
+ * return yield* keccak.hash(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))
115
+ * })
116
+ *
117
+ * const result = await Effect.runPromise(program.pipe(Effect.provide(KeccakLive)))
118
+ * ```
119
+ *
120
+ * @example Merging with other layers
121
+ * ```typescript
122
+ * import { KeccakLive } from 'voltaire-effect/crypto/Keccak256'
123
+ * import { SHA256Live } from 'voltaire-effect/crypto/SHA256'
124
+ * import * as Layer from 'effect/Layer'
125
+ *
126
+ * const CryptoLive = Layer.merge(KeccakLive, SHA256Live)
127
+ * ```
128
+ *
129
+ * @see {@link KeccakService} - The service tag
130
+ * @see {@link KeccakTest} - Test implementation for unit tests
131
+ * @since 0.0.1
132
+ */
133
+ declare const KeccakLive: Layer.Layer<KeccakService, never, never>;
134
+ /**
135
+ * Test layer for KeccakService returning deterministic zero-filled hashes.
136
+ *
137
+ * @description
138
+ * Provides a mock Keccak-256 implementation that always returns a 32-byte
139
+ * array filled with zeros. Use for unit testing without cryptographic overhead
140
+ * when the actual hash value doesn't matter for the test.
141
+ *
142
+ * This layer is useful for:
143
+ * - Unit tests that need deterministic output
144
+ * - Performance tests that want to isolate non-crypto logic
145
+ * - Tests where the hash value is not validated
146
+ *
147
+ * @example Using in tests
148
+ * ```typescript
149
+ * import { KeccakService, KeccakTest } from 'voltaire-effect/crypto/Keccak256'
150
+ * import * as Effect from 'effect/Effect'
151
+ * import { describe, it, expect } from 'vitest'
152
+ *
153
+ * describe('MyService', () => {
154
+ * it('should hash data', async () => {
155
+ * const program = Effect.gen(function* () {
156
+ * const keccak = yield* KeccakService
157
+ * return yield* keccak.hash(new Uint8Array([1, 2, 3]))
158
+ * })
159
+ *
160
+ * const result = await Effect.runPromise(program.pipe(Effect.provide(KeccakTest)))
161
+ * expect(result).toEqual(new Uint8Array(32)) // All zeros
162
+ * })
163
+ * })
164
+ * ```
165
+ *
166
+ * @see {@link KeccakService} - The service tag
167
+ * @see {@link KeccakLive} - Production implementation
168
+ * @since 0.0.1
169
+ */
170
+ declare const KeccakTest: Layer.Layer<KeccakService, never, never>;
171
+
172
+ /**
173
+ * @fileoverview Transport error class for JSON-RPC communication failures.
174
+ *
175
+ * @module TransportError
176
+ * @since 0.0.1
177
+ *
178
+ * @description
179
+ * Defines the error type used by all transport implementations when JSON-RPC
180
+ * communication fails. The error includes the JSON-RPC error code, message,
181
+ * and optional additional data for debugging.
182
+ *
183
+ * Common JSON-RPC error codes:
184
+ * - -32700: Parse error
185
+ * - -32600: Invalid request
186
+ * - -32601: Method not found
187
+ * - -32602: Invalid params
188
+ * - -32603: Internal error
189
+ * - -32000 to -32099: Server errors (implementation-defined)
190
+ *
191
+ * @see {@link TransportService} - The service that uses this error type
192
+ */
193
+ declare const TransportError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
194
+ readonly _tag: "TransportError";
195
+ } & Readonly<A>;
196
+ /**
197
+ * Error thrown when a transport operation fails.
198
+ *
199
+ * @description
200
+ * Contains JSON-RPC error code and optional data for debugging.
201
+ * This error is thrown by all transport implementations (HttpTransport,
202
+ * WebSocketTransport, BrowserTransport) when a JSON-RPC request fails.
203
+ *
204
+ * The error includes:
205
+ * - `code`: Standard JSON-RPC error code
206
+ * - `message`: Human-readable error description
207
+ * - `data`: Optional additional error data from the provider
208
+ *
209
+ * @since 0.0.1
210
+ *
211
+ * @example Creating a transport error
212
+ * ```typescript
213
+ * const error = new TransportError({
214
+ * code: -32603,
215
+ * message: 'Internal error',
216
+ * data: { details: 'Connection refused' }
217
+ * })
218
+ *
219
+ * console.log(error.code) // -32603
220
+ * console.log(error.message) // 'Internal error'
221
+ * console.log(error.data) // { details: 'Connection refused' }
222
+ * ```
223
+ *
224
+ * @example Handling transport errors in Effect
225
+ * ```typescript
226
+ * import { Effect } from 'effect'
227
+ * import { TransportService, TransportError, HttpTransport } from 'voltaire-effect'
228
+ *
229
+ * const program = Effect.gen(function* () {
230
+ * const transport = yield* TransportService
231
+ * return yield* transport.request<string>('eth_blockNumber')
232
+ * }).pipe(
233
+ * Effect.catchTag('TransportError', (error) => {
234
+ * if (error.code === -32601) {
235
+ * console.log('Method not supported by this node')
236
+ * }
237
+ * return Effect.fail(error)
238
+ * }),
239
+ * Effect.provide(HttpTransport('https://mainnet.infura.io/v3/YOUR_KEY'))
240
+ * )
241
+ * ```
242
+ *
243
+ * @see {@link TransportService} - The service that produces this error
244
+ */
245
+ declare class TransportError extends TransportError_base<{
246
+ /**
247
+ * The original input that caused the error.
248
+ */
249
+ readonly input: {
250
+ code: number;
251
+ message: string;
252
+ data?: unknown;
253
+ };
254
+ /**
255
+ * JSON-RPC error code.
256
+ */
257
+ readonly code: number;
258
+ /**
259
+ * Human-readable error message.
260
+ */
261
+ readonly message: string;
262
+ /**
263
+ * Additional error data from the JSON-RPC response.
264
+ *
265
+ * @description
266
+ * May contain provider-specific error details such as revert reasons,
267
+ * stack traces, or other debugging information.
268
+ */
269
+ readonly data?: unknown;
270
+ /**
271
+ * Optional underlying cause.
272
+ */
273
+ readonly cause?: unknown;
274
+ /**
275
+ * Optional context for debugging.
276
+ */
277
+ readonly context?: Record<string, unknown>;
278
+ }> {
279
+ /**
280
+ * Creates a new TransportError.
281
+ *
282
+ * @param input - JSON-RPC error response containing code, message, and optional data
283
+ * @param message - Optional override for the error message
284
+ * @param options - Optional error options
285
+ * @param options.cause - Underlying error that caused this failure
286
+ */
287
+ constructor(input: {
288
+ code: number;
289
+ message: string;
290
+ data?: unknown;
291
+ }, message?: string, options?: {
292
+ cause?: unknown;
293
+ context?: Record<string, unknown>;
294
+ });
295
+ }
296
+
297
+ /**
298
+ * @fileoverview Minimal Provider service for blockchain JSON-RPC operations.
299
+ *
300
+ * @module ProviderService
301
+ * @since 0.0.1
302
+ */
303
+
304
+ /**
305
+ * Minimal provider shape - only the request method.
306
+ * All operations are exposed as free functions that use this internally.
307
+ */
308
+ type ProviderShape = {
309
+ readonly request: <T>(method: string, params?: unknown[]) => Effect.Effect<T, TransportError>;
310
+ };
311
+ declare const ProviderService_base: Context.TagClass<ProviderService, "ProviderService", ProviderShape>;
312
+ /**
313
+ * Provider service for blockchain JSON-RPC operations.
314
+ * Use free functions (getBalance, getBlock, call, etc.) for operations.
315
+ */
316
+ declare class ProviderService extends ProviderService_base {
317
+ }
318
+
319
+ export { KeccakService as K, ProviderService as P, TransportError as T, type ProviderShape as a, KeccakLive as b, KeccakTest as c, type KeccakServiceShape as d };