@trezor/connect 9.0.3 → 9.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +1 -1
  2. package/lib/api/authorizeCoinJoin.js +6 -2
  3. package/lib/api/bitcoin/enhanceSignTx.js +1 -1
  4. package/lib/api/bitcoin/inputs.js +3 -2
  5. package/lib/api/bitcoin/refTx.js +3 -3
  6. package/lib/api/blockchainDisconnect.js +1 -1
  7. package/lib/api/blockchainSetCustomBackend.js +1 -1
  8. package/lib/api/cardano/cardanoAuxiliaryData.d.ts +1 -1
  9. package/lib/api/cardano/cardanoAuxiliaryData.js +38 -16
  10. package/lib/api/cardanoSignTransaction.d.ts +1 -0
  11. package/lib/api/cardanoSignTransaction.js +23 -3
  12. package/lib/api/getOwnershipProof.d.ts +1 -5
  13. package/lib/api/getOwnershipProof.js +5 -3
  14. package/lib/api/index.d.ts +1 -0
  15. package/lib/api/index.js +3 -1
  16. package/lib/api/setBusy.d.ts +7 -0
  17. package/lib/api/setBusy.js +27 -0
  18. package/lib/api/signTransaction.d.ts +1 -1
  19. package/lib/api/signTransaction.js +10 -3
  20. package/lib/backend/BackendManager.d.ts +21 -0
  21. package/lib/backend/BackendManager.js +91 -0
  22. package/lib/backend/Blockchain.d.ts +72 -0
  23. package/lib/backend/Blockchain.js +204 -0
  24. package/lib/backend/BlockchainLink.d.ts +6 -71
  25. package/lib/backend/BlockchainLink.js +12 -288
  26. package/lib/constants/cardano.d.ts +3 -1
  27. package/lib/constants/cardano.js +3 -1
  28. package/lib/core/AbstractMethod.d.ts +1 -0
  29. package/lib/core/index.d.ts +1 -1
  30. package/lib/core/index.js +1 -1
  31. package/lib/core/method.d.ts +1 -1
  32. package/lib/data/config.js +1 -1
  33. package/lib/data/version.d.ts +1 -1
  34. package/lib/data/version.js +1 -1
  35. package/lib/device/Device.d.ts +1 -1
  36. package/lib/device/Device.js +8 -2
  37. package/lib/device/DeviceCommands.d.ts +1 -0
  38. package/lib/device/DeviceCommands.js +11 -0
  39. package/lib/factory.js +1 -0
  40. package/lib/types/api/authorizeCoinJoin.d.ts +1 -0
  41. package/lib/types/api/bitcoin/index.d.ts +4 -0
  42. package/lib/types/api/cardano/index.d.ts +10 -3
  43. package/lib/types/api/index.d.ts +2 -0
  44. package/lib/types/api/setBusy.d.ts +4 -0
  45. package/lib/types/api/setBusy.js +3 -0
  46. package/package.json +10 -8
  47. package/lib/utils/bufferUtils.d.ts +0 -3
  48. package/lib/utils/bufferUtils.js +0 -11
