"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = encode;

var _bs = _interopRequireDefault(require("bs58"));

var _util = require("@polkadot/util");

var _decode = _interopRequireDefault(require("./decode"));

var _defaults = _interopRequireDefault(require("./defaults"));

var _sshash = _interopRequireDefault(require("./sshash"));

// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
function encode(_key, ss58Format = _defaults.default.prefix) {
  // decode it, this means we can re-encode an address
  const key = (0, _decode.default)(_key);
  (0, _util.assert)(_defaults.default.allowedDecodedLengths.includes(key.length), "Expected a valid key to convert, with length ".concat(_defaults.default.allowedDecodedLengths));
  const isPublicKey = key.length === 32;
  const input = (0, _util.u8aConcat)(new Uint8Array([ss58Format]), key);
  const hash = (0, _sshash.default)(input);
  return _bs.default.encode((0, _util.u8aToBuffer)((0, _util.u8aConcat)(input, hash.subarray(0, isPublicKey ? 2 : 1))));
}