"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.JsonToArray = void 0;
exports.Uint8ArrayToJS = Uint8ArrayToJS;
exports.jsonToBase64 = exports.jsonStringToBase64 = exports.encodeMbKey = exports.decodeMbKey = exports.concatArrayBuffers = exports.b64toUint8Array = exports.b64toJson = exports.Uint8ArrayTob64 = void 0;
var _encoding = require("@cosmjs/encoding");
var _constants = require("./constants");
var _bs = _interopRequireDefault(require("bs58"));
var b64toUint8Array = exports.b64toUint8Array = function b64toUint8Array(b64) {
  return Uint8Array.from(Buffer.from(b64, "base64"));
};
var b64toJson = exports.b64toJson = function b64toJson(b64) {
  return JSON.parse(Buffer.from(b64, "base64").toString());
};
var Uint8ArrayTob64 = exports.Uint8ArrayTob64 = function Uint8ArrayTob64(u8) {
  var b64 = Buffer.from(u8).toString("base64");
  return b64;
};

// JSON to Uint8Array parsing and visa versa
var JsonToArray = exports.JsonToArray = function JsonToArray(json) {
  return new Uint8Array(Buffer.from(json));
};
function Utf8ArrayToStr(array) {
  var out, i, c;
  var char2, char3;
  out = "";
  var len = array.length;
  i = 0;
  while (i < len) {
    c = array[i++];
    switch (c >> 4) {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        // 0xxxxxxx
        out += String.fromCharCode(c);
        break;
      case 12:
      case 13:
        // 110x xxxx   10xx xxxx
        char2 = array[i++];
        out += String.fromCharCode((c & 0x1f) << 6 | char2 & 0x3f);
        break;
      case 14:
        // 1110 xxxx  10xx xxxx  10xx xxxx
        char2 = array[i++];
        char3 = array[i++];
        out += String.fromCharCode((c & 0x0f) << 12 | (char2 & 0x3f) << 6 | (char3 & 0x3f) << 0);
        break;
    }
  }
  return out;
}
// Converts A Unit8Array to String
function Uint8ArrayToJS(data) {
  var decodedData = Utf8ArrayToStr(data);
  return decodedData;
}
var concatArrayBuffers = exports.concatArrayBuffers = function concatArrayBuffers() {
  for (var _len = arguments.length, bufs = new Array(_len), _key = 0; _key < _len; _key++) {
    bufs[_key] = arguments[_key];
  }
  var result = new Uint8Array(bufs.reduce(function (totalSize, buf) {
    return totalSize + buf.byteLength;
  }, 0));
  bufs.reduce(function (offset, buf) {
    result.set(buf, offset);
    return offset + buf.byteLength;
  }, 0);
  return result;
};
var jsonStringToBase64 = exports.jsonStringToBase64 = function jsonStringToBase64(jsonString) {
  var base64 = (0, _encoding.toBase64)(JsonToArray(jsonString));
  return base64;
};
var jsonToBase64 = exports.jsonToBase64 = function jsonToBase64(json) {
  var jsonString = JSON.stringify(json);
  return jsonStringToBase64(jsonString);
};

/**
 * encode a multibase base58-btc multicodec key
 * @param header Uint8Array header for multicodec encoding
 * @param key Uint8Array of key encoding
 */
var encodeMbKey = exports.encodeMbKey = function encodeMbKey(header, key) {
  var mbKey = new Uint8Array(header.length + key.length);
  mbKey.set(header);
  mbKey.set(key, header.length);
  return _constants.MULTIBASE_BASE58BTC_HEADER + _bs["default"].encode(mbKey);
};

/**
 * decode a multibase base58-btc multicodec key
 * @param mbKey string multibase base58-btc multicodec key
 */
var decodeMbKey = exports.decodeMbKey = function decodeMbKey(header, mbKey) {
  var decoded = _bs["default"].decode(mbKey.slice(_constants.MULTIBASE_BASE58BTC_HEADER.length));
  if (decoded.slice(0, header.length).toString() !== header.toString()) {
    throw new Error("Invalid multibase base58-btc multicodec key");
  }
  return decoded.slice(header.length);
};