@corpus-core/colibri-stateless 2.0.4 → 2.0.6
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/README.md +15 -1
- package/c4w.js +1 -1
- package/c4w.wasm +0 -0
- package/cjs/http.js +5 -32
- package/cjs/index.js +44 -54
- package/cjs/index.node.js +53 -0
- package/cjs/runtime.js +53 -0
- package/cjs/runtime_node.js +113 -0
- package/cjs/runtime_wasm.js +132 -0
- package/cjs/wasm.js +10 -0
- package/cjs/wasm_cjs.js +10 -0
- package/http.d.ts +25 -1
- package/http.js +5 -32
- package/index.d.ts +9 -2
- package/index.js +40 -51
- package/index.node.d.ts +24 -0
- package/index.node.js +35 -0
- package/package.json +13 -3
- package/prebuilds/darwin-arm64/colibri_native.node +0 -0
- package/prebuilds/darwin-x64/colibri_native.node +0 -0
- package/prebuilds/linux-arm64/colibri_native.node +0 -0
- package/prebuilds/linux-x64/colibri_native.node +0 -0
- package/prebuilds/win32-x64/colibri_native.node +0 -0
- package/runtime.d.ts +88 -0
- package/runtime.js +49 -0
- package/runtime_node.d.ts +28 -0
- package/runtime_node.js +110 -0
- package/runtime_wasm.d.ts +28 -0
- package/runtime_wasm.js +129 -0
- package/types.d.ts +4 -1
- package/wasm.d.ts +7 -0
- package/wasm.js +11 -0
- package/wasm_cjs.d.ts +7 -0
- package/wasm_cjs.js +9 -0
package/runtime.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 corpus.core
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
* this software and associated documentation files (the "Software"), to deal in
|
|
6
|
+
* the Software without restriction, including without limitation the rights to
|
|
7
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
* subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
17
|
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
18
|
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
19
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
*
|
|
21
|
+
* SPDX-License-Identifier: MIT
|
|
22
|
+
*/
|
|
23
|
+
import type { DataRequest } from './types.js';
|
|
24
|
+
import type { Storage } from './wasm_shared.js';
|
|
25
|
+
export type { Storage };
|
|
26
|
+
/**
|
|
27
|
+
* Opaque handle for a native/WASM context (prover, verifier or rpc).
|
|
28
|
+
* WASM uses heap pointers (numbers), the native addon uses externals.
|
|
29
|
+
*/
|
|
30
|
+
export type CtxHandle = unknown;
|
|
31
|
+
/**
|
|
32
|
+
* Parsed JSON status as returned by the C status-builder functions.
|
|
33
|
+
* For the prover, `result` is materialized as `Uint8Array` (the proof).
|
|
34
|
+
* For verify/rpc, `result` is the parsed JSON result value.
|
|
35
|
+
*/
|
|
36
|
+
export interface RuntimeStatus {
|
|
37
|
+
status: 'success' | 'error' | 'pending' | 'revert';
|
|
38
|
+
result?: any;
|
|
39
|
+
error?: string;
|
|
40
|
+
/** Revert data (EIP-3668 / CCIP-Read) when status is 'revert'. */
|
|
41
|
+
data?: any;
|
|
42
|
+
requests?: DataRequest[];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Value-level abstraction over the C core, implemented by the WASM build
|
|
46
|
+
* (`runtime_wasm.ts`) and the native Node.js addon (`runtime_node.ts`).
|
|
47
|
+
*
|
|
48
|
+
* All methods are synchronous: implementations are fully initialized when
|
|
49
|
+
* returned by `getRuntime()`.
|
|
50
|
+
*/
|
|
51
|
+
export interface C4Runtime {
|
|
52
|
+
readonly kind: 'wasm' | 'native';
|
|
53
|
+
getMethodType(chainId: bigint, method: string, paramsJson: string | null, flags: number): number;
|
|
54
|
+
createProverCtx(method: string, argsJson: string, chainId: bigint, flags: number): CtxHandle;
|
|
55
|
+
/** Executes one prover step. On success, `result` contains the proof bytes. */
|
|
56
|
+
executeProverCtx(ctx: CtxHandle): RuntimeStatus;
|
|
57
|
+
freeProverCtx(ctx: CtxHandle): void;
|
|
58
|
+
createVerifyCtx(proof: Uint8Array, method: string, argsJson: string, chainId: bigint, trustedCheckpoint: string | null, witnessKeys: string | null, flags: number, minLatestBlockTs: bigint): CtxHandle;
|
|
59
|
+
verifyProof(ctx: CtxHandle): RuntimeStatus;
|
|
60
|
+
freeVerifyCtx(ctx: CtxHandle): void;
|
|
61
|
+
createRpcCtx(method: string, paramsJson: string, chainId: bigint, proverFlags: number, verifyFlags: number, proverMode: number): CtxHandle;
|
|
62
|
+
executeRpcCtx(ctx: CtxHandle): RuntimeStatus;
|
|
63
|
+
freeRpcCtx(ctx: CtxHandle): void;
|
|
64
|
+
rpcCtxSetProxyUrls(ctx: CtxHandle, rpcUrls: string, beaconUrls: string): void;
|
|
65
|
+
rpcCtxSetWitnessKeys(ctx: CtxHandle, keys: string): void;
|
|
66
|
+
rpcCtxSetMinLatestBlockTs(ctx: CtxHandle, ts: bigint): void;
|
|
67
|
+
setCheckpoint(chainId: bigint, checkpoint: string): void;
|
|
68
|
+
reqSetResponse(req: DataRequest, data: Uint8Array, nodeIndex: number): void;
|
|
69
|
+
reqSetError(req: DataRequest, error: string, nodeIndex: number): void;
|
|
70
|
+
decodeProof(proof: Uint8Array): any;
|
|
71
|
+
registerStorage(storage: Storage): void;
|
|
72
|
+
}
|
|
73
|
+
export type RuntimeProvider = () => Promise<C4Runtime>;
|
|
74
|
+
/**
|
|
75
|
+
* Replaces the runtime provider (e.g. native addon in Node.js).
|
|
76
|
+
*
|
|
77
|
+
* Intended to be called once by the entry point before the first
|
|
78
|
+
* `getRuntime()` use. Calling it later resets the cached runtime, so the
|
|
79
|
+
* next `getRuntime()` initializes the new provider - context handles
|
|
80
|
+
* created by the previous runtime must not be used afterwards.
|
|
81
|
+
* @param provider Factory returning an initialized runtime
|
|
82
|
+
*/
|
|
83
|
+
export declare function setRuntimeProvider(provider: RuntimeProvider): void;
|
|
84
|
+
/**
|
|
85
|
+
* Returns the active runtime (initialized lazily, cached afterwards).
|
|
86
|
+
* @return The active C4Runtime instance
|
|
87
|
+
*/
|
|
88
|
+
export declare function getRuntime(): Promise<C4Runtime>;
|
package/runtime.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 corpus.core
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
* this software and associated documentation files (the "Software"), to deal in
|
|
6
|
+
* the Software without restriction, including without limitation the rights to
|
|
7
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
* subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
17
|
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
18
|
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
19
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
*
|
|
21
|
+
* SPDX-License-Identifier: MIT
|
|
22
|
+
*/
|
|
23
|
+
// The active provider defaults to the WASM runtime and is replaced by the
|
|
24
|
+
// Node entry point (`index.node.ts`) with the native-first provider.
|
|
25
|
+
// Lazy import keeps browser bundles free of unused code paths.
|
|
26
|
+
let activeProvider = async () => (await import('./runtime_wasm.js')).getWasmRuntime();
|
|
27
|
+
let cached = null;
|
|
28
|
+
/**
|
|
29
|
+
* Replaces the runtime provider (e.g. native addon in Node.js).
|
|
30
|
+
*
|
|
31
|
+
* Intended to be called once by the entry point before the first
|
|
32
|
+
* `getRuntime()` use. Calling it later resets the cached runtime, so the
|
|
33
|
+
* next `getRuntime()` initializes the new provider - context handles
|
|
34
|
+
* created by the previous runtime must not be used afterwards.
|
|
35
|
+
* @param provider Factory returning an initialized runtime
|
|
36
|
+
*/
|
|
37
|
+
export function setRuntimeProvider(provider) {
|
|
38
|
+
activeProvider = provider;
|
|
39
|
+
cached = null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the active runtime (initialized lazily, cached afterwards).
|
|
43
|
+
* @return The active C4Runtime instance
|
|
44
|
+
*/
|
|
45
|
+
export function getRuntime() {
|
|
46
|
+
if (!cached)
|
|
47
|
+
cached = activeProvider();
|
|
48
|
+
return cached;
|
|
49
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 corpus.core
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
* this software and associated documentation files (the "Software"), to deal in
|
|
6
|
+
* the Software without restriction, including without limitation the rights to
|
|
7
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
* subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
17
|
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
18
|
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
19
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
*
|
|
21
|
+
* SPDX-License-Identifier: MIT
|
|
22
|
+
*/
|
|
23
|
+
import type { C4Runtime } from './runtime.js';
|
|
24
|
+
/**
|
|
25
|
+
* Returns the (cached) Node runtime: native addon if available, otherwise WASM.
|
|
26
|
+
* @return Initialized runtime
|
|
27
|
+
*/
|
|
28
|
+
export declare function getNodeRuntime(): Promise<C4Runtime>;
|
package/runtime_node.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 corpus.core
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
* this software and associated documentation files (the "Software"), to deal in
|
|
6
|
+
* the Software without restriction, including without limitation the rights to
|
|
7
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
* subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
17
|
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
18
|
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
19
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
*
|
|
21
|
+
* SPDX-License-Identifier: MIT
|
|
22
|
+
*/
|
|
23
|
+
import { get_default_storage } from './wasm_shared.js';
|
|
24
|
+
import { module_dir } from './wasm.js';
|
|
25
|
+
async function loadNativeBinding() {
|
|
26
|
+
const { createRequire } = await import('node:module');
|
|
27
|
+
const { join, dirname } = await import('node:path');
|
|
28
|
+
const fs = await import('node:fs');
|
|
29
|
+
// Directory of the compiled JS: resolved by wasm.ts (ESM: import.meta.url)
|
|
30
|
+
// or wasm_cjs.ts (CJS: __dirname), which are swapped at build time.
|
|
31
|
+
const dir = await module_dir();
|
|
32
|
+
const requireNative = createRequire(join(dir, 'noop.js'));
|
|
33
|
+
const candidates = [];
|
|
34
|
+
if (process.env.C4_NATIVE_ADDON)
|
|
35
|
+
candidates.push(process.env.C4_NATIVE_ADDON);
|
|
36
|
+
const target = `${process.platform}-${process.arch}`;
|
|
37
|
+
// The CJS build lives in a `cjs/` subfolder; prebuilds sit at the package root.
|
|
38
|
+
candidates.push(join(dir, 'prebuilds', target, 'colibri_native.node'));
|
|
39
|
+
candidates.push(join(dirname(dir), 'prebuilds', target, 'colibri_native.node'));
|
|
40
|
+
for (const candidate of candidates) {
|
|
41
|
+
if (fs.existsSync(candidate))
|
|
42
|
+
return requireNative(candidate);
|
|
43
|
+
}
|
|
44
|
+
throw new Error(`no native colibri addon found for ${target} (checked: ${candidates.join(', ')})`);
|
|
45
|
+
}
|
|
46
|
+
function createNativeRuntime(binding) {
|
|
47
|
+
// Most methods are direct pass-throughs (N-API functions don't need `this`);
|
|
48
|
+
// only status parsing and the req_ptr string conversion add logic.
|
|
49
|
+
return {
|
|
50
|
+
kind: 'native',
|
|
51
|
+
getMethodType: binding.getMethodType,
|
|
52
|
+
createProverCtx: binding.createProverCtx,
|
|
53
|
+
executeProverCtx(ctx) {
|
|
54
|
+
const state = JSON.parse(binding.executeProverCtx(ctx));
|
|
55
|
+
// The JSON status carries the proof as native pointer (unusable in JS);
|
|
56
|
+
// fetch the bytes through the dedicated accessor instead.
|
|
57
|
+
if (state.status === 'success')
|
|
58
|
+
state.result = binding.getProof(ctx);
|
|
59
|
+
return state;
|
|
60
|
+
},
|
|
61
|
+
freeProverCtx: binding.freeProverCtx,
|
|
62
|
+
createVerifyCtx: binding.createVerifyCtx,
|
|
63
|
+
verifyProof: (ctx) => JSON.parse(binding.verifyProof(ctx)),
|
|
64
|
+
freeVerifyCtx: binding.freeVerifyCtx,
|
|
65
|
+
createRpcCtx: binding.createRpcCtx,
|
|
66
|
+
executeRpcCtx: (ctx) => JSON.parse(binding.executeRpcCtx(ctx)),
|
|
67
|
+
freeRpcCtx: binding.freeRpcCtx,
|
|
68
|
+
rpcCtxSetProxyUrls: binding.rpcCtxSetProxyUrls,
|
|
69
|
+
rpcCtxSetWitnessKeys: binding.rpcCtxSetWitnessKeys,
|
|
70
|
+
rpcCtxSetMinLatestBlockTs: binding.rpcCtxSetMinLatestBlockTs,
|
|
71
|
+
setCheckpoint: binding.setCheckpoint,
|
|
72
|
+
reqSetResponse: (req, data, nodeIndex) => binding.reqSetResponse(String(req.req_ptr), data, nodeIndex),
|
|
73
|
+
reqSetError: (req, error, nodeIndex) => binding.reqSetError(String(req.req_ptr), error, nodeIndex),
|
|
74
|
+
decodeProof(proof) {
|
|
75
|
+
const json = binding.decodeProof(proof);
|
|
76
|
+
if (!json)
|
|
77
|
+
throw new Error('Unknown proof format');
|
|
78
|
+
return JSON.parse(json);
|
|
79
|
+
},
|
|
80
|
+
registerStorage: binding.registerStorage,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
let runtime = null;
|
|
84
|
+
async function initNodeRuntime() {
|
|
85
|
+
if (process.env.C4_DISABLE_NATIVE !== '1') {
|
|
86
|
+
try {
|
|
87
|
+
const binding = await loadNativeBinding();
|
|
88
|
+
const rt = createNativeRuntime(binding);
|
|
89
|
+
// Same default persistence behavior as the WASM runtime (fs-based in Node).
|
|
90
|
+
rt.registerStorage(await get_default_storage());
|
|
91
|
+
return rt;
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
if (process.env.C4_FORCE_NATIVE === '1')
|
|
95
|
+
throw e;
|
|
96
|
+
if (process.env.C4_DEBUG_NATIVE === '1')
|
|
97
|
+
console.warn('colibri: native addon unavailable, falling back to WASM:', e);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return (await import('./runtime_wasm.js')).getWasmRuntime();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Returns the (cached) Node runtime: native addon if available, otherwise WASM.
|
|
104
|
+
* @return Initialized runtime
|
|
105
|
+
*/
|
|
106
|
+
export function getNodeRuntime() {
|
|
107
|
+
if (!runtime)
|
|
108
|
+
runtime = initNodeRuntime();
|
|
109
|
+
return runtime;
|
|
110
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 corpus.core
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
* this software and associated documentation files (the "Software"), to deal in
|
|
6
|
+
* the Software without restriction, including without limitation the rights to
|
|
7
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
* subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
17
|
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
18
|
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
19
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
*
|
|
21
|
+
* SPDX-License-Identifier: MIT
|
|
22
|
+
*/
|
|
23
|
+
import type { C4Runtime } from './runtime.js';
|
|
24
|
+
/**
|
|
25
|
+
* Returns the (cached) WASM-backed runtime.
|
|
26
|
+
* @return Initialized WASM runtime
|
|
27
|
+
*/
|
|
28
|
+
export declare function getWasmRuntime(): Promise<C4Runtime>;
|
package/runtime_wasm.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 corpus.core
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
* this software and associated documentation files (the "Software"), to deal in
|
|
6
|
+
* the Software without restriction, including without limitation the rights to
|
|
7
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
* subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
17
|
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
18
|
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
19
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
*
|
|
21
|
+
* SPDX-License-Identifier: MIT
|
|
22
|
+
*/
|
|
23
|
+
import { getC4w } from './wasm.js';
|
|
24
|
+
import { as_bytes, as_char_ptr, as_json, copy_to_c } from './wasm_shared.js';
|
|
25
|
+
// The WASM runtime wraps the pointer-level Emscripten API (`C4W`) behind the
|
|
26
|
+
// value-level `C4Runtime` interface. The module is loaded once via `getC4w()`.
|
|
27
|
+
// Note: the historical c4w_* exports say "proof_ctx" where the core C API
|
|
28
|
+
// (and this interface) say "prover" (c4_prover_create etc.).
|
|
29
|
+
function createRuntime(c4w) {
|
|
30
|
+
return {
|
|
31
|
+
kind: 'wasm',
|
|
32
|
+
getMethodType(chainId, method, paramsJson, flags) {
|
|
33
|
+
const free_buffers = [];
|
|
34
|
+
const type = c4w._c4w_get_method_type(chainId, as_char_ptr(method, c4w, free_buffers), paramsJson ? as_char_ptr(paramsJson, c4w, free_buffers) : 0, flags);
|
|
35
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
36
|
+
return type;
|
|
37
|
+
},
|
|
38
|
+
createProverCtx(method, argsJson, chainId, flags) {
|
|
39
|
+
const free_buffers = [];
|
|
40
|
+
const ctx = c4w._c4w_create_proof_ctx(as_char_ptr(method, c4w, free_buffers), as_char_ptr(argsJson, c4w, free_buffers), chainId, flags);
|
|
41
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
42
|
+
return ctx;
|
|
43
|
+
},
|
|
44
|
+
executeProverCtx(ctx) {
|
|
45
|
+
const state = as_json(c4w._c4w_execute_proof_ctx(ctx), c4w, true);
|
|
46
|
+
// On success the JSON carries the proof as heap pointer + length
|
|
47
|
+
// (owned by the prover ctx); materialize it as bytes.
|
|
48
|
+
if (state.status === 'success')
|
|
49
|
+
state.result = as_bytes(state.result, state.result_len, c4w);
|
|
50
|
+
return state;
|
|
51
|
+
},
|
|
52
|
+
freeProverCtx(ctx) {
|
|
53
|
+
c4w._c4w_free_proof_ctx(ctx);
|
|
54
|
+
},
|
|
55
|
+
createVerifyCtx(proof, method, argsJson, chainId, trustedCheckpoint, witnessKeys, flags, minLatestBlockTs) {
|
|
56
|
+
const free_buffers = [];
|
|
57
|
+
const ctx = c4w._c4w_create_verify_ctx(copy_to_c(proof, c4w, free_buffers), proof.length, as_char_ptr(method, c4w, free_buffers), as_char_ptr(argsJson, c4w, free_buffers), chainId, trustedCheckpoint ? as_char_ptr(trustedCheckpoint, c4w, free_buffers) : 0, witnessKeys ? as_char_ptr(witnessKeys, c4w, free_buffers) : 0, flags, minLatestBlockTs);
|
|
58
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
59
|
+
return ctx;
|
|
60
|
+
},
|
|
61
|
+
verifyProof(ctx) {
|
|
62
|
+
return as_json(c4w._c4w_verify_proof(ctx), c4w, true);
|
|
63
|
+
},
|
|
64
|
+
freeVerifyCtx(ctx) {
|
|
65
|
+
c4w._c4w_free_verify_ctx(ctx);
|
|
66
|
+
},
|
|
67
|
+
createRpcCtx(method, paramsJson, chainId, proverFlags, verifyFlags, proverMode) {
|
|
68
|
+
const free_buffers = [];
|
|
69
|
+
const ctx = c4w._c4w_create_rpc_ctx(as_char_ptr(method, c4w, free_buffers), as_char_ptr(paramsJson, c4w, free_buffers), chainId, proverFlags, verifyFlags, proverMode);
|
|
70
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
71
|
+
return ctx;
|
|
72
|
+
},
|
|
73
|
+
executeRpcCtx(ctx) {
|
|
74
|
+
return as_json(c4w._c4w_execute_rpc_ctx(ctx), c4w, true);
|
|
75
|
+
},
|
|
76
|
+
freeRpcCtx(ctx) {
|
|
77
|
+
c4w._c4w_free_rpc_ctx(ctx);
|
|
78
|
+
},
|
|
79
|
+
rpcCtxSetProxyUrls(ctx, rpcUrls, beaconUrls) {
|
|
80
|
+
const free_buffers = [];
|
|
81
|
+
c4w._c4w_rpc_ctx_set_proxy_urls(ctx, as_char_ptr(rpcUrls, c4w, free_buffers), as_char_ptr(beaconUrls, c4w, free_buffers));
|
|
82
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
83
|
+
},
|
|
84
|
+
rpcCtxSetWitnessKeys(ctx, keys) {
|
|
85
|
+
const free_buffers = [];
|
|
86
|
+
c4w._c4w_rpc_ctx_set_witness_keys(ctx, as_char_ptr(keys, c4w, free_buffers));
|
|
87
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
88
|
+
},
|
|
89
|
+
rpcCtxSetMinLatestBlockTs(ctx, ts) {
|
|
90
|
+
c4w._c4w_rpc_ctx_set_min_latest_block_ts(ctx, ts);
|
|
91
|
+
},
|
|
92
|
+
setCheckpoint(chainId, checkpoint) {
|
|
93
|
+
const free_buffers = [];
|
|
94
|
+
c4w._c4w_set_checkpoint(chainId, as_char_ptr(checkpoint, c4w, free_buffers));
|
|
95
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
96
|
+
},
|
|
97
|
+
reqSetResponse(req, data, nodeIndex) {
|
|
98
|
+
// ownership of the copied bytes is transferred to the C context
|
|
99
|
+
c4w._c4w_req_set_response(req.req_ptr, copy_to_c(data, c4w), data.length, nodeIndex);
|
|
100
|
+
},
|
|
101
|
+
reqSetError(req, error, nodeIndex) {
|
|
102
|
+
const free_buffers = [];
|
|
103
|
+
c4w._c4w_req_set_error(req.req_ptr, as_char_ptr(error, c4w, free_buffers), nodeIndex);
|
|
104
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
105
|
+
},
|
|
106
|
+
decodeProof(proof) {
|
|
107
|
+
const free_buffers = [];
|
|
108
|
+
const ptr = copy_to_c(proof, c4w, free_buffers);
|
|
109
|
+
const resultPtr = c4w._c4w_decode_proof(ptr, proof.length);
|
|
110
|
+
free_buffers.forEach(p => c4w._free(p));
|
|
111
|
+
if (!resultPtr)
|
|
112
|
+
throw new Error('Unknown proof format');
|
|
113
|
+
return as_json(resultPtr, c4w, true);
|
|
114
|
+
},
|
|
115
|
+
registerStorage(storage) {
|
|
116
|
+
c4w.storage = storage;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
let runtime = null;
|
|
121
|
+
/**
|
|
122
|
+
* Returns the (cached) WASM-backed runtime.
|
|
123
|
+
* @return Initialized WASM runtime
|
|
124
|
+
*/
|
|
125
|
+
export function getWasmRuntime() {
|
|
126
|
+
if (!runtime)
|
|
127
|
+
runtime = getC4w().then(createRuntime);
|
|
128
|
+
return runtime;
|
|
129
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -131,7 +131,10 @@ export interface DataRequest {
|
|
|
131
131
|
exclude_mask: number;
|
|
132
132
|
url: string;
|
|
133
133
|
payload: any;
|
|
134
|
-
|
|
134
|
+
/** Opaque pointer to the underlying `data_request_t`: a heap address (number)
|
|
135
|
+
* in the WASM runtime, or a decimal string in the native Node.js runtime
|
|
136
|
+
* (64-bit pointers are not safely representable as JS numbers). */
|
|
137
|
+
req_ptr: number | string;
|
|
135
138
|
/** Milliseconds to wait before (re-)executing this request (e.g. oblivious-node retry backoff). Optional. */
|
|
136
139
|
delay?: number;
|
|
137
140
|
/** Cache freshness bound in seconds. When set, forwarded as a `Cache-Control: max-age=<ttl>` request header so a shared cache/CDN never returns a response older than this bound (e.g. short bound for `latest` block proofs). Optional. */
|
package/wasm.d.ts
CHANGED
|
@@ -29,3 +29,10 @@ export declare const getC4w: () => Promise<C4W>;
|
|
|
29
29
|
export declare const get_prover_config_hex: (chainId: number) => Promise<string>;
|
|
30
30
|
export declare const set_trusted_checkpoint: (chainId: number, checkpoint: string) => Promise<void>;
|
|
31
31
|
export declare const decode_proof: (proof: Uint8Array) => Promise<any>;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the directory of the compiled module. Used in Node to locate
|
|
34
|
+
* packaged assets (e.g. native addon prebuilds) relative to the package root.
|
|
35
|
+
* The CJS build swaps this file with `wasm_cjs.ts`, which uses `__dirname`.
|
|
36
|
+
* @return Absolute directory path of this module
|
|
37
|
+
*/
|
|
38
|
+
export declare function module_dir(): Promise<string>;
|
package/wasm.js
CHANGED
|
@@ -60,3 +60,14 @@ export const getC4w = api.getC4w;
|
|
|
60
60
|
export const get_prover_config_hex = api.get_prover_config_hex;
|
|
61
61
|
export const set_trusted_checkpoint = api.set_trusted_checkpoint;
|
|
62
62
|
export const decode_proof = api.decode_proof;
|
|
63
|
+
/**
|
|
64
|
+
* Returns the directory of the compiled module. Used in Node to locate
|
|
65
|
+
* packaged assets (e.g. native addon prebuilds) relative to the package root.
|
|
66
|
+
* The CJS build swaps this file with `wasm_cjs.ts`, which uses `__dirname`.
|
|
67
|
+
* @return Absolute directory path of this module
|
|
68
|
+
*/
|
|
69
|
+
export async function module_dir() {
|
|
70
|
+
const { fileURLToPath } = await new Function('return import("node:url")')();
|
|
71
|
+
const { dirname } = await new Function('return import("node:path")')();
|
|
72
|
+
return dirname(fileURLToPath(import.meta.url));
|
|
73
|
+
}
|
package/wasm_cjs.d.ts
CHANGED
|
@@ -29,3 +29,10 @@ export declare const getC4w: () => Promise<C4W>;
|
|
|
29
29
|
export declare const get_prover_config_hex: (chainId: number) => Promise<string>;
|
|
30
30
|
export declare const set_trusted_checkpoint: (chainId: number, checkpoint: string) => Promise<void>;
|
|
31
31
|
export declare const decode_proof: (proof: Uint8Array) => Promise<any>;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the directory of the compiled module. Used in Node to locate
|
|
34
|
+
* packaged assets (e.g. native addon prebuilds) relative to the package root.
|
|
35
|
+
* This CJS variant replaces `wasm.ts` (ESM, `import.meta.url`) at build time.
|
|
36
|
+
* @return Absolute directory path of this module
|
|
37
|
+
*/
|
|
38
|
+
export declare function module_dir(): Promise<string>;
|
package/wasm_cjs.js
CHANGED
|
@@ -57,3 +57,12 @@ export const getC4w = api.getC4w;
|
|
|
57
57
|
export const get_prover_config_hex = api.get_prover_config_hex;
|
|
58
58
|
export const set_trusted_checkpoint = api.set_trusted_checkpoint;
|
|
59
59
|
export const decode_proof = api.decode_proof;
|
|
60
|
+
/**
|
|
61
|
+
* Returns the directory of the compiled module. Used in Node to locate
|
|
62
|
+
* packaged assets (e.g. native addon prebuilds) relative to the package root.
|
|
63
|
+
* This CJS variant replaces `wasm.ts` (ESM, `import.meta.url`) at build time.
|
|
64
|
+
* @return Absolute directory path of this module
|
|
65
|
+
*/
|
|
66
|
+
export async function module_dir() {
|
|
67
|
+
return __dirname;
|
|
68
|
+
}
|