ts-sdk

API

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables

Interfaces

 
DownloadResult
DownloaderConfig
FindFileData
RenewFileResult
UploadFileResult
UploadableFile
UploaderConfig

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Interface: DownloadResult

export interface DownloadResult {
    data: number[];
    mimeType: string | null;
}

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Interface: DownloaderConfig

export interface DownloaderConfig {
    networkPreset: "mainnet" | "testnet" | "local";
}

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Interface: FindFileData

export interface FindFileData {
    name: string;
    size: string;
    mimeType: string;
    expiryTime: number;
}

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Interface: RenewFileResult

export interface RenewFileResult {
    status: string;
    prevExpiryTime?: number;
    newExpiryTime?: number;
    amount?: number;
}

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Interface: UploadFileResult

export interface UploadFileResult {
    published: boolean;
    uhrpURL: string;
}

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Interface: UploadableFile

export interface UploadableFile {
    data: number[];
    type: string;
}

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Interface: UploaderConfig

export interface UploaderConfig {
    storageURL: string;
    wallet: WalletInterface;
}

See also: WalletInterface

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Classes

 
StorageDownloader
StorageUploader

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Class: StorageDownloader

Locates HTTP URLs where content can be downloaded. It uses the passed or the default one.

export class StorageDownloader {
    constructor(config?: DownloaderConfig) 
    public async resolve(uhrpUrl: string): Promise<string[]> 
    public async download(uhrpUrl: string): Promise<DownloadResult> 
}

See also: DownloadResult, DownloaderConfig

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Class: StorageUploader

The StorageUploader class provides client-side methods for:

export class StorageUploader {
    constructor(config: UploaderConfig) 
    public async publishFile(params: {
        file: UploadableFile;
        retentionPeriod: number;
    }): Promise<UploadFileResult> 
    public async findFile(uhrpUrl: string): Promise<FindFileData> 
    public async listUploads(): Promise<any> 
    public async renewFile(uhrpUrl: string, additionalMinutes: number): Promise<RenewFileResult> 
}

See also: FindFileData, RenewFileResult, UploadFileResult, UploadableFile, UploaderConfig

Constructor

Creates a new StorageUploader instance.

constructor(config: UploaderConfig) 

See also: UploaderConfig

Argument Details

Method findFile

Retrieves metadata for a file matching the given UHRP URL from the /find route.

public async findFile(uhrpUrl: string): Promise<FindFileData> 

See also: FindFileData

Returns

An object with file name, size, MIME type, and expiry time

Argument Details

Throws

If the server or the route returns an error

Method listUploads

Lists all advertisements belonging to the user from the /list route.

public async listUploads(): Promise<any> 

Returns

The array of uploads returned by the server

Throws

If the server or the route returns an error

Method publishFile

Publishes a file to the storage server with the specified retention period.

This will:

  1. Request an upload URL from the server.
  2. Perform an HTTP PUT to upload the file’s raw bytes.
  3. Return a UHRP URL referencing the file once published.
public async publishFile(params: {
    file: UploadableFile;
    retentionPeriod: number;
}): Promise<UploadFileResult> 

See also: UploadFileResult, UploadableFile

Returns

An object with the file’s UHRP URL

Throws

If the server or upload step returns a non-OK response

Method renewFile

Renews the hosting time for an existing file advertisement identified by uhrpUrl. Calls the /renew route to add additionalMinutes to the GCS customTime and re-mint the advertisement token on-chain.

public async renewFile(uhrpUrl: string, additionalMinutes: number): Promise<RenewFileResult> 

See also: RenewFileResult

Returns

An object with the new and previous expiry times, plus any cost

Argument Details

Throws

If the request fails or the server returns an error

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Functions

Types

Enums

Variables

 
getHashFromURL
getURLForFile
getURLForHash
isValidURL
normalizeURL

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Variable: getHashFromURL

getHashFromURL = (URL: string): number[] => {
    URL = normalizeURL(URL);
    const { data, prefix } = fromBase58Check(URL, undefined, 2);
    if (data.length !== 32) {
        throw new Error("Invalid length!");
    }
    if (toHex(prefix as number[]) !== "ce00") {
        throw new Error("Bad prefix");
    }
    return data as number[];
}

See also: fromBase58Check, normalizeURL, toHex

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Variable: getURLForFile

getURLForFile = (file: number[]): string => {
    const hash = sha256(file);
    return getURLForHash(hash);
}

See also: getURLForHash, sha256

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Variable: getURLForHash

getURLForHash = (hash: number[]): string => {
    if (hash.length !== 32) {
        throw new Error("Hash length must be 32 bytes (sha256)");
    }
    return toBase58Check(hash, toArray("ce00", "hex"));
}

See also: toArray, toBase58Check

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Variable: isValidURL

isValidURL = (URL: string): boolean => {
    try {
        getHashFromURL(URL);
        return true;
    }
    catch (e) {
        return false;
    }
}

See also: getHashFromURL

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables


Variable: normalizeURL

normalizeURL = (URL: string): string => {
    if (URL.toLowerCase().startsWith("uhrp:"))
        URL = URL.slice(5);
    if (URL.startsWith("//"))
        URL = URL.slice(2);
    return URL;
}

Links: API, Interfaces, Classes, Functions, Types, Enums, Variables