"use strict";

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

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

var _util = require("@polkadot/util");

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

var _Base = _interopRequireDefault(require("../../codec/Base"));

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

// 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.
const VERSIONS = ['ExtrinsicPayloadUnknown', // v0 is unknown
'ExtrinsicPayloadV1', 'ExtrinsicPayloadV2', 'ExtrinsicPayloadV3', 'ExtrinsicPayloadV4'];
/**
 * @name ExtrinsicPayload
 * @description
 * A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
 * on the contents included
 */

class ExtrinsicPayload extends _Base.default {
  constructor(value) {
    let {
      version
    } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    super(ExtrinsicPayload.decodeExtrinsicPayload(value, version));
  }

  static decodeExtrinsicPayload(value) {
    let version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _constants.DEFAULT_VERSION;

    if (value instanceof ExtrinsicPayload) {
      return value.raw;
    }

    return (0, _create.createType)(VERSIONS[version] || VERSIONS[0], value, {
      version
    });
  }
  /**
   * @description The block [[Hash]] the signature applies to (mortal/immortal)
   */


  get blockHash() {
    return this.raw.blockHash;
  }
  /**
   * @description The [[ExtrinsicEra]]
   */


  get era() {
    return this.raw.era;
  }
  /**
   * @description The genesis block [[Hash]] the signature applies to
   */


  get genesisHash() {
    // NOTE only v3+
    return this.raw.genesisHash || (0, _create.createType)('Hash');
  }
  /**
   * @description The [[U8a]] contained in the payload
   */


  get method() {
    return this.raw.method;
  }
  /**
   * @description The [[Index]]
   */


  get nonce() {
    return this.raw.nonce;
  }
  /**
   * @description The specVersion as a [[u32]] for this payload
   */


  get specVersion() {
    // NOTE only v3+
    return this.raw.specVersion || (0, _create.createType)('u32');
  }
  /**
   * @description The [[Balance]]
   */


  get tip() {
    // NOTE from v2+
    return this.raw.tip || (0, _create.createType)('Compact<Balance>');
  }
  /**
   * @description Compares the value of the input to see if there is a match
   */


  eq(other) {
    return this.raw.eq(other);
  }
  /**
   * @description Sign the payload with the keypair
   */


  sign(signerPair) {
    const signature = this.raw.sign(signerPair); // This is extensible, so we could quite readily extend to send back extra
    // information, such as for instance the payload, i.e. `payload: this.toHex()`
    // For the case here we sign via the extrinsic, we ignore the return, so generally
    // this is applicable for external signing

    return {
      signature: (0, _util.u8aToHex)(signature)
    };
  }
  /**
   * @description Converts the Object to JSON, typically used for RPC transfers
   */


  toJSON() {
    return this.toHex();
  }
  /**
   * @description Returns the string representation of the value
   */


  toString() {
    return this.toHex();
  }

}

exports.default = ExtrinsicPayload;