Vue and Nuxt
@hydraone-sdk/core/vue provides a composable that wraps the core client in reactive state. It requires vue 3.5 or later as a peer dependency.
Composable
import { computed } from 'vue';
import { useWalletBridgeClient } from '@hydraone-sdk/core/vue';
export function useGameSession() {
const { isConnected, address, balanceADA, connect, disconnect, error } = useWalletBridgeClient({
autoConnect: true,
});
const label = computed(() =>
isConnected.value ? `${address.value} (${balanceADA.value} ADA)` : 'Offline',
);
return { label, connect, disconnect, error };
}
useWalletBridgeClient(options?)
Refs: connectionState, isConnected, address, balanceADA, balanceLovelace (bigint), networkId, hostInfo, language, theme, error. Also returns the client.
Actions: init, connect, disconnect, refreshBalance, refreshAddress, signTx, submitTx, signData, setOrientation, triggerHaptic.
Options: any WalletBridgeClientOptions, plus client, autoConnect (default false, runs init() on mount) and autoRefreshBalance (default true).
address is the change address in bech32. theme is null until the host reports one. networkId is updated by NETWORK_CHANGED events; call client.getNetworkId() to read it on demand. connect is an alias of init, and disconnect is synchronous. error holds the last error from init, refreshBalance, signTx, submitTx or signData; a balance read that fails with ERR_WALLET_NOT_CONNECTED is not recorded.
Connect to the App Center
Inside the App Center, pass hostOrigin and the SDK creates the PostMessageTransport for you (and destroys it with the client). You do not need to import PostMessageTransport:
const { connect } = useWalletBridgeClient({
hostOrigin: 'https://alpha.hydraone.app', // or an array: ['https://alpha.hydraone.app', 'https://staging.hydraone.app']
});
The client is shared, so the first composable to run decides its options. Set hostOrigin in the first call, or register the plugin below.
Plugin (recommended, required for SSR)
createHydraOne gives each app instance its own client. Nuxt builds a new app for every request, so nothing is shared between users:
// plugins/hydra.ts
import { createHydraOne } from '@hydraone-sdk/core/vue';
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(createHydraOne({ hostOrigin: 'https://alpha.hydraone.app' }));
});
For a plain Vue app, call app.use(createHydraOne({ hostOrigin: '...' })) before app.mount. After that, useWalletBridgeClient() needs no options.
createHydraOne(options) accepts any client option (including hostOrigin) plus client (use an existing client). It provides the client under HYDRA_CLIENT_KEY. The composable looks up options.client, then the plugin, then the shared instance. When the app unmounts, the plugin destroys the client it created; a client you pass in is left alone.
Shared instances
Without the plugin, the composable uses one shared client for the whole app, created on first use. The first call must pass hostOrigin (or a transport), otherwise creating the client throws. On the server the shared client is not cached, so each call gets a fresh one and nothing leaks between requests.
If you need to share the transport, for example with the host storage relay, create it yourself and register the client:
import {
WalletBridgeClient,
PostMessageTransport,
HostStorageRelayAdapter,
} from '@hydraone-sdk/core';
import { setSharedWalletBridgeClient } from '@hydraone-sdk/core/vue';
export function installHydra() {
const transport = new PostMessageTransport({ hostOrigin: 'https://alpha.hydraone.app' });
const client = new WalletBridgeClient({ transport });
const storage = new HostStorageRelayAdapter({ transport });
setSharedWalletBridgeClient(client);
return { client, storage };
}
Call installHydra() before any component uses the composable, for example in a Nuxt client plugin (plugins/hydra.client.ts) or in your app entry. Use the returned storage to read and write data through the host. Pass null to the setter to reset it in tests.
Nuxt
- Run the shared-instance setup (
installHydraabove) in a.client.tsplugin, because it needswindowandpostMessage. - The composables are SSR-safe: they register client listeners only in the browser, run
autoConnecton mount, and remove the listeners withonScopeDispose. RegistercreateHydraOneas a universal plugin (without.client) so every server request gets its own client. - With
ssr: falsethe plugin is optional;useWalletBridgeClient({ hostOrigin })is enough. - Wrap wallet-dependent markup in
<ClientOnly>or av-ifonisConnectedso server and client markup match.
Testing
Create a WalletBridgeClient with a stub ITransport, register it with setSharedWalletBridgeClient, and mount your component with Vue Test Utils. See Testing and health checks.