@@ -0,0 +1,72 @@
1
+ import BlockchainLink, { ServerInfo, SubscriptionAccountInfo, BlockchainLinkParams, BlockchainLinkResponse } from '@trezor/blockchain-link';
2
+ import { ERRORS } from '../constants';
3
+ import type { CoinInfo, Proxy } from '../types';
4
+ import type { CoreMessage } from '../events';
5
+ export declare type BlockchainOptions = {
6
+ coinInfo: CoinInfo;
7
+ postMessage: (message: CoreMessage) => void;
8
+ proxy?: Proxy;
9
+ onionDomains?: {
10
+ [domain: string]: string;
11
+ };
12
+ debug?: boolean;
13
+ onConnected?: (url: string) => void;
14
+ onDisconnected?: () => void;
15
+ };
16
+ export declare class Blockchain {
17
+ link: BlockchainLink;
18
+ serverInfo?: ServerInfo;
19
+ coinInfo: BlockchainOptions['coinInfo'];
20
+ onionDomains: {
21
+ [onion: string]: string;
22
+ };
23
+ postMessage: BlockchainOptions['postMessage'];
24
+ feeForBlock: BlockchainLinkResponse<'estimateFee'>;
25
+ feeTimestamp: number;
26
+ private onConnected;
27
+ private onDisconnected;
28
+ constructor(options: BlockchainOptions);
29
+ onError(error: ERRORS.TrezorError): void;
30
+ init(): Promise<void>;
31
+ getTransactions(txs: string[]): Promise<import("@trezor/blockchain-link").TypedRawTransaction[]>;
32
+ getCurrentFiatRates(params: {
33
+ currencies?: string[];
34
+ }): Promise<{
35
+ ts: number;
36
+ rates: import("@trezor/blockchain-link").FiatRates;
37
+ }>;
38
+ getFiatRatesForTimestamps(params: {
39
+ timestamps: number[];
40
+ }): Promise<{
41
+ tickers: {
42
+ ts: number;
43
+ rates: import("@trezor/blockchain-link").FiatRates;
44
+ }[];
45
+ }>;
46
+ getAccountBalanceHistory(params: BlockchainLinkParams<'getAccountBalanceHistory'>): Promise<import("@trezor/blockchain-link").AccountBalanceHistory[]>;
47
+ getNetworkInfo(): Promise<ServerInfo>;
48
+ getAccountInfo(request: BlockchainLinkParams<'getAccountInfo'>): Promise<import("@trezor/blockchain-link").AccountInfo>;
49
+ getAccountUtxo(descriptor: string): Promise<import("@trezor/blockchain-link").Utxo[]>;
50
+ estimateFee(request: {
51
+ blocks?: number[];
52
+ }): Promise<{
53
+ feePerUnit: string;
54
+ feePerTx?: string | undefined;
55
+ feeLimit?: string | undefined;
56
+ }[]>;
57
+ subscribe(accounts?: SubscriptionAccountInfo[]): Promise<{
58
+ subscribed: boolean;
59
+ }>;
60
+ subscribeFiatRates(_currency?: string): Promise<{
61
+ subscribed: boolean;
62
+ }>;
63
+ unsubscribe(accounts?: SubscriptionAccountInfo[]): Promise<{
64
+ subscribed: boolean;
65
+ }>;
66
+ unsubscribeFiatRates(): Promise<{
67
+ subscribed: boolean;
68
+ }>;
69
+ pushTransaction(tx: string): Promise<string>;
70
+ disconnect(): void;
71
+ }
72
+ //# sourceMappingURL=Blockchain.d.ts.map
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Blockchain = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const blockchain_link_1 = tslib_1.__importDefault(require("@trezor/blockchain-link"));
6
+ const events_1 = require("../events");
7
+ const constants_1 = require("../constants");
8
+ const urlUtils_1 = require("../utils/urlUtils");
9
+ const workers_1 = require("../workers/workers");
10
+ const getWorker = (type) => {
11
+ switch (type) {
12
+ case 'blockbook':
13
+ return workers_1.BlockbookWorker;
14
+ case 'ripple':
15
+ return workers_1.RippleWorker;
16
+ case 'blockfrost':
17
+ return workers_1.BlockfrostWorker;
18
+ case 'electrum':
19
+ return workers_1.ElectrumWorker;
20
+ default:
21
+ return null;
22
+ }
23
+ };
24
+ class Blockchain {
25
+ constructor(options) {
26
+ this.feeForBlock = [];
27
+ this.feeTimestamp = 0;
28
+ this.coinInfo = options.coinInfo;
29
+ this.postMessage = options.postMessage;
30
+ this.onConnected = options.onConnected;
31
+ this.onDisconnected = options.onDisconnected;
32
+ const { blockchainLink } = options.coinInfo;
33
+ if (!blockchainLink) {
34
+ throw constants_1.ERRORS.TypedError('Backend_NotSupported');
35
+ }
36
+ const worker = getWorker(blockchainLink.type);
37
+ if (!worker) {
38
+ throw constants_1.ERRORS.TypedError('Backend_WorkerMissing', `BlockchainLink worker not found ${blockchainLink.type}`);
39
+ }
40
+ const { onionDomains } = options;
41
+ this.onionDomains = onionDomains
42
+ ? blockchainLink.url.reduce((a, url) => {
43
+ const onion = (0, urlUtils_1.getOnionDomain)(url, onionDomains);
44
+ if (onion !== url) {
45
+ a[onion] = url;
46
+ }
47
+ return a;
48
+ }, {})
49
+ : {};
50
+ const server = Object.keys(this.onionDomains).length
51
+ ? Object.keys(this.onionDomains)
52
+ : blockchainLink.url;
53
+ this.link = new blockchain_link_1.default({
54
+ name: this.coinInfo.shortcut,
55
+ worker,
56
+ server,
57
+ debug: options.debug,
58
+ proxy: options.proxy,
59
+ });
60
+ }
61
+ onError(error) {
62
+ var _a;
63
+ this.link.dispose();
64
+ this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.ERROR, {
65
+ coin: this.coinInfo,
66
+ error: error.message,
67
+ code: error.code,
68
+ }));
69
+ (_a = this.onDisconnected) === null || _a === void 0 ? void 0 : _a.call(this);
70
+ }
71
+ async init() {
72
+ this.link.on('connected', async () => {
73
+ var _a;
74
+ const info = await this.link.getInfo();
75
+ this.serverInfo = info;
76
+ const shortcut = this.coinInfo.shortcut === 'tXRP' ? 'XRP' : this.coinInfo.shortcut;
77
+ if (info.shortcut.toLowerCase() !== shortcut.toLowerCase()) {
78
+ this.onError(constants_1.ERRORS.TypedError('Backend_Invalid'));
79
+ return;
80
+ }
81
+ const cleanUrl = this.onionDomains[info.url];
82
+ (_a = this.onConnected) === null || _a === void 0 ? void 0 : _a.call(this, cleanUrl || info.url);
83
+ this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.CONNECT, {
84
+ coin: this.coinInfo,
85
+ ...info,
86
+ cleanUrl,
87
+ }));
88
+ });
89
+ this.link.on('disconnected', () => {
90
+ this.onError(constants_1.ERRORS.TypedError('Backend_Disconnected'));
91
+ });
92
+ this.link.on('error', error => {
93
+ this.onError(constants_1.ERRORS.TypedError('Backend_Error', error.message));
94
+ });
95
+ try {
96
+ await this.link.connect();
97
+ }
98
+ catch (error) {
99
+ this.onError(constants_1.ERRORS.TypedError('Backend_Error', error.message));
100
+ throw error;
101
+ }
102
+ }
103
+ getTransactions(txs) {
104
+ return Promise.all(txs.map(id => this.link.getTransaction(id)));
105
+ }
106
+ getCurrentFiatRates(params) {
107
+ return this.link.getCurrentFiatRates(params);
108
+ }
109
+ getFiatRatesForTimestamps(params) {
110
+ return this.link.getFiatRatesForTimestamps(params);
111
+ }
112
+ getAccountBalanceHistory(params) {
113
+ return this.link.getAccountBalanceHistory(params);
114
+ }
115
+ getNetworkInfo() {
116
+ return this.link.getInfo();
117
+ }
118
+ getAccountInfo(request) {
119
+ return this.link.getAccountInfo(request);
120
+ }
121
+ getAccountUtxo(descriptor) {
122
+ return this.link.getAccountUtxo(descriptor);
123
+ }
124
+ async estimateFee(request) {
125
+ const { blocks } = request;
126
+ if (blocks) {
127
+ const now = Date.now();
128
+ const outdated = now - this.feeTimestamp > 20 * 60 * 1000;
129
+ const unknownBlocks = blocks.filter(() => typeof this.feeForBlock !== 'string');
130
+ if (!outdated && unknownBlocks.length < 1) {
131
+ }
132
+ const fees = await this.link.estimateFee(request);
133
+ blocks.forEach((block, index) => {
134
+ this.feeForBlock[block] = fees[index];
135
+ });
136
+ this.feeTimestamp = now;
137
+ return fees;
138
+ }
139
+ return this.link.estimateFee(request);
140
+ }
141
+ async subscribe(accounts) {
142
+ if (this.link.listenerCount('block') === 0) {
143
+ this.link.on('block', block => {
144
+ this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.BLOCK, {
145
+ coin: this.coinInfo,
146
+ ...block,
147
+ }));
148
+ });
149
+ }
150
+ if (this.link.listenerCount('notification') === 0) {
151
+ this.link.on('notification', notification => {
152
+ this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.NOTIFICATION, {
153
+ coin: this.coinInfo,
154
+ notification,
155
+ }));
156
+ });
157
+ }
158
+ const blockSubscription = await this.link.subscribe({ type: 'block' });
159
+ if (!accounts) {
160
+ return blockSubscription;
161
+ }
162
+ return this.link.subscribe({
163
+ type: 'accounts',
164
+ accounts,
165
+ });
166
+ }
167
+ subscribeFiatRates(_currency) {
168
+ if (this.link.listenerCount('fiatRates') === 0) {
169
+ this.link.on('fiatRates', ({ rates }) => {
170
+ this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.FIAT_RATES_UPDATE, {
171
+ coin: this.coinInfo,
172
+ rates,
173
+ }));
174
+ });
175
+ }
176
+ return this.link.subscribe({
177
+ type: 'fiatRates',
178
+ });
179
+ }
180
+ async unsubscribe(accounts) {
181
+ if (!accounts) {
182
+ this.link.removeAllListeners('block');
183
+ this.link.removeAllListeners('fiatRates');
184
+ this.link.removeAllListeners('notification');
185
+ await this.link.unsubscribe({ type: 'fiatRates' });
186
+ return this.link.unsubscribe({ type: 'block' });
187
+ }
188
+ return this.link.unsubscribe({ type: 'accounts', accounts });
189
+ }
190
+ unsubscribeFiatRates() {
191
+ this.link.removeAllListeners('fiatRates');
192
+ return this.link.unsubscribe({ type: 'fiatRates' });
193
+ }
194
+ pushTransaction(tx) {
195
+ return this.link.pushTransaction(tx);
196
+ }
197
+ disconnect() {
198
+ this.link.removeAllListeners();
199
+ this.link.disconnect();
200
+ this.onError(constants_1.ERRORS.TypedError('Backend_Disconnected'));
201
+ }
202
+ }
203
+ exports.Blockchain = Blockchain;
204
+ //# sourceMappingURL=Blockchain.js.map
@@ -1,75 +1,10 @@
1
- import BlockchainLink, { ServerInfo, SubscriptionAccountInfo, BlockchainLinkParams, BlockchainLinkResponse } from '@trezor/blockchain-link';
2
- import { ERRORS } from '../constants';
3
- import type { CoinInfo, Proxy } from '../types';
4
- import type { CoreMessage } from '../events';
5
- declare type Options = {
6
- coinInfo: CoinInfo;
7
- postMessage: (message: CoreMessage) => void;
8
- proxy?: Proxy;
9
- onionDomains?: {
10
- [domain: string]: string;
11
- };
12
- debug?: boolean;
13
- };
14
- export declare class Blockchain {
15
- link: BlockchainLink;
16
- serverInfo?: ServerInfo;
17
- coinInfo: Options['coinInfo'];
18
- onionDomains: {
19
- [onion: string]: string;
20
- };
21
- postMessage: Options['postMessage'];
22
- feeForBlock: BlockchainLinkResponse<'estimateFee'>;
23
- feeTimestamp: number;
24
- constructor(options: Options);
25
- onError(error: ERRORS.TrezorError): void;
26
- init(): Promise<void>;
27
- getTransactions(txs: string[]): Promise<import("@trezor/blockchain-link").TypedRawTransaction[]>;
28
- getCurrentFiatRates(params: {
29
- currencies?: string[];
30
- }): Promise<{
31
- ts: number;
32
- rates: import("@trezor/blockchain-link").FiatRates;
33
- }>;
34
- getFiatRatesForTimestamps(params: {
35
- timestamps: number[];
36
- }): Promise<{
37
- tickers: {
38
- ts: number;
39
- rates: import("@trezor/blockchain-link").FiatRates;
40
- }[];
41
- }>;
42
- getAccountBalanceHistory(params: BlockchainLinkParams<'getAccountBalanceHistory'>): Promise<import("@trezor/blockchain-link").AccountBalanceHistory[]>;
43
- getNetworkInfo(): Promise<ServerInfo>;
44
- getAccountInfo(request: BlockchainLinkParams<'getAccountInfo'>): Promise<import("@trezor/blockchain-link").AccountInfo>;
45
- getAccountUtxo(descriptor: string): Promise<import("@trezor/blockchain-link").Utxo[]>;
46
- estimateFee(request: {
47
- blocks?: number[];
48
- }): Promise<{
49
- feePerUnit: string;
50
- feePerTx?: string | undefined;
51
- feeLimit?: string | undefined;
52
- }[]>;
53
- subscribe(accounts?: SubscriptionAccountInfo[]): Promise<{
54
- subscribed: boolean;
55
- }>;
56
- subscribeFiatRates(_currency?: string): Promise<{
57
- subscribed: boolean;
58
- }>;
59
- unsubscribe(accounts?: SubscriptionAccountInfo[]): Promise<{
60
- subscribed: boolean;
61
- }>;
62
- unsubscribeFiatRates(): Promise<{
63
- subscribed: boolean;
64
- }>;
65
- pushTransaction(tx: string): Promise<string>;
66
- disconnect(): void;
67
- }
68
- export declare const findBackend: (name: string) => Blockchain | null;
1
+ import type { BlockchainOptions as Options } from './Blockchain';
2
+ import type { CoinInfo } from '../types';
3
+ export { Blockchain } from './Blockchain';
4
+ export declare const findBackend: (coin: string) => import("./Blockchain").Blockchain | null;
69
5
  export declare const setCustomBackend: (coinInfo: CoinInfo, blockchainLink: CoinInfo['blockchainLink']) => void;
