@aztec/pxe 0.77.1 → 0.78.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.
Files changed (180) hide show
  1. package/dest/bin/index.js +1 -1
  2. package/dest/config/index.d.ts +1 -0
  3. package/dest/config/index.d.ts.map +1 -1
  4. package/dest/config/index.js +1 -0
  5. package/dest/entrypoints/client/bundle/index.d.ts +6 -0
  6. package/dest/entrypoints/client/bundle/index.d.ts.map +1 -0
  7. package/dest/entrypoints/client/bundle/index.js +5 -0
  8. package/dest/entrypoints/client/bundle/utils.d.ts +16 -0
  9. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -0
  10. package/dest/entrypoints/client/bundle/utils.js +30 -0
  11. package/dest/entrypoints/client/lazy/index.d.ts +6 -0
  12. package/dest/entrypoints/client/lazy/index.d.ts.map +1 -0
  13. package/dest/entrypoints/client/lazy/index.js +5 -0
  14. package/dest/entrypoints/client/lazy/utils.d.ts +15 -0
  15. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -0
  16. package/dest/entrypoints/client/lazy/utils.js +29 -0
  17. package/dest/entrypoints/client/pxe_creation_options.d.ts +11 -0
  18. package/dest/entrypoints/client/pxe_creation_options.d.ts.map +1 -0
  19. package/dest/entrypoints/server/index.d.ts +7 -0
  20. package/dest/entrypoints/server/index.d.ts.map +1 -0
  21. package/dest/entrypoints/server/index.js +6 -0
  22. package/dest/{utils/create_pxe_service.d.ts → entrypoints/server/utils.d.ts} +4 -4
  23. package/dest/entrypoints/server/utils.d.ts.map +1 -0
  24. package/dest/{utils/create_pxe_service.js → entrypoints/server/utils.js} +5 -11
  25. package/dest/kernel_oracle/index.d.ts +2 -1
  26. package/dest/kernel_oracle/index.d.ts.map +1 -1
  27. package/dest/kernel_prover/kernel_prover.d.ts.map +1 -1
  28. package/dest/kernel_prover/kernel_prover.js +0 -28
  29. package/dest/note_decryption_utils/add_public_values_to_payload.d.ts +2 -2
  30. package/dest/note_decryption_utils/add_public_values_to_payload.d.ts.map +1 -1
  31. package/dest/note_decryption_utils/add_public_values_to_payload.js +3 -3
  32. package/dest/{pxe_data_provider → pxe_oracle_interface}/index.d.ts +17 -9
  33. package/dest/pxe_oracle_interface/index.d.ts.map +1 -0
  34. package/dest/{pxe_data_provider → pxe_oracle_interface}/index.js +104 -61
  35. package/dest/pxe_oracle_interface/tagging_utils.d.ts.map +1 -0
  36. package/dest/pxe_service/error_enriching.d.ts +3 -4
  37. package/dest/pxe_service/error_enriching.d.ts.map +1 -1
  38. package/dest/pxe_service/error_enriching.js +4 -4
  39. package/dest/pxe_service/pxe_service.d.ts +18 -14
  40. package/dest/pxe_service/pxe_service.d.ts.map +1 -1
  41. package/dest/pxe_service/pxe_service.js +118 -65
  42. package/dest/storage/address_data_provider/address_data_provider.d.ts +13 -0
  43. package/dest/storage/address_data_provider/address_data_provider.d.ts.map +1 -0
  44. package/dest/storage/address_data_provider/address_data_provider.js +50 -0
  45. package/dest/storage/address_data_provider/index.d.ts +2 -0
  46. package/dest/storage/address_data_provider/index.d.ts.map +1 -0
  47. package/dest/storage/address_data_provider/index.js +1 -0
  48. package/dest/storage/auth_witness_data_provider/auth_witness_data_provider.d.ts +11 -0
  49. package/dest/storage/auth_witness_data_provider/auth_witness_data_provider.d.ts.map +1 -0
  50. package/dest/storage/auth_witness_data_provider/auth_witness_data_provider.js +20 -0
  51. package/dest/storage/auth_witness_data_provider/index.d.ts +2 -0
  52. package/dest/storage/auth_witness_data_provider/index.d.ts.map +1 -0
  53. package/dest/storage/auth_witness_data_provider/index.js +1 -0
  54. package/dest/storage/capsule_data_provider/capsule_data_provider.d.ts +16 -0
  55. package/dest/storage/capsule_data_provider/capsule_data_provider.d.ts.map +1 -0
  56. package/dest/storage/capsule_data_provider/capsule_data_provider.js +57 -0
  57. package/dest/storage/capsule_data_provider/index.d.ts +2 -0
  58. package/dest/storage/capsule_data_provider/index.d.ts.map +1 -0
  59. package/dest/storage/capsule_data_provider/index.js +1 -0
  60. package/dest/{contract_data_provider → storage/contract_data_provider}/contract_data_provider.d.ts +38 -33
  61. package/dest/storage/contract_data_provider/contract_data_provider.d.ts.map +1 -0
  62. package/dest/{contract_data_provider → storage/contract_data_provider}/contract_data_provider.js +87 -42
  63. package/dest/storage/contract_data_provider/index.d.ts.map +1 -0
  64. package/dest/storage/contract_data_provider/private_functions_tree.d.ts.map +1 -0
  65. package/dest/storage/data_provider.d.ts +4 -0
  66. package/dest/storage/data_provider.d.ts.map +1 -0
  67. package/dest/storage/data_provider.js +1 -0
  68. package/dest/storage/index.d.ts +10 -0
  69. package/dest/storage/index.d.ts.map +1 -0
  70. package/dest/storage/index.js +9 -0
  71. package/dest/storage/note_data_provider/index.d.ts +3 -0
  72. package/dest/storage/note_data_provider/index.d.ts.map +1 -0
  73. package/dest/storage/note_data_provider/index.js +2 -0
  74. package/dest/storage/note_data_provider/note_dao.d.ts.map +1 -0
  75. package/dest/storage/note_data_provider/note_data_provider.d.ts +20 -0
  76. package/dest/storage/note_data_provider/note_data_provider.d.ts.map +1 -0
  77. package/dest/storage/note_data_provider/note_data_provider.js +249 -0
  78. package/dest/storage/sync_data_provider/index.d.ts +2 -0
  79. package/dest/storage/sync_data_provider/index.d.ts.map +1 -0
  80. package/dest/storage/sync_data_provider/index.js +1 -0
  81. package/dest/storage/sync_data_provider/sync_data_provider.d.ts +12 -0
  82. package/dest/storage/sync_data_provider/sync_data_provider.d.ts.map +1 -0
  83. package/dest/storage/sync_data_provider/sync_data_provider.js +29 -0
  84. package/dest/storage/tagging_data_provider/index.d.ts +2 -0
  85. package/dest/storage/tagging_data_provider/index.d.ts.map +1 -0
  86. package/dest/storage/tagging_data_provider/index.js +1 -0
  87. package/dest/storage/tagging_data_provider/tagging_data_provider.d.ts +18 -0
  88. package/dest/storage/tagging_data_provider/tagging_data_provider.d.ts.map +1 -0
  89. package/dest/storage/tagging_data_provider/tagging_data_provider.js +65 -0
  90. package/dest/synchronizer/synchronizer.d.ts +7 -3
  91. package/dest/synchronizer/synchronizer.d.ts.map +1 -1
  92. package/dest/synchronizer/synchronizer.js +16 -11
  93. package/dest/test/pxe_test_suite.d.ts.map +1 -0
  94. package/dest/{pxe_service/test → test}/pxe_test_suite.js +2 -6
  95. package/package.json +19 -26
  96. package/src/bin/index.ts +1 -1
  97. package/src/config/index.ts +2 -0
  98. package/src/entrypoints/client/bundle/index.ts +5 -0
  99. package/src/entrypoints/client/bundle/utils.ts +58 -0
  100. package/src/entrypoints/client/lazy/index.ts +5 -0
  101. package/src/entrypoints/client/lazy/utils.ts +53 -0
  102. package/src/entrypoints/client/pxe_creation_options.ts +7 -0
  103. package/src/entrypoints/server/index.ts +6 -0
  104. package/src/{utils/create_pxe_service.ts → entrypoints/server/utils.ts} +7 -18
  105. package/src/kernel_oracle/index.ts +1 -1
  106. package/src/kernel_prover/kernel_prover.ts +0 -51
  107. package/src/note_decryption_utils/add_public_values_to_payload.ts +4 -4
  108. package/src/{pxe_data_provider → pxe_oracle_interface}/index.ts +128 -96
  109. package/src/pxe_service/error_enriching.ts +8 -6
  110. package/src/pxe_service/pxe_service.ts +155 -89
  111. package/src/storage/address_data_provider/address_data_provider.ts +71 -0
  112. package/src/storage/address_data_provider/index.ts +1 -0
  113. package/src/storage/auth_witness_data_provider/auth_witness_data_provider.ts +34 -0
  114. package/src/storage/auth_witness_data_provider/index.ts +1 -0
  115. package/src/storage/capsule_data_provider/capsule_data_provider.ts +80 -0
  116. package/src/storage/capsule_data_provider/index.ts +1 -0
  117. package/src/{contract_data_provider → storage/contract_data_provider}/contract_data_provider.ts +128 -48
  118. package/src/storage/data_provider.ts +3 -0
  119. package/src/storage/index.ts +10 -0
  120. package/src/storage/note_data_provider/index.ts +2 -0
  121. package/src/storage/note_data_provider/note_data_provider.ts +345 -0
  122. package/src/storage/sync_data_provider/index.ts +1 -0
  123. package/src/storage/sync_data_provider/sync_data_provider.ts +40 -0
  124. package/src/storage/tagging_data_provider/index.ts +1 -0
  125. package/src/storage/tagging_data_provider/tagging_data_provider.ts +92 -0
  126. package/src/synchronizer/synchronizer.ts +15 -10
  127. package/src/{pxe_service/test → test}/pxe_test_suite.ts +2 -9
  128. package/dest/contract_data_provider/contract_data_provider.d.ts.map +0 -1
  129. package/dest/contract_data_provider/index.d.ts.map +0 -1
  130. package/dest/contract_data_provider/private_functions_tree.d.ts.map +0 -1
  131. package/dest/database/index.d.ts +0 -3
  132. package/dest/database/index.d.ts.map +0 -1
  133. package/dest/database/index.js +0 -2
  134. package/dest/database/interfaces/contract_artifact_db.d.ts +0 -20
  135. package/dest/database/interfaces/contract_artifact_db.d.ts.map +0 -1
  136. package/dest/database/interfaces/contract_artifact_db.js +0 -3
  137. package/dest/database/interfaces/contract_instance_db.d.ts +0 -20
  138. package/dest/database/interfaces/contract_instance_db.d.ts.map +0 -1
  139. package/dest/database/interfaces/contract_instance_db.js +0 -3
  140. package/dest/database/interfaces/index.d.ts +0 -4
  141. package/dest/database/interfaces/index.d.ts.map +0 -1
  142. package/dest/database/interfaces/pxe_database.d.ts +0 -211
  143. package/dest/database/interfaces/pxe_database.d.ts.map +0 -1
  144. package/dest/database/interfaces/pxe_database.js +0 -4
  145. package/dest/database/interfaces/pxe_database_test_suite.d.ts +0 -7
  146. package/dest/database/interfaces/pxe_database_test_suite.d.ts.map +0 -1
  147. package/dest/database/interfaces/pxe_database_test_suite.js +0 -557
  148. package/dest/database/kv_pxe_database.d.ts +0 -58
  149. package/dest/database/kv_pxe_database.d.ts.map +0 -1
  150. package/dest/database/kv_pxe_database.js +0 -480
  151. package/dest/database/note_dao.d.ts.map +0 -1
  152. package/dest/index.d.ts +0 -9
  153. package/dest/index.d.ts.map +0 -1
  154. package/dest/index.js +0 -8
  155. package/dest/pxe_data_provider/index.d.ts.map +0 -1
  156. package/dest/pxe_data_provider/tagging_utils.d.ts.map +0 -1
  157. package/dest/pxe_service/test/pxe_test_suite.d.ts.map +0 -1
  158. package/dest/utils/create_pxe_service.d.ts.map +0 -1
  159. package/src/database/index.ts +0 -2
  160. package/src/database/interfaces/contract_artifact_db.ts +0 -20
  161. package/src/database/interfaces/contract_instance_db.ts +0 -21
  162. package/src/database/interfaces/index.ts +0 -3
  163. package/src/database/interfaces/pxe_database.ts +0 -240
  164. package/src/database/interfaces/pxe_database_test_suite.ts +0 -558
  165. package/src/database/kv_pxe_database.ts +0 -670
  166. package/src/index.ts +0 -9
  167. /package/dest/{database/interfaces/index.js → entrypoints/client/pxe_creation_options.js} +0 -0
  168. /package/dest/{pxe_data_provider → pxe_oracle_interface}/tagging_utils.d.ts +0 -0
  169. /package/dest/{pxe_data_provider → pxe_oracle_interface}/tagging_utils.js +0 -0
  170. /package/dest/{contract_data_provider → storage/contract_data_provider}/index.d.ts +0 -0
  171. /package/dest/{contract_data_provider → storage/contract_data_provider}/index.js +0 -0
  172. /package/dest/{contract_data_provider → storage/contract_data_provider}/private_functions_tree.d.ts +0 -0
  173. /package/dest/{contract_data_provider → storage/contract_data_provider}/private_functions_tree.js +0 -0
  174. /package/dest/{database → storage/note_data_provider}/note_dao.d.ts +0 -0
  175. /package/dest/{database → storage/note_data_provider}/note_dao.js +0 -0
  176. /package/dest/{pxe_service/test → test}/pxe_test_suite.d.ts +0 -0
  177. /package/src/{pxe_data_provider → pxe_oracle_interface}/tagging_utils.ts +0 -0
  178. /package/src/{contract_data_provider → storage/contract_data_provider}/index.ts +0 -0
  179. /package/src/{contract_data_provider → storage/contract_data_provider}/private_functions_tree.ts +0 -0
  180. /package/src/{database → storage/note_data_provider}/note_dao.ts +0 -0
