'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.Signer = undefined;var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}(); // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

var _scryptsy = require('scryptsy');var _scryptsy2 = _interopRequireDefault(_scryptsy);
var _ethereumjsTx = require('ethereumjs-tx');var _ethereumjsTx2 = _interopRequireDefault(_ethereumjsTx);
var _crypto = require('crypto');
var _browserifyAes = require('browserify-aes');

var _input = require('@parity/api/lib/format/input');
var _sha = require('@parity/api/lib/util/sha3');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}

// Adapted from https://github.com/kvhnuke/etherwallet/blob/mercury/app/scripts/myetherwallet.js
var
Signer = exports.Signer = function () {_createClass(Signer, null, [{ key: 'fromJson', value: function fromJson(
    json, password) {
      return Signer.
      getSeed(json, password).
      then(function (seed) {
        return new Signer(seed);
      });
    } }, { key: 'getSeed', value: function getSeed(

    json, password) {
      try {
        var seed = Signer.getSyncSeed(json, password);

        return Promise.resolve(seed);
      } catch (error) {
        return Promise.reject(error);
      }
    } }, { key: 'getSyncSeed', value: function getSyncSeed(

    json, password) {
      if (json.version !== 3) {
        throw new Error('Only V3 wallets are supported');
      }var

      kdf = json.crypto.kdf;
      var kdfparams = json.crypto.kdfparams || {};
      var pwd = Buffer.from(password);
      var salt = Buffer.from(kdfparams.salt, 'hex');
      var derivedKey = void 0;

      if (kdf === 'scrypt') {
        derivedKey = (0, _scryptsy2.default)(pwd, salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
      } else if (kdf === 'pbkdf2') {
        if (kdfparams.prf !== 'hmac-sha256') {
          throw new Error('Unsupported parameters to PBKDF2');
        }

        derivedKey = (0, _crypto.pbkdf2Sync)(pwd, salt, kdfparams.c, kdfparams.dklen, 'sha256');
      } else {
        throw new Error('Unsupported key derivation scheme');
      }

      var ciphertext = Buffer.from(json.crypto.ciphertext, 'hex');
      var mac = (0, _sha.sha3)(Buffer.concat([derivedKey.slice(16, 32), ciphertext]));

      if (mac !== (0, _input.inHex)(json.crypto.mac)) {
        throw new Error('Key derivation failed - possibly wrong password');
      }

      var decipher = (0, _browserifyAes.createDecipheriv)(
      json.crypto.cipher,
      derivedKey.slice(0, 16),
      Buffer.from(json.crypto.cipherparams.iv, 'hex'));


      var seed = Buffer.concat([decipher.update(ciphertext), decipher.final()]);

      while (seed.length < 32) {
        var nullBuff = Buffer.from([0x00]);

        seed = Buffer.concat([nullBuff, seed]);
      }

      return seed;
    } }]);

  function Signer(seed) {_classCallCheck(this, Signer);
    this.seed = seed;
  }_createClass(Signer, [{ key: 'signTransactionObject', value: function signTransactionObject(

    tx) {
      tx.sign(this.seed);

      return tx;
    } }, { key: 'signTransaction', value: function signTransaction(

    transaction) {
      var tx = new _ethereumjsTx2.default(transaction);

      return (0, _input.inHex)(this.signTransactionObject(tx).serialize().toString('hex'));
    } }]);return Signer;}();