// Borrou Partner API: collateral-address verification for Node 20+. v0.1 DRAFT. // // Every deposit and top-up address comes with `address_attestation`: Borrou's Ed25519 signature // over a short statement of the address. Verify it before your app shows the address, and show the // verification code beside it; the borrower's loan page shows the same code with the same address. // // 1. Find the key: GET /v1/address-keys lists the public keys by `key_id` (no credential needed). // Cache the list; fetch it again when an attestation names a key id you do not hold. // 2. Verify the signature over the statement's UTF-8 bytes exactly as received. // 3. Read the statement: eight `name=value` lines in a fixed order, LF-separated, no trailing // newline, for purpose BORROU_COLLATERAL_ADDRESS_V1, asset BTC, network bitcoin, and the same // key id as the attestation. // 4. Check its address and loan reference against what you are about to show. // 5. Work out the verification code from the signature and check it is the one you were given. // // Anything else: show no address, and contact TFG. import { createHash, createPublicKey, verify, type KeyObject } from "node:crypto"; export interface AddressKey { key_id: string; /** The 32 raw public-key bytes in base64url (a JWK's `x`). */ public_key: string; public_key_pem: string; } export interface AddressAttestation { algorithm: "Ed25519"; key_id: string; signed_at: string; statement: string; signature: string; verification_code: string; } export type AddressCheck = | { ok: true; verificationCode: string; signedAt: string } | { ok: false; reason: "unknown_key" | "bad_signature" | "malformed_statement" | "address_mismatch" | "loan_mismatch" | "code_mismatch" }; const FIELDS = ["purpose", "asset", "network", "address", "loan_reference", "wallet", "key_id", "signed_at"] as const; const BASE32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; /** The statement's fields, or null for anything Borrou would not sign. */ export function readStatement(statement: string): Record<(typeof FIELDS)[number], string> | null { const lines = statement.split("\n"); if (lines.length !== FIELDS.length) return null; const fields = {} as Record<(typeof FIELDS)[number], string>; for (const [i, line] of lines.entries()) { const eq = line.indexOf("="); if (eq < 0 || line.slice(0, eq) !== FIELDS[i]) return null; const value = line.slice(eq + 1); // Every value is printable ASCII with no spaces: a CR, a space or anything else is not Borrou's. if (!/^[\x21-\x7e]+$/.test(value)) return null; fields[FIELDS[i]!] = value; } if (fields.purpose !== "BORROU_COLLATERAL_ADDRESS_V1" || fields.asset !== "BTC" || fields.network !== "bitcoin") return null; return fields; } /** The code the borrower compares: the first eight base32 characters of SHA-256(signature bytes), as XXXX-XXXX. */ export function verificationCode(signature: string): string { const digest = createHash("sha256").update(Buffer.from(signature, "base64url")).digest(); let bits = 0; let buffer = 0; let code = ""; for (const byte of digest) { buffer = (buffer << 8) | byte; bits += 8; while (bits >= 5 && code.length < 8) { code += BASE32[(buffer >>> (bits - 5)) & 31]; bits -= 5; } buffer &= (1 << bits) - 1; if (code.length === 8) break; } return `${code.slice(0, 4)}-${code.slice(4)}`; } function publicKeyOf(key: AddressKey): KeyObject { return createPublicKey({ key: { kty: "OKP", crv: "Ed25519", x: key.public_key }, format: "jwk" }); } export function verifyAddressAttestation( shown: { address: string; loan_reference: string }, attestation: AddressAttestation, keys: readonly AddressKey[], ): AddressCheck { const key = keys.find((k) => k.key_id === attestation.key_id); if (!key) return { ok: false, reason: "unknown_key" }; const signature = Buffer.from(attestation.signature, "base64url"); if (signature.length !== 64 || !verify(null, Buffer.from(attestation.statement, "utf8"), publicKeyOf(key), signature)) { return { ok: false, reason: "bad_signature" }; } const fields = readStatement(attestation.statement); if (!fields || fields.key_id !== attestation.key_id) return { ok: false, reason: "malformed_statement" }; if (fields.address !== shown.address) return { ok: false, reason: "address_mismatch" }; if (fields.loan_reference !== shown.loan_reference) return { ok: false, reason: "loan_mismatch" }; const code = verificationCode(attestation.signature); if (code !== attestation.verification_code) return { ok: false, reason: "code_mismatch" }; return { ok: true, verificationCode: code, signedAt: fields.signed_at }; } // With the API (borrouClient.ts): // // const keys = (await borrou.get<{ keys: AddressKey[] }>("/v1/address-keys")).body.keys; // const deposit = (await borrou.get(`/v1/loans/${reference}/collateral/deposit-address`)).body; // if (!deposit.address_attestation) throw new Error("Unsigned address: sandbox only; never show it as verified."); // const check = verifyAddressAttestation({ address: deposit.address, loan_reference: reference }, deposit.address_attestation, keys); // if (!check.ok) throw new Error(`Do not show this address (${check.reason}); contact TFG.`); // show(deposit.address, check.verificationCode);