@@ -1,670 +0,0 @@
1
- import { toBufferBE } from '@aztec/foundation/bigint-buffer';
2
- import { Fr, type Point } from '@aztec/foundation/fields';
3
- import { toArray } from '@aztec/foundation/iterable';
4
- import { type LogFn, createDebugOnlyLogger } from '@aztec/foundation/log';
5
- import type {
6
- AztecAsyncArray,
7
- AztecAsyncKVStore,
8
- AztecAsyncMap,
9
- AztecAsyncMultiMap,
10
- AztecAsyncSingleton,
11
- } from '@aztec/kv-store';
12
- import { type ContractArtifact, FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
13
- import { contractArtifactFromBuffer, contractArtifactToBuffer } from '@aztec/stdlib/abi';
14
- import { AztecAddress } from '@aztec/stdlib/aztec-address';
15
- import type { InBlock } from '@aztec/stdlib/block';
16
- import {
17
- CompleteAddress,
18
- type ContractInstanceWithAddress,
19
- SerializableContractInstance,
20
- } from '@aztec/stdlib/contract';
21
- import type { PublicKey } from '@aztec/stdlib/keys';
22
- import type { IndexedTaggingSecret } from '@aztec/stdlib/logs';
23
- import { NoteStatus, type NotesFilter } from '@aztec/stdlib/note';
24
- import { MerkleTreeId } from '@aztec/stdlib/trees';
25
- import { BlockHeader } from '@aztec/stdlib/tx';
26
-
27
- import type { PxeDatabase } from './interfaces/pxe_database.js';
28
- import { NoteDao } from './note_dao.js';
29
-
30
- /**
31
- * A PXE database backed by LMDB.
32
- */
33
- export class KVPxeDatabase implements PxeDatabase {
34
- public static readonly SCHEMA_VERSION = 1;
35
-
36
- #synchronizedBlock: AztecAsyncSingleton<Buffer>;
37
- #completeAddresses: AztecAsyncArray<Buffer>;
38
- #completeAddressIndex: AztecAsyncMap<string, number>;
39
- #addressBook: AztecAsyncMap<string, true>;
40
- #authWitnesses: AztecAsyncMap<string, Buffer[]>;
41
- #notes: AztecAsyncMap<string, Buffer>;
42
- #nullifiedNotes: AztecAsyncMap<string, Buffer>;
43
- #nullifierToNoteId: AztecAsyncMap<string, string>;
44
- #nullifiersByBlockNumber: AztecAsyncMultiMap<number, string>;
45
-
46
- #nullifiedNotesToScope: AztecAsyncMultiMap<string, string>;
47
- #nullifiedNotesByContract: AztecAsyncMultiMap<string, string>;
48
- #nullifiedNotesByStorageSlot: AztecAsyncMultiMap<string, string>;
49
- #nullifiedNotesByTxHash: AztecAsyncMultiMap<string, string>;
50
- #nullifiedNotesByAddressPoint: AztecAsyncMultiMap<string, string>;
51
- #nullifiedNotesByNullifier: AztecAsyncMap<string, string>;
52
- #contractArtifacts: AztecAsyncMap<string, Buffer>;
53
- #contractInstances: AztecAsyncMap<string, Buffer>;
54
- #db: AztecAsyncKVStore;
55
-
56
- #scopes: AztecAsyncMap<string, true>;
57
- #notesToScope: AztecAsyncMultiMap<string, string>;
58
- #notesByContractAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
59
- #notesByStorageSlotAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
60
- #notesByTxHashAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
61
- #notesByAddressPointAndScope: Map<string, AztecAsyncMultiMap<string, string>>;
62
-
63
- // Stores the last index used for each tagging secret, taking direction into account
64
- // This is necessary to avoid reusing the same index for the same secret, which happens if
65
- // sender and recipient are the same
66
- #taggingSecretIndexesForSenders: AztecAsyncMap<string, number>;
67
- #taggingSecretIndexesForRecipients: AztecAsyncMap<string, number>;
68
-
69
- // Arbitrary data stored by contracts. Key is computed as `${contractAddress}:${key}`
70
- #capsules: AztecAsyncMap<string, Buffer>;
71
-
72
- debug: LogFn;
73
-
74
- protected constructor(private db: AztecAsyncKVStore) {
75
- this.#db = db;
76
-
77
- this.#completeAddresses = db.openArray('complete_addresses');
78
- this.#completeAddressIndex = db.openMap('complete_address_index');
79
-
80
- this.#addressBook = db.openMap('address_book');
81
-
82
- this.#authWitnesses = db.openMap('auth_witnesses');
83
-
84
- this.#contractArtifacts = db.openMap('contract_artifacts');
85
- this.#contractInstances = db.openMap('contracts_instances');
86
-
87
- this.#synchronizedBlock = db.openSingleton('header');
88
-
89
- this.#notes = db.openMap('notes');
90
- this.#nullifiedNotes = db.openMap('nullified_notes');
91
- this.#nullifierToNoteId = db.openMap('nullifier_to_note');
92
- this.#nullifiersByBlockNumber = db.openMultiMap('nullifier_to_block_number');
93
-
94
- this.#nullifiedNotesToScope = db.openMultiMap('nullified_notes_to_scope');
95
- this.#nullifiedNotesByContract = db.openMultiMap('nullified_notes_by_contract');
96
- this.#nullifiedNotesByStorageSlot = db.openMultiMap('nullified_notes_by_storage_slot');
97
- this.#nullifiedNotesByTxHash = db.openMultiMap('nullified_notes_by_tx_hash');
98
- this.#nullifiedNotesByAddressPoint = db.openMultiMap('nullified_notes_by_address_point');
99
- this.#nullifiedNotesByNullifier = db.openMap('nullified_notes_by_nullifier');
100
-
101
- this.#scopes = db.openMap('scopes');
102
- this.#notesToScope = db.openMultiMap('notes_to_scope');
103
- this.#notesByContractAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
104
- this.#notesByStorageSlotAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
105
- this.#notesByTxHashAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
106
- this.#notesByAddressPointAndScope = new Map<string, AztecAsyncMultiMap<string, string>>();
107
-
108
- this.#taggingSecretIndexesForSenders = db.openMap('tagging_secret_indexes_for_senders');
109
- this.#taggingSecretIndexesForRecipients = db.openMap('tagging_secret_indexes_for_recipients');
110
-
111
- this.#capsules = db.openMap('capsules');
112
-
113
- this.debug = createDebugOnlyLogger('aztec:kv-pxe-database');
114
- }
115
-
116
- public static async create(db: AztecAsyncKVStore): Promise<KVPxeDatabase> {
117
- const pxeDB = new KVPxeDatabase(db);
118
- for await (const scope of pxeDB.#scopes.keysAsync()) {
119
- pxeDB.#notesByContractAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_contract`));
120
- pxeDB.#notesByStorageSlotAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_storage_slot`));
121
- pxeDB.#notesByTxHashAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_tx_hash`));
122
- pxeDB.#notesByAddressPointAndScope.set(scope, db.openMultiMap(`${scope}:notes_by_address_point`));
123
- }
124
- return pxeDB;
125
- }
126
-
127
- public async getContract(
128
- address: AztecAddress,
129
- ): Promise<(ContractInstanceWithAddress & ContractArtifact) | undefined> {
130
- const instance = await this.getContractInstance(address);
131
- const artifact = instance && (await this.getContractArtifact(instance?.currentContractClassId));
132
- if (!instance || !artifact) {
133
- return undefined;
134
- }
135
- return { ...instance, ...artifact };
136
- }
137
-
138
- public async addContractArtifact(id: Fr, contract: ContractArtifact): Promise<void> {
139
- const privateFunctions = contract.functions.filter(
140
- functionArtifact => functionArtifact.functionType === FunctionType.PRIVATE,
141
- );
142
-
143
- const privateSelectors = await Promise.all(
144
- privateFunctions.map(async privateFunctionArtifact =>
145
- (
146
- await FunctionSelector.fromNameAndParameters(privateFunctionArtifact.name, privateFunctionArtifact.parameters)
147
- ).toString(),
148
- ),
149
- );
150
-
151
- if (privateSelectors.length !== new Set(privateSelectors).size) {
152
- throw new Error('Repeated function selectors of private functions');
153
- }
154
-
155
- await this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract));
156
- }
157
-
158
- public async getContractArtifact(id: Fr): Promise<ContractArtifact | undefined> {
159
- const contract = await this.#contractArtifacts.getAsync(id.toString());
160
- // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
161
- return contract && contractArtifactFromBuffer(Buffer.from(contract));
162
- }
163
-
164
- async addContractInstance(contract: ContractInstanceWithAddress): Promise<void> {
165
- await this.#contractInstances.set(
166
- contract.address.toString(),
167
- new SerializableContractInstance(contract).toBuffer(),
168
- );
169
- }
170
-
171
- async getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
172
- const contract = await this.#contractInstances.getAsync(address.toString());
173
- return contract && SerializableContractInstance.fromBuffer(contract).withAddress(address);
174
- }
175
-
176
- async getContractsAddresses(): Promise<AztecAddress[]> {
177
- const keys = await toArray(this.#contractInstances.keysAsync());
178
- return keys.map(AztecAddress.fromString);
179
- }
180
-
181
- async addAuthWitness(messageHash: Fr, witness: Fr[]): Promise<void> {
182
- await this.#authWitnesses.set(
183
- messageHash.toString(),
184
- witness.map(w => w.toBuffer()),
185
- );
186
- }
187
-
188
- async getAuthWitness(messageHash: Fr): Promise<Fr[] | undefined> {
189
- const witness = await this.#authWitnesses.getAsync(messageHash.toString());
190
- return Promise.resolve(witness?.map(w => Fr.fromBuffer(w)));
191
- }
192
-
193
- async addNote(note: NoteDao, scope?: AztecAddress): Promise<void> {
194
- await this.addNotes([note], scope);
195
- }
196
-
197
- async addNotes(notes: NoteDao[], scope: AztecAddress = AztecAddress.ZERO): Promise<void> {
198
- if (!(await this.#scopes.hasAsync(scope.toString()))) {
199
- await this.#addScope(scope);
200
- }
201
-
202
- return this.db.transactionAsync(async () => {
203
- for (const dao of notes) {
204
- // store notes by their index in the notes hash tree
205
- // this provides the uniqueness we need to store individual notes
206
- // and should also return notes in the order that they were created.
207
- // Had we stored them by their nullifier, they would be returned in random order
208
- const noteIndex = toBufferBE(dao.index, 32).toString('hex');
209
- await this.#notes.set(noteIndex, dao.toBuffer());
210
- await this.#notesToScope.set(noteIndex, scope.toString());
211
- await this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
212
-
213
- await this.#notesByContractAndScope.get(scope.toString())!.set(dao.contractAddress.toString(), noteIndex);
214
- await this.#notesByStorageSlotAndScope.get(scope.toString())!.set(dao.storageSlot.toString(), noteIndex);
215
- await this.#notesByTxHashAndScope.get(scope.toString())!.set(dao.txHash.toString(), noteIndex);
216
- await this.#notesByAddressPointAndScope.get(scope.toString())!.set(dao.addressPoint.toString(), noteIndex);
217
- }
218
- });
219
- }
220
-
221
- public removeNotesAfter(blockNumber: number): Promise<void> {
222
- return this.db.transactionAsync(async () => {
223
- const notes = await toArray(this.#notes.valuesAsync());
224
- for (const note of notes) {
225
- const noteDao = NoteDao.fromBuffer(note);
226
- if (noteDao.l2BlockNumber > blockNumber) {
227
- const noteIndex = toBufferBE(noteDao.index, 32).toString('hex');
228
- await this.#notes.delete(noteIndex);
229
- await this.#notesToScope.delete(noteIndex);
230
- await this.#nullifierToNoteId.delete(noteDao.siloedNullifier.toString());
231
- const scopes = await toArray(this.#scopes.keysAsync());
232
- for (const scope of scopes) {
233
- await this.#notesByAddressPointAndScope.get(scope)!.deleteValue(noteDao.addressPoint.toString(), noteIndex);
234
- await this.#notesByTxHashAndScope.get(scope)!.deleteValue(noteDao.txHash.toString(), noteIndex);
235
- await this.#notesByContractAndScope.get(scope)!.deleteValue(noteDao.contractAddress.toString(), noteIndex);
236
- await this.#notesByStorageSlotAndScope.get(scope)!.deleteValue(noteDao.storageSlot.toString(), noteIndex);
237
- }
238
- }
239
- }
240
- });
241
- }
242
-
243
- public async unnullifyNotesAfter(blockNumber: number): Promise<void> {
244
- const nullifiersToUndo: string[] = [];
245
- const currentBlockNumber = blockNumber + 1;
246
- const maxBlockNumber = (await this.getBlockNumber()) ?? currentBlockNumber;
247
- for (let i = currentBlockNumber; i <= maxBlockNumber; i++) {
248
- nullifiersToUndo.push(...(await toArray(this.#nullifiersByBlockNumber.getValuesAsync(i))));
249
- }
250
-
251
- const notesIndexesToReinsert = await Promise.all(
252
- nullifiersToUndo.map(nullifier => this.#nullifiedNotesByNullifier.getAsync(nullifier)),
253
- );
254
- const notNullNoteIndexes = notesIndexesToReinsert.filter(noteIndex => noteIndex != undefined);
255
- const nullifiedNoteBuffers = await Promise.all(
256
- notNullNoteIndexes.map(noteIndex => this.#nullifiedNotes.getAsync(noteIndex!)),
257
- );
258
- const noteDaos = nullifiedNoteBuffers
259
- .filter(buffer => buffer != undefined)
260
- .map(buffer => NoteDao.fromBuffer(buffer!));
261
-
262
- await this.db.transactionAsync(async () => {
263
- for (const dao of noteDaos) {
264
- const noteIndex = toBufferBE(dao.index, 32).toString('hex');
265
- await this.#notes.set(noteIndex, dao.toBuffer());
266
- await this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
267
-
268
- let scopes = (await toArray(this.#nullifiedNotesToScope.getValuesAsync(noteIndex))) ?? [];
269
-
270
- if (scopes.length === 0) {
271
- scopes = [new AztecAddress(dao.addressPoint.x).toString()];
272
- }
273
-
274
- for (const scope of scopes) {
275
- await this.#notesByContractAndScope.get(scope.toString())!.set(dao.contractAddress.toString(), noteIndex);
276
- await this.#notesByStorageSlotAndScope.get(scope.toString())!.set(dao.storageSlot.toString(), noteIndex);
277
- await this.#notesByTxHashAndScope.get(scope.toString())!.set(dao.txHash.toString(), noteIndex);
278
- await this.#notesByAddressPointAndScope.get(scope.toString())!.set(dao.addressPoint.toString(), noteIndex);
279
- await this.#notesToScope.set(noteIndex, scope);
280
- }
281
-
282
- await this.#nullifiedNotes.delete(noteIndex);
283
- await this.#nullifiedNotesToScope.delete(noteIndex);
284
- await this.#nullifiersByBlockNumber.deleteValue(dao.l2BlockNumber, dao.siloedNullifier.toString());
285
- await this.#nullifiedNotesByContract.deleteValue(dao.contractAddress.toString(), noteIndex);
286
- await this.#nullifiedNotesByStorageSlot.deleteValue(dao.storageSlot.toString(), noteIndex);
287
- await this.#nullifiedNotesByTxHash.deleteValue(dao.txHash.toString(), noteIndex);
288
- await this.#nullifiedNotesByAddressPoint.deleteValue(dao.addressPoint.toString(), noteIndex);
289
- await this.#nullifiedNotesByNullifier.delete(dao.siloedNullifier.toString());
290
- }
291
- });
292
- }
293
-
294
- async getNotes(filter: NotesFilter): Promise<NoteDao[]> {
295
- const publicKey: PublicKey | undefined = filter.owner ? await filter.owner.toAddressPoint() : undefined;
296
-
297
- filter.status = filter.status ?? NoteStatus.ACTIVE;
298
-
299
- const candidateNoteSources = [];
300
-
301
- filter.scopes ??= (await toArray(this.#scopes.keysAsync())).map(addressString =>
302
- AztecAddress.fromString(addressString),
303
- );
304
-
305
- const activeNoteIdsPerScope: string[][] = [];
306
-
307
- for (const scope of new Set(filter.scopes)) {
308
- const formattedScopeString = scope.toString();
309
- if (!(await this.#scopes.hasAsync(formattedScopeString))) {
310
- throw new Error('Trying to get incoming notes of an scope that is not in the PXE database');
311
- }
312
-
313
- activeNoteIdsPerScope.push(
314
- publicKey
315
- ? await toArray(
316
- this.#notesByAddressPointAndScope.get(formattedScopeString)!.getValuesAsync(publicKey.toString()),
317
- )
318
- : filter.txHash
319
- ? await toArray(
320
- this.#notesByTxHashAndScope.get(formattedScopeString)!.getValuesAsync(filter.txHash.toString()),
321
- )
322
- : filter.contractAddress
323
- ? await toArray(
324
- this.#notesByContractAndScope
325
- .get(formattedScopeString)!
326
- .getValuesAsync(filter.contractAddress.toString()),
327
- )
328
- : filter.storageSlot
329
- ? await toArray(
330
- this.#notesByStorageSlotAndScope.get(formattedScopeString)!.getValuesAsync(filter.storageSlot.toString()),
331
- )
332
- : await toArray(this.#notesByAddressPointAndScope.get(formattedScopeString)!.valuesAsync()),
333
- );
334
- }
335
-
336
- candidateNoteSources.push({
337
- ids: new Set(activeNoteIdsPerScope.flat()),
338
- notes: this.#notes,
339
- });
340
-
341
- if (filter.status == NoteStatus.ACTIVE_OR_NULLIFIED) {
342
- candidateNoteSources.push({
343
- ids: publicKey
344
- ? await toArray(this.#nullifiedNotesByAddressPoint.getValuesAsync(publicKey.toString()))
345
- : filter.txHash
346
- ? await toArray(this.#nullifiedNotesByTxHash.getValuesAsync(filter.txHash.toString()))
347
- : filter.contractAddress
348
- ? await toArray(this.#nullifiedNotesByContract.getValuesAsync(filter.contractAddress.toString()))
349
- : filter.storageSlot
350
- ? await toArray(this.#nullifiedNotesByStorageSlot.getValuesAsync(filter.storageSlot.toString()))
351
- : await toArray(this.#nullifiedNotes.keysAsync()),
352
- notes: this.#nullifiedNotes,
353
- });
354
- }
355
-
356
- const result: NoteDao[] = [];
357
- for (const { ids, notes } of candidateNoteSources) {
358
- for (const id of ids) {
359
- const serializedNote = await notes.getAsync(id);
360
- if (!serializedNote) {
361
- continue;
362
- }
363
-
364
- const note = NoteDao.fromBuffer(serializedNote);
365
- if (filter.contractAddress && !note.contractAddress.equals(filter.contractAddress)) {
366
- continue;
367
- }
368
-
369
- if (filter.txHash && !note.txHash.equals(filter.txHash)) {
370
- continue;
371
- }
372
-
373
- if (filter.storageSlot && !note.storageSlot.equals(filter.storageSlot!)) {
374
- continue;
375
- }
376
-
377
- if (publicKey && !note.addressPoint.equals(publicKey)) {
378
- continue;
379
- }
380
-
381
- if (filter.siloedNullifier && !note.siloedNullifier.equals(filter.siloedNullifier)) {
382
- continue;
383
- }
384
-
385
- result.push(note);
386
- }
387
- }
388
-
389
- return result;
390
- }
391
-
392
- removeNullifiedNotes(nullifiers: InBlock<Fr>[], accountAddressPoint: Point): Promise<NoteDao[]> {
393
- if (nullifiers.length === 0) {
394
- return Promise.resolve([]);
395
- }
396
-
397
- return this.db.transactionAsync(async () => {
398
- const nullifiedNotes: NoteDao[] = [];
399
-
400
- for (const blockScopedNullifier of nullifiers) {
401
- const { data: nullifier, l2BlockNumber: blockNumber } = blockScopedNullifier;
402
- const noteIndex = await this.#nullifierToNoteId.getAsync(nullifier.toString());
403
- if (!noteIndex) {
404
- continue;
405
- }
406
-
407
- const noteBuffer = noteIndex ? await this.#notes.getAsync(noteIndex) : undefined;
408
-
409
- if (!noteBuffer) {
410
- // note doesn't exist. Maybe it got nullified already
411
- continue;
412
- }
413
- const noteScopes = (await toArray(this.#notesToScope.getValuesAsync(noteIndex))) ?? [];
414
- const note = NoteDao.fromBuffer(noteBuffer);
415
- if (!note.addressPoint.equals(accountAddressPoint)) {
416
- // tried to nullify someone else's note
417
- continue;
418
- }
419
-
420
- nullifiedNotes.push(note);
421
-
422
- await this.#notes.delete(noteIndex);
423
- await this.#notesToScope.delete(noteIndex);
424
-
425
- const scopes = await toArray(this.#scopes.keysAsync());
426
-
427
- for (const scope of scopes) {
428
- await this.#notesByAddressPointAndScope.get(scope)!.deleteValue(accountAddressPoint.toString(), noteIndex);
429
- await this.#notesByTxHashAndScope.get(scope)!.deleteValue(note.txHash.toString(), noteIndex);
430
- await this.#notesByContractAndScope.get(scope)!.deleteValue(note.contractAddress.toString(), noteIndex);
431
- await this.#notesByStorageSlotAndScope.get(scope)!.deleteValue(note.storageSlot.toString(), noteIndex);
432
- }
433
-
434
- if (noteScopes !== undefined) {
435
- for (const scope of noteScopes) {
436
- await this.#nullifiedNotesToScope.set(noteIndex, scope);
437
- }
438
- }
439
- await this.#nullifiedNotes.set(noteIndex, note.toBuffer());
440
- await this.#nullifiersByBlockNumber.set(blockNumber, nullifier.toString());
441
- await this.#nullifiedNotesByContract.set(note.contractAddress.toString(), noteIndex);
442
- await this.#nullifiedNotesByStorageSlot.set(note.storageSlot.toString(), noteIndex);
443
- await this.#nullifiedNotesByTxHash.set(note.txHash.toString(), noteIndex);
444
- await this.#nullifiedNotesByAddressPoint.set(note.addressPoint.toString(), noteIndex);
445
- await this.#nullifiedNotesByNullifier.set(nullifier.toString(), noteIndex);
446
-
447
- await this.#nullifierToNoteId.delete(nullifier.toString());
448
- }
449
- return nullifiedNotes;
450
- });
451
- }
452
-
453
- async addNullifiedNote(note: NoteDao): Promise<void> {
454
- const noteIndex = toBufferBE(note.index, 32).toString('hex');
455
-
456
- await this.#nullifiedNotes.set(noteIndex, note.toBuffer());
457
- await this.#nullifiedNotesByContract.set(note.contractAddress.toString(), noteIndex);
458
- await this.#nullifiedNotesByStorageSlot.set(note.storageSlot.toString(), noteIndex);
459
- await this.#nullifiedNotesByTxHash.set(note.txHash.toString(), noteIndex);
460
- await this.#nullifiedNotesByAddressPoint.set(note.addressPoint.toString(), noteIndex);
461
- }
462
-
463
- async setHeader(header: BlockHeader): Promise<void> {
464
- await this.#synchronizedBlock.set(header.toBuffer());
465
- }
466
-
467
- async getBlockNumber(): Promise<number | undefined> {
468
- const headerBuffer = await this.#synchronizedBlock.getAsync();
469
- if (!headerBuffer) {
470
- return undefined;
471
- }
472
-
473
- return Number(BlockHeader.fromBuffer(headerBuffer).globalVariables.blockNumber.toBigInt());
474
- }
475
-
476
- async getBlockHeader(): Promise<BlockHeader> {
477
- const headerBuffer = await this.#synchronizedBlock.getAsync();
478
- if (!headerBuffer) {
479
- throw new Error(`Header not set`);
480
- }
481
-
482
- return BlockHeader.fromBuffer(headerBuffer);
483
- }
484
-
485
- async #addScope(scope: AztecAddress): Promise<boolean> {
486
- const scopeString = scope.toString();
487
-
488
- if (await this.#scopes.hasAsync(scopeString)) {
489
- return false;
490
- }
491
-
492
- await this.#scopes.set(scopeString, true);
493
- this.#notesByContractAndScope.set(scopeString, this.#db.openMultiMap(`${scopeString}:notes_by_contract`));
494
- this.#notesByStorageSlotAndScope.set(scopeString, this.#db.openMultiMap(`${scopeString}:notes_by_storage_slot`));
495
- this.#notesByTxHashAndScope.set(scopeString, this.#db.openMultiMap(`${scopeString}:notes_by_tx_hash`));
496
- this.#notesByAddressPointAndScope.set(scopeString, this.#db.openMultiMap(`${scopeString}:notes_by_address_point`));
497
-
498
- return true;
499
- }
500
-
501
- addCompleteAddress(completeAddress: CompleteAddress): Promise<boolean> {
502
- return this.db.transactionAsync(async () => {
503
- await this.#addScope(completeAddress.address);
504
-
505
- const addressString = completeAddress.address.toString();
506
- const buffer = completeAddress.toBuffer();
507
- const existing = await this.#completeAddressIndex.getAsync(addressString);
508
- if (existing === undefined) {
509
- const index = await this.#completeAddresses.lengthAsync();
510
- await this.#completeAddresses.push(buffer);
511
- await this.#completeAddressIndex.set(addressString, index);
512
-
513
- return true;
514
- } else {
515
- const existingBuffer = await this.#completeAddresses.atAsync(existing);
516
-
517
- if (existingBuffer && Buffer.from(existingBuffer).equals(buffer)) {
518
- return false;
519
- }
520
-
521
- throw new Error(
522
- `Complete address with aztec address ${addressString} but different public key or partial key already exists in memory database`,
523
- );
524
- }
525
- });
526
- }
527
-
528
- async #getCompleteAddress(address: AztecAddress): Promise<CompleteAddress | undefined> {
529
- const index = await this.#completeAddressIndex.getAsync(address.toString());
530
- if (index === undefined) {
531
- return undefined;
532
- }
533
-
534
- const value = await this.#completeAddresses.atAsync(index);
535
- return value ? await CompleteAddress.fromBuffer(value) : undefined;
536
- }
537
-
538
- getCompleteAddress(account: AztecAddress): Promise<CompleteAddress | undefined> {
539
- return this.#getCompleteAddress(account);
540
- }
541
-
542
- async getCompleteAddresses(): Promise<CompleteAddress[]> {
543
- return await Promise.all(
544
- (await toArray(this.#completeAddresses.valuesAsync())).map(v => CompleteAddress.fromBuffer(v)),
545
- );
546
- }
547
-
548
- async addSenderAddress(address: AztecAddress): Promise<boolean> {
549
- if (await this.#addressBook.hasAsync(address.toString())) {
550
- return false;
551
- }
552
-
553
- await this.#addressBook.set(address.toString(), true);
554
-
555
- return true;
556
- }
557
-
558
- async getSenderAddresses(): Promise<AztecAddress[]> {
559
- return (await toArray(this.#addressBook.keysAsync())).map(AztecAddress.fromString);
560
- }
561
-
562
- async removeSenderAddress(address: AztecAddress): Promise<boolean> {
563
- if (!(await this.#addressBook.hasAsync(address.toString()))) {
564
- return false;
565
- }
566
-
567
- await this.#addressBook.delete(address.toString());
568
-
569
- return true;
570
- }
571
-
572
- async estimateSize(): Promise<number> {
573
- const noteSize = (await this.getNotes({})).reduce((sum, note) => sum + note.getSize(), 0);
574
-
575
- const authWitsSize = (await toArray(this.#authWitnesses.valuesAsync())).reduce(
576
- (sum, value) => sum + value.length * Fr.SIZE_IN_BYTES,
577
- 0,
578
- );
579
- const addressesSize = (await this.#completeAddresses.lengthAsync()) * CompleteAddress.SIZE_IN_BYTES;
580
- const treeRootsSize = Object.keys(MerkleTreeId).length * Fr.SIZE_IN_BYTES;
581
-
582
- return noteSize + treeRootsSize + authWitsSize + addressesSize;
583
- }
584
-
585
- async setTaggingSecretsIndexesAsSender(indexedSecrets: IndexedTaggingSecret[]): Promise<void> {
586
- await this.#setTaggingSecretsIndexes(indexedSecrets, this.#taggingSecretIndexesForSenders);
587
- }
588
-
589
- async setTaggingSecretsIndexesAsRecipient(indexedSecrets: IndexedTaggingSecret[]): Promise<void> {
590
- await this.#setTaggingSecretsIndexes(indexedSecrets, this.#taggingSecretIndexesForRecipients);
591
- }
592
-
593
- async #setTaggingSecretsIndexes(indexedSecrets: IndexedTaggingSecret[], storageMap: AztecAsyncMap<string, number>) {
594
- await Promise.all(
595
- indexedSecrets.map(indexedSecret =>
596
- storageMap.set(indexedSecret.appTaggingSecret.toString(), indexedSecret.index),
597
- ),
598
- );
599
- }
600
-
601
- async getTaggingSecretsIndexesAsRecipient(appTaggingSecrets: Fr[]) {
602
- return await this.#getTaggingSecretsIndexes(appTaggingSecrets, this.#taggingSecretIndexesForRecipients);
603
- }
604
-
605
- async getTaggingSecretsIndexesAsSender(appTaggingSecrets: Fr[]) {
606
- return await this.#getTaggingSecretsIndexes(appTaggingSecrets, this.#taggingSecretIndexesForSenders);
607
- }
608
-
609
- #getTaggingSecretsIndexes(appTaggingSecrets: Fr[], storageMap: AztecAsyncMap<string, number>): Promise<number[]> {
610
- return Promise.all(appTaggingSecrets.map(async secret => (await storageMap.getAsync(`${secret.toString()}`)) ?? 0));
611
- }
612
-
613
- resetNoteSyncData(): Promise<void> {
614
- return this.db.transactionAsync(async () => {
615
- const recipients = await toArray(this.#taggingSecretIndexesForRecipients.keysAsync());
616
- await Promise.all(recipients.map(recipient => this.#taggingSecretIndexesForRecipients.delete(recipient)));
617
- const senders = await toArray(this.#taggingSecretIndexesForSenders.keysAsync());
618
- await Promise.all(senders.map(sender => this.#taggingSecretIndexesForSenders.delete(sender)));
619
- });
620
- }
621
-
622
- async storeCapsule(contractAddress: AztecAddress, slot: Fr, capsule: Fr[]): Promise<void> {
623
- await this.#capsules.set(dbSlotToKey(contractAddress, slot), Buffer.concat(capsule.map(value => value.toBuffer())));
624
- }
625
-
626
- async loadCapsule(contractAddress: AztecAddress, slot: Fr): Promise<Fr[] | null> {
627
- const dataBuffer = await this.#capsules.getAsync(dbSlotToKey(contractAddress, slot));
628
- if (!dataBuffer) {
629
- this.debug(`Data not found for contract ${contractAddress.toString()} and slot ${slot.toString()}`);
630
- return null;
631
- }
632
- const capsule: Fr[] = [];
633
- for (let i = 0; i < dataBuffer.length; i += Fr.SIZE_IN_BYTES) {
634
- capsule.push(Fr.fromBuffer(dataBuffer.subarray(i, i + Fr.SIZE_IN_BYTES)));
635
- }
636
- return capsule;
637
- }
638
-
639
- async deleteCapsule(contractAddress: AztecAddress, slot: Fr): Promise<void> {
640
- await this.#capsules.delete(dbSlotToKey(contractAddress, slot));
641
- }
642
-
643
- async copyCapsule(contractAddress: AztecAddress, srcSlot: Fr, dstSlot: Fr, numEntries: number): Promise<void> {
644
- // In order to support overlapping source and destination regions, we need to check the relative positions of source
645
- // and destination. If destination is ahead of source, then by the time we overwrite source elements using forward
646
- // indexes we'll have already read those. On the contrary, if source is ahead of destination we need to use backward
647
- // indexes to avoid reading elements that've been overwritten.
648
-
649
- const indexes = Array.from(Array(numEntries).keys());
650
- if (srcSlot.lt(dstSlot)) {
651
- indexes.reverse();
652
- }
653
-
654
- for (const i of indexes) {
655
- const currentSrcSlot = dbSlotToKey(contractAddress, srcSlot.add(new Fr(i)));
656
- const currentDstSlot = dbSlotToKey(contractAddress, dstSlot.add(new Fr(i)));
657
-
658
- const toCopy = await this.#capsules.getAsync(currentSrcSlot);
659
- if (!toCopy) {
660
- throw new Error(`Attempted to copy empty slot ${currentSrcSlot} for contract ${contractAddress.toString()}`);
661
- }
662
-
663
- await this.#capsules.set(currentDstSlot, toCopy);
664
- }
665
- }
666
- }
667
-
668
- function dbSlotToKey(contractAddress: AztecAddress, slot: Fr): string {
669
- return `${contractAddress.toString()}:${slot.toString()}`;
670
- }
package/src/index.ts DELETED
@@ -1,9 +0,0 @@
1
- export * from './pxe_service/index.js';
2
- export { pxeTestSuite } from './pxe_service/test/pxe_test_suite.js';
3
- export * from './pxe_http/index.js';
4
- export * from './config/index.js';
5
- export * from './utils/create_pxe_service.js';
6
-
7
- export * from './database/index.js';
8
- export { PXEDataProvider } from './pxe_data_provider/index.js';
9
- export * from './contract_data_provider/index.js';