import { base58 } from '@scure/base';
import { blake2b } from '@noble/hashes/blake2.js';

const SS58_PREFIX = new TextEncoder().encode("SS58PRE");
const CHECKSUM_LENGTH = 2;
const getSs58AddressInfo = (address) => {
  try {
    const decoded = base58.decode(address);
    const prefixBytes = decoded.subarray(0, decoded[0] & 64 ? 2 : 1);
    const publicKey = decoded.subarray(
      prefixBytes.length,
      decoded.length - CHECKSUM_LENGTH
    );
    const checksum = decoded.subarray(prefixBytes.length + publicKey.length);
    const expectedChecksum = blake2b(
      Uint8Array.of(...SS58_PREFIX, ...prefixBytes, ...publicKey),
      {
        dkLen: 64
      }
    ).subarray(0, CHECKSUM_LENGTH);
    const isChecksumValid = checksum[0] === expectedChecksum[0] && checksum[1] === expectedChecksum[1];
    if (!isChecksumValid) return { isValid: false };
    return {
      isValid: true,
      ss58Format: prefixBytesToNumber(prefixBytes),
      publicKey: publicKey.slice()
    };
  } catch (_) {
    return { isValid: false };
  }
};
const prefixBytesToNumber = (bytes) => {
  const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
  return dv.byteLength === 1 ? dv.getUint8(0) : dv.getUint16(0);
};
const withSs58Cache = (fn) => {
  let cache = {};
  let activityCount = 0;
  let latestCount = 0;
  const checkActivity = () => {
    if (activityCount === latestCount) {
      cache = {};
      activityCount = latestCount = 0;
    } else {
      latestCount = activityCount;
      setTimeout(checkActivity, 0);
    }
  };
  return (publicKey) => {
    var _a, _b;
    if (++activityCount === 1) checkActivity();
    let entry = cache;
    const lastIdx = publicKey.length - 1;
    for (let i = 0; i <= lastIdx; i++) entry = entry[_a = publicKey[i]] || (entry[_a] = {});
    return entry[_b = publicKey[lastIdx]] || (entry[_b] = fn(publicKey));
  };
};
const fromBufferToBase58 = (ss58Format) => {
  const prefixBytes = ss58Format < 64 ? Uint8Array.of(ss58Format) : Uint8Array.of(
    (ss58Format & 252) >> 2 | 64,
    ss58Format >> 8 | (ss58Format & 3) << 6
  );
  return withSs58Cache((publicKey) => {
    const checksum = blake2b(
      Uint8Array.of(...SS58_PREFIX, ...prefixBytes, ...publicKey),
      {
        dkLen: 64
      }
    ).subarray(0, CHECKSUM_LENGTH);
    return base58.encode(
      Uint8Array.of(...prefixBytes, ...publicKey, ...checksum)
    );
  });
};

export { fromBufferToBase58, getSs58AddressInfo };
//# sourceMappingURL=ss58-util.mjs.map
