ox
Version:
Ethereum Standard Library
137 lines • 3.58 kB
JavaScript
import * as Bytes from './Bytes.js';
import * as Errors from './Errors.js';
import * as Hex from './Hex.js';
import * as internal from './internal/base58.js';
/**
* Encodes a {@link ox#Bytes.Bytes} to a Base58-encoded string.
*
* @example
* ```ts twoslash
* import { Base58, Bytes } from 'ox'
*
* const value = Base58.fromBytes(
* Bytes.fromString('Hello World!')
* )
* // @log: '2NEpo7TZRRrLZSi2U'
* ```
*
* @param value - The byte array to encode.
* @returns The Base58 encoded string.
*/
export function fromBytes(value) {
return internal.from(value);
}
/**
* Encodes a {@link ox#Hex.Hex} to a Base58-encoded string.
*
* @example
* ```ts twoslash
* import { Base58, Hex } from 'ox'
*
* const value = Base58.fromHex(Hex.fromString('Hello World!'))
* // @log: '2NEpo7TZRRrLZSi2U'
* ```
*
* @param value - The byte array to encode.
* @returns The Base58 encoded string.
*/
export function fromHex(value) {
return internal.from(value);
}
/**
* Encodes a string to a Base58-encoded string.
*
* @example
* ```ts twoslash
* import { Base58 } from 'ox'
*
* const value = Base58.fromString('Hello World!')
* // @log: '2NEpo7TZRRrLZSi2U'
* ```
*
* @param value - The string to encode.
* @returns The Base58 encoded string.
*/
export function fromString(value) {
return internal.from(Bytes.fromString(value));
}
/**
* Decodes a Base58-encoded string to a {@link ox#Bytes.Bytes}.
*
* @example
* ```ts twoslash
* import { Base58 } from 'ox'
*
* const value = Base58.toBytes('2NEpo7TZRRrLZSi2U')
* // @log: Uint8Array [ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]
* ```
*
* @param value - The Base58 encoded string.
* @returns The decoded byte array.
*/
export function toBytes(value) {
return Bytes.fromHex(toHex(value));
}
/**
* Decodes a Base58-encoded string to {@link ox#Hex.Hex}.
*
* @example
* ```ts twoslash
* import { Base58 } from 'ox'
*
* const value = Base58.toHex('2NEpo7TZRRrLZSi2U')
* // @log: '0x48656c6c6f20576f726c6421'
* ```
*
* @param value - The Base58 encoded string.
* @returns The decoded hex string.
*/
export function toHex(value) {
if (value.length === 0)
return '0x';
let integer = BigInt(0);
let pad = 0;
let checkPad = true;
for (let i = 0; i < value.length; i++) {
const char = value[i];
// check for leading 1s
if (checkPad && char === '1')
pad++;
else
checkPad = false;
// check for invalid characters
if (typeof internal.alphabetToInteger[char] !== 'bigint')
throw new InvalidCharacterError({ character: char });
integer = integer * 58n;
integer = integer + internal.alphabetToInteger[char];
}
let body = integer === 0n ? '' : integer.toString(16);
if ((body.length & 1) !== 0)
body = `0${body}`;
return `0x${'00'.repeat(pad)}${body}`;
}
/**
* Decodes a Base58-encoded string to a string.
*
* @example
* ```ts twoslash
* import { Base58 } from 'ox'
*
* const value = Base58.toString('2NEpo7TZRRrLZSi2U')
* // @log: 'Hello World!'
* ```
*
* @param value - The Base58 encoded string.
* @returns The decoded string.
*/
export function toString(value) {
return Hex.toString(toHex(value));
}
/** Thrown when a Base58 string contains an invalid character. */
export class InvalidCharacterError extends Errors.BaseError {
name = 'Base58.InvalidCharacterError';
constructor({ character }) {
super(`Invalid Base58 character: "${character}".`);
}
}
//# sourceMappingURL=Base58.js.map