Reference
Client library
The TypeScript functions the app uses to build links, read escrows and make transactions.
On this page
Overview#
These are modules inside the repository, imported through the @/ alias. They are not published to npm.
Links and keys#
From src/lib/crypto.ts.
| Export | Returns | What it does |
|---|---|---|
| generateEphemeralKeypair() | EphemeralKeys | Makes a new Ed25519 keypair in memory |
| buildClaimUrl(baseUrl, escrowId, privateKey, note?, giftId?) | string | Builds the link, with the key, note and gift ID in the fragment |
| parseClaimFragment(hash) | { privateKey, note, giftId } | Reads the fragment. The note is cut to 60 characters. |
| MAX_NOTE_LENGTH | 60 | The longest note |
<base>/claim?id=<escrow ID>#k=<private key>&n=<note>&g=<gift blob ID>Escrows and transactions#
From src/lib/chain/escrow.ts.
| Export | Returns | What it does |
|---|---|---|
| readEscrows(ids) | Map<string, Escrow | null> | Reads open escrows, 50 at a time |
| readClosed(escrowId) | Closed | null | Finds how a deleted escrow ended |
| findTransaction(digest) | result | missing | unknown | Looks up any transaction |
| readCreated(digest) | CreateOutcome | Finds what became of a create transaction |
| createdEscrowId(tx) | string | null | Gets the escrow ID from a transaction result |
| buildCreate(input) | Transaction | Builds a create_escrow call |
| buildClaim(escrow, privateKey, recipient) | Transaction | Signs the claim message and builds a claim_escrow call |
| buildReclaim(escrow) | Transaction | Builds a reclaim_escrow call |
| claimMessage(escrowId, recipient) | Uint8Array | The 64 bytes the key signs |
| toUnits(amount) | bigint | Decimal to base units, at four decimal places |
| fromUnits(units) | number | Base units to decimal, for display |
| chainErrorMessage(err, fallback) | string | A message that is safe to show |
The read functions and buildClaim return promises of these types.
Missing is not the same as unreadable#
In the map from readEscrows, null means the escrow no longer exists. An absent ID means it couldn’t be read, so its status is unknown. Likewise, a CreateOutcome of unknown must never be treated as a failure, because the money may already be locked.
Accounts and withdrawals#
From src/lib/chain/account.ts.
| Export | Returns | What it does |
|---|---|---|
| readHoldings(owner) | Holding[] | Balances of the tokens this network supports |
| buildWithdrawal(withdrawal) | Transaction | Builds a transfer, or sends all SUI |
| estimateFee(withdrawal) | bigint | Simulates it with real coins. Throws if it would fail. |
| addressProblem(text, owner) | string | null | Why an address looks unsafe |
| parseUnits(text) | bigint | null | Typed amount to base units, exactly |
| formatUnits(units) | string | Base units to text, with every digit |
readHoldings, estimateFee and addressProblem return promises.
Gifts and Walrus#
From src/lib/gift.ts, src/lib/walrus.ts and src/lib/yetiCards.ts.
| Export | Returns | What it does |
|---|---|---|
| sealGift(gift, privateKey) | Uint8Array | Encrypts a gift with the link’s key |
| openGift(blob, privateKey) | Gift | Decrypts it. Throws if anything is off. |
| giftMime(bytes) | GiftMime | null | What the bytes really are |
| storeBlob(blob, signal?) | string | Stores bytes and returns the blob ID |
| readBlob(id) | Uint8Array | Reads bytes, up to the size limit |
| YETI_CARDS | YetiCard[] | The ready-made cards |
| loadCard(card) | Gift | Downloads a ready-made card |
| GIFTS_ENABLED | boolean | Whether this site can store and read gifts |
All but giftMime and the constants return promises. A sealed gift is a 12-byte IV, then the encrypted version byte, type byte and content.
Example: create a link#
import { LINK_DURATION_MS } from "@/lib/chain/config";
import { buildCreate, toUnits } from "@/lib/chain/escrow";
import { buildClaimUrl, generateEphemeralKeypair } from "@/lib/crypto";
const keys = generateEphemeralKeypair();
const transaction = buildCreate({
token: "SUI",
amountUnits: toUnits(10),
waterUnits: toUnits(0.15),
publicKeyHex: keys.publicKeyHex,
durationMs: LINK_DURATION_MS,
themeId: "glacial",
});
// Sign and send it, then read the escrow ID from the result.
const url = buildClaimUrl(
window.location.origin,
escrowId,
keys.privateKey,
"Thanks for lunch!"
);Saved history#
src/lib/storage.ts keeps an array of PaymentLinkRecord in localStorage, under floe_links_v2:<network>:<package ID>. Each deployment therefore has its own list.
| Export | Returns | What it does |
|---|---|---|
| getStoredLinks() | PaymentLinkRecord[] | Reads the list, or an empty one |
| saveLink(record) | void | Adds a record. Throws if storage is unavailable. |
| updateLink(digest, changes) | void | Changes one record |
| removeLink(digest) | void | Deletes one record |
| clearObsoleteLinks() | void | Deletes the first version’s data, which held keys |
PaymentLinkRecord#
| Field | Type | Description |
|---|---|---|
| digest | string | Create transaction digest. Identifies the record. |
| escrowId | string | null | Null until the payment is confirmed |
| sender | string | The account that paid |
| currency | USD | SUI | LOFI | What the sender typed in |
| enteredAmount | number | The amount they typed |
| token | SUI | LOFI | What the escrow holds |
| amountUnits | string | The held amount, in base units |
| recipientNote | string | The note |
| themeId | CardThemeId | The envelope |
| hasWaterDonation | boolean | Whether the roundup was on |
| hasGift | boolean? | Whether it had a gift. Not what or where. |
| createdAt | number | Milliseconds since 1970 |
| expiresAt | number | Milliseconds since 1970 |
| status | EscrowStatus | creating, pending, claimed, expired or reclaimed |
| claimedBy | string? | The recipient’s address |
| closedAt | number? | When it was claimed or refunded |
React hooks#
| Export | Returns | What it does |
|---|---|---|
| useLinks() | { links, status, refresh } | Saved links, updated from Sui |
| usePrices(enabled?) | { rates, status } | Live prices while something shows them |
rates is null until prices are known, and becomes null again when they go stale. See How fresh they are.