Skip to content

Commit

Permalink
Hard-code a Nimiq SDK version that includes the transaction-history-fix
Browse files Browse the repository at this point in the history
Until that is merged and released properly.
  • Loading branch information
sisou committed Dec 11, 2024
1 parent ad0ce95 commit c2bb85f
Show file tree
Hide file tree
Showing 20 changed files with 8,240 additions and 15 deletions.
4 changes: 2 additions & 2 deletions patches/@nimiq+ledger-api+3.1.1.patch
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ index b05f20b..b609e35 100644
+ // TODO either automatically adapt the version, based on @nimiq/core's package.json (here in the patch, or in the
+ // the Ledger API itself), or ideally make the dependency an external one in the Ledger API, and try to inject it
+ // in vue.config.js.
+ ? '/nimiq/v2.0.5/web/'
+ ? '/nimiq/v2.0.5-history-fix/web/'
// In other cases load @nimiq/core-web@next from jsdelivr. Load from cdn to avoid bundling a copy of core if it's
// not needed. This way, we also don't need to handle the wasm file in the rollup config.
: 'https://cdn.jsdelivr.net/npm/@nimiq/core@next/web/';
Expand Down Expand Up @@ -81,7 +81,7 @@ index 5b18143..6a45b5f 100644
+ // TODO either automatically adapt the version, based on @nimiq/core's package.json (here in the patch, or in the
+ // the Ledger API itself), or ideally make the dependency an external one in the Ledger API, and try to inject it
+ // in vue.config.js.
+ ? '/nimiq/v2.0.5/web/'
+ ? '/nimiq/v2.0.5-history-fix/web/'
// In other cases load @nimiq/core-web@next from jsdelivr. Load from cdn to avoid bundling a copy of core if it's
// not needed. This way, we also don't need to handle the wasm file in the rollup config.
: 'https://cdn.jsdelivr.net/npm/@nimiq/core@next/web/';
Expand Down
Empty file.
63 changes: 63 additions & 0 deletions public/nimiq/2.0.5-history-fix/launcher/browser/client-proxy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// launcher/client-proxy.ts
function clientFactory(workerFactory, comlinkWrapper) {
return {
async create(config) {
const worker = workerFactory();
await new Promise((resolve) => {
const readyListener = (event) => {
removeEventListener(worker, "message", readyListener);
if (getEventData(event) === "NIMIQ_ONLOAD") resolve();
};
addEventListener(worker, "message", readyListener);
});
console.debug("Client WASM worker loaded");
const client = comlinkWrapper(worker);
if (typeof window !== "undefined") {
window.addEventListener("offline", () => worker.postMessage("offline"));
window.addEventListener("online", () => worker.postMessage("online"));
}
if (typeof document !== "undefined") {
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
worker.postMessage("visible");
}
});
}
console.debug("Sending NIMIQ_INIT message to client worker");
worker.postMessage({
type: "NIMIQ_INIT",
config
});
await new Promise((resolve, reject) => {
addEventListener(worker, "message", (event) => {
const eventData = getEventData(event);
if (!("ok" in eventData)) return;
if (eventData.ok === true) resolve();
if (eventData.ok === false && "error" in eventData && typeof eventData.error === "string") {
const error = new Error(eventData.error);
if ("stack" in eventData && typeof eventData.stack === "string") {
error.stack = eventData.stack;
}
reject(error);
}
});
});
console.debug("Have client worker remote");
return client;
}
};
}
function addEventListener(worker, type, listener) {
const method = "addListener" in worker ? "addListener" : "addEventListener";
worker[method](type, listener);
}
function removeEventListener(worker, type, listener) {
const method = "removeListener" in worker ? "removeListener" : "removeEventListener";
worker[method](type, listener);
}
function getEventData(event) {
return typeof event === "object" && "data" in event ? event.data : event;
}
export {
clientFactory
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// launcher/cryptoutils-worker-proxy.ts
var remote;
function cryptoUtilsWorkerFactory(workerFactory, comlinkWrapper) {
const proxy = {};
["otpKdf"].forEach((method) => {
proxy[method] = async function() {
remote = remote || await startWorker(workerFactory, comlinkWrapper);
return remote[method](...arguments);
};
});
return proxy;
}
async function startWorker(workerFactory, comlinkWrapper) {
const worker = workerFactory();
await new Promise((resolve) => {
const readyListener = (event) => {
removeEventListener(worker, "message", readyListener);
if (getEventData(event) === "NIMIQ_ONLOAD") resolve();
};
addEventListener(worker, "message", readyListener);
});
console.debug("Have crypto worker remote");
return comlinkWrapper(worker);
}
function addEventListener(worker, type, listener) {
const method = "addListener" in worker ? "addListener" : "addEventListener";
worker[method](type, listener);
}
function removeEventListener(worker, type, listener) {
const method = "removeListener" in worker ? "removeListener" : "removeEventListener";
worker[method](type, listener);
}
function getEventData(event) {
return typeof event === "object" && "data" in event ? event.data : event;
}
export {
cryptoUtilsWorkerFactory
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// launcher/transfer-handlers.ts
function setupMainThreadTransferHandlers(Comlink, classes) {
Comlink.transferHandlers.set("function", {
canHandle: (obj) => typeof obj === "function",
serialize(obj) {
return Comlink.transferHandlers.get("proxy").serialize(obj);
}
// deserialize(plain) {}, // Cannot receive functions from worker
});
function canBeSerialized(obj) {
return obj instanceof classes.Address || obj instanceof classes.Transaction;
}
Comlink.transferHandlers.set("plain", {
canHandle: (obj) => canBeSerialized(obj) || Array.isArray(obj) && obj.some((item) => canBeSerialized(item)),
serialize(obj) {
if (Array.isArray(obj)) {
return [obj.map((item) => canBeSerialized(item) ? item.serialize() : item), []];
} else {
return [obj.serialize(), []];
}
}
// deserialize(plain) {}, // Cannot receive class instances from worker
});
}
export {
setupMainThreadTransferHandlers
};
Loading

0 comments on commit c2bb85f

Please sign in to comment.