35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.toBech32 = toBech32;
|
|
exports.fromBech32 = fromBech32;
|
|
exports.normalizeBech32 = normalizeBech32;
|
|
const base_1 = require("@scure/base");
|
|
function toBech32(prefix, data, limit) {
|
|
const address = base_1.bech32.encode(prefix, base_1.bech32.toWords(data), limit);
|
|
return address;
|
|
}
|
|
function hasBech32Separator(input) {
|
|
return input.indexOf("1") !== -1;
|
|
}
|
|
function fromBech32(address, limit = Infinity) {
|
|
// This extra check can be removed once
|
|
// https://github.com/paulmillr/scure-base/pull/45 is merged and published.
|
|
if (!hasBech32Separator(address))
|
|
throw new Error(`No bech32 separator found`);
|
|
const decodedAddress = base_1.bech32.decode(address, limit);
|
|
return {
|
|
prefix: decodedAddress.prefix,
|
|
data: new Uint8Array(base_1.bech32.fromWords(decodedAddress.words)),
|
|
};
|
|
}
|
|
/**
|
|
* Takes a bech32 address and returns a normalized (i.e. lower case) representation of it.
|
|
*
|
|
* The input is validated along the way, which makes this significantly safer than
|
|
* using `address.toLowerCase()`.
|
|
*/
|
|
function normalizeBech32(address) {
|
|
const { prefix, data } = fromBech32(address);
|
|
return toBech32(prefix, data);
|
|
}
|
|
//# sourceMappingURL=bech32.js.map
|