diff --git a/modules/ecash-lib/src/ecc.test.ts b/modules/ecash-lib/src/ecc.test.ts --- a/modules/ecash-lib/src/ecc.test.ts +++ b/modules/ecash-lib/src/ecc.test.ts @@ -3,14 +3,14 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. import { expect } from 'chai'; -import fs from 'node:fs/promises'; import { fromHex, toHex } from './io/hex.js'; -import { EccDummy, EccWasm, initWasm } from './ecc.js'; +import { EccDummy, EccWasm } from './ecc.js'; +import { initWasm } from './init.js'; describe('Ecc', async () => { // Can't use `fetch` for local file so we have to read it using `fs` - await initWasm(fs.readFile('./src/ffi/ecash_lib_wasm_bg.wasm')); + await initWasm(); it('EccWasm', () => { const ecc = new EccWasm(); diff --git a/modules/ecash-lib/src/ecc.ts b/modules/ecash-lib/src/ecc.ts --- a/modules/ecash-lib/src/ecc.ts +++ b/modules/ecash-lib/src/ecc.ts @@ -5,7 +5,6 @@ // These files are generated in "ecash-lib-wasm" via build-wasm.sh or // dockerbuild.sh. import * as ffi from './ffi/ecash_lib_wasm.js'; -import __wbg_init from './ffi/ecash_lib_wasm.js'; /** Interface to abstract over Elliptic Curve Cryptography */ export interface Ecc { @@ -21,8 +20,6 @@ /** Ecc implementation using WebAssembly */ export const EccWasm = ffi.Ecc; -/** Download and initialize the WASM module, or use the provided buffer */ -export const initWasm = __wbg_init; /** Dummy Ecc impl that always returns 0, useful for measuring tx size */ export class EccDummy implements Ecc { diff --git a/modules/ecash-lib/src/hash.test.ts b/modules/ecash-lib/src/hash.test.ts --- a/modules/ecash-lib/src/hash.test.ts +++ b/modules/ecash-lib/src/hash.test.ts @@ -3,10 +3,9 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. import { expect } from 'chai'; -import fs from 'node:fs/promises'; import { fromHex, toHex, toHexRev } from './io/hex.js'; -import { initWasm } from './ecc.js'; +import { initWasm } from './init.js'; import { sha256, sha256d, shaRmd160 } from './hash.js'; const GENESIS_HEADER_HEX = @@ -17,7 +16,7 @@ describe('Ecc', async () => { // Can't use `fetch` for local file so we have to read it using `fs` - await initWasm(fs.readFile('./src/ffi/ecash_lib_wasm_bg.wasm')); + await initWasm(); it('sha256', () => { expect(toHex(sha256(new Uint8Array()))).to.equal( diff --git a/modules/ecash-lib/src/index.ts b/modules/ecash-lib/src/index.ts --- a/modules/ecash-lib/src/index.ts +++ b/modules/ecash-lib/src/index.ts @@ -4,6 +4,7 @@ export * from './ecc.js'; export * from './hash.js'; +export * from './init.js'; export * from './op.js'; export * from './opcode.js'; export * from './script.js'; diff --git a/modules/ecash-lib/src/init.ts b/modules/ecash-lib/src/init.ts new file mode 100644 --- /dev/null +++ b/modules/ecash-lib/src/init.ts @@ -0,0 +1,18 @@ +// Copyright (c) 2024 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +import __wbg_init from './ffi/ecash_lib_wasm.js'; + +/** Load and initialize the WASM module */ +export async function initWasm() { + if (typeof window === 'undefined') { + // On Node, read the file off the disk + const fs = await import('node:fs/promises'); + const wasmUrl = new URL('ffi/ecash_lib_wasm_bg.wasm', import.meta.url); + return await __wbg_init(fs.readFile(wasmUrl)); + } else { + // Browser, use default, will use fetch internally + return await __wbg_init(); + } +} diff --git a/modules/ecash-lib/src/unsignedTx.test.ts b/modules/ecash-lib/src/unsignedTx.test.ts --- a/modules/ecash-lib/src/unsignedTx.test.ts +++ b/modules/ecash-lib/src/unsignedTx.test.ts @@ -3,7 +3,6 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. import { expect } from 'chai'; -import fs from 'node:fs/promises'; import { fromHex, toHex } from './io/hex.js'; import { Script } from './script.js'; @@ -19,7 +18,7 @@ SINGLE_ANYONECANPAY_BIP143, SINGLE_BIP143, } from './sigHashType.js'; -import { initWasm } from './ecc.js'; +import { initWasm } from './init.js'; const TX = new Tx({ version: 0xfacefeed, @@ -88,7 +87,7 @@ describe('UnsignedTx', async () => { // Can't use `fetch` for local file so we have to read it using `fs` - await initWasm(fs.readFile('./src/ffi/ecash_lib_wasm_bg.wasm')); + await initWasm(); it('UnsignedTx.dummyFromTx', () => { const dummy = UnsignedTx.dummyFromTx(new Tx()); diff --git a/modules/ecash-lib/tests/alp.test.ts b/modules/ecash-lib/tests/alp.test.ts --- a/modules/ecash-lib/tests/alp.test.ts +++ b/modules/ecash-lib/tests/alp.test.ts @@ -5,7 +5,6 @@ import { expect, use } from 'chai'; import { ChildProcess, spawn } from 'node:child_process'; import { EventEmitter, once } from 'node:events'; -import fs from 'node:fs/promises'; import { ChronikClientNode } from 'chronik-client'; @@ -144,7 +143,7 @@ }); // Can't use `fetch` for local file so we have to read it using `fs` - await initWasm(fs.readFile('./src/ffi/ecash_lib_wasm_bg.wasm')); + await initWasm(); ecc = new EccWasm(); // We got the coins, can fan out now diff --git a/modules/ecash-lib/tests/txBuilder.test.ts b/modules/ecash-lib/tests/txBuilder.test.ts --- a/modules/ecash-lib/tests/txBuilder.test.ts +++ b/modules/ecash-lib/tests/txBuilder.test.ts @@ -5,7 +5,6 @@ import { expect, use } from 'chai'; import { ChildProcess, spawn } from 'node:child_process'; import { EventEmitter, once } from 'node:events'; -import fs from 'node:fs/promises'; import { ChronikClientNode } from 'chronik-client'; @@ -155,7 +154,7 @@ }); // Can't use `fetch` for local file so we have to read it using `fs` - await initWasm(fs.readFile('./src/ffi/ecash_lib_wasm_bg.wasm')); + await initWasm(); ecc = new EccWasm(); // We got the coins, can fan out now