Skip to content

Blossom ​

Client functions for Blossom media servers: auth events, blob operations, and file encryption.

Types ​

typescript
export type BlossomAuthAction = "get" | "upload" | "list" | "delete"

export type BlossomAuthEventOpts = {
  action: BlossomAuthAction
  server: string
  hashes?: string[]
  expiration?: number
  content?: string
}

export type BlossomServer = {
  url: string
  pubkey?: string
}

export type BlossomErrorResponse = {
  message: string
  reason?: string
}

export interface EncryptedFile {
  key: string
  nonce: string
  ciphertext: Uint8Array
  algorithm: string
}

Authentication ​

typescript
// Creates a Blossom auth event for server operations
export declare const makeBlossomAuthEvent: (opts: BlossomAuthEventOpts) => StampedEvent

Blob Operations ​

typescript
// Builds URL for accessing a blob
export declare const buildBlobUrl: (server: string, sha256: string, extension?: string) => string

// Checks if a blob exists on server
export declare const checkBlobExists: (server: string, sha256: string, options?: { headers?: Record<string, string>; authEvent?: SignedEvent; signal?: AbortSignal }) => Promise<{exists: boolean; size?: number}>

// Downloads blob from server
export declare const getBlob: (server: string, sha256: string, options?: { headers?: Record<string, string>; authEvent?: SignedEvent; range?: {start: number; end?: number}; signal?: AbortSignal }) => Promise<Response>

// Checks if uploads are allowed (HEAD request to /upload)
export declare const canUploadBlob: (server: string, options?: { headers?: Record<string, string>; authEvent?: SignedEvent; signal?: AbortSignal }) => Promise<Response>

// Uploads blob to server
export declare const uploadBlob: (server: string, blob: Blob | ArrayBuffer, options?: { headers?: Record<string, string>; authEvent?: SignedEvent; signal?: AbortSignal }) => Promise<Response>

// Asks the server to copy a blob from another url (BUD-04 PUT /mirror)
export declare const mirrorBlob: (server: string, blobUrl: string, options?: { headers?: Record<string, string>; authEvent?: SignedEvent; signal?: AbortSignal }) => Promise<Response>

// Deletes blob from server
export declare const deleteBlob: (server: string, sha256: string, options?: { headers?: Record<string, string>; authEvent?: SignedEvent; signal?: AbortSignal }) => Promise<Response>

// Lists blobs for a pubkey
export declare const listBlobs: (server: string, pubkey: string, options?: { headers?: Record<string, string>; authEvent?: SignedEvent; since?: number; until?: number; signal?: AbortSignal }) => Promise<Response>

A signal aborts the request, so a caller can give up on a server that stalls. A mirror is authorized by an upload auth event whose hashes hold the blob's sha256, and the server answers with the same blob descriptor an upload returns.

File Encryption ​

typescript
// Encrypts a file using AES-GCM
export declare function encryptFile(file: Blob): Promise<EncryptedFile>

// Decrypts an encrypted file
export declare function decryptFile(encryptedFile: EncryptedFile): Promise<Uint8Array>

Example ​

typescript
import { uploadBlob, makeBlossomAuthEvent } from '@welshman/util'

// Create auth event for upload
const authEvent = makeBlossomAuthEvent({
  action: "upload",
  server: "https://blossom.example.com"
})

// Sign the auth event with your signer
const signedAuthEvent = await signer.sign(authEvent)

// Upload a file
const file = new File(["Hello world"], "hello.txt", { type: "text/plain" })
const response = await uploadBlob("https://blossom.example.com", file, {
  authEvent: signedAuthEvent
})
typescript
import { mirrorBlob, makeBlossomAuthEvent } from '@welshman/util'

// Copy a blob another server already holds, giving up after ten seconds
const server = "https://blossom.example.com"
const authEvent = await signer.sign(makeBlossomAuthEvent({action: "upload", server, hashes: [sha256]}))
const response = await mirrorBlob(server, `https://other.example.com/${sha256}`, {
  authEvent,
  signal: AbortSignal.timeout(10_000),
})
const descriptor = await response.json()