@otoplo/wallet-common 0.6.1 → 0.7.0
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.
- package/dist/index.d.ts +33 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +87 -45
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/services/rostrum.ts +322 -274
- package/src/services/wallet.ts +7 -3
- package/src/types/rostrum.types.ts +11 -0
package/package.json
CHANGED
package/src/services/rostrum.ts
CHANGED
|
@@ -1,275 +1,323 @@
|
|
|
1
|
-
import type { RequestResponse } from "@otoplo/electrum-client";
|
|
2
|
-
import { ConnectionStatus, ElectrumClient, TransportScheme } from "@otoplo/electrum-client";
|
|
3
|
-
import type { BlockTip, IFirstUse, IListUnspentRecord, ITemplateScript, ITokensBalance, ITokenUtxo, ITransaction, ITXHistory, IUtxo, RostrumParams, ServerFeatures } from "../types/rostrum.types";
|
|
4
|
-
import type { Balance } from "../types/wallet.types";
|
|
5
|
-
import { isTestnet } from "../utils/common";
|
|
6
|
-
import type { WalletDB } from "../persistence";
|
|
7
|
-
|
|
8
|
-
type RPCParameter = object | string | number | boolean | null;
|
|
9
|
-
type PagedResult = { cursor?: string | null; [key: string]: unknown };
|
|
10
|
-
|
|
11
|
-
export type RostrumEvent =
|
|
12
|
-
| { type: 'connected' }
|
|
13
|
-
| { type: 'disconnected' };
|
|
14
|
-
|
|
15
|
-
export type RostrumUpdateCallback = (event: RostrumEvent) => void;
|
|
16
|
-
|
|
17
|
-
export class RostrumService {
|
|
18
|
-
|
|
19
|
-
private readonly walletDb: WalletDB;
|
|
20
|
-
|
|
21
|
-
private client?: ElectrumClient;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
1
|
+
import type { RequestResponse } from "@otoplo/electrum-client";
|
|
2
|
+
import { ConnectionStatus, ElectrumClient, TransportScheme } from "@otoplo/electrum-client";
|
|
3
|
+
import type { BlockTip, IFirstUse, IListUnspentRecord, ITemplateScript, ITokensBalance, ITokenUtxo, ITransaction, ITXHistory, IUtxo, RostrumInstanceCheck, RostrumParams, ServerFeatures } from "../types/rostrum.types";
|
|
4
|
+
import type { Balance } from "../types/wallet.types";
|
|
5
|
+
import { isGenesisHashValid, isTestnet } from "../utils/common";
|
|
6
|
+
import type { WalletDB } from "../persistence";
|
|
7
|
+
|
|
8
|
+
type RPCParameter = object | string | number | boolean | null;
|
|
9
|
+
type PagedResult = { cursor?: string | null; [key: string]: unknown };
|
|
10
|
+
|
|
11
|
+
export type RostrumEvent =
|
|
12
|
+
| { type: 'connected' }
|
|
13
|
+
| { type: 'disconnected' };
|
|
14
|
+
|
|
15
|
+
export type RostrumUpdateCallback = (event: RostrumEvent) => void;
|
|
16
|
+
|
|
17
|
+
export class RostrumService {
|
|
18
|
+
|
|
19
|
+
private readonly walletDb: WalletDB;
|
|
20
|
+
|
|
21
|
+
private client?: ElectrumClient;
|
|
22
|
+
|
|
23
|
+
/** The server `client` connects to. */
|
|
24
|
+
private params?: RostrumParams;
|
|
25
|
+
|
|
26
|
+
private updateCallback?: RostrumUpdateCallback;
|
|
27
|
+
|
|
28
|
+
public constructor(walletDb: WalletDB) {
|
|
29
|
+
this.walletDb = walletDb;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public onUpdate(callback: RostrumUpdateCallback): void {
|
|
33
|
+
this.updateCallback = callback;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private notify(event: RostrumEvent): void {
|
|
37
|
+
this.updateCallback?.(event);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public isConnected(): boolean {
|
|
41
|
+
return this.client?.status === ConnectionStatus.CONNECTED;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public getFeatures(): Promise<ServerFeatures> {
|
|
45
|
+
return this.execute<ServerFeatures>('server.features');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public getBlockTip(): Promise<BlockTip> {
|
|
49
|
+
return this.execute<BlockTip>('blockchain.headers.tip');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public async getBalance(address: string): Promise<Balance> {
|
|
53
|
+
const balance = await this.execute<Balance>('blockchain.address.get_balance', address, 'exclude_tokens');
|
|
54
|
+
return { confirmed: BigInt(balance.confirmed).toString(), unconfirmed: BigInt(balance.unconfirmed).toString() };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public getTransactionsHistory(address: string, fromHeight = 0): Promise<ITXHistory[]> {
|
|
58
|
+
return this.paginate<ITXHistory>('blockchain.address.get_history', 'history', cursor =>
|
|
59
|
+
[address, { from_height: fromHeight }, cursor]
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public listTemplateScripts(template: string, constraint: string | null = null): Promise<ITemplateScript[]> {
|
|
64
|
+
return this.paginate<ITemplateScript>('blockchain.script.list_template', 'scripts', cursor =>
|
|
65
|
+
[template, constraint, cursor]
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public getFirstUse(address: string): Promise<IFirstUse> {
|
|
70
|
+
return this.execute<IFirstUse>('blockchain.address.get_first_use', address);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public async isAddressUsed(address: string): Promise<boolean> {
|
|
74
|
+
try {
|
|
75
|
+
const firstUse = await this.getFirstUse(address);
|
|
76
|
+
return !!(firstUse.tx_hash && firstUse.tx_hash !== "");
|
|
77
|
+
} catch (e) {
|
|
78
|
+
if (e instanceof Error && e.message.includes("not found")) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public getTransaction(id: string, verbose = true): Promise<ITransaction> {
|
|
86
|
+
return this.execute<ITransaction>('blockchain.transaction.get', id, verbose);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public getUtxo(outpoint: string): Promise<IUtxo> {
|
|
90
|
+
return this.execute<IUtxo>('blockchain.utxo.get', outpoint);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public getNexaUtxos(address: string): Promise<IListUnspentRecord[]> {
|
|
94
|
+
return this.paginate<IListUnspentRecord>('blockchain.address.listunspent', 'unspent', cursor =>
|
|
95
|
+
[address, 'exclude_tokens', cursor]
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public getTokenUtxos(address: string, token: string): Promise<ITokenUtxo[]> {
|
|
100
|
+
return this.paginate<ITokenUtxo>('token.address.listunspent', 'unspent', cursor =>
|
|
101
|
+
[address, cursor, token]
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public async getTokensBalance(address: string): Promise<Record<string, Balance>> {
|
|
106
|
+
const tokensBalance = await this.execute<ITokensBalance>('token.address.get_balance', address);
|
|
107
|
+
const balance: Record<string, Balance> = {};
|
|
108
|
+
|
|
109
|
+
for (const cToken in tokensBalance.confirmed) {
|
|
110
|
+
if (tokensBalance.confirmed[cToken] != 0) {
|
|
111
|
+
balance[cToken] = { confirmed: BigInt(tokensBalance.confirmed[cToken]).toString(), unconfirmed: "0" }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const uToken in tokensBalance.unconfirmed) {
|
|
116
|
+
if (tokensBalance.unconfirmed[uToken] != 0) {
|
|
117
|
+
if (balance[uToken]) {
|
|
118
|
+
balance[uToken].unconfirmed = BigInt(tokensBalance.unconfirmed[uToken]).toString();
|
|
119
|
+
} else {
|
|
120
|
+
balance[uToken] = { confirmed: "0", unconfirmed: BigInt(tokensBalance.unconfirmed[uToken]).toString() }
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return balance;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public broadcast(txHex: string): Promise<string> {
|
|
129
|
+
return this.execute<string>('blockchain.transaction.broadcast', txHex);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public subscribeHeaders(handler: (block: number) => void): Promise<RequestResponse> {
|
|
133
|
+
return this.client!.subscribe(([header]) => {
|
|
134
|
+
const height = (header as BlockTip | null)?.height;
|
|
135
|
+
handler(typeof height === 'number' ? height : 0);
|
|
136
|
+
}, 'blockchain.headers.subscribe');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public subscribeAddress(address: string, handler: (address: string, status: string | null) => void): Promise<RequestResponse> {
|
|
140
|
+
return this.client!.subscribe(([addr, status]) => handler(addr as string, status as string | null), 'blockchain.address.subscribe', address);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public async getLatency(): Promise<number> {
|
|
144
|
+
try {
|
|
145
|
+
const start = Date.now();
|
|
146
|
+
const res = await this.getBlockTip();
|
|
147
|
+
if (res) {
|
|
148
|
+
return Date.now() - start;
|
|
149
|
+
}
|
|
150
|
+
return 0;
|
|
151
|
+
} catch {
|
|
152
|
+
return 0;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public async connect(params?: RostrumParams): Promise<void> {
|
|
157
|
+
try {
|
|
158
|
+
await this.disconnect();
|
|
159
|
+
|
|
160
|
+
if (!params) {
|
|
161
|
+
params = await this.getCurrentInstance();
|
|
162
|
+
}
|
|
163
|
+
this.params = params;
|
|
164
|
+
|
|
165
|
+
this.client = new ElectrumClient({
|
|
166
|
+
application: "com.otoplo.wallet",
|
|
167
|
+
version: "1.4.3",
|
|
168
|
+
host: params.host,
|
|
169
|
+
port: params.port,
|
|
170
|
+
scheme: params.scheme,
|
|
171
|
+
});
|
|
172
|
+
this.client.on('connected', () => this.notify({ type: 'connected' }));
|
|
173
|
+
this.client.on('disconnected', () => this.notify({ type: 'disconnected' }));
|
|
174
|
+
await this.client.connect();
|
|
175
|
+
} catch (e) {
|
|
176
|
+
if (e instanceof Error) {
|
|
177
|
+
console.info(e.message);
|
|
178
|
+
} else {
|
|
179
|
+
console.error(e);
|
|
180
|
+
}
|
|
181
|
+
throw e;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public async disconnect(): Promise<void> {
|
|
186
|
+
await this.client?.disconnect();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Checks whether the wallet can use a server (e.g. before switching to it): that it's reachable, on the wallet's
|
|
191
|
+
* network and indexes scripts. Never throws.
|
|
192
|
+
*
|
|
193
|
+
* The server this service is connected to answers on its connection; any other one is checked on a temporary
|
|
194
|
+
* connection, closed after.
|
|
195
|
+
*/
|
|
196
|
+
public async checkInstance(params: RostrumParams): Promise<RostrumInstanceCheck> {
|
|
197
|
+
if (this.isConnected() && this.params && RostrumService.isSameInstance(this.params, params)) {
|
|
198
|
+
return this.check();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const probe = new RostrumService(this.walletDb);
|
|
202
|
+
try {
|
|
203
|
+
await probe.connect(params);
|
|
204
|
+
return await probe.check();
|
|
205
|
+
} catch {
|
|
206
|
+
return { status: 'offline' };
|
|
207
|
+
} finally {
|
|
208
|
+
await probe.disconnect();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private async check(): Promise<RostrumInstanceCheck> {
|
|
213
|
+
try {
|
|
214
|
+
const features = await this.getFeatures();
|
|
215
|
+
if (!isGenesisHashValid(features.genesis_hash)) {
|
|
216
|
+
return { status: 'wrong_network' };
|
|
217
|
+
}
|
|
218
|
+
if (!features.script_index) {
|
|
219
|
+
return { status: 'unsupported' };
|
|
220
|
+
}
|
|
221
|
+
const latency = await this.getLatency();
|
|
222
|
+
return latency ? { status: 'online', latency } : { status: 'offline' };
|
|
223
|
+
} catch {
|
|
224
|
+
return { status: 'offline' };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Walk a cursor-paginated rostrum query: start with "start" and follow the returned cursor until it is null.
|
|
230
|
+
* If the cursor expires mid-walk (e.g. reorg / mempool change), the walk is restarted from the beginning.
|
|
231
|
+
*/
|
|
232
|
+
private async paginate<T>(method: string, key: string, buildParams: (cursor: string) => RPCParameter[], maxRestarts = 3): Promise<T[]> {
|
|
233
|
+
for (let attempt = 0; ; attempt++) {
|
|
234
|
+
try {
|
|
235
|
+
const results: T[] = [];
|
|
236
|
+
let cursor: string | null = 'start';
|
|
237
|
+
while (cursor) {
|
|
238
|
+
const page: PagedResult = await this.execute<PagedResult>(method, ...buildParams(cursor));
|
|
239
|
+
results.push(...((page[key] as T[] | undefined) ?? []));
|
|
240
|
+
cursor = page.cursor ?? null;
|
|
241
|
+
}
|
|
242
|
+
return results;
|
|
243
|
+
} catch (e) {
|
|
244
|
+
if (attempt < maxRestarts && e instanceof Error && e.message.includes('cursor expired')) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
throw e;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private async execute<T>(method: string, ...parameters: RPCParameter[]): Promise<T> {
|
|
253
|
+
await this.waitForConnection();
|
|
254
|
+
return this.client!.request<T>(method, ...parameters);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private waitForConnection(timeout = 5000): Promise<void> {
|
|
258
|
+
const start = Date.now();
|
|
259
|
+
|
|
260
|
+
return new Promise((resolve, reject) => {
|
|
261
|
+
const check = (): void => {
|
|
262
|
+
if (this.isConnected()) {
|
|
263
|
+
return resolve();
|
|
264
|
+
}
|
|
265
|
+
if (Date.now() - start > timeout) {
|
|
266
|
+
return reject(new Error("Rostrum Connection timeout"));
|
|
267
|
+
}
|
|
268
|
+
setTimeout(check, 250);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
check();
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
public setCurrentInstance(params: RostrumParams): Promise<void> {
|
|
276
|
+
return this.walletDb.saveSetting("rostrum-params", JSON.stringify(params));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
public resetCurrentInstance(): Promise<void> {
|
|
280
|
+
return this.walletDb.removeSetting("rostrum-params");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
public async getCurrentInstance(): Promise<RostrumParams> {
|
|
284
|
+
const params = await this.walletDb.getSetting("rostrum-params");
|
|
285
|
+
if (params) {
|
|
286
|
+
return JSON.parse(params);
|
|
287
|
+
}
|
|
288
|
+
return RostrumService.getPredefinedInstances()[0];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Whether both point to the same server (the label aside). */
|
|
292
|
+
public static isSameInstance(a: RostrumParams, b: RostrumParams): boolean {
|
|
293
|
+
return a.scheme === b.scheme && a.host.toLowerCase() === b.host.toLowerCase() && a.port === b.port;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
public static getPredefinedInstances(): RostrumParams[] {
|
|
297
|
+
if (isTestnet()) {
|
|
298
|
+
return [
|
|
299
|
+
{
|
|
300
|
+
scheme: TransportScheme.WSS,
|
|
301
|
+
host: 'testnet-electrum.nexa.org',
|
|
302
|
+
port: 30004,
|
|
303
|
+
label: 'NexaOrg'
|
|
304
|
+
}
|
|
305
|
+
];
|
|
306
|
+
} else {
|
|
307
|
+
return [
|
|
308
|
+
{
|
|
309
|
+
scheme: TransportScheme.WSS,
|
|
310
|
+
host: 'rostrum.otoplo.com',
|
|
311
|
+
port: 443,
|
|
312
|
+
label: 'Otoplo'
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
scheme: TransportScheme.WSS,
|
|
316
|
+
host: 'electrum.nexa.org',
|
|
317
|
+
port: 20004,
|
|
318
|
+
label: 'NexaOrg'
|
|
319
|
+
}
|
|
320
|
+
];
|
|
321
|
+
}
|
|
322
|
+
}
|
|
275
323
|
}
|
package/src/services/wallet.ts
CHANGED
|
@@ -144,14 +144,18 @@ export class WalletManager {
|
|
|
144
144
|
}));
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Switches to another server (or reconnects to the current one without `params`): saves it, disconnects, then
|
|
149
|
+
* connects with `connect()`, retrying until it does. Check the server first with `RostrumService.checkInstance()`.
|
|
150
|
+
*
|
|
151
|
+
* @returns resolves like `connect()`: once connected and synced, or when stopped by `disconnectRostrum()`.
|
|
152
|
+
*/
|
|
147
153
|
public async reloadRostrum(params?: RostrumParams): Promise<void> {
|
|
148
154
|
if (params) {
|
|
149
155
|
await this.rostrumService.setCurrentInstance(params);
|
|
150
156
|
}
|
|
151
157
|
await this.disconnectRostrum();
|
|
152
|
-
|
|
153
|
-
this.startUpdates();
|
|
154
|
-
await this.subscribeAllAddresses();
|
|
158
|
+
return this.connect();
|
|
155
159
|
}
|
|
156
160
|
|
|
157
161
|
public async initRostrum(): Promise<void> {
|
|
@@ -7,6 +7,17 @@ export interface RostrumParams {
|
|
|
7
7
|
label: string;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Whether the wallet can use a server, from `RostrumService.checkInstance()`:
|
|
12
|
+
* - `online`: reachable and usable, with the round-trip time of a request in milliseconds.
|
|
13
|
+
* - `offline`: couldn't connect, or it didn't answer.
|
|
14
|
+
* - `wrong_network`: it serves another network (e.g. testnet while the wallet is on mainnet).
|
|
15
|
+
* - `unsupported`: it doesn't index scripts (`script_index`), which the wallet needs.
|
|
16
|
+
*/
|
|
17
|
+
export type RostrumInstanceCheck =
|
|
18
|
+
| { status: 'online'; latency: number }
|
|
19
|
+
| { status: 'offline' | 'wrong_network' | 'unsupported' };
|
|
20
|
+
|
|
10
21
|
export interface ServerFeatures {
|
|
11
22
|
genesis_hash: string;
|
|
12
23
|
server_version: string;
|