@corpus-core/colibri-stateless 0.2.0 → 0.2.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.
- package/README.md +44 -19
- package/c4w.js +1 -1
- package/index.d.ts +1 -0
- package/index.js +16 -8
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -39,6 +39,7 @@ export declare function handle_request(req: DataRequest, conf: Config): Promise<
|
|
|
39
39
|
export default class C4Client {
|
|
40
40
|
config: Config;
|
|
41
41
|
constructor(config?: Partial<Config>);
|
|
42
|
+
private getProoferConfig;
|
|
42
43
|
private get flags();
|
|
43
44
|
/**
|
|
44
45
|
* checks, whether the rpc-method is supported or proofable.
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Import the Emscripten-generated module
|
|
2
|
-
import {
|
|
2
|
+
import { as_bytes, as_char_ptr, as_json, copy_to_c, getC4w } from "./wasm.js";
|
|
3
3
|
export var MethodType;
|
|
4
4
|
(function (MethodType) {
|
|
5
5
|
MethodType[MethodType["PROOFABLE"] = 1] = "PROOFABLE";
|
|
@@ -11,12 +11,12 @@ async function initialize_storage(conf) {
|
|
|
11
11
|
// TODO handle trusted_block_hashes
|
|
12
12
|
const c4w = await getC4w();
|
|
13
13
|
}
|
|
14
|
-
async function fetch_rpc(urls,
|
|
14
|
+
async function fetch_rpc(urls, payload, as_proof = false) {
|
|
15
15
|
let last_error = "All nodes failed";
|
|
16
16
|
for (const url of urls) {
|
|
17
17
|
const response = await fetch(url, {
|
|
18
18
|
method: 'POST',
|
|
19
|
-
body: JSON.stringify({ id: 1, jsonrpc: "2.0",
|
|
19
|
+
body: JSON.stringify({ id: 1, jsonrpc: "2.0", ...payload }),
|
|
20
20
|
headers: {
|
|
21
21
|
"Content-Type": "application/json",
|
|
22
22
|
"Accept": as_proof ? "application/octet-stream" : "application/json"
|
|
@@ -44,7 +44,7 @@ function log(msg) {
|
|
|
44
44
|
}
|
|
45
45
|
export async function handle_request(req, conf) {
|
|
46
46
|
const free_buffers = [];
|
|
47
|
-
const servers = req.type == "beacon_api" ? conf.beacon_apis : conf.rpcs;
|
|
47
|
+
const servers = req.type == "beacon_api" ? ((conf.proofer && conf.proofer.length) ? conf.proofer : conf.beacon_apis) : conf.rpcs;
|
|
48
48
|
const c4w = await getC4w();
|
|
49
49
|
let path = (req.type == 'eth_rpc' && req.payload)
|
|
50
50
|
? `rpc: ${req.payload?.method}(${req.payload?.params.join(',')})`
|
|
@@ -119,9 +119,17 @@ export default class C4Client {
|
|
|
119
119
|
rpcs: ["https://rpc.ankr.com/eth"], // Default RPC
|
|
120
120
|
trusted_block_hashes: [],
|
|
121
121
|
proofer: ["https://mainnet.colibri-proof.tech"],
|
|
122
|
-
},
|
|
122
|
+
},
|
|
123
|
+
...config
|
|
123
124
|
};
|
|
124
125
|
}
|
|
126
|
+
async getProoferConfig() {
|
|
127
|
+
const c4w = await getC4w();
|
|
128
|
+
if (!c4w.storage)
|
|
129
|
+
return '0x';
|
|
130
|
+
const state = c4w.storage.get('state_' + this.config.chainId);
|
|
131
|
+
return '0x' + (state ? Array.from(state).map(_ => _.toString(16).padStart(2, '0')).join('') : '');
|
|
132
|
+
}
|
|
125
133
|
get flags() {
|
|
126
134
|
return this.config.include_code ? 1 : 0;
|
|
127
135
|
}
|
|
@@ -221,14 +229,14 @@ export default class C4Client {
|
|
|
221
229
|
switch (method_type) {
|
|
222
230
|
case MethodType.PROOFABLE: {
|
|
223
231
|
if (this.config.verify && !this.config.verify(method, args))
|
|
224
|
-
return await fetch_rpc(this.config.rpcs, method, args, false);
|
|
232
|
+
return await fetch_rpc(this.config.rpcs, { method, params: args }, false);
|
|
225
233
|
proof = this.config.proofer && this.config.proofer.length
|
|
226
|
-
? await fetch_rpc(this.config.proofer, method, args, true)
|
|
234
|
+
? await fetch_rpc(this.config.proofer, { method, params: args, c4: await this.getProoferConfig() }, true)
|
|
227
235
|
: await this.createProof(method, args);
|
|
228
236
|
break;
|
|
229
237
|
}
|
|
230
238
|
case MethodType.UNPROOFABLE:
|
|
231
|
-
return await fetch_rpc(this.config.rpcs, method, args, false);
|
|
239
|
+
return await fetch_rpc(this.config.rpcs, { method, params: args }, false);
|
|
232
240
|
case MethodType.NOT_SUPPORTED:
|
|
233
241
|
throw new Error(`Method ${method} is not supported`);
|
|
234
242
|
case MethodType.LOCAL:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@corpus-core/colibri-stateless",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
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",
|