Page MenuHomePhabricator

[ecash-lib] "module": "CommonJS" + separate "main" and "browser"
ClosedPublic

Authored by tobias_ruck on May 3 2024, 13:45.

Details

Summary

Currently, integrating ecash-lib into CashTab is very difficult, due to the different environments between jest and webpack:

  • Jest can't handle ES modules, and either fails to import ecash-lib altogether (as it is a module), or, if we change it to be a CommonJS compatible, the generated JS from wasm-bindgen breaks it as import.meta.url can't be used, even if it's never executed.
  • Webpack can't find node:fs/promises, even though it's lazily loaded and never actually imported.

wasm-bindgen can build ffi modules that work for either NodeJS or web, but it can't build one for both.
The simplest solution is to build both use the "main" and "browser" fields in package.json, and to have two different index files for both, one indexBrowser.ts for the browser, and one indexNodeJs.ts for NodeJS.

Also, jest is having issues with ECMAScript Modules (ESM), so we switch to "module": "CommonJS" in tsconfig.json, which will improve overall compatibility of this package.

We now have two init files, which dynamically switch the Ecc (renamed from EccWasm) and the hash functions out with the one for the correct target.

This makes it such that the library is still easy to use; the only function a user has to call is initWasm to load the WebAssembly, and NodeJS / webpack will pick the right module to load.

This diff has been shown to work on NodeJS, jest and webpack, and we could successfully integrate it into CashTab, see D16109.

Test Plan

npm run build

Event Timeline

Tail of the build log:

                                                              ^


ReferenceError: __dirname is not defined
    at initWasm (/work/modules/ecash-lib/src/init.ts:12:63)
    at Suite.<anonymous> (/work/modules/ecash-lib/src/ecc.test.ts:13:5)

Node.js v20.12.2
---------------------|---------|----------|---------|---------|-------------------
File                 | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------------|---------|----------|---------|---------|-------------------
All files            |       0 |        0 |       0 |       0 |                   
 ecash-lib           |       0 |        0 |       0 |       0 |                   
  eslint.config.js   |       0 |        0 |       0 |       0 |                   
 ecash-lib/dist/ffi  |       0 |        0 |       0 |       0 |                   
  ecash_lib_wasm.js  |       0 |        0 |       0 |       0 | 3-336             
 ecash-lib/src       |       0 |        0 |       0 |       0 |                   
  ecc.ts             |       0 |      100 |       0 |       0 | 22-35             
  hash.ts            |       0 |      100 |     100 |       0 | 7-9               
  index.ts           |       0 |        0 |       0 |       0 |                   
  init.ts            |       0 |        0 |       0 |       0 | 9-16              
  op.ts              |       0 |        0 |       0 |       0 | 33-124            
  opcode.ts          |       0 |      100 |     100 |       0 | 10-154            
  script.ts          |       0 |        0 |       0 |       0 | 26-158            
  sigHashType.ts     |       0 |        0 |       0 |       0 | 19-166            
  tx.ts              |       0 |        0 |       0 |       0 | 20-166            
  txBuilder.ts       |       0 |        0 |       0 |       0 | 74-245            
  unsignedTx.ts      |       0 |        0 |       0 |       0 | 38-229            
 ecash-lib/src/ffi   |       0 |        0 |       0 |       0 |                   
  ecash_lib_wasm.js  |       0 |        0 |       0 |       0 | 3-336             
 ecash-lib/src/io    |       0 |        0 |       0 |       0 |                   
  bytes.ts           |       0 |        0 |       0 |       0 | 13-64             
  hex.ts             |       0 |        0 |       0 |       0 | 5-68              
  int.ts             |       0 |        0 |       0 |       0 |                   
  str.ts             |       0 |      100 |       0 |       0 | 5-9               
  varsize.ts         |       0 |        0 |       0 |       0 | 14-47             
  writer.ts          |       0 |        0 |       0 |       0 |                   
  writerbytes.ts     |       0 |        0 |       0 |       0 | 21-79             
  writerlength.ts    |       0 |      100 |       0 |       0 | 16-41             
 ecash-lib/src/test  |       0 |        0 |       0 |       0 |                   
  testRunner.ts      |       0 |        0 |       0 |       0 | 17-191            
 ecash-lib/src/token |       0 |        0 |       0 |       0 |                   
  alp.ts             |       0 |        0 |       0 |       0 | 13-145            
  common.ts          |       0 |      100 |     100 |       0 | 9-12              
  empp.ts            |       0 |        0 |       0 |       0 | 11-28             
  slp.ts             |       0 |        0 |       0 |       0 | 13-211            
