@aztec/entrypoints 0.0.0-test.1 → 0.0.1-fake-ceab37513c

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.
@@ -0,0 +1,234 @@
1
+ import { GeneratorIndex } from '@aztec/constants';
2
+ import { padArrayEnd } from '@aztec/foundation/collection';
3
+ import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto';
4
+ import { Fr } from '@aztec/foundation/fields';
5
+ import type { Tuple } from '@aztec/foundation/serialize';
6
+ import { FunctionCall, FunctionType } from '@aztec/stdlib/abi';
7
+ import { HashedValues } from '@aztec/stdlib/tx';
8
+
9
+ // These must match the values defined in:
10
+ // - noir-projects/aztec-nr/aztec/src/entrypoint/app.nr
11
+ const APP_MAX_CALLS = 4;
12
+ // - and noir-projects/aztec-nr/aztec/src/entrypoint/fee.nr
13
+ const FEE_MAX_CALLS = 2;
14
+
15
+ /** Encoded function call for an Aztec entrypoint */
16
+ export type EncodedFunctionCall = {
17
+ /** Arguments hash for the call. */
18
+ /** This should be the calldata hash `hash([function_selector, ...args])` if `is_public` is true. */
19
+ args_hash: Fr;
20
+ /** Selector of the function to call */
21
+ function_selector: Fr;
22
+ /** Address of the contract to call */
23
+ target_address: Fr;
24
+ /** Whether the function is public or private */
25
+ is_public: boolean;
26
+ /** Whether the function can alter state */
27
+ is_static: boolean;
28
+ };
29
+
30
+ /** Type that represents function calls ready to be sent to a circuit for execution */
31
+ export type EncodedCalls = {
32
+ /** Function calls in the expected format (Noir's convention) */
33
+ encodedFunctionCalls: EncodedFunctionCall[];
34
+ /** The hashed args for the call, ready to be injected in the execution cache */
35
+ hashedArguments: HashedValues[];
36
+ };
37
+
38
+ /**
39
+ * Entrypoints derive their arguments from the calls that they'll ultimate make.
40
+ * This utility class helps in creating the payload for the entrypoint by taking into
41
+ * account how the calls are encoded and hashed.
42
+ * */
43
+ export abstract class EncodedCallsForEntrypoint implements EncodedCalls {
44
+ constructor(
45
+ /** Function calls in the expected format (Noir's convention) */
46
+ public encodedFunctionCalls: EncodedFunctionCall[],
47
+ /** The hashed args for the call, ready to be injected in the execution cache */
48
+ public hashedArguments: HashedValues[],
49
+ /** The index of the generator to use for hashing */
50
+ public generatorIndex: number,
51
+ /**
52
+ * A nonce to inject into the payload of the transaction. When used with cancellable=true, this nonce will be
53
+ * used to compute a nullifier that allows cancelling this transaction by submitting a new one with the same nonce
54
+ * but higher fee. The nullifier ensures only one transaction can succeed.
55
+ */
56
+ // eslint-disable-next-line camelcase
57
+ public tx_nonce: Fr,
58
+ ) {}
59
+
60
+ /* eslint-disable camelcase */
61
+ /**
62
+ * The function calls to execute. This uses snake_case naming so that it is compatible with Noir encoding
63
+ * @internal
64
+ */
65
+ get function_calls() {
66
+ return this.encodedFunctionCalls;
67
+ }
68
+ /* eslint-enable camelcase */
69
+
70
+ /**
71
+ * Serializes the payload to an array of fields
72
+ * @returns The fields of the payload
73
+ */
74
+ abstract toFields(): Fr[];
75
+
76
+ /**
77
+ * Hashes the payload
78
+ * @returns The hash of the payload
79
+ */
80
+ hash() {
81
+ return poseidon2HashWithSeparator(this.toFields(), this.generatorIndex);
82
+ }
83
+
84
+ /** Serializes the function calls to an array of fields. */
85
+ protected functionCallsToFields() {
86
+ return this.encodedFunctionCalls.flatMap(call => [
87
+ call.args_hash,
88
+ call.function_selector,
89
+ call.target_address,
90
+ new Fr(call.is_public),
91
+ new Fr(call.is_static),
92
+ ]);
93
+ }
94
+
95
+ /**
96
+ * Encodes a set of function calls for a dapp entrypoint
97
+ * @param functionCalls - The function calls to execute
98
+ * @returns The encoded calls
99
+ */
100
+ static async fromFunctionCalls(functionCalls: FunctionCall[]) {
101
+ const encoded = await encode(functionCalls);
102
+ return new EncodedAppEntrypointCalls(encoded.encodedFunctionCalls, encoded.hashedArguments, 0, Fr.random());
103
+ }
104
+
105
+ /**
106
+ * Encodes the functions for the app-portion of a transaction from a set of function calls and a nonce
107
+ * @param functionCalls - The function calls to execute
108
+ * @param txNonce - A nonce used to enable transaction cancellation when cancellable=true. Transactions with the same
109
+ * nonce can be replaced by submitting a new one with a higher fee.
110
+ * @returns The encoded calls
111
+ */
112
+ static async fromAppExecution(
113
+ functionCalls: FunctionCall[] | Tuple<FunctionCall, typeof APP_MAX_CALLS>,
114
+ txNonce = Fr.random(),
115
+ ) {
116
+ if (functionCalls.length > APP_MAX_CALLS) {
117
+ throw new Error(`Expected at most ${APP_MAX_CALLS} function calls, got ${functionCalls.length}`);
118
+ }
119
+ const paddedCalls = padArrayEnd(functionCalls, FunctionCall.empty(), APP_MAX_CALLS);
120
+ const encoded = await encode(paddedCalls);
121
+ return new EncodedAppEntrypointCalls(
122
+ encoded.encodedFunctionCalls,
123
+ encoded.hashedArguments,
124
+ GeneratorIndex.SIGNATURE_PAYLOAD,
125
+ txNonce,
126
+ );
127
+ }
128
+
129
+ /**
130
+ * Creates an encoded set of functions to pay the fee for a transaction
131
+ * @param functionCalls - The calls generated by the payment method
132
+ * @param isFeePayer - Whether the sender should be appointed as fee payer
133
+ * @returns The encoded calls
134
+ */
135
+ static async fromFeeCalls(
136
+ functionCalls: FunctionCall[] | Tuple<FunctionCall, typeof FEE_MAX_CALLS>,
137
+ isFeePayer: boolean,
138
+ ) {
139
+ const paddedCalls = padArrayEnd(functionCalls, FunctionCall.empty(), FEE_MAX_CALLS);
140
+ const encoded = await encode(paddedCalls);
141
+ return new EncodedFeeEntrypointCalls(
142
+ encoded.encodedFunctionCalls,
143
+ encoded.hashedArguments,
144
+ GeneratorIndex.FEE_PAYLOAD,
145
+ Fr.random(),
146
+ isFeePayer,
147
+ );
148
+ }
149
+ }
150
+
151
+ /** Encoded calls for app phase execution. */
152
+ export class EncodedAppEntrypointCalls extends EncodedCallsForEntrypoint {
153
+ constructor(
154
+ encodedFunctionCalls: EncodedFunctionCall[],
155
+ hashedArguments: HashedValues[],
156
+ generatorIndex: number,
157
+ txNonce: Fr,
158
+ ) {
159
+ super(encodedFunctionCalls, hashedArguments, generatorIndex, txNonce);
160
+ }
161
+
162
+ override toFields(): Fr[] {
163
+ return [...this.functionCallsToFields(), this.tx_nonce];
164
+ }
165
+ }
166
+
167
+ /** Encoded calls for fee payment */
168
+ export class EncodedFeeEntrypointCalls extends EncodedCallsForEntrypoint {
169
+ #isFeePayer: boolean;
170
+
171
+ constructor(
172
+ encodedFunctionCalls: EncodedFunctionCall[],
173
+ hashedArguments: HashedValues[],
174
+ generatorIndex: number,
175
+ txNonce: Fr,
176
+ isFeePayer: boolean,
177
+ ) {
178
+ super(encodedFunctionCalls, hashedArguments, generatorIndex, txNonce);
179
+ this.#isFeePayer = isFeePayer;
180
+ }
181
+
182
+ override toFields(): Fr[] {
183
+ return [...this.functionCallsToFields(), this.tx_nonce, new Fr(this.#isFeePayer)];
184
+ }
185
+
186
+ /* eslint-disable camelcase */
187
+ /** Whether the sender should be appointed as fee payer. */
188
+ get is_fee_payer() {
189
+ return this.#isFeePayer;
190
+ }
191
+ /* eslint-enable camelcase */
192
+ }
193
+
194
+ /**
195
+ * Computes a hash of a combined set of app and fee calls.
196
+ * @param appCalls - A set of app calls.
197
+ * @param feeCalls - A set of calls used to pay fees.
198
+ * @returns A hash of a combined call set.
199
+ */
200
+ export async function computeCombinedPayloadHash(
201
+ appPayload: EncodedAppEntrypointCalls,
202
+ feePayload: EncodedFeeEntrypointCalls,
203
+ ): Promise<Fr> {
204
+ return poseidon2HashWithSeparator(
205
+ [await appPayload.hash(), await feePayload.hash()],
206
+ GeneratorIndex.COMBINED_PAYLOAD,
207
+ );
208
+ }
209
+
210
+ /** Encodes FunctionCalls for execution, following Noir's convention */
211
+ export async function encode(calls: FunctionCall[]): Promise<EncodedCalls> {
212
+ const hashedArguments: HashedValues[] = [];
213
+ for (const call of calls) {
214
+ const hashed =
215
+ call.type === FunctionType.PUBLIC
216
+ ? await HashedValues.fromCalldata([call.selector.toField(), ...call.args])
217
+ : await HashedValues.fromArgs(call.args);
218
+ hashedArguments.push(hashed);
219
+ }
220
+
221
+ /* eslint-disable camelcase */
222
+ const encodedFunctionCalls: EncodedFunctionCall[] = calls.map((call, index) => ({
223
+ args_hash: hashedArguments[index].hash,
224
+ function_selector: call.selector.toField(),
225
+ target_address: call.to.toField(),
226
+ is_public: call.type == FunctionType.PUBLIC,
227
+ is_static: call.isStatic,
228
+ }));
229
+
230
+ return {
231
+ encodedFunctionCalls,
232
+ hashedArguments,
233
+ };
234
+ }
package/src/index.ts CHANGED
@@ -7,4 +7,7 @@
7
7
  */
8
8
 
9
9
  export * from './account_entrypoint.js';
10
- export * from './dapp_entrypoint.js';
10
+ export * from './interfaces.js';
11
+ export * from './default_entrypoint.js';
12
+ export * from './encoding.js';
13
+ export * from './default_multi_call_entrypoint.js';
@@ -0,0 +1,97 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+ import type { FieldsOf } from '@aztec/foundation/types';
3
+ import type { AuthWitness } from '@aztec/stdlib/auth-witness';
4
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
+ import type { GasSettings } from '@aztec/stdlib/gas';
6
+ import type { TxExecutionRequest } from '@aztec/stdlib/tx';
7
+
8
+ import type { ExecutionPayload } from './payload.js';
9
+
10
+ /**
11
+ * General options for the tx execution.
12
+ */
13
+ export type TxExecutionOptions = {
14
+ /** Whether the transaction can be cancelled. */
15
+ cancellable?: boolean;
16
+ /**
17
+ * A nonce to inject into the app payload of the transaction. When used with cancellable=true, this nonce will be
18
+ * used to compute a nullifier that allows cancelling this transaction by submitting a new one with the same nonce
19
+ * but higher fee. The nullifier ensures only one transaction can succeed.
20
+ */
21
+ txNonce?: Fr;
22
+ };
23
+
24
+ /**
25
+ * Creates transaction execution requests out of a set of function calls, a fee payment method and
26
+ * general options for the transaction
27
+ */
28
+ export interface EntrypointInterface {
29
+ /**
30
+ * Generates an execution request out of set of function calls.
31
+ * @param exec - The execution intents to be run.
32
+ * @param fee - The fee options for the transaction.
33
+ * @param options - Transaction nonce and whether the transaction is cancellable.
34
+ * @returns The authenticated transaction execution request.
35
+ */
36
+ createTxExecutionRequest(
37
+ exec: ExecutionPayload,
38
+ fee: FeeOptions,
39
+ options: TxExecutionOptions,
40
+ ): Promise<TxExecutionRequest>;
41
+ }
42
+
43
+ /** Creates authorization witnesses. */
44
+ export interface AuthWitnessProvider {
45
+ /**
46
+ * Computes an authentication witness from either a message hash
47
+ * @param messageHash - The message hash to approve
48
+ * @returns The authentication witness
49
+ */
50
+ createAuthWit(messageHash: Fr | Buffer): Promise<AuthWitness>;
51
+ }
52
+
53
+ /**
54
+ * Holds information about how the fee for a transaction is to be paid.
55
+ */
56
+ export interface FeePaymentMethod {
57
+ /** The asset used to pay the fee. */
58
+ getAsset(): Promise<AztecAddress>;
59
+ /**
60
+ * Returns the data to be added to the final execution request
61
+ * to pay the fee in the given asset
62
+ * @param gasSettings - The gas limits and max fees.
63
+ * @returns The function calls to pay the fee.
64
+ */
65
+ getExecutionPayload(gasSettings: GasSettings): Promise<ExecutionPayload>;
66
+ /**
67
+ * The expected fee payer for this tx.
68
+ * @param gasSettings - The gas limits and max fees.
69
+ */
70
+ getFeePayer(gasSettings: GasSettings): Promise<AztecAddress>;
71
+ }
72
+
73
+ /**
74
+ * Fee payment options for a transaction.
75
+ */
76
+ export type FeeOptions = {
77
+ /** The fee payment method to use */
78
+ paymentMethod: FeePaymentMethod;
79
+ /** The gas settings */
80
+ gasSettings: GasSettings;
81
+ };
82
+
83
+ // docs:start:user_fee_options
84
+ /** Fee options as set by a user. */
85
+ export type UserFeeOptions = {
86
+ /** The fee payment method to use */
87
+ paymentMethod?: FeePaymentMethod;
88
+ /** The gas settings */
89
+ gasSettings?: Partial<FieldsOf<GasSettings>>;
90
+ /** Percentage to pad the base fee by, if empty, defaults to 0.5 */
91
+ baseFeePadding?: number;
92
+ /** Whether to run an initial simulation of the tx with high gas limit to figure out actual gas settings. */
93
+ estimateGas?: boolean;
94
+ /** Percentage to pad the estimated gas limits by, if empty, defaults to 0.1. Only relevant if estimateGas is set. */
95
+ estimatedGasPadding?: number;
96
+ };
97
+ // docs:end:user_fee_options
package/src/payload.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { FunctionCall } from '@aztec/stdlib/abi';
2
+ import type { AuthWitness } from '@aztec/stdlib/auth-witness';
3
+ import { Capsule, HashedValues } from '@aztec/stdlib/tx';
4
+
5
+ /**
6
+ * Represents data necessary to perform an action in the network successfully.
7
+ * This class can be considered Aztec's "minimal execution unit".
8
+ * */
9
+ export class ExecutionPayload {
10
+ public constructor(
11
+ /** The function calls to be executed. */
12
+ public calls: FunctionCall[],
13
+ /** Any transient auth witnesses needed for this execution */
14
+ public authWitnesses: AuthWitness[],
15
+ /** Data passed through an oracle for this execution. */
16
+ public capsules: Capsule[],
17
+ /** Extra hashed values to be injected in the execution cache */
18
+ public extraHashedArgs: HashedValues[] = [],
19
+ ) {}
20
+
21
+ static empty() {
22
+ return new ExecutionPayload([], [], []);
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Merges an array ExecutionPayloads combining their calls, authWitnesses, capsules and extraArgHashes.
28
+ */
29
+ export function mergeExecutionPayloads(requests: ExecutionPayload[]): ExecutionPayload {
30
+ const calls = requests.map(r => r.calls).flat();
31
+ const combinedAuthWitnesses = requests.map(r => r.authWitnesses ?? []).flat();
32
+ const combinedCapsules = requests.map(r => r.capsules ?? []).flat();
33
+ const combinedextraHashedArgs = requests.map(r => r.extraHashedArgs ?? []).flat();
34
+ return new ExecutionPayload(calls, combinedAuthWitnesses, combinedCapsules, combinedextraHashedArgs);
35
+ }
@@ -1,19 +0,0 @@
1
- import type { AuthWitnessProvider } from '@aztec/aztec.js/account';
2
- import { type EntrypointInterface, type ExecutionRequestInit } from '@aztec/aztec.js/entrypoint';
3
- import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
- import { TxExecutionRequest } from '@aztec/stdlib/tx';
5
- /**
6
- * Implementation for an entrypoint interface that follows the default entrypoint signature
7
- * for an account, which accepts an AppPayload and a FeePayload as defined in noir-libs/aztec-noir/src/entrypoint module
8
- */
9
- export declare class DefaultDappEntrypoint implements EntrypointInterface {
10
- private userAddress;
11
- private userAuthWitnessProvider;
12
- private dappEntrypointAddress;
13
- private chainId;
14
- private version;
15
- constructor(userAddress: AztecAddress, userAuthWitnessProvider: AuthWitnessProvider, dappEntrypointAddress: AztecAddress, chainId?: number, version?: number);
16
- createTxExecutionRequest(exec: ExecutionRequestInit): Promise<TxExecutionRequest>;
17
- private getEntrypointAbi;
18
- }
19
- //# sourceMappingURL=dapp_entrypoint.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dapp_entrypoint.d.ts","sourceRoot":"","sources":["../src/dapp_entrypoint.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EAAE,KAAK,mBAAmB,EAAqB,KAAK,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAEpH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAA2B,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAI/E;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,mBAAmB;IAE7D,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,uBAAuB;IAC/B,OAAO,CAAC,qBAAqB;IAC7B,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,OAAO;gBAJP,WAAW,EAAE,YAAY,EACzB,uBAAuB,EAAE,mBAAmB,EAC5C,qBAAqB,EAAE,YAAY,EACnC,OAAO,GAAE,MAAyB,EAClC,OAAO,GAAE,MAAwB;IAGrC,wBAAwB,CAAC,IAAI,EAAE,oBAAoB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAqCvF,OAAO,CAAC,gBAAgB;CAiEzB"}
@@ -1,125 +0,0 @@
1
- import { Fr, computeAuthWitMessageHash, computeInnerAuthWitHash } from '@aztec/aztec.js';
2
- import type { AuthWitnessProvider } from '@aztec/aztec.js/account';
3
- import { type EntrypointInterface, EntrypointPayload, type ExecutionRequestInit } from '@aztec/aztec.js/entrypoint';
4
- import { type FunctionAbi, FunctionSelector, encodeArguments } from '@aztec/stdlib/abi';
5
- import type { AztecAddress } from '@aztec/stdlib/aztec-address';
6
- import { HashedValues, TxContext, TxExecutionRequest } from '@aztec/stdlib/tx';
7
-
8
- import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from './constants.js';
9
-
10
- /**
11
- * Implementation for an entrypoint interface that follows the default entrypoint signature
12
- * for an account, which accepts an AppPayload and a FeePayload as defined in noir-libs/aztec-noir/src/entrypoint module
13
- */
14
- export class DefaultDappEntrypoint implements EntrypointInterface {
15
- constructor(
16
- private userAddress: AztecAddress,
17
- private userAuthWitnessProvider: AuthWitnessProvider,
18
- private dappEntrypointAddress: AztecAddress,
19
- private chainId: number = DEFAULT_CHAIN_ID,
20
- private version: number = DEFAULT_VERSION,
21
- ) {}
22
-
23
- async createTxExecutionRequest(exec: ExecutionRequestInit): Promise<TxExecutionRequest> {
24
- const { calls, fee, capsules = [] } = exec;
25
- if (calls.length !== 1) {
26
- throw new Error(`Expected exactly 1 function call, got ${calls.length}`);
27
- }
28
-
29
- const payload = await EntrypointPayload.fromFunctionCalls(calls);
30
-
31
- const abi = this.getEntrypointAbi();
32
- const entrypointHashedArgs = await HashedValues.fromValues(encodeArguments(abi, [payload, this.userAddress]));
33
- const functionSelector = await FunctionSelector.fromNameAndParameters(abi.name, abi.parameters);
34
- // Default msg_sender for entrypoints is now Fr.max_value rather than 0 addr (see #7190 & #7404)
35
- const innerHash = await computeInnerAuthWitHash([
36
- Fr.MAX_FIELD_VALUE,
37
- functionSelector.toField(),
38
- entrypointHashedArgs.hash,
39
- ]);
40
- const outerHash = await computeAuthWitMessageHash(
41
- { consumer: this.dappEntrypointAddress, innerHash },
42
- { chainId: new Fr(this.chainId), version: new Fr(this.version) },
43
- );
44
-
45
- const authWitness = await this.userAuthWitnessProvider.createAuthWit(outerHash);
46
-
47
- const txRequest = TxExecutionRequest.from({
48
- firstCallArgsHash: entrypointHashedArgs.hash,
49
- origin: this.dappEntrypointAddress,
50
- functionSelector,
51
- txContext: new TxContext(this.chainId, this.version, fee.gasSettings),
52
- argsOfCalls: [...payload.hashedArguments, entrypointHashedArgs],
53
- authWitnesses: [authWitness],
54
- capsules,
55
- });
56
-
57
- return txRequest;
58
- }
59
-
60
- private getEntrypointAbi() {
61
- return {
62
- name: 'entrypoint',
63
- isInitializer: false,
64
- functionType: 'private',
65
- isInternal: false,
66
- isStatic: false,
67
- parameters: [
68
- {
69
- name: 'payload',
70
- type: {
71
- kind: 'struct',
72
- path: 'dapp_payload::DAppPayload',
73
- fields: [
74
- {
75
- name: 'function_calls',
76
- type: {
77
- kind: 'array',
78
- length: 1,
79
- type: {
80
- kind: 'struct',
81
- path: 'authwit::entrypoint::function_call::FunctionCall',
82
- fields: [
83
- { name: 'args_hash', type: { kind: 'field' } },
84
- {
85
- name: 'function_selector',
86
- type: {
87
- kind: 'struct',
88
- path: 'authwit::aztec::protocol_types::abis::function_selector::FunctionSelector',
89
- fields: [{ name: 'inner', type: { kind: 'integer', sign: 'unsigned', width: 32 } }],
90
- },
91
- },
92
- {
93
- name: 'target_address',
94
- type: {
95
- kind: 'struct',
96
- path: 'authwit::aztec::protocol_types::address::aztec_address::AztecAddress',
97
- fields: [{ name: 'inner', type: { kind: 'field' } }],
98
- },
99
- },
100
- { name: 'is_public', type: { kind: 'boolean' } },
101
- { name: 'is_static', type: { kind: 'boolean' } },
102
- ],
103
- },
104
- },
105
- },
106
- { name: 'nonce', type: { kind: 'field' } },
107
- ],
108
- },
109
- visibility: 'public',
110
- },
111
- {
112
- name: 'user_address',
113
- type: {
114
- kind: 'struct',
115
- path: 'authwit::aztec::protocol_types::address::aztec_address::AztecAddress',
116
- fields: [{ name: 'inner', type: { kind: 'field' } }],
117
- },
118
- visibility: 'public',
119
- },
120
- ],
121
- returnTypes: [],
122
- errorTypes: {},
123
- } as FunctionAbi;
124
- }
125
- }