@otoplo/wallet-common 0.8.2 → 0.9.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 +65 -52
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +409 -412
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/persistence/repository.ts +7 -12
- package/src/persistence/wallet-db.ts +216 -251
- package/src/services/asset.ts +190 -162
- package/src/services/cache.ts +26 -54
- package/src/types/db.types.ts +11 -18
- package/src/types/wallet.types.ts +10 -0
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AccountEntity, AssetEntity,
|
|
1
|
+
import type { AccountEntity, AssetDTO, AssetEntity, AssetMetadataEntity, AssetTransactionEntity, SessionEntity, TransactionEntity, AddressEntity, VaultEntity } from "../types/db.types";
|
|
2
2
|
import type { AssetType } from "../types/wallet.types";
|
|
3
3
|
|
|
4
4
|
export interface Repository {
|
|
@@ -12,17 +12,12 @@ export interface Repository {
|
|
|
12
12
|
countTransactions(account: number, tokenId?: string): Promise<number>;
|
|
13
13
|
clearTransactions(account: number): Promise<void>;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
countNfts(account: number): Promise<number>;
|
|
22
|
-
upsertNft(nft: NftEntity): Promise<void>;
|
|
23
|
-
getNfts(account: number, pageNum: number, pageSize: number): Promise<NftEntity[]>;
|
|
24
|
-
getNft(id: string): Promise<NftEntity | null | undefined>;
|
|
25
|
-
deleteNft(id: string): Promise<void>;
|
|
15
|
+
upsertAssetMetadata(metadata: AssetMetadataEntity): Promise<void>;
|
|
16
|
+
getAssetMetadata(id: string): Promise<AssetMetadataEntity | null | undefined>;
|
|
17
|
+
deleteAssetMetadata(id: string): Promise<void>;
|
|
18
|
+
|
|
19
|
+
getPageAssets(account: number, type: AssetType, pageNum: number, pageSize: number): Promise<AssetDTO[]>;
|
|
20
|
+
countAssets(account: number, type: AssetType): Promise<number>;
|
|
26
21
|
|
|
27
22
|
upsertAsset(asset: AssetEntity): Promise<void>;
|
|
28
23
|
getAssets(account: number, type: AssetType): Promise<AssetEntity[]>;
|
|
@@ -1,251 +1,216 @@
|
|
|
1
|
-
import type { Repository } from "./repository";
|
|
2
|
-
import { AccountType } from "../utils/enums";
|
|
3
|
-
import type { AccountDTO, AccountEntity, AddressDTO, AddressEntity, AssetEntity,
|
|
4
|
-
import type { AssetType } from "../types/wallet.types";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
public
|
|
77
|
-
return this.repository.
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public
|
|
81
|
-
return this.repository.
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public
|
|
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
|
-
return
|
|
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
|
-
balance: JSON.
|
|
188
|
-
tokensBalance: JSON.
|
|
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
|
-
public
|
|
214
|
-
return this.repository.
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
public async getVaults(): Promise<VaultDTO[]> {
|
|
218
|
-
const vaults = await this.repository.getVaults();
|
|
219
|
-
return vaults.map(v => ({
|
|
220
|
-
...v,
|
|
221
|
-
type: AccountType.VAULT,
|
|
222
|
-
balance: JSON.parse(v.balance),
|
|
223
|
-
tokensBalance: JSON.parse(v.tokensBalance)
|
|
224
|
-
}));
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
public saveVault(vault: VaultDTO): Promise<void> {
|
|
228
|
-
const vaultEntity: VaultEntity = {
|
|
229
|
-
address: vault.address,
|
|
230
|
-
idx: vault.idx,
|
|
231
|
-
block: vault.block,
|
|
232
|
-
height: vault.height,
|
|
233
|
-
statusHash: vault.statusHash,
|
|
234
|
-
balance: JSON.stringify(vault.balance),
|
|
235
|
-
tokensBalance: JSON.stringify(vault.tokensBalance)
|
|
236
|
-
};
|
|
237
|
-
return this.repository.upsertVault(vaultEntity);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
public getSetting(name: string): Promise<string | null | undefined> {
|
|
241
|
-
return this.repository.getSetting(name);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
public saveSetting(name: string, value: string): Promise<void> {
|
|
245
|
-
return this.repository.saveSetting(name, value);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
public removeSetting(name: string): Promise<void> {
|
|
249
|
-
return this.repository.deleteSetting(name);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
1
|
+
import type { Repository } from "./repository";
|
|
2
|
+
import { AccountType } from "../utils/enums";
|
|
3
|
+
import type { AccountDTO, AccountEntity, AddressDTO, AddressEntity, AssetDTO, AssetEntity, AssetMetadataEntity, AssetTransactionEntity, SessionEntity, TransactionDTO, TransactionEntity, VaultDTO, VaultEntity } from "../types/db.types";
|
|
4
|
+
import type { AssetType } from "../types/wallet.types";
|
|
5
|
+
|
|
6
|
+
export class WalletDB {
|
|
7
|
+
|
|
8
|
+
private readonly repository: Repository;
|
|
9
|
+
|
|
10
|
+
constructor(repository: Repository) {
|
|
11
|
+
this.repository = repository;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public initSchema(): Promise<boolean> {
|
|
15
|
+
return this.repository.initSchema();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public clearData(): Promise<void> {
|
|
19
|
+
return this.repository.clearData();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public upsertTransaction(tx: TransactionDTO): Promise<void> {
|
|
23
|
+
const txEntity: TransactionEntity = {
|
|
24
|
+
...tx,
|
|
25
|
+
othersOutputs: JSON.stringify(tx.othersOutputs),
|
|
26
|
+
myOutputs: JSON.stringify(tx.myOutputs),
|
|
27
|
+
myInputs: JSON.stringify(tx.myInputs)
|
|
28
|
+
};
|
|
29
|
+
return this.repository.upsertTransaction(txEntity);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public upsertAssetTransaction(assetTx: AssetTransactionEntity): Promise<void> {
|
|
33
|
+
return this.repository.upsertAssetTransaction(assetTx);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async getTransactions(account: number, tokenId?: string): Promise<TransactionEntity[]> {
|
|
37
|
+
const txs = await this.repository.getTransactions(account, tokenId);
|
|
38
|
+
return txs.map(tx => ({
|
|
39
|
+
...tx,
|
|
40
|
+
othersOutputs: JSON.parse(tx.othersOutputs),
|
|
41
|
+
myOutputs: JSON.parse(tx.myOutputs),
|
|
42
|
+
myInputs: JSON.parse(tx.myInputs)
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public async getPageTransactions(account: number, pageNum: number, pageSize: number, tokenId?: string): Promise<TransactionEntity[]> {
|
|
47
|
+
const txs = await this.repository.getPageTransactions(account, pageNum, pageSize, tokenId);
|
|
48
|
+
return txs.map(tx => ({
|
|
49
|
+
...tx,
|
|
50
|
+
othersOutputs: JSON.parse(tx.othersOutputs),
|
|
51
|
+
myOutputs: JSON.parse(tx.myOutputs),
|
|
52
|
+
myInputs: JSON.parse(tx.myInputs)
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public countTransactions(account: number, tokenId?: string): Promise<number> {
|
|
57
|
+
return this.repository.countTransactions(account, tokenId);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public clearTransactions(account: number): Promise<void> {
|
|
61
|
+
return this.repository.clearTransactions(account);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public getAssets(account: number, type: AssetType): Promise<AssetEntity[]> {
|
|
65
|
+
return this.repository.getAssets(account, type);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public saveAsset(asset: AssetEntity): Promise<void> {
|
|
69
|
+
return this.repository.upsertAsset(asset);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public saveAssetMetadata(metadata: AssetMetadataEntity): Promise<void> {
|
|
73
|
+
return this.repository.upsertAssetMetadata(metadata);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public getAssetMetadata(id: string): Promise<AssetMetadataEntity | null | undefined> {
|
|
77
|
+
return this.repository.getAssetMetadata(id);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public getPageAssets(account: number, type: AssetType, pageNum: number, pageSize: number): Promise<AssetDTO[]> {
|
|
81
|
+
return this.repository.getPageAssets(account, type, pageNum, pageSize);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public countAssets(account: number, type: AssetType): Promise<number> {
|
|
85
|
+
return this.repository.countAssets(account, type);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public async isAssetHeld(id: string): Promise<boolean> {
|
|
89
|
+
return await this.repository.countAssetsById(id) > 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Removes the asset from the account, and its metadata once no account holds it. */
|
|
93
|
+
public async deleteAsset(account: number, id: string): Promise<void> {
|
|
94
|
+
await this.repository.deleteAsset(account, id);
|
|
95
|
+
if (!await this.isAssetHeld(id)) {
|
|
96
|
+
await this.repository.deleteAssetMetadata(id);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public saveAccount(account: AccountDTO): Promise<void> {
|
|
101
|
+
const accountEntity: AccountEntity = {
|
|
102
|
+
id: account.id,
|
|
103
|
+
address: account.address,
|
|
104
|
+
name: account.name,
|
|
105
|
+
height: account.height,
|
|
106
|
+
hidden: account.hidden,
|
|
107
|
+
statusHash: account.statusHash,
|
|
108
|
+
balance: JSON.stringify(account.balance),
|
|
109
|
+
tokensBalance: JSON.stringify(account.tokensBalance)
|
|
110
|
+
}
|
|
111
|
+
return this.repository.upsertAccount(accountEntity);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public async getAccounts(): Promise<AccountDTO[]> {
|
|
115
|
+
const accs = await this.repository.getAccounts();
|
|
116
|
+
return accs.map(a => ({
|
|
117
|
+
...a,
|
|
118
|
+
type: AccountType.DAPP,
|
|
119
|
+
balance: JSON.parse(a.balance),
|
|
120
|
+
tokensBalance: JSON.parse(a.tokensBalance)
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public countAccounts(): Promise<number> {
|
|
125
|
+
return this.repository.countAccounts();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public updateAccountName(account: number, name: string): Promise<void> {
|
|
129
|
+
return this.repository.updateAccountName(account, name);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public saveSession(session: SessionEntity): Promise<void> {
|
|
133
|
+
return this.repository.upsertSession(session);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public getAccountSessions(accountId: number): Promise<SessionEntity[]> {
|
|
137
|
+
return this.repository.getSessionsByAccount(accountId);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public removeSession(sessionId: string): Promise<void> {
|
|
141
|
+
return this.repository.deleteSession(sessionId);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public saveAddress(address: AddressDTO): Promise<void> {
|
|
145
|
+
const addressEntity: AddressEntity = {
|
|
146
|
+
address: address.address,
|
|
147
|
+
idx: address.idx,
|
|
148
|
+
space: address.space,
|
|
149
|
+
height: address.height,
|
|
150
|
+
statusHash: address.statusHash,
|
|
151
|
+
used: address.used,
|
|
152
|
+
balance: JSON.stringify(address.balance),
|
|
153
|
+
tokensBalance: JSON.stringify(address.tokensBalance)
|
|
154
|
+
}
|
|
155
|
+
return this.repository.upsertAddress(addressEntity);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public async getReceiveAddresses(): Promise<AddressDTO[]> {
|
|
159
|
+
const addrs = await this.repository.getReceiveAddresses();
|
|
160
|
+
return addrs.map(a => ({
|
|
161
|
+
...a,
|
|
162
|
+
type: AccountType.MAIN,
|
|
163
|
+
balance: JSON.parse(a.balance),
|
|
164
|
+
tokensBalance: JSON.parse(a.tokensBalance)
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public async getChangeAddresses(): Promise<AddressDTO[]> {
|
|
169
|
+
const addrs = await this.repository.getChangeAddresses();
|
|
170
|
+
return addrs.map(a => ({
|
|
171
|
+
...a,
|
|
172
|
+
type: AccountType.MAIN,
|
|
173
|
+
balance: JSON.parse(a.balance),
|
|
174
|
+
tokensBalance: JSON.parse(a.tokensBalance)
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public countAddresses(): Promise<number> {
|
|
179
|
+
return this.repository.countAddresses();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public async getVaults(): Promise<VaultDTO[]> {
|
|
183
|
+
const vaults = await this.repository.getVaults();
|
|
184
|
+
return vaults.map(v => ({
|
|
185
|
+
...v,
|
|
186
|
+
type: AccountType.VAULT,
|
|
187
|
+
balance: JSON.parse(v.balance),
|
|
188
|
+
tokensBalance: JSON.parse(v.tokensBalance)
|
|
189
|
+
}));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public saveVault(vault: VaultDTO): Promise<void> {
|
|
193
|
+
const vaultEntity: VaultEntity = {
|
|
194
|
+
address: vault.address,
|
|
195
|
+
idx: vault.idx,
|
|
196
|
+
block: vault.block,
|
|
197
|
+
height: vault.height,
|
|
198
|
+
statusHash: vault.statusHash,
|
|
199
|
+
balance: JSON.stringify(vault.balance),
|
|
200
|
+
tokensBalance: JSON.stringify(vault.tokensBalance)
|
|
201
|
+
};
|
|
202
|
+
return this.repository.upsertVault(vaultEntity);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public getSetting(name: string): Promise<string | null | undefined> {
|
|
206
|
+
return this.repository.getSetting(name);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public saveSetting(name: string, value: string): Promise<void> {
|
|
210
|
+
return this.repository.saveSetting(name, value);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
public removeSetting(name: string): Promise<void> {
|
|
214
|
+
return this.repository.deleteSetting(name);
|
|
215
|
+
}
|
|
216
|
+
}
|