-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathVaultSource.ts
858 lines (811 loc) · 29.7 KB
/
VaultSource.ts
1
2
3
4
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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
import EventEmitter from "eventemitter3";
import { ChannelQueue } from "@buttercup/channel-queue";
import { Layerr } from "layerr";
import { Vault } from "./Vault.js";
import { Credentials } from "../credentials/Credentials.js";
import { getCredentials, setCredentials } from "../credentials/memory/credentials.js";
import { getMasterPassword, setMasterPassword } from "../credentials/memory/password.js";
import { getUniqueID } from "../tools/encoding.js";
import {
getSourceOfflineArchive,
sourceHasOfflineCopy,
storeSourceOfflineCopy
} from "../tools/vaultManagement.js";
import { credentialsToDatasource, prepareDatasourceCredentials } from "../datasources/register.js";
import { generateVaultInsights } from "../insight/vault.js";
import { AttachmentManager } from "../attachments/AttachmentManager.js";
import { TextDatasource } from "../datasources/TextDatasource.js";
import { VaultManager } from "./VaultManager.js";
import { convertFormatAVault } from "../io/formatB/conversion.js";
import { VaultFormatB } from "../index.common.js";
import { getFormatForID } from "../io/formatRouter.js";
import { VaultFormatID, VaultLiveSnapshot, VaultSourceID, VaultSourceStatus } from "../types.js";
interface StateChangeEnqueuedFunction {
(): void | Promise<any>;
}
export interface VaultSourceConfig {
colour?: string;
id?: VaultSourceID;
order?: number;
meta?: VaultSourceMetadata;
}
export interface VaultSourceSaveOptions {
storeOfflineCopy?: boolean;
}
export interface VaultSourceUnlockOptions {
initialiseRemote?: boolean;
loadOfflineCopy?: boolean;
storeOfflineCopy?: boolean;
}
export interface VaultSourceMetadata {
[property: string]: any;
}
const COLOUR_TEST = /^#([a-f0-9]{3}|[a-f0-9]{6})$/i;
const DEFAULT_COLOUR = "#000000";
const DEFAULT_ORDER = 1000;
function processDehydratedCredentials(
credentialsString: string,
masterPassword: string
): Promise<Credentials> {
if (/^v1\n/.test(credentialsString)) {
const [, sourceCredStr] = credentialsString.split("\n");
return Credentials.fromSecureString(sourceCredStr, masterPassword);
}
return Credentials.fromSecureString(credentialsString, masterPassword);
}
/**
* Vault source class for managing a single vault
* within a vault manager
* @augments EventEmitter
* @memberof module:Buttercup
*/
export class VaultSource extends EventEmitter {
static STATUS_LOCKED = VaultSourceStatus.Locked;
static STATUS_PENDING = VaultSourceStatus.Pending;
static STATUS_UNLOCKED = VaultSourceStatus.Unlocked;
/**
* Rehydrate the vault source from a dehydrated state
* @param dehydratedString The dehydrated form of the vault source
* @returns A rehydrated instance
* @memberof VaultSource
* @static
*/
static rehydrate(dehydratedString: string): VaultSource {
const target = JSON.parse(dehydratedString);
let credentials = target.credentials;
if (target.v !== 2) {
const { sourceCredentials, archiveCredentials } = target;
if (!sourceCredentials || !archiveCredentials) {
throw new Error("Invalid legacy vault state: missing credentials");
}
credentials = `v1\n${sourceCredentials}\n${archiveCredentials}`;
}
const {
id,
name,
type,
colour = DEFAULT_COLOUR,
order = DEFAULT_ORDER,
meta = {}
} = target;
return new VaultSource(name, type, credentials, {
id,
colour,
order,
meta
});
}
_attachmentManager: AttachmentManager = null;
_colour: string;
_credentials: string | Credentials;
_datasource: TextDatasource = null;
_id: VaultSourceID;
_meta: VaultSourceMetadata;
_name: string;
_order: number;
_queue: ChannelQueue;
_shares: Array<any> = [];
_status: VaultSourceStatus;
_type: string;
_vault: Vault = null;
_vaultManager: VaultManager = null;
constructor(
name: string,
type: string,
credentialsString: string,
config: VaultSourceConfig = {}
) {
super();
const {
colour = DEFAULT_COLOUR,
id = getUniqueID(),
order = DEFAULT_ORDER,
meta = {}
} = config;
// Queue for managing state transitions
this._queue = new ChannelQueue();
// Credentials state and status go hand-in-hand:
// - Locked = credentials string
// - Unlocked = credentials instance
this._credentials = credentialsString;
this._status = VaultSource.STATUS_LOCKED;
// Set other configuration items to properties
this._id = id;
this._name = name;
this._type = type;
this._colour = colour;
this._order = order;
this._meta = meta;
}
/**
* The attachment manager
* @memberof VaultSource
* @readonly
*/
get attachmentManager(): AttachmentManager {
return this._attachmentManager;
}
/**
* Source colour
* @memberof VaultSource
*/
get colour(): string {
return this._colour;
}
/**
* Source ID
* @memberof VaultSource
* @readonly
*/
get id(): VaultSourceID {
return this._id;
}
/**
* Meta data
* @memberof VaultSource
* @readonly
*/
get meta(): VaultSourceMetadata {
return { ...this._meta };
}
/**
* Source name
* @memberof VaultSource
* @readonly
*/
get name() {
return this._name;
}
/**
* The vault order on a vault management instance
* @memberof VaultSource
* @readonly
*/
get order(): number {
return this._order;
}
/**
* Source status
* @memberof VaultSource
* @readonly
*/
get status(): VaultSourceStatus {
return this._status;
}
/**
* The datasource type
* @memberof VaultSource
* @readonly
*/
get type(): string {
return this._type;
}
/**
* Vault reference
* @memberof VaultSource
* @readonly
*/
get vault(): Vault {
return this._vault;
}
set colour(newColour: string) {
if (COLOUR_TEST.test(newColour) !== true) {
throw new Layerr(`Failed setting colour: Invalid format (expected hex): ${newColour}`);
}
this._colour = newColour;
this.emit("updated");
}
set order(newOrder: number) {
if (isNaN(newOrder) || typeof newOrder !== "number" || newOrder < 0) {
throw new Layerr(
`Failed setting order: Order must be greater than or equal to 0: ${newOrder}`
);
}
this._order = newOrder;
this.emit("updated");
}
/**
* Change the master vault password
* @param oldPassword The original/current password
* @param newPassword The new password to change to
* @param meta Optional metadata
* @memberof VaultSource
*/
async changeMasterPassword(
oldPassword: string,
newPassword: string,
meta: { [key: string]: any } = {}
) {
if (oldPassword === newPassword) {
throw new Error("New password cannot be the same as the previous one");
} else if (!newPassword) {
throw new Error("New password must be specified");
}
const datasourceSupportsChange = this._datasource.supportsPasswordChange();
const newMasterCreds = new Credentials(meta, newPassword);
let wasLocked = false;
if (this.status !== VaultSource.STATUS_UNLOCKED) {
wasLocked = true;
// Locked, so unlock
await this.unlock(Credentials.fromPassword(oldPassword));
} else {
// Unlocked, so check password..
const masterPassword = getMasterPassword((<Credentials>this._credentials).id);
if (masterPassword !== oldPassword) {
throw new Error("Old password does not match current unlocked instance value");
}
// ..and then update
await this.update();
}
// Check datasource is ready
if (datasourceSupportsChange) {
const isReady = await this._datasource.changePassword(
prepareDatasourceCredentials(newMasterCreds, this._datasource.type),
/* preflight: */ true
);
if (!isReady) {
throw new Error("Datasource not capable of changing password at this time");
}
}
// Clear offline cache
await storeSourceOfflineCopy(this._vaultManager._cacheStorage, this.id, null);
// Change password
const newCredentials = Credentials.fromCredentials(
this._credentials as Credentials,
oldPassword
);
setMasterPassword(newCredentials.id, newPassword);
await this._updateVaultCredentials(newCredentials);
// Re-lock if it was locked earlier
if (wasLocked) {
await this.lock();
}
// Change remote if supported
if (datasourceSupportsChange) {
await this._datasource.changePassword(
prepareDatasourceCredentials(newMasterCreds, this._datasource.type),
/* preflight: */ false
);
}
this.emit("passwordChanged");
this.emit("updated");
}
/**
* Check if the vault source can be updated
* @memberof VaultSource
*/
canBeUpdated(): boolean {
return this.status === VaultSource.STATUS_UNLOCKED && this._vault.format.dirty === false;
}
/**
* Check if the source has an offline copy
* @returns A promise which resolves with whether an offline
* copy is available or not
* @memberof VaultSource
*/
checkOfflineCopy() {
return sourceHasOfflineCopy(this._vaultManager._cacheStorage, this.id);
}
/**
* Convert vault to a new format
* @param targetFormat The target format to convert to
* @memberof VaultSource
*/
async convert(targetFormat: VaultFormatID): Promise<void> {
if (this.status !== VaultSource.STATUS_UNLOCKED) {
throw new Layerr(
`Failed converting source: Source not unlocked (${this.status}): ${this.id}`
);
}
if (this.vault.format.getFormat().getFormatID() !== VaultFormatID.A) {
throw new Layerr(`Failed converting source: Source not in expected format: ${this.id}`);
}
if (targetFormat !== VaultFormatID.B) {
throw new Layerr(`Failed converting source: Target format not valid: ${this.id}`);
}
const formatBSource = convertFormatAVault(this.vault.format.source);
const format = new VaultFormatB(formatBSource);
this._vault = new Vault(format);
}
/**
* Dehydrate the source to a JSON string, ready for storage
* @memberof VaultSource
*/
dehydrate(): Promise<string> {
return this._enqueueStateChange(async () => {
const payload = {
v: 2,
id: this.id,
name: this.name,
type: this.type,
status: VaultSource.STATUS_LOCKED,
colour: this.colour,
order: this.order,
meta: this.meta,
credentials: null
};
if (this.status === VaultSource.STATUS_PENDING) {
throw new Layerr(`Failed dehydrating source: Source in pending state: ${this.id}`);
} else if (this.status === VaultSource.STATUS_LOCKED) {
payload.credentials = this._credentials;
} else {
payload.credentials = await (<Credentials>this._credentials).toSecureString();
}
return JSON.stringify(payload);
});
}
/**
* Get a live snapshot of the current unlocked state
* @returns A snapshot object
* @deprecated Will be removed in next major - insecure
*/
getLiveSnapshot(): VaultLiveSnapshot {
if (this.status !== VaultSourceStatus.Unlocked) {
throw new Layerr("Not possible to fetch live snapshot: Vault is not unlocked");
}
const credentialsID = (this._credentials as Credentials).id;
const credentials = getCredentials(credentialsID);
if (!credentials) {
throw new Layerr("Failed fetching live snapshot: Invalid credentials data");
}
const masterPassword = getMasterPassword(credentialsID);
return {
credentials,
formatID: this.vault._format.getFormat().getFormatID(),
formatSource: this.vault._format.source,
masterPassword,
sourceID: this.id,
version: "1a"
};
}
/**
* Get offline content, if it exists
* @returns A promise a resolves with the content, or null
* if it doesn't exist
* @memberof VaultSource
*/
async getOfflineContent(): Promise<string | null> {
const hasContent = await this.checkOfflineCopy();
if (hasContent) {
return getSourceOfflineArchive(this._vaultManager._cacheStorage, this.id);
}
return null;
}
/**
* Detect whether the local archives (in memory) differ from their remote copies
* Fetches the remote copies from their datasources and detects differences between
* them and their local counterparts. Does not change/update the local items.
* @returns A promise that resolves with a boolean - true if
* there are differences, false if there is not
* @memberof VaultSource
*/
localDiffersFromRemote(): Promise<boolean> {
if (this.status !== VaultSource.STATUS_UNLOCKED) {
return Promise.reject(
new Layerr(
`Failed diffing source: Source not unlocked (${this.status}): ${this.id}`
)
);
}
if (typeof (<any>this._datasource).localDiffersFromRemote === "function") {
return (<any>this._datasource).localDiffersFromRemote(
prepareDatasourceCredentials(
this._credentials as Credentials,
this._datasource.type
),
this.vault.format.history
);
}
if (this._datasource.type !== "text") {
// Only clear if not a TextDatasource
this._datasource.setContent("");
}
return this._datasource
.load(
prepareDatasourceCredentials(
this._credentials as Credentials,
this._datasource.type
)
)
.then(({ Format, history }) => {
if (Format !== this.vault.format.getFormat()) {
throw new Error("Loaded format does not match that of current vault");
}
return Format.historiesDiffer(this.vault.format.history, history);
});
}
/**
* Lock the source
* @memberof VaultSource
*/
async lock() {
if (this.status !== VaultSource.STATUS_UNLOCKED) {
throw new Layerr(
`Failed locking source: Source in invalid state (${this.status}): ${this.id}`
);
}
await this._enqueueStateChange(async () => {
this._status = VaultSource.STATUS_PENDING;
const currentCredentials = this._credentials;
const currentVault = this._vault;
const currentDatasource = this._datasource;
const currentAttachmentMgr = this._attachmentManager;
try {
const credentialsStr = await (<Credentials>this._credentials).toSecureString();
this._credentials = credentialsStr;
this._datasource = null;
this._vault = null;
this._attachmentManager = null;
this._status = VaultSource.STATUS_LOCKED;
this.emit("locked");
} catch (err) {
this._credentials = currentCredentials;
this._datasource = currentDatasource;
this._vault = currentVault;
this._status = VaultSource.STATUS_UNLOCKED;
this._attachmentManager = currentAttachmentMgr;
throw new Layerr(err, "Failed locking source");
}
});
}
/**
* Merge remote contents
* Detects differences between a local and a remote item, and merges the
* two copies together.
* @returns A promise that resolves with the newly merged archive -
* This archive is automatically saved over the original local copy.
* @memberof VaultSource
*/
async mergeFromRemote(): Promise<Vault> {
if (this._datasource.type !== "text") {
// Only clear if not a TextDatasource
this._datasource.setContent("");
}
const { Format, history } = await this._datasource.load(
prepareDatasourceCredentials(this._credentials as Credentials, this._datasource.type)
);
if (Format !== this._vault.format.getFormat()) {
throw new Error("Format loaded during merge did not match current");
}
const newVault = Format.vaultFromMergedHistories(history, this._vault.format.history);
this._vault._updateFormat(newVault.format);
return this._vault;
}
/**
* Rename the vault source
* @param name The new name
* @memberof VaultSource
*/
rename(name: string): void {
this._name = name;
this.emit("updated");
}
/**
* Restore unlocked state from a live snapshot
* @param snapshot The snapshot taken previously
* @deprecated Will be removed in next major - insecure
*/
async restoreFromLiveSnapshot(snapshot: VaultLiveSnapshot): Promise<void> {
if (this.status !== VaultSourceStatus.Locked) {
throw new Layerr("Cannot restore live snapshot: Vault is not locked");
}
if (snapshot.version !== "1a") {
throw new Layerr(
`Failed restoring live snapshot: Snapshot version unsupported or unrecognised: ${snapshot.version}`
);
}
// Setup credentials and datasource
const credentials = (this._credentials = new Credentials(
snapshot.credentials.data,
snapshot.masterPassword
));
setCredentials(credentials.id, snapshot.credentials);
// Initialise datasource
const datasource = (this._datasource = credentialsToDatasource(
Credentials.fromCredentials(credentials, snapshot.masterPassword)
));
datasource.sourceID = this.id;
// Setup vault
const Format = getFormatForID(snapshot.formatID);
this._vault = new Vault(new Format(snapshot.formatSource));
// Set statuses
this._status = VaultSource.STATUS_UNLOCKED;
this._attachmentManager = new AttachmentManager(this);
this.emit("unlocked");
}
/**
* Save the vault to the remote, ensuring that it's first merged and
* updated to prevent conflicts or overwrites.
* @memberof VaultSource
*/
async save(config: VaultSourceSaveOptions = {}) {
const { storeOfflineCopy = true } = config;
await this._enqueueStateChange(async () => {
if (await this.localDiffersFromRemote()) {
await this.mergeFromRemote();
}
// Capture encrypted content
let encryptedContent: string = null;
const encryptedCallback = ({ content }) => {
encryptedContent = content;
};
this._datasource.once("encryptedContent", encryptedCallback);
// Save
try {
await this._datasource.save(
this._vault.format.history,
prepareDatasourceCredentials(
this._credentials as Credentials,
this._datasource.type
)
);
} catch (err) {
this._datasource.off("encryptedContent", encryptedCallback);
throw err;
}
// Clear state
this._vault.format.dirty = false;
// Handle offline state
if (storeOfflineCopy && encryptedContent) {
// Store an offline copy for later use
await storeSourceOfflineCopy(
this._vaultManager._cacheStorage,
this.id,
encryptedContent
);
}
// Misc
await this._updateInsights();
}, /* stack */ "saving");
this.emit("updated");
}
supportsAttachments(): boolean {
if (this.status !== VaultSource.STATUS_UNLOCKED) return false;
return this._datasource.supportsAttachments();
}
async testMasterPassword(password: string): Promise<boolean> {
if (
this.status !== VaultSourceStatus.Locked &&
this.status !== VaultSourceStatus.Unlocked
) {
throw new Error(`Source in invalid state for password test: ${this.status}`);
}
const credStr =
this.status === VaultSourceStatus.Locked
? (this._credentials as string)
: await (<Credentials>this._credentials).toSecureString();
try {
await processDehydratedCredentials(credStr, password);
return true;
} catch (err) {
return false;
}
}
async unlock(vaultCredentials: Credentials, config: VaultSourceUnlockOptions = {}) {
if (!Credentials.isCredentials(vaultCredentials)) {
throw new Layerr(
`Failed unlocking source: Invalid credentials passed to source: ${this.id}`
);
}
const {
initialiseRemote = false,
loadOfflineCopy = false,
storeOfflineCopy = true
} = config;
if (this.status !== VaultSource.STATUS_LOCKED) {
throw new Layerr(
`Failed unlocking source: Source in invalid state (${this.status}): ${this.id}`
);
}
const masterPassword = getMasterPassword(vaultCredentials.id);
const originalCredentials = this._credentials;
this._status = VaultSource.STATUS_PENDING;
await this._enqueueStateChange(async () => {
// Get offline content if available and requested
const offlineContent = loadOfflineCopy ? await this.getOfflineContent() : null;
const credentials: Credentials = (this._credentials =
await processDehydratedCredentials(this._credentials as string, masterPassword));
// Initialise datasource
const datasource = (this._datasource = credentialsToDatasource(
Credentials.fromCredentials(credentials, masterPassword)
));
datasource.sourceID = this.id;
if (typeof offlineContent === "string") {
datasource.setContent(offlineContent);
}
// Listen for datasource updates
datasource.on("updated", () => {
this._waitNonPending()
.then(async () => {
if (this.status === VaultSource.STATUS_UNLOCKED) {
await this._updateCredentialsFromDatasource();
}
this.emit("updated");
})
.catch((err) => {
console.error(
`Error updating datasource credentials for vault: ${this.id}`,
err
);
});
});
// Perform pre-save or load
if (initialiseRemote) {
const defaultVault = Vault.createWithDefaults();
await datasource.save(defaultVault.format.history, credentials);
this._vault = defaultVault;
} else {
const { Format, history } = await datasource.load(credentials);
this._vault = Vault.createFromHistory(history, Format);
}
// Optimise storage
try {
this._vault.format.optimise();
} catch (err) {
throw new Layerr(err, "Failed optimising vault format");
}
// Handle offline state
if (storeOfflineCopy) {
// Store an offline copy for later use
await storeSourceOfflineCopy(
this._vaultManager._cacheStorage,
this.id,
datasource._content
);
}
if (loadOfflineCopy) {
// Flag the format as read-only
this.vault.format._readOnly = true;
}
// Configure source status
this._status = VaultSource.STATUS_UNLOCKED;
this._attachmentManager = new AttachmentManager(this);
this.emit("unlocked");
}).catch((err) => {
this._status = VaultSource.STATUS_LOCKED;
this._vault = null;
this._datasource = null;
this._credentials = originalCredentials;
this._attachmentManager = null;
throw new Layerr(err, "Failed unlocking source");
});
}
/**
* Update the vault
* @returns A promise that resolves once the update has
* completed
* @memberof VaultSource
*/
async update({ skipDiff = false } = {}) {
const didUpdate = await this._enqueueStateChange(
() =>
(skipDiff ? Promise.resolve(false) : this.localDiffersFromRemote()).then(
(differs) => {
if (differs) {
return this.mergeFromRemote().then(() => true);
}
return false;
}
),
// @todo shares
// .then(() => initialiseShares(this)),
/* stack */ "updating"
);
if (didUpdate) {
this.emit("updated");
}
}
/**
* Write the vault to the remote
* - This does not perform any merging or sync checks, but simply
* writes the vault contents to the remote, overwriting whatever
* was there before.
* @returns A promise that resolves when saving has completed
* @memberof VaultSource
*/
async write() {
await this._enqueueStateChange(async () => {
await this._datasource.save(
this._vault.format.history,
prepareDatasourceCredentials(
this._credentials as Credentials,
this._datasource.type
)
);
this._vault.format.dirty = false;
await this._updateInsights();
}, /* stack */ "saving");
this.emit("updated");
}
_applyShares() {
// @todo
// this._shares.forEach(share => {
// if (!share.archiveHasAppliedShare(this.archive)) {
// share.applyToArchive(this.archive);
// }
// });
}
_enqueueStateChange(cb: StateChangeEnqueuedFunction, stack?: string): Promise<any> {
const channel = this._queue.channel("state");
return stack ? channel.enqueue(cb, undefined, stack) : channel.enqueue(cb);
}
_unloadShares() {
const Format = this.vault.format.getFormat();
const extractedShares = Format.extractSharesFromHistory(this.vault.format.history);
// Reset archive history (without shares)
const { base } = extractedShares;
delete extractedShares.base;
this.vault.format.erase();
this.vault.format.execute(base);
// Update share payloads
Object.keys(extractedShares).forEach((shareID) => {
const share = this._shares.find((share) => share.id === shareID);
if (!share) {
throw new Error(
`Failed updating extracted share: No share found in workspace for ID: ${shareID}`
);
}
share.updateHistory(extractedShares[shareID]);
});
}
async _updateCredentialsFromDatasource() {
if (this.status !== VaultSource.STATUS_UNLOCKED) {
throw new Layerr(
`Failed updating source credentials: Source is not unlocked: ${this.id}`
);
}
const masterPassword = getMasterPassword((<Credentials>this._credentials).id);
this._credentials = Credentials.fromCredentials(
this._datasource.credentials,
masterPassword
);
}
async _updateInsights() {
if (this.status !== VaultSource.STATUS_UNLOCKED) {
throw new Layerr(`Failed updating vault insights: Source is not unlocked: ${this.id}`);
}
const insights = generateVaultInsights(this.vault);
await this._datasource.updateInsights(insights);
}
async _updateVaultCredentials(newCredentials) {
if (this.status !== VaultSource.STATUS_UNLOCKED) {
throw new Layerr(
`Failed updating vault credentials: Source is not unlocked: ${this.id}`
);
}
this._credentials = newCredentials;
await this.write();
}
_waitNonPending() {
return new Promise<void>((resolve) => {
if (this.status !== VaultSource.STATUS_PENDING) return resolve();
const handleChange = () => {
this.removeListener("unlocked", handleChange);
this.removeListener("locked", handleChange);
resolve();
};
this.on("unlocked", handleChange);
this.on("locked", handleChange);
});
}
}