@hazae41/bobine 0.0.22 โ†’ 0.1.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 CHANGED
@@ -1,205 +1,13 @@
1
- # Bobine ๐Ÿงต
1
+ # Bobine
2
2
 
3
- A blockchain in your garage
3
+ WebAssembly-based metered chain of compute
4
4
 
5
5
  ```bash
6
- deno install -gf -A npm:@hazae41/bobine
6
+ npm install @hazae41/bobine
7
7
  ```
8
8
 
9
- [**๐ŸŒ Website**](https://bobine.tech/) โ€ข [**๐Ÿ“ฆ NPM**](https://www.npmjs.com/package/@hazae41/bobine)
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
@@ -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
- sizeOrThrow(): number;
7
- writeOrThrow(cursor: Cursor): void;
6
+ size(): number;
7
+ write(cursor: Cursor): void;
8
8
  }
9
9
  export declare namespace Packed {
10
- function readOrThrow(cursor: Cursor): Packable;
11
- function sizeOrThrow(value: Packable): number;
12
- function writeOrThrow(value: Packable, cursor: Cursor): void;
10
+ function read(cursor: Cursor): Packable;
11
+ function size(value: Packable): number;
12
+ function write(value: Packable, cursor: Cursor): void;
13
13
  }
@@ -1,48 +1,48 @@
1
- /// <reference types="../bytes/lib.d.ts"/>
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
- sizeOrThrow() {
8
- return Packed.sizeOrThrow(this.value);
7
+ size() {
8
+ return Packed.size(this.value);
9
9
  }
10
- writeOrThrow(cursor) {
11
- Packed.writeOrThrow(this.value, cursor);
10
+ write(cursor) {
11
+ Packed.write(this.value, cursor);
12
12
  }
13
13
  }
14
14
  (function (Packed) {
15
- function readOrThrow(cursor) {
16
- const type = cursor.readUint8OrThrow();
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.readFloat64OrThrow(true);
20
+ return cursor.readFloat64(true);
21
21
  if (type === 2)
22
- return cursor.readOrThrow(cursor.readUint32OrThrow(true));
22
+ return cursor.read(cursor.readUint32(true));
23
23
  if (type === 3) {
24
- const size = cursor.readUint32OrThrow(true);
25
- const data = cursor.readOrThrow(size);
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.readUint8OrThrow();
30
- const size = cursor.readUint32OrThrow(true);
31
- const data = cursor.readOrThrow(size);
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.readUint32OrThrow(true);
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] = readOrThrow(cursor);
39
+ values[i] = read(cursor);
40
40
  return values;
41
41
  }
42
42
  throw new Error("Unknown pack type");
43
43
  }
44
- Packed.readOrThrow = readOrThrow;
45
- function sizeOrThrow(value) {
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 size = 0;
62
- size += 1;
63
- size += 4;
61
+ let total = 0;
62
+ total += 1;
63
+ total += 4;
64
64
  for (const subvalue of value)
65
- size += sizeOrThrow(subvalue);
66
- return size;
65
+ total += size(subvalue);
66
+ return total;
67
67
  }
68
68
  throw new Error("Unknown pack value");
69
69
  }
70
- Packed.sizeOrThrow = sizeOrThrow;
71
- function writeOrThrow(value, cursor) {
70
+ Packed.size = size;
71
+ function write(value, cursor) {
72
72
  if (value == null) {
73
- cursor.writeUint8OrThrow(0);
73
+ cursor.writeUint8(0);
74
74
  return;
75
75
  }
76
76
  if (typeof value === "number") {
77
- cursor.writeUint8OrThrow(1);
78
- cursor.writeFloat64OrThrow(value, true);
77
+ cursor.writeUint8(1);
78
+ cursor.writeFloat64(value, true);
79
79
  return;
80
80
  }
81
81
  if (value instanceof Uint8Array) {
82
- cursor.writeUint8OrThrow(2);
83
- cursor.writeUint32OrThrow(value.length, true);
84
- cursor.writeOrThrow(value);
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.writeUint8OrThrow(3);
88
+ cursor.writeUint8(3);
89
89
  const data = new TextEncoder().encode(value);
90
- cursor.writeUint32OrThrow(data.length, true);
91
- cursor.writeOrThrow(data);
90
+ cursor.writeUint32(data.length, true);
91
+ cursor.write(data);
92
92
  return;
93
93
  }
94
94
  if (typeof value === "bigint") {
95
- cursor.writeUint8OrThrow(4);
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.writeUint8OrThrow(negative);
100
- cursor.writeUint32OrThrow(data.length, true);
101
- cursor.writeOrThrow(data);
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.writeUint8OrThrow(5);
106
- cursor.writeUint32OrThrow(value.length, true);
105
+ cursor.writeUint8(5);
106
+ cursor.writeUint32(value.length, true);
107
107
  for (const subvalue of value)
108
- writeOrThrow(subvalue, cursor);
108
+ write(subvalue, cursor);
109
109
  return;
110
110
  }
111
111
  throw new Error("Unknown pack value");
112
112
  }
113
- Packed.writeOrThrow = writeOrThrow;
113
+ Packed.write = write;
114
114
  })(Packed || (Packed = {}));
@@ -1,3 +1,3 @@
1
1
  import type { Database } from "@tursodatabase/database";
2
- export declare function runAsImmediateOrThrow<T>(database: Database, callback: (database: Database) => Promise<T>): Promise<T>;
3
- export declare function runAsDeferredOrThrow<T>(database: Database, callback: (database: Database) => Promise<T>): Promise<T>;
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>;
@@ -1,4 +1,4 @@
1
- export async function runAsImmediateOrThrow(database, callback) {
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 runAsDeferredOrThrow(database, callback) {
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
@@ -1,3 +1 @@
1
- #!/usr/bin/env node
2
1
  export * from "./mods/mod.ts";
3
- export declare function main(args: string[]): Promise<void>;
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,8 @@
1
+ import { RpcError } from "@hazae41/jsonrpc";
2
+ export declare class RpcPaymentError extends RpcError {
3
+ #private;
4
+ readonly name: string;
5
+ static readonly code = -32002;
6
+ static readonly message = "Payment required";
7
+ constructor(message?: string);
8
+ }
@@ -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,3 @@
1
+ export * from "./errors/mod.ts";
2
+ export * from "./runner/mod.ts";
3
+ export * from "./worker/mod.ts";
@@ -0,0 +1,3 @@
1
+ export * from "./errors/mod.js";
2
+ export * from "./runner/mod.js";
3
+ export * from "./worker/mod.js";
@@ -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
+ }