@corpus-core/colibri-stateless 0.1.18 → 0.2.0
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 +185 -11
- package/c4w.js +1 -1
- package/index.d.ts +8 -0
- package/index.js +24 -1
- package/package.json +9 -2
- package/wasm.d.ts +1 -0
- package/c4w.wasm +0 -0
package/README.md
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
# C4 (corpus core colibri client)
|
|
6
6
|
|
|
7
7
|

|
|
8
|
-
[](https://github.com/corpus-core/c4/actions/workflows/cmake-multi-platform.yml)
|
|
9
8
|
|
|
10
9
|
The colibri client 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.
|
|
11
10
|
|
|
@@ -15,11 +14,107 @@ The colibri client is a stateless and trustless ethereum client, which is optimi
|
|
|
15
14
|
npm install @corpus-core/colibri-stateless
|
|
16
15
|
```
|
|
17
16
|
|
|
18
|
-
##
|
|
17
|
+
## Using Colibri as RPC Provider
|
|
19
18
|
|
|
20
|
-
|
|
19
|
+
The Colibri Class implements the EIP-1193 Interface, so any library supporting EIP-1193 Providers can easily use Colibri as RPCProvider.
|
|
20
|
+
|
|
21
|
+
Right now Subscription and Filters have not been implemented, so in case you need those features, jus use a different Provider for those tasks and the verify the found logs using Colibri. But those features will be implemented in one of the next releases.
|
|
22
|
+
|
|
23
|
+
### EthersJs 6.x
|
|
24
|
+
```javascript
|
|
25
|
+
import { BrowserProvider } from "ethers";
|
|
26
|
+
import Colibri from "@corpus-core/colibri-stateless";
|
|
27
|
+
|
|
28
|
+
async function main() {
|
|
29
|
+
|
|
30
|
+
// Initialize the client with the default configuration and RPCs
|
|
31
|
+
const client = new Colibri();
|
|
32
|
+
|
|
33
|
+
// Use Colibri client as the EIP-1193 provider for ethers (v6)
|
|
34
|
+
const provider = new BrowserProvider(client);
|
|
35
|
+
|
|
36
|
+
// Fetch the latest block using the ethers provider
|
|
37
|
+
const block = await provider.getBlock('latest');
|
|
38
|
+
console.log("Block fetched via ethers:", block);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
main().catch(console.error);
|
|
42
|
+
```
|
|
43
|
+
### EthersJs 5.x
|
|
44
|
+
```javascript
|
|
45
|
+
import * as ethers from "ethers";
|
|
46
|
+
import Colibri from "@corpus-core/colibri-stateless";
|
|
47
|
+
|
|
48
|
+
async function main() {
|
|
49
|
+
|
|
50
|
+
// Initialize the client with the default configuration and RPCs
|
|
51
|
+
const client = new Colibri();
|
|
52
|
+
|
|
53
|
+
// Use Colibri client as the EIP-1193 provider for ethers (v6)
|
|
54
|
+
const provider = new ethers.providers.Web3Provider(client);
|
|
55
|
+
|
|
56
|
+
// Fetch the latest block using the ethers provider
|
|
57
|
+
const block = await provider.getBlock('latest');
|
|
58
|
+
console.log("Block fetched via ethers:", block);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
main().catch(console.error);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Web3.js
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import Web3 from 'web3';
|
|
68
|
+
import Colibri from "@corpus-core/colibri-stateless";
|
|
69
|
+
|
|
70
|
+
async function main() {
|
|
71
|
+
|
|
72
|
+
// Initialize the client with the default configuration and RPCs
|
|
73
|
+
const client = new Colibri();
|
|
74
|
+
|
|
75
|
+
// Use Colibri client as the EIP-1193 provider for web3.js
|
|
76
|
+
const web3 = new Web3(client);
|
|
77
|
+
|
|
78
|
+
// Fetch the latest block using the web3.js provider
|
|
79
|
+
const block = await web3.eth.getBlock('latest');
|
|
80
|
+
console.log("Block fetched via web3.js:", block);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
main().catch(console.error);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Viem
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
import { createPublicClient, custom } from 'viem';
|
|
90
|
+
import { mainnet } from 'viem/chains';
|
|
21
91
|
import Colibri from "@corpus-core/colibri-stateless";
|
|
22
92
|
|
|
93
|
+
async function main() {
|
|
94
|
+
|
|
95
|
+
// Initialize the Colibri client
|
|
96
|
+
const colibriClient = new Colibri();
|
|
97
|
+
|
|
98
|
+
// Create a viem Public Client using Colibri as a custom EIP-1193 transport
|
|
99
|
+
const viemClient = createPublicClient({
|
|
100
|
+
chain: mainnet, // Specify the chain
|
|
101
|
+
transport: custom(colibriClient) // Wrap Colibri client
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Fetch the latest block using the viem client
|
|
105
|
+
const block = await viemClient.getBlock({ blockTag: 'latest' });
|
|
106
|
+
console.log("Block fetched via viem:", block);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
main().catch(console.error);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Building proofs in you app.
|
|
113
|
+
|
|
114
|
+
If you don't want to use a remote Service building the proofs, you can also use Colibri directly to build the proof or to verify. A Proof is juzst a UInt8Array or just bytes. You write it in a file or package it in your application and verify whenever it is needed:
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import Colibri from "@corpus-core/colibri-stateless";
|
|
23
118
|
|
|
24
119
|
async function main() {
|
|
25
120
|
const method = "eth_getTransactionByHash";
|
|
@@ -35,26 +130,105 @@ async function main() {
|
|
|
35
130
|
// Verify the proof against requested method and arguments
|
|
36
131
|
const result = await client.verifyProof(method, args, proof);
|
|
37
132
|
|
|
133
|
+
// the result will be the expected json
|
|
38
134
|
console.log(result);
|
|
39
135
|
}
|
|
40
136
|
|
|
41
137
|
main().then(console.log).catch(console.error);
|
|
42
|
-
|
|
43
138
|
```
|
|
44
139
|
|
|
140
|
+
## Configuration
|
|
141
|
+
|
|
142
|
+
The constructor of the colibri client accepts a configuration-object, which may configure the client. The following parameters are accepted:
|
|
143
|
+
|
|
144
|
+
- `chainId` - the chain to be used (default is 1, whoich is mainnet).
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
new Colibri({ chainId: 0x7})
|
|
148
|
+
```
|
|
149
|
+
- `beacon_apis` - urls for the beacon apis
|
|
150
|
+
An array of endpoints for accessing the beacon chain using the official [Eth Beacon Node API](https://ethereum.github.io/beacon-APIs/). The Array may contain more than one url, and if one API is not responding the next URL will work as fallback. This beacon API is currently used eitehr when building proofs directly or even if you are using a remote proofer, the LightClientUpdates (every 27h) will be fetched directly from the beacon API.
|
|
151
|
+
```js
|
|
152
|
+
new Colibri({ beacon_apis: [ 'https://lodestar-mainnet.chainsafe.io' ]})
|
|
153
|
+
```
|
|
154
|
+
- `rpcs` - RPCs for the executionlayer
|
|
155
|
+
a array of rpc-endpoints for accessing the execution layer. If you are using the remote proofer, you may not need it at all. But creating your proofs locally will require to access data from the execution layer. Having more than one rpc-url allows to use fallbacks in case one is not available.
|
|
156
|
+
```js
|
|
157
|
+
new Colibri({ beacon_apis: [
|
|
158
|
+
"https://nameless-sly-reel.quiknode.pro/<APIKEY>/",
|
|
159
|
+
"https://eth-mainnet.g.alchemy.com/v2/<APIKEY>",
|
|
160
|
+
"https://rpc.ankr.com/eth/<APIKEY>" ]})
|
|
161
|
+
```
|
|
162
|
+
- `proofer` - urls for remove proofer
|
|
163
|
+
a array of endpoints for remote proofer. This allows to generate the proof in the backend, where caches can speed up the process.
|
|
164
|
+
```js
|
|
165
|
+
new Colibri({ proofer: ["https://mainnet.colibri-proof.tech" ]})
|
|
166
|
+
```
|
|
167
|
+
- `trusted_block_hashes` - beacon block hashes used as trusted anchor
|
|
168
|
+
Thise array of blockhashes will be used as anchor for fetching the keys for the sync committee. So instead of starting with the genesis you can define a starting block, where you know the blockhash. If not trusted blockhash is set the beacon-api will be used to fetch one, but defining it is safer.
|
|
169
|
+
```js
|
|
170
|
+
new Colibri({ trusted_block_hashes: [ "0x4232db57354ddacec40adda0a502f7732ede19ba0687482a1e15ad20e5e7d1e7" ]})
|
|
171
|
+
```
|
|
172
|
+
- `cache` - cache impl for rpc-requests
|
|
173
|
+
you can provide your own implementation to cache JSON-RPC requests. those function will be used before a request is send, also allowing mock handlers to cache responses for tests.
|
|
174
|
+
```js
|
|
175
|
+
new Colibri({ cache: {
|
|
176
|
+
cacheable(req: DataRequest) {
|
|
177
|
+
return req.payload && req.payload.method!='eth_blockNumber'
|
|
178
|
+
},
|
|
179
|
+
get(req: DataRequest) {
|
|
180
|
+
try {
|
|
181
|
+
return fs.readFileSync(`${cache_dir}/${req.url}`);
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
return null
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
set(req: DataRequest, data: Uint8Array) {
|
|
188
|
+
fs.writeFileSync(`${cache_dir}/${req.url}`, data);
|
|
189
|
+
}
|
|
190
|
+
}})
|
|
191
|
+
```
|
|
192
|
+
- `debug` - if true you will see debug output on the console
|
|
193
|
+
```js
|
|
194
|
+
new Colibri({ debug: true})
|
|
195
|
+
```
|
|
196
|
+
- `include_code`- if true the code of the contracts will be included when creating proofs. this is only relevant when creating your own proofs for eth_call. (default: false)
|
|
197
|
+
```js
|
|
198
|
+
new Colibri({ include_code: true})
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
- `verify`- a function to decide which request should be verified and which should be fetched from the default RPC-Provider. It allows you to speed up performance for requests which are not critical.
|
|
202
|
+
```js
|
|
203
|
+
new Colibri({ verify: (method, args) => method != 'eth_blockNumber' })
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
45
208
|
## Building
|
|
46
209
|
|
|
47
|
-
In order to build the Javascript bindings, you need to have [emscripten installed](https://emscripten.org/docs/getting_started/downloads.html).
|
|
210
|
+
In order to build the Javascript bindings from source, you need to have [emscripten installed](https://emscripten.org/docs/getting_started/downloads.html).
|
|
211
|
+
|
|
212
|
+
*The Colibri JS-Binding has been tested with Version 4.0.3. Using latest or other versions may result in unexpected issues. For example Version 4.0.7 is not working. So make sure you select the right version when installing!*
|
|
48
213
|
|
|
49
214
|
```sh
|
|
50
|
-
git clone https://github.com/corpus-core/
|
|
51
|
-
cd
|
|
52
|
-
mkdir build
|
|
53
|
-
cd build
|
|
215
|
+
git clone https://github.com/corpus-core/colibri-stateless.git && cd colibri-stateless
|
|
216
|
+
mkdir build && cd build
|
|
54
217
|
emcmake cmake -DWASM=true -DCURL=false ..
|
|
55
|
-
make
|
|
218
|
+
make -j
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Of course, building your own version allows you to use any of the [cmake flags options](../building/cmake-colibri-lib.md). So you can choose which chains or which Proofs schouls be supported allowing a very small wasm build.
|
|
222
|
+
|
|
223
|
+
like
|
|
224
|
+
```sh
|
|
225
|
+
emcmake cmake -DWASM=true -DCURL=false -DETH_ACCOUNT=1 -DETH_CALL=0 -DETH_LOGS=0 -DETH_BLOCK=0 -DETH_TX=0 -DETH_RECEIPT=0 -DETH_UTIL=0 ..
|
|
56
226
|
```
|
|
57
|
-
|
|
227
|
+
|
|
228
|
+
this would turn off all code which is not needed dramticly decreasing the size, if all you want to do is verify accountProofs (like eth_getBalance).
|
|
229
|
+
|
|
230
|
+
After calling `make`, The js-module will be in the `build/emscripten` folder.
|
|
231
|
+
|
|
58
232
|
|
|
59
233
|
## Concept
|
|
60
234
|
|