@tinycloud/sdk-services 2.2.0-beta.12 → 2.2.0-beta.13

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/dist/index.cjs CHANGED
@@ -3024,6 +3024,9 @@ function base64Decode(str) {
3024
3024
  }
3025
3025
  return bytes;
3026
3026
  }
3027
+ function isUnlockSigner(signer) {
3028
+ return typeof signer === "object" && signer !== null && typeof signer.signMessage === "function";
3029
+ }
3027
3030
  function defaultVaultMessage(input) {
3028
3031
  switch (input.code) {
3029
3032
  case "DECRYPTION_FAILED":
@@ -3061,6 +3064,7 @@ var DataVaultService = class extends BaseService {
3061
3064
  this.masterKey = null;
3062
3065
  this.encryptionIdentity = null;
3063
3066
  this._isUnlocked = false;
3067
+ this.unlockInFlight = null;
3064
3068
  this.vaultConfig = config;
3065
3069
  this._config = config;
3066
3070
  }
@@ -3120,30 +3124,40 @@ var DataVaultService = class extends BaseService {
3120
3124
  * signatures exist (browser only).
3121
3125
  */
3122
3126
  async unlock(signer) {
3123
- return this.withTelemetry("unlock", void 0, async () => {
3127
+ const unlockSigner = isUnlockSigner(signer) ? signer : void 0;
3128
+ if (this._isUnlocked && this.masterKey && (this.encryptionIdentity || !unlockSigner)) {
3129
+ return { ok: true, data: void 0 };
3130
+ }
3131
+ if (this.unlockInFlight) {
3132
+ return this.unlockInFlight;
3133
+ }
3134
+ this.unlockInFlight = this.withTelemetry("unlock", void 0, async () => {
3124
3135
  const spaceId = this.vaultConfig.spaceId;
3125
3136
  const versionConfig = VaultVersionConfig[CURRENT_VAULT_VERSION];
3126
3137
  const masterCacheKey = `vault-master:${spaceId}`;
3127
3138
  const identityCacheKey = `vault-identity:${this.tc.address}`;
3128
3139
  try {
3129
- let masterSigBytes = await loadCachedSignature(masterCacheKey);
3130
- if (!masterSigBytes) {
3131
- if (!signer) {
3132
- return vaultError({
3133
- code: "VAULT_LOCKED",
3134
- message: "Signer is required when no cached master signature exists"
3135
- });
3140
+ if (!this.masterKey) {
3141
+ let masterSigBytes = await loadCachedSignature(masterCacheKey);
3142
+ if (!masterSigBytes) {
3143
+ if (!unlockSigner) {
3144
+ return vaultError({
3145
+ code: "VAULT_LOCKED",
3146
+ message: "Signer is required when no cached master signature exists"
3147
+ });
3148
+ }
3149
+ const sig = await unlockSigner.signMessage(
3150
+ versionConfig.masterMessage(spaceId)
3151
+ );
3152
+ masterSigBytes = toBytes(sig);
3153
+ await cacheSignature(masterCacheKey, masterSigBytes);
3136
3154
  }
3137
- const s = signer;
3138
- const sig = await s.signMessage(versionConfig.masterMessage(spaceId));
3139
- masterSigBytes = toBytes(sig);
3140
- await cacheSignature(masterCacheKey, masterSigBytes);
3155
+ this.masterKey = this.crypto.deriveKey(
3156
+ masterSigBytes,
3157
+ this.crypto.sha256(toBytes(spaceId)),
3158
+ toBytes("vault-master")
3159
+ );
3141
3160
  }
3142
- this.masterKey = this.crypto.deriveKey(
3143
- masterSigBytes,
3144
- this.crypto.sha256(toBytes(spaceId)),
3145
- toBytes("vault-master")
3146
- );
3147
3161
  const publicSpaceId = this.tc.makePublicSpaceId(this.tc.address, this.tc.chainId);
3148
3162
  let existingPubKey = null;
3149
3163
  try {
@@ -3166,13 +3180,14 @@ var DataVaultService = class extends BaseService {
3166
3180
  } else {
3167
3181
  let identitySigBytes = await loadCachedSignature(identityCacheKey);
3168
3182
  if (!identitySigBytes) {
3169
- if (!signer) {
3183
+ if (!unlockSigner) {
3170
3184
  this.encryptionIdentity = null;
3171
3185
  this._isUnlocked = true;
3172
3186
  return ok(void 0);
3173
3187
  }
3174
- const s = signer;
3175
- const sig = await s.signMessage(versionConfig.identityMessage);
3188
+ const sig = await unlockSigner.signMessage(
3189
+ versionConfig.identityMessage
3190
+ );
3176
3191
  identitySigBytes = toBytes(sig);
3177
3192
  await cacheSignature(identityCacheKey, identitySigBytes);
3178
3193
  }
@@ -3202,6 +3217,11 @@ var DataVaultService = class extends BaseService {
3202
3217
  });
3203
3218
  }
3204
3219
  });
3220
+ try {
3221
+ return await this.unlockInFlight;
3222
+ } finally {
3223
+ this.unlockInFlight = null;
3224
+ }
3205
3225
  }
3206
3226
  /**
3207
3227
  * Clear the cached vault signatures.