core-3nweb-client-lib 0.27.1 → 0.27.3

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.
Files changed (56) hide show
  1. package/build/api-defs/files.d.ts +4 -7
  2. package/build/core/asmail/inbox/attachments/fs.d.ts +2 -1
  3. package/build/core/asmail/inbox/attachments/fs.js +4 -3
  4. package/build/core/asmail/inbox/index.js +1 -1
  5. package/build/core/id-manager/index.d.ts +43 -0
  6. package/build/core/{id-manager.js → id-manager/index.js} +33 -114
  7. package/build/core/id-manager/key-storage.d.ts +21 -0
  8. package/build/core/id-manager/key-storage.js +96 -0
  9. package/build/core/index.js +21 -23
  10. package/build/core/sign-in.d.ts +1 -2
  11. package/build/core/sign-in.js +4 -13
  12. package/build/core/sign-up.d.ts +2 -0
  13. package/build/core/sign-up.js +2 -1
  14. package/build/core/storage/index.d.ts +4 -2
  15. package/build/core/storage/index.js +36 -57
  16. package/build/core/storage/local/storage.d.ts +1 -1
  17. package/build/core/storage/synced/obj-files-gc.d.ts +1 -4
  18. package/build/core/storage/synced/obj-files-gc.js +1 -18
  19. package/build/core/storage/synced/obj-files.d.ts +9 -1
  20. package/build/core/storage/synced/obj-files.js +41 -33
  21. package/build/core/storage/synced/obj-status.d.ts +18 -7
  22. package/build/core/storage/synced/obj-status.js +148 -83
  23. package/build/core/storage/synced/storage.d.ts +7 -2
  24. package/build/core/storage/synced/storage.js +50 -10
  25. package/build/core/storage/synced/upsyncer.d.ts +4 -4
  26. package/build/core/storage/synced/upsyncer.js +12 -6
  27. package/build/lib-client/3nstorage/exceptions.d.ts +13 -1
  28. package/build/lib-client/3nstorage/exceptions.js +9 -3
  29. package/build/lib-client/3nstorage/service.d.ts +6 -1
  30. package/build/lib-client/3nstorage/service.js +31 -15
  31. package/build/lib-client/3nstorage/util/file-based-json.js +2 -1
  32. package/build/lib-client/3nstorage/util/for-arrays.d.ts +1 -0
  33. package/build/lib-client/3nstorage/util/for-arrays.js +32 -0
  34. package/build/lib-client/3nstorage/xsp-fs/common.d.ts +5 -4
  35. package/build/lib-client/3nstorage/xsp-fs/common.js +1 -0
  36. package/build/lib-client/3nstorage/xsp-fs/file.js +2 -2
  37. package/build/lib-client/3nstorage/xsp-fs/folder-node.d.ts +11 -5
  38. package/build/lib-client/3nstorage/xsp-fs/folder-node.js +232 -68
  39. package/build/lib-client/3nstorage/xsp-fs/fs.js +15 -19
  40. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.d.ts +4 -9
  41. package/build/lib-client/3nstorage/xsp-fs/node-in-fs.js +16 -17
  42. package/build/lib-client/3nstorage/xsp-fs/xsp-payload-v1.js +1 -1
  43. package/build/lib-client/cryptor/cryptor-wasm.js +1 -1
  44. package/build/lib-client/cryptor/cryptor.wasm +0 -0
  45. package/build/lib-client/local-files/device-fs.js +11 -11
  46. package/build/lib-client/user-with-mid-session.d.ts +2 -1
  47. package/build/lib-client/user-with-mid-session.js +7 -1
  48. package/build/lib-common/async-fs-node.js +8 -8
  49. package/build/lib-common/exceptions/file.d.ts +4 -2
  50. package/build/lib-common/exceptions/file.js +24 -58
  51. package/build/lib-common/ipc/generic-ipc.js +5 -4
  52. package/build/lib-common/objs-on-disk/utils.js +1 -1
  53. package/build/lib-common/service-api/3nstorage/owner.d.ts +8 -5
  54. package/build/lib-common/service-api/3nstorage/owner.js +2 -1
  55. package/package.json +3 -2
  56. package/build/core/id-manager.d.ts +0 -46
@@ -18,10 +18,13 @@
18
18
 
