chainCode: DataView
}
-const b = async () => {
+const b = () => {
/**
* 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)
- }
+ if (typeof window === 'undefined' && typeof self === 'undefined') {
+ try {
+ import('node:worker_threads').then(worker_threads => {
+ const { isMainThread, parentPort } = worker_threads
+ if (!isMainThread && parentPort) {
+ var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
+ var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
+ }
+ })
+ } catch { }
}
const BIP44_COIN_NANO = 165
/**
* Listens for messages from a calling function.
*/
- if (addEventListener != null) {
+ if (typeof addEventListener === 'function') {
addEventListener('message', (message: any): void => {
const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
- process(data).then(results => {
+ calculate(data).then(results => {
const buf = new TextEncoder().encode(JSON.stringify(results)).buffer
+ //@ts-expect-error
postMessage(buf, [buf])
})
})
}
- async function process (data: any[]): Promise<any[]> {
+ async function calculate (data: any[]): Promise<any[]> {
return new Promise(async (resolve) => {
for (const d of data) {
d.privateKey = await nanoCKD(d.seed, d.index)
const coinKey = await CKDpriv(purposeKey, BIP44_COIN_NANO + HARDENED_OFFSET)
const accountKey = await CKDpriv(coinKey, index + HARDENED_OFFSET)
const privateKey = new Uint8Array(accountKey.privateKey.buffer)
- return privateKey.reduce((key, byte) => key.concat(byte.toString(16).padStart(2, '0')), '')
+ let hex = ''
+ for (let i = 0; i < privateKey.length; i++) {
+ hex += privateKey[i].toString(16).padStart(2, '0')
+ }
+ return hex
}
async function slip10 (curve: string, S: string): Promise<ExtendedKey> {
return { nanoCKD }
}
-export const Bip44Ckd = await b()
+export const Bip44Ckd = 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 = async () => {\r
+const n = () => {\r
/**\r
* Polyfill for window methods which do not exist when executing Node.js tests.\r
*/\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
+ if (typeof window === 'undefined' && typeof self === 'undefined') {\r
+ try {\r
+ import('node:worker_threads').then(worker_threads => {\r
+ const { isMainThread, parentPort } = 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
+ })\r
+ } catch { }\r
}\r
\r
/**\r
* Listens for messages from a calling function.\r
*/\r
- if (addEventListener != null) {\r
+ if (typeof addEventListener === 'function') {\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
+ //@ts-expect-error\r
postMessage(buf, [buf])\r
})\r
}\r
return { sign, open, detached, verify, convert }\r
}\r
\r
-export const NanoNaCl = await n()\r
+export const NanoNaCl = n()\r
\r
const start = n.toString().indexOf('{') + 1\r
const end = n.toString().lastIndexOf('return')\r
}
constructor (fn: string) {
- if (typeof Window === 'undefined') {
+ if (typeof window === 'undefined' && typeof self === 'undefined') {
this.#url = fn
} else {
this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' }))
/**
* 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)
- }
+ if (typeof window === 'undefined' && typeof self === 'undefined') {
+ try {
+ import('node:worker_threads').then(worker_threads => {
+ const { isMainThread, parentPort } = worker_threads
+ if (!isMainThread && parentPort) {
+ var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
+ var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
+ }
+ })
+ } catch (err) { }
}
const SEND_THRESHOLD = '0xfffffff8'
/**
* Listens for messages from a calling function.
*/
- if (addEventListener != null) {
+ if (typeof addEventListener === 'function') {
addEventListener('message', (message: any): void => {
const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
for (const d of data) {
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])
})
}