@hazae41/bobine 0.0.23 โ 0.1.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 +5 -197
- package/out/libs/packed/mod.d.ts +5 -5
- package/out/libs/packed/mod.js +42 -42
- package/out/libs/sql/mod.d.ts +2 -2
- package/out/libs/sql/mod.js +2 -2
- package/out/mod.d.ts +0 -2
- package/out/mod.js +0 -24
- package/out/mods/bobine/errors/mod.d.ts +8 -0
- package/out/mods/bobine/errors/mod.js +12 -0
- package/out/mods/bobine/mod.d.ts +3 -0
- package/out/mods/bobine/mod.js +3 -0
- package/out/mods/bobine/runner/mod.d.ts +12 -0
- package/out/mods/bobine/runner/mod.js +447 -0
- package/out/mods/bobine/worker/bin.d.ts +12 -0
- package/out/mods/bobine/worker/bin.js +67 -0
- package/out/mods/bobine/worker/mod.d.ts +10 -0
- package/out/mods/bobine/worker/mod.js +105 -0
- package/out/mods/mod.d.ts +1 -1
- package/out/mods/mod.js +1 -1
- package/package.json +11 -17
- package/out/libs/database/mod.d.ts +0 -1
- package/out/libs/database/mod.js +0 -1
- package/out/mods/config/mod.d.ts +0 -12
- package/out/mods/config/mod.js +0 -1
- package/out/mods/helper/bin.d.ts +0 -1
- package/out/mods/helper/bin.js +0 -78
- package/out/mods/server/bin.d.ts +0 -1
- package/out/mods/server/bin.js +0 -72
- package/out/mods/server/mod.d.ts +0 -9
- package/out/mods/server/mod.js +0 -314
- package/out/mods/worker/bin.d.ts +0 -1
- package/out/mods/worker/bin.js +0 -464
package/README.md
CHANGED
|
@@ -1,205 +1,13 @@
|
|
|
1
|
-
# Bobine
|
|
1
|
+
# Bobine
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
WebAssembly-based metered chain of compute
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
-
|
|
6
|
+
npm install @hazae41/bobine
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
[**๐ Website**](https://bobine.tech/) โข [
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- Ultra simple
|
|
14
|
-
- WebAssembly modules
|
|
15
|
-
- Based on web technologies
|
|
16
|
-
- Resistant to 50% attacks
|
|
17
|
-
- Account* abstraction
|
|
18
|
-
- Verifiable without ZK
|
|
19
|
-
|
|
20
|
-
\* There are no built-in accounting concepts, only pure generic APIs for cryptography and storage
|
|
9
|
+
[**๐ฆ NPM**](https://www.npmjs.com/package/@hazae41/bobine) โข [**๐ Website**](https://bobine.tech/) โข [**๐ Docs**](./docs/index.md)
|
|
21
10
|
|
|
22
11
|
## Ecosystem
|
|
23
12
|
|
|
24
|
-
- [Awesome Bobine](https://github.com/hazae41/awesome-bobine) / A curated list on Bobine ecosystem
|
|
25
|
-
|
|
26
|
-
## Usage
|
|
27
|
-
|
|
28
|
-
### Running the server
|
|
29
|
-
|
|
30
|
-
Install Deno
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
npm install -g deno
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Install the binary with Deno
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
deno install -gf -A npm:@hazae41/bobine
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
Generate an Ed25519 keypair by running the following code in your browser/deno/node/bun console
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
const keypair = await crypto.subtle.generateKey("Ed25519", true, ["sign", "verify"])
|
|
46
|
-
|
|
47
|
-
const privateKey = await crypto.subtle.exportKey("pkcs8", keypair.privateKey)
|
|
48
|
-
|
|
49
|
-
const publicKey = await crypto.subtle.exportKey("raw", keypair.publicKey)
|
|
50
|
-
|
|
51
|
-
console.log(`ED25519_PRIVATE_KEY_HEX=${new Uint8Array(privateKey).toHex()}`)
|
|
52
|
-
|
|
53
|
-
console.log(`ED25519_PUBLIC_KEY_HEX=${new Uint8Array(publicKey).toHex()}`)
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Create an `.env.local` file and replace your Ed25519 values
|
|
57
|
-
|
|
58
|
-
```env
|
|
59
|
-
DATABASE_PATH=./local/database.db
|
|
60
|
-
|
|
61
|
-
SCRIPTS_PATH=./local/scripts
|
|
62
|
-
|
|
63
|
-
ED25519_PRIVATE_KEY_HEX=302e020100300506032b657004220420edff8b2503b91f58bc0f0435ca17de549f89d6a7cde4c277161e031669395005
|
|
64
|
-
ED25519_PUBLIC_KEY_HEX=90dcd81a473a4e59a84df6cb8f77af3d34c7fd6171ed959ca04a75f07a57b4b9
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
Run the server
|
|
68
|
-
|
|
69
|
-
```bash
|
|
70
|
-
bobine serve --env=./.env.local
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
Or you can install `@hazae41/bobine` and use the `serve()` function
|
|
74
|
-
|
|
75
|
-
### Making your own module
|
|
76
|
-
|
|
77
|
-
You can clone my [AssemblyScript starter](https://github.com/hazae41/create-bobine-assemblyscript-module) to get started with AssemblyScript
|
|
78
|
-
|
|
79
|
-
### Using the HTTP API
|
|
80
|
-
|
|
81
|
-
#### POST /api/create
|
|
82
|
-
|
|
83
|
-
Creates a new module using some WebAssembly code and some unique salt
|
|
84
|
-
|
|
85
|
-
Accepts a form data with the following fields
|
|
86
|
-
|
|
87
|
-
- `code` as bytes = your WebAssembly code (your .wasm file)
|
|
88
|
-
|
|
89
|
-
- `salt` as bytes = some bytes (any length, can be 0) that will be hashed with your code to produce your module address
|
|
90
|
-
|
|
91
|
-
- `effort` as bytes = some unique 32 bytes whose sha256 `hash` validates `((2n ** 256n) / BigInt("0x" + hash.toHex())) > (code.length + salt.length)`
|
|
92
|
-
|
|
93
|
-
#### POST /api/execute
|
|
94
|
-
|
|
95
|
-
Execute some function on a module
|
|
96
|
-
|
|
97
|
-
Accepts a form data with the following fields
|
|
98
|
-
|
|
99
|
-
- `module` as string = your module address (32-bytes hex)
|
|
100
|
-
|
|
101
|
-
- `method` as string = WebAssembly function to be executed
|
|
102
|
-
|
|
103
|
-
- `params` as bytes = WebAssembly function parameters as pack-encoded bytes (see pack encoding below)
|
|
104
|
-
|
|
105
|
-
- `effort` as bytes = some unique 32 bytes whose sha256 `hash` whose result in `((2n ** 256n) / BigInt("0x" + hash.toHex()))` will be the maximum number of sparks (gas) used
|
|
106
|
-
|
|
107
|
-
#### POST /api/simulate
|
|
108
|
-
|
|
109
|
-
Simulate some function on a module (it won't write anything to storage and will execute in mode `2` so verifications such as signatures can be skipped)
|
|
110
|
-
|
|
111
|
-
Accepts a form data with the following fields
|
|
112
|
-
|
|
113
|
-
- `module` as string = your module address (32-bytes hex)
|
|
114
|
-
|
|
115
|
-
- `method` as string = WebAssembly function to be executed
|
|
116
|
-
|
|
117
|
-
- `params` as bytes = WebAssembly function parameters as pack-encoded bytes (see pack encoding below)
|
|
118
|
-
|
|
119
|
-
- `effort` as bytes = some unique 32 bytes whose sha256 `hash` whose result in `((2n ** 256n) / BigInt("0x" + hash.toHex()))` will be the maximum number of sparks (gas) used
|
|
120
|
-
|
|
121
|
-
### Using the WebAssembly API
|
|
122
|
-
|
|
123
|
-
The WebAssembly VM extensively uses reference types for its API and for module-to-module communication
|
|
124
|
-
|
|
125
|
-
#### Using the WebAssembly API via AssemblyScript
|
|
126
|
-
|
|
127
|
-
You can use [stdbob](https://github.com/hazae41/stdbob) to easily import AssemblyScript declarations for all internal modules
|
|
128
|
-
|
|
129
|
-
Or you can declare internal modules manually with the module name and method name
|
|
130
|
-
|
|
131
|
-
```tsx
|
|
132
|
-
@external("bigints", "add")
|
|
133
|
-
declare function add(x: externref, y: externref): externref
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
And you can declare external modules by using the module address as hex
|
|
137
|
-
|
|
138
|
-
```tsx
|
|
139
|
-
@external("5feeee846376f6436990aa2757bc67fbc4498bcc9993b647788e273ad6fde474", "add")
|
|
140
|
-
declare function add(x: externref, y: externref): externref
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
#### Blobs module
|
|
144
|
-
|
|
145
|
-
You can pass bytes between modules by storing them in the blob storage and loading them via reference
|
|
146
|
-
|
|
147
|
-
#### BigInts module
|
|
148
|
-
|
|
149
|
-
You can work with infinite-precision bigints and convert them with blobs and texts
|
|
150
|
-
|
|
151
|
-
And many others
|
|
152
|
-
|
|
153
|
-
#### Packs module
|
|
154
|
-
|
|
155
|
-
You can pack various arguments (numbers, refs) into a pack which can be passed between modules and/or encoded/decoded into bytes
|
|
156
|
-
|
|
157
|
-
#### Environment module
|
|
158
|
-
|
|
159
|
-
Get infos about the executing environment
|
|
160
|
-
|
|
161
|
-
#### Modules module
|
|
162
|
-
|
|
163
|
-
Modules are identified by their address as a blob of bytes (pure sha256-output 32-length bytes without any encoding)
|
|
164
|
-
|
|
165
|
-
You can dynamically create modules, call modules, get their bytecode
|
|
166
|
-
|
|
167
|
-
#### Storage module
|
|
168
|
-
|
|
169
|
-
You can use a private key-value storage (it works like storage and events at the same time)
|
|
170
|
-
|
|
171
|
-
#### SHA-256 module
|
|
172
|
-
|
|
173
|
-
Use the SHA-256 hashing algorithm
|
|
174
|
-
|
|
175
|
-
#### Ed25519 module
|
|
176
|
-
|
|
177
|
-
Use the Ed25519 signing algorithm to verify any signature and (experimentally) sign payload using the miner's private key
|
|
178
|
-
|
|
179
|
-
#### Symbols module (experimental)
|
|
180
|
-
|
|
181
|
-
Create unique references that can be passed around
|
|
182
|
-
|
|
183
|
-
#### References module (experimental)
|
|
184
|
-
|
|
185
|
-
Translate any reference into a unique private pointer that can be stored into data structures
|
|
186
|
-
|
|
187
|
-
This can be useful if you want to check a reference for authenticity
|
|
188
|
-
|
|
189
|
-
```tsx
|
|
190
|
-
const sessions = new Set<i32>()
|
|
191
|
-
|
|
192
|
-
export function login(password: blobref): symbolref {
|
|
193
|
-
const session = symbols.create()
|
|
194
|
-
|
|
195
|
-
sessions.put(symbols.numerize(session))
|
|
196
|
-
|
|
197
|
-
return session
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export function verify(session: symbolref) {
|
|
201
|
-
return sessions.has(symbols.numerize(session))
|
|
202
|
-
}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
You should never accept a pointer instead of a real reference because they can be easily guessed by an attacking module
|
|
13
|
+
- [Awesome Bobine](https://github.com/hazae41/awesome-bobine) / A curated list on Bobine ecosystem
|
package/out/libs/packed/mod.d.ts
CHANGED
|
@@ -3,11 +3,11 @@ export type Packable = null | number | Uint8Array | string | bigint | Array<Pack
|
|
|
3
3
|
export declare class Packed {
|
|
4
4
|
readonly value: Packable;
|
|
5
5
|
constructor(value: Packable);
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
size(): number;
|
|
7
|
+
write(cursor: Cursor): void;
|
|
8
8
|
}
|
|
9
9
|
export declare namespace Packed {
|
|
10
|
-
function
|
|
11
|
-
function
|
|
12
|
-
function
|
|
10
|
+
function read(cursor: Cursor): Packable;
|
|
11
|
+
function size(value: Packable): number;
|
|
12
|
+
function write(value: Packable, cursor: Cursor): void;
|
|
13
13
|
}
|
package/out/libs/packed/mod.js
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
|
|
1
|
+
// deno-lint-ignore-file no-namespace
|
|
2
2
|
export class Packed {
|
|
3
3
|
value;
|
|
4
4
|
constructor(value) {
|
|
5
5
|
this.value = value;
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
return Packed.
|
|
7
|
+
size() {
|
|
8
|
+
return Packed.size(this.value);
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
Packed.
|
|
10
|
+
write(cursor) {
|
|
11
|
+
Packed.write(this.value, cursor);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
(function (Packed) {
|
|
15
|
-
function
|
|
16
|
-
const type = cursor.
|
|
15
|
+
function read(cursor) {
|
|
16
|
+
const type = cursor.readUint8();
|
|
17
17
|
if (type === 0)
|
|
18
18
|
return null;
|
|
19
19
|
if (type === 1)
|
|
20
|
-
return cursor.
|
|
20
|
+
return cursor.readFloat64(true);
|
|
21
21
|
if (type === 2)
|
|
22
|
-
return cursor.
|
|
22
|
+
return cursor.read(cursor.readUint32(true));
|
|
23
23
|
if (type === 3) {
|
|
24
|
-
const size = cursor.
|
|
25
|
-
const data = cursor.
|
|
24
|
+
const size = cursor.readUint32(true);
|
|
25
|
+
const data = cursor.read(size);
|
|
26
26
|
return new TextDecoder().decode(data);
|
|
27
27
|
}
|
|
28
28
|
if (type === 4) {
|
|
29
|
-
const negative = cursor.
|
|
30
|
-
const size = cursor.
|
|
31
|
-
const data = cursor.
|
|
29
|
+
const negative = cursor.readUint8();
|
|
30
|
+
const size = cursor.readUint32(true);
|
|
31
|
+
const data = cursor.read(size);
|
|
32
32
|
const absolute = BigInt("0x" + data.toHex());
|
|
33
33
|
return negative ? -absolute : absolute;
|
|
34
34
|
}
|
|
35
35
|
if (type === 5) {
|
|
36
|
-
const length = cursor.
|
|
36
|
+
const length = cursor.readUint32(true);
|
|
37
37
|
const values = new Array(length);
|
|
38
38
|
for (let i = 0; i < length; i++)
|
|
39
|
-
values[i] =
|
|
39
|
+
values[i] = read(cursor);
|
|
40
40
|
return values;
|
|
41
41
|
}
|
|
42
42
|
throw new Error("Unknown pack type");
|
|
43
43
|
}
|
|
44
|
-
Packed.
|
|
45
|
-
function
|
|
44
|
+
Packed.read = read;
|
|
45
|
+
function size(value) {
|
|
46
46
|
if (value == null)
|
|
47
47
|
return 1;
|
|
48
48
|
if (typeof value === "number")
|
|
@@ -58,57 +58,57 @@ export class Packed {
|
|
|
58
58
|
return 1 + 1 + 4 + data.length;
|
|
59
59
|
}
|
|
60
60
|
if (Array.isArray(value)) {
|
|
61
|
-
let
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
let total = 0;
|
|
62
|
+
total += 1;
|
|
63
|
+
total += 4;
|
|
64
64
|
for (const subvalue of value)
|
|
65
|
-
|
|
66
|
-
return
|
|
65
|
+
total += size(subvalue);
|
|
66
|
+
return total;
|
|
67
67
|
}
|
|
68
68
|
throw new Error("Unknown pack value");
|
|
69
69
|
}
|
|
70
|
-
Packed.
|
|
71
|
-
function
|
|
70
|
+
Packed.size = size;
|
|
71
|
+
function write(value, cursor) {
|
|
72
72
|
if (value == null) {
|
|
73
|
-
cursor.
|
|
73
|
+
cursor.writeUint8(0);
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
76
|
if (typeof value === "number") {
|
|
77
|
-
cursor.
|
|
78
|
-
cursor.
|
|
77
|
+
cursor.writeUint8(1);
|
|
78
|
+
cursor.writeFloat64(value, true);
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
81
|
if (value instanceof Uint8Array) {
|
|
82
|
-
cursor.
|
|
83
|
-
cursor.
|
|
84
|
-
cursor.
|
|
82
|
+
cursor.writeUint8(2);
|
|
83
|
+
cursor.writeUint32(value.length, true);
|
|
84
|
+
cursor.write(value);
|
|
85
85
|
return;
|
|
86
86
|
}
|
|
87
87
|
if (typeof value === "string") {
|
|
88
|
-
cursor.
|
|
88
|
+
cursor.writeUint8(3);
|
|
89
89
|
const data = new TextEncoder().encode(value);
|
|
90
|
-
cursor.
|
|
91
|
-
cursor.
|
|
90
|
+
cursor.writeUint32(data.length, true);
|
|
91
|
+
cursor.write(data);
|
|
92
92
|
return;
|
|
93
93
|
}
|
|
94
94
|
if (typeof value === "bigint") {
|
|
95
|
-
cursor.
|
|
95
|
+
cursor.writeUint8(4);
|
|
96
96
|
const [negative, absolute] = value < 0n ? [1, -value] : [0, value];
|
|
97
97
|
const text = absolute.toString(16);
|
|
98
98
|
const data = Uint8Array.fromHex(text.length % 2 === 1 ? "0" + text : text);
|
|
99
|
-
cursor.
|
|
100
|
-
cursor.
|
|
101
|
-
cursor.
|
|
99
|
+
cursor.writeUint8(negative);
|
|
100
|
+
cursor.writeUint32(data.length, true);
|
|
101
|
+
cursor.write(data);
|
|
102
102
|
return;
|
|
103
103
|
}
|
|
104
104
|
if (Array.isArray(value)) {
|
|
105
|
-
cursor.
|
|
106
|
-
cursor.
|
|
105
|
+
cursor.writeUint8(5);
|
|
106
|
+
cursor.writeUint32(value.length, true);
|
|
107
107
|
for (const subvalue of value)
|
|
108
|
-
|
|
108
|
+
write(subvalue, cursor);
|
|
109
109
|
return;
|
|
110
110
|
}
|
|
111
111
|
throw new Error("Unknown pack value");
|
|
112
112
|
}
|
|
113
|
-
Packed.
|
|
113
|
+
Packed.write = write;
|
|
114
114
|
})(Packed || (Packed = {}));
|
package/out/libs/sql/mod.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { Database } from "@tursodatabase/database";
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function
|
|
2
|
+
export declare function runAsImmediate<T>(database: Database, callback: (database: Database) => Promise<T>): Promise<T>;
|
|
3
|
+
export declare function runAsDeferred<T>(database: Database, callback: (database: Database) => Promise<T>): Promise<T>;
|
package/out/libs/sql/mod.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export async function
|
|
1
|
+
export async function runAsImmediate(database, callback) {
|
|
2
2
|
await database.exec("BEGIN IMMEDIATE TRANSACTION;");
|
|
3
3
|
try {
|
|
4
4
|
const result = await callback(database);
|
|
@@ -10,7 +10,7 @@ export async function runAsImmediateOrThrow(database, callback) {
|
|
|
10
10
|
throw e;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
export async function
|
|
13
|
+
export async function runAsDeferred(database, callback) {
|
|
14
14
|
await database.exec("BEGIN DEFERRED TRANSACTION;");
|
|
15
15
|
try {
|
|
16
16
|
const result = await callback(database);
|
package/out/mod.d.ts
CHANGED
package/out/mod.js
CHANGED
|
@@ -1,25 +1 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
export * from "./mods/mod.js";
|
|
3
|
-
import process from "node:process";
|
|
4
|
-
import * as server from "./mods/server/bin.js";
|
|
5
|
-
export async function main(args) {
|
|
6
|
-
const subargs = new Array();
|
|
7
|
-
for (let i = 0; i < args.length; i++) {
|
|
8
|
-
const arg = args[i];
|
|
9
|
-
if (arg.startsWith("--")) {
|
|
10
|
-
subargs.push(arg);
|
|
11
|
-
continue;
|
|
12
|
-
}
|
|
13
|
-
if (arg === "serve") {
|
|
14
|
-
subargs.push(...args.slice(i + 1));
|
|
15
|
-
await server.main(subargs);
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
break;
|
|
19
|
-
}
|
|
20
|
-
console.log("serve [--env=<env file as path>] [--port=<port>] [--dev]");
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (import.meta.main) {
|
|
24
|
-
await main(process.argv.slice(2));
|
|
25
|
-
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { RpcError } from "@hazae41/jsonrpc";
|
|
3
|
+
export class RpcPaymentError extends RpcError {
|
|
4
|
+
#class = _a;
|
|
5
|
+
name = this.#class.name;
|
|
6
|
+
static code = -32002;
|
|
7
|
+
static message = "Payment required";
|
|
8
|
+
constructor(message = _a.message) {
|
|
9
|
+
super(_a.code, message);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
_a = RpcPaymentError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Database } from "@tursodatabase/database";
|
|
2
|
+
export declare class Runner {
|
|
3
|
+
#private;
|
|
4
|
+
readonly uuid: string;
|
|
5
|
+
readonly path: string;
|
|
6
|
+
readonly data: Database;
|
|
7
|
+
constructor(uuid: string, path: string, data: Database);
|
|
8
|
+
setup(): Promise<void>;
|
|
9
|
+
create(code: Uint8Array<ArrayBuffer>, salt: Uint8Array<ArrayBuffer>): Promise<string>;
|
|
10
|
+
execute(module: string, method: string, params: Uint8Array<ArrayBuffer>, target: bigint): Promise<Uint8Array<ArrayBuffer>>;
|
|
11
|
+
simulate(module: string, method: string, params: Uint8Array<ArrayBuffer>, target: bigint): Promise<Uint8Array<ArrayBuffer>>;
|
|
12
|
+
}
|