import { base58btc } from 'multiformats/bases/base58';
import varint from 'varint';
import { ensureString } from '../types/string.js';
import { normalizeToU8a } from '../types/bytes.js';
import { catchFnErrorWith } from '../error.js';
import 'crypto';
import '../interfaces.js';
import '../assert.js';
import '../types/iterable.js';
import '../types/object.js';
import '../inheritance.js';
import '../types/set.js';

/**
 * Encodes supplied bytes as base58btc string using given prefix.
 * @param {Uint8Array} value
 * @param {Uint8Array} prefix
 * @returns {string}
 */
const encodeAsBase58btc = (prefix, value) => {
  const prefixBytes = normalizeToU8a(prefix);
  const bytes = normalizeToU8a(value);

  // Use the supplied prefix
  const multibase = new Uint8Array(prefixBytes.length + bytes.length);

  // Add multibase prefix and concatenate with bytes
  multibase.set(prefixBytes);
  multibase.set(bytes, prefixBytes.length);

  // Return the encoded base58btc multibase string
  return base58btc.encode(multibase);
};

/**
 * Decodes bytes of the supplied base58btc string.
 * @param {string} string
 * @returns {Uint8Array}
 */
const decodeFromBase58btc = catchFnErrorWith(
  'Invalid base58btc string',
  (string) => {
    // Decode base58btc multibase string
    const decoded = base58btc.decode(ensureString(string));
    varint.decode(decoded); // Decode to get byte length
    return decoded.slice(varint.decode.bytes);
  },
);

export { decodeFromBase58btc, encodeAsBase58btc };
//# sourceMappingURL=base-58-btc.js.map
