'use strict';

var base58 = require('multiformats/bases/base58');
var varint = require('varint');
var utils_types_string = require('../types/string.cjs');
var utils_types_bytes = require('../types/bytes.cjs');
var utils_error = require('../error.cjs');
require('crypto');
require('../interfaces.cjs');
require('../assert.cjs');
require('../types/iterable.cjs');
require('../types/object.cjs');
require('../inheritance.cjs');
require('../types/set.cjs');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var varint__default = /*#__PURE__*/_interopDefault(varint);

/**
 * Encodes supplied bytes as base58btc string using given prefix.
 * @param {Uint8Array} value
 * @param {Uint8Array} prefix
 * @returns {string}
 */
const encodeAsBase58btc = (prefix, value) => {
  const prefixBytes = utils_types_bytes.normalizeToU8a(prefix);
  const bytes = utils_types_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 base58.base58btc.encode(multibase);
};

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

exports.decodeFromBase58btc = decodeFromBase58btc;
exports.encodeAsBase58btc = encodeAsBase58btc;
//# sourceMappingURL=base-58-btc.cjs.map
