From d4cafb0cb736ed20315070cf20d9ab4a3a8309d6 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 29 Sep 2025 09:50:51 -0700 Subject: [PATCH] Add type guard utils to wallet utils --- packages/util/src/Blossom.ts | 94 +++++++++++++++++++++++++----------- packages/util/src/Wallet.ts | 27 +++++++---- 2 files changed, 83 insertions(+), 38 deletions(-) diff --git a/packages/util/src/Blossom.ts b/packages/util/src/Blossom.ts index 697cf00..a730b65 100644 --- a/packages/util/src/Blossom.ts +++ b/packages/util/src/Blossom.ts @@ -57,15 +57,18 @@ export const buildBlobUrl = (server: string, sha256: string, extension?: string) export const checkBlobExists = async ( server: string, sha256: string, - options: { + { + headers = {}, + authEvent, + }: { + headers?: Record authEvent?: SignedEvent } = {}, ): Promise<{exists: boolean; size?: number}> => { const url = buildBlobUrl(server, sha256) - const headers: Record = {} - if (options.authEvent) { - headers.Authorization = makeHttpAuthHeader(options.authEvent) + if (authEvent) { + headers.Authorization = makeHttpAuthHeader(authEvent) } try { @@ -88,20 +91,24 @@ export const checkBlobExists = async ( export const getBlob = async ( server: string, sha256: string, - options: { + { + headers = {}, + authEvent, + range, + }: { + headers?: Record authEvent?: SignedEvent range?: {start: number; end?: number} } = {}, ) => { const url = buildBlobUrl(server, sha256) - const headers: Record = {} - if (options.authEvent) { - headers.Authorization = makeHttpAuthHeader(options.authEvent) + if (authEvent) { + headers.Authorization = makeHttpAuthHeader(authEvent) } - if (options.range) { - const {end, start} = options.range + if (range) { + const {end, start} = range headers.Range = end !== undefined ? `bytes=${start}-${end}` : `bytes=${start}-` } @@ -109,20 +116,43 @@ export const getBlob = async ( return fetch(url, {headers}) } +export const canUploadBlob = async ( + server: string, + { + headers = {}, + authEvent, + }: { + headers?: Record + authEvent?: SignedEvent + } = {}, +) => { + const url = new URL(server) + const uploadUrl = `${url.origin}/upload` + + if (authEvent) { + headers.Authorization = makeHttpAuthHeader(authEvent) + } + + return fetch(uploadUrl, {method: "HEAD", headers}) +} + export const uploadBlob = async ( server: string, blob: Blob | ArrayBuffer, - options: { + { + headers = {}, + authEvent, + }: { + headers?: Record authEvent?: SignedEvent } = {}, ) => { const url = new URL(server) const uploadUrl = `${url.origin}/upload` const body = blob instanceof Blob ? blob : new Blob([blob]) - const headers: Record = {} - if (options.authEvent) { - headers.Authorization = makeHttpAuthHeader(options.authEvent) + if (authEvent) { + headers.Authorization = makeHttpAuthHeader(authEvent) } return fetch(uploadUrl, {method: "PUT", headers, body}) @@ -131,16 +161,18 @@ export const uploadBlob = async ( export const deleteBlob = async ( server: string, sha256: string, - options: { + { + headers = {}, + authEvent, + }: { + headers?: Record authEvent?: SignedEvent } = {}, ) => { const url = buildBlobUrl(server, sha256) - const headers: Record = {} - - if (options.authEvent) { - headers.Authorization = makeHttpAuthHeader(options.authEvent) + if (authEvent) { + headers.Authorization = makeHttpAuthHeader(authEvent) } return fetch(url, {method: "DELETE", headers}) @@ -149,7 +181,13 @@ export const deleteBlob = async ( export const listBlobs = async ( server: string, pubkey: string, - options: { + { + headers = {}, + authEvent, + since, + until, + }: { + headers?: Record authEvent?: SignedEvent since?: number until?: number @@ -159,19 +197,19 @@ export const listBlobs = async ( const listUrl = `${url.origin}/list/${pubkey}` const searchParams = new URLSearchParams() - if (options.since !== undefined) { - searchParams.append("since", options.since.toString()) + + if (since !== undefined) { + searchParams.append("since", since.toString()) } - if (options.until !== undefined) { - searchParams.append("until", options.until.toString()) + + if (until !== undefined) { + searchParams.append("until", until.toString()) } const fullUrl = searchParams.toString() ? `${listUrl}?${searchParams.toString()}` : listUrl - const headers: Record = {} - - if (options.authEvent) { - headers.Authorization = makeHttpAuthHeader(options.authEvent) + if (authEvent) { + headers.Authorization = makeHttpAuthHeader(authEvent) } return fetch(fullUrl, {headers}) diff --git a/packages/util/src/Wallet.ts b/packages/util/src/Wallet.ts index 9208cbe..efe9649 100644 --- a/packages/util/src/Wallet.ts +++ b/packages/util/src/Wallet.ts @@ -16,16 +16,23 @@ export type NWCInfo = { } export enum WalletType { - WebLn = "webln", + WebLN = "webln", NWC = "nwc", } -export type Wallet = - | { - type: WalletType.WebLn - info: WebLNInfo - } - | { - type: WalletType.NWC - info: NWCInfo - } +export type WebLNWallet = { + type: WalletType.WebLN + info: WebLNInfo +} + +export type NWCWallet = { + type: WalletType.NWC + info: NWCInfo +} + +export type Wallet = WebLNWallet | NWCWallet + +export const isWebLNWallet = (wallet: Wallet): wallet is WebLNWallet => + wallet.type === WalletType.WebLN + +export const isNWCWallet = (wallet: Wallet): wallet is NWCWallet => wallet.type === WalletType.NWC