"use strict";
// Goal of this file is to provide utils for injecting device/signer dependency to coin-modules.
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMessageSigner = exports.createResolver = exports.executeWithSigner = void 0;
const rxjs_1 = require("rxjs");
const deviceAccess_1 = require("../hw/deviceAccess");
/**
 * Retrieve `transport` to provide it to the signer and give some sort of scope for which the `transport` will be valid.
 * @param signerFactory
 * @returns SignerContext
 */
function executeWithSigner(signerFactory) {
    return (deviceId, fn) => (0, rxjs_1.firstValueFrom)((0, deviceAccess_1.withDevice)(deviceId)(transport => (0, rxjs_1.from)(fn(signerFactory(transport)))));
}
exports.executeWithSigner = executeWithSigner;
/**
 * Inject the `signer` so it can be used by the resolver function.
 * @param signer
 * @param coinResolver
 * @returns Resolver
 */
function createResolver(signerFactory, coinResolver) {
    return (transport, opts) => {
        const signerContext = (_, fn) => fn(signerFactory(transport));
        return coinResolver(signerContext)("", opts);
    };
}
exports.createResolver = createResolver;
/**
 * Inject the `signer` so it can be used by the hw-signMessage function.
 */
function createMessageSigner(signerFactory, messageSigner) {
    return (transport, account, messageData) => {
        const signerContext = (_, fn) => fn(signerFactory(transport));
        return messageSigner(signerContext)("", account, messageData);
    };
}
exports.createMessageSigner = createMessageSigner;
//# sourceMappingURL=setup.js.map