@aztec/entrypoints 0.0.0-test.1 → 0.0.1-commit.b655e406

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,148 @@
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 = 5;
12
+
13
+ /** Encoded function call for an Aztec entrypoint */
14
+ export type EncodedFunctionCall = {
15
+ /** Arguments hash for the call. */
16
+ /** This should be the calldata hash `hash([function_selector, ...args])` if `is_public` is true. */
17
+ args_hash: Fr;
18
+ /** Selector of the function to call */
19
+ function_selector: Fr;
20
+ /** Address of the contract to call */
21
+ target_address: Fr;
22
+ /** Whether the function is public or private */
23
+ is_public: boolean;
24
+ /** Only applicable for enqueued public function calls. Whether the msg_sender will be set to "null". */
25
+ hide_msg_sender: 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 class EncodedAppEntrypointCalls 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
+ toFields(): Fr[] {
75
+ return [...this.functionCallsToFields(), this.tx_nonce];
76
+ }
77
+
78
+ /**
79
+ * Hashes the payload
80
+ * @returns The hash of the payload
81
+ */
82
+ hash() {
83
+ return poseidon2HashWithSeparator(this.toFields(), this.generatorIndex);
84
+ }
85
+
86
+ /** Serializes the function calls to an array of fields. */
87
+ protected functionCallsToFields() {
88
+ return this.encodedFunctionCalls.flatMap(call => [
89
+ call.args_hash,
90
+ call.function_selector,
91
+ call.target_address,
92
+ new Fr(call.is_public),
93
+ new Fr(call.hide_msg_sender),
94
+ new Fr(call.is_static),
95
+ ]);
96
+ }
97
+
98
+ /**
99
+ * Encodes the functions for the app-portion of a transaction from a set of function calls and a nonce
100
+ * @param functionCalls - The function calls to execute
101
+ * @param txNonce - A nonce used to enable transaction cancellation when cancellable=true. Transactions with the same
102
+ * nonce can be replaced by submitting a new one with a higher fee.
103
+ * @returns The encoded calls
104
+ */
105
+ static async create(
106
+ functionCalls: FunctionCall[] | Tuple<FunctionCall, typeof APP_MAX_CALLS>,
107
+ txNonce = Fr.random(),
108
+ ) {
109
+ if (functionCalls.length > APP_MAX_CALLS) {
110
+ throw new Error(`Expected at most ${APP_MAX_CALLS} function calls, got ${functionCalls.length}`);
111
+ }
112
+ const paddedCalls = padArrayEnd(functionCalls, FunctionCall.empty(), APP_MAX_CALLS);
113
+ const encoded = await encode(paddedCalls);
114
+ return new EncodedAppEntrypointCalls(
115
+ encoded.encodedFunctionCalls,
116
+ encoded.hashedArguments,
117
+ GeneratorIndex.SIGNATURE_PAYLOAD,
118
+ txNonce,
119
+ );
120
+ }
121
+ }
122
+
123
+ /** Encodes FunctionCalls for execution, following Noir's convention */
124
+ export async function encode(calls: FunctionCall[]): Promise<EncodedCalls> {
125
+ const hashedArguments: HashedValues[] = [];
126
+ for (const call of calls) {
127
+ const hashed =
128
+ call.type === FunctionType.PUBLIC
129
+ ? await HashedValues.fromCalldata([call.selector.toField(), ...call.args])
130
+ : await HashedValues.fromArgs(call.args);
131
+ hashedArguments.push(hashed);
132
+ }
133
+
134
+ /* eslint-disable camelcase */
135
+ const encodedFunctionCalls: EncodedFunctionCall[] = calls.map((call, index) => ({
136
+ args_hash: hashedArguments[index].hash,
137
+ function_selector: call.selector.toField(),
138
+ target_address: call.to.toField(),
139
+ is_public: call.type == FunctionType.PUBLIC,
140
+ hide_msg_sender: call.hideMsgSender,
141
+ is_static: call.isStatic,
142
+ }));
143
+
144
+ return {
145
+ encodedFunctionCalls,
146
+ hashedArguments,
147
+ };
148
+ }
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,46 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+ import type { AuthWitness } from '@aztec/stdlib/auth-witness';
3
+ import type { GasSettings } from '@aztec/stdlib/gas';
4
+ import type { TxExecutionRequest } from '@aztec/stdlib/tx';
5
+
6
+ import type { ExecutionPayload } from './payload.js';
7
+
8
+ /**
9
+ * Information on the connected chain. Used by wallets when constructing transactions to protect against replay
10
+ * attacks.
11
+ */
12
+ export type ChainInfo = {
13
+ /** The L1 chain id */
14
+ chainId: Fr;
15
+ /** The version of the rollup */
16
+ version: Fr;
17
+ };
18
+
19
+ /**
20
+ * Creates transaction execution requests out of a set of function calls, a fee payment method and
21
+ * general options for the transaction
22
+ */
23
+ export interface EntrypointInterface {
24
+ /**
25
+ * Generates an execution request out of set of function calls.
26
+ * @param exec - The execution intents to be run.
27
+ * @param gasSettings - The gas settings for the transaction.
28
+ * @param options - Miscellaneous tx options that enable/disable features of the entrypoint
29
+ * @returns The authenticated transaction execution request.
30
+ */
31
+ createTxExecutionRequest(
32
+ exec: ExecutionPayload,
33
+ gasSettings: GasSettings,
34
+ options?: any,
35
+ ): Promise<TxExecutionRequest>;
36
+ }
37
+
38
+ /** Creates authorization witnesses. */
39
+ export interface AuthWitnessProvider {
40
+ /**
41
+ * Computes an authentication witness from either a message hash
42
+ * @param messageHash - The message hash to approve
43
+ * @returns The authentication witness
44
+ */
45
+ createAuthWit(messageHash: Fr | Buffer): Promise<AuthWitness>;
46
+ }
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
- }