@the9ines/bolt-core 0.5.1 → 0.6.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/dist/btr/constants.d.ts +20 -0
- package/dist/btr/constants.js +25 -0
- package/dist/btr/encrypt.d.ts +26 -0
- package/dist/btr/encrypt.js +56 -0
- package/dist/btr/errors.d.ts +28 -0
- package/dist/btr/errors.js +38 -0
- package/dist/btr/index.d.ts +15 -0
- package/dist/btr/index.js +21 -0
- package/dist/btr/key-schedule.d.ts +33 -0
- package/dist/btr/key-schedule.js +38 -0
- package/dist/btr/negotiate.d.ts +34 -0
- package/dist/btr/negotiate.js +50 -0
- package/dist/btr/ratchet.d.ts +34 -0
- package/dist/btr/ratchet.js +44 -0
- package/dist/btr/replay.d.ts +30 -0
- package/dist/btr/replay.js +81 -0
- package/dist/btr/state.d.ts +81 -0
- package/dist/btr/state.js +146 -0
- package/dist/crypto.d.ts +4 -9
- package/dist/crypto.js +14 -9
- package/dist/errors.d.ts +2 -2
- package/dist/errors.js +6 -1
- package/dist/identity.d.ts +1 -3
- package/dist/identity.js +5 -3
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/sas.d.ts +6 -0
- package/dist/sas.js +13 -0
- package/dist/wasm-crypto.d.ts +118 -0
- package/dist/wasm-crypto.js +119 -0
- package/package.json +4 -3
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM-backed protocol adapter (RUSTIFY-BROWSER-CORE-1 RB3+RB4).
|
|
3
|
+
*
|
|
4
|
+
* RB3: crypto/session/SAS functions backed by Rust/WASM.
|
|
5
|
+
* RB4: BTR state (BtrEngine, BtrTransferCtx) + transfer state (SendSession)
|
|
6
|
+
* backed by Rust/WASM opaque handles.
|
|
7
|
+
*
|
|
8
|
+
* TS tweetnacl/BTR implementations remain as fallback (PM-RB-03: condition-gated).
|
|
9
|
+
*/
|
|
10
|
+
/** WASM adapter instance. null until initWasmCrypto() succeeds. */
|
|
11
|
+
let _wasmCrypto = null;
|
|
12
|
+
/** Whether WASM init has been attempted. */
|
|
13
|
+
let _initAttempted = false;
|
|
14
|
+
/**
|
|
15
|
+
* Get the WASM crypto adapter, or null if not initialized or init failed.
|
|
16
|
+
* Callers should fall back to TS crypto if this returns null.
|
|
17
|
+
*/
|
|
18
|
+
export function getWasmCrypto() {
|
|
19
|
+
return _wasmCrypto;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Initialize WASM crypto from a pre-loaded WASM module.
|
|
23
|
+
*
|
|
24
|
+
* BR2: Accepts an already-loaded+initialized WASM module (provided by
|
|
25
|
+
* transport-web's initProtocolWasm()). This avoids bare module specifier
|
|
26
|
+
* issues — the loader lives in transport-web where the artifact is embedded.
|
|
27
|
+
*
|
|
28
|
+
* PM-RB-03: TS fallback remains operational if this is never called.
|
|
29
|
+
*
|
|
30
|
+
* @param wasmModule - The loaded bolt-protocol-wasm module (with exported functions)
|
|
31
|
+
*/
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
export function initWasmCryptoFromModule(wasmModule) {
|
|
34
|
+
if (_wasmCrypto)
|
|
35
|
+
return true;
|
|
36
|
+
try {
|
|
37
|
+
_wasmModule = wasmModule;
|
|
38
|
+
_wasmCrypto = {
|
|
39
|
+
generateEphemeralKeyPair: () => wasmModule.generateEphemeralKeyPair(),
|
|
40
|
+
generateIdentityKeyPair: () => wasmModule.generateIdentityKeyPair(),
|
|
41
|
+
sealBoxPayload: (p, rpk, sk) => wasmModule.sealBoxPayload(p, rpk, sk),
|
|
42
|
+
openBoxPayload: (s, spk, rsk) => wasmModule.openBoxPayload(s, spk, rsk),
|
|
43
|
+
computeSas: (ia, ib, ea, eb) => wasmModule.computeSas(ia, ib, ea, eb),
|
|
44
|
+
generateSecurePeerCode: () => wasmModule.generateSecurePeerCode(),
|
|
45
|
+
isValidPeerCode: (c) => wasmModule.isValidPeerCode(c),
|
|
46
|
+
sha256Hex: (d) => wasmModule.sha256Hex(d),
|
|
47
|
+
};
|
|
48
|
+
console.log('[BOLT-WASM] Protocol authority initialized (Rust/WASM: crypto + BTR + transfer)');
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
console.warn('[BOLT-WASM] Failed to initialize from module:', e);
|
|
53
|
+
_wasmCrypto = null;
|
|
54
|
+
_wasmModule = null;
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Initialize WASM crypto. Legacy entry point — attempts dynamic import.
|
|
60
|
+
* Prefer initProtocolWasm() from @the9ines/bolt-transport-web instead.
|
|
61
|
+
*/
|
|
62
|
+
export async function initWasmCrypto() {
|
|
63
|
+
if (_initAttempted)
|
|
64
|
+
return _wasmCrypto !== null;
|
|
65
|
+
_initAttempted = true;
|
|
66
|
+
try {
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
const wasm = await (Function('return import("bolt-protocol-wasm")')());
|
|
69
|
+
await wasm.default();
|
|
70
|
+
return initWasmCryptoFromModule(wasm);
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
console.warn('[BOLT-WASM] Failed to initialize — falling back to TS protocol:', e);
|
|
74
|
+
_wasmCrypto = null;
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// ══════════════════════════════════════════════════════════════════
|
|
79
|
+
// RB4: BTR + Transfer State Authority
|
|
80
|
+
// ══════════════════════════════════════════════════════════════════
|
|
81
|
+
/** WASM module reference for constructing opaque handles. */
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
+
let _wasmModule = null;
|
|
84
|
+
/**
|
|
85
|
+
* Get the raw WASM module for constructing BTR/transfer handles.
|
|
86
|
+
* Returns null if WASM not initialized.
|
|
87
|
+
*/
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
+
export function getWasmModule() {
|
|
90
|
+
return _wasmModule;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create a WASM-backed BTR engine. Returns null if WASM not available.
|
|
94
|
+
* Caller must call .free() when done to zeroize key material.
|
|
95
|
+
*/
|
|
96
|
+
export function createWasmBtrEngine(sharedSecret) {
|
|
97
|
+
if (!_wasmModule)
|
|
98
|
+
return null;
|
|
99
|
+
try {
|
|
100
|
+
return new _wasmModule.WasmBtrEngine(sharedSecret);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create a WASM-backed send session. Returns null if WASM not available.
|
|
108
|
+
* Rust owns transfer-state transitions. TS proposes events; Rust validates.
|
|
109
|
+
*/
|
|
110
|
+
export function createWasmSendSession() {
|
|
111
|
+
if (!_wasmModule)
|
|
112
|
+
return null;
|
|
113
|
+
try {
|
|
114
|
+
return new _wasmModule.WasmSendSession();
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@the9ines/bolt-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Bolt Protocol core crypto primitives and utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -15,11 +15,12 @@
|
|
|
15
15
|
"build": "tsc",
|
|
16
16
|
"test": "vitest run",
|
|
17
17
|
"audit-exports": "node scripts/audit-exports.mjs",
|
|
18
|
-
"generate-vectors": "node scripts/print-test-vectors.mjs",
|
|
19
|
-
"check-vectors": "node scripts/print-test-vectors.mjs --check",
|
|
18
|
+
"generate-vectors": "echo '[DEPRECATED] Use: cargo test -p bolt-core --features vectors --test vector_golden_core' && node scripts/print-test-vectors.mjs",
|
|
19
|
+
"check-vectors": "echo '[DEPRECATED] Canonical vectors are now Rust-generated. Use: cargo test -p bolt-core --features vectors' && node scripts/print-test-vectors.mjs --check",
|
|
20
20
|
"prepublishOnly": "npm run build"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
+
"@noble/hashes": "^2.0.1",
|
|
23
24
|
"tweetnacl": "^1.0.3",
|
|
24
25
|
"tweetnacl-util": "^0.15.1"
|
|
25
26
|
},
|