@aztec/aztec.js 0.0.1-commit.8afd444 → 0.0.1-commit.8f9871590

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 (46) hide show
  1. package/dest/api/contract.d.ts +2 -2
  2. package/dest/api/contract.js +2 -2
  3. package/dest/api/deployment.d.ts +1 -2
  4. package/dest/api/deployment.d.ts.map +1 -1
  5. package/dest/api/deployment.js +0 -1
  6. package/dest/api/events.d.ts +10 -6
  7. package/dest/api/events.d.ts.map +1 -1
  8. package/dest/api/events.js +30 -20
  9. package/dest/api/fields.d.ts +2 -1
  10. package/dest/api/fields.d.ts.map +1 -1
  11. package/dest/api/fields.js +1 -0
  12. package/dest/api/wallet.d.ts +2 -2
  13. package/dest/api/wallet.d.ts.map +1 -1
  14. package/dest/api/wallet.js +1 -1
  15. package/dest/contract/batch_call.js +4 -1
  16. package/dest/contract/contract_function_interaction.d.ts +3 -12
  17. package/dest/contract/contract_function_interaction.d.ts.map +1 -1
  18. package/dest/contract/contract_function_interaction.js +10 -7
  19. package/dest/contract/protocol_contracts/contract-class-registry.d.ts +2 -13
  20. package/dest/contract/protocol_contracts/contract-class-registry.d.ts.map +1 -1
  21. package/dest/contract/protocol_contracts/contract-class-registry.js +12 -258
  22. package/dest/fee/fee_juice_payment_method_with_claim.js +6 -6
  23. package/dest/fee/private_fee_payment_method.js +10 -10
  24. package/dest/fee/public_fee_payment_method.js +10 -10
  25. package/dest/fee/sponsored_fee_payment.js +3 -3
  26. package/dest/wallet/wallet.d.ts +146 -76
  27. package/dest/wallet/wallet.d.ts.map +1 -1
  28. package/dest/wallet/wallet.js +25 -21
  29. package/package.json +19 -10
  30. package/src/api/contract.ts +2 -2
  31. package/src/api/deployment.ts +0 -1
  32. package/src/api/events.ts +35 -27
  33. package/src/api/fields.ts +1 -0
  34. package/src/api/wallet.ts +5 -1
  35. package/src/contract/batch_call.ts +1 -1
  36. package/src/contract/contract_function_interaction.ts +17 -7
  37. package/src/contract/protocol_contracts/contract-class-registry.ts +3 -145
  38. package/src/fee/fee_juice_payment_method_with_claim.ts +5 -5
  39. package/src/fee/private_fee_payment_method.ts +7 -7
  40. package/src/fee/public_fee_payment_method.ts +8 -8
  41. package/src/fee/sponsored_fee_payment.ts +3 -3
  42. package/src/wallet/wallet.ts +82 -35
  43. package/dest/deployment/broadcast_function.d.ts +0 -24
  44. package/dest/deployment/broadcast_function.d.ts.map +0 -1
  45. package/dest/deployment/broadcast_function.js +0 -74
  46. package/src/deployment/broadcast_function.ts +0 -148
@@ -7,8 +7,7 @@ import {
7
7
  type ContractArtifact,
8
8
  ContractArtifactSchema,
9
9
  type EventMetadataDefinition,
10
- type FunctionCall,
11
- FunctionType,
10
+ FunctionCall,
12
11
  } from '@aztec/stdlib/abi';
13
12
  import { AuthWitness } from '@aztec/stdlib/auth-witness';
14
13
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
@@ -140,37 +139,66 @@ export type BatchResults<T extends readonly BatchedMethod[]> = {
140
139
  };
141
140
 
142
141
  /**
143
- * Filter options when querying private events.
142
+ * Base filter options for event queries.
144
143
  */
