@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.
- package/README.md +26 -0
- package/c4w.js +1 -1
- package/c4w.wasm +0 -0
- package/cjs/chains.js +84 -0
- package/cjs/connectionState.js +156 -0
- package/cjs/eventEmitter.js +54 -0
- package/cjs/http.js +172 -0
- package/cjs/index.js +327 -0
- package/cjs/package.json +3 -0
- package/cjs/strategy.js +114 -0
- package/cjs/subscriptionManager.js +314 -0
- package/cjs/transactionVerifier.js +202 -0
- package/cjs/types.js +71 -0
- package/cjs/wasm.js +59 -0
- package/cjs/wasm_cjs.js +59 -0
- package/cjs/wasm_shared.js +187 -0
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/package.json +9 -5
- package/wasm.d.ts +8 -52
- package/wasm.js +27 -152
- package/wasm_cjs.d.ts +30 -0
- package/wasm_cjs.js +52 -0
- package/wasm_shared.d.ts +67 -0
- package/wasm_shared.js +177 -0
package/wasm_shared.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
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
|
+
export declare function isNodeEnvironment(): boolean;
|
|
52
|
+
export declare function isBrowserEnvironment(): boolean;
|
|
53
|
+
export declare function get_default_storage(): Promise<Storage>;
|
|
54
|
+
export declare function as_char_ptr(str: string, c4w: C4W, free_ptrs?: number[]): number;
|
|
55
|
+
export declare function as_json(ptr: number, c4w: C4W, free_ptrs?: number[] | boolean): any;
|
|
56
|
+
export declare function as_bytes(ptr: number, len: number, c4w: C4W, free_ptrs?: number[] | boolean): Uint8Array;
|
|
57
|
+
export declare function copy_to_c(data: Uint8Array, c4w: C4W, free_ptrs?: number[]): number;
|
|
58
|
+
export declare function createC4wApi(options: {
|
|
59
|
+
importC4wModule: () => Promise<any>;
|
|
60
|
+
resolveWasmLocation: (override: string | null) => string | null;
|
|
61
|
+
}): {
|
|
62
|
+
set_wasm_url: (url: string) => void;
|
|
63
|
+
loadC4WModule: () => Promise<C4W>;
|
|
64
|
+
getC4w: () => Promise<C4W>;
|
|
65
|
+
get_prover_config_hex: (chainId: number) => Promise<string>;
|
|
66
|
+
set_trusted_checkpoint: (chainId: number, checkpoint: string) => Promise<void>;
|
|
67
|
+
};
|
package/wasm_shared.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
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
|
+
export function isNodeEnvironment() {
|
|
24
|
+
return (typeof process !== 'undefined' && process.versions != null && process.versions.node != null);
|
|
25
|
+
}
|
|
26
|
+
export function isBrowserEnvironment() {
|
|
27
|
+
return (typeof window !== 'undefined' && typeof document !== 'undefined');
|
|
28
|
+
}
|
|
29
|
+
export async function get_default_storage() {
|
|
30
|
+
if (isBrowserEnvironment())
|
|
31
|
+
// web interface
|
|
32
|
+
return {
|
|
33
|
+
get: (key) => {
|
|
34
|
+
const value = window.localStorage.getItem(key);
|
|
35
|
+
if (value) {
|
|
36
|
+
const length = value.length / 2;
|
|
37
|
+
const uint8Array = new Uint8Array(length);
|
|
38
|
+
for (let i = 0; i < length; i++) {
|
|
39
|
+
uint8Array[i] = parseInt(value.substr(i * 2, 2), 16);
|
|
40
|
+
}
|
|
41
|
+
return uint8Array;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
},
|
|
45
|
+
set: (key, value) => {
|
|
46
|
+
window.localStorage.setItem(key, Array.from(value).map(_ => _.toString(16).padStart(2, '0')).join(''));
|
|
47
|
+
},
|
|
48
|
+
del: (key) => {
|
|
49
|
+
window.localStorage.removeItem(key);
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
else if (isNodeEnvironment()) {
|
|
53
|
+
const fs = await import('fs');
|
|
54
|
+
// node interface
|
|
55
|
+
return {
|
|
56
|
+
get: (key) => {
|
|
57
|
+
try {
|
|
58
|
+
return fs.readFileSync(key);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
set: (key, value) => {
|
|
65
|
+
fs.writeFileSync(key, value);
|
|
66
|
+
},
|
|
67
|
+
del: (key) => {
|
|
68
|
+
fs.unlinkSync(key);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
throw new Error('Unsupported environment');
|
|
73
|
+
}
|
|
74
|
+
export function as_char_ptr(str, c4w, free_ptrs) {
|
|
75
|
+
const ptr = c4w._malloc(str.length + 1);
|
|
76
|
+
c4w.stringToUTF8(str, ptr, str.length + 1);
|
|
77
|
+
if (free_ptrs)
|
|
78
|
+
free_ptrs.push(ptr);
|
|
79
|
+
return ptr;
|
|
80
|
+
}
|
|
81
|
+
export function as_json(ptr, c4w, free_ptrs) {
|
|
82
|
+
const str = c4w.UTF8ToString(ptr);
|
|
83
|
+
if (free_ptrs) {
|
|
84
|
+
if (Array.isArray(free_ptrs))
|
|
85
|
+
free_ptrs.push(ptr);
|
|
86
|
+
else
|
|
87
|
+
c4w._free(ptr);
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
return JSON.parse(str);
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
console.error(e);
|
|
94
|
+
console.error(str);
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export function as_bytes(ptr, len, c4w, free_ptrs) {
|
|
99
|
+
const bytes = new Uint8Array(len);
|
|
100
|
+
bytes.set(c4w.HEAPU8.subarray(ptr, ptr + len));
|
|
101
|
+
if (free_ptrs) {
|
|
102
|
+
if (Array.isArray(free_ptrs))
|
|
103
|
+
free_ptrs.push(ptr);
|
|
104
|
+
else
|
|
105
|
+
c4w._free(ptr);
|
|
106
|
+
}
|
|
107
|
+
return bytes;
|
|
108
|
+
}
|
|
109
|
+
export function copy_to_c(data, c4w, free_ptrs) {
|
|
110
|
+
const ptr = c4w._malloc(data.length + 1);
|
|
111
|
+
c4w.HEAPU8.set(data, ptr);
|
|
112
|
+
c4w.HEAPU8[ptr + data.length] = 0;
|
|
113
|
+
if (free_ptrs)
|
|
114
|
+
free_ptrs.push(ptr);
|
|
115
|
+
return ptr;
|
|
116
|
+
}
|
|
117
|
+
export function createC4wApi(options) {
|
|
118
|
+
let wasmUrlOverride = null;
|
|
119
|
+
let moduleInstance = null;
|
|
120
|
+
let modulePromise = null;
|
|
121
|
+
function set_wasm_url(url) {
|
|
122
|
+
wasmUrlOverride = url;
|
|
123
|
+
}
|
|
124
|
+
async function loadC4WModule() {
|
|
125
|
+
const module = await options.importC4wModule();
|
|
126
|
+
const args = {};
|
|
127
|
+
const wasmLocation = options.resolveWasmLocation(wasmUrlOverride);
|
|
128
|
+
if (wasmLocation) {
|
|
129
|
+
args.locateFile = (path) => path.endsWith('.wasm') ? wasmLocation : path;
|
|
130
|
+
}
|
|
131
|
+
return module.default(args);
|
|
132
|
+
}
|
|
133
|
+
async function getC4w() {
|
|
134
|
+
if (moduleInstance)
|
|
135
|
+
return moduleInstance;
|
|
136
|
+
if (!modulePromise) {
|
|
137
|
+
modulePromise = loadC4WModule().then(async (loadedModule) => {
|
|
138
|
+
moduleInstance = loadedModule;
|
|
139
|
+
moduleInstance.storage = await get_default_storage();
|
|
140
|
+
moduleInstance._init_storage();
|
|
141
|
+
modulePromise = null;
|
|
142
|
+
return moduleInstance;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return modulePromise;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Returns the prover config state for a given chain as hex string with 0x prefix.
|
|
149
|
+
* @param chainId Chain identifier
|
|
150
|
+
* @return Hex string (e.g. "0x...") or "0x" if no state present
|
|
151
|
+
*/
|
|
152
|
+
async function get_prover_config_hex(chainId) {
|
|
153
|
+
const c4w = await getC4w();
|
|
154
|
+
if (!c4w.storage)
|
|
155
|
+
return '0x';
|
|
156
|
+
const state = c4w.storage.get('states_' + chainId);
|
|
157
|
+
return '0x' + (state ? Array.from(state).map(_ => _.toString(16).padStart(2, '0')).join('') : '');
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Sets the trusted checkpoint inside the C context to initialize state.
|
|
161
|
+
* @param chainId Chain identifier
|
|
162
|
+
* @param checkpoint Trusted checkpoint root hex string
|
|
163
|
+
*/
|
|
164
|
+
async function set_trusted_checkpoint(chainId, checkpoint) {
|
|
165
|
+
const c4w = await getC4w();
|
|
166
|
+
const free_buffers = [];
|
|
167
|
+
c4w._c4w_create_verify_ctx(0, 0, 0, 0, BigInt(chainId), as_char_ptr(checkpoint, c4w, free_buffers), 0);
|
|
168
|
+
free_buffers.forEach(ptr => c4w._free(ptr));
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
set_wasm_url,
|
|
172
|
+
loadC4WModule,
|
|
173
|
+
getC4w,
|
|
174
|
+
get_prover_config_hex,
|
|
175
|
+
set_trusted_checkpoint,
|
|
176
|
+
};
|
|
177
|
+
}
|