"use strict";

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

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

var _create = require("../../../codec/create");

var _constants = require("../constants");

var _ExtrinsicSignature = _interopRequireDefault(require("../v2/ExtrinsicSignature"));

var _ExtrinsicPayload = _interopRequireDefault(require("./ExtrinsicPayload"));

// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

/**
 * @name ExtrinsicSignatureV3
 * @description
 * A container for the [[Signature]] associated with a specific [[Extrinsic]]
 */
class ExtrinsicSignatureV3 extends _ExtrinsicSignature.default {
  /**
   * @description Adds a raw signature
   */
  addSignature(signer, signature, payload) {
    return this.injectSignature((0, _create.createType)('Address', signer), (0, _create.createType)('Signature', signature), new _ExtrinsicPayload.default(payload));
  }
  /**
   * @description Generate a payload and pplies the signature from a keypair
   */


  sign(method, account, _ref) {
    let {
      blockHash,
      era,
      genesisHash,
      nonce,
      runtimeVersion: {
        specVersion
      },
      tip
    } = _ref;
    const signer = (0, _create.createType)('Address', account.publicKey);
    const payload = new _ExtrinsicPayload.default({
      blockHash,
      era: era || _constants.IMMORTAL_ERA,
      genesisHash,
      method: method.toHex(),
      nonce,
      specVersion,
      tip: tip || 0
    });
    const signature = (0, _create.createType)('Signature', payload.sign(account));
    return this.injectSignature(signer, signature, payload);
  }

}

exports.default = ExtrinsicSignatureV3;