@bsv/wallet-toolbox-client 1.1.34 → 1.1.35
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/out/src/sdk/WERR_errors.d.ts +1 -1
- package/out/src/sdk/WERR_errors.d.ts.map +1 -1
- package/out/src/sdk/WERR_errors.js +2 -2
- package/out/src/sdk/WERR_errors.js.map +1 -1
- package/out/src/sdk/WalletStorage.interfaces.d.ts +107 -0
- package/out/src/sdk/WalletStorage.interfaces.d.ts.map +1 -1
- package/out/src/sdk/index.d.ts +0 -2
- package/out/src/sdk/index.d.ts.map +1 -1
- package/out/src/sdk/index.js +0 -2
- package/out/src/sdk/index.js.map +1 -1
- package/out/src/storage/StorageReader.d.ts +1 -1
- package/out/src/storage/StorageReader.d.ts.map +1 -1
- package/out/src/storage/StorageReaderWriter.d.ts.map +1 -1
- package/out/src/storage/StorageReaderWriter.js +2 -1
- package/out/src/storage/StorageReaderWriter.js.map +1 -1
- package/out/src/storage/StorageSyncReader.d.ts +2 -18
- package/out/src/storage/StorageSyncReader.d.ts.map +1 -1
- package/out/src/storage/StorageSyncReader.js +1 -105
- package/out/src/storage/StorageSyncReader.js.map +1 -1
- package/out/src/storage/WalletStorageManager.d.ts +86 -10
- package/out/src/storage/WalletStorageManager.d.ts.map +1 -1
- package/out/src/storage/WalletStorageManager.js +235 -78
- package/out/src/storage/WalletStorageManager.js.map +1 -1
- package/out/src/storage/schema/entities/User.d.ts +2 -2
- package/out/src/storage/schema/entities/User.d.ts.map +1 -1
- package/out/src/storage/schema/entities/User.js +1 -1
- package/out/src/storage/schema/entities/User.js.map +1 -1
- package/out/src/storage/schema/tables/User.d.ts +1 -1
- package/out/src/storage/schema/tables/User.d.ts.map +1 -1
- package/package.json +2 -2
- package/out/src/sdk/StorageSyncReader.d.ts +0 -121
- package/out/src/sdk/StorageSyncReader.d.ts.map +0 -1
- package/out/src/sdk/StorageSyncReader.js +0 -3
- package/out/src/sdk/StorageSyncReader.js.map +0 -1
- package/out/src/sdk/StorageSyncReaderWriter.d.ts +0 -89
- package/out/src/sdk/StorageSyncReaderWriter.d.ts.map +0 -1
- package/out/src/sdk/StorageSyncReaderWriter.js +0 -3
- package/out/src/sdk/StorageSyncReaderWriter.js.map +0 -1
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { AbortActionArgs, AbortActionResult, InternalizeActionArgs, InternalizeActionResult, ListActionsResult, ListCertificatesResult, ListOutputsResult, RelinquishCertificateArgs, RelinquishOutputArgs } from '@bsv/sdk';
|
|
2
|
-
import { sdk, StorageProvider,
|
|
2
|
+
import { sdk, StorageProvider, TableCertificate, TableCertificateX, TableOutput, TableOutputBasket, TableProvenTxReq, TableSettings, TableUser } from '../index.client';
|
|
3
|
+
declare class ManagedStorage {
|
|
4
|
+
storage: sdk.WalletStorageProvider;
|
|
5
|
+
isAvailable: boolean;
|
|
6
|
+
isStorageProvider: boolean;
|
|
7
|
+
settings?: TableSettings;
|
|
8
|
+
user?: TableUser;
|
|
9
|
+
constructor(storage: sdk.WalletStorageProvider);
|
|
10
|
+
}
|
|
3
11
|
/**
|
|
4
12
|
* The `WalletStorageManager` class delivers authentication checking storage access to the wallet.
|
|
5
13
|
*
|
|
@@ -16,11 +24,41 @@ import { sdk, StorageProvider, StorageSyncReader, TableCertificate, TableCertifi
|
|
|
16
24
|
* for these services.
|
|
17
25
|
*/
|
|
18
26
|
export declare class WalletStorageManager implements sdk.WalletStorage {
|
|
19
|
-
|
|
27
|
+
/**
|
|
28
|
+
* All configured stores including current active, backups, and conflicting actives.
|
|
29
|
+
*/
|
|
30
|
+
_stores: ManagedStorage[];
|
|
31
|
+
/**
|
|
32
|
+
* True if makeAvailable has been run and access to managed stores (active) is allowed
|
|
33
|
+
*/
|
|
34
|
+
_isAvailable: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The current active store which is only enabled if the store's user record activeStorage property matches its settings record storageIdentityKey property
|
|
37
|
+
*/
|
|
38
|
+
_active?: ManagedStorage;
|
|
39
|
+
/**
|
|
40
|
+
* Stores to which state is pushed by updateBackups.
|
|
41
|
+
*/
|
|
42
|
+
_backups?: ManagedStorage[];
|
|
43
|
+
/**
|
|
44
|
+
* Stores whose user record activeStorage property disagrees with the active store's user record activeStorage property.
|
|
45
|
+
*/
|
|
46
|
+
_conflictingActives?: ManagedStorage[];
|
|
47
|
+
/**
|
|
48
|
+
* identityKey is always valid, userId and isActive are valid only if _isAvailable
|
|
49
|
+
*/
|
|
20
50
|
_authId: sdk.AuthId;
|
|
51
|
+
/**
|
|
52
|
+
* Configured services if any. If valid, shared with stores (which may ignore it).
|
|
53
|
+
*/
|
|
21
54
|
_services?: sdk.WalletServices;
|
|
22
|
-
|
|
55
|
+
/**
|
|
56
|
+
* How many read access operations are pending
|
|
57
|
+
*/
|
|
23
58
|
_readerCount: number;
|
|
59
|
+
/**
|
|
60
|
+
* How many write access operations are pending
|
|
61
|
+
*/
|
|
24
62
|
_writerCount: number;
|
|
25
63
|
/**
|
|
26
64
|
* if true, allow only a single writer to proceed at a time.
|
|
@@ -39,9 +77,41 @@ export declare class WalletStorageManager implements sdk.WalletStorage {
|
|
|
39
77
|
_storageProviderLocked: boolean;
|
|
40
78
|
constructor(identityKey: string, active?: sdk.WalletStorageProvider, backups?: sdk.WalletStorageProvider[]);
|
|
41
79
|
isStorageProvider(): boolean;
|
|
42
|
-
|
|
80
|
+
isAvailable(): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* The active storage is "enabled" only if its `storageIdentityKey` matches the user's currently selected `activeStorage`,
|
|
83
|
+
* and only if there are no stores with conflicting `activeStorage` selections.
|
|
84
|
+
*
|
|
85
|
+
* A wallet may be created without including the user's currently selected active storage. This allows readonly access to their wallet data.
|
|
86
|
+
*
|
|
87
|
+
* In addition, if there are conflicting `activeStorage` selections among backup storage providers then the active remains disabled.
|
|
88
|
+
*/
|
|
89
|
+
get isActiveEnabled(): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* @returns true if at least one WalletStorageProvider has been added.
|
|
92
|
+
*/
|
|
93
|
+
canMakeAvailable(): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* This async function must be called after construction and before
|
|
96
|
+
* any other async function can proceed.
|
|
97
|
+
*
|
|
98
|
+
* Runs through `_stores` validating all properties and partitioning across `_active`, `_backups`, `_conflictingActives`.
|
|
99
|
+
*
|
|
100
|
+
* @throws WERR_INVALID_PARAMETER if canMakeAvailable returns false.
|
|
101
|
+
*
|
|
102
|
+
* @returns {TableSettings} from the active storage.
|
|
103
|
+
*/
|
|
104
|
+
makeAvailable(): Promise<TableSettings>;
|
|
105
|
+
private verifyActive;
|
|
43
106
|
getAuth(mustBeActive?: boolean): Promise<sdk.AuthId>;
|
|
107
|
+
getUserId(): Promise<number>;
|
|
44
108
|
getActive(): sdk.WalletStorageProvider;
|
|
109
|
+
getActiveSettings(): TableSettings;
|
|
110
|
+
getActiveUser(): TableUser;
|
|
111
|
+
getActiveStore(): string;
|
|
112
|
+
getBackupStores(): string[];
|
|
113
|
+
getConflictingStores(): string[];
|
|
114
|
+
getAllStores(): string[];
|
|
45
115
|
getActiveForWriter(): Promise<sdk.WalletStorageWriter>;
|
|
46
116
|
getActiveForReader(): Promise<sdk.WalletStorageReader>;
|
|
47
117
|
getActiveForSync(): Promise<sdk.WalletStorageSync>;
|
|
@@ -61,12 +131,10 @@ export declare class WalletStorageManager implements sdk.WalletStorage {
|
|
|
61
131
|
* @returns true if the active `WalletStorageProvider` also implements `StorageProvider`
|
|
62
132
|
*/
|
|
63
133
|
isActiveStorageProvider(): boolean;
|
|
64
|
-
isAvailable(): boolean;
|
|
65
134
|
addWalletStorageProvider(provider: sdk.WalletStorageProvider): Promise<void>;
|
|
66
135
|
setServices(v: sdk.WalletServices): void;
|
|
67
136
|
getServices(): sdk.WalletServices;
|
|
68
137
|
getSettings(): TableSettings;
|
|
69
|
-
makeAvailable(): Promise<TableSettings>;
|
|
70
138
|
migrate(storageName: string, storageIdentityKey: string): Promise<string>;
|
|
71
139
|
destroy(): Promise<void>;
|
|
72
140
|
findOrInsertUser(identityKey: string): Promise<{
|
|
@@ -87,17 +155,25 @@ export declare class WalletStorageManager implements sdk.WalletStorage {
|
|
|
87
155
|
findOutputBaskets(args: sdk.FindOutputBasketsArgs): Promise<TableOutputBasket[]>;
|
|
88
156
|
findOutputs(args: sdk.FindOutputsArgs): Promise<TableOutput[]>;
|
|
89
157
|
findProvenTxReqs(args: sdk.FindProvenTxReqsArgs): Promise<TableProvenTxReq[]>;
|
|
90
|
-
syncFromReader(identityKey: string, reader:
|
|
91
|
-
updateBackups(activeSync?: sdk.WalletStorageSync): Promise<void>;
|
|
92
|
-
syncToWriter(auth: sdk.AuthId, writer: sdk.WalletStorageProvider, activeSync?: sdk.WalletStorageSync): Promise<{
|
|
158
|
+
syncFromReader(identityKey: string, reader: sdk.WalletStorageSyncReader, activeSync?: sdk.WalletStorageSync, log?: string): Promise<{
|
|
93
159
|
inserts: number;
|
|
94
160
|
updates: number;
|
|
161
|
+
log: string;
|
|
95
162
|
}>;
|
|
163
|
+
syncToWriter(auth: sdk.AuthId, writer: sdk.WalletStorageProvider, activeSync?: sdk.WalletStorageSync, log?: string): Promise<{
|
|
164
|
+
inserts: number;
|
|
165
|
+
updates: number;
|
|
166
|
+
log: string;
|
|
167
|
+
}>;
|
|
168
|
+
updateBackups(activeSync?: sdk.WalletStorageSync): Promise<void>;
|
|
96
169
|
/**
|
|
97
170
|
* Updates backups and switches to new active storage provider from among current backup providers.
|
|
98
171
|
*
|
|
172
|
+
* Also resolves conflicting actives.
|
|
173
|
+
*
|
|
99
174
|
* @param storageIdentityKey of current backup storage provider that is to become the new active provider.
|
|
100
175
|
*/
|
|
101
|
-
setActive(storageIdentityKey: string): Promise<
|
|
176
|
+
setActive(storageIdentityKey: string): Promise<string>;
|
|
102
177
|
}
|
|
178
|
+
export {};
|
|
103
179
|
//# sourceMappingURL=WalletStorageManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WalletStorageManager.d.ts","sourceRoot":"","sources":["../../../../src/storage/WalletStorageManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,qBAAqB,EACrB,uBAAuB,EAEvB,iBAAiB,EACjB,sBAAsB,EAEtB,iBAAiB,EACjB,yBAAyB,EACzB,oBAAoB,EACrB,MAAM,UAAU,CAAA;AACjB,OAAO,EAEL,GAAG,EACH,eAAe,
|
|
1
|
+
{"version":3,"file":"WalletStorageManager.d.ts","sourceRoot":"","sources":["../../../../src/storage/WalletStorageManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,qBAAqB,EACrB,uBAAuB,EAEvB,iBAAiB,EACjB,sBAAsB,EAEtB,iBAAiB,EACjB,yBAAyB,EACzB,oBAAoB,EACrB,MAAM,UAAU,CAAA;AACjB,OAAO,EAEL,GAAG,EACH,eAAe,EAEf,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EACb,SAAS,EAEV,MAAM,iBAAiB,CAAA;AAGxB,cAAM,cAAc;IAMC,OAAO,EAAE,GAAG,CAAC,qBAAqB;IALrD,WAAW,EAAE,OAAO,CAAA;IACpB,iBAAiB,EAAE,OAAO,CAAA;IAC1B,QAAQ,CAAC,EAAE,aAAa,CAAA;IACxB,IAAI,CAAC,EAAE,SAAS,CAAA;gBAEG,OAAO,EAAE,GAAG,CAAC,qBAAqB;CAItD;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,oBAAqB,YAAW,GAAG,CAAC,aAAa;IAC5D;;OAEG;IACH,OAAO,EAAE,cAAc,EAAE,CAAK;IAC9B;;OAEG;IACH,YAAY,EAAE,OAAO,CAAQ;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,cAAc,CAAA;IACxB;;OAEG;IACH,QAAQ,CAAC,EAAE,cAAc,EAAE,CAAA;IAC3B;;OAEG;IACH,mBAAmB,CAAC,E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AAiB,EAClC,GAAG,GAAE,MAAW,GACf,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IA6CvD,YAAY,CAChB,IAAI,EAAE,GAAG,CAAC,MAAM,EAChB,MAAM,EAAE,GAAG,CAAC,qBAAqB,EACjC,UAAU,CAAC,EAAE,GAAG,CAAC,iBAAiB,EAClC,GAAG,GAAE,MAAW,GACf,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IAwCvD,aAAa,CAAC,UAAU,CAAC,EAAE,GAAG,CAAC,iBAAiB;IAStD;;;;;;OAMG;IACG,SAAS,CAAC,kBAAkB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CA8E7D"}
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WalletStorageManager = void 0;
|
|
4
4
|
const index_client_1 = require("../index.client");
|
|
5
|
+
class ManagedStorage {
|
|
6
|
+
constructor(storage) {
|
|
7
|
+
this.storage = storage;
|
|
8
|
+
this.isStorageProvider = storage.isStorageProvider();
|
|
9
|
+
this.isAvailable = false;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
5
12
|
/**
|
|
6
13
|
* The `WalletStorageManager` class delivers authentication checking storage access to the wallet.
|
|
7
14
|
*
|
|
@@ -19,9 +26,21 @@ const index_client_1 = require("../index.client");
|
|
|
19
26
|
*/
|
|
20
27
|
class WalletStorageManager {
|
|
21
28
|
constructor(identityKey, active, backups) {
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
/**
|
|
30
|
+
* All configured stores including current active, backups, and conflicting actives.
|
|
31
|
+
*/
|
|
32
|
+
this._stores = [];
|
|
33
|
+
/**
|
|
34
|
+
* True if makeAvailable has been run and access to managed stores (active) is allowed
|
|
35
|
+
*/
|
|
36
|
+
this._isAvailable = false;
|
|
37
|
+
/**
|
|
38
|
+
* How many read access operations are pending
|
|
39
|
+
*/
|
|
24
40
|
this._readerCount = 0;
|
|
41
|
+
/**
|
|
42
|
+
* How many write access operations are pending
|
|
43
|
+
*/
|
|
25
44
|
this._writerCount = 0;
|
|
26
45
|
/**
|
|
27
46
|
* if true, allow only a single writer to proceed at a time.
|
|
@@ -38,41 +57,143 @@ class WalletStorageManager {
|
|
|
38
57
|
* queue the blocked requests so they get executed in order when released.
|
|
39
58
|
*/
|
|
40
59
|
this._storageProviderLocked = false;
|
|
41
|
-
|
|
60
|
+
const stores = [...(backups || [])];
|
|
42
61
|
if (active)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.stores = this.stores.concat(backups);
|
|
62
|
+
stores.unshift(active);
|
|
63
|
+
this._stores = stores.map(s => new ManagedStorage(s));
|
|
46
64
|
this._authId = { identityKey };
|
|
47
65
|
}
|
|
48
66
|
isStorageProvider() {
|
|
49
67
|
return false;
|
|
50
68
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
isAvailable() {
|
|
70
|
+
return this._isAvailable;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The active storage is "enabled" only if its `storageIdentityKey` matches the user's currently selected `activeStorage`,
|
|
74
|
+
* and only if there are no stores with conflicting `activeStorage` selections.
|
|
75
|
+
*
|
|
76
|
+
* A wallet may be created without including the user's currently selected active storage. This allows readonly access to their wallet data.
|
|
77
|
+
*
|
|
78
|
+
* In addition, if there are conflicting `activeStorage` selections among backup storage providers then the active remains disabled.
|
|
79
|
+
*/
|
|
80
|
+
get isActiveEnabled() {
|
|
81
|
+
return (this._active !== undefined &&
|
|
82
|
+
this._active.settings.storageIdentityKey ===
|
|
83
|
+
this._active.user.activeStorage &&
|
|
84
|
+
this._conflictingActives !== undefined &&
|
|
85
|
+
this._conflictingActives.length === 0);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @returns true if at least one WalletStorageProvider has been added.
|
|
89
|
+
*/
|
|
90
|
+
canMakeAvailable() {
|
|
91
|
+
return this._stores.length > 0;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* This async function must be called after construction and before
|
|
95
|
+
* any other async function can proceed.
|
|
96
|
+
*
|
|
97
|
+
* Runs through `_stores` validating all properties and partitioning across `_active`, `_backups`, `_conflictingActives`.
|
|
98
|
+
*
|
|
99
|
+
* @throws WERR_INVALID_PARAMETER if canMakeAvailable returns false.
|
|
100
|
+
*
|
|
101
|
+
* @returns {TableSettings} from the active storage.
|
|
102
|
+
*/
|
|
103
|
+
async makeAvailable() {
|
|
104
|
+
var _a;
|
|
105
|
+
if (this._isAvailable)
|
|
106
|
+
return this._active.settings;
|
|
107
|
+
this._active = undefined;
|
|
108
|
+
this._backups = [];
|
|
109
|
+
this._conflictingActives = [];
|
|
110
|
+
if (this._stores.length < 1)
|
|
111
|
+
throw new index_client_1.sdk.WERR_INVALID_PARAMETER('active', 'valid. Must add active storage provider to wallet.');
|
|
112
|
+
// Initial backups. conflictingActives will be removed.
|
|
113
|
+
const backups = [];
|
|
114
|
+
let i = -1;
|
|
115
|
+
for (const store of this._stores) {
|
|
116
|
+
i++;
|
|
117
|
+
if (!store.isAvailable || !store.settings) {
|
|
118
|
+
// Validate all ManagedStorage properties.
|
|
119
|
+
store.settings = await store.storage.makeAvailable();
|
|
120
|
+
const r = await store.storage.findOrInsertUser(this._authId.identityKey);
|
|
121
|
+
store.user = r.user;
|
|
122
|
+
store.isAvailable = true;
|
|
123
|
+
}
|
|
124
|
+
if (!this._active)
|
|
125
|
+
// _stores[0] becomes the default active store. It may be replaced if it is not the user's "enabled" activeStorage and that store is found among the remainder (backups).
|
|
126
|
+
this._active = store;
|
|
127
|
+
else {
|
|
128
|
+
const ua = store.user.activeStorage;
|
|
129
|
+
const si = store.settings.storageIdentityKey;
|
|
130
|
+
if (ua === si && !this.isActiveEnabled) {
|
|
131
|
+
// This store's user record selects it as an enabled active storage...
|
|
132
|
+
// swap the current not-enabled active for this storeage.
|
|
133
|
+
backups.push(this._active);
|
|
134
|
+
this._active = store;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// This store is a backup: Its user record selects some other storage as active.
|
|
138
|
+
backups.push(store);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// Review backups, partition out conflicting actives.
|
|
143
|
+
const si = (_a = this._active.settings) === null || _a === void 0 ? void 0 : _a.storageIdentityKey;
|
|
144
|
+
for (const store of backups) {
|
|
145
|
+
if (store.user.activeStorage !== si)
|
|
146
|
+
this._conflictingActives.push(store);
|
|
147
|
+
else
|
|
148
|
+
this._backups.push(store);
|
|
149
|
+
}
|
|
150
|
+
this._isAvailable = true;
|
|
151
|
+
this._authId.userId = this._active.user.userId;
|
|
152
|
+
this._authId.isActive = this.isActiveEnabled;
|
|
153
|
+
return this._active.settings;
|
|
154
|
+
}
|
|
155
|
+
verifyActive() {
|
|
156
|
+
if (!this._active || !this._isAvailable)
|
|
157
|
+
throw new index_client_1.sdk.WERR_INVALID_OPERATION('An active WalletStorageProvider must be added to this WalletStorageManager and makeAvailable must be called.');
|
|
158
|
+
return this._active;
|
|
55
159
|
}
|
|
56
160
|
async getAuth(mustBeActive) {
|
|
57
161
|
if (!this.isAvailable())
|
|
58
162
|
await this.makeAvailable();
|
|
59
|
-
const { user, isNew } = await this.getActive().findOrInsertUser(this._authId.identityKey);
|
|
60
|
-
if (!user)
|
|
61
|
-
throw new index_client_1.sdk.WERR_INVALID_PARAMETER('identityKey', 'exist on storage.');
|
|
62
|
-
this._authId.userId = user.userId;
|
|
63
|
-
this._authId.isActive =
|
|
64
|
-
user.activeStorage === undefined ||
|
|
65
|
-
user.activeStorage === this.getSettings().storageIdentityKey;
|
|
66
163
|
if (mustBeActive && !this._authId.isActive)
|
|
67
164
|
throw new index_client_1.sdk.WERR_NOT_ACTIVE();
|
|
68
165
|
return this._authId;
|
|
69
166
|
}
|
|
167
|
+
async getUserId() {
|
|
168
|
+
return (await this.getAuth()).userId;
|
|
169
|
+
}
|
|
70
170
|
getActive() {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
171
|
+
return this.verifyActive().storage;
|
|
172
|
+
}
|
|
173
|
+
getActiveSettings() {
|
|
174
|
+
return this.verifyActive().settings;
|
|
175
|
+
}
|
|
176
|
+
getActiveUser() {
|
|
177
|
+
return this.verifyActive().user;
|
|
178
|
+
}
|
|
179
|
+
getActiveStore() {
|
|
180
|
+
return this.verifyActive().settings.storageIdentityKey;
|
|
181
|
+
}
|
|
182
|
+
getBackupStores() {
|
|
183
|
+
this.verifyActive();
|
|
184
|
+
return this._backups.map(b => b.settings.storageIdentityKey);
|
|
185
|
+
}
|
|
186
|
+
getConflictingStores() {
|
|
187
|
+
this.verifyActive();
|
|
188
|
+
return this._conflictingActives.map(b => b.settings.storageIdentityKey);
|
|
189
|
+
}
|
|
190
|
+
getAllStores() {
|
|
191
|
+
this.verifyActive();
|
|
192
|
+
return this._stores.map(b => b.settings.storageIdentityKey);
|
|
74
193
|
}
|
|
75
194
|
async getActiveForWriter() {
|
|
195
|
+
if (!this.isAvailable())
|
|
196
|
+
await this.makeAvailable();
|
|
76
197
|
while (this._storageProviderLocked ||
|
|
77
198
|
this._syncLocked ||
|
|
78
199
|
(this._isSingleWriter && this._writerCount > 0) ||
|
|
@@ -83,6 +204,8 @@ class WalletStorageManager {
|
|
|
83
204
|
return this.getActive();
|
|
84
205
|
}
|
|
85
206
|
async getActiveForReader() {
|
|
207
|
+
if (!this.isAvailable())
|
|
208
|
+
await this.makeAvailable();
|
|
86
209
|
while (this._storageProviderLocked ||
|
|
87
210
|
this._syncLocked ||
|
|
88
211
|
(this._isSingleWriter && this._writerCount > 0)) {
|
|
@@ -92,6 +215,8 @@ class WalletStorageManager {
|
|
|
92
215
|
return this.getActive();
|
|
93
216
|
}
|
|
94
217
|
async getActiveForSync() {
|
|
218
|
+
if (!this.isAvailable())
|
|
219
|
+
await this.makeAvailable();
|
|
95
220
|
// Wait for a current sync task to complete...
|
|
96
221
|
while (this._syncLocked) {
|
|
97
222
|
await (0, index_client_1.wait)(100);
|
|
@@ -108,6 +233,8 @@ class WalletStorageManager {
|
|
|
108
233
|
return this.getActive();
|
|
109
234
|
}
|
|
110
235
|
async getActiveForStorageProvider() {
|
|
236
|
+
if (!this.isAvailable())
|
|
237
|
+
await this.makeAvailable();
|
|
111
238
|
// Wait for a current storageProvider call to complete...
|
|
112
239
|
while (this._storageProviderLocked) {
|
|
113
240
|
await (0, index_client_1.wait)(100);
|
|
@@ -178,19 +305,18 @@ class WalletStorageManager {
|
|
|
178
305
|
isActiveStorageProvider() {
|
|
179
306
|
return this.getActive().isStorageProvider();
|
|
180
307
|
}
|
|
181
|
-
isAvailable() {
|
|
182
|
-
return this.getActive().isAvailable();
|
|
183
|
-
}
|
|
184
308
|
async addWalletStorageProvider(provider) {
|
|
185
309
|
await provider.makeAvailable();
|
|
186
310
|
if (this._services)
|
|
187
311
|
provider.setServices(this._services);
|
|
188
|
-
this.
|
|
312
|
+
this._stores.push(new ManagedStorage(provider));
|
|
313
|
+
this._isAvailable = false;
|
|
314
|
+
await this.makeAvailable();
|
|
189
315
|
}
|
|
190
316
|
setServices(v) {
|
|
191
317
|
this._services = v;
|
|
192
|
-
for (const store of this.
|
|
193
|
-
store.setServices(v);
|
|
318
|
+
for (const store of this._stores)
|
|
319
|
+
store.storage.setServices(v);
|
|
194
320
|
}
|
|
195
321
|
getServices() {
|
|
196
322
|
if (!this._services)
|
|
@@ -200,23 +326,17 @@ class WalletStorageManager {
|
|
|
200
326
|
getSettings() {
|
|
201
327
|
return this.getActive().getSettings();
|
|
202
328
|
}
|
|
203
|
-
async makeAvailable() {
|
|
204
|
-
return await this.runAsWriter(async (writer) => {
|
|
205
|
-
writer.makeAvailable();
|
|
206
|
-
return writer.getSettings();
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
329
|
async migrate(storageName, storageIdentityKey) {
|
|
210
330
|
return await this.runAsWriter(async (writer) => {
|
|
211
331
|
return writer.migrate(storageName, storageIdentityKey);
|
|
212
332
|
});
|
|
213
333
|
}
|
|
214
334
|
async destroy() {
|
|
215
|
-
if (this.
|
|
335
|
+
if (this._stores.length < 1)
|
|
216
336
|
return;
|
|
217
337
|
return await this.runAsWriter(async (writer) => {
|
|
218
|
-
for (const store of this.
|
|
219
|
-
await store.destroy();
|
|
338
|
+
for (const store of this._stores)
|
|
339
|
+
await store.storage.destroy();
|
|
220
340
|
});
|
|
221
341
|
}
|
|
222
342
|
async findOrInsertUser(identityKey) {
|
|
@@ -318,92 +438,129 @@ class WalletStorageManager {
|
|
|
318
438
|
return await reader.findProvenTxReqs(args);
|
|
319
439
|
});
|
|
320
440
|
}
|
|
321
|
-
async syncFromReader(identityKey, reader) {
|
|
441
|
+
async syncFromReader(identityKey, reader, activeSync, log = '') {
|
|
322
442
|
const auth = await this.getAuth();
|
|
323
443
|
if (identityKey !== auth.identityKey)
|
|
324
444
|
throw new index_client_1.sdk.WERR_UNAUTHORIZED();
|
|
325
445
|
const readerSettings = await reader.makeAvailable();
|
|
326
|
-
|
|
446
|
+
let inserts = 0, updates = 0;
|
|
447
|
+
log = await this.runAsSync(async (sync) => {
|
|
327
448
|
const writer = sync;
|
|
328
449
|
const writerSettings = this.getSettings();
|
|
329
|
-
|
|
330
|
-
let
|
|
450
|
+
log += `syncFromReader from ${readerSettings.storageName} to ${writerSettings.storageName}\n`;
|
|
451
|
+
let i = -1;
|
|
331
452
|
for (;;) {
|
|
453
|
+
i++;
|
|
332
454
|
const ss = await index_client_1.EntitySyncState.fromStorage(writer, identityKey, readerSettings);
|
|
333
455
|
const args = ss.makeRequestSyncChunkArgs(identityKey, writerSettings.storageIdentityKey);
|
|
334
456
|
const chunk = await reader.getSyncChunk(args);
|
|
457
|
+
if (chunk.user) {
|
|
458
|
+
// Merging state from a reader cannot update activeStorage
|
|
459
|
+
chunk.user.activeStorage = this._active.user.activeStorage;
|
|
460
|
+
}
|
|
335
461
|
const r = await writer.processSyncChunk(args, chunk);
|
|
336
462
|
inserts += r.inserts;
|
|
337
463
|
updates += r.updates;
|
|
338
|
-
|
|
464
|
+
log += `chunk ${i} inserted ${r.inserts} updated ${r.updates} ${r.maxUpdated_at}\n`;
|
|
339
465
|
if (r.done)
|
|
340
466
|
break;
|
|
341
467
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
async updateBackups(activeSync) {
|
|
347
|
-
const auth = await this.getAuth();
|
|
348
|
-
return await this.runAsSync(async (sync) => {
|
|
349
|
-
for (const backup of this.stores.slice(1)) {
|
|
350
|
-
await this.syncToWriter(auth, backup, sync);
|
|
351
|
-
}
|
|
468
|
+
log += `syncFromReader complete: ${inserts} inserts, ${updates} updates\n`;
|
|
469
|
+
return log;
|
|
352
470
|
}, activeSync);
|
|
471
|
+
return { inserts, updates, log };
|
|
353
472
|
}
|
|
354
|
-
async syncToWriter(auth, writer, activeSync) {
|
|
473
|
+
async syncToWriter(auth, writer, activeSync, log = '') {
|
|
355
474
|
const identityKey = auth.identityKey;
|
|
356
475
|
const writerSettings = await writer.makeAvailable();
|
|
357
|
-
|
|
476
|
+
let inserts = 0, updates = 0;
|
|
477
|
+
log = await this.runAsSync(async (sync) => {
|
|
358
478
|
const reader = sync;
|
|
359
479
|
const readerSettings = this.getSettings();
|
|
360
|
-
|
|
361
|
-
let
|
|
480
|
+
log += `syncToWriter from ${readerSettings.storageName} to ${writerSettings.storageName}\n`;
|
|
481
|
+
let i = -1;
|
|
362
482
|
for (;;) {
|
|
483
|
+
i++;
|
|
363
484
|
const ss = await index_client_1.EntitySyncState.fromStorage(writer, identityKey, readerSettings);
|
|
364
485
|
const args = ss.makeRequestSyncChunkArgs(identityKey, writerSettings.storageIdentityKey);
|
|
365
486
|
const chunk = await reader.getSyncChunk(args);
|
|
366
487
|
const r = await writer.processSyncChunk(args, chunk);
|
|
367
488
|
inserts += r.inserts;
|
|
368
489
|
updates += r.updates;
|
|
369
|
-
log +=
|
|
490
|
+
log += `chunk ${i} inserted ${r.inserts} updated ${r.updates} ${r.maxUpdated_at}\n`;
|
|
370
491
|
if (r.done)
|
|
371
492
|
break;
|
|
372
493
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
494
|
+
log += `syncToWriter complete: ${inserts} inserts, ${updates} updates\n`;
|
|
495
|
+
return log;
|
|
496
|
+
}, activeSync);
|
|
497
|
+
return { inserts, updates, log };
|
|
498
|
+
}
|
|
499
|
+
async updateBackups(activeSync) {
|
|
500
|
+
const auth = await this.getAuth(true);
|
|
501
|
+
return await this.runAsSync(async (sync) => {
|
|
502
|
+
for (const backup of this._backups) {
|
|
503
|
+
await this.syncToWriter(auth, backup.storage, sync);
|
|
504
|
+
}
|
|
376
505
|
}, activeSync);
|
|
377
506
|
}
|
|
378
507
|
/**
|
|
379
508
|
* Updates backups and switches to new active storage provider from among current backup providers.
|
|
380
509
|
*
|
|
510
|
+
* Also resolves conflicting actives.
|
|
511
|
+
*
|
|
381
512
|
* @param storageIdentityKey of current backup storage provider that is to become the new active provider.
|
|
382
513
|
*/
|
|
383
514
|
async setActive(storageIdentityKey) {
|
|
384
|
-
|
|
515
|
+
if (!this.isAvailable())
|
|
516
|
+
await this.makeAvailable();
|
|
517
|
+
// Confirm a valid storageIdentityKey: must match one of the _stores.
|
|
518
|
+
const newActiveIndex = this._stores.findIndex(s => s.settings.storageIdentityKey === storageIdentityKey);
|
|
385
519
|
if (newActiveIndex < 0)
|
|
386
|
-
throw new index_client_1.sdk.WERR_INVALID_PARAMETER('storageIdentityKey', `registered with this "WalletStorageManager"
|
|
387
|
-
|
|
520
|
+
throw new index_client_1.sdk.WERR_INVALID_PARAMETER('storageIdentityKey', `registered with this "WalletStorageManager". ${storageIdentityKey} does not match any managed store.`);
|
|
521
|
+
const identityKey = (await this.getAuth()).identityKey;
|
|
522
|
+
const newActive = this._stores[newActiveIndex];
|
|
523
|
+
let log = `setActive to ${newActive.settings.storageName}`;
|
|
524
|
+
if (newActiveIndex === 0 && this.isActiveEnabled)
|
|
388
525
|
/** Setting the current active as the new active is a permitted no-op. */
|
|
389
|
-
return
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
.
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
//
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
526
|
+
return ` unchanged\n`;
|
|
527
|
+
log += '\n';
|
|
528
|
+
log += await this.runAsSync(async (sync) => {
|
|
529
|
+
let log = '';
|
|
530
|
+
// Handle case where new active is current active to resolve conflicts.
|
|
531
|
+
// And where new active is one of the current conflict actives.
|
|
532
|
+
this._conflictingActives.push(this._active);
|
|
533
|
+
// Remove the new active from conflicting actives and
|
|
534
|
+
// set new active as the conflicting active that matches the target `storageIdentityKey`
|
|
535
|
+
this._conflictingActives = this._conflictingActives.filter(ca => {
|
|
536
|
+
const isNewActive = ca.settings.storageIdentityKey === storageIdentityKey;
|
|
537
|
+
if (isNewActive)
|
|
538
|
+
this._active = ca;
|
|
539
|
+
return !isNewActive;
|
|
540
|
+
});
|
|
541
|
+
if (this._conflictingActives.length > 0) {
|
|
542
|
+
// Merge state from conflicting actives into `_active`.
|
|
543
|
+
for (const conflict of this._conflictingActives) {
|
|
544
|
+
const sfr = await this.syncFromReader(identityKey, conflict.storage, newActive.storage);
|
|
545
|
+
log += sfr.log;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
for (const store of this._stores) {
|
|
549
|
+
// Update all store's user records to reflect new active store
|
|
550
|
+
await store.storage.setActive({ identityKey, userId: store.user.userId }, storageIdentityKey);
|
|
551
|
+
// Update cached user.activeStorage of all stores
|
|
552
|
+
store.user.activeStorage = storageIdentityKey;
|
|
553
|
+
// Push state merged from all conflicting actives to all non-active stores.
|
|
554
|
+
if (store.settings.storageIdentityKey !== storageIdentityKey) {
|
|
555
|
+
const stwr = await this.syncToWriter({ identityKey, userId: store.user.userId, isActive: false }, store.storage, newActive.storage);
|
|
556
|
+
log += stwr.log;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
this._isAvailable = false;
|
|
560
|
+
await this.makeAvailable();
|
|
561
|
+
return log;
|
|
406
562
|
});
|
|
563
|
+
return log;
|
|
407
564
|
}
|
|
408
565
|
}
|
|
409
566
|
exports.WalletStorageManager = WalletStorageManager;
|