@aztec/pxe 0.65.2 → 0.67.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.
Files changed (97) hide show
  1. package/dest/bin/index.js +5 -6
  2. package/dest/config/index.d.ts +0 -9
  3. package/dest/config/index.d.ts.map +1 -1
  4. package/dest/config/index.js +1 -17
  5. package/dest/config/package_info.d.ts +5 -0
  6. package/dest/config/package_info.d.ts.map +1 -0
  7. package/dest/config/package_info.js +4 -0
  8. package/dest/contract_data_oracle/index.js +2 -2
  9. package/dest/database/incoming_note_dao.d.ts +1 -1
  10. package/dest/database/incoming_note_dao.d.ts.map +1 -1
  11. package/dest/database/incoming_note_dao.js +2 -2
  12. package/dest/database/kv_pxe_database.d.ts +10 -13
  13. package/dest/database/kv_pxe_database.d.ts.map +1 -1
  14. package/dest/database/kv_pxe_database.js +153 -230
  15. package/dest/database/outgoing_note_dao.js +2 -2
  16. package/dest/database/pxe_database.d.ts +7 -25
  17. package/dest/database/pxe_database.d.ts.map +1 -1
  18. package/dest/database/pxe_database_test_suite.d.ts.map +1 -1
  19. package/dest/database/pxe_database_test_suite.js +18 -59
  20. package/dest/index.d.ts +2 -0
  21. package/dest/index.d.ts.map +1 -1
  22. package/dest/index.js +3 -1
  23. package/dest/kernel_oracle/index.d.ts.map +1 -1
  24. package/dest/kernel_oracle/index.js +3 -3
  25. package/dest/kernel_prover/hints/build_private_kernel_reset_private_inputs.d.ts +1 -1
  26. package/dest/kernel_prover/hints/build_private_kernel_reset_private_inputs.d.ts.map +1 -1
  27. package/dest/kernel_prover/hints/build_private_kernel_reset_private_inputs.js +13 -15
  28. package/dest/kernel_prover/index.d.ts +1 -0
  29. package/dest/kernel_prover/index.d.ts.map +1 -1
  30. package/dest/kernel_prover/index.js +2 -1
  31. package/dest/kernel_prover/kernel_prover.d.ts +1 -0
  32. package/dest/kernel_prover/kernel_prover.d.ts.map +1 -1
  33. package/dest/kernel_prover/kernel_prover.js +38 -4
  34. package/dest/kernel_prover/test/test_circuit_prover.d.ts.map +1 -1
  35. package/dest/kernel_prover/test/test_circuit_prover.js +4 -4
  36. package/dest/note_decryption_utils/add_public_values_to_payload.js +2 -2
  37. package/dest/note_decryption_utils/brute_force_note_info.d.ts +3 -3
  38. package/dest/note_decryption_utils/brute_force_note_info.d.ts.map +1 -1
  39. package/dest/note_decryption_utils/brute_force_note_info.js +8 -8
  40. package/dest/note_decryption_utils/produce_note_daos.d.ts +3 -6
  41. package/dest/note_decryption_utils/produce_note_daos.d.ts.map +1 -1
  42. package/dest/note_decryption_utils/produce_note_daos.js +5 -19
  43. package/dest/note_decryption_utils/produce_note_daos_for_key.d.ts +1 -1
  44. package/dest/note_decryption_utils/produce_note_daos_for_key.d.ts.map +1 -1
  45. package/dest/pxe_service/error_enriching.d.ts +3 -3
  46. package/dest/pxe_service/error_enriching.d.ts.map +1 -1
  47. package/dest/pxe_service/error_enriching.js +10 -10
  48. package/dest/pxe_service/index.d.ts +0 -1
  49. package/dest/pxe_service/index.d.ts.map +1 -1
  50. package/dest/pxe_service/index.js +1 -2
  51. package/dest/pxe_service/pxe_service.d.ts +4 -16
  52. package/dest/pxe_service/pxe_service.d.ts.map +1 -1
  53. package/dest/pxe_service/pxe_service.js +86 -101
  54. package/dest/pxe_service/test/pxe_test_suite.d.ts.map +1 -1
  55. package/dest/pxe_service/test/pxe_test_suite.js +1 -3
  56. package/dest/simulator/index.d.ts +1 -1
  57. package/dest/simulator/index.d.ts.map +1 -1
  58. package/dest/simulator/index.js +2 -2
  59. package/dest/simulator_oracle/index.d.ts +7 -6
  60. package/dest/simulator_oracle/index.d.ts.map +1 -1
  61. package/dest/simulator_oracle/index.js +65 -44
  62. package/dest/synchronizer/synchronizer.d.ts +10 -40
  63. package/dest/synchronizer/synchronizer.d.ts.map +1 -1
  64. package/dest/synchronizer/synchronizer.js +35 -69
  65. package/dest/{pxe_service → utils}/create_pxe_service.d.ts +1 -1
  66. package/dest/utils/create_pxe_service.d.ts.map +1 -0
  67. package/dest/utils/create_pxe_service.js +49 -0
  68. package/package.json +31 -19
  69. package/src/bin/index.ts +4 -5
  70. package/src/config/index.ts +0 -21
  71. package/src/config/package_info.ts +3 -0
  72. package/src/contract_data_oracle/index.ts +1 -1
  73. package/src/database/incoming_note_dao.ts +2 -2
  74. package/src/database/kv_pxe_database.ts +212 -309
  75. package/src/database/outgoing_note_dao.ts +1 -1
  76. package/src/database/pxe_database.ts +7 -28
  77. package/src/database/pxe_database_test_suite.ts +20 -75
  78. package/src/index.ts +2 -0
  79. package/src/kernel_oracle/index.ts +2 -2
  80. package/src/kernel_prover/hints/build_private_kernel_reset_private_inputs.ts +14 -16
  81. package/src/kernel_prover/index.ts +2 -0
  82. package/src/kernel_prover/kernel_prover.ts +61 -2
  83. package/src/kernel_prover/test/test_circuit_prover.ts +5 -3
  84. package/src/note_decryption_utils/add_public_values_to_payload.ts +1 -1
  85. package/src/note_decryption_utils/brute_force_note_info.ts +9 -9
  86. package/src/note_decryption_utils/produce_note_daos.ts +5 -48
  87. package/src/note_decryption_utils/produce_note_daos_for_key.ts +1 -1
  88. package/src/pxe_service/error_enriching.ts +14 -12
  89. package/src/pxe_service/index.ts +0 -1
  90. package/src/pxe_service/pxe_service.ts +137 -160
  91. package/src/pxe_service/test/pxe_test_suite.ts +0 -3
  92. package/src/simulator/index.ts +1 -1
  93. package/src/simulator_oracle/index.ts +71 -75
  94. package/src/synchronizer/synchronizer.ts +37 -77
  95. package/src/{pxe_service → utils}/create_pxe_service.ts +10 -10
  96. package/dest/pxe_service/create_pxe_service.d.ts.map +0 -1
  97. package/dest/pxe_service/create_pxe_service.js +0 -49
