@trezor/connect
Version:
High-level javascript interface for Trezor hardware wallet.
82 lines • 2.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrezorConnectDynamic = void 0;
const constants_1 = require("../constants");
const proxy_event_emitter_1 = require("../utils/proxy-event-emitter");
class TrezorConnectDynamic {
eventEmitter;
currentTarget;
implementations;
getInitTarget;
handleErrorFallback;
lastSettings;
constructor({ implementations, getInitTarget, handleErrorFallback, }) {
this.implementations = implementations;
this.currentTarget = this.implementations[0].type;
this.getInitTarget = getInitTarget;
this.handleErrorFallback = handleErrorFallback;
this.eventEmitter = new proxy_event_emitter_1.ProxyEventEmitter(this.implementations.map(impl => impl.impl.eventEmitter));
}
getTarget() {
return this.implementations.find(impl => impl.type === this.currentTarget).impl;
}
async switchTarget(target) {
if (this.currentTarget === target) {
return;
}
if (!this.lastSettings) {
throw constants_1.ERRORS.TypedError('Init_NotInitialized');
}
await this.getTarget().dispose();
this.currentTarget = target;
await this.getTarget().init(this.lastSettings);
}
manifest(manifest) {
this.lastSettings = { ...this.lastSettings, manifest };
this.getTarget().manifest(manifest);
}
async init(settings) {
if (!settings?.manifest) {
throw constants_1.ERRORS.TypedError('Init_ManifestMissing');
}
this.lastSettings = settings;
this.currentTarget = this.getInitTarget(settings);
try {
return await this.getTarget().init(this.lastSettings);
}
catch (error) {
if (await this.handleErrorFallback(error.code)) {
return await this.getTarget().init(settings);
}
throw error;
}
}
setTransports({ transports }) {
this.lastSettings = { ...this.lastSettings, transports };
this.getTarget().setTransports({ transports });
}
async call(params) {
const response = await this.getTarget().call(params);
if (!response.success) {
if (await this.handleErrorFallback(response.payload.code)) {
return await this.getTarget().call(params);
}
}
return response;
}
requestLogin(params) {
return this.getTarget().requestLogin(params);
}
uiResponse(params) {
return this.getTarget().uiResponse(params);
}
cancel(error) {
return this.getTarget().cancel(error);
}
dispose() {
this.eventEmitter.removeAllListeners();
return this.getTarget().dispose();
}
}
exports.TrezorConnectDynamic = TrezorConnectDynamic;
//# sourceMappingURL=dynamic.js.map