Connection.toJson
The state of a connection as text, for local storage or any
other place that holds text. Connection.fromJson reads it back, and never
fails. For a cookie, Connection.toCookie writes this text
with what a cookie needs.
import { Effect, Layer, Stream } from "effect";import { Connection, Wallets } from "@ethect/core";
// Whatever changes (account, chain, wallet, disconnection), what is stored follows.const persist = Connection.changes.pipe( Stream.map(Connection.toJson), Stream.runForEach((text) => Effect.sync(() => localStorage.setItem("connection", text))),);
// On the next visit, the app starts from it.const App = (stored: string | null) => Connection.layer({ initial: Connection.fromJson(stored ?? undefined) }).pipe( Layer.provideMerge(Wallets.browser), );
const app = App(localStorage.getItem("connection"));In a cookie
Section titled “In a cookie”Connection.toCookie and
Connection.fromCookie are these two functions
for a cookie: the text is percent-encoded and gets its attributes, and it is read back from
document.cookie or from a Cookie header.
fromJson is for a framework that hands you the value of one cookie, already decoded.
import { Layer } from "effect";import { Connection, Wallets } from "@ethect/core";
// `cookie` is the value of the `connection` cookie, as your framework gives it: decoded.const Server = (cookie: string | undefined) => Connection.layer({ initial: Connection.fromJson(cookie) }).pipe( Layer.provideMerge(Wallets.of([])), );fromJson reads JSON. A value that is still percent-encoded reads as disconnected: that
one is for fromCookie.
fromJson
Section titled “fromJson”Reads what toJson wrote. The result is a state to start from: pass it to
Connection.layer as initial.
import { Connection } from "@ethect/core";
Connection.fromJson('{"v":1,"wallet":"io.metamask","accounts":["0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"],"chainId":1}');// { status: "reconnecting", wallet: "io.metamask", accounts: ["0xf39fd6e5…fffb92266"], chainId: 1 }
Connection.fromJson('{"v":1}'); // { status: "disconnected" }Connection.fromJson(undefined); // { status: "disconnected" }: no cookieConnection.fromJson("not json"); // { status: "disconnected" }What is stored
Section titled “What is stored”| The state | toJson gives |
|---|---|
connected, reconnecting |
{"v":1,"wallet":"io.metamask","accounts":["0xf39Fd6e5…cffFb92266"],"chainId":1}, each account in its EIP-55 form |
disconnected, connecting |
{"v":1} |
| What is read | fromJson gives |
|---|---|
| a stored connection | reconnecting, with its wallet, accounts and chain |
{"v":1} |
disconnected |
| anything else: no text, text that is not JSON, another version, a field that is missing or of another type, an account that is not an address | disconnected |
- Storing
disconnectedmatters. After the user disconnects, what is stored says so. The next visit startsdisconnected, andreconnectdoes nothing: the user is not connected again behind their back. - The id of the wallet is what is remembered.
reconnectlooks for a wallet with that id among theWallets. No provider, no permission and no key is stored.
Returns
Section titled “Returns”| Function | Type |
|---|---|
toJson(state) |
string |
fromJson(text) |
Connection.Disconnected | Connection.Reconnecting |
Parameters
Section titled “Parameters”- Type:
Connection.State
The state to store: usually each one that Connection.changes
gives.
- Type:
string | undefined
What storage holds, or undefined when it holds nothing.
Behavior
Section titled “Behavior”fromJsonnever fails and never throws, whatever it is given.- A stored connection reads as
reconnecting, never asconnected. Only the wallet can say that it is connected:reconnectasks it. - A cookie is a hint, never an identity. Anyone can write a cookie with any address in it. Render with it, and decide nothing from it: what an account may see or do is proved by a signature, on the server.
- Accounts are written in EIP-55, as every address ethect writes.
- Addresses are read as addresses. An account in any case is read as the lowercase
address, so what was stored in lowercase before reads as it did. One that is not 20 bytes of
hex makes the whole text read as
disconnected. - The version is checked. Text with another
vthan1reads asdisconnected. - What it does not know is left out. Other fields in the text are ignored.
Under the hood
Section titled “Under the hood”Nothing is sent, and nothing is asked of a wallet: these are two pure functions.
toJson(state) connected, reconnecting → {"v":1,"wallet":…,"accounts":[…in EIP-55],"chainId":…} anything else → {"v":1}
fromJson(text) JSON with v 1, a wallet, accounts that are addresses and a chain id → { status: "reconnecting", wallet, accounts, chainId } anything else → { status: "disconnected" }