70
6
  export declare const isBackendSupported: (coinInfo: CoinInfo) => void;
71
- export declare const initBlockchain: (coinInfo: CoinInfo, postMessage: Options['postMessage']) => Promise<Blockchain>;
72
- export declare const reconnectAllBackends: () => Promise<Blockchain[]>;
7
+ export declare const initBlockchain: (coinInfo: CoinInfo, postMessage: Options['postMessage']) => Promise<import("./Blockchain").Blockchain>;
8
+ export declare const reconnectAllBackends: () => Promise<import("./Blockchain").Blockchain[]>;
73
9
  export declare const dispose: () => void;
74
- export {};
75
10
  //# sourceMappingURL=BlockchainLink.d.ts.map
@@ -1,298 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dispose = exports.reconnectAllBackends = exports.initBlockchain = exports.isBackendSupported = exports.setCustomBackend = exports.findBackend = exports.Blockchain = void 0;
4
- const tslib_1 = require("tslib");
5
- const blockchain_link_1 = tslib_1.__importDefault(require("@trezor/blockchain-link"));
6
- const config_1 = require("../data/config");
7
- const DataManager_1 = require("../data/DataManager");
8
- const events_1 = require("../events");
9
- const constants_1 = require("../constants");
10
- const urlUtils_1 = require("../utils/urlUtils");
11
- const workers_1 = require("../workers/workers");
12
- const getWorker = (type) => {
13
- switch (type) {
14
- case 'blockbook':
15
- return workers_1.BlockbookWorker;
16
- case 'ripple':
17
- return workers_1.RippleWorker;
18
- case 'blockfrost':
19
- return workers_1.BlockfrostWorker;
20
- case 'electrum':
21
- return workers_1.ElectrumWorker;
22
- default:
23
- return null;
24
- }
25
- };
26
- class Blockchain {
27
- constructor(options) {
28
- this.feeForBlock = [];
29
- this.feeTimestamp = 0;
30
- this.coinInfo = options.coinInfo;
31
- this.postMessage = options.postMessage;
32
- const { blockchainLink } = options.coinInfo;
33
- if (!blockchainLink) {
34
- throw constants_1.ERRORS.TypedError('Backend_NotSupported');
35
- }
36
- const worker = getWorker(blockchainLink.type);
37
- if (!worker) {
38
- throw constants_1.ERRORS.TypedError('Backend_WorkerMissing', `BlockchainLink worker not found ${blockchainLink.type}`);
39
- }
40
- const { onionDomains } = options;
41
- this.onionDomains = onionDomains
42
- ? blockchainLink.url.reduce((a, url) => {
43
- const onion = (0, urlUtils_1.getOnionDomain)(url, onionDomains);
44
- if (onion !== url) {
45
- a[onion] = url;
46
- }
47
- return a;
48
- }, {})
49
- : {};
50
- const server = Object.keys(this.onionDomains).length
51
- ? Object.keys(this.onionDomains)
52
- : blockchainLink.url;
53
- this.link = new blockchain_link_1.default({
54
- name: this.coinInfo.shortcut,
55
- worker,
56
- server,
57
- debug: options.debug,
58
- proxy: options.proxy,
59
- });
60
- }
61
- onError(error) {
62
- this.link.dispose();
63
- this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.ERROR, {
64
- coin: this.coinInfo,
65
- error: error.message,
66
- code: error.code,
67
- }));
68
- removeBackend(this);
69
- }
70
- async init() {
71
- this.link.on('connected', async () => {
72
- const info = await this.link.getInfo();
73
- this.serverInfo = info;
74
- const shortcut = this.coinInfo.shortcut === 'tXRP' ? 'XRP' : this.coinInfo.shortcut;
75
- if (info.shortcut.toLowerCase() !== shortcut.toLowerCase()) {
76
- this.onError(constants_1.ERRORS.TypedError('Backend_Invalid'));
77
- return;
78
- }
79
- const cleanUrl = this.onionDomains[info.url];
80
- setPreferredBacked(this.coinInfo, cleanUrl || info.url);
81
- this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.CONNECT, {
82
- coin: this.coinInfo,
83
- ...info,
84
- cleanUrl,
85
- }));
86
- });
87
- this.link.on('disconnected', () => {
88
- this.onError(constants_1.ERRORS.TypedError('Backend_Disconnected'));
89
- });
90
- this.link.on('error', error => {
91
- this.onError(constants_1.ERRORS.TypedError('Backend_Error', error.message));
92
- });
93
- try {
94
- await this.link.connect();
95
- }
96
- catch (error) {
97
- this.onError(constants_1.ERRORS.TypedError('Backend_Error', error.message));
98
- throw error;
99
- }
100
- }
101
- getTransactions(txs) {
102
- return Promise.all(txs.map(id => this.link.getTransaction(id)));
103
- }
104
- getCurrentFiatRates(params) {
105
- return this.link.getCurrentFiatRates(params);
106
- }
107
- getFiatRatesForTimestamps(params) {
108
- return this.link.getFiatRatesForTimestamps(params);
109
- }
110
- getAccountBalanceHistory(params) {
111
- return this.link.getAccountBalanceHistory(params);
112
- }
113
- getNetworkInfo() {
114
- return this.link.getInfo();
115
- }
116
- getAccountInfo(request) {
117
- return this.link.getAccountInfo(request);
118
- }
119
- getAccountUtxo(descriptor) {
120
- return this.link.getAccountUtxo(descriptor);
121
- }
122
- async estimateFee(request) {
123
- const { blocks } = request;
124
- if (blocks) {
125
- const now = Date.now();
126
- const outdated = now - this.feeTimestamp > 20 * 60 * 1000;
127
- const unknownBlocks = blocks.filter(() => typeof this.feeForBlock !== 'string');
128
- if (!outdated && unknownBlocks.length < 1) {
129
- }
130
- const fees = await this.link.estimateFee(request);
131
- blocks.forEach((block, index) => {
132
- this.feeForBlock[block] = fees[index];
133
- });
134
- this.feeTimestamp = now;
135
- return fees;
136
- }
137
- return this.link.estimateFee(request);
138
- }
139
- async subscribe(accounts) {
140
- if (this.link.listenerCount('block') === 0) {
141
- this.link.on('block', block => {
142
- this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.BLOCK, {
143
- coin: this.coinInfo,
144
- ...block,
145
- }));
146
- });
147
- }
148
- if (this.link.listenerCount('notification') === 0) {
149
- this.link.on('notification', notification => {
150
- this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.NOTIFICATION, {
151
- coin: this.coinInfo,
152
- notification,
153
- }));
154
- });
155
- }
156
- const blockSubscription = await this.link.subscribe({ type: 'block' });
157
- if (!accounts) {
158
- return blockSubscription;
159
- }
160
- return this.link.subscribe({
161
- type: 'accounts',
162
- accounts,
163
- });
164
- }
165
- subscribeFiatRates(_currency) {
166
- if (this.link.listenerCount('fiatRates') === 0) {
167
- this.link.on('fiatRates', ({ rates }) => {
168
- this.postMessage((0, events_1.createBlockchainMessage)(events_1.BLOCKCHAIN.FIAT_RATES_UPDATE, {
169
- coin: this.coinInfo,
170
- rates,
171
- }));
172
- });
173
- }
174
- return this.link.subscribe({
175
- type: 'fiatRates',
176
- });
177
- }
178
- async unsubscribe(accounts) {
179
- if (!accounts) {
180
- this.link.removeAllListeners('block');
181
- this.link.removeAllListeners('fiatRates');
182
- this.link.removeAllListeners('notification');
183
- await this.link.unsubscribe({ type: 'fiatRates' });
184
- return this.link.unsubscribe({ type: 'block' });
185
- }
186
- return this.link.unsubscribe({ type: 'accounts', accounts });
187
- }
188
- unsubscribeFiatRates() {
189
- this.link.removeAllListeners('fiatRates');
190
- return this.link.unsubscribe({ type: 'fiatRates' });
191
- }
192
- pushTransaction(tx) {
193
- return this.link.pushTransaction(tx);
194
- }
195
- disconnect() {
196
- this.link.removeAllListeners();
197
- this.link.disconnect();
198
- this.onError(constants_1.ERRORS.TypedError('Backend_Disconnected'));
199
- }
200
- }
201
- exports.Blockchain = Blockchain;
202
- const instances = [];
203
- const customBackends = {};
204
- const preferredBackends = {};
205
- const removeBackend = (backend) => {
206
- const index = instances.indexOf(backend);
207
- if (index >= 0) {
208
- instances.splice(index, 1);
209
- }
210
- };
211
- const findBackend = (name) => {
212
- for (let i = 0; i < instances.length; i++) {
213
- if (instances[i].coinInfo.name === name) {
214
- return instances[i];
215
- }
216
- }
217
- return null;
218
- };
4
+ const BackendManager_1 = require("./BackendManager");
5
+ var Blockchain_1 = require("./Blockchain");
6
+ Object.defineProperty(exports, "Blockchain", { enumerable: true, get: function () { return Blockchain_1.Blockchain; } });
7
+ const backends = new BackendManager_1.BackendManager();
8
+ const findBackend = (coin) => backends.get(coin);
219
9
  exports.findBackend = findBackend;
