snstr
Version:
Secure Nostr Software Toolkit for Renegades - A comprehensive TypeScript library for Nostr protocol implementation
51 lines (50 loc) • 1.72 kB
JavaScript
/**
* NIP-21: `nostr:` URI scheme
*
* Provides helpers to create and parse `nostr:` URIs which
* wrap NIP-19 bech32 identifiers.
*
* @see https://github.com/nostr-protocol/nips/blob/master/21.md
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NOSTR_URI_PREFIX = void 0;
exports.encodeNostrURI = encodeNostrURI;
exports.decodeNostrURI = decodeNostrURI;
const nip19_1 = require("../nip19");
/** Prefix for nostr URIs */
exports.NOSTR_URI_PREFIX = "nostr:";
/**
* Create a `nostr:` URI from a NIP-19 identifier.
*
* @param entity - Bech32 encoded entity (npub, note, nprofile, nevent, naddr)
* @returns URI string in the form `nostr:<entity>`
* @throws Error if the entity is an nsec or invalid string
*/
function encodeNostrURI(entity) {
if (!entity || typeof entity !== "string") {
throw new Error("Invalid NIP-19 entity");
}
if (entity.startsWith(nip19_1.Prefix.PrivateKey)) {
throw new Error("nsec entities are not allowed in nostr URIs");
}
// Basic prefix check to ensure it looks like bech32
if (!entity.includes("1")) {
throw new Error("Invalid bech32 format for NIP-19 entity");
}
return `${exports.NOSTR_URI_PREFIX}${entity}`;
}
/**
* Parse a `nostr:` URI and decode the underlying NIP-19 entity.
*
* @param uri - URI string starting with `nostr:`
* @returns Decoded NIP-19 entity
* @throws Error if the URI is malformed or contains an invalid entity
*/
function decodeNostrURI(uri) {
if (!uri.startsWith(exports.NOSTR_URI_PREFIX)) {
throw new Error("Invalid nostr URI format");
}
const entity = uri.slice(exports.NOSTR_URI_PREFIX.length);
return (0, nip19_1.decode)(entity);
}
;