19
19
  declare namespace web3n.files {
20
20
 
21
- interface FileException extends RuntimeException {
21
+ interface FileException extends RuntimeException, FileExceptionFlag {
22
22
  type: 'file';
23
23
  code: string|undefined;
24
24
  path: string;
25
+ }
26
+
27
+ interface FileExceptionFlag {
25
28
  notFound?: true;
26
29
  alreadyExists?: true;
27
30
  notDirectory?: true;
@@ -48,19 +51,13 @@ declare namespace web3n.files {
48
51
  alreadyExists: 'EEXIST';
49
52
  notDirectory: 'ENOTDIR';
50
53
  notFile: 'ENOTFILE';
51
- notLink: 'not-link';
52
54
  isDirectory: 'EISDIR';
53
55
  notEmpty: 'ENOTEMPTY';
54
56
  endOfFile: 'EEOF';
55
57
  opNotPermitted: 'EPERM';
56
58
  busy: 'EBUSY';
57
59
  ioError: 'EIO';
58
- concurrentUpdate: 'concurrent-update';
59
- parsingError: 'parsing-error';
60
60
  notImplemented: 'ENOSYS';
61
- isEndless: 'is-endless';
62
- storageClosed: 'storage-closed';
63
- versionMismatch: 'version-mismatch';
64
61
  }
65
62
 
66
63
  interface FSSyncException extends RuntimeException {
@@ -1,6 +1,7 @@
1
1
  import { StorageGetter, FolderInJSON } from '../../../../lib-client/3nstorage/xsp-fs/common';
2
2
  import { AsyncSBoxCryptor } from 'xsp-files';
3
3
  import { MsgOnDisk } from '../msg-on-disk';
4
+ import { LogError } from '../../../../lib-client/logging/log-to-file';
4
5
  declare type ReadonlyFS = web3n.files.ReadonlyFS;
5
- export declare function fsForAttachments(msg: MsgOnDisk, rootJson: FolderInJSON, storages: StorageGetter, cryptor: AsyncSBoxCryptor): ReadonlyFS;
6
+ export declare function fsForAttachments(msg: MsgOnDisk, rootJson: FolderInJSON, storages: StorageGetter, cryptor: AsyncSBoxCryptor, logError: LogError): ReadonlyFS;
6
7
  export {};
@@ -20,10 +20,11 @@ exports.fsForAttachments = void 0;
20
20
  const common_1 = require("../../../../lib-client/3nstorage/xsp-fs/common");
21
21
  const fs_1 = require("../../../../lib-client/3nstorage/xsp-fs/fs");
22
22
  class AttachmentStore {
23
- constructor(msg, getStorages, cryptor) {
23
+ constructor(msg, getStorages, cryptor, logError) {
24
24
  this.msg = msg;
25
25
  this.getStorages = getStorages;
26
26
  this.cryptor = cryptor;
27
+ this.logError = logError;
27
28
  this.type = 'asmail-msg';
28
29
  this.versioned = false;
29
30
  this.nodes = new common_1.NodesContainer();
@@ -66,8 +67,8 @@ class AttachmentStore {
66
67
  }
67
68
  Object.freeze(AttachmentStore.prototype);
68
69
  Object.freeze(AttachmentStore);
69
- function fsForAttachments(msg, rootJson, storages, cryptor) {
70
- const storage = new AttachmentStore(msg, storages, cryptor);
70
+ function fsForAttachments(msg, rootJson, storages, cryptor, logError) {
71
+ const storage = new AttachmentStore(msg, storages, cryptor, logError);
71
72
  const fs = fs_1.XspFS.fromASMailMsgRootFromJSON(storage, rootJson, 'attachments');
72
73
  return fs;
73
74
  }
@@ -297,7 +297,7 @@ class InboxOnServer {
297
297
  }
298
298
  const attachments = msg.attachmentsJSON;
299
299
  if (attachments) {
300
- m.attachments = (0, fs_1.fsForAttachments)(msgOnDisk, attachments, this.storages, this.cryptor);
300
+ m.attachments = (0, fs_1.fsForAttachments)(msgOnDisk, attachments, this.storages, this.cryptor, this.logError);
301
301
  }
302
302
  return m;
303
303
  }
@@ -0,0 +1,43 @@
1
+ import { user as mid } from '../../lib-common/mid-sigs-NaCl-Ed';
2
+ import { JsonKey } from '../../lib-common/jwkeys';
3
+ import { GenerateKey } from '../sign-in';
4
+ import { LogError, LogWarning } from '../../lib-client/logging/log-to-file';
5
+ import { NetClient } from '../../lib-client/request-utils';
6
+ import { ServiceLocator } from '../../lib-client/service-locator';
7
+ declare type WritableFS = web3n.files.WritableFS;
8
+ /**
9
+ * This function completes provisioning process, returning a promise, resolvable
10
+ * to either true, when all is done, or to false, when challenge reply is not
11
+ * accepted by the server.
12
+ */
13
+ export interface CompleteProvisioning {
14
+ keyParams: any;
15
+ complete(defaultSKey: Uint8Array): Promise<boolean>;
16
+ }
17
+ /**
18
+ * This returns a promise, resolvable to mailerId signer.
19
+ */
20
+ export declare type GetSigner = () => Promise<mid.MailerIdSigner>;
21
+ export declare type SetupManagerStorage = (fs: WritableFS, keysToSave?: JsonKey[]) => Promise<void>;
22
+ export declare class IdManager {
23
+ private readonly store;
24
+ private readonly makeNet;
25
+ private readonly midServiceFor;
26
+ private address;
27
+ private signer;
28
+ private provisioningProc;
29
+ private constructor();
30
+ static initWithoutStore(address: string, resolver: ServiceLocator, makeNet: () => NetClient, logError: LogError, logWarning: LogWarning): Promise<((midLoginKey: GenerateKey | Uint8Array) => Promise<{
31
+ idManager: IdManager;
32
+ setupManagerStorage: SetupManagerStorage;
33
+ } | undefined>) | undefined>;
34
+ static initFromCachedStore(address: string, fs: WritableFS, resolver: ServiceLocator, makeNet: () => NetClient, logError: LogError, logWarning: LogWarning): Promise<IdManager | undefined>;
35
+ private startProvisionWithoutSavedKey;
36
+ private provisionUsingSavedKey;
37
+ getId(): string;
38
+ getSigner: GetSigner;
39
+ isProvisionedAndValid(): boolean;
40
+ makeMailerIdCAP(): Service;
41
+ }
42
+ declare type Service = web3n.mailerid.Service;
43
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2015 - 2018, 2020 - 2021 3NSoft Inc.
3
+ Copyright (C) 2015 - 2018, 2020 - 2022 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -18,24 +18,21 @@
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.IdManager = void 0;
20
20
  const ecma_nacl_1 = require("ecma-nacl");
21
- const provisioner_1 = require("../lib-client/mailer-id/provisioner");
22
- const jwkeys_1 = require("../lib-common/jwkeys");
23
- const synced_1 = require("../lib-common/processes/synced");
24
- const login_1 = require("../lib-client/mailer-id/login");
21
+ const provisioner_1 = require("../../lib-client/mailer-id/provisioner");
22
+ const jwkeys_1 = require("../../lib-common/jwkeys");
23
+ const synced_1 = require("../../lib-common/processes/synced");
24
+ const login_1 = require("../../lib-client/mailer-id/login");
25
+ const key_storage_1 = require("./key-storage");
25
26
  const CERTIFICATE_DURATION_SECONDS = 16 * 60 * 60;
26
27
  const ASSERTION_VALIDITY = 15 * 60;
27
28
  const MIN_SECS_LEFT_ASSUMED_OK = 10 * 60;
28
- const LOGIN_KEY_FILE_NAME = 'login-keys';
29
29
  class IdManager {
30
- constructor(makeNet, midServiceFor, logError, logWarning, address, localFS) {
30
+ constructor(store, makeNet, midServiceFor, address) {
31
+ this.store = store;
31
32
  this.makeNet = makeNet;
32
33
  this.midServiceFor = midServiceFor;
33
- this.logError = logError;
34
- this.logWarning = logWarning;
35
34
  this.address = address;
36
35
  this.signer = undefined;
37
- this.localFS = undefined;
38
- this.syncedFS = undefined;
39
36
  this.provisioningProc = new synced_1.SingleProc();
40
37
  this.getSigner = async () => {
41
38
  if (!this.address) {
@@ -46,117 +43,46 @@ class IdManager {
46
43
  }
47
44
  return this.signer;
48
45
  };
49
- if (localFS) {
50
- this.localFS = localFS;
51
- }
52
46
  Object.seal(this);
53
47
  }
54
- static async initInOneStepWithoutStore(address, midLoginKey, resolver, makeNet, logError, logWarning) {
55
- const stepTwo = await IdManager.initWithoutStore(address, resolver, makeNet, logError, logWarning);
56
- if (!stepTwo) {
57
- throw new Error(`MailerId server doesn't recognize identity ${address}`);
58
- }
59
- return stepTwo(midLoginKey);
60
- }
61
48
  static async initWithoutStore(address, resolver, makeNet, logError, logWarning) {
62
- const idManager = new IdManager(makeNet, resolver, logError, logWarning, address);
63
- const completion = await idManager.provisionWithGivenKey(address);
64
- if (!completion) {
49
+ const { store, setupManagerStorage } = key_storage_1.IdKeysStorage.makeWithoutStorage(logError, logWarning);
50
+ const idManager = new IdManager(store, makeNet, resolver, address);
51
+ const provisioning = await idManager.startProvisionWithoutSavedKey(address);
52
+ if (!provisioning) {
65
53
  return;
66
54
  }
67
55
  return async (midLoginKey) => {
68
56
  const key = ((typeof midLoginKey === 'function') ?
69
- await midLoginKey(completion.keyParams) :
57
+ await midLoginKey(provisioning.keyParams) :
70
58
  midLoginKey);
71
- const isDone = await completion.complete(key);
59
+ const isDone = await provisioning.complete(key);
72
60
  key.fill(0);
73
- return (isDone ? idManager : undefined);
61
+ if (!isDone) {
62
+ return;
63
+ }
64
+ return {
65
+ idManager,
66
+ setupManagerStorage: (fs, keys) => setupManagerStorage(fs, (keys ? {
67
+ address: idManager.address,
68
+ keys
69
+ } : undefined))
70
+ };
74
71
  };
75
72
  }
76
- static async initFromLocalStore(address, localFS, resolver, makeNet, logError, logWarning) {
77
- const idMan = new IdManager(makeNet, resolver, logError, logWarning, address);
78
- if (localFS.type !== 'local') {
79
- throw new Error(`Expected local storage is typed as ${localFS.type}`);
80
- }
81
- idMan.localFS = localFS;
73
+ static async initFromCachedStore(address, fs, resolver, makeNet, logError, logWarning) {
74
+ const store = key_storage_1.IdKeysStorage.makeWithStorage(fs, logError, logWarning);
75
+ const idManager = new IdManager(store, makeNet, resolver, address);
82
76
  try {
83
- await idMan.provisionUsingSavedKey();
77
+ await idManager.provisionUsingSavedKey();
78
+ return idManager;
84
79
  }
85
80
  catch (err) {
86
81
  await logError(err, `Can't initialize id manager from local store`);
87
82
  return;
88
83
  }
89
- return idMan;
90
- }
91
- async ensureLocalCacheOfKeys() {
92
- if (!this.localFS || !this.syncedFS) {
93
- throw new Error(`Id manager's storages are not set.`);
94
- }
95
- const keysCached = await this.localFS.checkFilePresence(LOGIN_KEY_FILE_NAME);
96
- if (keysCached) {
97
- return;
98
- }
99
- try {
100
- const bytes = await this.syncedFS.readBytes(LOGIN_KEY_FILE_NAME);
101
- await this.localFS.writeBytes(LOGIN_KEY_FILE_NAME, bytes);
102
- bytes.fill(0);
103
- }
104
- catch (err) {
105
- await this.logError(err, `Fail to ensure local cache of MailerId login keys.`);
106
- }
107
- }
108
- async getSavedKey() {
109
- if (!this.localFS) {
110
- throw new Error(`Id manager's local storage is not set.`);
111
- }
112
- const json = await this.localFS.readJSONFile(LOGIN_KEY_FILE_NAME).catch(notFoundOrReThrow);
113
- if (json) {
114
- return json.keys[0];
115
- }
116
- if (this.syncedFS) {
117
- const json = await this.syncedFS.readJSONFile(LOGIN_KEY_FILE_NAME).catch(notFoundOrReThrow);
118
- if (json) {
119
- await this.ensureLocalCacheOfKeys();
120
- return json.keys[0];
121
- }
122
- else {
123
- await this.logWarning(`IdManager: there is no login MailerId login keys`);
124
- }
125
- }
126
- return;
127
- }
128
- async setStorages(localFS, syncedFS, keysToSave) {
129
- if (localFS) {
130
- if (localFS.type !== 'local') {
131
- throw new Error(`Expected local storage is typed as ${localFS.type}`);
132
- }
133
- this.localFS = localFS;
134
- }
135
- else {
136
- if (!this.localFS) {
137
- throw new Error(`Local storage is not given`);
138
- }
139
- }
140
- if (syncedFS.type !== 'synced') {
141
- throw new Error(`Expected synced storage is typed as ${syncedFS.type}`);
142
- }
143
- this.syncedFS = syncedFS;
144
- if (keysToSave) {
145
- const json = {
146
- address: this.address,
147
- keys: keysToSave
148
- };
149
- await this.localFS.writeJSONFile(LOGIN_KEY_FILE_NAME, json);
150
- await this.syncedFS.writeJSONFile(LOGIN_KEY_FILE_NAME, json);
151
- // XXX must add work with not-online condition
152
- await this.syncedFS.v.sync.upload(LOGIN_KEY_FILE_NAME);
153
- await this.syncedFS.v.sync.upload('');
154
- }
155
- else {
156
- await this.ensureLocalCacheOfKeys();
157
- }
158
84
  }
159
- async provisionWithGivenKey(address) {
85
+ async startProvisionWithoutSavedKey(address) {
160
86
  const midUrl = await this.midServiceFor(address);
161
87
  const provisioner = new provisioner_1.MailerIdProvisioner(address, midUrl, this.makeNet());
162
88
  try {
@@ -202,7 +128,7 @@ class IdManager {
202
128
  proc = this.provisioningProc.start(async () => {
203
129
  const midUrl = await this.midServiceFor(this.address);
204
130
  const provisioner = new provisioner_1.MailerIdProvisioner(this.address, midUrl, this.makeNet());
205
- const key = await this.getSavedKey();
131
+ const key = await this.store.getSavedKey();
206
132
  if (!key) {
207
133
  throw new Error(`No saved MailerId login key can be found`);
208
134
  }
@@ -245,15 +171,8 @@ class IdManager {
245
171
  }
246
172
  }
247
173
  exports.IdManager = IdManager;
248
- /**
249
- * This is a catch callback, which returns undefined on file(folder) not found
250
- * exception, and re-throws all other exceptions/errors.
251
- */
252
- function notFoundOrReThrow(exc) {
253
- if (!exc.notFound) {
254
- throw exc;
255
- }
256
- }
174
+ Object.freeze(IdManager.prototype);
175
+ Object.freeze(IdManager);
257
176
  async function doMidLogin(loginUrl, userId, net, signer) {
258
177
  const { sessionId, redirect } = await (0, login_1.startMidSession)(userId, net, loginUrl);
259
178
  if (!sessionId) {
@@ -0,0 +1,21 @@
1
+ import { LogError, LogWarning } from "../../lib-client/logging/log-to-file";
2
+ import { JsonKey } from "../../lib-common/jwkeys";
3
+ declare type WritableFS = web3n.files.WritableFS;
4
+ export interface LoginKeysJSON {
5
+ address: string;
6
+ keys: JsonKey[];
7
+ }
8
+ export declare class IdKeysStorage {
9
+ private readonly logError;
10
+ private readonly logWarning;
11
+ private fs;
12
+ private constructor();
13
+ static makeWithStorage(fs: WritableFS, logError: LogError, logWarning: LogWarning): IdKeysStorage;
14
+ static makeWithoutStorage(logError: LogError, logWarning: LogWarning): {
15
+ store: IdKeysStorage;
16
+ setupManagerStorage: (fs: WritableFS, keysToSave?: LoginKeysJSON) => Promise<void>;
17
+ };
18
+ getSavedKey(): Promise<JsonKey | undefined>;
19
+ private setStorageFS;
20
+ }
21
+ export {};
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (C) 2022 3NSoft Inc.
4
+
5
+ This program is free software: you can redistribute it and/or modify it under
6
+ the terms of the GNU General Public License as published by the Free Software
7
+ Foundation, either version 3 of the License, or (at your option) any later
8
+ version.
9
+
10
+ This program is distributed in the hope that it will be useful, but
11
+ WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ See the GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License along with
16
+ this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.IdKeysStorage = void 0;
20
+ const assert_1 = require("../../lib-common/assert");
21
+ const error_1 = require("../../lib-common/exceptions/error");
22
+ const LOGIN_KEY_FILE_NAME = 'login-keys';
23
+ class IdKeysStorage {
24
+ constructor(logError, logWarning, fs) {
25
+ this.logError = logError;
26
+ this.logWarning = logWarning;
27
+ this.fs = undefined;
28
+ if (fs) {
29
+ (0, assert_1.assert)(fs.type === 'synced');
30
+ this.fs = fs;
31
+ }
32
+ Object.seal(this);
33
+ }
34
+ static makeWithStorage(fs, logError, logWarning) {
35
+ return new IdKeysStorage(logError, logWarning, fs);
36
+ }
37
+ static makeWithoutStorage(logError, logWarning) {
38
+ const store = new IdKeysStorage(logError, logWarning);
39
+ return {
40
+ store,
41
+ setupManagerStorage: (fs, keys) => store.setStorageFS(fs, keys)
42
+ };
43
+ }
44
+ async getSavedKey() {
45
+ var _a, _b;
46
+ if (!((_b = (_a = this.fs) === null || _a === void 0 ? void 0 : _a.v) === null || _b === void 0 ? void 0 : _b.sync)) {
47
+ throw new Error(`Id manager's storages are not set.`);
48
+ }
49
+ try {
50
+ const json = await this.fs.readJSONFile(LOGIN_KEY_FILE_NAME);
51
+ return json.keys[0];
52
+ }
53
+ catch (exc) {
54
+ if (!exc.notFound) {
55
+ throw exc;
56
+ }
57
+ await this.fs.v.sync.updateStatusInfo('');
58
+ await this.fs.v.sync.adoptRemote('');
59
+ if (await this.fs.checkFilePresence(LOGIN_KEY_FILE_NAME)) {
60
+ return this.getSavedKey();
61
+ }
62
+ else {
63
+ await this.logWarning(`IdManager: no saved login MailerId keys`);
64
+ return;
65
+ }
66
+ }
67
+ }
68
+ async setStorageFS(fs, keysToSave) {
69
+ (0, assert_1.assert)(!this.fs);
70
+ (0, assert_1.assert)(fs.type === 'synced');
71
+ this.fs = fs;
72
+ if (keysToSave) {
73
+ await this.fs.writeJSONFile(LOGIN_KEY_FILE_NAME, keysToSave);
74
+ // XXX must add work with not-online condition
75
+ await this.fs.v.sync.upload(LOGIN_KEY_FILE_NAME);
76
+ await this.fs.v.sync.upload('');
77
+ }
78
+ else {
79
+ try {
80
+ await this.fs.readJSONFile(LOGIN_KEY_FILE_NAME);
81
+ }
82
+ catch (exc) {
83
+ throw (0, error_1.errWithCause)(exc, `Fail expection read of login MailerId keys from the storage`);
84
+ }
85
+ }
86
+ }
87
+ }
88
+ exports.IdKeysStorage = IdKeysStorage;
89
+ Object.freeze(IdKeysStorage.prototype);
90
+ Object.freeze(IdKeysStorage);
91
+ function notFoundOrReThrow(exc) {
92
+ if (!exc.notFound) {
93
+ throw exc;
94
+ }
95
+ }
96
+ Object.freeze(exports);
@@ -42,17 +42,23 @@ class Core {
42
42
  this.closingProc = undefined;
43
43
  this.initForNewUser = async (u) => {
44
44
  // 1) init of id manager without setting fs
45
- const idManager = await id_manager_1.IdManager.initInOneStepWithoutStore(u.address, u.midSKey.default, this.makeResolver('mailerid'), () => this.makeNet(), this.logger.logError, this.logger.logWarning);
46
- if (!idManager) {
45
+ const stepTwo = await id_manager_1.IdManager.initWithoutStore(u.address, this.makeResolver('mailerid'), () => this.makeNet(), this.logger.logError, this.logger.logWarning);
46
+ if (!stepTwo) {
47
+ throw new Error(`MailerId server doesn't recognize identity ${u.address}`);
48
+ }
49
+ // 2) complete id manager login, without use of fs
50
+ const idManagerInit = await stepTwo(u.midSKey.default);
51
+ if (!idManagerInit) {
47
52
  throw new Error(`Failed to provision MailerId identity`);
48
53
  }
49
- // 2) setup storage
50
- const storesUp = await this.storages.initFromRemote(u.address, idManager.getSigner, u.storeSKey, this.makeNet, this.makeResolver('3nstorage'), this.logger.logError);
54
+ const { idManager, setupManagerStorage } = idManagerInit;
55
+ // 3) initialize all storages
56
+ const storesUp = await this.storages.initFreshForNewUser(u.address, idManager.getSigner, u.storeParams, u.storeSKey, this.makeNet, this.makeResolver('3nstorage'), this.logger.logError);
51
57
  if (!storesUp) {
52
58
  throw new Error(`Stores failed to initialize`);
53
59
  }
54
60
  // 3) give id manager fs, in which it will record labeled key(s)
55
- await idManager.setStorages(await this.storages.makeLocalFSForApp(MAILERID_APP_NAME), await this.storages.makeSyncedFSForApp(MAILERID_APP_NAME), [u.midSKey.labeled]);
61
+ await setupManagerStorage(await this.storages.makeSyncedFSForApp(MAILERID_APP_NAME), [u.midSKey.labeled]);
56
62
  return idManager;
57
63
  };
58
64
  this.initForExistingUserWithoutCache = async (address) => {
@@ -63,17 +69,18 @@ class Core {
63
69
  }
64
70
  return async (midLoginKey, storageKey) => {
65
71
  // 2) complete id manager login, without use of fs
66
- const idManager = await stepTwo(midLoginKey);
67
- if (!idManager) {
72
+ const idManagerInit = await stepTwo(midLoginKey);
73
+ if (!idManagerInit) {
68
74
  return;
69
75
  }
76
+ const { idManager, setupManagerStorage } = idManagerInit;
70
77
  // 3) initialize all storages
71
78
  const storeDone = await this.storages.initFromRemote(address, idManager.getSigner, storageKey, this.makeNet, this.makeResolver('3nstorage'), this.logger.logError);
72
79
  if (!storeDone) {
73
80
  return;
74
81
  }
75
82
  // 4) complete initialization of id manager
76
- await idManager.setStorages(await this.storages.makeLocalFSForApp(MAILERID_APP_NAME), await this.storages.makeSyncedFSForApp(MAILERID_APP_NAME));
83
+ await setupManagerStorage(await this.storages.makeSyncedFSForApp(MAILERID_APP_NAME));
77
84
  return idManager;
78
85
  };
79
86
  };
@@ -82,21 +89,12 @@ class Core {
82
89
  if (!completeStorageInit) {
83
90
  return;
84
91
  }
85
- const idManager = await id_manager_1.IdManager.initFromLocalStore(address, await this.storages.makeLocalFSForApp(MAILERID_APP_NAME), this.makeResolver('mailerid'), () => this.makeNet(), this.logger.logError, this.logger.logWarning);
86
- if (idManager) {
87
- const res = await completeStorageInit(idManager.getSigner);
88
- await idManager.setStorages(undefined, await this.storages.makeSyncedFSForApp(MAILERID_APP_NAME));
89
- return (res ? idManager : undefined);
92
+ const idManager = await id_manager_1.IdManager.initFromCachedStore(address, await this.storages.makeSyncedFSForApp(MAILERID_APP_NAME), this.makeResolver('mailerid'), () => this.makeNet(), this.logger.logError, this.logger.logWarning);
93
+ if (!idManager) {
94
+ return;
90
95
  }
91
- return async (midLoginKey) => {
92
- const idManager = await id_manager_1.IdManager.initInOneStepWithoutStore(address, midLoginKey, this.makeResolver('mailerid'), () => this.makeNet(), this.logger.logError, this.logger.logWarning);
93
- if (!idManager) {
94
- return;
95
- }
96
- const res = await completeStorageInit(idManager.getSigner);
97
- await idManager.setStorages(await this.storages.makeLocalFSForApp(MAILERID_APP_NAME), await this.storages.makeSyncedFSForApp(MAILERID_APP_NAME));
98
- return (res ? idManager : undefined);
99
- };
96
+ completeStorageInit(idManager.getSigner);
97
+ return idManager;
100
98
  };
101
99
  this.closeBroadcast = new rxjs_1.Subject();
102
100
  this.close$ = this.closeBroadcast.asObservable();
@@ -112,7 +110,7 @@ class Core {
112
110
  return core;
113
111
  }
114
112
  start() {
115
- const signUp = new sign_up_1.SignUp(this.signUpUrl, this.cryptor.cryptor, () => this.makeNet(), this.appDirs.getUsersOnDisk, this.logger.logError);
113
+ const signUp = new sign_up_1.SignUp(this.signUpUrl, this.cryptor.cryptor, this.makeNet.bind(this), this.appDirs.getUsersOnDisk, this.logger.logError);
116
114
  const signIn = new sign_in_1.SignIn(this.cryptor.cryptor, this.initForExistingUserWithoutCache, this.initForExistingUserWithCache, this.appDirs.getUsersOnDisk, this.logger.logError);
117
115
  const capsForStartup = {
118
116
  signUp: signUp.exposedService(),
@@ -6,8 +6,7 @@ import { LogError } from '../lib-client/logging/log-to-file';
6
6
  export declare type GenerateKey = (derivParams: ScryptGenParams) => Promise<Uint8Array>;
7
7
  export declare type StartInitWithoutCache = (address: string) => Promise<CompleteInitWithoutCache | undefined>;
8
8
  export declare type CompleteInitWithoutCache = (midLoginKey: GenerateKey, storageKey: GenerateKey) => Promise<IdManager | undefined>;
9
- export declare type InitWithCache = (address: string, storageKey: GenerateKey) => Promise<IdManager | undefined | InitTwoWithCache>;
10
- export declare type InitTwoWithCache = (midLoginKey: GenerateKey) => Promise<IdManager | undefined>;
9
+ export declare type InitWithCache = (address: string, storageKey: GenerateKey) => Promise<IdManager | undefined>;
11
10
  declare type SignInService = web3n.startup.SignInService;
12
11
  export declare class SignIn {
13
12
  private cryptor;
@@ -63,23 +63,14 @@ class SignIn {
63
63
  try {
64
64
  const storeKeyProgressCB = makeKeyGenProgressCB(0, 99, progressCB);
65
65
  const storeKeyGen = params => (0, key_derivation_1.deriveStorageSKey)(this.cryptor, pass, params, storeKeyProgressCB);
66
- const res = await this.initWithCache(user, storeKeyGen);
67
- if (!res) {
68
- return false;
69
- }
70
- if (typeof res === 'object') {
71
- this.doneBroadcast.next(res);
66
+ const idManager = await this.initWithCache(user, storeKeyGen);
67
+ if (idManager) {
68
+ this.doneBroadcast.next(idManager);
72
69
  return true;
73
70
  }
74
- progressCB(49);
75
- const midKeyProgressCB = makeKeyGenProgressCB(50, 99, progressCB);
76
- const midKeyGen = async (params) => (await (0, key_derivation_1.deriveMidKeyPair)(this.cryptor, pass, params, midKeyProgressCB)).skey;
77
- const idManager = await res(midKeyGen);
78
- if (!idManager) {
71
+ else {
79
72
  return false;
80
73
  }
81
- this.doneBroadcast.next(idManager);
82
- return true;
83
74
  }
84
75
  catch (err) {
85
76
  throw await this.logAndWrap(err, 'Failing to start in a state with cache');
@@ -1,5 +1,6 @@
1
1
  import { NetClient } from '../lib-client/request-utils';
2
2
  import { JsonKey } from '../lib-common/jwkeys';
3
+ import * as keyDeriv from '../lib-client/key-derivation';
3
4
  import { GetUsersOnDisk } from './app-files';
4
5
  import { Cryptor } from '../lib-client/cryptor/cryptor';
5
6
  import { LogError } from '../lib-client/logging/log-to-file';
@@ -40,5 +41,6 @@ export interface CreatedUser {
40
41
  labeled: JsonKey;
41
42
  };
42
43
  storeSKey: Uint8Array;
44
+ storeParams: keyDeriv.ScryptGenParams;
43
45
  }
44
46
  export {};
@@ -96,7 +96,8 @@ class SignUp {
96
96
  default: this.mid.defaultSKey,
97
97
  labeled: this.mid.labeledSKey
98
98
  },
99
- storeSKey: this.store.skey
99
+ storeSKey: this.store.skey,
100
+ storeParams: this.store.params.kdParams
100
101
  });
101
102
  this.forgetKeys();
102
103
  return true;
@@ -2,6 +2,7 @@ import { GetSigner } from '../id-manager';
2
2
  import { GenerateKey } from '../sign-in';
3
3
  import { StorageGetter } from '../../lib-client/3nstorage/xsp-fs/common';
4
4
  import { ServiceLocator } from '../../lib-client/service-locator';
5
+ import { ScryptGenParams } from '../../lib-client/key-derivation';
5
6
  import { AsyncSBoxCryptor } from 'xsp-files';
6
7
  import { StoragePathForUser } from '../app-files';
7
8
  import { LogError } from '../../lib-client/logging/log-to-file';
@@ -30,8 +31,9 @@ export declare class Storages implements FactoryOfFSs {
30
31
  * This is a storage getter for links and linking in synced storage.
31
32
  */
32
33
  private storageGetterForSyncedStorage;
33
- startInitFromCache(user: string, keyGen: GenerateKey, makeNet: MakeNet, resolver: ServiceLocator, logError: LogError): Promise<((getSigner: GetSigner) => Promise<boolean>) | undefined>;
34
- initFromRemote(user: string, getSigner: GetSigner, keyOrGen: GenerateKey | Uint8Array, makeNet: MakeNet, resolver: ServiceLocator, logError: LogError): Promise<boolean>;
34
+ startInitFromCache(user: string, keyGen: GenerateKey, makeNet: MakeNet, resolver: ServiceLocator, logError: LogError): Promise<((getSigner: GetSigner) => void) | undefined>;
35
+ initFromRemote(user: string, getSigner: GetSigner, generateKey: GenerateKey, makeNet: MakeNet, resolver: ServiceLocator, logError: LogError): Promise<boolean>;
36
+ initFreshForNewUser(user: string, getSigner: GetSigner, params: ScryptGenParams, key: Uint8Array, makeNet: MakeNet, resolver: ServiceLocator, logError: LogError): Promise<boolean>;
35
37
  makeSyncedFSForApp(appFolder: string): Promise<WritableFS>;
36
38
  makeLocalFSForApp(appFolder: string): Promise<WritableFS>;
37
39
  getUserFS(type: StorageType): Promise<FSItem>;