@otoplo/wallet-common 0.5.2 → 0.6.1
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 +889 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +259 -195
- package/dist/index.js.map +1 -1
- package/package.json +8 -9
- package/src/services/rostrum.ts +42 -22
- package/src/services/wallet.ts +758 -694
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/persistence/index.d.ts +0 -3
- package/dist/types/persistence/index.d.ts.map +0 -1
- package/dist/types/persistence/repository.d.ts +0 -43
- package/dist/types/persistence/repository.d.ts.map +0 -1
- package/dist/types/persistence/wallet-db.d.ts +0 -44
- package/dist/types/persistence/wallet-db.d.ts.map +0 -1
- package/dist/types/services/asset.d.ts +0 -21
- package/dist/types/services/asset.d.ts.map +0 -1
- package/dist/types/services/cache.d.ts +0 -14
- package/dist/types/services/cache.d.ts.map +0 -1
- package/dist/types/services/discovery.d.ts +0 -18
- package/dist/types/services/discovery.d.ts.map +0 -1
- package/dist/types/services/index.d.ts +0 -10
- package/dist/types/services/index.d.ts.map +0 -1
- package/dist/types/services/key.d.ts +0 -17
- package/dist/types/services/key.d.ts.map +0 -1
- package/dist/types/services/market.d.ts +0 -33
- package/dist/types/services/market.d.ts.map +0 -1
- package/dist/types/services/rostrum.d.ts +0 -39
- package/dist/types/services/rostrum.d.ts.map +0 -1
- package/dist/types/services/session.d.ts +0 -48
- package/dist/types/services/session.d.ts.map +0 -1
- package/dist/types/services/transaction.d.ts +0 -41
- package/dist/types/services/transaction.d.ts.map +0 -1
- package/dist/types/services/tx-transformer.d.ts +0 -22
- package/dist/types/services/tx-transformer.d.ts.map +0 -1
- package/dist/types/services/wallet.d.ts +0 -107
- package/dist/types/services/wallet.d.ts.map +0 -1
- package/dist/types/types/dapp.types.d.ts +0 -19
- package/dist/types/types/dapp.types.d.ts.map +0 -1
- package/dist/types/types/db.types.d.ts +0 -138
- package/dist/types/types/db.types.d.ts.map +0 -1
- package/dist/types/types/index.d.ts +0 -6
- package/dist/types/types/index.d.ts.map +0 -1
- package/dist/types/types/notification.types.d.ts +0 -21
- package/dist/types/types/notification.types.d.ts.map +0 -1
- package/dist/types/types/rostrum.types.d.ts +0 -132
- package/dist/types/types/rostrum.types.d.ts.map +0 -1
- package/dist/types/types/wallet.types.d.ts +0 -80
- package/dist/types/types/wallet.types.d.ts.map +0 -1
- package/dist/types/utils/asset.d.ts +0 -5
- package/dist/types/utils/asset.d.ts.map +0 -1
- package/dist/types/utils/common.d.ts +0 -64
- package/dist/types/utils/common.d.ts.map +0 -1
- package/dist/types/utils/enums.d.ts +0 -16
- package/dist/types/utils/enums.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts +0 -7
- package/dist/types/utils/index.d.ts.map +0 -1
- package/dist/types/utils/keypath.d.ts +0 -5
- package/dist/types/utils/keypath.d.ts.map +0 -1
- package/dist/types/utils/seed.d.ts +0 -5
- package/dist/types/utils/seed.d.ts.map +0 -1
- package/dist/types/utils/vault.d.ts +0 -10
- package/dist/types/utils/vault.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@otoplo/wallet-common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Shared common library for internal use in Otoplo wallet",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "vgrunner",
|
|
7
7
|
"type": "module",
|
|
8
|
-
"types": "dist/
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
9
|
"main": "dist/index.js",
|
|
10
10
|
"module": "dist/index.js",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
-
"types": "./dist/
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
14
|
"default": "./dist/index.js"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
@@ -24,23 +24,22 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"check": "tsc --noEmit",
|
|
26
26
|
"lint": "eslint .",
|
|
27
|
-
"build": "
|
|
27
|
+
"build": "tsdown"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
30
30
|
"type": "git",
|
|
31
31
|
"url": "git+https://gitlab.com/nexa/otoplo/otoplo-wallet-common.git"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@otoplo/electrum-client": "^
|
|
34
|
+
"@otoplo/electrum-client": "^2.0.0",
|
|
35
35
|
"@scure/bip39": "^2.4.0",
|
|
36
36
|
"js-big-decimal": "^2.2.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"eslint": "^10.
|
|
39
|
+
"eslint": "^10.16.0",
|
|
40
|
+
"tsdown": "^0.23.7",
|
|
40
41
|
"typescript": "^6.0.3",
|
|
41
|
-
"typescript-eslint": "^8.
|
|
42
|
-
"unplugin-dts": "^1.1.2",
|
|
43
|
-
"vite": "^8.3.8"
|
|
42
|
+
"typescript-eslint": "^8.74.0"
|
|
44
43
|
},
|
|
45
44
|
"peerDependencies": {
|
|
46
45
|
"libnexa-ts": "^4.0.0",
|
package/src/services/rostrum.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RequestResponse} from "@otoplo/electrum-client";
|
|
1
|
+
import type { RequestResponse } from "@otoplo/electrum-client";
|
|
2
2
|
import { ConnectionStatus, ElectrumClient, TransportScheme } from "@otoplo/electrum-client";
|
|
3
3
|
import type { BlockTip, IFirstUse, IListUnspentRecord, ITemplateScript, ITokensBalance, ITokenUtxo, ITransaction, ITXHistory, IUtxo, RostrumParams, ServerFeatures } from "../types/rostrum.types";
|
|
4
4
|
import type { Balance } from "../types/wallet.types";
|
|
@@ -8,15 +8,35 @@ import type { WalletDB } from "../persistence";
|
|
|
8
8
|
type RPCParameter = object | string | number | boolean | null;
|
|
9
9
|
type PagedResult = { cursor?: string | null; [key: string]: unknown };
|
|
10
10
|
|
|
11
|
+
export type RostrumEvent =
|
|
12
|
+
| { type: 'connected' }
|
|
13
|
+
| { type: 'disconnected' };
|
|
14
|
+
|
|
15
|
+
export type RostrumUpdateCallback = (event: RostrumEvent) => void;
|
|
16
|
+
|
|
11
17
|
export class RostrumService {
|
|
12
18
|
|
|
13
19
|
private readonly walletDb: WalletDB;
|
|
14
20
|
|
|
15
21
|
private client?: ElectrumClient;
|
|
16
22
|
|
|
23
|
+
private updateCallback?: RostrumUpdateCallback;
|
|
24
|
+
|
|
17
25
|
public constructor(walletDb: WalletDB) {
|
|
18
26
|
this.walletDb = walletDb;
|
|
19
27
|
}
|
|
28
|
+
|
|
29
|
+
public onUpdate(callback: RostrumUpdateCallback): void {
|
|
30
|
+
this.updateCallback = callback;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private notify(event: RostrumEvent): void {
|
|
34
|
+
this.updateCallback?.(event);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public isConnected(): boolean {
|
|
38
|
+
return this.client?.status === ConnectionStatus.CONNECTED;
|
|
39
|
+
}
|
|
20
40
|
|
|
21
41
|
public getFeatures(): Promise<ServerFeatures> {
|
|
22
42
|
return this.execute<ServerFeatures>('server.features');
|
|
@@ -106,16 +126,15 @@ export class RostrumService {
|
|
|
106
126
|
return this.execute<string>('blockchain.transaction.broadcast', txHex);
|
|
107
127
|
}
|
|
108
128
|
|
|
109
|
-
public subscribeHeaders(handler: (block: number) => void): Promise<
|
|
110
|
-
return this.client!.subscribe((
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
handler(height);
|
|
129
|
+
public subscribeHeaders(handler: (block: number) => void): Promise<RequestResponse> {
|
|
130
|
+
return this.client!.subscribe(([header]) => {
|
|
131
|
+
const height = (header as BlockTip | null)?.height;
|
|
132
|
+
handler(typeof height === 'number' ? height : 0);
|
|
114
133
|
}, 'blockchain.headers.subscribe');
|
|
115
134
|
}
|
|
116
135
|
|
|
117
|
-
public subscribeAddress(address: string, handler: (
|
|
118
|
-
return this.client!.subscribe(handler, 'blockchain.address.subscribe', address);
|
|
136
|
+
public subscribeAddress(address: string, handler: (address: string, status: string | null) => void): Promise<RequestResponse> {
|
|
137
|
+
return this.client!.subscribe(([addr, status]) => handler(addr as string, status as string | null), 'blockchain.address.subscribe', address);
|
|
119
138
|
}
|
|
120
139
|
|
|
121
140
|
public async getLatency(): Promise<number> {
|
|
@@ -133,11 +152,21 @@ export class RostrumService {
|
|
|
133
152
|
|
|
134
153
|
public async connect(params?: RostrumParams): Promise<void> {
|
|
135
154
|
try {
|
|
155
|
+
await this.disconnect();
|
|
156
|
+
|
|
136
157
|
if (!params) {
|
|
137
158
|
params = await this.getCurrentInstance();
|
|
138
159
|
}
|
|
139
160
|
|
|
140
|
-
this.client = new ElectrumClient(
|
|
161
|
+
this.client = new ElectrumClient({
|
|
162
|
+
application: "com.otoplo.wallet",
|
|
163
|
+
version: "1.4.3",
|
|
164
|
+
host: params.host,
|
|
165
|
+
port: params.port,
|
|
166
|
+
scheme: params.scheme,
|
|
167
|
+
});
|
|
168
|
+
this.client.on('connected', () => this.notify({ type: 'connected' }));
|
|
169
|
+
this.client.on('disconnected', () => this.notify({ type: 'disconnected' }));
|
|
141
170
|
await this.client.connect();
|
|
142
171
|
} catch (e) {
|
|
143
172
|
if (e instanceof Error) {
|
|
@@ -149,13 +178,8 @@ export class RostrumService {
|
|
|
149
178
|
}
|
|
150
179
|
}
|
|
151
180
|
|
|
152
|
-
public async disconnect(
|
|
153
|
-
|
|
154
|
-
return await this.client?.disconnect(force) ?? false;
|
|
155
|
-
} catch (e) {
|
|
156
|
-
console.error(e)
|
|
157
|
-
return false;
|
|
158
|
-
}
|
|
181
|
+
public async disconnect(): Promise<void> {
|
|
182
|
+
await this.client?.disconnect();
|
|
159
183
|
}
|
|
160
184
|
|
|
161
185
|
/**
|
|
@@ -184,11 +208,7 @@ export class RostrumService {
|
|
|
184
208
|
|
|
185
209
|
private async execute<T>(method: string, ...parameters: RPCParameter[]): Promise<T> {
|
|
186
210
|
await this.waitForConnection();
|
|
187
|
-
|
|
188
|
-
if (res instanceof Error) {
|
|
189
|
-
throw res;
|
|
190
|
-
}
|
|
191
|
-
return res as T;
|
|
211
|
+
return this.client!.request<T>(method, ...parameters);
|
|
192
212
|
}
|
|
193
213
|
|
|
194
214
|
private waitForConnection(timeout = 5000): Promise<void> {
|
|
@@ -196,7 +216,7 @@ export class RostrumService {
|
|
|
196
216
|
|
|
197
217
|
return new Promise((resolve, reject) => {
|
|
198
218
|
const check = (): void => {
|
|
199
|
-
if (this.
|
|
219
|
+
if (this.isConnected()) {
|
|
200
220
|
return resolve();
|
|
201
221
|
}
|
|
202
222
|
if (Date.now() - start > timeout) {
|