---------------------|---------|----------|---------|---------|-------------------

##teamcity[blockOpened name='Code Coverage Summary']
##teamcity[buildStatisticValue key='CodeCoverageAbsBCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsBTotal' value='1095']
##teamcity[buildStatisticValue key='CodeCoverageAbsRCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsRTotal' value='387']
##teamcity[buildStatisticValue key='CodeCoverageAbsMCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsMTotal' value='181']
##teamcity[buildStatisticValue key='CodeCoverageAbsLCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsLTotal' value='1071']
##teamcity[blockClosed name='Code Coverage Summary']
mv: cannot stat 'test_results/ecash-lib-junit.xml': No such file or directory
Build ecash-lib-tests failed with exit code 1

Failed tests logs:

====== ALP: "before all" hook for "TxBuilder P2PKH ALP".ALP "before all" hook for "TxBuilder P2PKH ALP" ======
ReferenceError: __dirname is not defined
    at initWasm (src/init.ts:12:63)
    at Function.setup (src/test/testRunner.ts:114:9)
    at Context.<anonymous> (tests/alp.test.ts:44:18)
====== ALP: "after all" hook for "TxBuilder P2PKH ALP".ALP "after all" hook for "TxBuilder P2PKH ALP" ======
TypeError: Cannot read properties of undefined (reading 'stop')
    at Context.<anonymous> (tests/alp.test.ts:52:16)
    at process.processImmediate (node:internal/timers:478:21)
====== SLP Integration Test: "before all" hook for "TxBuilder P2PKH SLP FUNGIBLE".SLP Integration Test "before all" hook for "TxBuilder P2PKH SLP FUNGIBLE" ======
ReferenceError: __dirname is not defined
    at initWasm (src/init.ts:12:63)
    at Function.setup (src/test/testRunner.ts:114:9)
    at Context.<anonymous> (tests/slp.test.ts:68:18)
====== SLP Integration Test: "after all" hook for "TxBuilder P2PKH SLP NFT1".SLP Integration Test "after all" hook for "TxBuilder P2PKH SLP NFT1" ======
TypeError: Cannot read properties of undefined (reading 'stop')
    at Context.<anonymous> (tests/slp.test.ts:76:16)
    at process.processImmediate (node:internal/timers:478:21)
====== TxBuilder: "before all" hook for "TxBuilder P2PKH Wallet".TxBuilder "before all" hook for "TxBuilder P2PKH Wallet" ======
ReferenceError: __dirname is not defined
    at initWasm (src/init.ts:12:63)
    at Function.setup (src/test/testRunner.ts:114:9)
    at Context.<anonymous> (tests/txBuilder.test.ts:54:18)
====== TxBuilder: "after all" hook for "TxBuilder leftover failure".TxBuilder "after all" hook for "TxBuilder leftover failure" ======
TypeError: Cannot read properties of undefined (reading 'stop')
    at Context.<anonymous> (tests/txBuilder.test.ts:62:16)
    at process.processImmediate (node:internal/timers:478:21)

Each failure log is accessible here:
ALP: "before all" hook for "TxBuilder P2PKH ALP".ALP "before all" hook for "TxBuilder P2PKH ALP"
ALP: "after all" hook for "TxBuilder P2PKH ALP".ALP "after all" hook for "TxBuilder P2PKH ALP"
SLP Integration Test: "before all" hook for "TxBuilder P2PKH SLP FUNGIBLE".SLP Integration Test "before all" hook for "TxBuilder P2PKH SLP FUNGIBLE"
SLP Integration Test: "after all" hook for "TxBuilder P2PKH SLP NFT1".SLP Integration Test "after all" hook for "TxBuilder P2PKH SLP NFT1"
TxBuilder: "before all" hook for "TxBuilder P2PKH Wallet".TxBuilder "before all" hook for "TxBuilder P2PKH Wallet"
TxBuilder: "after all" hook for "TxBuilder leftover failure".TxBuilder "after all" hook for "TxBuilder leftover failure"