145
- export type PrivateEventFilter = {
146
- /** The address of the contract that emitted the events. */
147
- contractAddress: AztecAddress;
148
- /** Addresses of accounts that are in scope for this filter. */
149
- scopes: AztecAddress[];
144
+ export type EventFilterBase = {
150
145
  /** Transaction in which the events were emitted. */
151
146
  txHash?: TxHash;
152
147
  /** The block number from which to start fetching events (inclusive).
153
148
  * Optional. If provided, it must be greater or equal than 1.
154
149
  * Defaults to the initial L2 block number (INITIAL_L2_BLOCK_NUM).
155
- * */
150
+ */
156
151
  fromBlock?: BlockNumber;
157
152
  /** The block number until which to fetch logs (not inclusive).
158
153
  * Optional. If provided, it must be greater than fromBlock.
159
- * Defaults to the latest known block to PXE + 1.
160
154
  */
161
155
  toBlock?: BlockNumber;
162
156
  };
163
157
 
164
158
  /**
165
- * An ABI decoded private event with associated metadata.
159
+ * Filter options when querying private events.
166
160
  */
167
- export type PrivateEvent<T> = {
161
+ export type PrivateEventFilter = EventFilterBase & {
162
+ /** The address of the contract that emitted the events. */
163
+ contractAddress: AztecAddress;
164
+ /** Addresses of accounts that are in scope for this filter. */
165
+ scopes: AztecAddress[];
166
+ };
167
+
168
+ /**
169
+ * Filter options when querying public events.
170
+ */
171
+ export type PublicEventFilter = EventFilterBase & {
172
+ /** The address of the contract that emitted the events. */
173
+ contractAddress?: AztecAddress;
174
+ };
175
+
176
+ /**
177
+ * An ABI decoded event with associated metadata.
178
+ * @typeParam T - The decoded event type
179
+ * @typeParam M - Additional metadata fields (empty by default)
180
+ */
181
+ export type Event<T, M extends object = object> = {
168
182
  /** The ABI decoded event */
169
183
  event: T;
170
184
  /** Metadata describing event context information such as tx and block */
171
- metadata: InTx;
185
+ metadata: InTx & M;
172
186
  };
173
187
 
188
+ /** An ABI decoded private event with associated metadata. */
189
+ export type PrivateEvent<T> = Event<T>;
190
+
191
+ /** An ABI decoded public event with associated metadata (includes contract address). */
192
+ export type PublicEvent<T> = Event<
193
+ T,
194
+ {
195
+ /**
196
+ * Address of the contract that emitted this event
197
+ */
198
+ contractAddress: AztecAddress;
199
+ }
200
+ >;
201
+
174
202
  /**
175
203
  * Contract metadata including deployment and registration status.
176
204
  */
@@ -197,6 +225,16 @@ export type ContractClassMetadata = {
197
225
  isContractClassPubliclyRegistered: boolean;
198
226
  };
199
227
 
228
+ /**
229
+ * Options for simulating a utility function call.
230
+ */
231
+ export type SimulateUtilityOptions = {
232
+ /** The scope for the utility simulation (determines which notes and keys are visible). */
233
+ scope: AztecAddress;
234
+ /** Optional auth witnesses to use during execution. */
235
+ authWitnesses?: AuthWitness[];
236
+ };
237
+
200
238
  /**
201
239
  * The wallet interface.
202
240
  */
@@ -217,7 +255,7 @@ export type Wallet = {
217
255
  secretKey?: Fr,
218
256
  ): Promise<ContractInstanceWithAddress>;
219
257
  simulateTx(exec: ExecutionPayload, opts: SimulateOptions): Promise<TxSimulationResult>;
220
- simulateUtility(call: FunctionCall, authwits?: AuthWitness[]): Promise<UtilitySimulationResult>;
258
+ simulateUtility(call: FunctionCall, opts: SimulateUtilityOptions): Promise<UtilitySimulationResult>;
221
259
  profileTx(exec: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult>;
222
260
  sendTx<W extends InteractionWaitOptions = undefined>(
223
261
  exec: ExecutionPayload,
@@ -228,19 +266,8 @@ export type Wallet = {
228
266
  batch<const T extends readonly BatchedMethod[]>(methods: T): Promise<BatchResults<T>>;
229
267
  };
230
268
 
231
- export const FunctionCallSchema = z.object({
232
- name: z.string(),
233
- to: schemas.AztecAddress,
234
- selector: schemas.FunctionSelector,
235
- type: z.nativeEnum(FunctionType),
236
- isStatic: z.boolean(),
237
- hideMsgSender: z.boolean(),
238
- args: z.array(schemas.Fr),
239
- returnTypes: z.array(AbiTypeSchema),
240
- });
241
-
242
269
  export const ExecutionPayloadSchema = z.object({
243
- calls: z.array(FunctionCallSchema),
270
+ calls: z.array(FunctionCall.schema),
244
271
  authWitnesses: z.array(AuthWitness.schema),
245
272
  capsules: z.array(Capsule.schema),
246
273
  extraHashedArgs: z.array(HashedValues.schema),
@@ -297,7 +324,7 @@ export const MessageHashOrIntentSchema = z.union([
297
324
  z.object({ consumer: schemas.AztecAddress, innerHash: schemas.Fr }),
298
325
  z.object({
299
326
  caller: schemas.AztecAddress,
300
- call: FunctionCallSchema,
327
+ call: FunctionCall.schema,
301
328
  }),
302
329
  ]);
303
330
 
@@ -307,6 +334,21 @@ export const EventMetadataDefinitionSchema = z.object({
307
334
  fieldNames: z.array(z.string()),
308
335
  });
309
336
 
337
+ const EventFilterBaseSchema = z.object({
338
+ txHash: optional(TxHash.schema),
339
+ fromBlock: optional(BlockNumberPositiveSchema),
340
+ toBlock: optional(BlockNumberPositiveSchema),
341
+ });
342
+
343
+ export const PrivateEventFilterSchema = EventFilterBaseSchema.extend({
344
+ contractAddress: schemas.AztecAddress,
345
+ scopes: z.array(schemas.AztecAddress),
346
+ });
347
+
348
+ export const PublicEventFilterSchema = EventFilterBaseSchema.extend({
349
+ contractAddress: optional(schemas.AztecAddress),
350
+ });
351
+
310
352
  export const PrivateEventSchema: z.ZodType<any> = zodFor<PrivateEvent<AbiDecoded>>()(
311
353
  z.object({
312
354
  event: AbiDecodedSchema,
@@ -314,13 +356,12 @@ export const PrivateEventSchema: z.ZodType<any> = zodFor<PrivateEvent<AbiDecoded
314
356
  }),
315
357
  );
316
358
 
317
- export const PrivateEventFilterSchema = z.object({
318
- contractAddress: schemas.AztecAddress,
319
- scopes: z.array(schemas.AztecAddress),
320
- txHash: optional(TxHash.schema),
321
- fromBlock: optional(BlockNumberPositiveSchema),
322
- toBlock: optional(BlockNumberPositiveSchema),
323
- });
359
+ export const PublicEventSchema = zodFor<PublicEvent<AbiDecoded>>()(
360
+ z.object({
361
+ event: AbiDecodedSchema,
362
+ metadata: z.intersection(inTxSchema(), z.object({ contractAddress: schemas.AztecAddress })),
363
+ }),
364
+ );
324
365
 
325
366
  export const ContractMetadataSchema = z.object({
326
367
  instance: optional(ContractInstanceWithAddressSchema),
@@ -479,7 +520,13 @@ const WalletMethodSchemas = {
479
520
  simulateTx: z.function().args(ExecutionPayloadSchema, SimulateOptionsSchema).returns(TxSimulationResult.schema),
480
521
  simulateUtility: z
481
522
  .function()
482
- .args(FunctionCallSchema, optional(z.array(AuthWitness.schema)))
523
+ .args(
524
+ FunctionCall.schema,
525
+ z.object({
526
+ scope: schemas.AztecAddress,
527
+ authWitnesses: optional(z.array(AuthWitness.schema)),
528
+ }),
529
+ )
483
530
  .returns(UtilitySimulationResult.schema),
484
531
  profileTx: z.function().args(ExecutionPayloadSchema, ProfileOptionsSchema).returns(TxProfileResult.schema),
485
532
  sendTx: z
@@ -1,24 +0,0 @@
1
- import { type ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
2
- import type { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
3
- import type { Wallet } from '../wallet/index.js';
4
- /**
5
- * Sets up a call to broadcast a private function's bytecode via the ClassRegistry contract.
6
- * Note that this is not required for users to call the function, but is rather a convenience to make
7
- * this code publicly available so dapps or wallets do not need to redistribute it.
8
- * @param wallet - Wallet to send the transaction.
9
- * @param artifact - Contract artifact that contains the function to be broadcast.
10
- * @param selector - Selector of the function to be broadcast.
11
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
12
- */
13
- export declare function broadcastPrivateFunction(wallet: Wallet, artifact: ContractArtifact, selector: FunctionSelector): Promise<ContractFunctionInteraction>;
14
- /**
15
- * Sets up a call to broadcast a utility function's bytecode via the ClassRegistry contract.
16
- * Note that this is not required for users to call the function, but is rather a convenience to make
17
- * this code publicly available so dapps or wallets do not need to redistribute it.
18
- * @param wallet - Wallet to send the transaction.
19
- * @param artifact - Contract artifact that contains the function to be broadcast.
20
- * @param selector - Selector of the function to be broadcast.
21
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
22
- */
23
- export declare function broadcastUtilityFunction(wallet: Wallet, artifact: ContractArtifact, selector: FunctionSelector): Promise<ContractFunctionInteraction>;
24
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnJvYWRjYXN0X2Z1bmN0aW9uLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGVwbG95bWVudC9icm9hZGNhc3RfZnVuY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUUEsT0FBTyxFQUFFLEtBQUssZ0JBQWdCLEVBQUUsZ0JBQWdCLEVBQWdDLE1BQU0sbUJBQW1CLENBQUM7QUFTMUcsT0FBTyxLQUFLLEVBQUUsMkJBQTJCLEVBQUUsTUFBTSw4Q0FBOEMsQ0FBQztBQUVoRyxPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSxvQkFBb0IsQ0FBQztBQUVqRDs7Ozs7Ozs7R0FRRztBQUNILHdCQUFzQix3QkFBd0IsQ0FDNUMsTUFBTSxFQUFFLE1BQU0sRUFDZCxRQUFRLEVBQUUsZ0JBQWdCLEVBQzFCLFFBQVEsRUFBRSxnQkFBZ0IsR0FDekIsT0FBTyxDQUFDLDJCQUEyQixDQUFDLENBb0R0QztBQUVEOzs7Ozs7OztHQVFHO0FBQ0gsd0JBQXNCLHdCQUF3QixDQUM1QyxNQUFNLEVBQUUsTUFBTSxFQUNkLFFBQVEsRUFBRSxnQkFBZ0IsRUFDMUIsUUFBUSxFQUFFLGdCQUFnQixHQUN6QixPQUFPLENBQUMsMkJBQTJCLENBQUMsQ0E4Q3RDIn0=
@@ -1 +0,0 @@
1
- {"version":3,"file":"broadcast_function.d.ts","sourceRoot":"","sources":["../../src/deployment/broadcast_function.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,gBAAgB,EAAE,gBAAgB,EAAgC,MAAM,mBAAmB,CAAC;AAS1G,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,8CAA8C,CAAC;AAEhG,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;;;;;;;GAQG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,2BAA2B,CAAC,CAoDtC;AAED;;;;;;;;GAQG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,2BAA2B,CAAC,CA8CtC"}
@@ -1,74 +0,0 @@
1
- import { ARTIFACT_FUNCTION_TREE_MAX_HEIGHT, CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT, MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS } from '@aztec/constants';
2
- import { padArrayEnd } from '@aztec/foundation/collection';
3
- import { Fr } from '@aztec/foundation/curves/bn254';
4
- import { ProtocolContractAddress } from '@aztec/protocol-contracts';
5
- import { FunctionSelector, FunctionType, bufferAsFields } from '@aztec/stdlib/abi';
6
- import { computeVerificationKeyHash, createPrivateFunctionMembershipProof, createUtilityFunctionMembershipProof, getContractClassFromArtifact } from '@aztec/stdlib/contract';
7
- import { Capsule } from '@aztec/stdlib/tx';
8
- import { ContractClassRegistryContract } from '../contract/protocol_contracts/contract-class-registry.js';
9
- /**
10
- * Sets up a call to broadcast a private function's bytecode via the ClassRegistry contract.
11
- * Note that this is not required for users to call the function, but is rather a convenience to make
12
- * this code publicly available so dapps or wallets do not need to redistribute it.
13
- * @param wallet - Wallet to send the transaction.
14
- * @param artifact - Contract artifact that contains the function to be broadcast.
15
- * @param selector - Selector of the function to be broadcast.
16
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
17
- */ export async function broadcastPrivateFunction(wallet, artifact, selector) {
18
- const contractClass = await getContractClassFromArtifact(artifact);
19
- const privateFunctions = artifact.functions.filter((fn)=>fn.functionType === FunctionType.PRIVATE);
20
- const functionsAndSelectors = await Promise.all(privateFunctions.map(async (fn)=>({
21
- f: fn,
22
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)
23
- })));
24
- const privateFunctionArtifact = functionsAndSelectors.find((fn)=>selector.equals(fn.selector))?.f;
25
- if (!privateFunctionArtifact) {
26
- throw new Error(`Private function with selector ${selector.toString()} not found`);
27
- }
28
- const { artifactTreeSiblingPath, artifactTreeLeafIndex, artifactMetadataHash, functionMetadataHash, utilityFunctionsTreeRoot, privateFunctionTreeSiblingPath, privateFunctionTreeLeafIndex } = await createPrivateFunctionMembershipProof(selector, artifact);
29
- const vkHash = await computeVerificationKeyHash(privateFunctionArtifact);
30
- const classRegistry = ContractClassRegistryContract.at(wallet);
31
- const bytecode = bufferAsFields(privateFunctionArtifact.bytecode, MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS);
32
- return classRegistry.methods.broadcast_private_function(contractClass.id, artifactMetadataHash, utilityFunctionsTreeRoot, privateFunctionTreeSiblingPath, privateFunctionTreeLeafIndex, padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT), artifactTreeLeafIndex, // eslint-disable-next-line camelcase
33
- {
34
- selector,
35
- metadata_hash: functionMetadataHash,
36
- vk_hash: vkHash
37
- }).with({
38
- capsules: [
39
- new Capsule(ProtocolContractAddress.ContractClassRegistry, new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT), bytecode)
40
- ]
41
- });
42
- }
43
- /**
44
- * Sets up a call to broadcast a utility function's bytecode via the ClassRegistry contract.
45
- * Note that this is not required for users to call the function, but is rather a convenience to make
46
- * this code publicly available so dapps or wallets do not need to redistribute it.
47
- * @param wallet - Wallet to send the transaction.
48
- * @param artifact - Contract artifact that contains the function to be broadcast.
49
- * @param selector - Selector of the function to be broadcast.
50
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
51
- */ export async function broadcastUtilityFunction(wallet, artifact, selector) {
52
- const contractClass = await getContractClassFromArtifact(artifact);
53
- const utilityFunctions = artifact.functions.filter((fn)=>fn.functionType === FunctionType.UTILITY);
54
- const utilityFunctionsAndSelectors = await Promise.all(utilityFunctions.map(async (fn)=>({
55
- f: fn,
56
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)
57
- })));
58
- const utilityFunctionArtifact = utilityFunctionsAndSelectors.find((fn)=>selector.equals(fn.selector))?.f;
59
- if (!utilityFunctionArtifact) {
60
- throw new Error(`Utility function with selector ${selector.toString()} not found`);
61
- }
62
- const { artifactMetadataHash, artifactTreeLeafIndex, artifactTreeSiblingPath, functionMetadataHash, privateFunctionsArtifactTreeRoot } = await createUtilityFunctionMembershipProof(selector, artifact);
63
- const classRegistry = ContractClassRegistryContract.at(wallet);
64
- const bytecode = bufferAsFields(utilityFunctionArtifact.bytecode, MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS);
65
- return classRegistry.methods.broadcast_utility_function(contractClass.id, artifactMetadataHash, privateFunctionsArtifactTreeRoot, padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT), artifactTreeLeafIndex, // eslint-disable-next-line camelcase
66
- {
67
- selector,
68
- metadata_hash: functionMetadataHash
69
- }).with({
70
- capsules: [
71
- new Capsule(ProtocolContractAddress.ContractClassRegistry, new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT), bytecode)
72
- ]
73
- });
74
- }
@@ -1,148 +0,0 @@
1
- import {
2
- ARTIFACT_FUNCTION_TREE_MAX_HEIGHT,
3
- CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT,
4
- MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS,
5
- } from '@aztec/constants';
6
- import { padArrayEnd } from '@aztec/foundation/collection';
7
- import { Fr } from '@aztec/foundation/curves/bn254';
8
- import { ProtocolContractAddress } from '@aztec/protocol-contracts';
9
- import { type ContractArtifact, FunctionSelector, FunctionType, bufferAsFields } from '@aztec/stdlib/abi';
10
- import {
11
- computeVerificationKeyHash,
12
- createPrivateFunctionMembershipProof,
13
- createUtilityFunctionMembershipProof,
14
- getContractClassFromArtifact,
15
- } from '@aztec/stdlib/contract';
16
- import { Capsule } from '@aztec/stdlib/tx';
17
-
18
- import type { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
19
- import { ContractClassRegistryContract } from '../contract/protocol_contracts/contract-class-registry.js';
20
- import type { Wallet } from '../wallet/index.js';
21
-
22
- /**
23
- * Sets up a call to broadcast a private function's bytecode via the ClassRegistry contract.
24
- * Note that this is not required for users to call the function, but is rather a convenience to make
25
- * this code publicly available so dapps or wallets do not need to redistribute it.
26
- * @param wallet - Wallet to send the transaction.
27
- * @param artifact - Contract artifact that contains the function to be broadcast.
28
- * @param selector - Selector of the function to be broadcast.
29
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
30
- */
31
- export async function broadcastPrivateFunction(
32
- wallet: Wallet,
33
- artifact: ContractArtifact,
34
- selector: FunctionSelector,
35
- ): Promise<ContractFunctionInteraction> {
36
- const contractClass = await getContractClassFromArtifact(artifact);
37
- const privateFunctions = artifact.functions.filter(fn => fn.functionType === FunctionType.PRIVATE);
38
- const functionsAndSelectors = await Promise.all(
39
- privateFunctions.map(async fn => ({
40
- f: fn,
41
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters),
42
- })),
43
- );
44
- const privateFunctionArtifact = functionsAndSelectors.find(fn => selector.equals(fn.selector))?.f;
45
- if (!privateFunctionArtifact) {
46
- throw new Error(`Private function with selector ${selector.toString()} not found`);
47
- }
48
-
49
- const {
50
- artifactTreeSiblingPath,
51
- artifactTreeLeafIndex,
52
- artifactMetadataHash,
53
- functionMetadataHash,
54
- utilityFunctionsTreeRoot,
55
- privateFunctionTreeSiblingPath,
56
- privateFunctionTreeLeafIndex,
57
- } = await createPrivateFunctionMembershipProof(selector, artifact);
58
-
59
- const vkHash = await computeVerificationKeyHash(privateFunctionArtifact);
60
-
61
- const classRegistry = ContractClassRegistryContract.at(wallet);
62
- const bytecode = bufferAsFields(
63
- privateFunctionArtifact.bytecode,
64
- MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS,
65
- );
66
- return classRegistry.methods
67
- .broadcast_private_function(
68
- contractClass.id,
69
- artifactMetadataHash,
70
- utilityFunctionsTreeRoot,
71
- privateFunctionTreeSiblingPath,
72
- privateFunctionTreeLeafIndex,
73
- padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT),
74
- artifactTreeLeafIndex,
75
- // eslint-disable-next-line camelcase
76
- { selector, metadata_hash: functionMetadataHash, vk_hash: vkHash },
77
- )
78
- .with({
79
- capsules: [
80
- new Capsule(
81
- ProtocolContractAddress.ContractClassRegistry,
82
- new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT),
83
- bytecode,
84
- ),
85
- ],
86
- });
87
- }
88
-
89
- /**
90
- * Sets up a call to broadcast a utility function's bytecode via the ClassRegistry contract.
91
- * Note that this is not required for users to call the function, but is rather a convenience to make
92
- * this code publicly available so dapps or wallets do not need to redistribute it.
93
- * @param wallet - Wallet to send the transaction.
94
- * @param artifact - Contract artifact that contains the function to be broadcast.
95
- * @param selector - Selector of the function to be broadcast.
96
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
97
- */
98
- export async function broadcastUtilityFunction(
99
- wallet: Wallet,
100
- artifact: ContractArtifact,
101
- selector: FunctionSelector,
102
- ): Promise<ContractFunctionInteraction> {
103
- const contractClass = await getContractClassFromArtifact(artifact);
104
- const utilityFunctions = artifact.functions.filter(fn => fn.functionType === FunctionType.UTILITY);
105
- const utilityFunctionsAndSelectors = await Promise.all(
106
- utilityFunctions.map(async fn => ({
107
- f: fn,
108
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters),
109
- })),
110
- );
111
- const utilityFunctionArtifact = utilityFunctionsAndSelectors.find(fn => selector.equals(fn.selector))?.f;
112
- if (!utilityFunctionArtifact) {
113
- throw new Error(`Utility function with selector ${selector.toString()} not found`);
114
- }
115
-
116
- const {
117
- artifactMetadataHash,
118
- artifactTreeLeafIndex,
119
- artifactTreeSiblingPath,
120
- functionMetadataHash,
121
- privateFunctionsArtifactTreeRoot,
122
- } = await createUtilityFunctionMembershipProof(selector, artifact);
123
-
124
- const classRegistry = ContractClassRegistryContract.at(wallet);
125
- const bytecode = bufferAsFields(
126
- utilityFunctionArtifact.bytecode,
127
- MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS,
128
- );
129
- return classRegistry.methods
130
- .broadcast_utility_function(
131
- contractClass.id,
132
- artifactMetadataHash,
133
- privateFunctionsArtifactTreeRoot,
134
- padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT),
135
- artifactTreeLeafIndex,
136
- // eslint-disable-next-line camelcase
137
- { selector, metadata_hash: functionMetadataHash },
138
- )
139
- .with({
140
- capsules: [
141
- new Capsule(
142
- ProtocolContractAddress.ContractClassRegistry,
143
- new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT),
144
- bytecode,
145
- ),
146
- ],
147
- });
148
- }