Installed wallets (Wallets)
The wallets a page can talk to: a list, and a stream of that list which tells when a wallet
announces itself. Wallets is a service, so the list comes from the browser, from a fixed
list in a test or on a server, or from several sources at once. Connecting to one of them is
Connection.
import { Effect, Stream } from "effect";import { Wallets } from "@ethect/core";
// A wallet picker: the list fills in as wallets announce themselves.const picker = (show: (wallets: ReadonlyArray<Wallets.Wallet>) => void) => Wallets.changes.pipe( Stream.runForEach((wallets) => Effect.sync(() => show(wallets))), // In an app this layer is one of the layers of its runtime, built once. Effect.provide(Wallets.browser), );
Effect.runFork(picker((wallets) => console.log(wallets.map((wallet) => wallet.name))));// ["MetaMask"]// ["MetaMask", "Rabby"]| Export | Kind | Description |
|---|---|---|
Wallets.browser |
layer | Injected wallets: EIP-6963 announcements, and window.ethereum when no wallet announced it. |
Wallets.of(wallets) |
layer | A fixed list: none on a server, a fake in a test, a wallet of your own. |
Wallets.all(...sources) |
layer | Several sources as one list. |
Wallets.get |
effect | The wallets known now. |
Wallets.changes |
stream | The list now, and again each time a wallet announces itself. |
Wallets.Wallets |
service | Where the wallets come from: get and changes. |
Wallets.Wallet |
type | A wallet: id, name, icon, and the provider asked for when connecting. |
Wallet
Section titled “Wallet”What a picker shows, and what Connection.connect takes.
| Field | Type | Holds |
|---|---|---|
id |
string |
The same on every visit: the wallet’s reverse domain name ("io.metamask"), or "injected" for window.ethereum. It is what a stored connection remembers. |
name |
string |
The name to show. |
icon |
string, optional |
An image as a data URI, as the wallet gave it. |
provider |
Effect<Eip1193.Provider, Signer.Error> |
The wallet’s provider. An effect: it runs when the user picks the wallet, and not before. |
An injected wallet’s provider is already there. A wallet that loads an SDK or opens a
session does it in that effect, so nothing is loaded for a wallet nobody picks.
import { Effect } from "effect";import { type Eip1193, Signer, type Wallets } from "@ethect/core";
// Stands for the SDK of a wallet: it gives a provider once it is loaded.declare const loadSdk: () => Promise<{ readonly provider: Eip1193.Provider }>;
const ledger: Wallets.Wallet = { id: "com.ledger", name: "Ledger", // Run by `Connection.connect`. What it fails with is what `connect` fails with. provider: Effect.tryPromise({ try: () => loadSdk().then((sdk) => sdk.provider), catch: () => new Signer.Error({ reason: "Unknown", message: "The Ledger SDK did not load" }), }),};The icon comes from the wallet, which is code you did not write. Show it as the src of an
image, and nowhere else.
browser
Section titled “browser”The wallets installed in the browser. Each one announces itself (EIP-6963), so several wallets are listed side by side, each with its own provider.
import { Effect } from "effect";import { Wallets } from "@ethect/core";
const installed = Wallets.get.pipe(Effect.provide(Wallets.browser));// [{ id: "io.metamask", name: "MetaMask", icon: "data:image/svg+xml;…", provider }, …]| The page has | The list has |
|---|---|
| wallets that announce themselves | one wallet each, with id its reverse domain name, in the order they announced |
window.ethereum, which no wallet announced |
one more wallet: id: "injected", name: "Browser wallet", no icon |
window.ethereum, which a wallet announced |
that wallet once, under its own name |
no window: a server |
nothing, for good |
- The list grows. A wallet that loads after the page announces itself later:
changesgives the list again. "injected"comes one turn of the event loop after the layer is built. By then the wallets that were there have announced themselves, so a wallet is never shown twice."injected"goes when its wallet announces itself. A wallet that later announces the object inwindow.ethereumtakes its place, under its own id.- The same id again replaces the wallet, in the place it had. An announcement that changes nothing is not said again.
A list that does not change.
import { Layer } from "effect";import { Connection, Wallets } from "@ethect/core";
// On a server there is no wallet: the page is rendered from what the cookie held.const Server = (cookies: string | null) => Connection.layer({ initial: Connection.fromCookie(cookies) }).pipe(Layer.provide(Wallets.of([])));In a test, the list is the wallet the test controls:
Wallets.of([wallet.wallet]), with a TestWallet.
Several sources as one list: the lists one after the other, in the order the sources are given.
import { Wallets } from "@ethect/core";
declare const ledger: Wallets.Wallet;
// The wallets of the browser first, then yours.const Sources = Wallets.all(Wallets.browser, Wallets.of([ledger]));A wallet that two sources know is listed once: the first one is kept.
- Type:
Effect<ReadonlyArray<Wallets.Wallet>, never, Wallets.Wallets>
The wallets known now. Right after Wallets.browser is built, it already has the wallets that
were installed when the page loaded: they answer before the layer is ready.
changes
Section titled “changes”- Type:
Stream<ReadonlyArray<Wallets.Wallet>, never, Wallets.Wallets>
The list now, then the whole list again each time it changes. Whoever subscribes late still gets the list as it is.
The stream of a list that cannot change ends after its one list: Wallets.of, and
Wallets.browser where there is no window. In a browser it never ends.
A source of your own
Section titled “A source of your own”Wallets.Wallets is the service: a get and a changes. A layer that provides it is a
source, and goes into Wallets.all like the others.
import { Effect, Layer, SubscriptionRef } from "effect";import { Wallets } from "@ethect/core";
// Wallets that are found one by one: paired devices, sessions that were restored.const Paired = Layer.effect(Wallets.Wallets)( Effect.gen(function* () { const found = yield* SubscriptionRef.make<ReadonlyArray<Wallets.Wallet>>([]); return { get: SubscriptionRef.get(found), changes: SubscriptionRef.changes(found), // the list now, then each new one }; }),);
const Sources = Wallets.all(Wallets.browser, Paired);Under the hood
Section titled “Under the hood”No request is sent to any wallet: listing wallets asks them nothing.
Wallets.browser, when the layer is builtno
windowgives an empty list, for good- your programlistens to
"eip6963:announceProvider"onwindow - your programdispatches
"eip6963:requestProvider": the wallets that are there answer at once
each announcement
{ info: { uuid, name, icon, rdns }, provider }- your programa wallet
{ id: rdns, name, icon, provider }. The same id again replaces it - your programwhat is not an announcement is left out
- your programa wallet
one turn of the event loop later
window.ethereumexists, and no announced wallet has that object as its providergives one more wallet:{ id: "injected", name: "Browser wallet", provider }a wallet later announces that objectgives
"injected"is removed
the layer's scope closes
- your programthe listener is removed