@the-w1zard/singularity 1.0.10 → 1.0.12
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.html +2946 -5
- package/README.md +2974 -0
- package/dist/comps/DISRUPTOR_CRYPTO.js +293 -0
- package/dist/comps/binary.js +611 -0
- package/dist/comps/byte_id.js +40 -0
- package/dist/comps/cell-worker.js +120 -0
- package/dist/comps/cell.js +2478 -0
- package/dist/comps/create_live_self.js +22 -0
- package/dist/comps/queue.js +532 -0
- package/dist/comps/vm-types.js +1 -0
- package/dist/db/client/kv_idb.js +590 -0
- package/dist/db/server/bench.js +28 -0
- package/dist/db/server/binary.js +43 -0
- package/dist/db/server/constants.js +8 -0
- package/dist/db/server/crc.js +15 -0
- package/dist/db/server/db.js +1475 -0
- package/dist/db/server/manifest.js +34 -0
- package/dist/db/server/pages.js +100 -0
- package/dist/db/server/storage-worker.js +152 -0
- package/dist/db/server/types.js +14 -0
- package/dist/db/server/wal.js +81 -0
- package/dist/index.js +1 -0
- package/dist/signals.js +18435 -0
- package/dist/y.js +1324 -0
- package/package.json +14 -6
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
export class DISRUPTOR_CRYPTO {
|
|
2
|
+
ser(value) {
|
|
3
|
+
if (value instanceof Map) {
|
|
4
|
+
return JSON.stringify({ __type: "Map", value: Array.from(value.entries()).map(([key, val]) => [this.ser(key), this.ser(val)]) });
|
|
5
|
+
}
|
|
6
|
+
if (value instanceof Set) {
|
|
7
|
+
return JSON.stringify({ __type: "Set", value: Array.from(value.values()).map((value) => this.ser(value)) });
|
|
8
|
+
}
|
|
9
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
10
|
+
}
|
|
11
|
+
deser(raw) {
|
|
12
|
+
if (raw === null)
|
|
13
|
+
return null;
|
|
14
|
+
try {
|
|
15
|
+
const parsed = JSON.parse(raw);
|
|
16
|
+
if (parsed?.__type === "Map") {
|
|
17
|
+
return new Map(parsed.value.map(([key, value]) => [this.deser(key), this.deser(value)]));
|
|
18
|
+
}
|
|
19
|
+
if (parsed?.__type === "Set") {
|
|
20
|
+
return new Set(parsed.value.map((value) => this.deser(value)));
|
|
21
|
+
}
|
|
22
|
+
return parsed;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return raw;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
hasNode = typeof process !== "undefined" && !!process.versions?.node;
|
|
29
|
+
_buf(bytes) {
|
|
30
|
+
const out = new Uint8Array(new ArrayBuffer(bytes.byteLength));
|
|
31
|
+
out.set(bytes);
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
_hasSubtle = typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.subtle !== "undefined" && typeof globalThis.crypto.getRandomValues === "function";
|
|
35
|
+
_nodeCrypto = null;
|
|
36
|
+
_getNodeCrypto() {
|
|
37
|
+
if (this._nodeCrypto)
|
|
38
|
+
return this._nodeCrypto;
|
|
39
|
+
if (!this.hasNode)
|
|
40
|
+
return null;
|
|
41
|
+
try {
|
|
42
|
+
this._nodeCrypto = require("node:crypto");
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
this._nodeCrypto = null;
|
|
46
|
+
}
|
|
47
|
+
return this._nodeCrypto;
|
|
48
|
+
}
|
|
49
|
+
constructor() { }
|
|
50
|
+
// ── random bytes ────────────────────────────────────────────────────────
|
|
51
|
+
random_bytes(size = 32) {
|
|
52
|
+
if (this._hasSubtle)
|
|
53
|
+
return globalThis.crypto.getRandomValues(new Uint8Array(size));
|
|
54
|
+
const nc = this._getNodeCrypto();
|
|
55
|
+
if (nc)
|
|
56
|
+
return new Uint8Array(nc.randomBytes(size));
|
|
57
|
+
// last resort — NOT cryptographically secure, but the bus will refuse
|
|
58
|
+
// to init auth without real randomness, so this never protects anything.
|
|
59
|
+
const out = new Uint8Array(size);
|
|
60
|
+
for (let i = 0; i < size; i++)
|
|
61
|
+
out[i] = (Math.random() * 256) | 0;
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
// ── HMAC-SHA256 ─────────────────────────────────────────────────────────
|
|
65
|
+
// Returned as hex so it matches the existing computeHmacSha256 contract.
|
|
66
|
+
// Async version — used when WebCrypto is the only option.
|
|
67
|
+
async hash_hmac_hex_async(secret, data) {
|
|
68
|
+
const keyBytes = typeof secret === "string" ? this.to_utf8_bytes(secret) : secret;
|
|
69
|
+
const dataBytes = typeof data === "string" ? this.to_utf8_bytes(data) : data;
|
|
70
|
+
if (this._hasSubtle) {
|
|
71
|
+
const key = await globalThis.crypto.subtle.importKey("raw", this._buf(keyBytes), // ← wrap
|
|
72
|
+
{ name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
73
|
+
const sig = await globalThis.crypto.subtle.sign("HMAC", key, this._buf(dataBytes));
|
|
74
|
+
return this.to_hex(new Uint8Array(sig));
|
|
75
|
+
}
|
|
76
|
+
const nc = this._getNodeCrypto();
|
|
77
|
+
if (nc) {
|
|
78
|
+
const h = nc.createHmac("sha256", Buffer.from(keyBytes));
|
|
79
|
+
h.update(Buffer.from(dataBytes));
|
|
80
|
+
return h.digest("hex");
|
|
81
|
+
}
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
// Cache used by the sync path when only WebCrypto is available.
|
|
85
|
+
// Populated by hash_hmac_hex_async's callers, read by hash_hmac_hex.
|
|
86
|
+
_hmacCache = new Map();
|
|
87
|
+
// ── SHA-256 ─────────────────────────────────────────────────────────────
|
|
88
|
+
async hash_sha256_hex(data) {
|
|
89
|
+
const bytes = typeof data === "string" ? this.to_utf8_bytes(data) : data;
|
|
90
|
+
if (this._hasSubtle) {
|
|
91
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", this._buf(bytes));
|
|
92
|
+
return this.to_hex(new Uint8Array(digest));
|
|
93
|
+
}
|
|
94
|
+
const nc = this._getNodeCrypto();
|
|
95
|
+
if (nc) {
|
|
96
|
+
const h = nc.createHash("sha256");
|
|
97
|
+
h.update(Buffer.from(bytes));
|
|
98
|
+
return h.digest("hex");
|
|
99
|
+
}
|
|
100
|
+
return "";
|
|
101
|
+
}
|
|
102
|
+
// ── HKDF (derive a session key from a shared secret) ────────────────────
|
|
103
|
+
async derive_hkdf(ikm, salt, info, length = 32) {
|
|
104
|
+
if (this._hasSubtle) {
|
|
105
|
+
const key = await globalThis.crypto.subtle.importKey("raw", this._buf(ikm), // ← wrap
|
|
106
|
+
"HKDF", false, ["deriveBits"]);
|
|
107
|
+
const bits = await globalThis.crypto.subtle.deriveBits({
|
|
108
|
+
name: "HKDF",
|
|
109
|
+
hash: "SHA-256",
|
|
110
|
+
salt: this._buf(salt), // ← wrap
|
|
111
|
+
info: this._buf(info), // ← wrap
|
|
112
|
+
}, key, length * 8);
|
|
113
|
+
return new Uint8Array(bits);
|
|
114
|
+
}
|
|
115
|
+
const nc = this._getNodeCrypto();
|
|
116
|
+
if (nc) {
|
|
117
|
+
return new Uint8Array(nc.hkdfSync("sha256", Buffer.from(ikm), Buffer.from(salt), Buffer.from(info), length));
|
|
118
|
+
}
|
|
119
|
+
return new Uint8Array(length);
|
|
120
|
+
}
|
|
121
|
+
// ── AES-GCM (the bus's symmetric p encryption) ────────────────────
|
|
122
|
+
async encrypt_aes_gcm(key, iv, data) {
|
|
123
|
+
if (this._hasSubtle) {
|
|
124
|
+
const k = await globalThis.crypto.subtle.importKey("raw", this._buf(key), // ← wrap
|
|
125
|
+
"AES-GCM", false, ["encrypt"]);
|
|
126
|
+
const ct = await globalThis.crypto.subtle.encrypt({ name: "AES-GCM", iv: this._buf(iv) }, // ← wrap
|
|
127
|
+
k, this._buf(data));
|
|
128
|
+
return new Uint8Array(ct);
|
|
129
|
+
}
|
|
130
|
+
const nc = this._getNodeCrypto();
|
|
131
|
+
if (nc) {
|
|
132
|
+
const cipher = nc.createCipheriv("aes-256-gcm", Buffer.from(key), Buffer.from(iv));
|
|
133
|
+
const ct = Buffer.concat([cipher.update(Buffer.from(data)), cipher.final()]);
|
|
134
|
+
const tag = cipher.getAuthTag();
|
|
135
|
+
// append tag, matching WebCrypto's output format
|
|
136
|
+
return new Uint8Array(Buffer.concat([ct, tag]));
|
|
137
|
+
}
|
|
138
|
+
return data;
|
|
139
|
+
}
|
|
140
|
+
async decrypt_aes_gcm(key, iv, ct) {
|
|
141
|
+
if (this._hasSubtle) {
|
|
142
|
+
const k = await globalThis.crypto.subtle.importKey("raw", this._buf(key), // ← wrap
|
|
143
|
+
"AES-GCM", false, ["decrypt"]);
|
|
144
|
+
const pt = await globalThis.crypto.subtle.decrypt({ name: "AES-GCM", iv: this._buf(iv) }, // ← wrap
|
|
145
|
+
k, this._buf(ct));
|
|
146
|
+
return new Uint8Array(pt);
|
|
147
|
+
}
|
|
148
|
+
const nc = this._getNodeCrypto();
|
|
149
|
+
if (nc) {
|
|
150
|
+
// split off the 16-byte tag from the end
|
|
151
|
+
const tag = ct.slice(ct.length - 16);
|
|
152
|
+
const body = ct.slice(0, ct.length - 16);
|
|
153
|
+
const decipher = nc.createDecipheriv("aes-256-gcm", Buffer.from(key), Buffer.from(iv));
|
|
154
|
+
decipher.setAuthTag(Buffer.from(tag));
|
|
155
|
+
const pt = Buffer.concat([decipher.update(Buffer.from(body)), decipher.final()]);
|
|
156
|
+
return new Uint8Array(pt);
|
|
157
|
+
}
|
|
158
|
+
return ct;
|
|
159
|
+
}
|
|
160
|
+
// ── Encoding ────────────────────────────────────────────────────────────
|
|
161
|
+
to_utf8_bytes(str) {
|
|
162
|
+
if (typeof TextEncoder !== "undefined")
|
|
163
|
+
return new TextEncoder().encode(str);
|
|
164
|
+
const nc = this._getNodeCrypto();
|
|
165
|
+
if (nc)
|
|
166
|
+
return new Uint8Array(Buffer.from(str, "utf8"));
|
|
167
|
+
// last resort
|
|
168
|
+
const out = new Uint8Array(str.length);
|
|
169
|
+
for (let i = 0; i < str.length; i++)
|
|
170
|
+
out[i] = str.charCodeAt(i) & 0xff;
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
to_hex(data) {
|
|
174
|
+
if (typeof Buffer !== "undefined")
|
|
175
|
+
return Buffer.from(data).toString("hex");
|
|
176
|
+
let out = "";
|
|
177
|
+
for (let i = 0; i < data.length; i++)
|
|
178
|
+
out += data[i].toString(16).padStart(2, "0");
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
from_hex(hex) {
|
|
182
|
+
if (typeof Buffer !== "undefined")
|
|
183
|
+
return new Uint8Array(Buffer.from(hex, "hex"));
|
|
184
|
+
const out = new Uint8Array(hex.length / 2);
|
|
185
|
+
for (let i = 0; i < hex.length; i += 2)
|
|
186
|
+
out[i / 2] = parseInt(hex.slice(i, i + 2), 16);
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
to_base64(data) {
|
|
190
|
+
if (typeof Buffer !== "undefined")
|
|
191
|
+
return Buffer.from(data).toString("base64");
|
|
192
|
+
let bin = "";
|
|
193
|
+
for (let i = 0; i < data.length; i++)
|
|
194
|
+
bin += String.fromCharCode(data[i]);
|
|
195
|
+
return btoa(bin);
|
|
196
|
+
}
|
|
197
|
+
from_base64(str) {
|
|
198
|
+
if (typeof Buffer !== "undefined")
|
|
199
|
+
return new Uint8Array(Buffer.from(str, "base64"));
|
|
200
|
+
const bin = atob(str);
|
|
201
|
+
const out = new Uint8Array(bin.length);
|
|
202
|
+
for (let i = 0; i < bin.length; i++)
|
|
203
|
+
out[i] = bin.charCodeAt(i);
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
// ── Constants ───────────────────────────────────────────────────────────
|
|
207
|
+
ready() {
|
|
208
|
+
return this._hasSubtle || this._getNodeCrypto() !== null;
|
|
209
|
+
}
|
|
210
|
+
secure_equal(a, b) {
|
|
211
|
+
let diff = a.length ^ b.length;
|
|
212
|
+
const len = Math.max(a.length, b.length);
|
|
213
|
+
for (let i = 0; i < len; i++)
|
|
214
|
+
diff |= (a[i] ?? 0) ^ (b[i] ?? 0);
|
|
215
|
+
return diff === 0;
|
|
216
|
+
}
|
|
217
|
+
hash_hmac_hex(secret, data) {
|
|
218
|
+
const nc = this._getNodeCrypto();
|
|
219
|
+
if (!nc)
|
|
220
|
+
throw new Error("Synchronous HMAC requires Node crypto");
|
|
221
|
+
const key = typeof secret === "string" ? this.to_utf8_bytes(secret) : secret;
|
|
222
|
+
const bytes = typeof data === "string" ? this.to_utf8_bytes(data) : data;
|
|
223
|
+
return nc.createHmac("sha256", Buffer.from(key)).update(Buffer.from(bytes)).digest("hex");
|
|
224
|
+
}
|
|
225
|
+
hash_hmac(secret, data) {
|
|
226
|
+
return this.hash_hmac_hex(secret, data);
|
|
227
|
+
}
|
|
228
|
+
hash_sha256(data) {
|
|
229
|
+
const nc = this._getNodeCrypto();
|
|
230
|
+
if (!nc)
|
|
231
|
+
throw new Error("Synchronous hashing requires Node crypto");
|
|
232
|
+
const bytes = typeof data === "string" ? this.to_utf8_bytes(data) : data;
|
|
233
|
+
return nc.createHash("sha256").update(Buffer.from(bytes)).digest("hex");
|
|
234
|
+
}
|
|
235
|
+
derive_key(password, salt, iterations = 310_000, length = 32) {
|
|
236
|
+
const nc = this._getNodeCrypto();
|
|
237
|
+
if (!nc)
|
|
238
|
+
throw new Error("Synchronous PBKDF2 requires Node crypto");
|
|
239
|
+
return new Uint8Array(nc.pbkdf2Sync(Buffer.from(this.to_utf8_bytes(password)), Buffer.from(salt), iterations, length, "sha256"));
|
|
240
|
+
}
|
|
241
|
+
symmetric_encrypt(p, key) {
|
|
242
|
+
const nc = this._getNodeCrypto();
|
|
243
|
+
if (!nc)
|
|
244
|
+
throw new Error("Synchronous encryption requires Node crypto");
|
|
245
|
+
if (key.length !== 32)
|
|
246
|
+
throw new Error("AES-256-GCM requires a 32-byte key");
|
|
247
|
+
const nonce = nc.randomBytes(12);
|
|
248
|
+
const cipher = nc.createCipheriv("aes-256-gcm", Buffer.from(key), nonce);
|
|
249
|
+
const ciphertext = Buffer.concat([cipher.update(p, "utf8"), cipher.final(), cipher.getAuthTag()]);
|
|
250
|
+
return {
|
|
251
|
+
ciphertext: this.to_base64(new Uint8Array(ciphertext)),
|
|
252
|
+
nonce: this.to_base64(new Uint8Array(nonce)),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
symmetric_decrypt(p, key) {
|
|
256
|
+
const nc = this._getNodeCrypto();
|
|
257
|
+
if (!nc)
|
|
258
|
+
throw new Error("Synchronous decryption requires Node crypto");
|
|
259
|
+
if (key.length !== 32)
|
|
260
|
+
throw new Error("AES-256-GCM requires a 32-byte key");
|
|
261
|
+
const ciphertext = this.from_base64(p.ciphertext);
|
|
262
|
+
const nonce = this.from_base64(p.nonce);
|
|
263
|
+
if (nonce.length !== 12 || ciphertext.length < 16)
|
|
264
|
+
throw new Error("Invalid AES-GCM payload");
|
|
265
|
+
const body = ciphertext.slice(0, -16);
|
|
266
|
+
const tag = ciphertext.slice(-16);
|
|
267
|
+
const decipher = nc.createDecipheriv("aes-256-gcm", Buffer.from(key), Buffer.from(nonce));
|
|
268
|
+
decipher.setAuthTag(Buffer.from(tag));
|
|
269
|
+
return Buffer.concat([decipher.update(Buffer.from(body)), decipher.final()]).toString("utf8");
|
|
270
|
+
}
|
|
271
|
+
generate_key_pair(_type) {
|
|
272
|
+
const nc = this._getNodeCrypto();
|
|
273
|
+
if (!nc)
|
|
274
|
+
throw new Error("Synchronous key generation requires Node crypto");
|
|
275
|
+
const pair = nc.generateKeyPairSync("ed25519");
|
|
276
|
+
return {
|
|
277
|
+
publicKey: new Uint8Array(pair.publicKey.export({ type: "spki", format: "der" })),
|
|
278
|
+
secretKey: new Uint8Array(pair.privateKey.export({ type: "pkcs8", format: "der" })),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
pqc_generate_KEM_keys() {
|
|
282
|
+
const nc = this._getNodeCrypto();
|
|
283
|
+
if (!nc)
|
|
284
|
+
throw new Error("PQC key generation requires a supported Node crypto provider");
|
|
285
|
+
const pair = nc.generateKeyPairSync("ml-kem-768");
|
|
286
|
+
return {
|
|
287
|
+
publicKey: new Uint8Array(pair.publicKey.export({ type: "spki", format: "der" })),
|
|
288
|
+
secretKey: new Uint8Array(pair.privateKey.export({ type: "pkcs8", format: "der" })),
|
|
289
|
+
algorithm: "ML-KEM-768",
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
export const BUS_CRYPTO = new DISRUPTOR_CRYPTO();
|