@anuma/sdk 4.2.0 → 4.2.1
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/expo/{chunk-VBO6KSNQ.mjs → chunk-3CDSIN22.mjs} +304 -49
- package/dist/expo/index.cjs +307 -49
- package/dist/expo/index.d.mts +92 -7
- package/dist/expo/index.d.ts +92 -7
- package/dist/expo/index.mjs +7 -1
- package/dist/expo/{recall-OCQOA7M3.mjs → recall-NARBDQAK.mjs} +1 -1
- package/dist/index.cjs +31 -17
- package/dist/index.mjs +31 -17
- package/dist/react/{chunk-BS7C4CUW.mjs → chunk-CUJDSH55.mjs} +304 -49
- package/dist/react/index.cjs +307 -49
- package/dist/react/index.d.mts +86 -7
- package/dist/react/index.d.ts +86 -7
- package/dist/react/index.mjs +7 -1
- package/dist/react/{recall-WXCZHXHZ.mjs → recall-FPPCKYYH.mjs} +1 -1
- package/dist/server/{chunk-WADHFDMA.mjs → chunk-UQDYBQVF.mjs} +304 -49
- package/dist/server/index.cjs +307 -49
- package/dist/server/index.d.mts +85 -6
- package/dist/server/index.d.ts +85 -6
- package/dist/server/index.mjs +7 -1
- package/dist/server/{recall-QTVT7HCZ.mjs → recall-ZHEPQNEU.mjs} +1 -1
- package/package.json +1 -1
|
@@ -14277,8 +14277,21 @@ import { v7 as uuidv72 } from "uuid";
|
|
|
14277
14277
|
|
|
14278
14278
|
// src/react/useEncryption.ts
|
|
14279
14279
|
var SIGN_MESSAGE = "The app is asking you to sign this message to generate a key, which will be used to encrypt data.";
|
|
14280
|
+
var EncryptionKeyMissingError = class extends Error {
|
|
14281
|
+
constructor(address, version) {
|
|
14282
|
+
super(
|
|
14283
|
+
`Encryption key (${version}) not found for ${address}. Please sign a message to generate your encryption key.`
|
|
14284
|
+
);
|
|
14285
|
+
this.name = "EncryptionKeyMissingError";
|
|
14286
|
+
this.address = address;
|
|
14287
|
+
this.version = version;
|
|
14288
|
+
}
|
|
14289
|
+
};
|
|
14280
14290
|
var encryptionKeyStore = /* @__PURE__ */ new Map();
|
|
14281
14291
|
var pendingKeyRequests = /* @__PURE__ */ new Map();
|
|
14292
|
+
var pendingCandidateDerives = /* @__PURE__ */ new Map();
|
|
14293
|
+
var refreshCandidatesByAddress = /* @__PURE__ */ new Map();
|
|
14294
|
+
var divergentDeriveAddresses = /* @__PURE__ */ new Set();
|
|
14282
14295
|
var sessionEpoch = 0;
|
|
14283
14296
|
var cryptoKeyCache = /* @__PURE__ */ new Map();
|
|
14284
14297
|
var keyAvailableCallbacks = /* @__PURE__ */ new Map();
|
|
@@ -14321,14 +14334,11 @@ function notifyKeyAvailable(address) {
|
|
|
14321
14334
|
}
|
|
14322
14335
|
}
|
|
14323
14336
|
var keyPairStore = /* @__PURE__ */ new Map();
|
|
14324
|
-
function getStoredKey(address) {
|
|
14325
|
-
const keys = encryptionKeyStore.get(address);
|
|
14326
|
-
return keys?.current ?? null;
|
|
14327
|
-
}
|
|
14328
14337
|
function getStoredKeyByVersion(address, version) {
|
|
14329
14338
|
const keys = encryptionKeyStore.get(address);
|
|
14330
14339
|
if (!keys) return null;
|
|
14331
|
-
|
|
14340
|
+
const value = version === "v2" ? keys.legacy : keys.current;
|
|
14341
|
+
return value ?? null;
|
|
14332
14342
|
}
|
|
14333
14343
|
function setStoredKey(address, keys) {
|
|
14334
14344
|
encryptionKeyStore.set(address, keys);
|
|
@@ -14337,6 +14347,8 @@ function clearEncryptionKey(address) {
|
|
|
14337
14347
|
encryptionKeyStore.delete(address);
|
|
14338
14348
|
cryptoKeyCache.delete(`${address}:v2`);
|
|
14339
14349
|
cryptoKeyCache.delete(`${address}:v3`);
|
|
14350
|
+
refreshCandidatesByAddress.delete(address);
|
|
14351
|
+
divergentDeriveAddresses.delete(address);
|
|
14340
14352
|
}
|
|
14341
14353
|
function clearAllEncryptionState() {
|
|
14342
14354
|
sessionEpoch += 1;
|
|
@@ -14344,6 +14356,9 @@ function clearAllEncryptionState() {
|
|
|
14344
14356
|
cryptoKeyCache.clear();
|
|
14345
14357
|
keyAvailableCallbacks.clear();
|
|
14346
14358
|
pendingKeyRequests.clear();
|
|
14359
|
+
pendingCandidateDerives.clear();
|
|
14360
|
+
refreshCandidatesByAddress.clear();
|
|
14361
|
+
divergentDeriveAddresses.clear();
|
|
14347
14362
|
clearAllKeyPairs();
|
|
14348
14363
|
for (const cb of clearAllEncryptionStateCallbacks) {
|
|
14349
14364
|
try {
|
|
@@ -14529,9 +14544,7 @@ async function getEncryptionKey(address, version = "v3") {
|
|
|
14529
14544
|
}
|
|
14530
14545
|
const keyHex = getStoredKeyByVersion(address, version);
|
|
14531
14546
|
if (!keyHex) {
|
|
14532
|
-
throw new
|
|
14533
|
-
"Encryption key not found. Please sign a message to generate your encryption key."
|
|
14534
|
-
);
|
|
14547
|
+
throw new EncryptionKeyMissingError(address, version);
|
|
14535
14548
|
}
|
|
14536
14549
|
const keyBytes = hexToBytes(keyHex);
|
|
14537
14550
|
const cryptoKey = await crypto.subtle.importKey(
|
|
@@ -14595,8 +14608,43 @@ async function decryptDataBytesFromBytes(encrypted, address, version = "v3") {
|
|
|
14595
14608
|
);
|
|
14596
14609
|
return new Uint8Array(decryptedData);
|
|
14597
14610
|
}
|
|
14598
|
-
function hasEncryptionKey(address) {
|
|
14599
|
-
return
|
|
14611
|
+
function hasEncryptionKey(address, version) {
|
|
14612
|
+
return getStoredKeyByVersion(address, version ?? "v3") !== null;
|
|
14613
|
+
}
|
|
14614
|
+
function seedEncryptionKeys(address, keys) {
|
|
14615
|
+
if (!isValidWalletAddress(address)) {
|
|
14616
|
+
throw new Error(
|
|
14617
|
+
`Invalid wallet address: ${address}. Address must start with 0x and be 42 characters (0x + 40 hex characters).`
|
|
14618
|
+
);
|
|
14619
|
+
}
|
|
14620
|
+
if (!keys.legacy && !keys.current) {
|
|
14621
|
+
throw new Error("seedEncryptionKeys: at least one of legacy or current is required");
|
|
14622
|
+
}
|
|
14623
|
+
if (keys.legacy !== void 0) {
|
|
14624
|
+
assertAes256KeyHex("legacy", keys.legacy);
|
|
14625
|
+
}
|
|
14626
|
+
if (keys.current !== void 0) {
|
|
14627
|
+
assertAes256KeyHex("current", keys.current);
|
|
14628
|
+
}
|
|
14629
|
+
const existing = encryptionKeyStore.get(address);
|
|
14630
|
+
const next = {
|
|
14631
|
+
legacy: keys.legacy ?? existing?.legacy,
|
|
14632
|
+
current: keys.current ?? existing?.current
|
|
14633
|
+
};
|
|
14634
|
+
setStoredKey(address, next);
|
|
14635
|
+
if (keys.legacy !== void 0) cryptoKeyCache.delete(`${address}:v2`);
|
|
14636
|
+
if (keys.current !== void 0) cryptoKeyCache.delete(`${address}:v3`);
|
|
14637
|
+
refreshCandidatesByAddress.delete(address);
|
|
14638
|
+
divergentDeriveAddresses.delete(address);
|
|
14639
|
+
notifyKeyAvailable(address);
|
|
14640
|
+
}
|
|
14641
|
+
var AES_256_KEY_HEX = /^[0-9a-fA-F]{64}$/;
|
|
14642
|
+
function assertAes256KeyHex(field, value) {
|
|
14643
|
+
if (!AES_256_KEY_HEX.test(value)) {
|
|
14644
|
+
throw new Error(
|
|
14645
|
+
`seedEncryptionKeys: ${field} must be 64 hex characters (32-byte AES key), got length ${value.length}`
|
|
14646
|
+
);
|
|
14647
|
+
}
|
|
14600
14648
|
}
|
|
14601
14649
|
async function encryptBytesWithKey(plaintext, key) {
|
|
14602
14650
|
const plaintextBytes = typeof plaintext === "string" ? SHARED_TEXT_ENCODER.encode(plaintext) : plaintext;
|
|
@@ -14645,20 +14693,23 @@ async function decryptDataBatch(encryptedValues, address) {
|
|
|
14645
14693
|
const key = await getEncryptionKey(address);
|
|
14646
14694
|
return Promise.all(encryptedValues.map((value) => decryptDataWithKey(value, key)));
|
|
14647
14695
|
}
|
|
14648
|
-
async function requestEncryptionKey(walletAddress, signMessage, embeddedWalletSigner) {
|
|
14696
|
+
async function requestEncryptionKey(walletAddress, signMessage, embeddedWalletSigner, options) {
|
|
14649
14697
|
if (!isValidWalletAddress(walletAddress)) {
|
|
14650
14698
|
throw new Error(
|
|
14651
14699
|
`Invalid wallet address: ${walletAddress}. Address must start with 0x and be 42 characters (0x + 40 hex characters).`
|
|
14652
14700
|
);
|
|
14653
14701
|
}
|
|
14654
|
-
|
|
14655
|
-
|
|
14656
|
-
return;
|
|
14702
|
+
if (!options?.force && getStoredKeyByVersion(walletAddress, "v3") && getStoredKeyByVersion(walletAddress, "v2")) {
|
|
14703
|
+
return true;
|
|
14657
14704
|
}
|
|
14658
|
-
|
|
14659
|
-
|
|
14660
|
-
|
|
14661
|
-
|
|
14705
|
+
if (!options?.force && divergentDeriveAddresses.has(walletAddress)) {
|
|
14706
|
+
return false;
|
|
14707
|
+
}
|
|
14708
|
+
if (!options?.force) {
|
|
14709
|
+
const pending = pendingKeyRequests.get(walletAddress);
|
|
14710
|
+
if (pending) {
|
|
14711
|
+
return pending;
|
|
14712
|
+
}
|
|
14662
14713
|
}
|
|
14663
14714
|
const startEpoch = sessionEpoch;
|
|
14664
14715
|
const promise = (async () => {
|
|
@@ -14686,20 +14737,143 @@ async function requestEncryptionKey(walletAddress, signMessage, embeddedWalletSi
|
|
|
14686
14737
|
deriveKeyFromSignatureV3(signature)
|
|
14687
14738
|
]);
|
|
14688
14739
|
if (sessionEpoch !== startEpoch) {
|
|
14689
|
-
return;
|
|
14740
|
+
return false;
|
|
14741
|
+
}
|
|
14742
|
+
const existing = encryptionKeyStore.get(walletAddress);
|
|
14743
|
+
if (!options?.force && existing && (existing.legacy || existing.current)) {
|
|
14744
|
+
const matchesCurrent = Boolean(existing.current && existing.current === currentKey);
|
|
14745
|
+
const matchesLegacy = Boolean(existing.legacy && existing.legacy === legacyKey);
|
|
14746
|
+
if (!matchesCurrent && !matchesLegacy) {
|
|
14747
|
+
getLogger().warn(
|
|
14748
|
+
"requestEncryptionKey: derived keys do not match existing store; leaving unchanged"
|
|
14749
|
+
);
|
|
14750
|
+
divergentDeriveAddresses.add(walletAddress);
|
|
14751
|
+
return false;
|
|
14752
|
+
}
|
|
14753
|
+
setStoredKey(walletAddress, {
|
|
14754
|
+
legacy: existing.legacy ?? legacyKey,
|
|
14755
|
+
current: existing.current ?? currentKey
|
|
14756
|
+
});
|
|
14757
|
+
} else {
|
|
14758
|
+
setStoredKey(walletAddress, { legacy: legacyKey, current: currentKey });
|
|
14690
14759
|
}
|
|
14691
|
-
|
|
14760
|
+
cryptoKeyCache.delete(`${walletAddress}:v2`);
|
|
14761
|
+
cryptoKeyCache.delete(`${walletAddress}:v3`);
|
|
14762
|
+
refreshCandidatesByAddress.delete(walletAddress);
|
|
14763
|
+
divergentDeriveAddresses.delete(walletAddress);
|
|
14692
14764
|
notifyKeyAvailable(walletAddress);
|
|
14765
|
+
return true;
|
|
14693
14766
|
})();
|
|
14694
14767
|
pendingKeyRequests.set(walletAddress, promise);
|
|
14695
14768
|
try {
|
|
14696
|
-
await promise;
|
|
14769
|
+
return await promise;
|
|
14697
14770
|
} finally {
|
|
14698
14771
|
if (pendingKeyRequests.get(walletAddress) === promise) {
|
|
14699
14772
|
pendingKeyRequests.delete(walletAddress);
|
|
14700
14773
|
}
|
|
14701
14774
|
}
|
|
14702
14775
|
}
|
|
14776
|
+
async function refreshEncryptionKeyIfMatches(walletAddress, probeCiphertext, signMessage, embeddedWalletSigner) {
|
|
14777
|
+
if (!isValidWalletAddress(walletAddress)) {
|
|
14778
|
+
throw new Error(
|
|
14779
|
+
`Invalid wallet address: ${walletAddress}. Address must start with 0x and be 42 characters (0x + 40 hex characters).`
|
|
14780
|
+
);
|
|
14781
|
+
}
|
|
14782
|
+
const parsed = parsePrefixedCiphertext(probeCiphertext);
|
|
14783
|
+
if (!parsed) {
|
|
14784
|
+
return false;
|
|
14785
|
+
}
|
|
14786
|
+
const cached = refreshCandidatesByAddress.get(walletAddress);
|
|
14787
|
+
if (cached) {
|
|
14788
|
+
return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, cached);
|
|
14789
|
+
}
|
|
14790
|
+
let derivePromise = pendingCandidateDerives.get(walletAddress);
|
|
14791
|
+
if (!derivePromise) {
|
|
14792
|
+
derivePromise = (async () => {
|
|
14793
|
+
const candidates2 = await deriveRefreshCandidates(signMessage, embeddedWalletSigner);
|
|
14794
|
+
if (candidates2) {
|
|
14795
|
+
refreshCandidatesByAddress.set(walletAddress, candidates2);
|
|
14796
|
+
}
|
|
14797
|
+
return candidates2;
|
|
14798
|
+
})().finally(() => {
|
|
14799
|
+
if (pendingCandidateDerives.get(walletAddress) === derivePromise) {
|
|
14800
|
+
pendingCandidateDerives.delete(walletAddress);
|
|
14801
|
+
}
|
|
14802
|
+
});
|
|
14803
|
+
pendingCandidateDerives.set(walletAddress, derivePromise);
|
|
14804
|
+
}
|
|
14805
|
+
const candidates = await derivePromise;
|
|
14806
|
+
if (!candidates) {
|
|
14807
|
+
return false;
|
|
14808
|
+
}
|
|
14809
|
+
return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates);
|
|
14810
|
+
}
|
|
14811
|
+
function parsePrefixedCiphertext(probeCiphertext) {
|
|
14812
|
+
const version = probeCiphertext.startsWith("enc:v3:") ? "v3" : probeCiphertext.startsWith("enc:v2:") ? "v2" : null;
|
|
14813
|
+
if (!version) return null;
|
|
14814
|
+
const encryptedData = probeCiphertext.slice(
|
|
14815
|
+
version === "v3" ? "enc:v3:".length : "enc:v2:".length
|
|
14816
|
+
);
|
|
14817
|
+
if (encryptedData.length < 56 || !/^[0-9a-f]+$/i.test(encryptedData)) {
|
|
14818
|
+
return null;
|
|
14819
|
+
}
|
|
14820
|
+
return { version, encryptedData };
|
|
14821
|
+
}
|
|
14822
|
+
async function deriveRefreshCandidates(signMessage, embeddedWalletSigner) {
|
|
14823
|
+
const startEpoch = sessionEpoch;
|
|
14824
|
+
const signOptions = { showWalletUIs: false };
|
|
14825
|
+
let signature;
|
|
14826
|
+
try {
|
|
14827
|
+
if (embeddedWalletSigner) {
|
|
14828
|
+
signature = await embeddedWalletSigner(SIGN_MESSAGE, signOptions);
|
|
14829
|
+
} else {
|
|
14830
|
+
signature = await signMessage(SIGN_MESSAGE, signOptions);
|
|
14831
|
+
}
|
|
14832
|
+
} catch (error) {
|
|
14833
|
+
if (embeddedWalletSigner && error instanceof Error) {
|
|
14834
|
+
getLogger().warn(
|
|
14835
|
+
"Embedded wallet signing failed during key refresh, falling back to standard signMessage:",
|
|
14836
|
+
error.message
|
|
14837
|
+
);
|
|
14838
|
+
signature = await signMessage(SIGN_MESSAGE, signOptions);
|
|
14839
|
+
} else {
|
|
14840
|
+
throw error;
|
|
14841
|
+
}
|
|
14842
|
+
}
|
|
14843
|
+
const [legacyKey, currentKey] = await Promise.all([
|
|
14844
|
+
deriveKeyFromSignature(signature),
|
|
14845
|
+
deriveKeyFromSignatureV3(signature)
|
|
14846
|
+
]);
|
|
14847
|
+
if (sessionEpoch !== startEpoch) {
|
|
14848
|
+
return null;
|
|
14849
|
+
}
|
|
14850
|
+
return { legacy: legacyKey, current: currentKey };
|
|
14851
|
+
}
|
|
14852
|
+
async function tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates) {
|
|
14853
|
+
const candidateHex = parsed.version === "v2" ? candidates.legacy : candidates.current;
|
|
14854
|
+
const keyBytes = hexToBytes(candidateHex);
|
|
14855
|
+
const candidateCryptoKey = await crypto.subtle.importKey(
|
|
14856
|
+
"raw",
|
|
14857
|
+
keyBytes.buffer,
|
|
14858
|
+
{ name: "AES-GCM" },
|
|
14859
|
+
false,
|
|
14860
|
+
["encrypt", "decrypt"]
|
|
14861
|
+
);
|
|
14862
|
+
try {
|
|
14863
|
+
await decryptDataWithKey(parsed.encryptedData, candidateCryptoKey);
|
|
14864
|
+
} catch {
|
|
14865
|
+
getLogger().warn(
|
|
14866
|
+
"refreshEncryptionKeyIfMatches: candidate keys failed to decrypt probe; leaving store unchanged"
|
|
14867
|
+
);
|
|
14868
|
+
return false;
|
|
14869
|
+
}
|
|
14870
|
+
setStoredKey(walletAddress, { legacy: candidates.legacy, current: candidates.current });
|
|
14871
|
+
cryptoKeyCache.delete(`${walletAddress}:v2`);
|
|
14872
|
+
cryptoKeyCache.delete(`${walletAddress}:v3`);
|
|
14873
|
+
divergentDeriveAddresses.delete(walletAddress);
|
|
14874
|
+
notifyKeyAvailable(walletAddress);
|
|
14875
|
+
return true;
|
|
14876
|
+
}
|
|
14703
14877
|
var KEYPAIR_STORAGE_PREFIX = "ecdh_keypair_";
|
|
14704
14878
|
async function persistKeyPair(address, startEpoch) {
|
|
14705
14879
|
if (typeof window === "undefined") {
|
|
@@ -14970,20 +15144,29 @@ async function encryptField(value, address, signMessage, embeddedWalletSigner, s
|
|
|
14970
15144
|
const encrypted = await encryptData(value, address);
|
|
14971
15145
|
return `${ENCRYPTION_PREFIX}${encrypted}`;
|
|
14972
15146
|
}
|
|
14973
|
-
async function
|
|
14974
|
-
if (!value) return value;
|
|
15147
|
+
async function decryptFieldDetailed(value, address) {
|
|
15148
|
+
if (!value) return { status: "plaintext", value };
|
|
14975
15149
|
const detected = detectEncryptionVersion(value);
|
|
14976
|
-
if (!detected) return value;
|
|
15150
|
+
if (!detected) return { status: "plaintext", value };
|
|
14977
15151
|
if (detected.encryptedData.length < 56 || !/^[0-9a-f]+$/i.test(detected.encryptedData)) {
|
|
14978
|
-
return value;
|
|
15152
|
+
return { status: "invalid_payload", value, version: detected.version };
|
|
14979
15153
|
}
|
|
14980
15154
|
try {
|
|
14981
|
-
|
|
15155
|
+
const plaintext = await decryptData(detected.encryptedData, address, detected.version);
|
|
15156
|
+
return { status: "ok", value: plaintext, version: detected.version };
|
|
14982
15157
|
} catch (error) {
|
|
14983
|
-
|
|
14984
|
-
|
|
15158
|
+
if (error instanceof EncryptionKeyMissingError) {
|
|
15159
|
+
getLogger().warn("Failed to decrypt field (key missing for version):", error.message);
|
|
15160
|
+
return { status: "key_missing", value, version: detected.version };
|
|
15161
|
+
}
|
|
15162
|
+
getLogger().warn("Failed to decrypt field (auth mismatch or crypto error):", error);
|
|
15163
|
+
return { status: "auth_mismatch", value, version: detected.version };
|
|
14985
15164
|
}
|
|
14986
15165
|
}
|
|
15166
|
+
async function decryptField(value, address) {
|
|
15167
|
+
const result = await decryptFieldDetailed(value, address);
|
|
15168
|
+
return result.value;
|
|
15169
|
+
}
|
|
14987
15170
|
async function encryptJsonField(value, address, signMessage, embeddedWalletSigner, skipKeyRequest) {
|
|
14988
15171
|
if (!value) return void 0;
|
|
14989
15172
|
const jsonString = JSON.stringify(value);
|
|
@@ -15083,20 +15266,40 @@ async function encryptMessageFields(message, address, signMessage, embeddedWalle
|
|
|
15083
15266
|
throw error;
|
|
15084
15267
|
}
|
|
15085
15268
|
}
|
|
15086
|
-
async function
|
|
15087
|
-
if (value === void 0) return void 0;
|
|
15088
|
-
if (value === null) return value;
|
|
15269
|
+
async function decryptMaybeJsonFieldDetailed(value, address) {
|
|
15270
|
+
if (value === void 0) return { value: void 0, status: "plaintext" };
|
|
15271
|
+
if (value === null) return { value, status: "plaintext" };
|
|
15089
15272
|
if (typeof value === "string") {
|
|
15090
15273
|
if (isEncrypted(value)) {
|
|
15091
|
-
|
|
15274
|
+
const detailed = await decryptFieldDetailed(value, address);
|
|
15275
|
+
if (detailed.status !== "ok") {
|
|
15276
|
+
return { value: void 0, status: detailed.status, probe: value };
|
|
15277
|
+
}
|
|
15278
|
+
try {
|
|
15279
|
+
return { value: JSON.parse(detailed.value), status: "ok" };
|
|
15280
|
+
} catch (error) {
|
|
15281
|
+
getLogger().warn("Failed to parse decrypted JSON field:", error);
|
|
15282
|
+
return { value: void 0, status: "invalid_payload", probe: value };
|
|
15283
|
+
}
|
|
15092
15284
|
}
|
|
15093
15285
|
try {
|
|
15094
|
-
return JSON.parse(value);
|
|
15286
|
+
return { value: JSON.parse(value), status: "plaintext" };
|
|
15095
15287
|
} catch {
|
|
15096
|
-
return void 0;
|
|
15288
|
+
return { value: void 0, status: "plaintext" };
|
|
15097
15289
|
}
|
|
15098
15290
|
}
|
|
15099
|
-
return value;
|
|
15291
|
+
return { value, status: "plaintext" };
|
|
15292
|
+
}
|
|
15293
|
+
function worseDecryptStatus(a, b) {
|
|
15294
|
+
if (b === "ok" || b === "plaintext") return a;
|
|
15295
|
+
if (!a || a === "ok" || a === "plaintext") return b;
|
|
15296
|
+
const rank = (s) => {
|
|
15297
|
+
if (s === "auth_mismatch") return 3;
|
|
15298
|
+
if (s === "key_missing") return 2;
|
|
15299
|
+
if (s === "invalid_payload") return 1;
|
|
15300
|
+
return 0;
|
|
15301
|
+
};
|
|
15302
|
+
return rank(b) >= rank(a) ? b : a;
|
|
15100
15303
|
}
|
|
15101
15304
|
async function decryptMessageFields(message, address, signMessage, embeddedWalletSigner) {
|
|
15102
15305
|
if (!address) {
|
|
@@ -15109,33 +15312,82 @@ async function decryptMessageFields(message, address, signMessage, embeddedWalle
|
|
|
15109
15312
|
getLogger().warn("Failed to request encryption key for decryption:", error);
|
|
15110
15313
|
}
|
|
15111
15314
|
}
|
|
15112
|
-
|
|
15113
|
-
const
|
|
15114
|
-
|
|
15315
|
+
let assembled = await assembleDecryptedMessage(message, address);
|
|
15316
|
+
const needsRefresh = assembled.decryptionStatus === "auth_mismatch" || assembled.decryptionStatus === "key_missing";
|
|
15317
|
+
if (needsRefresh && signMessage && assembled.refreshProbe) {
|
|
15318
|
+
try {
|
|
15319
|
+
const refreshed = await refreshEncryptionKeyIfMatches(
|
|
15320
|
+
address,
|
|
15321
|
+
assembled.refreshProbe,
|
|
15322
|
+
signMessage,
|
|
15323
|
+
embeddedWalletSigner
|
|
15324
|
+
);
|
|
15325
|
+
if (refreshed) {
|
|
15326
|
+
assembled = await assembleDecryptedMessage(message, address);
|
|
15327
|
+
}
|
|
15328
|
+
} catch (error) {
|
|
15329
|
+
getLogger().warn("Failed to refresh encryption key after decrypt failure:", error);
|
|
15330
|
+
}
|
|
15331
|
+
}
|
|
15332
|
+
const { refreshProbe: _probe, ...result } = assembled;
|
|
15333
|
+
return result;
|
|
15334
|
+
}
|
|
15335
|
+
async function assembleDecryptedMessage(message, address) {
|
|
15336
|
+
const contentResult = await decryptFieldDetailed(message.content, address);
|
|
15337
|
+
const thinkingResult = message.thinking ? await decryptFieldDetailed(message.thinking, address) : null;
|
|
15338
|
+
const vectorResult = await decryptMaybeJsonFieldDetailed(
|
|
15115
15339
|
message.vector,
|
|
15116
15340
|
address
|
|
15117
15341
|
);
|
|
15118
|
-
const
|
|
15342
|
+
const chunksResult = await decryptMaybeJsonFieldDetailed(
|
|
15119
15343
|
message.chunks,
|
|
15120
15344
|
address
|
|
15121
15345
|
);
|
|
15122
|
-
const
|
|
15346
|
+
const sourcesResult = await decryptMaybeJsonFieldDetailed(
|
|
15123
15347
|
message.sources,
|
|
15124
15348
|
address
|
|
15125
15349
|
);
|
|
15126
|
-
const
|
|
15350
|
+
const thoughtProcessResult = await decryptMaybeJsonFieldDetailed(
|
|
15127
15351
|
message.thoughtProcess,
|
|
15128
15352
|
address
|
|
15129
15353
|
);
|
|
15130
|
-
|
|
15354
|
+
let decryptionStatus;
|
|
15355
|
+
let refreshProbe;
|
|
15356
|
+
const consider = (status, probe) => {
|
|
15357
|
+
decryptionStatus = worseDecryptStatus(decryptionStatus, status);
|
|
15358
|
+
if (!refreshProbe && probe && (status === "auth_mismatch" || status === "key_missing")) {
|
|
15359
|
+
refreshProbe = probe;
|
|
15360
|
+
}
|
|
15361
|
+
};
|
|
15362
|
+
consider(contentResult.status, isEncrypted(message.content) ? message.content : void 0);
|
|
15363
|
+
if (thinkingResult && message.thinking && typeof message.thinking === "string") {
|
|
15364
|
+
consider(thinkingResult.status, isEncrypted(message.thinking) ? message.thinking : void 0);
|
|
15365
|
+
}
|
|
15366
|
+
if (vectorResult.probe) consider(vectorResult.status, vectorResult.probe);
|
|
15367
|
+
if (chunksResult.probe) consider(chunksResult.status, chunksResult.probe);
|
|
15368
|
+
if (sourcesResult.probe) consider(sourcesResult.status, sourcesResult.probe);
|
|
15369
|
+
if (thoughtProcessResult.probe) {
|
|
15370
|
+
consider(thoughtProcessResult.status, thoughtProcessResult.probe);
|
|
15371
|
+
}
|
|
15372
|
+
const statusForConsumer = decryptionStatus === "ok" || decryptionStatus === "plaintext" || !decryptionStatus ? void 0 : decryptionStatus;
|
|
15373
|
+
const result = {
|
|
15131
15374
|
...message,
|
|
15132
|
-
content:
|
|
15133
|
-
thinking:
|
|
15134
|
-
vector:
|
|
15135
|
-
chunks:
|
|
15136
|
-
sources:
|
|
15137
|
-
thoughtProcess:
|
|
15375
|
+
content: contentResult.value,
|
|
15376
|
+
thinking: thinkingResult ? thinkingResult.value : message.thinking,
|
|
15377
|
+
vector: vectorResult.value,
|
|
15378
|
+
chunks: chunksResult.value,
|
|
15379
|
+
sources: sourcesResult.value,
|
|
15380
|
+
thoughtProcess: thoughtProcessResult.value
|
|
15138
15381
|
};
|
|
15382
|
+
if (statusForConsumer) {
|
|
15383
|
+
result.decryptionStatus = statusForConsumer;
|
|
15384
|
+
} else {
|
|
15385
|
+
delete result.decryptionStatus;
|
|
15386
|
+
}
|
|
15387
|
+
if (refreshProbe) {
|
|
15388
|
+
result.refreshProbe = refreshProbe;
|
|
15389
|
+
}
|
|
15390
|
+
return result;
|
|
15139
15391
|
}
|
|
15140
15392
|
|
|
15141
15393
|
// src/lib/db/chat/types.ts
|
|
@@ -20350,7 +20602,7 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
|
|
|
20350
20602
|
legacy.map((r) => ({ id: r.uniqueId, content: r.content, score: r.similarity }))
|
|
20351
20603
|
);
|
|
20352
20604
|
}
|
|
20353
|
-
const { recall: recall2 } = await import("./recall-
|
|
20605
|
+
const { recall: recall2 } = await import("./recall-ZHEPQNEU.mjs");
|
|
20354
20606
|
let recallDegraded = [];
|
|
20355
20607
|
const result = await recall2(
|
|
20356
20608
|
query,
|
|
@@ -21302,6 +21554,7 @@ export {
|
|
|
21302
21554
|
require_logError,
|
|
21303
21555
|
require_Relation,
|
|
21304
21556
|
require_watermelondb,
|
|
21557
|
+
EncryptionKeyMissingError,
|
|
21305
21558
|
onClearAllEncryptionState,
|
|
21306
21559
|
onKeyAvailable,
|
|
21307
21560
|
clearEncryptionKey,
|
|
@@ -21314,11 +21567,13 @@ export {
|
|
|
21314
21567
|
decryptDataBytes,
|
|
21315
21568
|
decryptDataBytesFromBytes,
|
|
21316
21569
|
hasEncryptionKey,
|
|
21570
|
+
seedEncryptionKeys,
|
|
21317
21571
|
encryptDataWithKey,
|
|
21318
21572
|
decryptDataWithKey,
|
|
21319
21573
|
encryptDataBatch,
|
|
21320
21574
|
decryptDataBatch,
|
|
21321
21575
|
requestEncryptionKey,
|
|
21576
|
+
refreshEncryptionKeyIfMatches,
|
|
21322
21577
|
requestKeyPair,
|
|
21323
21578
|
exportPublicKey,
|
|
21324
21579
|
hasKeyPair,
|