@aztec/wallets 0.0.1-commit.a5db02d → 0.0.1-commit.aa0c64f

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.
@@ -223,7 +223,7 @@ export class EmbeddedWallet extends BaseWallet {
223
223
  * private kernel circuit execution.
224
224
  */ async simulateViaEntrypoint(executionPayload, opts) {
225
225
  const { from, feeOptions, additionalScopes, skipTxValidation, skipFeeEnforcement, sendMessagesAs } = opts;
226
- const scopes = this.scopesFrom(from, additionalScopes);
226
+ const scopes = this.scopesFrom(from, additionalScopes ?? [], sendMessagesAs);
227
227
  const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
228
228
  const finalExecutionPayload = feeExecutionPayload ? mergeExecutionPayloads([
229
229
  feeExecutionPayload,
@@ -39,6 +39,13 @@ export interface OpenEncryptedEmbeddedStoresOptions {
39
39
  * @internal
40
40
  */
41
41
  export type OpenSqliteEncryptedStoreFn = (log: Logger, name: string, poolDirectory: string | undefined, encryptionKey: Uint8Array) => Promise<AztecSQLiteOPFSStore>;
42
+ /**
43
+ * Internal seam for tests to inject a fake store wiper. Defaults to removing the store's OPFS pool directory
44
+ * outright. Not part of the public API.
45
+ *
46
+ * @internal
47
+ */
48
+ export type WipeSqliteStoreFn = (poolDirectory: string | undefined) => Promise<void>;
42
49
  /**
43
50
  * Opens the PXE and wallet stores in sequence, both encrypted with keys obtained from `getEncryptionKey`.
44
51
  *
@@ -60,8 +67,8 @@ export type OpenSqliteEncryptedStoreFn = (log: Logger, name: string, poolDirecto
60
67
  * @param log - Logger for both stores.
61
68
  * @param openStore - Internal test seam. Do not pass in production code.
62
69
  */
63
- export declare function openEncryptedEmbeddedStores(config: OpenEncryptedEmbeddedStoresOptions, getEncryptionKey: () => Promise<Uint8Array>, log: Logger, openStore?: OpenSqliteEncryptedStoreFn): Promise<{
70
+ export declare function openEncryptedEmbeddedStores(config: OpenEncryptedEmbeddedStoresOptions, getEncryptionKey: () => Promise<Uint8Array>, log: Logger, openStore?: OpenSqliteEncryptedStoreFn, wipeStore?: WipeSqliteStoreFn): Promise<{
64
71
  pxeStore: AztecSQLiteOPFSStore;
65
72
  walletStore: AztecSQLiteOPFSStore;
66
73
  }>;
67
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RvcmVfZW5jcnlwdGlvbi5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2VtYmVkZGVkL3N0b3JlX2VuY3J5cHRpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7Ozs7O0dBUUc7QUFDSCxPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUNwRCxPQUFPLEVBQUUsb0JBQW9CLEVBQUUscUJBQXFCLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUUxRixnRUFBZ0U7QUFDaEUsTUFBTSxNQUFNLGlCQUFpQixHQUFHLEtBQUssR0FBRyxRQUFRLENBQUM7QUFFakQ7OztHQUdHO0FBQ0gscUJBQWEsNkJBQThCLFNBQVEsS0FBSztJQUN0RCxRQUFRLENBQUMsU0FBUyxFQUFFLGlCQUFpQixDQUFDO0lBRXRDLFlBQVksU0FBUyxFQUFFLGlCQUFpQixFQUFFLElBQUksRUFBRTtRQUFFLEtBQUssRUFBRSxxQkFBcUIsQ0FBQTtLQUFFLEVBSS9FO0NBQ0Y7QUFFRCw2REFBNkQ7QUFDN0QsTUFBTSxXQUFXLGtDQUFrQztJQUNqRCxHQUFHLEVBQUU7UUFBRSxJQUFJLEVBQUUsTUFBTSxDQUFDO1FBQUMsYUFBYSxDQUFDLEVBQUUsTUFBTSxDQUFBO0tBQUUsQ0FBQztJQUM5QyxNQUFNLEVBQUU7UUFBRSxJQUFJLEVBQUUsTUFBTSxDQUFDO1FBQUMsYUFBYSxDQUFDLEVBQUUsTUFBTSxDQU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AO,CAAC,IAAI,CAAC,CAAC;AAgBrF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,kCAAkC,EAC1C,gBAAgB,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,EAC3C,GAAG,EAAE,MAAM,EACX,SAAS,GAAE,0BAA6C,EACxD,SAAS,GAAE,iBAAoC,GAC9C,OAAO,CAAC;IAAE,QAAQ,EAAE,oBAAoB,CAAC;IAAC,WAAW,EAAE,oBAAoB,CAAA;CAAE,CAAC,CAWhF"}
@@ -6,7 +6,7 @@
6
6
  * - `storeName: 'pxe' | 'wallet'`, telling callers WHICH store failed.
7
7
  * - Cleanup: when the wallet store fails to open, ensures the already-opened PXE store is closed before the error
8
8
  * surfaces, so callers don't leak the SAH Pool's OPFS lock.
9
- */ import { AztecSQLiteOPFSStore, SqliteEncryptionError } from '@aztec/kv-store/sqlite-opfs';
9
+ */ import { AztecSQLiteOPFSStore, SqliteCorruptionError, SqliteEncryptionError, deletePoolDirectory } from '@aztec/kv-store/sqlite-opfs';
10
10
  /**
11
11
  * Thrown by {@link openEncryptedEmbeddedStores} when one of the two stores cannot be decrypted with the supplied
12
12
  * key. The original {@link SqliteEncryptionError} is preserved as `cause`.
@@ -21,6 +21,18 @@
21
21
  }
22
22
  }
23
23
  const defaultOpenStore = (log, name, poolDirectory, encryptionKey)=>AztecSQLiteOPFSStore.open(log, name, false, poolDirectory, encryptionKey);
24
+ /**
25
+ * Deletes a store's OPFS pool directory. Safe to call only after a *failed* open() — a live store's SAH pool holds
26
+ * a lock on the directory and the removal would reject. A store with no `poolDirectory` lives in the shared default
27
+ * pool, which we won't blow away (it would take unrelated stores with it), so wiping is a no-op there.
28
+ */ const defaultWipeStore = async (poolDirectory)=>{
29
+ if (!poolDirectory) {
30
+ return;
31
+ }
32
+ await deletePoolDirectory(poolDirectory).catch(()=>{
33
+ // Already gone / never created — nothing to wipe.
34
+ });
35
+ };
24
36
  /**
25
37
  * Opens the PXE and wallet stores in sequence, both encrypted with keys obtained from `getEncryptionKey`.
26
38
  *
@@ -41,10 +53,10 @@ const defaultOpenStore = (log, name, poolDirectory, encryptionKey)=>AztecSQLiteO
41
53
  * detaches on transfer, so each call must allocate).
42
54
  * @param log - Logger for both stores.
43
55
  * @param openStore - Internal test seam. Do not pass in production code.
44
- */ export async function openEncryptedEmbeddedStores(config, getEncryptionKey, log, openStore = defaultOpenStore) {
45
- const pxeStore = await openOneStore('pxe', config.pxe, getEncryptionKey, log, openStore);
56
+ */ export async function openEncryptedEmbeddedStores(config, getEncryptionKey, log, openStore = defaultOpenStore, wipeStore = defaultWipeStore) {
57
+ const pxeStore = await openOneStore('pxe', config.pxe, getEncryptionKey, log, openStore, wipeStore);
46
58
  try {
47
- const walletStore = await openOneStore('wallet', config.wallet, getEncryptionKey, log, openStore);
59
+ const walletStore = await openOneStore('wallet', config.wallet, getEncryptionKey, log, openStore, wipeStore);
48
60
  return {
49
61
  pxeStore,
50
62
  walletStore
@@ -56,7 +68,7 @@ const defaultOpenStore = (log, name, poolDirectory, encryptionKey)=>AztecSQLiteO
56
68
  throw err;
57
69
  }
58
70
  }
59
- async function openOneStore(storeName, { name, poolDirectory }, getEncryptionKey, log, openStore) {
71
+ async function openOneStore(storeName, { name, poolDirectory }, getEncryptionKey, log, openStore, wipeStore) {
60
72
  const key = await getEncryptionKey();
61
73
  try {
62
74
  return await openStore(log, name, poolDirectory, key);
@@ -66,6 +78,16 @@ async function openOneStore(storeName, { name, poolDirectory }, getEncryptionKey
66
78
  cause: err
67
79
  });
68
80
  }
81
+ if (err instanceof SqliteCorruptionError) {
82
+ // A corrupt image is unrecoverable — no key or retry against the same bytes brings it back. Self-heal
83
+ // instead of dead-ending forever: wipe the store's OPFS directory (safe — the failed open left no SAH-pool
84
+ // lock behind) and reopen a fresh, empty one, so callers see a normal first-run rather than a permanent
85
+ // "database disk image is malformed" on every open.
86
+ log.warn(`Embedded wallet '${storeName}' store is corrupt (${err.message}); wiping and reopening fresh`);
87
+ await wipeStore(poolDirectory);
88
+ // open() transferred (detached) the previous key buffer, so fetch a fresh one for the reopen.
89
+ return await openStore(log, name, poolDirectory, await getEncryptionKey());
90
+ }
69
91
  throw err;
70
92
  }
71
93
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aztec/wallets",
3
3
  "homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/wallets",
4
- "version": "0.0.1-commit.a5db02d",
4
+ "version": "0.0.1-commit.aa0c64f",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./embedded": {
@@ -28,16 +28,16 @@
28
28
  "../package.common.json"
29
29
  ],
30
30
  "dependencies": {
31
- "@aztec/accounts": "0.0.1-commit.a5db02d",
32
- "@aztec/aztec.js": "0.0.1-commit.a5db02d",
33
- "@aztec/entrypoints": "0.0.1-commit.a5db02d",
34
- "@aztec/foundation": "0.0.1-commit.a5db02d",
35
- "@aztec/kv-store": "0.0.1-commit.a5db02d",
36
- "@aztec/protocol-contracts": "0.0.1-commit.a5db02d",
37
- "@aztec/pxe": "0.0.1-commit.a5db02d",
38
- "@aztec/standard-contracts": "0.0.1-commit.a5db02d",
39
- "@aztec/stdlib": "0.0.1-commit.a5db02d",
40
- "@aztec/wallet-sdk": "0.0.1-commit.a5db02d"
31
+ "@aztec/accounts": "0.0.1-commit.aa0c64f",
32
+ "@aztec/aztec.js": "0.0.1-commit.aa0c64f",
33
+ "@aztec/entrypoints": "0.0.1-commit.aa0c64f",
34
+ "@aztec/foundation": "0.0.1-commit.aa0c64f",
35
+ "@aztec/kv-store": "0.0.1-commit.aa0c64f",
36
+ "@aztec/protocol-contracts": "0.0.1-commit.aa0c64f",
37
+ "@aztec/pxe": "0.0.1-commit.aa0c64f",
38
+ "@aztec/standard-contracts": "0.0.1-commit.aa0c64f",
39
+ "@aztec/stdlib": "0.0.1-commit.aa0c64f",
40
+ "@aztec/wallet-sdk": "0.0.1-commit.aa0c64f"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@jest/globals": "^30.0.0",
@@ -329,7 +329,7 @@ export class EmbeddedWallet extends BaseWallet {
329
329
  opts: SimulateViaEntrypointOptions,
330
330
  ): Promise<TxSimulationResultWithAppOffset> {
331
331
  const { from, feeOptions, additionalScopes, skipTxValidation, skipFeeEnforcement, sendMessagesAs } = opts;
332
- const scopes = this.scopesFrom(from, additionalScopes);
332
+ const scopes = this.scopesFrom(from, additionalScopes ?? [], sendMessagesAs);
333
333
 
334
334
  const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
335
335
  const finalExecutionPayload = feeExecutionPayload
@@ -8,7 +8,12 @@
8
8
  * surfaces, so callers don't leak the SAH Pool's OPFS lock.
9
9
  */
10
10
  import type { Logger } from '@aztec/foundation/log';
11
- import { AztecSQLiteOPFSStore, SqliteEncryptionError } from '@aztec/kv-store/sqlite-opfs';
11
+ import {
12
+ AztecSQLiteOPFSStore,
13
+ SqliteCorruptionError,
14
+ SqliteEncryptionError,
15
+ deletePoolDirectory,
16
+ } from '@aztec/kv-store/sqlite-opfs';
12
17
 
13
18
  /** Which of the embedded wallet's two stores failed to open. */
14
19
  export type EmbeddedStoreName = 'pxe' | 'wallet';
@@ -49,6 +54,28 @@ export type OpenSqliteEncryptedStoreFn = (
49
54
  const defaultOpenStore: OpenSqliteEncryptedStoreFn = (log, name, poolDirectory, encryptionKey) =>
50
55
  AztecSQLiteOPFSStore.open(log, name, false, poolDirectory, encryptionKey);
51
56
 
57
+ /**
58
+ * Internal seam for tests to inject a fake store wiper. Defaults to removing the store's OPFS pool directory
59
+ * outright. Not part of the public API.
60
+ *
61
+ * @internal
62
+ */
63
+ export type WipeSqliteStoreFn = (poolDirectory: string | undefined) => Promise<void>;
64
+
65
+ /**
66
+ * Deletes a store's OPFS pool directory. Safe to call only after a *failed* open() — a live store's SAH pool holds
67
+ * a lock on the directory and the removal would reject. A store with no `poolDirectory` lives in the shared default
68
+ * pool, which we won't blow away (it would take unrelated stores with it), so wiping is a no-op there.
69
+ */
70
+ const defaultWipeStore: WipeSqliteStoreFn = async poolDirectory => {
71
+ if (!poolDirectory) {
72
+ return;
73
+ }
74
+ await deletePoolDirectory(poolDirectory).catch(() => {
75
+ // Already gone / never created — nothing to wipe.
76
+ });
77
+ };
78
+
52
79
  /**
53
80
  * Opens the PXE and wallet stores in sequence, both encrypted with keys obtained from `getEncryptionKey`.
54
81
  *
@@ -75,10 +102,11 @@ export async function openEncryptedEmbeddedStores(
75
102
  getEncryptionKey: () => Promise<Uint8Array>,
76
103
  log: Logger,
77
104
  openStore: OpenSqliteEncryptedStoreFn = defaultOpenStore,
105
+ wipeStore: WipeSqliteStoreFn = defaultWipeStore,
78
106
  ): Promise<{ pxeStore: AztecSQLiteOPFSStore; walletStore: AztecSQLiteOPFSStore }> {
79
- const pxeStore = await openOneStore('pxe', config.pxe, getEncryptionKey, log, openStore);
107
+ const pxeStore = await openOneStore('pxe', config.pxe, getEncryptionKey, log, openStore, wipeStore);
80
108
  try {
81
- const walletStore = await openOneStore('wallet', config.wallet, getEncryptionKey, log, openStore);
109
+ const walletStore = await openOneStore('wallet', config.wallet, getEncryptionKey, log, openStore, wipeStore);
82
110
  return { pxeStore, walletStore };
83
111
  } catch (err) {
84
112
  // Cleanup is best-effort — if close() itself throws (e.g. worker already dead), swallow it so the original error
@@ -94,6 +122,7 @@ async function openOneStore(
94
122
  getEncryptionKey: () => Promise<Uint8Array>,
95
123
  log: Logger,
96
124
  openStore: OpenSqliteEncryptedStoreFn,
125
+ wipeStore: WipeSqliteStoreFn,
97
126
  ): Promise<AztecSQLiteOPFSStore> {
98
127
  const key = await getEncryptionKey();
99
128
  try {
@@ -102,6 +131,16 @@ async function openOneStore(
102
131
  if (err instanceof SqliteEncryptionError && err.code === 'decrypt_failed') {
103
132
  throw new EmbeddedWalletEncryptionError(storeName, { cause: err });
104
133
  }
134
+ if (err instanceof SqliteCorruptionError) {
135
+ // A corrupt image is unrecoverable — no key or retry against the same bytes brings it back. Self-heal
136
+ // instead of dead-ending forever: wipe the store's OPFS directory (safe — the failed open left no SAH-pool
137
+ // lock behind) and reopen a fresh, empty one, so callers see a normal first-run rather than a permanent
138
+ // "database disk image is malformed" on every open.
139
+ log.warn(`Embedded wallet '${storeName}' store is corrupt (${err.message}); wiping and reopening fresh`);
140
+ await wipeStore(poolDirectory);
141
+ // open() transferred (detached) the previous key buffer, so fetch a fresh one for the reopen.
142
+ return await openStore(log, name, poolDirectory, await getEncryptionKey());
143
+ }
105
144
  throw err;
106
145
  }
107
146
  }