"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.generateMnemonic = exports.EnglishMnemonic = exports.Bip39 = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var bip39 = _interopRequireWildcard(require("bip39-light"));
var _encoding = require("@cosmjs/encoding");
var _crypto = require("@cosmjs/crypto");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t3 in e) "default" !== _t3 && {}.hasOwnProperty.call(e, _t3) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t3)) && (i.get || i.set) ? o(f, _t3, i) : f[_t3] = e[_t3]); return f; })(e, t); }
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
var Bip39 = exports.Bip39 = /*#__PURE__*/function () {
  function Bip39() {
    (0, _classCallCheck2["default"])(this, Bip39);
  }
  return (0, _createClass2["default"])(Bip39, null, [{
    key: "encode",
    value:
    /**
     * Encodes raw entropy of length 16, 20, 24, 28 or 32 bytes as an English mnemonic between 12 and 24 words.
     *
     * | Entropy            | Words |
     * |--------------------|-------|
     * | 128 bit (16 bytes) |    12 |
     * | 160 bit (20 bytes) |    15 |
     * | 192 bit (24 bytes) |    18 |
     * | 224 bit (28 bytes) |    21 |
     * | 256 bit (32 bytes) |    24 |
     *
     *
     * @see https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic
     * @param entropy The entropy to be encoded. This must be cryptographically secure.
     */
    function encode(entropy) {
      var allowedEntropyLengths = [16, 20, 24, 28, 32];
      if (allowedEntropyLengths.indexOf(entropy.length) === -1) {
        throw new Error("invalid input length");
      }
      return new EnglishMnemonic(bip39.entropyToMnemonic((0, _encoding.toHex)(entropy)));
    }
  }, {
    key: "decode",
    value: function decode(mnemonic) {
      return (0, _encoding.fromHex)(bip39.mnemonicToEntropy(mnemonic.toString()));
    }
  }, {
    key: "mnemonicToSeed",
    value: function () {
      var _mnemonicToSeed = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee(mnemonic, password) {
        var _t, _t2;
        return _regenerator().w(function (_context) {
          while (1) switch (_context.n) {
            case 0:
              _t = Uint8Array;
              _context.n = 1;
              return bip39.mnemonicToSeed(mnemonic.toString(), password);
            case 1:
              _t2 = _context.v;
              return _context.a(2, new _t(_t2));
          }
        }, _callee);
      }));
      function mnemonicToSeed(_x, _x2) {
        return _mnemonicToSeed.apply(this, arguments);
      }
      return mnemonicToSeed;
    }()
  }]);
}();
var EnglishMnemonic = exports.EnglishMnemonic = /*#__PURE__*/function () {
  function EnglishMnemonic(mnemonic) {
    (0, _classCallCheck2["default"])(this, EnglishMnemonic);
    (0, _defineProperty2["default"])(this, "data", void 0);
    if (!EnglishMnemonic.mnemonicMatcher.test(mnemonic)) {
      throw new Error("Invalid mnemonic format");
    }
    var words = mnemonic.split(" ");
    var allowedWordsLengths = [12, 15, 18, 21, 24];
    if (allowedWordsLengths.indexOf(words.length) === -1) {
      throw new Error("Invalid word count in mnemonic (allowed: ".concat(allowedWordsLengths, " got: ").concat(words.length, ")"));
    }
    if (!bip39.validateMnemonic(mnemonic)) {
      throw new Error("Mnemonic is invalid.");
    }

    // Throws with informative error message if mnemonic is not valid
    bip39.mnemonicToEntropy(mnemonic);
    this.data = mnemonic;
  }
  return (0, _createClass2["default"])(EnglishMnemonic, [{
    key: "toString",
    value: function toString() {
      return this.data;
    }
  }]);
}();
// list of space separated lower case words (1 or more)
(0, _defineProperty2["default"])(EnglishMnemonic, "mnemonicMatcher", /^[a-z]+( [a-z]+)*$/);
var generateMnemonic = exports.generateMnemonic = function generateMnemonic() {
  var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12;
  var entropyLength = 4 * Math.floor(11 * length / 33);
  var entropy = _crypto.Random.getBytes(entropyLength);
  var mnemonic = Bip39.encode(entropy);
  return mnemonic.toString();
};