Connector API
Configure the wallet connector once at the application root, then reuse it to connect wallets, read approved accounts, and observe state changes.
Prerequisites: @d9-network/dapp-connector@0.2.0 and effect@4.0.5 (see SDK Packages).
1. Create the connector
Section titled “1. Create the connector”Create src/connector.ts and instantiate the layer once for the entire application:
import { D9DAppConnector } from "@d9-network/dapp-connector";import { d9Config } from "./config";
export const connectorOptions = { dappName: "My D9 DApp", walletConnect: { projectId: d9Config.projectId, relayUrl: d9Config.relayUrl, metadata: { name: "My D9 DApp", description: "My decentralized application", url: window.location.origin, icons: [`${window.location.origin}/icon.png`], }, },};
export const connectorLayer = D9DAppConnector.layer(connectorOptions);2. Connect and read accounts
Section titled “2. Connect and read accounts”import { Effect } from "effect";import { D9DAppConnector } from "@d9-network/dapp-connector";
export const connectProgram = Effect.gen(function* () { const connector = yield* D9DAppConnector; const start = yield* connector.connect({ transport: "auto" });
if (start.transport === "walletconnect") { // Render start.uri as a QR code or mobile deep link before waiting for approval yield* start.approval; }
return yield* connector.getAccounts;});start.approval, start.cancel, start.response, connector.snapshot, and connector.getAccounts are Effect values—consume them with yield*, not await.
Run the programs in steps 2 and 3 with a runtime built from connectorLayer, such as walletRuntime in Quickstart.
Verify: getAccounts returns the list of user-approved accounts.
3. Subscribe to connection changes
Section titled “3. Subscribe to connection changes”connector.changes is an Effect Stream that emits the current snapshot immediately upon subscription:
import { Effect, Stream } from "effect";import { D9DAppConnector } from "@d9-network/dapp-connector";
export const watchConnectionProgram = Effect.gen(function* () { const connector = yield* D9DAppConnector; yield* Stream.runForEach(connector.changes, (snapshot) => Effect.logInfo(`Wallet status: ${snapshot.status} (${snapshot.transport})`), );});4. Open D9 Wallet on mobile
Section titled “4. Open D9 Wallet on mobile”Pass the WalletConnect pairing URI and your wallet URL scheme to buildD9WalletConnectAppLink from the /protocol entry point:
import { buildD9WalletConnectAppLink } from "@d9-network/dapp-connector/protocol";
const deepLink = buildD9WalletConnectAppLink(start.uri, d9Config.walletScheme);See Browser Connection for a complete end-to-end example.
Without Effect: the /web facade
Section titled “Without Effect: the /web facade”For React, Vue, or plain TypeScript/JavaScript apps that do not use Effect directly, import the Promise-based /web facade with the same connectorOptions:
import { createWebD9Connector } from "@d9-network/dapp-connector/web";import { connectorOptions } from "./connector";
export const webConnector = createWebD9Connector(connectorOptions);
// Read the current state synchronouslyconst accounts = webConnector.getAccounts();const snapshot = webConnector.getSnapshot();
// Subscribe to state changesconst unsubscribe = webConnector.subscribe((nextSnapshot) => { console.log("Updated status:", nextSnapshot.status);});
// Disconnect the session, or shut down the connector permanentlyawait webConnector.disconnect();await webConnector.close();/protocol exports
Section titled “/protocol exports”The /protocol entry point contains pure data helpers and schemas with no transport SDK dependencies:
| Export | Purpose |
|---|---|
serializeD9SiwxMessage |
Build the canonical CAIP-122 sign-in message |
D9AuthenticateParamsSchema |
Decode and validate a sign-in challenge (Effect Schema) |
D9_WALLET_CONNECT_CHAIN_ID |
Constant d9net:000000000000000000000000000000d9 |