220
- const setPreferredBacked = (coinInfo, url) => {
221
- if (!url) {
222
- delete preferredBackends[coinInfo.shortcut];
223
- }
224
- else if (coinInfo.blockchainLink) {
225
- preferredBackends[coinInfo.shortcut] = {
226
- ...coinInfo,
227
- blockchainLink: {
228
- ...coinInfo.blockchainLink,
229
- url: [url],
230
- },
231
- };
232
- }
233
- };
234
- const setCustomBackend = (coinInfo, blockchainLink) => {
235
- setPreferredBacked(coinInfo);
236
- if (!blockchainLink || blockchainLink.url.length === 0) {
237
- delete customBackends[coinInfo.shortcut];
238
- }
239
- else {
240
- customBackends[coinInfo.shortcut] = {
241
- ...coinInfo,
242
- blockchainLink,
243
- };
244
- }
245
- };
10
+ const setCustomBackend = (coinInfo, blockchainLink) => (blockchainLink === null || blockchainLink === void 0 ? void 0 : blockchainLink.url.length)
11
+ ? backends.setCustom(coinInfo.shortcut, blockchainLink)
12
+ : backends.removeCustom(coinInfo.shortcut);
246
13
  exports.setCustomBackend = setCustomBackend;
247
- const isBackendSupported = (coinInfo) => {
248
- const info = customBackends[coinInfo.shortcut] || coinInfo;
249
- if (!info.blockchainLink) {
250
- throw constants_1.ERRORS.TypedError('Backend_NotSupported');
251
- }
252
- };
14
+ const isBackendSupported = (coinInfo) => backends.isSupported(coinInfo);
253
15
  exports.isBackendSupported = isBackendSupported;
