"use strict";
// Copyright 2017-2020 @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.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("@polkadot/util");
const Struct_1 = __importDefault(require("../../codec/Struct"));
const codec_1 = require("../../codec");
// We explicitly cast the type here to get the actual TypeScript exports right
// We can ignore the properties, added via Struct.with
const _Payload = Struct_1.default.with({
    address: 'Address',
    blockHash: 'Hash',
    blockNumber: 'BlockNumber',
    era: 'ExtrinsicEra',
    genesisHash: 'Hash',
    method: 'Call',
    nonce: 'Compact<Index>',
    runtimeVersion: 'RuntimeVersion',
    tip: 'Compact<Balance>',
    version: 'u8'
});
/**
 * @name SignerPayload
 * @description
 * A generic signer payload that can be used for serialization between API and signer
 */
class SignerPayload extends _Payload {
    /**
     * @description Creates an representation of the structure as an ISignerPayload JSON
     */
    toPayload() {
        const { address, blockHash, blockNumber, era, genesisHash, method, nonce, runtimeVersion: { specVersion }, tip, version } = this;
        return {
            address: address.toString(),
            blockHash: blockHash.toHex(),
            blockNumber: blockNumber.toHex(),
            era: era.toHex(),
            genesisHash: genesisHash.toHex(),
            method: method.toHex(),
            nonce: nonce.toHex(),
            specVersion: specVersion.toHex(),
            tip: tip.toHex(),
            version: version.toNumber()
        };
    }
    /**
     * @description Creates a representation of the payload in raw Exrinsic form
     */
    toRaw() {
        const payload = this.toPayload();
        // NOTE Explicitly pass the bare flag so the method is encoded un-prefixed (non-decodable, for signing only)
        const data = util_1.u8aToHex(codec_1.createType(this.registry, 'ExtrinsicPayload', payload, { version: payload.version }).toU8a({ method: true }));
        return {
            address: payload.address,
            data,
            type: 'payload'
        };
    }
}
exports.default = SignerPayload;
