Skip to content

identifierToUri

identifierToUri(identifier): string

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

Parameters

identifier: string

The identifier string to be transformed into a URI.

Returns

string

A string representing the URI constructed from the identifier.

Remarks

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.

Example

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

Throws

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.

Description

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.

Source

identifier/src/Identifier.ts:497