'use strict';

var base58 = require('multiformats/bases/base58');
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');

/**
 * Encodes supplied bytes as multibase base58btc string using given prefix.
 * @param {Uint8Array} value
 * @param {Uint8Array} prefix
 * @returns {string}
 */
const encodeAsMultibase = (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 multibase base58btc string.
 * @param {string} string
 * @returns {Uint8Array}
 */
const decodeFromMultibase = utils_error.catchFnErrorWith(
  'Invalid base58btc string',
  (string) => base58.base58btc.decode(utils_types_string.ensureString(string)),
);

const isMultibaseBytes = (bytes) => bytes.length === 34;

exports.decodeFromMultibase = decodeFromMultibase;
exports.encodeAsMultibase = encodeAsMultibase;
exports.isMultibaseBytes = isMultibaseBytes;
//# sourceMappingURL=multibase.cjs.map
