Sealed rooms
Sealed-bid encoding, commitments, consent digests, Seal encryption and bid opening. Exported whole from loan-room.ts, room-open.ts and sealed.ts, plus three names from seal.ts.
Signatures below are copied from the source; descriptions are the source's own doc comments.
From loan-room.ts#
Exported by index.ts.
OFFER_PLAINTEXT_LEN#
export const OFFER_PLAINTEXT_LEN = 104;OFFER_CT_LEN#
The v5 overhead was measured on a 36-byte identity; room ‖ "offers" is 38, and Seal carries the identity in the clear.
export const OFFER_CT_LEN = OFFER_PLAINTEXT_LEN + SEAL_OVERHEAD_T2 + 2;SealedOpening#
export type SealedOpening = { rateBps: bigint; salt: Uint8Array; treasuryId: string; capId: string };offerPlaintext#
export function offerPlaintext({ rateBps, salt, treasuryId, capId }: SealedOpening): Uint8ArrayparseOfferPlaintext#
export function parseOfferPlaintext(bytes: Uint8Array): SealedOpeningofferCommitment#
loan_room::commitment_of: blake2b256(plaintext ‖ bcs(lender)).
export function offerCommitment(o: SealedOpening, lender: string): Uint8ArrayconsentDigest#
loan_room::consent_of: blake2b256(bcs(room) ‖ bcs(rate) ‖ salt).
export function consentDigest(roomId: string, rateBps: bigint, salt: Uint8Array): Uint8Arrayconsented#
take_consent's own test (treasury.move:416): the opening's cap holds the consent in the opening's treasury.
export async function consented(client: SuiGrpcClient, roomId: string, o: SealedOpening): Promise<boolean>offersIdentity#
export function offersIdentity(roomId: string): stringOFFER_THRESHOLD#
export const OFFER_THRESHOLD = 2;encryptOffer#
export async function encryptOffer(seal: SealClient, pkg: string, roomId: string, o: SealedOpening)offerApproveTxBytes#
export function offerApproveTxBytes(client: SuiGrpcClient, pkg: string, loanCoin: string, roomId: string)OpenFault#
export type OpenFault = "NO_ACCESS" | "BAD_SIGNATURE" | "KEY_SERVERS";SealLike#
export type SealLike = Pick<SealClient, "fetchKeys" | "decrypt">;Opened#
export type Opened =
{ ok: true; openings: (SealedOpening | null)[] } | { ok: false; fault: OpenFault; error: string };openOffers#
One fetchKeys for the room's identity, then a local decrypt per ciphertext; null marks a ciphertext that does not open.
export async function openOffers(
seal: SealLike,
a: { roomId: string; session: SessionKey; txBytes: Uint8Array; ciphertexts: Uint8Array[] },
): Promise<Opened>From room-open.ts#
Exported by index.ts.
RoomForOpening#
GET /v1/borrow/rooms/:id, as the browser receives it.
export type RoomForOpening = {
nowMs: number;
room: { id: string; closeMs: number; settleByMs: number; maxRateBps: number; amount: string };
offers: { index: number; lender: string; commitment: string; ciphertext: string }[];
seal: { pkg: string; loanCoin: string };
};RankedOpening#
export type RankedOpening = {
index: number;
lender: string;
rateBps: number;
salt: string;
treasuryId: string;
capId: string;
};OpenState#
export type OpenState =
"NOT_CLOSED" | "SETTLE_WINDOW_CLOSED" | "NO_ACCESS" | "BAD_SIGNATURE" | "KEY_SERVERS_DOWN";OpenResult#
export type OpenResult =
| { ok: true; ranked: RankedOpening[]; unopened: number; attempts: number }
| { ok: false; state: OpenState; error: string; attempts: number };OpenDeps#
export type OpenDeps = {
client: SuiGrpcClient;
seal: SealLike;
signer: Signer;
now?: () => number;
sleep?: (ms: number) => Promise<void>;
maxAttempts?: number;
baseDelayMs?: number;
/** The seal_approve PTB the key servers dry-run; built against the fullnode unless given. */
approve?: () => Promise<Uint8Array>;
/** Whether the opening's lender consented on chain; reads the treasury's consents Table unless given. */
consented?: (o: SealedOpening) => Promise<boolean>;
};openRoomBids#
Runs where the borrower's key is: the browser with their passkey (one prompt signs the Seal session), or an agent process with its own key. Only openings matching an on-chain commitment and backed by their lender's on-chain consent come back, cheapest first.
export async function openRoomBids(deps: OpenDeps, r: RoomForOpening): Promise<OpenResult>From sealed.ts#
Exported by index.ts.
RoomView#
export type RoomView = {
id: string;
amount: bigint;
maxRateBps: number;
termMs: number;
closeMs: number;
collateral: bigint;
};Opening#
export type Opening = { rateBps: bigint; salt: Uint8Array; treasuryId: string; capId: string };SealedOnChain#
export type SealedOnChain = { index: number; lender: string; commitment: string };bidTerms#
Whether this lender's sealed bid can fill the room under its own mandate, as treasury::draw will check at settle.
export function bidTerms(
p: LenderPolicy,
room: RoomView,
roomValue: bigint,
outstanding: bigint,
nowMs: number,
): { ok: true; rateBps: number; ltvBps: number } | { ok: false; reason: string }rankOpenings#
Openings the borrower can prove against the room's commitments and its lender's on-chain consent (anyone can seal and commit to any plaintext), cheapest first, ties in placement order.
export function rankOpenings(
onChain: SealedOnChain[],
openings: Opening[],
commit: (o: Opening, lender: string) => string,
maxRateBps: number,
consented: (o: Opening) => boolean,
): { index: number; opening: Opening }[]From seal.ts#
Exported by index.ts.
TESTNET_KEY_SERVERS#
export const TESTNET_KEY_SERVERS = [
"0x73d05d62c18d9374e3ea529e8e0ed6161da1a141a94d3f76ae3fe4e99356db75",
"0xf5d14a81a982144ae441cd7d64b09027f116a468bd36e7eca494f750591623c8",
] as const;SEAL_OVERHEAD_T2#
export const SEAL_OVERHEAD_T2 = 352;sealClient#
Mysten's two testnet key servers, threshold 2 (loan-room.ts OFFER_THRESHOLD). Localnet has no key servers.
export function sealClient(client: SuiGrpcClient, extra: Pick<SealClientOptions, "fetch" | "timeout"> = {})