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.

ExportReturnsWhat it does
generateEphemeralKeypair()EphemeralKeysMakes a new Ed25519 keypair in memory
buildClaimUrl(baseUrl, escrowId, privateKey, note?, giftId?)stringBuilds 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_LENGTH60The longest note
Link format
<base>/claim?id=<escrow ID>#k=<private key>&n=<note>&g=<gift blob ID>

Escrows and transactions#

From src/lib/chain/escrow.ts.

ExportReturnsWhat it does
readEscrows(ids)Map<string, Escrow | null>Reads open escrows, 50 at a time
readClosed(escrowId)Closed | nullFinds how a deleted escrow ended
findTransaction(digest)result | missing | unknownLooks up any transaction
readCreated(digest)CreateOutcomeFinds what became of a create transaction
createdEscrowId(tx)string | nullGets the escrow ID from a transaction result
buildCreate(input)TransactionBuilds a create_escrow call
buildClaim(escrow, privateKey, recipient)TransactionSigns the claim message and builds a claim_escrow call
buildReclaim(escrow)TransactionBuilds a reclaim_escrow call
claimMessage(escrowId, recipient)Uint8ArrayThe 64 bytes the key signs
toUnits(amount)bigintDecimal to base units, at four decimal places
fromUnits(units)numberBase units to decimal, for display
chainErrorMessage(err, fallback)stringA 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.

ExportReturnsWhat it does
readHoldings(owner)Holding[]Balances of the tokens this network supports
buildWithdrawal(withdrawal)TransactionBuilds a transfer, or sends all SUI
estimateFee(withdrawal)bigintSimulates it with real coins. Throws if it would fail.
addressProblem(text, owner)string | nullWhy an address looks unsafe
parseUnits(text)bigint | nullTyped amount to base units, exactly
formatUnits(units)stringBase 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.

ExportReturnsWhat it does
sealGift(gift, privateKey)Uint8ArrayEncrypts a gift with the link’s key
openGift(blob, privateKey)GiftDecrypts it. Throws if anything is off.
giftMime(bytes)GiftMime | nullWhat the bytes really are
storeBlob(blob, signal?)stringStores bytes and returns the blob ID
readBlob(id)Uint8ArrayReads bytes, up to the size limit
YETI_CARDSYetiCard[]The ready-made cards
loadCard(card)GiftDownloads a ready-made card
GIFTS_ENABLEDbooleanWhether 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#

create.ts
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.

ExportReturnsWhat it does
getStoredLinks()PaymentLinkRecord[]Reads the list, or an empty one
saveLink(record)voidAdds a record. Throws if storage is unavailable.
updateLink(digest, changes)voidChanges one record
removeLink(digest)voidDeletes one record
clearObsoleteLinks()voidDeletes the first version’s data, which held keys

PaymentLinkRecord#

FieldTypeDescription
digeststringCreate transaction digest. Identifies the record.
escrowIdstring | nullNull until the payment is confirmed
senderstringThe account that paid
currencyUSD | SUI | LOFIWhat the sender typed in
enteredAmountnumberThe amount they typed
tokenSUI | LOFIWhat the escrow holds
amountUnitsstringThe held amount, in base units
recipientNotestringThe note
themeIdCardThemeIdThe envelope
hasWaterDonationbooleanWhether the roundup was on
hasGiftboolean?Whether it had a gift. Not what or where.
createdAtnumberMilliseconds since 1970
expiresAtnumberMilliseconds since 1970
statusEscrowStatuscreating, pending, claimed, expired or reclaimed
claimedBystring?The recipient’s address
closedAtnumber?When it was claimed or refunded

React hooks#

ExportReturnsWhat 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.