selfhold

SELFHOLD / FOR DEVELOPERS Guide pages ↓

Connect an account

Ask for the person's account once, from a click. After that, your page can read the address without asking.

Connect

button.onclick = async () => {
  const address = await wallet.connect();
  showConnected(address);
};

connect() opens the wallet's window and resolves with the address once the person agrees. Call it first in the click handler, before you await anything else: browsers open windows only in direct answer to a click. If you wait first, the browser blocks the window and connect() fails with PopupBlocked.

Read the address on page load

const address = await wallet.address(); // null if this site wasn't given one
render(address ? signedIn(address) : signedOut());

address() never opens a window and never asks the person. Selfhold's window remembers the address it gave your site in your site's own storage, so it's there on the next visit.

Which wallet

By default the library uses Selfhold for Chrome where it's installed, and Selfhold's window everywhere else. To choose:

const wallet = selfhold({ use: "window" });    // always Selfhold's window
const chrome = selfhold({ use: "extension" }); // only Selfhold for Chrome

Where Selfhold for Chrome isn't installed, every call on chrome, and reading chrome.kind, throws NoWallet. Check window.lattice first, or use the default.

Selfhold for Chrome can reach your page after your script starts. If your page offers the choice, read wallet.kind when you render rather than once at load, or listen for the extension's lattice#initialized event on window.

When the account changes

The person can switch accounts in Selfhold, or use another passkey. Pass the address you built a write for as account:

await wallet.writeRecords(request, { account: address });

If the wallet uses another account now, nothing is sent, and the call fails with AccountChanged, or NotConnected if the site was disconnected. Read wallet.address() again and redraw your page.

Disconnect

wallet.disconnect() forgets the address on your site. With Selfhold's window, the person's account and passkey are untouched; they can connect again at any time.

On phones

Something wrong or missing here? Report it on GitHub ↗