254
- const initBlockchain = async (coinInfo, postMessage) => {
255
- let backend = (0, exports.findBackend)(coinInfo.name);
256
- if (!backend) {
257
- backend = new Blockchain({
258
- coinInfo: preferredBackends[coinInfo.shortcut] ||
259
- customBackends[coinInfo.shortcut] ||
260
- coinInfo,
261
- postMessage,
262
- debug: DataManager_1.DataManager.getSettings('debug'),
263
- proxy: DataManager_1.DataManager.getSettings('proxy'),
264
- onionDomains: DataManager_1.DataManager.getSettings('useOnionLinks') && !customBackends[coinInfo.shortcut]
265
- ? config_1.config.onionDomains
266
- : undefined,
267
- });
268
- instances.push(backend);
269
- try {
270
- await backend.init();
271
- }
272
- catch (error) {
273
- removeBackend(backend);
274
- setPreferredBacked(coinInfo);
275
- throw error;
276
- }
277
- }
278
- return backend;
279
- };
16
+ const initBlockchain = (coinInfo, postMessage) => backends.getOrConnect(coinInfo, postMessage);
280
17
  exports.initBlockchain = initBlockchain;
281
- const reconnectAllBackends = () => {
282
- const params = instances.map(i => [
283
- i.coinInfo,
284
- i.postMessage,
285
- ]);
286
- while (instances.length > 0) {
287
- instances[0].disconnect();
288
- }
289
- return Promise.all(params.map(p => (0, exports.initBlockchain)(...p)));
290
- };
18
+ const reconnectAllBackends = () => backends.reconnectAll();
291
19
  exports.reconnectAllBackends = reconnectAllBackends;
