@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@otoplo/wallet-common",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "Shared common library for internal use in Otoplo wallet",
5
5
  "license": "MIT",
6
6
  "author": "vgrunner",
@@ -1,4 +1,4 @@
1
- import type { AccountEntity, AssetEntity, AssetTransactionEntity, NftEntity, SessionEntity, TokenEntity, TransactionEntity, AddressEntity, VaultEntity } from "../types/db.types";
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
- countTokens(account: number): Promise<number>;
16
- upsertToken(token: TokenEntity): Promise<void>;
17
- getTokens(account: number, pageNum: number, pageSize: number): Promise<TokenEntity[]>;
18
- getToken(id: string): Promise<TokenEntity | null | undefined>;
19
- deleteToken(id: string): Promise<void>;
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, AssetTransactionEntity, NftEntity, SessionEntity, TokenEntity, TransactionDTO, TransactionEntity, VaultDTO, VaultEntity } from "../types/db.types";
4
- import type { AssetType } from "../types/wallet.types";
5
- import { currentTimestamp } from "../utils/common";
6
-
7
- export class WalletDB {
8
-
9
- private readonly repository: Repository;
10
-
11
- constructor(repository: Repository) {
12
- this.repository = repository;
13
- }
14
-
15
- public initSchema(): Promise<boolean> {
16
- return this.repository.initSchema();
17
- }
18
-
19
- public clearData(): Promise<void> {
20
- return this.repository.clearData();
21
- }
22
-
23
- public upsertTransaction(tx: TransactionDTO): Promise<void> {
24
- const txEntity: TransactionEntity = {
25
- ...tx,
26
- othersOutputs: JSON.stringify(tx.othersOutputs),
27
- myOutputs: JSON.stringify(tx.myOutputs),
28
- myInputs: JSON.stringify(tx.myInputs)
29
- };
30
- return this.repository.upsertTransaction(txEntity);
31
- }
32
-
33
- public upsertAssetTransaction(assetTx: AssetTransactionEntity): Promise<void> {
34
- return this.repository.upsertAssetTransaction(assetTx);
35
- }
36
-
37
- public async getTransactions(account: number, tokenId?: string): Promise<TransactionEntity[]> {
38
- const txs = await this.repository.getTransactions(account, tokenId);
39
- return txs.map(tx => ({
40
- ...tx,
41
- othersOutputs: JSON.parse(tx.othersOutputs),
42
- myOutputs: JSON.parse(tx.myOutputs),
43
- myInputs: JSON.parse(tx.myInputs)
44
- }));
45
- }
46
-
47
- public async getPageTransactions(account: number, pageNum: number, pageSize: number, tokenId?: string): Promise<TransactionEntity[]> {
48
- const txs = await this.repository.getPageTransactions(account, pageNum, pageSize, tokenId);
49
- return txs.map(tx => ({
50
- ...tx,
51
- othersOutputs: JSON.parse(tx.othersOutputs),
52
- myOutputs: JSON.parse(tx.myOutputs),
53
- myInputs: JSON.parse(tx.myInputs)
54
- }));
55
- }
56
-
57
- public countTransactions(account: number, tokenId?: string): Promise<number> {
58
- return this.repository.countTransactions(account, tokenId);
59
- }
60
-
61
- public clearTransactions(account: number): Promise<void> {
62
- return this.repository.clearTransactions(account);
63
- }
64
-
65
- public async saveToken(account: number, token: TokenEntity): Promise<void> {
66
- const asset: AssetEntity = {
67
- accountId: account,
68
- tokenIdHex: token.tokenIdHex,
69
- type: "token",
70
- addedTime: currentTimestamp()
71
- }
72
- await this.repository.upsertToken(token);
73
- await this.saveAsset(asset);
74
- }
75
-
76
- public getTokens(account: number, pageNum: number, pageSize: number): Promise<TokenEntity[]> {
77
- return this.repository.getTokens(account, pageNum, pageSize);
78
- }
79
-
80
- public getToken(id: string): Promise<TokenEntity | null | undefined> {
81
- return this.repository.getToken(id);
82
- }
83
-
84
- public async deleteToken(account: number, tokenId: string): Promise<void> {
85
- await this.removeAsset(account, tokenId);
86
- const count = await this.repository.countAssetsById(tokenId);
87
- if (count == 0) {
88
- await this.repository.deleteToken(tokenId);
89
- }
90
- }
91
-
92
- public async saveNft(account: number, nft: NftEntity, time: number): Promise<void> {
93
- const asset: AssetEntity = {
94
- accountId: account,
95
- tokenIdHex: nft.tokenIdHex,
96
- type: "nft",
97
- addedTime: time
98
- }
99
- await this.repository.upsertNft(nft);
100
- await this.saveAsset(asset);
101
- }
102
-
103
- public getNfts(account: number, pageNum: number, pageSize: number): Promise<NftEntity[]> {
104
- return this.repository.getNfts(account, pageNum, pageSize);
105
- }
106
-
107
- public getNft(id: string): Promise<NftEntity | null | undefined> {
108
- return this.repository.getNft(id);
109
- }
110
-
111
- public async deleteNft(account: number, tokenId: string): Promise<void> {
112
- await this.removeAsset(account, tokenId);
113
- const count = await this.repository.countAssetsById(tokenId);
114
- if (count == 0) {
115
- await this.repository.deleteNft(tokenId);
116
- }
117
- }
118
-
119
- public countNfts(account: number): Promise<number> {
120
- return this.repository.countNfts(account);
121
- }
122
-
123
- public getAssets(account: number, type: AssetType): Promise<AssetEntity[]> {
124
- return this.repository.getAssets(account, type);
125
- }
126
-
127
- public saveAsset(asset: AssetEntity): Promise<void> {
128
- return this.repository.upsertAsset(asset);
129
- }
130
-
131
- public removeAsset(account: number, id: string): Promise<void> {
132
- return this.repository.deleteAsset(account, id);
133
- }
134
-
135
- public saveAccount(account: AccountDTO): Promise<void> {
136
- const accountEntity: AccountEntity = {
137
- id: account.id,
138
- address: account.address,
139
- name: account.name,
140
- height: account.height,
141
- hidden: account.hidden,
142
- statusHash: account.statusHash,
143
- balance: JSON.stringify(account.balance),
144
- tokensBalance: JSON.stringify(account.tokensBalance)
145
- }
146
- return this.repository.upsertAccount(accountEntity);
147
- }
148
-
149
- public async getAccounts(): Promise<AccountDTO[]> {
150
- const accs = await this.repository.getAccounts();
151
- return accs.map(a => ({
152
- ...a,
153
- type: AccountType.DAPP,
154
- balance: JSON.parse(a.balance),
155
- tokensBalance: JSON.parse(a.tokensBalance)
156
- }));
157
- }
158
-
159
- public countAccounts(): Promise<number> {
160
- return this.repository.countAccounts();
161
- }
162
-
163
- public updateAccountName(account: number, name: string): Promise<void> {
164
- return this.repository.updateAccountName(account, name);
165
- }
166
-
167
- public saveSession(session: SessionEntity): Promise<void> {
168
- return this.repository.upsertSession(session);
169
- }
170
-
171
- public getAccountSessions(accountId: number): Promise<SessionEntity[]> {
172
- return this.repository.getSessionsByAccount(accountId);
173
- }
174
-
175
- public removeSession(sessionId: string): Promise<void> {
176
- return this.repository.deleteSession(sessionId);
177
- }
178
-
179
- public saveAddress(address: AddressDTO): Promise<void> {
180
- const addressEntity: AddressEntity = {
181
- address: address.address,
182
- idx: address.idx,
183
- space: address.space,
184
- height: address.height,
185
- statusHash: address.statusHash,
186
- used: address.used,
187
- balance: JSON.stringify(address.balance),
188
- tokensBalance: JSON.stringify(address.tokensBalance)
189
- }
190
- return this.repository.upsertAddress(addressEntity);
191
- }
192
-
193
- public async getReceiveAddresses(): Promise<AddressDTO[]> {
194
- const addrs = await this.repository.getReceiveAddresses();
195
- return addrs.map(a => ({
196
- ...a,
197
- type: AccountType.MAIN,
198
- balance: JSON.parse(a.balance),
199
- tokensBalance: JSON.parse(a.tokensBalance)
200
- }));
201
- }
202
-
203
- public async getChangeAddresses(): Promise<AddressDTO[]> {
204
- const addrs = await this.repository.getChangeAddresses();
205
- return addrs.map(a => ({
206
- ...a,
207
- type: AccountType.MAIN,
208
- balance: JSON.parse(a.balance),
209
- tokensBalance: JSON.parse(a.tokensBalance)
210
- }));
211
- }
212
-
213
- public countAddresses(): Promise<number> {
214
- return this.repository.countAddresses();
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
+ }