@corpus-core/colibri-stateless 0.1.17 → 0.1.20

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/index.d.ts CHANGED
@@ -4,6 +4,10 @@ export interface Cache {
4
4
  get(req: DataRequest): Uint8Array | undefined;
5
5
  set(req: DataRequest, data: Uint8Array): void;
6
6
  }
7
+ interface RequestArguments {
8
+ readonly method: string;
9
+ readonly params?: readonly unknown[] | object;
10
+ }
7
11
  export interface Config {
8
12
  chainId: number;
9
13
  beacon_apis: string[];
@@ -25,13 +29,51 @@ export interface DataRequest {
25
29
  payload: any;
26
30
  req_ptr: number;
27
31
  }
32
+ export declare enum MethodType {
33
+ PROOFABLE = 1,
34
+ UNPROOFABLE = 2,
35
+ NOT_SUPPORTED = 3,
36
+ LOCAL = 4
37
+ }
28
38
  export declare function handle_request(req: DataRequest, conf: Config): Promise<void>;
29
39
  export default class C4Client {
30
40
  config: Config;
31
41
  constructor(config?: Partial<Config>);
32
42
  private get flags();
43
+ /**
44
+ * checks, whether the rpc-method is supported or proofable.
45
+ * @param method - The method to check
46
+ * @returns The method type
47
+ */
48
+ getMethodSupport(method: string): Promise<MethodType>;
49
+ /**
50
+ * creates a proof for the given method and arguments
51
+ * @param method - The method to create a proof for
52
+ * @param args - The arguments to create a proof for
53
+ * @returns The proof
54
+ */
33
55
  createProof(method: string, args: any[]): Promise<Uint8Array>;
56
+ /**
57
+ * verifies a proof for the given method and arguments
58
+ * @param method - The method to verify the proof for
59
+ * @param args - The arguments to verify the proof for
60
+ * @param proof - The proof to verify
61
+ * @returns The result
62
+ */
34
63
  verifyProof(method: string, args: any[], proof: Uint8Array): Promise<any>;
64
+ /**
65
+ * executes a rpc-method, which includes
66
+ * -creating or fetching the proof
67
+ * -verifying the proof
68
+ * -returning the result
69
+ * @param method - The method to execute
70
+ * @param args - The arguments to execute the method with
71
+ * @returns The result
72
+ */
35
73
  rpc(method: string, args: any[]): Promise<any>;
36
74
  static register_storage(storage: C4Storage): Promise<void>;
75
+ request(args: RequestArguments): Promise<any>;
76
+ on(event: string, callback: (data: any) => void): void;
77
+ removeListener(event: string, callback: (data: any) => void): void;
37
78
  }
79
+ export {};
package/index.js CHANGED
@@ -1,5 +1,12 @@
1
1
  // Import the Emscripten-generated module
2
2
  import { getC4w, as_char_ptr, as_json, as_bytes, copy_to_c } from "./wasm.js";
3
+ export var MethodType;
4
+ (function (MethodType) {
5
+ MethodType[MethodType["PROOFABLE"] = 1] = "PROOFABLE";
6
+ MethodType[MethodType["UNPROOFABLE"] = 2] = "UNPROOFABLE";
7
+ MethodType[MethodType["NOT_SUPPORTED"] = 3] = "NOT_SUPPORTED";
8
+ MethodType[MethodType["LOCAL"] = 4] = "LOCAL";
9
+ })(MethodType || (MethodType = {}));
3
10
  async function initialize_storage(conf) {
4
11
  // TODO handle trusted_block_hashes
5
12
  const c4w = await getC4w();
@@ -97,13 +104,32 @@ export default class C4Client {
97
104
  chainId: 1, // Default chainId
98
105
  beacon_apis: ["https://lodestar-mainnet.chainsafe.io"], // Default beacon API
99
106
  rpcs: ["https://rpc.ankr.com/eth"], // Default RPC
100
- trusted_block_hashes: []
107
+ trusted_block_hashes: [],
108
+ proofer: ["https://c4.incubed.net"],
101
109
  }, ...config
102
110
  };
103
111
  }
104
112
  get flags() {
105
113
  return this.config.include_code ? 1 : 0;
106
114
  }
115
+ /**
116
+ * checks, whether the rpc-method is supported or proofable.
117
+ * @param method - The method to check
118
+ * @returns The method type
119
+ */
120
+ async getMethodSupport(method) {
121
+ const c4w = await getC4w();
122
+ const free_buffers = [];
123
+ const method_type = c4w._c4w_get_method_type(BigInt(this.config.chainId), as_char_ptr(method, c4w, free_buffers));
124
+ free_buffers.forEach(ptr => c4w._free(ptr));
125
+ return method_type;
126
+ }
127
+ /**
128
+ * creates a proof for the given method and arguments
129
+ * @param method - The method to create a proof for
130
+ * @param args - The arguments to create a proof for
131
+ * @returns The proof
132
+ */
107
133
  async createProof(method, args) {
108
134
  const c4w = await getC4w();
109
135
  const free_buffers = [];
@@ -131,6 +157,13 @@ export default class C4Client {
131
157
  c4w._c4w_free_proof_ctx(ctx);
132
158
  }
133
159
  }
