chainCode: DataView
}
-const b = () => {
+const b = async () => {
+ /**
+ * Polyfill for window methods which do not exist when executing Node.js tests.
+ */
+ if (typeof Window === 'undefined') {
+ const { isMainThread, parentPort } = await import('node:worker_threads')
+ if (!isMainThread && parentPort) {
+ var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
+ var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
+ }
+ }
+
const BIP44_COIN_NANO = 165
const BIP44_PURPOSE = 44
const HARDENED_OFFSET = 0x80000000
/**
* Listens for messages from a calling function.
*/
- addEventListener('message', (message) => {
- const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
- process(data).then(results => {
- const buf = new TextEncoder().encode(JSON.stringify(results)).buffer
- //@ts-expect-error
- postMessage(buf, [buf])
+ if (addEventListener != null) {
+ addEventListener('message', (message: any): void => {
+ const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
+ process(data).then(results => {
+ const buf = new TextEncoder().encode(JSON.stringify(results)).buffer
+ postMessage(buf, [buf])
+ })
})
- })
+ }
async function process (data: any[]): Promise<any[]> {
return new Promise(async (resolve) => {
return { nanoCKD }
}
-export const Bip44Ckd = b()
+export const Bip44Ckd = await b()
const start = b.toString().indexOf('{') + 1
const end = b.toString().lastIndexOf('return')
// See for details: https://docs.nano.org/integration-guides/the-basics/\r
// Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js\r
\r
-const n = () => {\r
+const n = async () => {\r
/**\r
- * Listens for messages from a calling function.\r
+ * Polyfill for window methods which do not exist when executing Node.js tests.\r
*/\r
- addEventListener('message', message => {\r
- const data = JSON.parse(new TextDecoder().decode(message.data ?? message))\r
- for (const d of data) {\r
- d.publicKey = convert(d.privateKey)\r
+ if (typeof Window === 'undefined') {\r
+ const { isMainThread, parentPort } = await import('node:worker_threads')\r
+ if (!isMainThread && parentPort) {\r
+ var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)\r
+ var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)\r
}\r
- const buf = new TextEncoder().encode(JSON.stringify(data)).buffer\r
- //@ts-expect-error\r
- postMessage(buf, [buf])\r
- })\r
+ }\r
+\r
+ /**\r
+ * Listens for messages from a calling function.\r
+ */\r
+ if (addEventListener != null) {\r
+ addEventListener('message', (message: any): void => {\r
+ const data = JSON.parse(new TextDecoder().decode(message.data ?? message))\r
+ for (const d of data) {\r
+ d.publicKey = convert(d.privateKey)\r
+ }\r
+ const buf = new TextEncoder().encode(JSON.stringify(data)).buffer\r
+ postMessage(buf, [buf])\r
+ })\r
+ }\r
\r
const gf = function (init?: number[]): Float64Array {\r
const r = new Float64Array(16)\r
return { sign, open, detached, verify, convert }\r
}\r
\r
-export const NanoNaCl = n()\r
+export const NanoNaCl = await n()\r
\r
const start = n.toString().indexOf('{') + 1\r
const end = n.toString().lastIndexOf('return')\r
}
constructor (fn: string) {
- this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' }))
+ if (typeof Window === 'undefined') {
+ this.#url = fn
+ } else {
+ this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' }))
+ }
for (let i = this.#cores; i > 0; i--) {
const thread = {
isBusy: false,
}
dismiss (): void {
- URL.revokeObjectURL(this.#url)
+ if (typeof Window === 'undefined') {
+ this.#threads.forEach(thread => thread.worker.terminate())
+ } else {
+ URL.revokeObjectURL(this.#url)
+ }
}
}
// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
// SPDX-License-Identifier: GPL-3.0-or-later
-const p = () => {
+const p = async () => {
+ /**
+ * Polyfill for window methods which do not exist when executing Node.js tests.
+ */
+ if (typeof Window === 'undefined') {
+ const { isMainThread, parentPort } = await import('node:worker_threads')
+ if (!isMainThread && parentPort) {
+ var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
+ var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
+ }
+ }
+
const SEND_THRESHOLD = '0xfffffff8'
/**
* Listens for messages from a calling function.
*/
- addEventListener('message', message => {
- const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
- for (const d of data) {
- if (d === 'stop') {
- close()
- postMessage(new ArrayBuffer(0))
- } else {
- find(d.hash, d.threshold ?? SEND_THRESHOLD).then(nonce => {
- d.work = nonce
- const buf = new TextEncoder().encode(JSON.stringify(data)).buffer
- //@ts-expect-error
- postMessage(buf, [buf])
- })
+ if (addEventListener != null) {
+ addEventListener('message', (message: any): void => {
+ const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
+ for (const d of data) {
+ if (d === 'stop') {
+ close()
+ postMessage(new ArrayBuffer(0))
+ } else {
+ find(d.hash, d.threshold ?? SEND_THRESHOLD).then(nonce => {
+ d.work = nonce
+ const buf = new TextEncoder().encode(JSON.stringify(data)).buffer
+ postMessage(buf, [buf])
+ })
+ }
}
- }
- })
+ })
+ }
async function find (hash: string, threshold: string = SEND_THRESHOLD): Promise<string> {
return new Promise<string>(resolve => {
return { find }
}
-export const Pow = p()
+export const Pow = await p()
const start = p.toString().indexOf('{') + 1
const end = p.toString().lastIndexOf('return')