argus/src/web/libauth-entry.js

78 lines
2.6 KiB
JavaScript
Raw Normal View History

// A single shared libauth for the whole page.
//
// WHY THIS FILE EXISTS
// libauth initialises its WASM crypto (secp256k1, sha256, ripemd160, …) with a
// TOP-LEVEL AWAIT. If two bundles each carry their own copy, each has its own
// unsettled init, and esbuild's inlined dynamic imports can hand out a bundle's
// exports before that init completes. Symptom, observed on cold load:
//
// TypeError: Cannot read properties of undefined
// (reading 'derivePublicKeyCompressed')
// at deriveNostrPublicKey (wizardconnect.js)
// at generateKeyExchangeCredentials
// at initiateDappRelay
//
// — failing 65 ms in, then "working" on every retry once the WASM had warmed.
// A bug that disappears when you look at it twice is exactly the kind that
// ships.
//
// So libauth becomes a real, separate ES module that every other bundle imports
// by bare specifier, resolved through the import map in register.html. The
// browser's module loader guarantees a dependency's top-level await settles
// before any importer evaluates — the ordering we need — and the page downloads
// libauth once instead of three times.
//
// WHY THE EXPORTS ARE LISTED BY HAND
// `export *` would defeat tree-shaking and ship all ~1 MB of libauth. This list
// is the union of what our own modules use and what @wizardconnect/core and
// /dapp import (they resolve to this module too, via the import map). Missing a
// name would be a runtime failure in a path we cannot unit-test, so
// build-web.mjs verifies this list against the imports every other bundle
// actually emits, and fails the build if anything is absent. Add freely; the
// check will tell you if you remove something still in use.
export {
// ---- used by @wizardconnect/core and @wizardconnect/dapp ----
bech32PaddedToBin,
binToBech32Padded,
decodeHdPublicKey,
deriveHdPublicNodeChild,
generatePrivateKey,
hash256,
secp256k1,
// ---- encoding ----
binToHex,
hexToBin,
utf8ToBin,
binToUtf8,
binToBase64,
base64ToBin,
// The WalletConnect / WizardConnect wire format: "<Uint8Array: 0x..>" and
// "<bigint: ..n>". Wallets validate these strictly.
stringify,
// ---- addresses ----
cashAddressToLockingBytecode,
lockingBytecodeToCashAddress,
encodeCashAddress,
decodeCashAddress,
// ---- hashing ----
sha256,
hash160,
ripemd160,
// ---- transactions ----
encodeTransaction,
decodeTransaction,
generateSigningSerializationBCH,
// ---- keys / HD wallets (the built-in wallet) ----
generateBip39Mnemonic,
deriveSeedFromBip39Mnemonic,
deriveHdPrivateNodeFromSeed,
deriveHdPath,
deriveHdPublicNode,
} from "@bitauth/libauth";