SELFHOLD / FOR DEVELOPERS Guide pages ↓
Test your integration
Develop on localhost against the real Selfhold window, and test end to end with a browser's virtual passkey.
Develop on localhost
Selfhold's window takes calls from http://localhost and http://127.0.0.1, on any port, as well as from https sites. It shows them to the person as "a site on this computer". Any other http site is ignored.
The account you make while developing is a real Selfhold account, with a real passkey. Use a separate browser profile for it if you'd rather keep it apart from your own.
A chain to write to
Writes go to a real Lattice chain. To develop without touching one people use, run a node on your computer that hosts a chain of your own, and pass its address as a host:
const wallet = selfhold({ hosts: () => ["http://127.0.0.1:8080"] });
Selfhold's window, at https://wallet.selfhold.app, reads and sends to that node from the browser. So the node must allow requests from that origin, and your browser may ask you to allow the page to reach devices on your local network. See lattice.build ↗ for running a node. To test coin-free writes, run lattice-sponsor ↗ beside it and pass it as sponsor.
End-to-end tests with Playwright
Chromium's virtual authenticator stands in for the person's passkey, so a test can make an account and approve requests with no one there. Three things matter:
- A passkey belongs to one window's authenticator. The library opens a window for each request, and the window closes when it's done. In a test, keep it open and give every request the same one.
- The window's buttons wake only once it has focus. Bring it to the front before pressing them.
- The authenticator needs PRF (
hasPrf: true): the account's key is derived from it.
This helper does all three. Save it as selfhold-passkeys.js:
// A virtual passkey for Selfhold's window in a test. A passkey belongs to one window's
// authenticator, so every request opens in the same window, and the window stays open.
export async function passkeys(context, wallet = "wallet.selfhold.app") {
await context.addInitScript((wallet) => {
if (location.host === wallet) window.close = () => {};
// A new address each time, so the window loads the new request.
const open = window.open.bind(window);
window.open = (url, _name, features) => {
const to = new URL(url);
to.searchParams.set("request", String(Date.now()));
return open(to.href, "selfhold-test", features);
};
}, wallet);
let selfhold = null;
return async function window(press) {
const opened = selfhold ? selfhold.waitForEvent("load") : context.waitForEvent("page");
await press();
const page = await opened;
if (!selfhold) {
selfhold = page;
const cdp = await context.newCDPSession(selfhold);
await cdp.send("WebAuthn.enable");
await cdp.send("WebAuthn.addVirtualAuthenticator", {
options: { protocol: "ctap2", transport: "internal", hasResidentKey: true, hasUserVerification: true,
isUserVerified: true, hasPrf: true, automaticPresenceSimulation: true },
});
}
// Its buttons wake only once the window has focus.
await selfhold.bringToFront();
return selfhold;
};
}
Then a test presses your page's buttons through it, and Selfhold's:
import { test, expect } from "@playwright/test";
import { passkeys } from "./selfhold-passkeys.js";
test("a visitor connects and signs with Selfhold", async ({ page, context }) => {
const window = await passkeys(context);
await page.goto("http://localhost:8000/");
let selfhold = await window(() => page.getByRole("button", { name: "Connect" }).click());
await selfhold.getByRole("button", { name: "Create an account" }).click();
await selfhold.getByRole("button", { name: "Connect", exact: true }).click();
await expect(page.getByText(/^Connected: /)).toBeVisible();
selfhold = await window(() => page.getByRole("button", { name: "Sign a message" }).click());
await selfhold.getByRole("button", { name: "Sign with your passkey" }).click();
await expect(page.getByText(/^Signed: /)).toBeVisible();
});
To approve a write, press Approve with your passkey in the window; to decline, Decline. The helper brings the window to the front for you.
Unit tests
In unit tests, stand in for the wallet object: it's a plain object with kind, connect, address, disconnect, writeRecords and signMessage. Throw new SelfholdError("Declined", "…") (exported by the library) to test each failure your page handles.
Something wrong or missing here? Report it on GitHub ↗