text: "use strict";
  var __read = (this && this.__read) || function (o, n) {
      var m = typeof Symbol === "function" && o[Symbol.iterator];
      if (!m) return o;
      var i = m.call(o), r, ar = [], e;
      try {
          while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
      }
      catch (error) { e = { error: error }; }
      finally {
          try {
              if (r && !r.done && (m = i["return"])) m.call(i);
          }
          finally { if (e) throw e.error; }
      }
      return ar;
  };
  var __spreadArray = (this && this.__spreadArray) || function (to, from) {
      for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
          to[j] = from[i];
      return to;
  };
  var __values = (this && this.__values) || function(o) {
      var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
      if (m) return m.call(o);
      if (o && typeof o.length === "number") return {
          next: function () {
              if (o && i >= o.length) o = void 0;
              return { value: o && o[i++], done: !o };
          }
      };
      throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.signatureToString = exports.stringToSignature = exports.privateKeyToString = exports.privateKeyToLegacyString = exports.stringToPrivateKey = exports.convertLegacyPublicKeys = exports.convertLegacyPublicKey = exports.publicKeyToString = exports.publicKeyToLegacyString = exports.stringToPublicKey = exports.signatureDataSize = exports.privateKeyDataSize = exports.publicKeyDataSize = exports.KeyType = exports.base64ToBinary = exports.binaryToBase58 = exports.base58ToBinary = exports.signedBinaryToDecimal = exports.binaryToDecimal = exports.signedDecimalToBinary = exports.decimalToBinary = exports.negate = exports.isNegative = void 0;
  /**
   * @module Numeric
   */
  var hash_js_1 = require("hash.js");
  // copyright defined in eosjs/LICENSE.txt
  var ripemd160 = require('./ripemd').RIPEMD160.hash;
  var base58Chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  var base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  var create_base58_map = function () {
      var base58M = Array(256).fill(-1);
      for (var i = 0; i < base58Chars.length; ++i) {
          base58M[base58Chars.charCodeAt(i)] = i;
      }
      return base58M;
  };
  var base58Map = create_base58_map();
  var create_base64_map = function () {
      var base64M = Array(256).fill(-1);
      for (var i = 0; i < base64Chars.length; ++i) {
          base64M[base64Chars.charCodeAt(i)] = i;
      }
      base64M['='.charCodeAt(0)] = 0;
      return base64M;
  };
  var base64Map = create_base64_map();
  /** Is `bignum` a negative number? */
  var isNegative = function (bignum) {
      return (bignum[bignum.length - 1] & 0x80) !== 0;
  };
  exports.isNegative = isNegative;
  /** Negate `bignum` */
  var negate = function (bignum) {
      var carry = 1;
      for (var i = 0; i < bignum.length; ++i) {
          var x = (~bignum[i] & 0xff) + carry;
          bignum[i] = x;
          carry = x >> 8;
      }
  };
  exports.negate = negate;
  /**
   * Convert an unsigned decimal number in `s` to a bignum
   *
   * @param size bignum size (bytes)
   */
  var decimalToBinary = function (size, s) {
      var result = new Uint8Array(size);
      for (var i = 0; i < s.length; ++i) {
          var srcDigit = s.charCodeAt(i);
          if (srcDigit < '0'.charCodeAt(0) || srcDigit > '9'.charCodeAt(0)) {
              throw new Error('invalid number');
          }
          var carry = srcDigit - '0'.charCodeAt(0);
          for (var j = 0; j < size; ++j) {
              var x = result[j] * 10 + carry;
              result[j] = x;
              carry = x >> 8;
          }
          if (carry) {
              throw new Error('number is out of range');
          }
      }
      return result;
  };
  exports.decimalToBinary = decimalToBinary;
  /**
   * Convert a signed decimal number in `s` to a bignum
   *
   * @param size bignum size (bytes)
   */
  var signedDecimalToBinary = function (size, s) {
      var negative = s[0] === '-';
      if (negative) {
          s = s.substr(1);
      }
      var result = exports.decimalToBinary(size, s);
      if (negative) {
          exports.negate(result);
          if (!exports.isNegative(result)) {
              throw new Error('number is out of range');
          }
      }
      else if (exports.isNegative(result)) {
          throw new Error('number is out of range');
      }
      return result;
  };
  exports.signedDecimalToBinary = signedDecimalToBinary;
  /**
   * Convert `bignum` to an unsigned decimal number
   *
   * @param minDigits 0-pad result to this many digits
   */
  var binaryToDecimal = function (bignum, minDigits) {
      if (minDigits === void 0) { minDigits = 1; }
      var result = Array(minDigits).fill('0'.charCodeAt(0));
      for (var i = bignum.length - 1; i >= 0; --i) {
          var carry = bignum[i];
          for (var j = 0; j < result.length; ++j) {
              var x = ((result[j] - '0'.charCodeAt(0)) << 8) + carry;
              result[j] = '0'.charCodeAt(0) + x % 10;
              carry = (x / 10) | 0;
          }
          while (carry) {
              result.push('0'.charCodeAt(0) + carry % 10);
              carry = (carry / 10) | 0;
          }
      }
      result.reverse();
      return String.fromCharCode.apply(String, __spreadArray([], __read(result)));
  };
  exports.binaryToDecimal = binaryToDecimal;
  /**
   * Convert `bignum` to a signed decimal number
   *
   * @param minDigits 0-pad result to this many digits
   */
  var signedBinaryToDecimal = function (bignum, minDigits) {
      if (minDigits === void 0) { minDigits = 1; }
      if (exports.isNegative(bignum)) {
          var x = bignum.slice();
          exports.negate(x);
          return '-' + exports.binaryToDecimal(x, minDigits);
      }
      return exports.binaryToDecimal(bignum, minDigits);
  };
  exports.signedBinaryToDecimal = signedBinaryToDecimal;
  var base58ToBinaryVarSize = function (s) {
      var e_1, _a;
      var result = [];
      for (var i = 0; i < s.length; ++i) {
          var carry = base58Map[s.charCodeAt(i)];
          if (carry < 0) {
              throw new Error('invalid base-58 value');
          }
          for (var j = 0; j < result.length; ++j) {
              var x = result[j] * 58 + carry;
              result[j] = x & 0xff;
              carry = x >> 8;
          }
          if (carry) {
              result.push(carry);
          }
      }
      try {
          for (var s_1 = __values(s), s_1_1 = s_1.next(); !s_1_1.done; s_1_1 = s_1.next()) {
              var ch = s_1_1.value;
              if (ch === '1') {
                  result.push(0);
              }
              else {
                  break;
              }
          }
      }
      catch (e_1_1) { e_1 = { error: e_1_1 }; }
      finally {
          try {
              if (s_1_1 && !s_1_1.done && (_a = s_1.return)) _a.call(s_1);
          }
          finally { if (e_1) throw e_1.error; }
      }
      result.reverse();
      return new Uint8Array(result);
  };
  /**
   * Convert an unsigned base-58 number in `s` to a bignum
   *
   * @param size bignum size (bytes)
   */
  var base58ToBinary = function (size, s) {
      if (!size) {
          return base58ToBinaryVarSize(s);
      }
      var result = new Uint8Array(size);
      for (var i = 0; i < s.length; ++i) {
          var carry = base58Map[s.charCodeAt(i)];
          if (carry < 0) {
              throw new Error('invalid base-58 value');
          }
          for (var j = 0; j < size; ++j) {
              var x = result[j] * 58 + carry;
              result[j] = x;
              carry = x >> 8;
          }
          if (carry) {
              throw new Error('base-58 value is out of range');
          }
      }
      result.reverse();
      return result;
  };
  exports.base58ToBinary = base58ToBinary;
  /**
   * Convert `bignum` to a base-58 number
   *
   * @param minDigits 0-pad result to this many digits
   */
  var binaryToBase58 = function (bignum, minDigits) {
      var e_2, _a, e_3, _b;
      if (minDigits === void 0) { minDigits = 1; }
      var result = [];
      try {
          for (var bignum_1 = __values(bignum), bignum_1_1 = bignum_1.next(); !bignum_1_1.done; bignum_1_1 = bignum_1.next()) {
              var byte = bignum_1_1.value;
              var carry = byte;
              for (var j = 0; j < result.length; ++j) {
                  var x = (base58Map[result[j]] << 8) + carry;
                  result[j] = base58Chars.charCodeAt(x % 58);
                  carry = (x / 58) | 0;
              }
              while (carry) {
                  result.push(base58Chars.charCodeAt(carry % 58));
                  carry = (carry / 58) | 0;
              }
          }
      }
      catch (e_2_1) { e_2 = { error: e_2_1 }; }
      finally {
          try {
              if (bignum_1_1 && !bignum_1_1.done && (_a = bignum_1.return)) _a.call(bignum_1);
          }
          finally { if (e_2) throw e_2.error; }
      }
      try {
          for (var bignum_2 = __values(bignum), bignum_2_1 = bignum_2.next(); !bignum_2_1.done; bignum_2_1 = bignum_2.next()) {
              var byte = bignum_2_1.value;
              if (byte) {
                  break;
              }
              else {
                  result.push('1'.charCodeAt(0));
              }
          }
      }
      catch (e_3_1) { e_3 = { error: e_3_1 }; }
      finally {
          try {
              if (bignum_2_1 && !bignum_2_1.done && (_b = bignum_2.return)) _b.call(bignum_2);
          }
          finally { if (e_3) throw e_3.error; }
      }
      result.reverse();
      return String.fromCharCode.apply(String, __spreadArray([], __read(result)));
  };
  exports.binaryToBase58 = binaryToBase58;
  /** Convert an unsigned base-64 number in `s` to a bignum */
  var base64ToBinary = function (s) {
      var len = s.length;
      if ((len & 3) === 1 && s[len - 1] === '=') {
          len -= 1;
      } // fc appends an extra '='
      if ((len & 3) !== 0) {
          throw new Error('base-64 value is not padded correctly');
      }
      var groups = len >> 2;
      var bytes = groups * 3;
      if (len > 0 && s[len - 1] === '=') {
          if (s[len - 2] === '=') {
              bytes -= 2;
          }
          else {
              bytes -= 1;
          }
      }
      var result = new Uint8Array(bytes);
      for (var group = 0; group < groups; ++group) {
          var digit0 = base64Map[s.charCodeAt(group * 4 + 0)];
          var digit1 = base64Map[s.charCodeAt(group * 4 + 1)];
          var digit2 = base64Map[s.charCodeAt(group * 4 + 2)];
          var digit3 = base64Map[s.charCodeAt(group * 4 + 3)];
          result[group * 3 + 0] = (digit0 << 2) | (digit1 >> 4);
          if (group * 3 + 1 < bytes) {
              result[group * 3 + 1] = ((digit1 & 15) << 4) | (digit2 >> 2);
          }
          if (group * 3 + 2 < bytes) {
              result[group * 3 + 2] = ((digit2 & 3) << 6) | digit3;
          }
      }
      return result;
  };
  exports.base64ToBinary = base64ToBinary;
  /** Key types this library supports */
  var KeyType;
  (function (KeyType) {
      KeyType[KeyType["k1"] = 0] = "k1";
      KeyType[KeyType["r1"] = 1] = "r1";
      KeyType[KeyType["wa"] = 2] = "wa";
  })(KeyType = exports.KeyType || (exports.KeyType = {}));
  /** Public key data size, excluding type field */
  exports.publicKeyDataSize = 33;
  /** Private key data size, excluding type field */
  exports.privateKeyDataSize = 32;
  /** Signature data size, excluding type field */
  exports.signatureDataSize = 65;
  var digestSuffixRipemd160 = function (data, suffix) {
      var d = new Uint8Array(data.length + suffix.length);
      for (var i = 0; i < data.length; ++i) {
          d[i] = data[i];
      }
      for (var i = 0; i < suffix.length; ++i) {
          d[data.length + i] = suffix.charCodeAt(i);
      }
      return ripemd160(d);
  };
  var stringToKey = function (s, type, size, suffix) {
      var whole = exports.base58ToBinary(size ? size + 4 : 0, s);
      var result = { type: type, data: new Uint8Array(whole.buffer, 0, whole.length - 4) };
      var digest = new Uint8Array(digestSuffixRipemd160(result.data, suffix));
      if (digest[0] !== whole[whole.length - 4] || digest[1] !== whole[whole.length - 3]
          || digest[2] !== whole[whole.length - 2] || digest[3] !== whole[whole.length - 1]) {
          throw new Error('checksum doesn\'t match');
      }
      return result;
  };
  var keyToString = function (key, suffix, prefix) {
      var digest = new Uint8Array(digestSuffixRipemd160(key.data, suffix));
      var whole = new Uint8Array(key.data.length + 4);
      for (var i = 0; i < key.data.length; ++i) {
          whole[i] = key.data[i];
      }
      for (var i = 0; i < 4; ++i) {
          whole[i + key.data.length] = digest[i];
      }
      return prefix + exports.binaryToBase58(whole);
  };
  /** Convert key in `s` to binary form */
  var stringToPublicKey = function (s) {
      if (typeof s !== 'string') {
          throw new Error('expected string containing public key');
      }
      if (s.substr(0, 3) === 'OSB') {
          var whole = exports.base58ToBinary(exports.publicKeyDataSize + 4, s.substr(3));
          var key = { type: KeyType.k1, data: new Uint8Array(exports.publicKeyDataSize) };
          for (var i = 0; i < exports.publicKeyDataSize; ++i) {
              key.data[i] = whole[i];
          }
          var digest = new Uint8Array(ripemd160(key.data));
          if (digest[0] !== whole[exports.publicKeyDataSize] || digest[1] !== whole[34]
              || digest[2] !== whole[35] || digest[3] !== whole[36]) {
              throw new Error('checksum doesn\'t match');
          }
          return key;
      }
      else if (s.substr(0, 7) === 'PUB_K1_') {
          return stringToKey(s.substr(7), KeyType.k1, exports.publicKeyDataSize, 'K1');
      }
      else if (s.substr(0, 7) === 'PUB_R1_') {
          return stringToKey(s.substr(7), KeyType.r1, exports.publicKeyDataSize, 'R1');
      }
      else if (s.substr(0, 7) === 'PUB_WA_') {
          return stringToKey(s.substr(7), KeyType.wa, 0, 'WA');
      }
      else {
          throw new Error('unrecognized public key format');
      }
  };
  exports.stringToPublicKey = stringToPublicKey;
  /** Convert public `key` to legacy string (base-58) form */
  var publicKeyToLegacyString = function (key) {
      if (key.type === KeyType.k1 && key.data.length === exports.publicKeyDataSize) {
          return keyToString(key, '', 'OSB');
      }
      else if (key.type === KeyType.r1 || key.type === KeyType.wa) {
          throw new Error('Key format not supported in legacy conversion');
      }
      else {
          throw new Error('unrecognized public key format');
      }
  };
  exports.publicKeyToLegacyString = publicKeyToLegacyString;
  /** Convert `key` to string (base-58) form */
  var publicKeyToString = function (key) {
      if (key.type === KeyType.k1 && key.data.length === exports.publicKeyDataSize) {
          return keyToString(key, 'K1', 'PUB_K1_');
      }
      else if (key.type === KeyType.r1 && key.data.length === exports.publicKeyDataSize) {
          return keyToString(key, 'R1', 'PUB_R1_');
      }
      else if (key.type === KeyType.wa) {
          return keyToString(key, 'WA', 'PUB_WA_');
      }
      else {
          throw new Error('unrecognized public key format');
      }
  };
  exports.publicKeyToString = publicKeyToString;
  /** If a key is in the legacy format (`EOS` prefix), then convert it to the new format (`PUB_K1_`).
   * Leaves other formats untouched
   */
  var convertLegacyPublicKey = function (s) {
      if (s.substr(0, 3) === 'OSB') {
          return exports.publicKeyToString(exports.stringToPublicKey(s));
      }
      return s;
  };
  exports.convertLegacyPublicKey = convertLegacyPublicKey;
  /** If a key is in the legacy format (`EOS` prefix), then convert it to the new format (`PUB_K1_`).
   * Leaves other formats untouched
   */
  var convertLegacyPublicKeys = function (keys) {
      return keys.map(exports.convertLegacyPublicKey);
  };
  exports.convertLegacyPublicKeys = convertLegacyPublicKeys;
  /** Convert key in `s` to binary form */
  var stringToPrivateKey = function (s) {
      if (typeof s !== 'string') {
          throw new Error('expected string containing private key');
      }
      if (s.substr(0, 7) === 'PVT_R1_') {
          return stringToKey(s.substr(7), KeyType.r1, exports.privateKeyDataSize, 'R1');
      }
      else if (s.substr(0, 7) === 'PVT_K1_') {
          return stringToKey(s.substr(7), KeyType.k1, exports.privateKeyDataSize, 'K1');
      }
      else {
          // todo: Verify checksum: sha256(sha256(key.data)).
          //       Not critical since a bad key will fail to produce a
          //       valid signature anyway.
          var whole = exports.base58ToBinary(exports.privateKeyDataSize + 5, s);
          var key = { type: KeyType.k1, data: new Uint8Array(exports.privateKeyDataSize) };
          if (whole[0] !== 0x80) {
              throw new Error('unrecognized private key type');
          }
          for (var i = 0; i < exports.privateKeyDataSize; ++i) {
              key.data[i] = whole[i + 1];
          }
          return key;
      }
  };
  exports.stringToPrivateKey = stringToPrivateKey;
  /** Convert private `key` to legacy string (base-58) form */
  var privateKeyToLegacyString = function (key) {
      if (key.type === KeyType.k1 && key.data.length === exports.privateKeyDataSize) {
          var whole_1 = [];
          whole_1.push(128);
          key.data.forEach(function (byte) { return whole_1.push(byte); });
          var digest = new Uint8Array(hash_js_1.sha256().update(hash_js_1.sha256().update(whole_1).digest()).digest());
          var result = new Uint8Array(exports.privateKeyDataSize + 5);
          for (var i = 0; i < whole_1.length; i++) {
              result[i] = whole_1[i];
          }
          for (var i = 0; i < 4; i++) {
              result[i + whole_1.length] = digest[i];
          }
          return exports.binaryToBase58(result);
      }
      else if (key.type === KeyType.r1 || key.type === KeyType.wa) {
          throw new Error('Key format not supported in legacy conversion');
      }
      else {
          throw new Error('unrecognized public key format');
      }
  };
  exports.privateKeyToLegacyString = privateKeyToLegacyString;
  /** Convert `key` to string (base-58) form */
  var privateKeyToString = function (key) {
      if (key.type === KeyType.r1) {
          return keyToString(key, 'R1', 'PVT_R1_');
      }
      else if (key.type === KeyType.k1) {
          return keyToString(key, 'K1', 'PVT_K1_');
      }
      else {
          throw new Error('unrecognized private key format');
      }
  };
  exports.privateKeyToString = privateKeyToString;
  /** Convert key in `s` to binary form */
  var stringToSignature = function (s) {
      if (typeof s !== 'string') {
          throw new Error('expected string containing signature');
      }
      if (s.substr(0, 7) === 'SIG_K1_') {
          return stringToKey(s.substr(7), KeyType.k1, exports.signatureDataSize, 'K1');
      }
      else if (s.substr(0, 7) === 'SIG_R1_') {
          return stringToKey(s.substr(7), KeyType.r1, exports.signatureDataSize, 'R1');
      }
      else if (s.substr(0, 7) === 'SIG_WA_') {
          return stringToKey(s.substr(7), KeyType.wa, 0, 'WA');
      }
      else {
          throw new Error('unrecognized signature format');
      }
  };
  exports.stringToSignature = stringToSignature;
  /** Convert `signature` to string (base-58) form */
  var signatureToString = function (signature) {
      if (signature.type === KeyType.k1) {
          return keyToString(signature, 'K1', 'SIG_K1_');
      }
      else if (signature.type === KeyType.r1) {
          return keyToString(signature, 'R1', 'SIG_R1_');
      }
      else if (signature.type === KeyType.wa) {
          return keyToString(signature, 'WA', 'SIG_WA_');
      }
      else {
          throw new Error('unrecognized signature format');
      }
  };
  exports.signatureToString = signatureToString;
  //# sourceMappingURL=eosjs-numeric.js.map