"use strict";
// Copyright 2017-2020 @polkadot/api authors & contributors
// SPDX-License-Identifier: Apache-2.0
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _SingleAccountSigner_keyringPair, _SingleAccountSigner_registry, _SingleAccountSigner_signDelay;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SingleAccountSigner = void 0;
const util_1 = require("@polkadot/util");
let id = 0;
class SingleAccountSigner {
    constructor(registry, keyringPair, signDelay = 0) {
        _SingleAccountSigner_keyringPair.set(this, void 0);
        _SingleAccountSigner_registry.set(this, void 0);
        _SingleAccountSigner_signDelay.set(this, void 0);
        __classPrivateFieldSet(this, _SingleAccountSigner_keyringPair, keyringPair, "f");
        __classPrivateFieldSet(this, _SingleAccountSigner_registry, registry, "f");
        __classPrivateFieldSet(this, _SingleAccountSigner_signDelay, signDelay, "f");
    }
    signPayload(payload) {
        return __awaiter(this, void 0, void 0, function* () {
            if (payload.address !== __classPrivateFieldGet(this, _SingleAccountSigner_keyringPair, "f").address) {
                throw new Error("does not have the keyringPair");
            }
            return new Promise((resolve) => {
                setTimeout(() => {
                    const signed = __classPrivateFieldGet(this, _SingleAccountSigner_registry, "f").createType("ExtrinsicPayload", payload, { version: payload.version }).sign(__classPrivateFieldGet(this, _SingleAccountSigner_keyringPair, "f"));
                    resolve(Object.assign({ id: ++id }, signed));
                }, __classPrivateFieldGet(this, _SingleAccountSigner_signDelay, "f"));
            });
        });
    }
    signRaw({ address, data }) {
        return __awaiter(this, void 0, void 0, function* () {
            if (address !== __classPrivateFieldGet(this, _SingleAccountSigner_keyringPair, "f").address) {
                throw new Error("does not have the keyringPair");
            }
            return new Promise((resolve) => {
                setTimeout(() => {
                    const signature = util_1.u8aToHex(__classPrivateFieldGet(this, _SingleAccountSigner_keyringPair, "f").sign(util_1.hexToU8a(data)));
                    resolve({
                        id: ++id,
                        signature
                    });
                }, __classPrivateFieldGet(this, _SingleAccountSigner_signDelay, "f"));
            });
        });
    }
}
exports.SingleAccountSigner = SingleAccountSigner;
_SingleAccountSigner_keyringPair = new WeakMap(), _SingleAccountSigner_registry = new WeakMap(), _SingleAccountSigner_signDelay = new WeakMap();
//# sourceMappingURL=SingleAccountSigner.js.map