Skip to content

Module BSV::Storage::Utils

Helpers for encoding and decoding UHRP (Unified Hash Resource Protocol) URLs.

A UHRP URL is a Base58Check string with a two-byte xCEx00 prefix prepended to the SHA-256 hash of the content. This makes each URL self-verifying and stable across storage providers.

URL normalisation

normalize_url strips the uhrp: scheme (case-insensitive) and the optional // authority prefix, matching the TS SDK contract exactly. The web+uhrp:// variant used by some browser extension manifests is not normalised here — that diverges from the TS reference (Python normalises it; we follow TS).

Constants

UHRP_PREFIX

Two-byte UHRP version prefix: 0xCE 0x00.

Public Class Methods

get_hash_from_url(url)

Decode a UHRP URL and return the 32-byte binary SHA-256 hash.

Accepts URLs with or without the uhrp: scheme and optional // authority. - @param url [String] UHRP URL (with or without uhrp: prefix) - @raise [ArgumentError] if the URL is not a String, empty, has a bad prefix, or has wrong length - @raise [BSV::Primitives::Base58::ChecksumError] if the Base58Check checksum fails - @return [String] 32-byte binary SHA-256 hash

get_url_for_file(data)

Compute the SHA-256 hash of data and encode it as a UHRP URL. - @param data [String] binary file content - @return [String] Base58Check-encoded UHRP URL

get_url_for_hash(hash)

Encode a 32-byte binary SHA-256 hash as a UHRP URL. - @param hash [String] 32-byte binary string (SHA-256 hash) - @raise [ArgumentError] if hash is not exactly 32 bytes - @return [String] Base58Check-encoded UHRP URL

normalize_url(url)

Strip the uhrp: scheme (case-insensitive) and optional // from a URL.

Follows the TS SDK contract: only uhrp: and uhrp:// are normalised. web+uhrp:// is deliberately not stripped (TS does not strip it; Python does). - @param url [String] URL to normalise - @return [String] normalised URL

valid_url?(url)

Return true if url is a syntactically valid UHRP URL. - @param url [String] URL to validate - @return [Boolean]