@@ -1,15 +1,9 @@
1
- import {
2
- type InBlock,
3
- type IncomingNotesFilter,
4
- MerkleTreeId,
5
- NoteStatus,
6
- type OutgoingNotesFilter,
7
- } from '@aztec/circuit-types';
1
+ import { type InBlock, type IncomingNotesFilter, MerkleTreeId, NoteStatus } from '@aztec/circuit-types';
8
2
  import {
9
3
  AztecAddress,
4
+ BlockHeader,
10
5
  CompleteAddress,
11
6
  type ContractInstanceWithAddress,
12
- Header,
13
7
  type IndexedTaggingSecret,
14
8
  type PublicKey,
15
9
  SerializableContractInstance,
@@ -17,66 +11,59 @@ import {
17
11
  import { type ContractArtifact, FunctionSelector, FunctionType } from '@aztec/foundation/abi';
18
12
  import { toBufferBE } from '@aztec/foundation/bigint-buffer';
19
13
  import { Fr } from '@aztec/foundation/fields';
14
+ import { toArray } from '@aztec/foundation/iterable';
20
15
  import {
21
- type AztecArray,
22
- type AztecKVStore,
23
- type AztecMap,
24
- type AztecMultiMap,
25
- type AztecSet,
26
- type AztecSingleton,
16
+ type AztecAsyncArray,
17
+ type AztecAsyncKVStore,
18
+ type AztecAsyncMap,
19
+ type AztecAsyncMultiMap,
20
+ type AztecAsyncSet,
21
+ type AztecAsyncSingleton,
27
22
  } from '@aztec/kv-store';
28
23
  import { contractArtifactFromBuffer, contractArtifactToBuffer } from '@aztec/types/abi';
29
24
 
30
25
  import { IncomingNoteDao } from './incoming_note_dao.js';
31
- import { OutgoingNoteDao } from './outgoing_note_dao.js';
32
26
  import { type PxeDatabase } from './pxe_database.js';
33
27
 
34
28
  /**
35
29
  * A PXE database backed by LMDB.
36
30
  */
37
31
  export class KVPxeDatabase implements PxeDatabase {
38
- #synchronizedBlock: AztecSingleton<Buffer>;
39
- #completeAddresses: AztecArray<Buffer>;
40
- #completeAddressIndex: AztecMap<string, number>;
41
- #addressBook: AztecSet<string>;
42
- #authWitnesses: AztecMap<string, Buffer[]>;
43
- #capsules: AztecArray<Buffer[]>;
44
- #notes: AztecMap<string, Buffer>;
45
- #nullifiedNotes: AztecMap<string, Buffer>;
46
- #nullifierToNoteId: AztecMap<string, string>;
47
- #nullifiersByBlockNumber: AztecMultiMap<number, string>;
48
-
49
- #nullifiedNotesToScope: AztecMultiMap<string, string>;
50
- #nullifiedNotesByContract: AztecMultiMap<string, string>;
51
- #nullifiedNotesByStorageSlot: AztecMultiMap<string, string>;
52
- #nullifiedNotesByTxHash: AztecMultiMap<string, string>;
53
- #nullifiedNotesByAddressPoint: AztecMultiMap<string, string>;
54
- #nullifiedNotesByNullifier: AztecMap<string, string>;
55
- #syncedBlockPerPublicKey: AztecMap<string, number>;
56
- #contractArtifacts: AztecMap<string, Buffer>;
57
- #contractInstances: AztecMap<string, Buffer>;
58
- #db: AztecKVStore;
59
-
60
- #outgoingNotes: AztecMap<string, Buffer>;
61
- #outgoingNotesByContract: AztecMultiMap<string, string>;
62
- #outgoingNotesByStorageSlot: AztecMultiMap<string, string>;
63
- #outgoingNotesByTxHash: AztecMultiMap<string, string>;
64
- #outgoingNotesByOvpkM: AztecMultiMap<string, string>;
65
-
66
- #scopes: AztecSet<string>;
67
- #notesToScope: AztecMultiMap<string, string>;
68
- #notesByContractAndScope: Map<string, AztecMultiMap<string, string>>;
69
- #notesByStorageSlotAndScope: Map<string, AztecMultiMap<string, string>>;
70
- #notesByTxHashAndScope: Map<string, AztecMultiMap<string, string>>;
71
- #notesByAddressPointAndScope: Map<string, AztecMultiMap<string, string>>;
32
+ #synchronizedBlock: AztecAsyncSingleton<Buffer>;
33
+ #completeAddresses: AztecAsyncArray<Buffer>;
34
+ #completeAddressIndex: AztecAsyncMap<string, number>;
35
+ #addressBook: AztecAsyncSet<string>;
36
+ #authWitnesses: AztecAsyncMap<string, Buffer[]>;
37
+ #capsules: AztecAsyncArray<Buffer[]>;
38
+ #notes: AztecAsyncMap<string, Buffer>;
39
+ #nullifiedNotes: AztecAsyncMap<string, Buffer>;
40
+ #nullifierToNoteId: AztecAsyncMap<string, string>;
41
+ #nullifiersByBlockNumber: AztecAsyncMultiMap<number, string>;
42
+
43
+ #nullifiedNotesToScope: AztecAsyncMultiMap<string, string>;
44
+ #nullifiedNotesByContract: AztecAsyncMultiMap<string, string>;
45
+ #nullifiedNotesByStorageSlot: AztecAsyncMultiMap<string, string>;
46
+ #nullifiedNotesByTxHash: AztecAsyncMultiMap<string, string>;
47
+ #nullifiedNotesByAddressPoint: AztecAsyncMultiMap<string, string>;
48
+ #nullifiedNotesByNullifier: AztecAsyncMap<string, string>;
49
+ #contractArtifacts: AztecAsyncMap<string, Buffer>;
50
+ #contractInstances: AztecAsyncMap<string, Buffer>;
51
+ #db: AztecAsyncKVStore;
52
+
53
+ #scopes: AztecAsyncSet<string>;
54
+ #notesToScope: AztecAsyncMultiMap<string, string>;
55
+ #notesByContractAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
56
+ #notesByStorageSlotAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
57
+ #notesByTxHashAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
58
+ #notesByAddressPointAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
72
59
 
73
60
  // Stores the last index used for each tagging secret, taking direction into account
74
61
  // This is necessary to avoid reusing the same index for the same secret, which happens if
75
62
  // sender and recipient are the same
76
- #taggingSecretIndexesForSenders: AztecMap<string, number>;
77
- #taggingSecretIndexesForRecipients: AztecMap<string, number>;
63
+ #taggingSecretIndexesForSenders: AztecAsyncMap<string, number>;
64
+ #taggingSecretIndexesForRecipients: AztecAsyncMap<string, number>;
78
65
 
79
- constructor(private db: AztecKVStore) {
66
+ protected constructor(private db: AztecAsyncKVStore) {
80
67
  this.#db = db;
81
68
 
82
69
  this.#completeAddresses = db.openArray('complete_addresses');
@@ -91,7 +78,6 @@ export class KVPxeDatabase implements PxeDatabase {
91
78
  this.#contractInstances = db.openMap('contracts_instances');
92
79
 
93
80
  this.#synchronizedBlock = db.openSingleton('header');
94
- this.#syncedBlockPerPublicKey = db.openMap('synced_block_per_public_key');
95
81
 
96
82
  this.#notes = db.openMap('notes');
97
83
  this.#nullifiedNotes = db.openMap('nullified_notes');
@@ -105,30 +91,28 @@ export class KVPxeDatabase implements PxeDatabase {
105
91
  this.#nullifiedNotesByAddressPoint = db.openMultiMap('nullified_notes_by_address_point');
106
92
  this.#nullifiedNotesByNullifier = db.openMap('nullified_notes_by_nullifier');
107
93
 
108
- this.#outgoingNotes = db.openMap('outgoing_notes');
109
- this.#outgoingNotesByContract = db.openMultiMap('outgoing_notes_by_contract');
110
- this.#outgoingNotesByStorageSlot = db.openMultiMap('outgoing_notes_by_storage_slot');
111
- this.#outgoingNotesByTxHash = db.openMultiMap('outgoing_notes_by_tx_hash');
112
- this.#outgoingNotesByOvpkM = db.openMultiMap('outgoing_notes_by_ovpk_m');
113
-
114
94
  this.#scopes = db.openSet('scopes');
115
95
  this.#notesToScope = db.openMultiMap('notes_to_scope');
116
- this.#notesByContractAndScope = new Map<string, AztecMultiMap<string, string>>();
117
- this.#notesByStorageSlotAndScope = new Map<string, AztecMultiMap<string, string>>();
118
- this.#notesByTxHashAndScope = new Map<string, AztecMultiMap<string, string>>();
119
- this.#notesByAddressPointAndScope = new Map<string, AztecMultiMap<string, string>>();
120
-
121
- for (const scope of this.#scopes.entries()) {
122
- this.#notesByContractAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_contract`));
123
- this.#notesByStorageSlotAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_storage_slot`));
124
- this.#notesByTxHashAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_tx_hash`));
125
- this.#notesByAddressPointAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_address_point`));
126
- }
96
+ this.#notesByContractAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
97
+ this.#notesByStorageSlotAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
98
+ this.#notesByTxHashAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
99
+ this.#notesByAddressPointAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
127
100
 
128
101
  this.#taggingSecretIndexesForSenders = db.openMap('tagging_secret_indexes_for_senders');
129
102
  this.#taggingSecretIndexesForRecipients = db.openMap('tagging_secret_indexes_for_recipients');
130
103
  }
131
104
 
105
+ public static async create(db: AztecAsyncKVStore): Promise<KVPxeDatabase> {
106
+ const pxeDB = new KVPxeDatabase(db);
107
+ for await (const scope of pxeDB.#scopes.entriesAsync()) {
108
+ pxeDB.#notesByContractAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_contract`));
109
+ pxeDB.#notesByStorageSlotAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_storage_slot`));
110
+ pxeDB.#notesByTxHashAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_tx_hash`));
111
+ pxeDB.#notesByAddressPointAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_address_point`));
112
+ }
113
+ return pxeDB;
114
+ }
115
+
132
116
  public async getContract(
133
117
  address: AztecAddress,
134
118
  ): Promise<(ContractInstanceWithAddress & ContractArtifact) | undefined> {
@@ -157,10 +141,10 @@ export class KVPxeDatabase implements PxeDatabase {
157
141
  await this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract));
158
142
  }
159
143
 
160
- public getContractArtifact(id: Fr): Promise<ContractArtifact | undefined> {
161
- const contract = this.#contractArtifacts.get(id.toString());
162
- // TODO(@spalladino): AztecMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
163
- return Promise.resolve(contract && contractArtifactFromBuffer(Buffer.from(contract)));
144
+ public async getContractArtifact(id: Fr): Promise<ContractArtifact | undefined> {
145
+ const contract = await this.#contractArtifacts.getAsync(id.toString());
146
+ // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
147
+ return contract && contractArtifactFromBuffer(Buffer.from(contract));
164
148
  }
165
149
 
166
150
  async addContractInstance(contract: ContractInstanceWithAddress): Promise<void> {
@@ -170,13 +154,14 @@ export class KVPxeDatabase implements PxeDatabase {
170
154
  );
171
155
  }
172
156
 
173
- getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
174
- const contract = this.#contractInstances.get(address.toString());
175
- return Promise.resolve(contract && SerializableContractInstance.fromBuffer(contract).withAddress(address));
157
+ async getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
158
+ const contract = await this.#contractInstances.getAsync(address.toString());
159
+ return contract && SerializableContractInstance.fromBuffer(contract).withAddress(address);
176
160
  }
177
161
 
178
- getContractsAddresses(): Promise<AztecAddress[]> {
179
- return Promise.resolve(Array.from(this.#contractInstances.keys()).map(AztecAddress.fromString));
162
+ async getContractsAddresses(): Promise<AztecAddress[]> {
163
+ const keys = await toArray(this.#contractInstances.keysAsync());
164
+ return keys.map(AztecAddress.fromString);
180
165
  }
181
166
 
182
167
  async addAuthWitness(messageHash: Fr, witness: Fr[]): Promise<void> {
@@ -186,8 +171,8 @@ export class KVPxeDatabase implements PxeDatabase {
186
171
  );
187
172
  }
188
173
 
189
- getAuthWitness(messageHash: Fr): Promise<Fr[] | undefined> {
190
- const witness = this.#authWitnesses.get(messageHash.toString());
174
+ async getAuthWitness(messageHash: Fr): Promise<Fr[] | undefined> {
175
+ const witness = await this.#authWitnesses.getAsync(messageHash.toString());
191
176
  return Promise.resolve(witness?.map(w => Fr.fromBuffer(w)));
192
177
  }
193
178
 
@@ -201,176 +186,164 @@ export class KVPxeDatabase implements PxeDatabase {
201
186
  }
202
187
 
203
188
  async addNote(note: IncomingNoteDao, scope?: AztecAddress): Promise<void> {
204
- await this.addNotes([note], [], scope);
189
+ await this.addNotes([note], scope);
205
190
  }
206
191
 
207
- async addNotes(
208
- incomingNotes: IncomingNoteDao[],
209
- outgoingNotes: OutgoingNoteDao[],
210
- scope: AztecAddress = AztecAddress.ZERO,
211
- ): Promise<void> {
212
- if (!this.#scopes.has(scope.toString())) {
192
+ async addNotes(incomingNotes: IncomingNoteDao[], scope: AztecAddress = AztecAddress.ZERO): Promise<void> {
193
+ if (!(await this.#scopes.hasAsync(scope.toString()))) {
213
194
  await this.#addScope(scope);
214
195
  }
215
196
 
216
- return this.db.transaction(() => {
197
+ return this.db.transactionAsync(async () => {
217
198
  for (const dao of incomingNotes) {
218
199
  // store notes by their index in the notes hash tree
219
200
  // this provides the uniqueness we need to store individual notes
220
201
  // and should also return notes in the order that they were created.
221
202
  // Had we stored them by their nullifier, they would be returned in random order
222
203
  const noteIndex = toBufferBE(dao.index, 32).toString('hex');
223
- void this.#notes.set(noteIndex, dao.toBuffer());
224
- void this.#notesToScope.set(noteIndex, scope.toString());
225
- void this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
226
-
227
- void this.#notesByContractAndScope.get(scope.toString())!.set(dao.contractAddress.toString(), noteIndex);
228
- void this.#notesByStorageSlotAndScope.get(scope.toString())!.set(dao.storageSlot.toString(), noteIndex);
229
- void this.#notesByTxHashAndScope.get(scope.toString())!.set(dao.txHash.toString(), noteIndex);
230
- void this.#notesByAddressPointAndScope.get(scope.toString())!.set(dao.addressPoint.toString(), noteIndex);
231
- }
232
-
233
- for (const dao of outgoingNotes) {
234
- const noteIndex = toBufferBE(dao.index, 32).toString('hex');
235
- void this.#outgoingNotes.set(noteIndex, dao.toBuffer());
236
- void this.#outgoingNotesByContract.set(dao.contractAddress.toString(), noteIndex);
237
- void this.#outgoingNotesByStorageSlot.set(dao.storageSlot.toString(), noteIndex);
238
- void this.#outgoingNotesByTxHash.set(dao.txHash.toString(), noteIndex);
239
- void this.#outgoingNotesByOvpkM.set(dao.ovpkM.toString(), noteIndex);
204
+ await this.#notes.set(noteIndex, dao.toBuffer());
205
+ await this.#notesToScope.set(noteIndex, scope.toString());
206
+ await this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
207
+
208
+ await this.#notesByContractAndScope.get(scope.toString())!.set(dao.contractAddress.toString(), noteIndex);
209
+ await this.#notesByStorageSlotAndScope.get(scope.toString())!.set(dao.storageSlot.toString(), noteIndex);
210
+ await this.#notesByTxHashAndScope.get(scope.toString())!.set(dao.txHash.toString(), noteIndex);
211
+ await this.#notesByAddressPointAndScope.get(scope.toString())!.set(dao.addressPoint.toString(), noteIndex);
240
212
  }
241
213
  });
242
214
  }
243
215
 
244
216
  public removeNotesAfter(blockNumber: number): Promise<void> {
245
- return this.db.transaction(() => {
246
- for (const note of this.#notes.values()) {
217
+ return this.db.transactionAsync(async () => {
218
+ const notes = await toArray(this.#notes.valuesAsync());
219
+ for (const note of notes) {
247
220
  const noteDao = IncomingNoteDao.fromBuffer(note);
248
221
  if (noteDao.l2BlockNumber > blockNumber) {
249
222
  const noteIndex = toBufferBE(noteDao.index, 32).toString('hex');
250
- void this.#notes.delete(noteIndex);
251
- void this.#notesToScope.delete(noteIndex);
252
- void this.#nullifierToNoteId.delete(noteDao.siloedNullifier.toString());
253
- for (const scope of this.#scopes.entries()) {
254
- void this.#notesByAddressPointAndScope.get(scope)!.deleteValue(noteDao.addressPoint.toString(), noteIndex);
255
- void this.#notesByTxHashAndScope.get(scope)!.deleteValue(noteDao.txHash.toString(), noteIndex);
256
- void this.#notesByContractAndScope.get(scope)!.deleteValue(noteDao.contractAddress.toString(), noteIndex);
257
- void this.#notesByStorageSlotAndScope.get(scope)!.deleteValue(noteDao.storageSlot.toString(), noteIndex);
223
+ await this.#notes.delete(noteIndex);
224
+ await this.#notesToScope.delete(noteIndex);
225
+ await this.#nullifierToNoteId.delete(noteDao.siloedNullifier.toString());
226
+ const scopes = await toArray(this.#scopes.entriesAsync());
227
+ for (const scope of scopes) {
228
+ await this.#notesByAddressPointAndScope.get(scope)!.deleteValue(noteDao.addressPoint.toString(), noteIndex);
229
+ await this.#notesByTxHashAndScope.get(scope)!.deleteValue(noteDao.txHash.toString(), noteIndex);
230
+ await this.#notesByContractAndScope.get(scope)!.deleteValue(noteDao.contractAddress.toString(), noteIndex);
231
+ await this.#notesByStorageSlotAndScope.get(scope)!.deleteValue(noteDao.storageSlot.toString(), noteIndex);
258
232
  }
259
233
  }
260
234
  }
261
-
262
- for (const note of this.#outgoingNotes.values()) {
263
- const noteDao = OutgoingNoteDao.fromBuffer(note);
264
- if (noteDao.l2BlockNumber > blockNumber) {
265
- const noteIndex = toBufferBE(noteDao.index, 32).toString('hex');
266
- void this.#outgoingNotes.delete(noteIndex);
267
- void this.#outgoingNotesByContract.deleteValue(noteDao.contractAddress.toString(), noteIndex);
268
- void this.#outgoingNotesByStorageSlot.deleteValue(noteDao.storageSlot.toString(), noteIndex);
269
- void this.#outgoingNotesByTxHash.deleteValue(noteDao.txHash.toString(), noteIndex);
270
- void this.#outgoingNotesByOvpkM.deleteValue(noteDao.ovpkM.toString(), noteIndex);
271
- }
272
- }
273
235
  });
274
236
  }
275
237
 
276
238
  public async unnullifyNotesAfter(blockNumber: number): Promise<void> {
277
239
  const nullifiersToUndo: string[] = [];
278
240
  const currentBlockNumber = blockNumber + 1;
279
- const maxBlockNumber = this.getBlockNumber() ?? currentBlockNumber;
241
+ const maxBlockNumber = (await this.getBlockNumber()) ?? currentBlockNumber;
280
242
  for (let i = currentBlockNumber; i <= maxBlockNumber; i++) {
281
- nullifiersToUndo.push(...this.#nullifiersByBlockNumber.getValues(i));
243
+ nullifiersToUndo.push(...(await toArray(this.#nullifiersByBlockNumber.getValuesAsync(i))));
282
244
  }
283
245
 
284
- const notesIndexesToReinsert = await this.db.transaction(() =>
285
- nullifiersToUndo.map(nullifier => this.#nullifiedNotesByNullifier.get(nullifier)),
246
+ const notesIndexesToReinsert = await Promise.all(
247
+ nullifiersToUndo.map(nullifier => this.#nullifiedNotesByNullifier.getAsync(nullifier)),
248
+ );
249
+ const notNullNoteIndexes = notesIndexesToReinsert.filter(noteIndex => noteIndex != undefined);
250
+ const nullifiedNoteBuffers = await Promise.all(
251
+ notNullNoteIndexes.map(noteIndex => this.#nullifiedNotes.getAsync(noteIndex!)),
286
252
  );
287
- const nullifiedNoteBuffers = await this.db.transaction(() => {
288
- return notesIndexesToReinsert
289
- .filter(noteIndex => noteIndex != undefined)
290
- .map(noteIndex => this.#nullifiedNotes.get(noteIndex!));
291
- });
292
253
  const noteDaos = nullifiedNoteBuffers
293
254
  .filter(buffer => buffer != undefined)
294
255
  .map(buffer => IncomingNoteDao.fromBuffer(buffer!));
295
256
 
296
- await this.db.transaction(() => {
257
+ await this.db.transactionAsync(async () => {
297
258
  for (const dao of noteDaos) {
298
259
  const noteIndex = toBufferBE(dao.index, 32).toString('hex');
299
- void this.#notes.set(noteIndex, dao.toBuffer());
300
- void this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
260
+ await this.#notes.set(noteIndex, dao.toBuffer());
261
+ await this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
301
262
 
302
- let scopes = Array.from(this.#nullifiedNotesToScope.getValues(noteIndex) ?? []);
263
+ let scopes = (await toArray(this.#nullifiedNotesToScope.getValuesAsync(noteIndex))) ?? [];
303
264
 
304
265
  if (scopes.length === 0) {
305
266
  scopes = [new AztecAddress(dao.addressPoint.x).toString()];
306
267
  }
307
268
 
308
269
  for (const scope of scopes) {
309
- void this.#notesByContractAndScope.get(scope)!.set(dao.contractAddress.toString(), noteIndex);
310
- void this.#notesByStorageSlotAndScope.get(scope)!.set(dao.storageSlot.toString(), noteIndex);
311
- void this.#notesByTxHashAndScope.get(scope)!.set(dao.txHash.toString(), noteIndex);
312
- void this.#notesByAddressPointAndScope.get(scope)!.set(dao.addressPoint.toString(), noteIndex);
313
- void this.#notesToScope.set(noteIndex, scope);
270
+ await this.#notesByContractAndScope.get(scope.toString())!.set(dao.contractAddress.toString(), noteIndex);
271
+ await this.#notesByStorageSlotAndScope.get(scope.toString())!.set(dao.storageSlot.toString(), noteIndex);
272
+ await this.#notesByTxHashAndScope.get(scope.toString())!.set(dao.txHash.toString(), noteIndex);
273
+ await this.#notesByAddressPointAndScope.get(scope.toString())!.set(dao.addressPoint.toString(), noteIndex);
274
+ await this.#notesToScope.set(noteIndex, scope);
314
275
  }
315
276
 
316
- void this.#nullifiedNotes.delete(noteIndex);
317
- void this.#nullifiedNotesToScope.delete(noteIndex);
318
- void this.#nullifiersByBlockNumber.deleteValue(dao.l2BlockNumber, dao.siloedNullifier.toString());
319
- void this.#nullifiedNotesByContract.deleteValue(dao.contractAddress.toString(), noteIndex);
320
- void this.#nullifiedNotesByStorageSlot.deleteValue(dao.storageSlot.toString(), noteIndex);
321
- void this.#nullifiedNotesByTxHash.deleteValue(dao.txHash.toString(), noteIndex);
322
- void this.#nullifiedNotesByAddressPoint.deleteValue(dao.addressPoint.toString(), noteIndex);
323
- void this.#nullifiedNotesByNullifier.delete(dao.siloedNullifier.toString());
277
+ await this.#nullifiedNotes.delete(noteIndex);
278
+ await this.#nullifiedNotesToScope.delete(noteIndex);
279
+ await this.#nullifiersByBlockNumber.deleteValue(dao.l2BlockNumber, dao.siloedNullifier.toString());
280
+ await this.#nullifiedNotesByContract.deleteValue(dao.contractAddress.toString(), noteIndex);
281
+ await this.#nullifiedNotesByStorageSlot.deleteValue(dao.storageSlot.toString(), noteIndex);
282
+ await this.#nullifiedNotesByTxHash.deleteValue(dao.txHash.toString(), noteIndex);
283
+ await this.#nullifiedNotesByAddressPoint.deleteValue(dao.addressPoint.toString(), noteIndex);
284
+ await this.#nullifiedNotesByNullifier.delete(dao.siloedNullifier.toString());
324
285
  }
325
286
  });
326
287
  }
327
288
 
328
- getIncomingNotes(filter: IncomingNotesFilter): Promise<IncomingNoteDao[]> {
289
+ async getIncomingNotes(filter: IncomingNotesFilter): Promise<IncomingNoteDao[]> {
329
290
  const publicKey: PublicKey | undefined = filter.owner ? filter.owner.toAddressPoint() : undefined;
330
291
 
331
292
  filter.status = filter.status ?? NoteStatus.ACTIVE;
332
293
 
333
294
  const candidateNoteSources = [];
334
295
 
335
- filter.scopes ??= [...this.#scopes.entries()].map(addressString => AztecAddress.fromString(addressString));
296
+ filter.scopes ??= (await toArray(this.#scopes.entriesAsync())).map(addressString =>
297
+ AztecAddress.fromString(addressString),
298
+ );
336
299
 
337
- const activeNoteIdsPerScope: IterableIterator<string>[] = [];
300
+ const activeNoteIdsPerScope: string[][] = [];
338
301
 
339
302
  for (const scope of new Set(filter.scopes)) {
340
303
  const formattedScopeString = scope.toString();
341
- if (!this.#scopes.has(formattedScopeString)) {
304
+ if (!this.#scopes.hasAsync(formattedScopeString)) {
342
305
  throw new Error('Trying to get incoming notes of an scope that is not in the PXE database');
343
306
  }
344
307
 
345
308
  activeNoteIdsPerScope.push(
346
309
  publicKey
347
- ? this.#notesByAddressPointAndScope.get(formattedScopeString)!.getValues(publicKey.toString())
310
+ ? await toArray(
311
+ this.#notesByAddressPointAndScope.get(formattedScopeString)!.getValuesAsync(publicKey.toString()),
312
+ )
348
313
  : filter.txHash
349
- ? this.#notesByTxHashAndScope.get(formattedScopeString)!.getValues(filter.txHash.toString())
314
+ ? await toArray(
315
+ this.#notesByTxHashAndScope.get(formattedScopeString)!.getValuesAsync(filter.txHash.toString()),
316
+ )
350
317
  : filter.contractAddress
351
- ? this.#notesByContractAndScope.get(formattedScopeString)!.getValues(filter.contractAddress.toString())
318
+ ? await toArray(
319
+ this.#notesByContractAndScope
320
+ .get(formattedScopeString)!
321
+ .getValuesAsync(filter.contractAddress.toString()),
322
+ )
352
323
  : filter.storageSlot
353
- ? this.#notesByStorageSlotAndScope.get(formattedScopeString)!.getValues(filter.storageSlot.toString())
354
- : this.#notesByAddressPointAndScope.get(formattedScopeString)!.values(),
324
+ ? await toArray(
325
+ this.#notesByStorageSlotAndScope.get(formattedScopeString)!.getValuesAsync(filter.storageSlot.toString()),
326
+ )
327
+ : await toArray(this.#notesByAddressPointAndScope.get(formattedScopeString)!.valuesAsync()),
355
328
  );
356
329
  }
357
330
 
358
331
  candidateNoteSources.push({
359
- ids: new Set(activeNoteIdsPerScope.flatMap(iterableIterator => [...iterableIterator])),
332
+ ids: new Set(activeNoteIdsPerScope.flat()),
360
333
  notes: this.#notes,
361
334
  });
362
335
 
363
336
  if (filter.status == NoteStatus.ACTIVE_OR_NULLIFIED) {
364
337
  candidateNoteSources.push({
365
338
  ids: publicKey
366
- ? this.#nullifiedNotesByAddressPoint.getValues(publicKey.toString())
339
+ ? await toArray(this.#nullifiedNotesByAddressPoint.getValuesAsync(publicKey.toString()))
367
340
  : filter.txHash
368
- ? this.#nullifiedNotesByTxHash.getValues(filter.txHash.toString())
341
+ ? await toArray(this.#nullifiedNotesByTxHash.getValuesAsync(filter.txHash.toString()))
369
342
  : filter.contractAddress
370
- ? this.#nullifiedNotesByContract.getValues(filter.contractAddress.toString())
343
+ ? await toArray(this.#nullifiedNotesByContract.getValuesAsync(filter.contractAddress.toString()))
371
344
  : filter.storageSlot
372
- ? this.#nullifiedNotesByStorageSlot.getValues(filter.storageSlot.toString())
373
- : this.#nullifiedNotes.keys(),
345
+ ? await toArray(this.#nullifiedNotesByStorageSlot.getValuesAsync(filter.storageSlot.toString()))
346
+ : await toArray(this.#nullifiedNotes.keysAsync()),
374
347
  notes: this.#nullifiedNotes,
375
348
  });
376
349
  }
@@ -378,7 +351,7 @@ export class KVPxeDatabase implements PxeDatabase {
378
351
  const result: IncomingNoteDao[] = [];
379
352
  for (const { ids, notes } of candidateNoteSources) {
380
353
  for (const id of ids) {
381
- const serializedNote = notes.get(id);
354
+ const serializedNote = await notes.getAsync(id);
382
355
  if (!serializedNote) {
383
356
  continue;
384
357
  }
@@ -408,57 +381,7 @@ export class KVPxeDatabase implements PxeDatabase {
408
381
  }
409
382
  }
410
383
 
411
- return Promise.resolve(result);
412
- }
413
-
414
- getOutgoingNotes(filter: OutgoingNotesFilter): Promise<OutgoingNoteDao[]> {
415
- const ovpkM: PublicKey | undefined = filter.owner
416
- ? this.#getCompleteAddress(filter.owner)?.publicKeys.masterOutgoingViewingPublicKey
417
- : undefined;
418
-
419
- // Check if ovpkM is truthy
420
- const ids = ovpkM
421
- ? this.#outgoingNotesByOvpkM.getValues(ovpkM.toString())
422
- : // If ovpkM is falsy, check if filter.txHash is truthy
423
- filter.txHash
424
- ? this.#outgoingNotesByTxHash.getValues(filter.txHash.toString())
425
- : // If both ovpkM and filter.txHash are falsy, check if filter.contractAddress is truthy
426
- filter.contractAddress
427
- ? this.#outgoingNotesByContract.getValues(filter.contractAddress.toString())
428
- : // If ovpkM, filter.txHash, and filter.contractAddress are all falsy, check if filter.storageSlot is truthy
429
- filter.storageSlot
430
- ? this.#outgoingNotesByStorageSlot.getValues(filter.storageSlot.toString())
431
- : // If none of the above conditions are met, retrieve all keys from this.#outgoingNotes
432
- this.#outgoingNotes.keys();
433
-
434
- const notes: OutgoingNoteDao[] = [];
435
- for (const id of ids) {
436
- const serializedNote = this.#outgoingNotes.get(id);
437
- if (!serializedNote) {
438
- continue;
439
- }
440
-
441
- const note = OutgoingNoteDao.fromBuffer(serializedNote);
442
- if (filter.contractAddress && !note.contractAddress.equals(filter.contractAddress)) {
443
- continue;
444
- }
445
-
446
- if (filter.txHash && !note.txHash.equals(filter.txHash)) {
447
- continue;
448
- }
449
-
450
- if (filter.storageSlot && !note.storageSlot.equals(filter.storageSlot!)) {
451
- continue;
452
- }
453
-
454
- if (ovpkM && !note.ovpkM.equals(ovpkM)) {
455
- continue;
456
- }
457
-
458
- notes.push(note);
459
- }
460
-
461
- return Promise.resolve(notes);
384
+ return result;
462
385
  }
463
386
 
464
387
  removeNullifiedNotes(nullifiers: InBlock<Fr>[], accountAddressPoint: PublicKey): Promise<IncomingNoteDao[]> {
@@ -466,23 +389,23 @@ export class KVPxeDatabase implements PxeDatabase {
466
389
  return Promise.resolve([]);
467
390
  }
468
391
 
469
- return this.#db.transaction(() => {
392
+ return this.db.transactionAsync(async () => {
470
393
  const nullifiedNotes: IncomingNoteDao[] = [];
471
394
 
472
395
  for (const blockScopedNullifier of nullifiers) {
473
396
  const { data: nullifier, l2BlockNumber: blockNumber } = blockScopedNullifier;
474
- const noteIndex = this.#nullifierToNoteId.get(nullifier.toString());
397
+ const noteIndex = await this.#nullifierToNoteId.getAsync(nullifier.toString());
475
398
  if (!noteIndex) {
476
399
  continue;
477
400
  }
478
401
 
479
- const noteBuffer = noteIndex ? this.#notes.get(noteIndex) : undefined;
402
+ const noteBuffer = noteIndex ? await this.#notes.getAsync(noteIndex) : undefined;
480
403
 
481
404
  if (!noteBuffer) {
482
405
  // note doesn't exist. Maybe it got nullified already
483
406
  continue;
484
407
  }
485
- const noteScopes = this.#notesToScope.getValues(noteIndex) ?? [];
408
+ const noteScopes = (await toArray(this.#notesToScope.getValuesAsync(noteIndex))) ?? [];
486
409
  const note = IncomingNoteDao.fromBuffer(noteBuffer);
487
410
  if (!note.addressPoint.equals(accountAddressPoint)) {
488
411
  // tried to nullify someone else's note
@@ -491,32 +414,33 @@ export class KVPxeDatabase implements PxeDatabase {
491
414
 
492
415
  nullifiedNotes.push(note);
493
416
 
494
- void this.#notes.delete(noteIndex);
495
- void this.#notesToScope.delete(noteIndex);
417
+ await this.#notes.delete(noteIndex);
418
+ await this.#notesToScope.delete(noteIndex);
496
419
 
497
- for (const scope of this.#scopes.entries()) {
498
- void this.#notesByAddressPointAndScope.get(scope)!.deleteValue(accountAddressPoint.toString(), noteIndex);
499
- void this.#notesByTxHashAndScope.get(scope)!.deleteValue(note.txHash.toString(), noteIndex);
500
- void this.#notesByContractAndScope.get(scope)!.deleteValue(note.contractAddress.toString(), noteIndex);
501
- void this.#notesByStorageSlotAndScope.get(scope)!.deleteValue(note.storageSlot.toString(), noteIndex);
420
+ const scopes = await toArray(this.#scopes.entriesAsync());
421
+
422
+ for (const scope of scopes) {
423
+ await this.#notesByAddressPointAndScope.get(scope)!.deleteValue(accountAddressPoint.toString(), noteIndex);
424
+ await this.#notesByTxHashAndScope.get(scope)!.deleteValue(note.txHash.toString(), noteIndex);
425
+ await this.#notesByContractAndScope.get(scope)!.deleteValue(note.contractAddress.toString(), noteIndex);
426
+ await this.#notesByStorageSlotAndScope.get(scope)!.deleteValue(note.storageSlot.toString(), noteIndex);
502
427
  }
503
428
 
504
429
  if (noteScopes !== undefined) {
505
430
  for (const scope of noteScopes) {
506
- void this.#nullifiedNotesToScope.set(noteIndex, scope);
431
+ await this.#nullifiedNotesToScope.set(noteIndex, scope);
507
432
  }
508
433
  }
509
- void this.#nullifiedNotes.set(noteIndex, note.toBuffer());
510
- void this.#nullifiersByBlockNumber.set(blockNumber, nullifier.toString());
511
- void this.#nullifiedNotesByContract.set(note.contractAddress.toString(), noteIndex);
512
- void this.#nullifiedNotesByStorageSlot.set(note.storageSlot.toString(), noteIndex);
513
- void this.#nullifiedNotesByTxHash.set(note.txHash.toString(), noteIndex);
514
- void this.#nullifiedNotesByAddressPoint.set(note.addressPoint.toString(), noteIndex);
515
- void this.#nullifiedNotesByNullifier.set(nullifier.toString(), noteIndex);
516
-
517
- void this.#nullifierToNoteId.delete(nullifier.toString());
434
+ await this.#nullifiedNotes.set(noteIndex, note.toBuffer());
435
+ await this.#nullifiersByBlockNumber.set(blockNumber, nullifier.toString());
436
+ await this.#nullifiedNotesByContract.set(note.contractAddress.toString(), noteIndex);
437
+ await this.#nullifiedNotesByStorageSlot.set(note.storageSlot.toString(), noteIndex);
438
+ await this.#nullifiedNotesByTxHash.set(note.txHash.toString(), noteIndex);
439
+ await this.#nullifiedNotesByAddressPoint.set(note.addressPoint.toString(), noteIndex);
440
+ await this.#nullifiedNotesByNullifier.set(nullifier.toString(), noteIndex);
441
+
442
+ await this.#nullifierToNoteId.delete(nullifier.toString());
518
443
  }
519
-
520
444
  return nullifiedNotes;
521
445
  });
522
446
  }
@@ -529,36 +453,34 @@ export class KVPxeDatabase implements PxeDatabase {
529
453
  await this.#nullifiedNotesByStorageSlot.set(note.storageSlot.toString(), noteIndex);
530
454
  await this.#nullifiedNotesByTxHash.set(note.txHash.toString(), noteIndex);
531
455
  await this.#nullifiedNotesByAddressPoint.set(note.addressPoint.toString(), noteIndex);
532
-
533
- return Promise.resolve();
534
456
  }
535
457
 
536
- async setHeader(header: Header): Promise<void> {
458
+ async setHeader(header: BlockHeader): Promise<void> {
537
459
  await this.#synchronizedBlock.set(header.toBuffer());
538
460
  }
539
461
 
540
- getBlockNumber(): number | undefined {
541
- const headerBuffer = this.#synchronizedBlock.get();
462
+ async getBlockNumber(): Promise<number | undefined> {
463
+ const headerBuffer = await this.#synchronizedBlock.getAsync();
542
464
  if (!headerBuffer) {
543
465
  return undefined;
544
466
  }
545
467
 
546
- return Number(Header.fromBuffer(headerBuffer).globalVariables.blockNumber.toBigInt());
468
+ return Number(BlockHeader.fromBuffer(headerBuffer).globalVariables.blockNumber.toBigInt());
547
469
  }
548
470
 
549
- getHeader(): Header {
550
- const headerBuffer = this.#synchronizedBlock.get();
471
+ async getBlockHeader(): Promise<BlockHeader> {
472
+ const headerBuffer = await this.#synchronizedBlock.getAsync();
551
473
  if (!headerBuffer) {
552
474
  throw new Error(`Header not set`);
553
475
  }
554
476
 
555
- return Header.fromBuffer(headerBuffer);
477
+ return BlockHeader.fromBuffer(headerBuffer);
556
478
  }
557
479
 
558
480
  async #addScope(scope: AztecAddress): Promise<boolean> {
559
481
  const scopeString = scope.toString();
560
482
 
561
- if (this.#scopes.has(scopeString)) {
483
+ if (await this.#scopes.hasAsync(scopeString)) {
562
484
  return false;
563
485
  }
564
486
 
@@ -571,23 +493,23 @@ export class KVPxeDatabase implements PxeDatabase {
571
493
  return true;
572
494
  }
573
495
 
574
- async addCompleteAddress(completeAddress: CompleteAddress): Promise<boolean> {
575
- await this.#addScope(completeAddress.address);
496
+ addCompleteAddress(completeAddress: CompleteAddress): Promise<boolean> {
497
+ return this.db.transactionAsync(async () => {
498
+ await this.#addScope(completeAddress.address);
576
499
 
577
- return this.#db.transaction(() => {
578
500
  const addressString = completeAddress.address.toString();
579
501
  const buffer = completeAddress.toBuffer();
580
- const existing = this.#completeAddressIndex.get(addressString);
581
- if (typeof existing === 'undefined') {
582
- const index = this.#completeAddresses.length;
583
- void this.#completeAddresses.push(buffer);
584
- void this.#completeAddressIndex.set(addressString, index);
502
+ const existing = await this.#completeAddressIndex.getAsync(addressString);
503
+ if (existing === undefined) {
504
+ const index = await this.#completeAddresses.lengthAsync();
505
+ await this.#completeAddresses.push(buffer);
506
+ await this.#completeAddressIndex.set(addressString, index);
585
507
 
586
508
  return true;
587
509
  } else {
588
- const existingBuffer = this.#completeAddresses.at(existing);
510
+ const existingBuffer = await this.#completeAddresses.atAsync(existing);
589
511
 
590
- if (existingBuffer?.equals(buffer)) {
512
+ if (existingBuffer && Buffer.from(existingBuffer).equals(buffer)) {
591
513
  return false;
592
514
  }
593
515
 
@@ -598,26 +520,26 @@ export class KVPxeDatabase implements PxeDatabase {
598
520
  });
599
521
  }
600
522
 
601
- #getCompleteAddress(address: AztecAddress): CompleteAddress | undefined {
602
- const index = this.#completeAddressIndex.get(address.toString());
603
- if (typeof index === 'undefined') {
523
+ async #getCompleteAddress(address: AztecAddress): Promise<CompleteAddress | undefined> {
524
+ const index = await this.#completeAddressIndex.getAsync(address.toString());
525
+ if (index === undefined) {
604
526
  return undefined;
605
527
  }
606
528
 
607
- const value = this.#completeAddresses.at(index);
529
+ const value = await this.#completeAddresses.atAsync(index);
608
530
  return value ? CompleteAddress.fromBuffer(value) : undefined;
609
531
  }
610
532
 
611
533
  getCompleteAddress(account: AztecAddress): Promise<CompleteAddress | undefined> {
612
- return Promise.resolve(this.#getCompleteAddress(account));
534
+ return this.#getCompleteAddress(account);
613
535
  }
614
536
 
615
- getCompleteAddresses(): Promise<CompleteAddress[]> {
616
- return Promise.resolve(Array.from(this.#completeAddresses).map(v => CompleteAddress.fromBuffer(v)));
537
+ async getCompleteAddresses(): Promise<CompleteAddress[]> {
538
+ return (await toArray(this.#completeAddresses.valuesAsync())).map(v => CompleteAddress.fromBuffer(v));
617
539
  }
618
540
 
619
541
  async addContactAddress(address: AztecAddress): Promise<boolean> {
620
- if (this.#addressBook.has(address.toString())) {
542
+ if (await this.#addressBook.hasAsync(address.toString())) {
621
543
  return false;
622
544
  }
623
545
 
@@ -626,12 +548,12 @@ export class KVPxeDatabase implements PxeDatabase {
626
548
  return true;
627
549
  }
628
550
 
629
- getContactAddresses(): AztecAddress[] {
630
- return [...this.#addressBook.entries()].map(AztecAddress.fromString);
551
+ async getContactAddresses(): Promise<AztecAddress[]> {
552
+ return (await toArray(this.#addressBook.entriesAsync())).map(AztecAddress.fromString);
631
553
  }
632
554
 
633
555
  async removeContactAddress(address: AztecAddress): Promise<boolean> {
634
- if (!this.#addressBook.has(address.toString())) {
556
+ if (!this.#addressBook.hasAsync(address.toString())) {
635
557
  return false;
636
558
  }
637
559
 
@@ -640,32 +562,17 @@ export class KVPxeDatabase implements PxeDatabase {
640
562
  return true;
641
563
  }
642
564
 
643
- getSynchedBlockNumberForAccount(account: AztecAddress): number | undefined {
644
- return this.#syncedBlockPerPublicKey.get(account.toString());
645
- }
646
-
647
- setSynchedBlockNumberForAccount(account: AztecAddress, blockNumber: number): Promise<void> {
648
- return this.#syncedBlockPerPublicKey.set(account.toString(), blockNumber);
649
- }
650
-
651
565
  async estimateSize(): Promise<number> {
652
- const incomingNotesSize = Array.from(await this.getIncomingNotes({})).reduce(
653
- (sum, note) => sum + note.getSize(),
654
- 0,
655
- );
656
- const outgoingNotesSize = Array.from(await this.getOutgoingNotes({})).reduce(
657
- (sum, note) => sum + note.getSize(),
658
- 0,
659
- );
566
+ const incomingNotesSize = (await this.getIncomingNotes({})).reduce((sum, note) => sum + note.getSize(), 0);
660
567
 
661
- const authWitsSize = Array.from(this.#authWitnesses.values()).reduce(
568
+ const authWitsSize = (await toArray(this.#authWitnesses.valuesAsync())).reduce(
662
569
  (sum, value) => sum + value.length * Fr.SIZE_IN_BYTES,
663
570
  0,
664
571
  );
665
- const addressesSize = this.#completeAddresses.length * CompleteAddress.SIZE_IN_BYTES;
572
+ const addressesSize = (await this.#completeAddresses.lengthAsync()) * CompleteAddress.SIZE_IN_BYTES;
666
573
  const treeRootsSize = Object.keys(MerkleTreeId).length * Fr.SIZE_IN_BYTES;
667
574
 
668
- return incomingNotesSize + outgoingNotesSize + treeRootsSize + authWitsSize + addressesSize;
575
+ return incomingNotesSize + treeRootsSize + authWitsSize + addressesSize;
669
576
  }
670
577
 
671
578
  async setTaggingSecretsIndexesAsSender(indexedSecrets: IndexedTaggingSecret[]): Promise<void> {
@@ -676,12 +583,10 @@ export class KVPxeDatabase implements PxeDatabase {
676
583
  await this.#setTaggingSecretsIndexes(indexedSecrets, this.#taggingSecretIndexesForRecipients);
677
584
  }
678
585
 
679
- #setTaggingSecretsIndexes(indexedSecrets: IndexedTaggingSecret[], storageMap: AztecMap<string, number>) {
680
- return this.db.transaction(() => {
681
- indexedSecrets.forEach(
682
- indexedSecret => void storageMap.set(indexedSecret.secret.toString(), indexedSecret.index),
683
- );
684
- });
586
+ async #setTaggingSecretsIndexes(indexedSecrets: IndexedTaggingSecret[], storageMap: AztecAsyncMap<string, number>) {
587
+ await Promise.all(
588
+ indexedSecrets.map(indexedSecret => storageMap.set(indexedSecret.secret.toString(), indexedSecret.index)),
589
+ );
685
590
  }
686
591
 
687
592
  async getTaggingSecretsIndexesAsRecipient(appTaggingSecrets: Fr[]) {
@@ -692,18 +597,16 @@ export class KVPxeDatabase implements PxeDatabase {
692
597
  return await this.#getTaggingSecretsIndexes(appTaggingSecrets, this.#taggingSecretIndexesForSenders);
693
598
  }
694
599
 
695
- #getTaggingSecretsIndexes(appTaggingSecrets: Fr[], storageMap: AztecMap<string, number>): Promise<number[]> {
696
- return this.db.transaction(() => appTaggingSecrets.map(secret => storageMap.get(`${secret.toString()}`) ?? 0));
600
+ #getTaggingSecretsIndexes(appTaggingSecrets: Fr[], storageMap: AztecAsyncMap<string, number>): Promise<number[]> {
601
+ return Promise.all(appTaggingSecrets.map(async secret => (await storageMap.getAsync(`${secret.toString()}`)) ?? 0));
697
602
  }
698
603
 
699
- async resetNoteSyncData(): Promise<void> {
700
- await this.db.transaction(() => {
701
- for (const recipient of this.#taggingSecretIndexesForRecipients.keys()) {
702
- void this.#taggingSecretIndexesForRecipients.delete(recipient);
703
- }
704
- for (const sender of this.#taggingSecretIndexesForSenders.keys()) {
705
- void this.#taggingSecretIndexesForSenders.delete(sender);
706
- }
604
+ resetNoteSyncData(): Promise<void> {
605
+ return this.db.transactionAsync(async () => {
606
+ const recipients = await toArray(this.#taggingSecretIndexesForRecipients.keysAsync());
607
+ await Promise.all(recipients.map(recipient => this.#taggingSecretIndexesForRecipients.delete(recipient)));
608
+ const senders = await toArray(this.#taggingSecretIndexesForSenders.keysAsync());
609
+ await Promise.all(senders.map(sender => this.#taggingSecretIndexesForSenders.delete(sender)));
707
610
  });
708
611
  }
709
612
  }