@roochnetwork/rooch-sdk
Version:
106 lines (105 loc) • 3.56 kB
JavaScript
import { bech32m } from "@scure/base";
import { isBytes, str } from "../utils/bytes.js";
import { fromHEX, getHexByteLength, isHex } from "../utils/hex.js";
import { BitcoinAddress } from "./bitcoin.js";
import { ROOCH_ADDRESS_LENGTH, ROOCH_BECH32_PREFIX } from "./address.js";
import { RoochAddress } from "./rooch.js";
function decodeToRoochAddressStr(input) {
if (typeof input === "string") {
if (input === "") {
throw Error("Invalid Address");
}
if (isValidRoochAddress(input)) {
if (input.startsWith("rooch")) {
return new RoochAddress(input).toHexAddress();
}
return input;
}
if (isValidBitcoinAddress(input)) {
return new BitcoinAddress(input).genRoochAddress().toHexAddress();
}
throw Error("Invalid Address");
}
if (isBytes(input)) {
return str("hex", input);
}
return decodeToRoochAddressStr(input.toStr());
}
function decodeToPackageAddressStr(input) {
const packageAddressStr = decodeToRoochAddressStr(input);
if (packageAddressStr.length === ROOCH_ADDRESS_LENGTH * 2) {
return packageAddressStr;
}
if (packageAddressStr.length === ROOCH_ADDRESS_LENGTH * 2 + 2 && packageAddressStr.startsWith("0x")) {
return packageAddressStr.slice(2);
}
throw Error("Invalid Address");
}
function convertToRoochAddressBytes(input) {
if (typeof input === "string") {
const normalizeAddress = normalizeRoochAddress(input);
if (isHex(normalizeAddress) && getHexByteLength(normalizeAddress) === ROOCH_ADDRESS_LENGTH) {
return fromHEX(normalizeAddress);
}
if (input.startsWith(ROOCH_BECH32_PREFIX)) {
const decode = bech32m.decodeToBytes(input);
if (decode.prefix === ROOCH_BECH32_PREFIX && decode.bytes.length === ROOCH_ADDRESS_LENGTH) {
return decode.bytes;
}
}
return new BitcoinAddress(input).genRoochAddress().toBytes();
}
return isBytes(input) ? input : convertToRoochAddressBytes(input.toStr());
}
function isValidBitcoinAddress(input) {
try {
new BitcoinAddress(input);
return true;
} catch (_) {
}
return false;
}
function isValidRoochAddress(input) {
if (typeof input === "string") {
const normalizeAddress = normalizeRoochAddress(input);
if (isHex(normalizeAddress) && getHexByteLength(normalizeAddress) === ROOCH_ADDRESS_LENGTH) {
return true;
}
if (input.startsWith(ROOCH_BECH32_PREFIX)) {
const decode = bech32m.decodeToBytes(input);
return decode.prefix === ROOCH_BECH32_PREFIX && decode.bytes.length === ROOCH_ADDRESS_LENGTH;
}
return false;
}
return isBytes(input) ? input.length === ROOCH_ADDRESS_LENGTH : isValidAddress(input.toStr());
}
function isValidAddress(input) {
if (typeof input === "string") {
if (isValidRoochAddress(input)) {
return true;
}
return isValidBitcoinAddress(input);
}
return isBytes(input) ? input.length === ROOCH_ADDRESS_LENGTH : isValidAddress(input.toStr());
}
function normalizeRoochAddress(input, forceAdd0x = false) {
let address2 = input.toLowerCase();
if (!forceAdd0x && address2.startsWith("0x")) {
address2 = address2.slice(2);
}
return `0x${address2.padStart(ROOCH_ADDRESS_LENGTH * 2, "0")}`;
}
function canonicalRoochAddress(input, forceAdd0x = false) {
return normalizeRoochAddress(input, forceAdd0x);
}
export {
canonicalRoochAddress,
convertToRoochAddressBytes,
decodeToPackageAddressStr,
decodeToRoochAddressStr,
isValidAddress,
isValidBitcoinAddress,
isValidRoochAddress,
normalizeRoochAddress
};
//# sourceMappingURL=util.js.map