@corpus-core/colibri-stateless 1.1.1 → 1.1.2

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,59 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2025 corpus.core
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ * this software and associated documentation files (the "Software"), to deal in
7
+ * the Software without restriction, including without limitation the rights to
8
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ * the Software, and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ *
22
+ * SPDX-License-Identifier: MIT
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.set_trusted_checkpoint = exports.get_prover_config_hex = exports.getC4w = exports.loadC4WModule = exports.set_wasm_url = exports.get_default_storage = exports.copy_to_c = exports.as_json = exports.as_char_ptr = exports.as_bytes = void 0;
26
+ // This file is used only for the CommonJS build output. It must not use
27
+ // `import.meta` because that is a parse error in CommonJS environments (e.g. Jest).
28
+ const node_path_1 = require("node:path");
29
+ const node_url_1 = require("node:url");
30
+ const wasm_shared_js_1 = require("./wasm_shared.js");
31
+ Object.defineProperty(exports, "as_bytes", { enumerable: true, get: function () { return wasm_shared_js_1.as_bytes; } });
32
+ Object.defineProperty(exports, "as_char_ptr", { enumerable: true, get: function () { return wasm_shared_js_1.as_char_ptr; } });
33
+ Object.defineProperty(exports, "as_json", { enumerable: true, get: function () { return wasm_shared_js_1.as_json; } });
34
+ Object.defineProperty(exports, "copy_to_c", { enumerable: true, get: function () { return wasm_shared_js_1.copy_to_c; } });
35
+ Object.defineProperty(exports, "get_default_storage", { enumerable: true, get: function () { return wasm_shared_js_1.get_default_storage; } });
36
+ async function dynamicImport(specifier) {
37
+ // Prevent TypeScript from transforming `import()` into `require()` in CJS output.
38
+ // Node supports dynamic import in CommonJS.
39
+ const importer = new Function('p', 'return import(p)');
40
+ return importer(specifier);
41
+ }
42
+ const api = (0, wasm_shared_js_1.createC4wApi)({
43
+ importC4wModule: async () => {
44
+ const pkgRoot = (0, node_path_1.join)(__dirname, '..');
45
+ const glueUrl = (0, node_url_1.pathToFileURL)((0, node_path_1.join)(pkgRoot, 'c4w.js')).href;
46
+ return await dynamicImport(glueUrl);
47
+ },
48
+ resolveWasmLocation: (override) => {
49
+ if (override)
50
+ return override;
51
+ const pkgRoot = (0, node_path_1.join)(__dirname, '..');
52
+ return (0, node_path_1.join)(pkgRoot, 'c4w.wasm');
53
+ },
54
+ });
55
+ exports.set_wasm_url = api.set_wasm_url;
56
+ exports.loadC4WModule = api.loadC4WModule;
57
+ exports.getC4w = api.getC4w;
58
+ exports.get_prover_config_hex = api.get_prover_config_hex;
59
+ exports.set_trusted_checkpoint = api.set_trusted_checkpoint;
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2025 corpus.core
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ * this software and associated documentation files (the "Software"), to deal in
7
+ * the Software without restriction, including without limitation the rights to
8
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ * the Software, and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ *
22
+ * SPDX-License-Identifier: MIT
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.isNodeEnvironment = isNodeEnvironment;
26
+ exports.isBrowserEnvironment = isBrowserEnvironment;
27
+ exports.get_default_storage = get_default_storage;
28
+ exports.as_char_ptr = as_char_ptr;
29
+ exports.as_json = as_json;
30
+ exports.as_bytes = as_bytes;
31
+ exports.copy_to_c = copy_to_c;
32
+ exports.createC4wApi = createC4wApi;
33
+ function isNodeEnvironment() {
34
+ return (typeof process !== 'undefined' && process.versions != null && process.versions.node != null);
35
+ }
36
+ function isBrowserEnvironment() {
37
+ return (typeof window !== 'undefined' && typeof document !== 'undefined');
38
+ }
39
+ async function get_default_storage() {
40
+ if (isBrowserEnvironment())
41
+ // web interface
42
+ return {
43
+ get: (key) => {
44
+ const value = window.localStorage.getItem(key);
45
+ if (value) {
46
+ const length = value.length / 2;
47
+ const uint8Array = new Uint8Array(length);
48
+ for (let i = 0; i < length; i++) {
49
+ uint8Array[i] = parseInt(value.substr(i * 2, 2), 16);
50
+ }
51
+ return uint8Array;
52
+ }
53
+ return null;
54
+ },
55
+ set: (key, value) => {
56
+ window.localStorage.setItem(key, Array.from(value).map(_ => _.toString(16).padStart(2, '0')).join(''));
57
+ },
58
+ del: (key) => {
59
+ window.localStorage.removeItem(key);
60
+ },
61
+ };
62
+ else if (isNodeEnvironment()) {
63
+ const fs = await Promise.resolve().then(() => require('fs'));
64
+ // node interface
65
+ return {
66
+ get: (key) => {
67
+ try {
68
+ return fs.readFileSync(key);
69
+ }
70
+ catch (e) {
71
+ return null;
72
+ }
73
+ },
74
+ set: (key, value) => {
75
+ fs.writeFileSync(key, value);
76
+ },
77
+ del: (key) => {
78
+ fs.unlinkSync(key);
79
+ },
80
+ };
81
+ }
82
+ throw new Error('Unsupported environment');
83
+ }
84
+ function as_char_ptr(str, c4w, free_ptrs) {
85
+ const ptr = c4w._malloc(str.length + 1);
86
+ c4w.stringToUTF8(str, ptr, str.length + 1);
87
+ if (free_ptrs)
88
+ free_ptrs.push(ptr);
89
+ return ptr;
90
+ }
91
+ function as_json(ptr, c4w, free_ptrs) {
92
+ const str = c4w.UTF8ToString(ptr);
93
+ if (free_ptrs) {
94
+ if (Array.isArray(free_ptrs))
95
+ free_ptrs.push(ptr);
96
+ else
97
+ c4w._free(ptr);
98
+ }
99
+ try {
100
+ return JSON.parse(str);
101
+ }
102
+ catch (e) {
103
+ console.error(e);
104
+ console.error(str);
105
+ return null;
106
+ }
107
+ }
108
+ function as_bytes(ptr, len, c4w, free_ptrs) {
109
+ const bytes = new Uint8Array(len);
110
+ bytes.set(c4w.HEAPU8.subarray(ptr, ptr + len));
111
+ if (free_ptrs) {
112
+ if (Array.isArray(free_ptrs))
113
+ free_ptrs.push(ptr);
114
+ else
115
+ c4w._free(ptr);
116
+ }
117
+ return bytes;
118
+ }
119
+ function copy_to_c(data, c4w, free_ptrs) {
120
+ const ptr = c4w._malloc(data.length + 1);
121
+ c4w.HEAPU8.set(data, ptr);
122
+ c4w.HEAPU8[ptr + data.length] = 0;
123
+ if (free_ptrs)
124
+ free_ptrs.push(ptr);
125
+ return ptr;
126
+ }
127
+ function createC4wApi(options) {
128
+ let wasmUrlOverride = null;
129
+ let moduleInstance = null;
130
+ let modulePromise = null;
131
+ function set_wasm_url(url) {
132
+ wasmUrlOverride = url;
133
+ }
134
+ async function loadC4WModule() {
135
+ const module = await options.importC4wModule();
136
+ const args = {};
137
+ const wasmLocation = options.resolveWasmLocation(wasmUrlOverride);
138
+ if (wasmLocation) {
139
+ args.locateFile = (path) => path.endsWith('.wasm') ? wasmLocation : path;
140
+ }
141
+ return module.default(args);
142
+ }
143
+ async function getC4w() {
144
+ if (moduleInstance)
145
+ return moduleInstance;
146
+ if (!modulePromise) {
147
+ modulePromise = loadC4WModule().then(async (loadedModule) => {
148
+ moduleInstance = loadedModule;
149
+ moduleInstance.storage = await get_default_storage();
150
+ moduleInstance._init_storage();
151
+ modulePromise = null;
152
+ return moduleInstance;
153
+ });
154
+ }
155
+ return modulePromise;
156
+ }
157
+ /**
158
+ * Returns the prover config state for a given chain as hex string with 0x prefix.
159
+ * @param chainId Chain identifier
160
+ * @return Hex string (e.g. "0x...") or "0x" if no state present
161
+ */
162
+ async function get_prover_config_hex(chainId) {
163
+ const c4w = await getC4w();
164
+ if (!c4w.storage)
165
+ return '0x';
166
+ const state = c4w.storage.get('states_' + chainId);
167
+ return '0x' + (state ? Array.from(state).map(_ => _.toString(16).padStart(2, '0')).join('') : '');
168
+ }
169
+ /**
170
+ * Sets the trusted checkpoint inside the C context to initialize state.
171
+ * @param chainId Chain identifier
172
+ * @param checkpoint Trusted checkpoint root hex string
173
+ */
174
+ async function set_trusted_checkpoint(chainId, checkpoint) {
175
+ const c4w = await getC4w();
176
+ const free_buffers = [];
177
+ c4w._c4w_create_verify_ctx(0, 0, 0, 0, BigInt(chainId), as_char_ptr(checkpoint, c4w, free_buffers), 0);
178
+ free_buffers.forEach(ptr => c4w._free(ptr));
179
+ }
180
+ return {
181
+ set_wasm_url,
182
+ loadC4WModule,
183
+ getC4w,
184
+ get_prover_config_hex,
185
+ set_trusted_checkpoint,
186
+ };
187
+ }
package/index.d.ts CHANGED
@@ -25,6 +25,7 @@ import { ProviderRpcError, RequestArguments, Cache, Config as C4Config, DataRequ
25
25
  import { EthSubscribeSubscriptionType, EthNewFilterType } from './subscriptionManager.js';
26
26
  import Strategy from './strategy.js';
27
27
  export { Strategy };
28
+ export { set_wasm_url } from "./wasm.js";
28
29
  export { ProviderRpcError, RequestArguments, Cache, C4Config, DataRequest, C4MethodType as MethodType, ProviderMessage, EthSubscribeSubscriptionType, EthNewFilterType };
29
30
  export { TransactionVerifier, PrototypeProtection } from './transactionVerifier.js';
30
31
  export default class C4Client {
package/index.js CHANGED
@@ -31,6 +31,8 @@ import Strategy from './strategy.js';
31
31
  import { TransactionVerifier, PrototypeProtection } from './transactionVerifier.js';
32
32
  import { fetch_rpc, handle_request } from './http.js';
33
33
  export { Strategy };
34
+ // Public helper for controlling WASM loading behavior in Node/bundlers.
35
+ export { set_wasm_url } from "./wasm.js";
34
36
  // Re-export types needed by consumers of the C4Client module
35
37
  export { ProviderRpcError, C4MethodType as MethodType };
36
38
  // Re-export transaction verification utilities
package/package.json CHANGED
@@ -1,9 +1,17 @@
1
1
  {
2
2
  "name": "@corpus-core/colibri-stateless",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Colibri is a stateless and trustless ethereum client, which is optimized for the mobile apps or embedded devices, because it does not hols the state, but verifies on demand.",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
+ "types": "index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "import": "./index.js",
12
+ "require": "./cjs/index.js"
13
+ }
14
+ },
7
15
  "scripts": {
8
16
  "test": "npm run test:node && npm run test:webpack",
9
17
  "test:node": "node --test test/*.test.mjs",
@@ -43,10 +51,6 @@
43
51
  },
44
52
  "homepage": "https://github.com/corpus-core/c4#readme",
45
53
  "dependencies": {},
46
- "devDependencies": {
47
- "@types/node": "^22.13.1",
48
- "typescript": "^5.7.3"
49
- },
50
54
  "publishConfig": {
51
55
  "access": "public"
52
56
  }
package/wasm.d.ts CHANGED
@@ -20,55 +20,11 @@
20
20
  *
21
21
  * SPDX-License-Identifier: MIT
22
22
  */
23
- export interface C4W {
24
- _c4w_create_proof_ctx: (method: number, args: number, chainId: bigint, flags: number) => number;
25
- _c4w_free_proof_ctx: (proofCtx: number) => void;
26
- _c4w_execute_proof_ctx: (proofCtx: number) => number;
27
- _c4w_get_pending_data_request: (proofCtx: number) => number;
28
- _c4w_req_set_response: (reqPtr: number, data: number, len: number, node_index: number) => void;
29
- _c4w_req_set_error: (reqPtr: number, error: number, node_index: number) => void;
30
- _c4w_get_method_type: (chain_id: bigint, method: number) => number;
31
- _c4w_create_verify_ctx: (proof: number, proof_len: number, method: number, args: number, chain_id: bigint, trusted_checkpoint: number, witness_keys: number) => number;
32
- _c4w_free_verify_ctx: (verifyCtx: number) => void;
33
- _c4w_verify_proof: (verifyCtx: number) => number;
34
- _c4w_req_free: (reqPtr: number) => void;
35
- _init_storage: () => void;
36
- HEAPU8: Uint8Array;
37
- stringToUTF8: (str: string, ptr: number, length: number) => number;
38
- UTF8ToString: (ptr: number) => string;
39
- _malloc: (size: number) => number;
40
- _free: (ptr: number) => void;
41
- storage: Storage;
42
- }
43
- export interface Storage {
44
- get: (key: string) => Uint8Array | null;
45
- set: (key: string, value: Uint8Array) => void;
46
- del: (key: string) => void;
47
- }
48
- export type C4WModule = {
49
- then: (cb: (mod: C4W) => void) => void;
50
- };
51
- /**
52
- * Optionally override the URL/path used to load the WASM binary when not embedded.
53
- * @param url Absolute/relative URL in browsers, or filesystem path in Node.
54
- */
55
- export declare function set_wasm_url(url: string): void;
56
- export declare function loadC4WModule(): Promise<C4W>;
57
- export declare function getC4w(): Promise<C4W>;
58
- export declare function get_default_storage(): Promise<Storage>;
59
- export declare function as_char_ptr(str: string, c4w: C4W, free_ptrs?: number[]): number;
60
- export declare function as_json(ptr: number, c4w: C4W, free_ptrs?: number[] | boolean): any;
61
- export declare function as_bytes(ptr: number, len: number, c4w: C4W, free_ptrs?: number[] | boolean): Uint8Array;
62
- export declare function copy_to_c(data: Uint8Array, c4w: C4W, free_ptrs?: number[]): number;
63
- /**
64
- * Returns the prover config state for a given chain as hex string with 0x prefix.
65
- * @param chainId Chain identifier
66
- * @return Hex string (e.g. "0x...") or "0x" if no state present
67
- */
68
- export declare function get_prover_config_hex(chainId: number): Promise<string>;
69
- /**
70
- * Sets the trusted checkpoint inside the C context to initialize state.
71
- * @param chainId Chain identifier
72
- * @param checkpoint Trusted checkpoint root hex string
73
- */
74
- export declare function set_trusted_checkpoint(chainId: number, checkpoint: string): Promise<void>;
23
+ import { as_bytes, as_char_ptr, as_json, copy_to_c, get_default_storage, type C4W, type C4WModule, type Storage } from './wasm_shared.js';
24
+ export type { C4W, C4WModule, Storage };
25
+ export { as_bytes, as_char_ptr, as_json, copy_to_c, get_default_storage };
26
+ export declare const set_wasm_url: (url: string) => void;
27
+ export declare const loadC4WModule: () => Promise<C4W>;
28
+ export declare const getC4w: () => Promise<C4W>;
29
+ export declare const get_prover_config_hex: (chainId: number) => Promise<string>;
30
+ export declare const set_trusted_checkpoint: (chainId: number, checkpoint: string) => Promise<void>;
package/wasm.js CHANGED
@@ -20,156 +20,31 @@
20
20
  *
21
21
  * SPDX-License-Identifier: MIT
22
22
  */
23
- let wasmUrlOverride = null;
24
- /**
25
- * Optionally override the URL/path used to load the WASM binary when not embedded.
26
- * @param url Absolute/relative URL in browsers, or filesystem path in Node.
27
- */
28
- export function set_wasm_url(url) {
29
- wasmUrlOverride = url;
30
- }
31
- export async function loadC4WModule() {
32
- const module = (await import("./c4w.js"));
33
- const args = {};
34
- if (wasmUrlOverride) {
35
- args.locateFile = (path) => path.endsWith('.wasm') ? wasmUrlOverride : path;
36
- }
37
- else if (isBrowserEnvironment()) {
38
- // Default browser-friendly resolution so bundlers copy the asset without extra config
39
- args.locateFile = (path) => path.endsWith('.wasm') ? new URL('./c4w.wasm', import.meta.url).toString() : path;
40
- }
41
- return module.default(args); // Emscripten initializes the module
42
- }
43
- let module = null;
44
- let modulePromise = null;
45
- export async function getC4w() {
46
- if (module)
47
- return module;
48
- if (!modulePromise) {
49
- modulePromise = loadC4WModule().then(async (loadedModule) => {
50
- module = loadedModule;
51
- module.storage = await get_default_storage();
52
- module._init_storage();
53
- modulePromise = null; // Reset the promise after loading
54
- return module;
55
- });
56
- }
57
- return modulePromise;
58
- }
59
- function isNodeEnvironment() {
60
- return (typeof process !== 'undefined' && process.versions != null && process.versions.node != null);
61
- }
62
- function isBrowserEnvironment() {
63
- return (typeof window !== 'undefined' && typeof document !== 'undefined');
64
- }
65
- export async function get_default_storage() {
66
- if (isBrowserEnvironment())
67
- // web interface
68
- return {
69
- get: (key) => {
70
- const value = window.localStorage.getItem(key);
71
- if (value) {
72
- const length = value.length / 2;
73
- const uint8Array = new Uint8Array(length);
74
- for (let i = 0; i < length; i++) {
75
- uint8Array[i] = parseInt(value.substr(i * 2, 2), 16);
76
- }
77
- return uint8Array;
78
- }
79
- return null;
80
- },
81
- set: (key, value) => {
82
- window.localStorage.setItem(key, Array.from(value).map(_ => _.toString(16).padStart(2, '0')).join(''));
83
- },
84
- del: (key) => {
85
- window.localStorage.removeItem(key);
86
- },
87
- };
88
- else if (isNodeEnvironment()) {
89
- const fs = await import('fs');
90
- // node interface
91
- return {
92
- get: (key) => {
93
- try {
94
- return fs.readFileSync(key);
95
- }
96
- catch (e) {
97
- return null;
98
- }
99
- },
100
- set: (key, value) => {
101
- fs.writeFileSync(key, value);
102
- },
103
- del: (key) => {
104
- fs.unlinkSync(key);
105
- },
106
- };
107
- }
108
- throw new Error('Unsupported environment');
109
- }
110
- export function as_char_ptr(str, c4w, free_ptrs) {
111
- const ptr = c4w._malloc(str.length + 1);
112
- c4w.stringToUTF8(str, ptr, str.length + 1);
113
- if (free_ptrs)
114
- free_ptrs.push(ptr);
115
- return ptr;
116
- }
117
- export function as_json(ptr, c4w, free_ptrs) {
118
- const str = c4w.UTF8ToString(ptr);
119
- if (free_ptrs) {
120
- if (Array.isArray(free_ptrs))
121
- free_ptrs.push(ptr);
122
- else
123
- c4w._free(ptr);
124
- }
125
- try {
126
- return JSON.parse(str);
127
- }
128
- catch (e) {
129
- console.error(e);
130
- console.error(str);
23
+ import { as_bytes, as_char_ptr, as_json, copy_to_c, createC4wApi, get_default_storage, isBrowserEnvironment, isNodeEnvironment, } from './wasm_shared.js';
24
+ export { as_bytes, as_char_ptr, as_json, copy_to_c, get_default_storage };
25
+ const api = createC4wApi({
26
+ importC4wModule: async () => (await import("./c4w.js")),
27
+ resolveWasmLocation: (override) => {
28
+ if (override)
29
+ return override;
30
+ if (isBrowserEnvironment()) {
31
+ // Default browser-friendly resolution so bundlers copy the asset without extra config
32
+ return new URL('./c4w.wasm', import.meta.url).toString();
33
+ }
34
+ if (isNodeEnvironment()) {
35
+ // Node ESM default: resolve the sidecar wasm next to this module.
36
+ // We return a filesystem path (not a file:// URL) because Emscripten's
37
+ // Node loader expects a path for fs.readFileSync().
38
+ const wasmUrl = new URL('./c4w.wasm', import.meta.url);
39
+ if (wasmUrl.protocol !== 'file:')
40
+ return wasmUrl.toString();
41
+ return decodeURIComponent(wasmUrl.pathname);
42
+ }
131
43
  return null;
132
- }
133
- }
134
- export function as_bytes(ptr, len, c4w, free_ptrs) {
135
- const bytes = new Uint8Array(len);
136
- bytes.set(c4w.HEAPU8.subarray(ptr, ptr + len));
137
- if (free_ptrs) {
138
- if (Array.isArray(free_ptrs))
139
- free_ptrs.push(ptr);
140
- else
141
- c4w._free(ptr);
142
- }
143
- return bytes;
144
- }
145
- export function copy_to_c(data, c4w, free_ptrs) {
146
- const ptr = c4w._malloc(data.length + 1);
147
- c4w.HEAPU8.set(data, ptr);
148
- c4w.HEAPU8[ptr + data.length] = 0;
149
- if (free_ptrs)
150
- free_ptrs.push(ptr);
151
- return ptr;
152
- }
153
- /**
154
- * Returns the prover config state for a given chain as hex string with 0x prefix.
155
- * @param chainId Chain identifier
156
- * @return Hex string (e.g. "0x...") or "0x" if no state present
157
- */
158
- export async function get_prover_config_hex(chainId) {
159
- const c4w = await getC4w();
160
- if (!c4w.storage)
161
- return '0x';
162
- const state = c4w.storage.get('states_' + chainId);
163
- return '0x' + (state ? Array.from(state).map(_ => _.toString(16).padStart(2, '0')).join('') : '');
164
- }
165
- /**
166
- * Sets the trusted checkpoint inside the C context to initialize state.
167
- * @param chainId Chain identifier
168
- * @param checkpoint Trusted checkpoint root hex string
169
- */
170
- export async function set_trusted_checkpoint(chainId, checkpoint) {
171
- const c4w = await getC4w();
172
- const free_buffers = [];
173
- c4w._c4w_create_verify_ctx(0, 0, 0, 0, BigInt(chainId), as_char_ptr(checkpoint, c4w, free_buffers), 0);
174
- free_buffers.forEach(ptr => c4w._free(ptr));
175
- }
44
+ },
45
+ });
46
+ export const set_wasm_url = api.set_wasm_url;
47
+ export const loadC4WModule = api.loadC4WModule;
48
+ export const getC4w = api.getC4w;
49
+ export const get_prover_config_hex = api.get_prover_config_hex;
50
+ export const set_trusted_checkpoint = api.set_trusted_checkpoint;
package/wasm_cjs.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Copyright (c) 2025 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 { as_bytes, as_char_ptr, as_json, copy_to_c, get_default_storage, type C4W, type C4WModule, type Storage } from './wasm_shared.js';
24
+ export type { C4W, C4WModule, Storage };
25
+ export { as_bytes, as_char_ptr, as_json, copy_to_c, get_default_storage };
26
+ export declare const set_wasm_url: (url: string) => void;
27
+ export declare const loadC4WModule: () => Promise<C4W>;
28
+ export declare const getC4w: () => Promise<C4W>;
29
+ export declare const get_prover_config_hex: (chainId: number) => Promise<string>;
30
+ export declare const set_trusted_checkpoint: (chainId: number, checkpoint: string) => Promise<void>;
package/wasm_cjs.js ADDED
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Copyright (c) 2025 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
+ // This file is used only for the CommonJS build output. It must not use
24
+ // `import.meta` because that is a parse error in CommonJS environments (e.g. Jest).
25
+ import { join } from 'node:path';
26
+ import { pathToFileURL } from 'node:url';
27
+ import { as_bytes, as_char_ptr, as_json, copy_to_c, createC4wApi, get_default_storage, } from './wasm_shared.js';
28
+ export { as_bytes, as_char_ptr, as_json, copy_to_c, get_default_storage };
29
+ async function dynamicImport(specifier) {
30
+ // Prevent TypeScript from transforming `import()` into `require()` in CJS output.
31
+ // Node supports dynamic import in CommonJS.
32
+ const importer = new Function('p', 'return import(p)');
33
+ return importer(specifier);
34
+ }
35
+ const api = createC4wApi({
36
+ importC4wModule: async () => {
37
+ const pkgRoot = join(__dirname, '..');
38
+ const glueUrl = pathToFileURL(join(pkgRoot, 'c4w.js')).href;
39
+ return await dynamicImport(glueUrl);
40
+ },
41
+ resolveWasmLocation: (override) => {
42
+ if (override)
43
+ return override;
44
+ const pkgRoot = join(__dirname, '..');
45
+ return join(pkgRoot, 'c4w.wasm');
46
+ },
47
+ });
48
+ export const set_wasm_url = api.set_wasm_url;
49
+ export const loadC4WModule = api.loadC4WModule;
50
+ export const getC4w = api.getC4w;
51
+ export const get_prover_config_hex = api.get_prover_config_hex;
52
+ export const set_trusted_checkpoint = api.set_trusted_checkpoint;