core-3nweb-client-lib 0.48.6 → 0.49.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.
@@ -28,7 +28,7 @@ export declare class Core {
28
28
  private isInitialized;
29
29
  private constructor();
30
30
  static make(conf: CoreConf, makeNet: MakeNet, makeResolver: ServiceLocatorMaker, makeCryptor: makeCryptor, random: AsyncRNG): Core;
31
- start(): {
31
+ start(saveStorageKeyForAutologin?: (storageKey: Uint8Array) => void): {
32
32
  capsForStartup: web3n.startup.W3N;
33
33
  coreInit: Promise<string>;
34
34
  coreAppsInit: Promise<void>;
@@ -53,11 +53,11 @@ class Core {
53
53
  const core = new Core(makeNet, makeResolver, makeCryptor, random, dirs, logger, conf.signUpUrl);
54
54
  return core;
55
55
  }
56
- start() {
56
+ start(saveStorageKeyForAutologin) {
57
57
  const { promise: midPromise, resolve: midDone } = (0, deferred_1.defer)();
58
58
  const { watchBoot, emitBootEvent } = makeForBootEvents();
59
- const signUp = new sign_up_1.SignUp(this.signUpUrl, this.cryptor.cryptor, this.random, this.makeNet, this.appDirs.getUsersOnDisk, user => this.initForNewUser(user, midDone, emitBootEvent), watchBoot, this.logger.logError);
60
- const signIn = new sign_in_1.SignIn(this.cryptor.cryptor, addr => this.initForExistingUserWithoutCache(addr, midDone, emitBootEvent), (addr, storageKey) => this.initForExistingUserWithCache(addr, storageKey, midDone, emitBootEvent), this.appDirs.getUsersOnDisk, watchBoot, this.logger.logError);
59
+ const signUp = new sign_up_1.SignUp(this.signUpUrl, this.cryptor.cryptor, this.random, this.makeNet, this.appDirs.getUsersOnDisk, user => this.initForNewUser(user, midDone, emitBootEvent), saveStorageKeyForAutologin, watchBoot, this.logger.logError);
60
+ const signIn = new sign_in_1.SignIn(this.cryptor.cryptor, addr => this.initForExistingUserWithoutCache(addr, midDone, emitBootEvent), (addr, storageKey) => this.initForExistingUserWithCache(addr, storageKey, midDone, emitBootEvent), this.appDirs.getUsersOnDisk, saveStorageKeyForAutologin, watchBoot, this.logger.logError);
61
61
  const capsForStartup = {
62
62
  signUp: signUp.exposedService(),
63
63
  signIn: signIn.exposedService()
@@ -9,14 +9,15 @@ export type InitWithCache = (address: string, storageKey: GenerateKey) => Promis
9
9
  type SignInService = web3n.startup.SignInService;
10
10
  type ProgressCB = web3n.startup.ProgressCB;
11
11
  export declare class SignIn {
12
- private cryptor;
13
- private startInitWithoutCache;
14
- private initWithCache;
15
- private getUsersOnDisk;
12
+ private readonly cryptor;
13
+ private readonly startInitWithoutCache;
14
+ private readonly initWithCache;
15
+ private readonly getUsersOnDisk;
16
+ private readonly saveStorageKeyForAutologin;
16
17
  private readonly watchBoot;
17
18
  private readonly logError;
18
19
  private completeInitWithoutCache;
19
- constructor(cryptor: Cryptor, startInitWithoutCache: StartInitWithoutCache, initWithCache: InitWithCache, getUsersOnDisk: GetUsersOnDisk, watchBoot: SignInService['watchBoot'], logError: LogError);
20
+ constructor(cryptor: Cryptor, startInitWithoutCache: StartInitWithoutCache, initWithCache: InitWithCache, getUsersOnDisk: GetUsersOnDisk, saveStorageKeyForAutologin: ((storageKey: Uint8Array) => void) | undefined, watchBoot: SignInService['watchBoot'], logError: LogError);
20
21
  exposedService(): SignInService;
21
22
  private startLoginToRemoteStorage;
22
23
  private completeLoginAndLocalSetup;
@@ -21,11 +21,12 @@ exports.makeKeyGenProgressCB = makeKeyGenProgressCB;
21
21
  const key_derivation_1 = require("../../lib-client/key-derivation");
22
22
  const error_1 = require("../../lib-common/exceptions/error");
23
23
  class SignIn {
24
- constructor(cryptor, startInitWithoutCache, initWithCache, getUsersOnDisk, watchBoot, logError) {
24
+ constructor(cryptor, startInitWithoutCache, initWithCache, getUsersOnDisk, saveStorageKeyForAutologin, watchBoot, logError) {
25
25
  this.cryptor = cryptor;
26
26
  this.startInitWithoutCache = startInitWithoutCache;
27
27
  this.initWithCache = initWithCache;
28
28
  this.getUsersOnDisk = getUsersOnDisk;
29
+ this.saveStorageKeyForAutologin = saveStorageKeyForAutologin;
29
30
  this.watchBoot = watchBoot;
30
31
  this.logError = logError;
31
32
  this.completeInitWithoutCache = undefined;
@@ -51,6 +52,7 @@ class SignIn {
51
52
  }
52
53
  }
53
54
  async completeLoginAndLocalSetup(pass, progressCB) {
55
+ var _a;
54
56
  if (!this.completeInitWithoutCache) {
55
57
  throw new Error(`Call method startLoginToRemoteStorage() before calling this.`);
56
58
  }
@@ -58,18 +60,35 @@ class SignIn {
58
60
  const midKeyProgressCB = makeKeyGenProgressCB(0, 50, progressCB);
59
61
  const midKeyGen = async (params) => (await (0, key_derivation_1.deriveMidKeyPair)(this.cryptor, pass, params, midKeyProgressCB)).skey;
60
62
  const storeKeyProgressCB = makeKeyGenProgressCB(51, 100, progressCB);
61
- const storeKeyGen = params => (0, key_derivation_1.deriveStorageSKey)(this.cryptor, pass, params, storeKeyProgressCB);
62
- return await this.completeInitWithoutCache(midKeyGen, storeKeyGen);
63
+ let storeKey;
64
+ const storeKeyGen = async (params) => {
65
+ storeKey = await (0, key_derivation_1.deriveStorageSKey)(this.cryptor, pass, params, storeKeyProgressCB);
66
+ return storeKey;
67
+ };
68
+ const ok = await this.completeInitWithoutCache(midKeyGen, storeKeyGen);
69
+ if (ok) {
70
+ (_a = this.saveStorageKeyForAutologin) === null || _a === void 0 ? void 0 : _a.call(this, storeKey);
71
+ }
72
+ return ok;
63
73
  }
64
74
  catch (err) {
65
75
  throw await this.logAndWrap(err, 'Fail to initialize from a state without cache');
66
76
  }
67
77
  }
68
78
  async useExistingStorage(user, pass, progressCB) {
79
+ var _a;
69
80
  try {
81
+ let storeKey;
70
82
  const storeKeyProgressCB = makeKeyGenProgressCB(0, 99, progressCB);
71
- const storeKeyGen = params => (0, key_derivation_1.deriveStorageSKey)(this.cryptor, pass, params, storeKeyProgressCB);
72
- return await this.initWithCache(user, storeKeyGen);
83
+ const storeKeyGen = async (params) => {
84
+ storeKey = await (0, key_derivation_1.deriveStorageSKey)(this.cryptor, pass, params, storeKeyProgressCB);
85
+ return storeKey;
86
+ };
87
+ const ok = await this.initWithCache(user, storeKeyGen);
88
+ if (ok) {
89
+ (_a = this.saveStorageKeyForAutologin) === null || _a === void 0 ? void 0 : _a.call(this, storeKey);
90
+ }
91
+ return ok;
73
92
  }
74
93
  catch (err) {
75
94
  throw await this.logAndWrap(err, 'Failing to start in a state with cache');
@@ -18,6 +18,7 @@ export declare class SignUp {
18
18
  private readonly makeNet;
19
19
  private readonly getUsersOnDisk;
20
20
  private readonly initForNewUser;
21
+ private readonly saveStorageKeyForAutologin;
21
22
  private readonly watchBoot;
22
23
  private readonly logError;
23
24
  private mid;
@@ -25,7 +26,7 @@ export declare class SignUp {
25
26
  private serviceURL;
26
27
  private netLazyInit;
27
28
  private get net();
28
- constructor(serviceURL: string, cryptor: Cryptor, random: AsyncRNG, makeNet: () => NetClient, getUsersOnDisk: GetUsersOnDisk, initForNewUser: (u: CreatedUser) => Promise<void>, watchBoot: SignUpService['watchBoot'], logError: LogError);
29
+ constructor(serviceURL: string, cryptor: Cryptor, random: AsyncRNG, makeNet: () => NetClient, getUsersOnDisk: GetUsersOnDisk, initForNewUser: (u: CreatedUser) => Promise<void>, saveStorageKeyForAutologin: ((storageKey: Uint8Array) => void) | undefined, watchBoot: SignUpService['watchBoot'], logError: LogError);
29
30
  private setServiceURL;
30
31
  private getAvailableDomains;
31
32
  private getAvailableAddresses;
@@ -61,12 +61,13 @@ class SignUp {
61
61
  }
62
62
  return this.netLazyInit;
63
63
  }
64
- constructor(serviceURL, cryptor, random, makeNet, getUsersOnDisk, initForNewUser, watchBoot, logError) {
64
+ constructor(serviceURL, cryptor, random, makeNet, getUsersOnDisk, initForNewUser, saveStorageKeyForAutologin, watchBoot, logError) {
65
65
  this.cryptor = cryptor;
66
66
  this.random = random;
67
67
  this.makeNet = makeNet;
68
68
  this.getUsersOnDisk = getUsersOnDisk;
69
69
  this.initForNewUser = initForNewUser;
70
+ this.saveStorageKeyForAutologin = saveStorageKeyForAutologin;
70
71
  this.watchBoot = watchBoot;
71
72
  this.logError = logError;
72
73
  this.mid = undefined;
@@ -163,6 +164,7 @@ class SignUp {
163
164
  if (!accountCreated) {
164
165
  return false;
165
166
  }
167
+ const storeKey = this.store.skey;
166
168
  this.initForNewUser({
167
169
  address,
168
170
  midSKey: {
@@ -171,7 +173,8 @@ class SignUp {
171
173
  },
172
174
  storeSKey: this.store.skey,
173
175
  storeParams: this.store.params.kdParams
174
- });
176
+ })
177
+ .then(() => { var _a; return (_a = this.saveStorageKeyForAutologin) === null || _a === void 0 ? void 0 : _a.call(this, storeKey); });
175
178
  this.forgetKeys();
176
179
  return true;
177
180
  }
@@ -86,7 +86,6 @@ function completionAndObsOfProgress(progressCB) {
86
86
  progressCB(p);
87
87
  }
88
88
  },
89
- complete: () => completion === null || completion === void 0 ? void 0 : completion.reject(new Error()),
90
89
  error: err => completion === null || completion === void 0 ? void 0 : completion.reject(err)
91
90
  };
92
91
  return {