Skip to content

identifierToUri

identifierToUri(identifier): string

Transforms a given identifier into a URI by appending a relevant prefix, if necessary.

identifier: string

The identifier string to be transformed into a URI.

string

A string representing the URI constructed from the identifier.

This function takes a string identifier, validates it, and converts it into a URI by appending a suitable prefix. It also checks if the identifier already has a valid prefix and, if so, returns it unchanged.

const identifier = 'base58EncodedIdentifier';
try {
const uri = identifierToUri(identifier);
console.log('URI:', uri);
} catch (error) {
console.error('Error:', error);
}

Error if the input is not a non-empty string, if the identifier’s checksum is invalid, if the identifier is unrecognized, or if there is an error during decoding.

The function first checks if the input is a valid non-empty string. If the identifier already starts with a known prefix, it is returned as is. Otherwise, the function decodes the identifier and checks its checksum. If valid, it retrieves the corresponding prefix for the identifier from a mapping. An error is thrown if the checksum is invalid, the prefix is not found, or there’s an issue in decoding. The function then concatenates the prefix and identifier to form the URI and returns it.

identifier/src/Identifier.ts:534