@avalabs/crypto-wasm
v0.3.1
Published
WebAssembly build of avalabs-crypto-core. Ships a single-file JS bundle (wasm inlined as base64) plus a thin TypeScript wrapper exposing pubkey-only batched address derivation for the web app, Chrome extension, and Node. Most callers should depend on @ava
Readme
@avalabs/crypto-wasm
WebAssembly build of @avalabs/crypto-core. Ships:
dist/index.{js,cjs,d.ts,d.cts}— thin TypeScript wrapper exposing the pubkey-only public surface documented under API below.dist/crypto.js— Emscripten loader with the WASM binary base64- inlined inside it (single artifact, MV3 extension-safe). When built withCRYPTO_WASM_SINGLE_FILE=OFF, also emitsdist/crypto.wasmnext to it for web-app builds that prefer streaming compile + a smaller bundle.
Most callers should depend on @avalabs/crypto-sdk instead — it
routes to this package on web / extension / Node (via the browser /
import exports conditions) and to @avalabs/crypto-nitro on React
Native. The direct exports on this package carry @deprecated JSDoc
hints to nudge consumers toward the unified entry point.
Build prerequisites
The build needs Emscripten (emcc, emcmake, emmake) on $PATH.
libsecp256k1 is vendored on demand into
packages-internal/crypto-core/deps/secp256k1 by scripts/build-wasm.sh
— no separate setup step needed for a clean checkout.
# One-time emsdk setup (if not already installed)
~/emsdk/emsdk install latest
~/emsdk/emsdk activate latest
source ~/emsdk/emsdk_env.shBuild
pnpm --filter @avalabs/crypto-wasm run buildRuns the TypeScript wrapper build (tsdown) and the WASM build
(scripts/build-wasm.sh). The WASM build:
- Sources
~/emsdk/emsdk_env.shifEMSDKisn't already exported. - Vendors libsecp256k1 into
packages-internal/crypto-core/deps/secp256k1if missing. emcmake cmakeagainstCMakeLists.txt, which transitively includespackages-internal/crypto-core/CMakeLists.txt.emmake make -jcompiles the artifacts.- Copies them into
dist/(and intosrc/so tsdown can resolveimport createCryptoModule from './crypto.js'at TypeScript-bundle time).
Override the artifact mode with:
CRYPTO_WASM_SINGLE_FILE=OFF pnpm --filter @avalabs/crypto-wasm run build:wasmAPI
ℹ️ Most consumers should import these from
@avalabs/crypto-sdk— same signatures, but the SDK picks the right backend (nitro on React Native, this package elsewhere) at bundle time. The direct exports below carry@deprecatedJSDoc tags.
import {
init,
deriveAddressesFromXpubs,
deriveAddressesForEvm,
deriveAddressesForSvm,
deriveAddressesForBtc,
deriveAddressesForAvalanche,
MAX_BATCH_SIZE,
type DerivedSecp256k1Addresses,
type DerivedAvalancheAddresses,
} from '@avalabs/crypto-wasm';
// 1. Load the WASM module. Required once before any derive*() call.
// Idempotent — repeated invocations re-use the cached promise.
await init();
// 2. Xpub-driven batch derivation (Ledger / watch-only flow).
// `avalancheXpubs[i]` and `accountIndices[i]` pair up.
const rows: DerivedSecp256k1Addresses[] = await deriveAddressesFromXpubs(
'xpub...', // EVM xpub at m/44'/60'/0'
['xpub...', 'xpub...'], // Avalanche xpubs (one per account)
false, // isTestnet
[0, 1], // account indices
);
// rows[i] === { accountIndex, evm, btc, avm, pvm, coreEth }
// 3. Per-chain encoders for batches of already-derived pubkeys.
// Each accepts Uint8Array | ArrayBuffer per element; the wrapper
// takes a tight copy at the boundary, so slices of a larger buffer
// work correctly.
const evmAddrs = await deriveAddressesForEvm(compressedSecp256k1PubKeys33);
const btcAddrs = await deriveAddressesForBtc(compressedSecp256k1PubKeys33, false);
const svmAddrs = await deriveAddressesForSvm(ed25519PubKeys32);
const avaxBundles: DerivedAvalancheAddresses[] =
await deriveAddressesForAvalanche(
compressedAvaxPubKeys33, // drives X- / P-
compressedEvmPubKeys33, // drives C-
false, // isTestnet
);
// avaxBundles[i] === { x, p, coreEth }
// Hard upper bound on items per batch (currently 1024). Re-exported so
// callers can chunk deterministically instead of catching `RangeError`.
console.log(MAX_BATCH_SIZE);What's not in the surface here
signSchnorr, verifySchnorr, sign, verify, getPublicKey*,
getExtendedPublicKey, and pointAddScalar currently exist only in
@avalabs/crypto-nitro. Adding them here is a follow-up — track via
the crypto-wasm milestone.