160
+ /**
161
+ * verifies a proof for the given method and arguments
162
+ * @param method - The method to verify the proof for
163
+ * @param args - The arguments to verify the proof for
164
+ * @param proof - The proof to verify
165
+ * @returns The result
166
+ */
134
167
  async verifyProof(method, args, proof) {
135
168
  const c4w = await getC4w();
136
169
  const free_buffers = [];
@@ -158,17 +191,46 @@ export default class C4Client {
158
191
  c4w._c4w_free_verify_ctx(ctx);
159
192
  }
160
193
  }
194
+ /**
195
+ * executes a rpc-method, which includes
196
+ * -creating or fetching the proof
197
+ * -verifying the proof
198
+ * -returning the result
199
+ * @param method - The method to execute
200
+ * @param args - The arguments to execute the method with
201
+ * @returns The result
202
+ */
161
203
  async rpc(method, args) {
162
- // skip verify
163
- if (this.config.verify && !this.config.verify(method, args))
164
- return await fetch_rpc(this.config.rpcs, method, args, false);
165
- let proof = this.config.proofer && this.config.proofer.length
166
- ? await fetch_rpc(this.config.proofer, method, args, true)
167
- : await this.createProof(method, args);
204
+ const method_type = await this.getMethodSupport(method);
205
+ let proof = new Uint8Array();
206
+ switch (method_type) {
207
+ case MethodType.PROOFABLE: {
208
+ if (this.config.verify && !this.config.verify(method, args))
209
+ return await fetch_rpc(this.config.rpcs, method, args, false);
210
+ proof = this.config.proofer && this.config.proofer.length
211
+ ? await fetch_rpc(this.config.proofer, method, args, true)
212
+ : await this.createProof(method, args);
213
+ break;
214
+ }
215
+ case MethodType.UNPROOFABLE:
216
+ return await fetch_rpc(this.config.rpcs, method, args, false);
217
+ case MethodType.NOT_SUPPORTED:
218
+ throw new Error(`Method ${method} is not supported`);
219
+ case MethodType.LOCAL:
220
+ break;
221
+ }
168
222
  return this.verifyProof(method, args, proof);
169
223
  }
170
224
  static async register_storage(storage) {
171
225
  const c4w = await getC4w();
172
226
  c4w.storage = storage;
173
227
  }
228
+ async request(args) {
229
+ const res = await this.rpc(args.method, args.params || []);
230
+ return res;
231
+ }
232
+ on(event, callback) {
233
+ }
234
+ removeListener(event, callback) {
235
+ }
174
236
  }
package/package.json CHANGED
@@ -1,9 +1,17 @@
1
1
  {
2
2
  "name": "@corpus-core/colibri-stateless",
3
- "version": "0.1.17",
3
+ "version": "0.1.20",
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
- "scripts": {},
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "npm run test:node && npm run test:webpack",
9
+ "test:node": "node --test test/*.test.mjs",
10
+ "test:webpack": "npm run webpack:install && npm run webpack:build",
11
+ "webpack:install": "cd webpack && npm install --legacy-peer-deps",
12
+ "webpack:build": "cd webpack && npm run build",
13
+ "webpack:start": "cd webpack && npm run start"
14
+ },
7
15
  "repository": {
8
16
  "type": "git",
9
17
  "url": "git+https://github.com/corpus-core/c4.git"
package/wasm.d.ts CHANGED
@@ -5,6 +5,7 @@ export interface C4W {
5
5
  _c4w_get_pending_data_request: (proofCtx: number) => number;
6
6
  _c4w_req_set_response: (reqPtr: number, data: number, len: number, node_index: number) => void;
7
7
  _c4w_req_set_error: (reqPtr: number, error: number, node_index: number) => void;
8
+ _c4w_get_method_type: (chain_id: bigint, method: number) => number;
8
9
  _c4w_create_verify_ctx: (proof: number, proof_len: number, method: number, args: number, chain_id: bigint) => number;
9
10
  _c4w_free_verify_ctx: (verifyCtx: number) => void;
10
11
  _c4w_verify_proof: (verifyCtx: number) => number;
package/wasm.js CHANGED
@@ -84,7 +84,14 @@ export function as_json(ptr, c4w, free_ptrs) {
84
84
  else
85
85
  c4w._free(ptr);
86
86
  }
87
- return JSON.parse(str);
87
+ try {
88
+ return JSON.parse(str);
89
+ }
90
+ catch (e) {
91
+ console.error(e);
92
+ console.error(str);
93
+ return null;
94
+ }
88
95
  }
89
96
  export function as_bytes(ptr, len, c4w, free_ptrs) {
90
97
  const bytes = new Uint8Array(len);
package/c4w.wasm DELETED
Binary file