Tail of the build log:

added 362 packages, and audited 364 packages in 2s

60 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> ecash-lib@0.1.0 build
> tsc && tsc -p ./tsconfig.build.json && cp -r ./src/ffi ./dist

/work/modules/ecash-agora /work/modules/ecash-lib /work/modules/chronik-client /work/modules/ecash-lib-wasm /work/abc-ci-builds/ecash-agora-integration-tests

added 366 packages, and audited 369 packages in 2s

60 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> ecash-agora@0.1.0 integration-tests
> mocha --import=tsx ./tests/*.test.ts --timeout 60000 --reporter mocha-junit-reporter --reporter-options mochaFile=test_results/ecash-agora-integration-tests-junit.xml --reporter-options testsuitesTitle=Ecash Agora Integration Tests --reporter-options rootSuiteTitle=Ecash Agora


 Exception during run: /work/modules/ecash-agora/src/oneshot.ts:6
    ALL_ANYONECANPAY_BIP143,
    ^

SyntaxError: The requested module 'ecash-lib' does not provide an export named 'ALL_ANYONECANPAY_BIP143'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async formattedImport (/work/modules/ecash-agora/node_modules/mocha/lib/nodejs/esm-utils.js:9:14)
    at async exports.requireOrImport (/work/modules/ecash-agora/node_modules/mocha/lib/nodejs/esm-utils.js:42:28)
    at async exports.loadFilesAsync (/work/modules/ecash-agora/node_modules/mocha/lib/nodejs/esm-utils.js:100:20)
    at async singleRun (/work/modules/ecash-agora/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async exports.handler (/work/modules/ecash-agora/node_modules/mocha/lib/cli/run.js:370:5)
-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |       0 |      100 |       0 |       0 |                   
 ecash-agora       |       0 |        0 |       0 |       0 |                   
  eslint.config.js |       0 |        0 |       0 |       0 |                   
 ecash-agora/src   |       0 |      100 |       0 |       0 |                   
  index.ts         |       0 |        0 |       0 |       0 |                   
  oneshot.ts       |       0 |      100 |       0 |       0 | 68-181            
-------------------|---------|----------|---------|---------|-------------------

##teamcity[blockOpened name='Code Coverage Summary']
##teamcity[buildStatisticValue key='CodeCoverageAbsBCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsBTotal' value='31']
##teamcity[buildStatisticValue key='CodeCoverageAbsRCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsRTotal' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsMCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsMTotal' value='8']
##teamcity[buildStatisticValue key='CodeCoverageAbsLCovered' value='0']
##teamcity[buildStatisticValue key='CodeCoverageAbsLTotal' value='31']
##teamcity[blockClosed name='Code Coverage Summary']
mv: cannot stat 'test_results/ecash-agora-integration-tests-junit.xml': No such file or directory
Build ecash-agora-integration-tests failed with exit code 1

use "main" and "browser" in package.json to have two separate versions for NodeJS and Browser

tobias_ruck retitled this revision from [ecash-lib] Support `module: CommonJS` to [ecash-lib] Use separate "main" and "browser" files for the WebAssembly.
tobias_ruck edited the summary of this revision. (Show Details)

Tail of the build log:

Test does not depend on mock-chronik-client, skipping mock-chronik-client dependencies...
Test depends on ecash-lib-wasm. Building WebAssembly...
/work/modules/ecash-lib-wasm /work/abc-ci-builds/ecash-lib-tests
warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
note: for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
    Updating crates.io index
    Updating git repository `https://github.com/raipay/secp256k1-abc`
 Downloading crates ...
  Downloaded digest v0.10.7
  Downloaded thiserror v1.0.58
  Downloaded typenum v1.17.0
  Downloaded wasm-bindgen-macro-support v0.2.92
  Downloaded wasm-bindgen-macro v0.2.92
  Downloaded wasm-bindgen-backend v0.2.92
  Downloaded wasm-bindgen v0.2.92
  Downloaded thiserror-impl v1.0.58
  Downloaded syn v2.0.58
  Downloaded ripemd v0.1.3
  Downloaded proc-macro2 v1.0.79
  Downloaded bumpalo v3.16.0
  Downloaded sha2 v0.10.8
  Downloaded generic-array v0.14.7
  Downloaded crypto-common v0.1.6
  Downloaded cpufeatures v0.2.12
  Downloaded cc v1.0.92
  Downloaded block-buffer v0.10.4
   Compiling proc-macro2 v1.0.79
   Compiling unicode-ident v1.0.12
   Compiling typenum v1.17.0
   Compiling version_check v0.9.4
   Compiling wasm-bindgen-shared v0.2.92
   Compiling cc v1.0.92
   Compiling bumpalo v3.16.0
   Compiling log v0.4.21
   Compiling once_cell v1.19.0
   Compiling thiserror v1.0.58
   Compiling wasm-bindgen v0.2.92
   Compiling cfg-if v1.0.0
   Compiling abc-rust-lint v0.1.0 (/work/chronik/abc-rust-lint)
   Compiling generic-array v0.14.7
   Compiling quote v1.0.36
   Compiling syn v2.0.58
   Compiling secp256k1-sys-abc v0.4.1 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling crypto-common v0.1.6
   Compiling block-buffer v0.10.4
   Compiling digest v0.10.7
   Compiling sha2 v0.10.8
   Compiling ripemd v0.1.3
   Compiling wasm-bindgen-backend v0.2.92
   Compiling secp256k1-abc v0.20.3 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling thiserror-impl v1.0.58
   Compiling wasm-bindgen-macro-support v0.2.92
   Compiling wasm-bindgen-macro v0.2.92
   Compiling ecash-lib-wasm v0.1.0 (/work/modules/ecash-lib-wasm)
    Finished release-wasm [optimized] target(s) in 6.00s
sed: can't read : No such file or directory
sed: can't read : No such file or directory
Build ecash-lib-tests failed with exit code 2

Tail of the build log:

[553/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/transactionrecord.cpp.o
[554/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/transactiontablemodel.cpp.o
[555/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/transactionview.cpp.o
[556/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletmodeltransaction.cpp.o
[557/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletframe.cpp.o
[558/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/paymentrequestplus.cpp.o
[559/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletcontroller.cpp.o
[560/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletmodel.cpp.o
[561/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletview.cpp.o
[562/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/qrc_bitcoin.cpp.o
[563/568] Linking CXX executable src/bench/bitcoin-bench
[564/568] Linking CXX static library src/qt/libbitcoin-qt-base.a
[565/568] Automatic MOC for target bitcoin-qt
[566/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt.dir/bitcoin-qt_autogen/mocs_compilation.cpp.o
[567/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt.dir/main.cpp.o
[568/568] Linking CXX executable src/qt/bitcoin-qt
Test depends on ecash-lib-wasm. Building WebAssembly...
/work/modules/ecash-lib-wasm /work/abc-ci-builds/ecash-lib-integration-tests
warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
note: for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
 Downloading crates ...
  Downloaded wasm-bindgen-macro-support v0.2.92
  Downloaded wasm-bindgen-macro v0.2.92
  Downloaded wasm-bindgen-backend v0.2.92
  Downloaded bumpalo v3.16.0
  Downloaded wasm-bindgen v0.2.92
   Compiling proc-macro2 v1.0.79
   Compiling unicode-ident v1.0.12
   Compiling version_check v0.9.4
   Compiling typenum v1.17.0
   Compiling wasm-bindgen-shared v0.2.92
   Compiling cc v1.0.92
   Compiling bumpalo v3.16.0
   Compiling log v0.4.21
   Compiling once_cell v1.19.0
   Compiling cfg-if v1.0.0
   Compiling wasm-bindgen v0.2.92
   Compiling thiserror v1.0.58
   Compiling abc-rust-lint v0.1.0 (/work/chronik/abc-rust-lint)
   Compiling generic-array v0.14.7
   Compiling quote v1.0.36
   Compiling syn v2.0.58
   Compiling secp256k1-sys-abc v0.4.1 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling block-buffer v0.10.4
   Compiling crypto-common v0.1.6
   Compiling digest v0.10.7
   Compiling sha2 v0.10.8
   Compiling ripemd v0.1.3
   Compiling wasm-bindgen-backend v0.2.92
   Compiling secp256k1-abc v0.20.3 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling thiserror-impl v1.0.58
   Compiling wasm-bindgen-macro-support v0.2.92
   Compiling wasm-bindgen-macro v0.2.92
   Compiling ecash-lib-wasm v0.1.0 (/work/modules/ecash-lib-wasm)
    Finished release-wasm [optimized] target(s) in 4.39s
sed: can't read : No such file or directory
sed: can't read : No such file or directory
Build ecash-lib-integration-tests failed with exit code 2

Tail of the build log:

[553/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletmodeltransaction.cpp.o
[554/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/coincontroldialog.cpp.o
[555/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/transactiontablemodel.cpp.o
[556/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/signverifymessagedialog.cpp.o
[557/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/transactiondesc.cpp.o
[558/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/paymentserver.cpp.o
[559/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/paymentrequestplus.cpp.o
[560/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/sendcoinsdialog.cpp.o
[561/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletcontroller.cpp.o
[562/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletview.cpp.o
[563/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt-base.dir/walletmodel.cpp.o
[564/568] Linking CXX static library src/qt/libbitcoin-qt-base.a
[565/568] Automatic MOC for target bitcoin-qt
[566/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt.dir/bitcoin-qt_autogen/mocs_compilation.cpp.o
[567/568] Building CXX object src/qt/CMakeFiles/bitcoin-qt.dir/main.cpp.o
[568/568] Linking CXX executable src/qt/bitcoin-qt
Test depends on ecash-lib-wasm. Building WebAssembly...
/work/modules/ecash-lib-wasm /work/abc-ci-builds/ecash-agora-integration-tests
warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
note: for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
 Downloading crates ...
  Downloaded wasm-bindgen-macro v0.2.92
  Downloaded wasm-bindgen-macro-support v0.2.92
  Downloaded wasm-bindgen-backend v0.2.92
  Downloaded bumpalo v3.16.0
  Downloaded wasm-bindgen v0.2.92
   Compiling proc-macro2 v1.0.79
   Compiling unicode-ident v1.0.12
   Compiling version_check v0.9.4
   Compiling typenum v1.17.0
   Compiling wasm-bindgen-shared v0.2.92
   Compiling bumpalo v3.16.0
   Compiling log v0.4.21
   Compiling cc v1.0.92
   Compiling once_cell v1.19.0
   Compiling thiserror v1.0.58
   Compiling wasm-bindgen v0.2.92
   Compiling cfg-if v1.0.0
   Compiling abc-rust-lint v0.1.0 (/work/chronik/abc-rust-lint)
   Compiling generic-array v0.14.7
   Compiling quote v1.0.36
   Compiling syn v2.0.58
   Compiling secp256k1-sys-abc v0.4.1 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling block-buffer v0.10.4
   Compiling crypto-common v0.1.6
   Compiling digest v0.10.7
   Compiling sha2 v0.10.8
   Compiling ripemd v0.1.3
   Compiling wasm-bindgen-backend v0.2.92
   Compiling secp256k1-abc v0.20.3 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling thiserror-impl v1.0.58
   Compiling wasm-bindgen-macro-support v0.2.92
   Compiling wasm-bindgen-macro v0.2.92
   Compiling ecash-lib-wasm v0.1.0 (/work/modules/ecash-lib-wasm)
    Finished release-wasm [optimized] target(s) in 4.60s
sed: can't read : No such file or directory
sed: can't read : No such file or directory
Build ecash-agora-integration-tests failed with exit code 2
bytesofman requested changes to this revision.May 4 2024, 13:35
bytesofman added a subscriber: bytesofman.

Unfortunate about the complicated overhead here but seems to be pretty standard to getting wasm to work in JS. Appropriate that

  • it's done here, in the library, and isn't really something app devs need to worry about
  • changes are not really expected to the implementation, even if the wasm changes the implementation should still be the same
modules/ecash-agora/tests/oneshot.test.ts
23 ↗(On Diff #47614)

it's nice to clean up these unrelated import order / line break changes (remove from the diff if unrelated)

55 ↗(On Diff #47614)

seems like this move is good, i.e initWasm() "should" come before setting ecc

modules/ecash-lib-wasm/build-wasm.sh
22 ↗(On Diff #47614)

do we know that simply replacing --target web with --target nodejs will not accomplish the same thing?

What is different between these two builds?

Probably makes sense to do both as it's certainly optimized in some way for one or the other, and npm will allow us to have separate file refs for each. Would like to understand more about the difference here though.

modules/ecash-lib/src/ecc.test.ts
7 ↗(On Diff #47614)

what's causing this import order to change in this diff? name of the ref file for initWasm?

12 ↗(On Diff #47614)

should document in the ecash-lib README that we need to call await initWasm() and ecc = new Ecc()

Add sections for wasm notes and ECC usage -- the lib cannot work without these being used correctly and developers unfamiliar with wasm or ECC cannot be expected to just know this.

This revision now requires changes to proceed.May 4 2024, 13:35

Depends on D16105

D16105 should be abandoned, rebase this one on master

  • Remove es2016 dependency, build targeting es2020 directly
  • Add comments explaining why two separate wasm-bindgen builds
  • Fix some imports
  • Improve README
tobias_ruck retitled this revision from [ecash-lib] Use separate "main" and "browser" files for the WebAssembly to [ecash-lib] "module": "CommonJS" + separate "main" and "browser".May 4 2024, 14:45
tobias_ruck edited the summary of this revision. (Show Details)

Failed tests logs:

====== SLP: "before all" hook for "SLP NFT1 Agora Oneshot".SLP "before all" hook for "SLP NFT1 Agora Oneshot" ======
TypeError: ecc_js_1.Ecc is not a constructor
    at Function.setup (/work/modules/ecash-lib/src/test/testRunner.ts:113:21)
    at Context.<anonymous> (tests/oneshot.test.ts:54:18)
====== SLP: "after all" hook for "SLP NFT1 Agora Oneshot".SLP "after all" hook for "SLP NFT1 Agora Oneshot" ======
TypeError: Cannot read properties of undefined (reading 'stop')
    at Context.<anonymous> (tests/oneshot.test.ts:62:16)
    at process.processImmediate (node:internal/timers:478:21)

Each failure log is accessible here:
SLP: "before all" hook for "SLP NFT1 Agora Oneshot".SLP "before all" hook for "SLP NFT1 Agora Oneshot"
SLP: "after all" hook for "SLP NFT1 Agora Oneshot".SLP "after all" hook for "SLP NFT1 Agora Oneshot"

modules/ecash-agora/tests/oneshot.test.ts
24 ↗(On Diff #47617)

this comes from the automatic import sorting in VSCode, do you want to keep it or remove it?

modules/ecash-lib/tests/alp.test.ts
9 ↗(On Diff #47617)

these import changes come from index.ts being removed, and I didn't want to use indexNodeJs.js everywhere, so I just import the files directly which somehow also made it more readable (or at least shorter)

modules/ecash-lib/README.md
30 ↗(On Diff #47616)
92 ↗(On Diff #47616)

not really related to this diff

1 - why do the params for satoshis/kb and dust satoshis not default to these values?
2 - why is satoshis/kb used instead of satoshis/byte? seems like satoshis/byte is more standard?

modules/ecash-lib/README.md
92 ↗(On Diff #47616)
  1. I'm not sure what the right approach here is, but since the plan for eCash is to have the tx fee be determined by Avalanche at some point, it seems better to require the user to specify it for now. And IMO the dev should think about the fee, should it be changable by the user (as in Electrum), should it be hard-coded, should it come from a server? For dust, a default might make sense but IMO it's ok to have the user specify it, to have a consistent API. Some networks use different dust amounts.
  2. sats/kB allows the user to specify e.g. 1100 sats/kB (as an integer), and get exact results. 1.1 is not exactly representable in floating point numbers, so that could introduce weird bugs. Also, the node uses this unit, see CFeeRate in /src/feerate.h

fix README, english are hard

bytesofman added inline comments.
modules/ecash-agora/tests/oneshot.test.ts
24 ↗(On Diff #47617)

just keep it, otherwise have to deal with it later

modules/ecash-lib/tests/alp.test.ts
9 ↗(On Diff #47617)

yup looks fine

This revision is now accepted and ready to land.May 4 2024, 20:33