SELFHOLD / FOR DEVELOPERS Guide pages ↓
API reference
Everything @selfhold/web exports. Types are TypeScript; the library ships its own declarations.
Making a wallet
selfhold(options?: Options): Wallet
Makes a wallet object. It opens nothing and asks nothing until you call it.
| Parameter | Type | |
|---|---|---|
options.use | "auto" | "window" | "extension" | Which wallet. Default "auto": Selfhold for Chrome where it's installed, else Selfhold's window. |
options.hosts | () => string[] | Promise<string[]> | The hosts Selfhold's window sends a write to, and a wait reads from. Default: the hosts Nexus lists for the write's chain. Selfhold for Chrome sends through the node the person chose in it. |
options.sponsor | () => string | Promise<string> | A fee sponsor's base URL, or "" for none. Default: none. Selfhold's window only. See Pay fees with work. |
options.wallet | string | The window's page. Default https://wallet.selfhold.app/. Tests point it at a local copy. |
Returns a Wallet.
const wallet = selfhold({ sponsor: () => "https://sponsor.your-app.example" });
The wallet
wallet.kind: "extension" | "window"
Which wallet is in use right now. Selfhold for Chrome can reach the page after your script starts, so read it when you need it.
Made with use: "extension" where Selfhold for Chrome isn't installed, reading kind throws NoWallet, as do address() and disconnect().
wallet.connect(): Promise<string>
Asks the person to connect their account to your site. Call it from a click.
Returns the account's address.
Throws SelfholdError: PopupBlocked, Closed, Declined, Reloaded, NoWallet, Failed.
wallet.address(): Promise<string | null>
The address this site was given. It never asks the person and never opens a window.
Returns the address, or null if the site has none.
Throws NoWallet, only with use: "extension" and no extension.
wallet.disconnect(): Promise<void>
Forgets the address on your site. With Selfhold for Chrome, the extension forgets your site too.
wallet.writeRecords(request, options?): Promise<Sent | null>
Writes records on one chain, and pays addresses from the account in the same transaction, once the person approves. Call it from a click.
| Parameter | Type | |
|---|---|---|
request | WriteRequest | Promise<WriteRequest | null> | The write, or a promise of it. Resolving to null sends nothing. |
request.chainPath | string[] | The chain, such as ["Nexus", "your-chain"]. |
request.actions | { key: string; oldValue?: string; newValue?: string }[] | The records to add, change or delete. See Actions. |
request.payments | { to: string; amount: string }[] | Optional. Addresses to pay, in whole coins. |
request.fee | string | Optional. The fee to pay. Default: the wallet sets it from what the node asks. |
options.account | string | The address you built the write for. If the wallet's is another, nothing is sent. |
options.progress | (text: string) => void | Hears what Selfhold's window is doing. |
Returns a Sent, or null if the request resolved to null.
Throws SelfholdError: PopupBlocked, Closed, Declined, Reloaded, AccountChanged, NotConnected, NoWallet, Failed; or a NeedsCoins.
wallet.signMessage({ text }): Promise<{ publicKey: string; signature: string }>
Signs text, once the person approves. The signed bytes are "lattice-message:" + your origin + "\n" + text, so a signature for one site can't be used as another's. Call it from a click.
Returns the account's public key and the signature.
Throws SelfholdError: PopupBlocked, Closed, Declined, Reloaded, AccountChanged, NoWallet, Failed.
Sent
What writeRecords() resolves with: a write that is signed and taken by a node.
| Property | Type | |
|---|---|---|
transactionCID | string | The transaction's ID. |
account | string | The account that signed it. |
chainPath | string[] | Its chain. |
nonce | string | undefined | Its nonce, where the wallet says it. |
host | string | undefined | The host it went to, where the wallet says it. Selfhold for Chrome gives only a public https host, never the person's own node. |
sent.wait(options?: WaitOptions): Promise<void>
Resolves once the write lands. See Wait for it to land.
| Parameter | Type | |
|---|---|---|
options.onChange | (state: "sent" | "slow" | "late") => void | Hears each new state, from "sent". |
options.check | () => Promise<boolean | string> | Decides from your own state: true landed, false not yet, a string when another write took its place. Default: the account's nonce has passed the write's. |
options.signal | AbortSignal | Stops waiting; wait() rejects with its reason. |
Throws SelfholdError: Lost, NotRecorded; Failed if there is no host to read and no check.
sent.checkNow(): void
Reads at once instead of at the next turn: a “Check again” for a late write.
Errors
class SelfholdError extends Error
Every failure. code: one of the codes. message: written for the person. docUrl: this code's entry in Errors.
Construct one with new SelfholdError(code, message), for example in tests.
class NeedsCoins extends SelfholdError
The account can't pay for the write. Its code is "NeedsCoins". address: the account. needed, balance: bigints, in coins. sponsor: why the fee sponsor didn't pay, "down", "dear" or "most", or null if none was asked.
asSelfholdError(error: unknown): SelfholdError
Turns any failure into a SelfholdError: itself if it is one, else coded from the wallet's own code or words, else Failed.
Constants
TIMES- The wait's timings, in milliseconds:
slow30 000,late300 000,giveUp1 800 000,every2 500 (how often it reads),everyLate15 000,read20 000 (how long one read may take). PROTOCOL- The version of the messages between the library and Selfhold's window:
1. ERRORS_URL- Where error codes are explained:
https://selfhold.app/docs/errors.html.
Something wrong or missing here? Report it on GitHub ↗