Browser Connection
Connect to D9 Wallet from a web application and render the user’s approved accounts.
Prerequisites: @d9-network/dapp-connector@0.2.0 and connectorOptions from Connector API.
1. Add status and account elements
Section titled “1. Add status and account elements”<p id="status"></p><select id="accounts"></select>2. Connect inside D9 Wallet’s in-app browser (src/main.ts)
Section titled “2. Connect inside D9 Wallet’s in-app browser (src/main.ts)”import { createWebD9Connector } from "@d9-network/dapp-connector/web";import { connectorOptions } from "./connector";
const connector = createWebD9Connector(connectorOptions);
const statusEl = document.getElementById("status");const accountsEl = document.getElementById("accounts");if (!(statusEl instanceof HTMLElement) || !(accountsEl instanceof HTMLSelectElement)) { throw new Error("Expected #status and select#accounts in the page");}
// Update the UI on every connection state changeconnector.subscribe((snapshot) => { statusEl.textContent = `Status: ${snapshot.status} (${snapshot.transport ?? "none"})`; accountsEl.innerHTML = ""; for (const account of snapshot.accounts) { const opt = document.createElement("option"); opt.value = account.address; opt.textContent = account.name ? `${account.name} (${account.address.slice(0, 8)}...)` : account.address; accountsEl.appendChild(opt); }});
// Connect via the injected providerexport async function connectInjected() { const isAvailable = await connector.papi.isAvailable(); if (!isAvailable) { alert("D9 Wallet injected provider is not available in this browser."); return; } await connector.papi.connect();}
export async function disconnect() { await connector.disconnect();}Bind connectInjected() and disconnect() to your UI buttons.
3. Connect from any other browser (Effect)
Section titled “3. Connect from any other browser (Effect)”The injected provider is only present inside D9 Wallet’s in-app browser. In desktop or standard mobile browsers, connect with transport: "auto". When WalletConnect starts, render uri as a QR code and provide deepLink as an Open D9 Wallet link:
import { Effect } from "effect";import { D9DAppConnector } from "@d9-network/dapp-connector";import { buildD9WalletConnectAppLink } from "@d9-network/dapp-connector/protocol";import { d9Config } from "./config";import { walletRuntime } from "./wallet";
export async function connectNative( onQrUri: (uri: string, deepLink: string) => void,) { return walletRuntime.runPromise( Effect.gen(function* () { const connector = yield* D9DAppConnector; const start = yield* connector.connect({ transport: "auto" });
if (start.transport === "walletconnect") { if (start.uri) { const deepLink = buildD9WalletConnectAppLink(start.uri, d9Config.walletScheme); onQrUri(start.uri, deepLink); } yield* start.approval; }
return yield* connector.getAccounts; }), );}walletRuntime is configured in Quickstart.
Verify
Section titled “Verify”- Step 2: After wallet approval,
#statusdisplaysconnectedandselect#accountslists the approved accounts. - Step 3:
connectNativeresolves to the approved accounts array without mutating the DOM directly.
Common errors
Section titled “Common errors”| Error | Resolution |
|---|---|
Expected #status and select#accounts in the page |
Add the HTML elements from step 1. |
Alert: D9 Wallet injected provider is not available… |
Open the page in D9 Wallet’s in-app browser, or use the WalletConnect flow in step 3. |