292
- const dispose = () => {
293
- while (instances.length > 0) {
294
- instances[0].disconnect();
295
- }
296
- };
20
+ const dispose = () => backends.dispose();
297
21
  exports.dispose = dispose;
298
22
  //# sourceMappingURL=BlockchainLink.js.map
@@ -1,6 +1,8 @@
1
1
  export declare enum PROTOCOL_MAGICS {
2
2
  mainnet = 764824073,
3
- testnet = 1097911063
3
+ testnet_preprod = 1,
4
+ testnet_preview = 2,
5
+ testnet_legacy = 1097911063
4
6
  }
5
7
  export declare enum NETWORK_IDS {
6
8
  mainnet = 1,
@@ -4,7 +4,9 @@ exports.NETWORK_IDS = exports.PROTOCOL_MAGICS = void 0;
4
4
  var PROTOCOL_MAGICS;
5
5
  (function (PROTOCOL_MAGICS) {
6
6
  PROTOCOL_MAGICS[PROTOCOL_MAGICS["mainnet"] = 764824073] = "mainnet";
7
- PROTOCOL_MAGICS[PROTOCOL_MAGICS["testnet"] = 1097911063] = "testnet";
7
+ PROTOCOL_MAGICS[PROTOCOL_MAGICS["testnet_preprod"] = 1] = "testnet_preprod";
8
+ PROTOCOL_MAGICS[PROTOCOL_MAGICS["testnet_preview"] = 2] = "testnet_preview";
9
+ PROTOCOL_MAGICS[PROTOCOL_MAGICS["testnet_legacy"] = 1097911063] = "testnet_legacy";
8
10
  })(PROTOCOL_MAGICS = exports.PROTOCOL_MAGICS || (exports.PROTOCOL_MAGICS = {}));
9
11
  var NETWORK_IDS;
10
12
  (function (NETWORK_IDS) {