setRustApi method

void setRustApi({
  1. String? libraryPath,
  2. String? wasmGlueUrl,
  3. String? wasmModuleUrl,
  4. bool randomFromBackend = true,
  5. bool seal = false,
})

Selects the Rust backend via Bu.

Cross-platform safe: each platform reads only its own fields.

  • Native VM: libraryPath selects the dynamic library (DynamicLibrary.open); null uses the platform default name. The wasm fields are ignored.
  • Web: wasmGlueUrl selects the wasm-bindgen glue ES module (default wasm_assets/bu.js, relative to the page), loaded with a dynamic import(); wasmModuleUrl overrides the .wasm location (null defaults to bu_bg.wasm next to the glue via the module's import.meta.url, or a pre-set globalThis.__bu_wasm_url). libraryPath is ignored. The glue must be a --target web build (see tool/build_backends.dart --wasm in blockchain_utils_connector): a --target no-modules classic script has no exports and fails to load — no companion files are needed. Await Bu.ensureWasmLoaded (see BlockchainUtilsTestApi.initWasmBrowser) before calling this on web, as Bu.init alone does not fetch the module.

Implementation

void setRustApi({
  String? libraryPath,
  String? wasmGlueUrl,
  String? wasmModuleUrl,

  /// Route [QuickCrypto.generateRandom] through the backend RNG
  /// (seeded from Fortuna on first use). Pass `false` to keep the
  /// current generator (e.g. a custom one from `setupRandom`).
  bool randomFromBackend = true,

  bool seal = false,
}) {
  _checkNotSealed('setRustApi');
  final bu = Bu.init(
    BuConfig(
      libraryPath: libraryPath,
      wasmGlueUrl: wasmGlueUrl,
      wasmModuleUrl: wasmModuleUrl,
    ),
  );
  final frostApi = NativeFrostApi(bu.frost);
  blockchainUtils.setApi(
    DefaultBlockchainUtilsApi(
      hash: NativeHashApi(bu.hash),
      blockCipher: NativeBlockCipherApi(bu.blockCipher),
      ff1: ValidatedFf1Api(NativeFf1Api(bu.ff1)),
      ed25519: NativeEd25519CurveApi(bu.curves.ed25519()),
      secp256k1: NativeSecp256k1CurveApi(bu.curves.secp256k1()),
      secp256r1: NativeSecp256r1CurveApi(bu.curves.secp256r1()),
      jubjub: NativeJubJubCurveApi(bu.curves.jubjub()),
      signing: DefaultBlockchainUtilsSigningApi(
        secp256k1: ValidatedSecp256k1SigningApi(
          NativeSecp256k1SigningApi(bu.secp256k1Signing),
        ),
        ed25519: NativeED25519SigningApi(bu.ed25519Signing),
        secp256r1: NativeSecp256r1SigningApi(bu.secp256r1Signing),
        bls: NativeBlsSigningApi(bu.blsSigning),
        mldsa: NativeMLDSASigningApi(bu.mlDsa),
        jubjub: NativeRedJubJubSigningApi(bu.reddsa),
        pallas: NativeRedPallasSigningApi(bu.reddsa),
        sr25519: NativeSr25519SigningApi(bu.sr25519),
      ),
      aed: NativeAEADApi(bu.aead),
      streamCipher: NativeStreamCipherApi(bu.streamCipher),
      keyDerivation: NativeKeyDerivationApi(bu.bindings),
      g1: NativeG1ProjectiveCurveApi(bu.curves.g1()),
      g2: NativeG2ProjectiveCurveApi(bu.curves.g2()),
      bn254G1: NativeBn254G1CurveApi(bu.curves.bn254G1()),
      bn254G2: NativeBn254G2CurveApi(bu.curves.bn254G2()),
      muSig2: NativeMuSig2Api(bu.musig2),
      frost: frostApi,
      dkls23: ValidatedDkls23KeysApi(NativeDkls23KeysApi(bu.dkls23)),
      pallas: NativePallasCurveApi(bu.curves.pallas()),
      vesta: NativeVestaCurveApi(bu.curves.vesta()),
      ristretto: NativeRistrettoCurveApi(bu.curves.ristretto()),
      x25519: NativeX25519Api(bu.x25519),
      monero: NativeMoneroApi(bu.monero),
      hw: NativeHwApi(bu.hw),
      sapling: NativeSaplingApi(bu.sapling),
      orchard: NativeOrchardApi(bu.orchard),
      merkle: NativeMerkleApi(bu.merkle),
      rng: NativeRngApi(bu.rng),
    ),
  );
  _backend = bu;
  // Wire the RNG before sealing: once sealed, setupBackendRng throws.
  if (randomFromBackend) QuickCrypto.setupBackendRng();
  if (seal) this.seal();
}