45 lines
2.5 KiB
JavaScript
45 lines
2.5 KiB
JavaScript
// DeckCraft v0.9.0's recovery::instance_id calls std::process::id on wasm32.
|
|
// Seed its OnceLock before __wbindgen_start, using the browser's random source.
|
|
// This layout applies ONLY to the exact, SHA-256-checked official WASM below.
|
|
// Vendor assets and their SRI hashes are not rewritten. Future builds bypass this shim.
|
|
(() => {
|
|
"use strict";
|
|
const expected = "e0849d8de375fe7726a621f7e98112b76a7b0d0bc361d701bcae66b639c8e354";
|
|
const filename = "deckcraft-web-f413cd376cc2a98b_bg.wasm";
|
|
const address = 5695280; // OnceLock<u64>: value at +0, completed state byte at +8.
|
|
const streaming = WebAssembly.instantiateStreaming?.bind(WebAssembly);
|
|
const instantiate = WebAssembly.instantiate.bind(WebAssembly);
|
|
const verified = new WeakSet();
|
|
async function matches(bytes) {
|
|
const hash = await crypto.subtle.digest("SHA-256", bytes);
|
|
return Array.from(new Uint8Array(hash), value => value.toString(16).padStart(2, "0")).join("") === expected;
|
|
}
|
|
function initialize(result) {
|
|
const instance = result instanceof WebAssembly.Instance ? result : result.instance;
|
|
const memory = instance?.exports.memory;
|
|
if (!(memory instanceof WebAssembly.Memory) || memory.buffer.byteLength < address + 9) throw new Error("Unsupported DeckCraft recovery memory layout");
|
|
const view = new DataView(memory.buffer);
|
|
if (view.getBigUint64(address, true) !== 0n || view.getUint8(address + 8) !== 0) throw new Error("DeckCraft recovery state was already initialized");
|
|
// Same 53-bit/low-24-bit constraints as recovery::UID_MASK and instance_id.
|
|
const random = crypto.getRandomValues(new Uint32Array(1))[0] & 0x1fffffff;
|
|
view.setBigUint64(address, BigInt(random || 1) << 24n, true);
|
|
view.setUint8(address + 8, 3);
|
|
if (result.module) verified.add(result.module);
|
|
return result;
|
|
}
|
|
if (streaming) WebAssembly.instantiateStreaming = async (source, imports) => {
|
|
const response = await source;
|
|
const selected = new URL(response.url, location.href).pathname.endsWith("/" + filename);
|
|
const compatible = selected && await matches(await response.clone().arrayBuffer());
|
|
const result = await streaming(response, imports);
|
|
return compatible ? initialize(result) : result;
|
|
};
|
|
WebAssembly.instantiate = async (source, imports) => {
|
|
const known = source instanceof WebAssembly.Module
|
|
? verified.has(source)
|
|
: await matches(source);
|
|
const result = await instantiate(source, imports);
|
|
return known ? initialize(result) : result;
|
|
};
|
|
})();
|