@aztec/entrypoints 0.80.0 → 0.82.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/account_entrypoint.d.ts +3 -3
- package/dest/account_entrypoint.d.ts.map +1 -1
- package/dest/account_entrypoint.js +25 -11
- package/dest/dapp_entrypoint.d.ts +3 -3
- package/dest/dapp_entrypoint.d.ts.map +1 -1
- package/dest/dapp_entrypoint.js +25 -20
- package/dest/default_entrypoint.d.ts +13 -0
- package/dest/default_entrypoint.d.ts.map +1 -0
- package/dest/default_entrypoint.js +35 -0
- package/dest/encoding.d.ts +111 -0
- package/dest/encoding.d.ts.map +1 -0
- package/dest/encoding.js +135 -0
- package/dest/index.d.ts +3 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -0
- package/dest/interfaces.d.ts +83 -0
- package/dest/interfaces.d.ts.map +1 -0
- package/dest/interfaces.js +2 -0
- package/dest/payload.d.ts +32 -0
- package/dest/payload.d.ts.map +1 -0
- package/dest/payload.js +27 -0
- package/package.json +10 -6
- package/src/account_entrypoint.ts +34 -15
- package/src/dapp_entrypoint.ts +35 -17
- package/src/default_entrypoint.ts +48 -0
- package/src/encoding.ts +225 -0
- package/src/index.ts +3 -0
- package/src/interfaces.ts +93 -0
- package/src/payload.ts +35 -0
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
* Represents data necessary to perform an action in the network successfully.
|
|
6
|
+
* This class can be considered Aztec's "minimal execution unit".
|
|
7
|
+
* */
|
|
8
|
+
export declare class ExecutionPayload {
|
|
9
|
+
/** The function calls to be executed. */
|
|
10
|
+
calls: FunctionCall[];
|
|
11
|
+
/** Any transient auth witnesses needed for this execution */
|
|
12
|
+
authWitnesses: AuthWitness[];
|
|
13
|
+
/** Data passed through an oracle for this execution. */
|
|
14
|
+
capsules: Capsule[];
|
|
15
|
+
/** Extra hashed values to be injected in the execution cache */
|
|
16
|
+
extraHashedArgs: HashedValues[];
|
|
17
|
+
constructor(
|
|
18
|
+
/** The function calls to be executed. */
|
|
19
|
+
calls: FunctionCall[],
|
|
20
|
+
/** Any transient auth witnesses needed for this execution */
|
|
21
|
+
authWitnesses: AuthWitness[],
|
|
22
|
+
/** Data passed through an oracle for this execution. */
|
|
23
|
+
capsules: Capsule[],
|
|
24
|
+
/** Extra hashed values to be injected in the execution cache */
|
|
25
|
+
extraHashedArgs?: HashedValues[]);
|
|
26
|
+
static empty(): ExecutionPayload;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Merges an array ExecutionPayloads combining their calls, authWitnesses, capsules and extraArgHashes.
|
|
30
|
+
*/
|
|
31
|
+
export declare function mergeExecutionPayloads(requests: ExecutionPayload[]): ExecutionPayload;
|
|
32
|
+
//# sourceMappingURL=payload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payload.d.ts","sourceRoot":"","sources":["../src/payload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEzD;;;KAGK;AACL,qBAAa,gBAAgB;IAEzB,yCAAyC;IAClC,KAAK,EAAE,YAAY,EAAE;IAC5B,6DAA6D;IACtD,aAAa,EAAE,WAAW,EAAE;IACnC,wDAAwD;IACjD,QAAQ,EAAE,OAAO,EAAE;IAC1B,gEAAgE;IACzD,eAAe,EAAE,YAAY,EAAE;;IAPtC,yCAAyC;IAClC,KAAK,EAAE,YAAY,EAAE;IAC5B,6DAA6D;IACtD,aAAa,EAAE,WAAW,EAAE;IACnC,wDAAwD;IACjD,QAAQ,EAAE,OAAO,EAAE;IAC1B,gEAAgE;IACzD,eAAe,GAAE,YAAY,EAAO;IAG7C,MAAM,CAAC,KAAK;CAGb;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,gBAAgB,EAAE,GAAG,gBAAgB,CAMrF"}
|
package/dest/payload.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents data necessary to perform an action in the network successfully.
|
|
3
|
+
* This class can be considered Aztec's "minimal execution unit".
|
|
4
|
+
* */ export class ExecutionPayload {
|
|
5
|
+
calls;
|
|
6
|
+
authWitnesses;
|
|
7
|
+
capsules;
|
|
8
|
+
extraHashedArgs;
|
|
9
|
+
constructor(/** The function calls to be executed. */ calls, /** Any transient auth witnesses needed for this execution */ authWitnesses, /** Data passed through an oracle for this execution. */ capsules, /** Extra hashed values to be injected in the execution cache */ extraHashedArgs = []){
|
|
10
|
+
this.calls = calls;
|
|
11
|
+
this.authWitnesses = authWitnesses;
|
|
12
|
+
this.capsules = capsules;
|
|
13
|
+
this.extraHashedArgs = extraHashedArgs;
|
|
14
|
+
}
|
|
15
|
+
static empty() {
|
|
16
|
+
return new ExecutionPayload([], [], []);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Merges an array ExecutionPayloads combining their calls, authWitnesses, capsules and extraArgHashes.
|
|
21
|
+
*/ export function mergeExecutionPayloads(requests) {
|
|
22
|
+
const calls = requests.map((r)=>r.calls).flat();
|
|
23
|
+
const combinedAuthWitnesses = requests.map((r)=>r.authWitnesses ?? []).flat();
|
|
24
|
+
const combinedCapsules = requests.map((r)=>r.capsules ?? []).flat();
|
|
25
|
+
const combinedextraHashedArgs = requests.map((r)=>r.extraHashedArgs ?? []).flat();
|
|
26
|
+
return new ExecutionPayload(calls, combinedAuthWitnesses, combinedCapsules, combinedextraHashedArgs);
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -2,11 +2,15 @@
|
|
|
2
2
|
"name": "@aztec/entrypoints",
|
|
3
3
|
"homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/entrypoints",
|
|
4
4
|
"description": "Implementation of sample contract entrypoints for the Aztec Network",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.82.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
"./dapp": "./dest/dapp_entrypoint.js",
|
|
9
|
-
"./account": "./dest/account_entrypoint.js"
|
|
9
|
+
"./account": "./dest/account_entrypoint.js",
|
|
10
|
+
"./default": "./dest/default_entrypoint.js",
|
|
11
|
+
"./interfaces": "./dest/interfaces.js",
|
|
12
|
+
"./payload": "./dest/payload.js",
|
|
13
|
+
"./encoding": "./dest/encoding.js"
|
|
10
14
|
},
|
|
11
15
|
"typedocOptions": {
|
|
12
16
|
"entryPoints": [
|
|
@@ -61,10 +65,10 @@
|
|
|
61
65
|
]
|
|
62
66
|
},
|
|
63
67
|
"dependencies": {
|
|
64
|
-
"@aztec/
|
|
65
|
-
"@aztec/foundation": "0.
|
|
66
|
-
"@aztec/protocol-contracts": "0.
|
|
67
|
-
"@aztec/stdlib": "0.
|
|
68
|
+
"@aztec/constants": "0.82.0",
|
|
69
|
+
"@aztec/foundation": "0.82.0",
|
|
70
|
+
"@aztec/protocol-contracts": "0.82.0",
|
|
71
|
+
"@aztec/stdlib": "0.82.0",
|
|
68
72
|
"tslib": "^2.4.0"
|
|
69
73
|
},
|
|
70
74
|
"devDependencies": {
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import type { AuthWitnessProvider } from '@aztec/aztec.js/account';
|
|
2
|
-
import {
|
|
3
|
-
type EntrypointInterface,
|
|
4
|
-
EntrypointPayload,
|
|
5
|
-
type ExecutionRequestInit,
|
|
6
|
-
computeCombinedPayloadHash,
|
|
7
|
-
} from '@aztec/aztec.js/entrypoint';
|
|
8
1
|
import { type FunctionAbi, FunctionSelector, encodeArguments } from '@aztec/stdlib/abi';
|
|
9
2
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
10
3
|
import { HashedValues, TxContext, TxExecutionRequest } from '@aztec/stdlib/tx';
|
|
11
4
|
|
|
12
5
|
import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from './constants.js';
|
|
6
|
+
import { EncodedCallsForEntrypoint, computeCombinedPayloadHash } from './encoding.js';
|
|
7
|
+
import type { AuthWitnessProvider, EntrypointInterface, FeeOptions, TxExecutionOptions } from './interfaces.js';
|
|
8
|
+
import { ExecutionPayload } from './payload.js';
|
|
13
9
|
|
|
14
10
|
/**
|
|
15
11
|
* Implementation for an entrypoint interface that follows the default entrypoint signature
|
|
@@ -23,27 +19,50 @@ export class DefaultAccountEntrypoint implements EntrypointInterface {
|
|
|
23
19
|
private version: number = DEFAULT_VERSION,
|
|
24
20
|
) {}
|
|
25
21
|
|
|
26
|
-
async createTxExecutionRequest(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
async createTxExecutionRequest(
|
|
23
|
+
exec: ExecutionPayload,
|
|
24
|
+
fee: FeeOptions,
|
|
25
|
+
options: TxExecutionOptions,
|
|
26
|
+
): Promise<TxExecutionRequest> {
|
|
27
|
+
// Initial request with calls, authWitnesses and capsules
|
|
28
|
+
const { calls, authWitnesses, capsules, extraHashedArgs } = exec;
|
|
29
|
+
// Global tx options
|
|
30
|
+
const { cancellable, nonce } = options;
|
|
31
|
+
// Encode the calls for the app
|
|
32
|
+
const appEncodedCalls = await EncodedCallsForEntrypoint.fromAppExecution(calls, nonce);
|
|
33
|
+
// Get the execution payload for the fee, it includes the calls and potentially authWitnesses
|
|
34
|
+
const { calls: feeCalls, authWitnesses: feeAuthwitnesses } = await fee.paymentMethod.getExecutionPayload(
|
|
35
|
+
fee.gasSettings,
|
|
36
|
+
);
|
|
37
|
+
// Encode the calls for the fee
|
|
38
|
+
const feePayer = await fee.paymentMethod.getFeePayer(fee.gasSettings);
|
|
39
|
+
const isFeePayer = feePayer.equals(this.address);
|
|
40
|
+
const feeEncodedCalls = await EncodedCallsForEntrypoint.fromFeeCalls(feeCalls, isFeePayer);
|
|
30
41
|
|
|
42
|
+
// Obtain the entrypoint hashed args, built from the app and fee encoded calls
|
|
31
43
|
const abi = this.getEntrypointAbi();
|
|
32
44
|
const entrypointHashedArgs = await HashedValues.fromValues(
|
|
33
|
-
encodeArguments(abi, [
|
|
45
|
+
encodeArguments(abi, [appEncodedCalls, feeEncodedCalls, !!cancellable]),
|
|
34
46
|
);
|
|
35
47
|
|
|
48
|
+
// Generate the combined payload auth witness, by signing the hash of the combined payload
|
|
36
49
|
const combinedPayloadAuthWitness = await this.auth.createAuthWit(
|
|
37
|
-
await computeCombinedPayloadHash(
|
|
50
|
+
await computeCombinedPayloadHash(appEncodedCalls, feeEncodedCalls),
|
|
38
51
|
);
|
|
39
52
|
|
|
53
|
+
// Assemble the tx request
|
|
40
54
|
const txRequest = TxExecutionRequest.from({
|
|
41
55
|
firstCallArgsHash: entrypointHashedArgs.hash,
|
|
42
56
|
origin: this.address,
|
|
43
57
|
functionSelector: await FunctionSelector.fromNameAndParameters(abi.name, abi.parameters),
|
|
44
58
|
txContext: new TxContext(this.chainId, this.version, fee.gasSettings),
|
|
45
|
-
argsOfCalls: [
|
|
46
|
-
|
|
59
|
+
argsOfCalls: [
|
|
60
|
+
...appEncodedCalls.hashedArguments,
|
|
61
|
+
...feeEncodedCalls.hashedArguments,
|
|
62
|
+
entrypointHashedArgs,
|
|
63
|
+
...extraHashedArgs,
|
|
64
|
+
],
|
|
65
|
+
authWitnesses: [...authWitnesses, ...feeAuthwitnesses, combinedPayloadAuthWitness],
|
|
47
66
|
capsules,
|
|
48
67
|
});
|
|
49
68
|
|
package/src/dapp_entrypoint.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { Fr
|
|
2
|
-
import type
|
|
3
|
-
import {
|
|
4
|
-
import { type FunctionAbi, FunctionSelector, encodeArguments } from '@aztec/stdlib/abi';
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { type FunctionAbi, FunctionSelector, FunctionType, encodeArguments } from '@aztec/stdlib/abi';
|
|
3
|
+
import { computeInnerAuthWitHash, computeOuterAuthWitHash } from '@aztec/stdlib/auth-witness';
|
|
5
4
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
5
|
import { HashedValues, TxContext, TxExecutionRequest } from '@aztec/stdlib/tx';
|
|
7
6
|
|
|
8
7
|
import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from './constants.js';
|
|
8
|
+
import { EncodedCallsForEntrypoint } from './encoding.js';
|
|
9
|
+
import type { AuthWitnessProvider, EntrypointInterface, FeeOptions, TxExecutionOptions } from './interfaces.js';
|
|
10
|
+
import { ExecutionPayload } from './payload.js';
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Implementation for an entrypoint interface that follows the default entrypoint signature
|
|
@@ -20,16 +22,29 @@ export class DefaultDappEntrypoint implements EntrypointInterface {
|
|
|
20
22
|
private version: number = DEFAULT_VERSION,
|
|
21
23
|
) {}
|
|
22
24
|
|
|
23
|
-
async createTxExecutionRequest(
|
|
24
|
-
|
|
25
|
+
async createTxExecutionRequest(
|
|
26
|
+
exec: ExecutionPayload,
|
|
27
|
+
fee: FeeOptions,
|
|
28
|
+
options: TxExecutionOptions,
|
|
29
|
+
): Promise<TxExecutionRequest> {
|
|
30
|
+
if (options.nonce || options.cancellable !== undefined) {
|
|
31
|
+
throw new Error('TxExecutionOptions are not supported in DappEntrypoint');
|
|
32
|
+
}
|
|
33
|
+
// Initial request with calls, authWitnesses and capsules
|
|
34
|
+
const { calls, authWitnesses, capsules, extraHashedArgs } = exec;
|
|
25
35
|
if (calls.length !== 1) {
|
|
26
36
|
throw new Error(`Expected exactly 1 function call, got ${calls.length}`);
|
|
27
37
|
}
|
|
28
38
|
|
|
29
|
-
|
|
39
|
+
// Encode the function call the dapp is ultimately going to invoke
|
|
40
|
+
const encodedCalls = await EncodedCallsForEntrypoint.fromFunctionCalls(calls);
|
|
30
41
|
|
|
42
|
+
// Obtain the entrypoint hashed args, built from the function call and the user's address
|
|
31
43
|
const abi = this.getEntrypointAbi();
|
|
32
|
-
const entrypointHashedArgs = await HashedValues.fromValues(encodeArguments(abi, [
|
|
44
|
+
const entrypointHashedArgs = await HashedValues.fromValues(encodeArguments(abi, [encodedCalls, this.userAddress]));
|
|
45
|
+
|
|
46
|
+
// Construct an auth witness for the entrypoint, by signing the hash of the action to perform
|
|
47
|
+
// (the dapp calls a function on the user's behalf)
|
|
33
48
|
const functionSelector = await FunctionSelector.fromNameAndParameters(abi.name, abi.parameters);
|
|
34
49
|
// Default msg_sender for entrypoints is now Fr.max_value rather than 0 addr (see #7190 & #7404)
|
|
35
50
|
const innerHash = await computeInnerAuthWitHash([
|
|
@@ -37,31 +52,34 @@ export class DefaultDappEntrypoint implements EntrypointInterface {
|
|
|
37
52
|
functionSelector.toField(),
|
|
38
53
|
entrypointHashedArgs.hash,
|
|
39
54
|
]);
|
|
40
|
-
const outerHash = await
|
|
41
|
-
|
|
42
|
-
|
|
55
|
+
const outerHash = await computeOuterAuthWitHash(
|
|
56
|
+
this.dappEntrypointAddress,
|
|
57
|
+
new Fr(this.chainId),
|
|
58
|
+
new Fr(this.version),
|
|
59
|
+
innerHash,
|
|
43
60
|
);
|
|
44
61
|
|
|
45
|
-
const
|
|
62
|
+
const entypointAuthwitness = await this.userAuthWitnessProvider.createAuthWit(outerHash);
|
|
46
63
|
|
|
64
|
+
// Assemble the tx request
|
|
47
65
|
const txRequest = TxExecutionRequest.from({
|
|
48
66
|
firstCallArgsHash: entrypointHashedArgs.hash,
|
|
49
67
|
origin: this.dappEntrypointAddress,
|
|
50
68
|
functionSelector,
|
|
51
69
|
txContext: new TxContext(this.chainId, this.version, fee.gasSettings),
|
|
52
|
-
argsOfCalls: [...
|
|
53
|
-
authWitnesses: [...authWitnesses
|
|
70
|
+
argsOfCalls: [...encodedCalls.hashedArguments, entrypointHashedArgs, ...extraHashedArgs],
|
|
71
|
+
authWitnesses: [entypointAuthwitness, ...authWitnesses],
|
|
54
72
|
capsules,
|
|
55
73
|
});
|
|
56
74
|
|
|
57
75
|
return txRequest;
|
|
58
76
|
}
|
|
59
77
|
|
|
60
|
-
private getEntrypointAbi() {
|
|
78
|
+
private getEntrypointAbi(): FunctionAbi {
|
|
61
79
|
return {
|
|
62
80
|
name: 'entrypoint',
|
|
63
81
|
isInitializer: false,
|
|
64
|
-
functionType:
|
|
82
|
+
functionType: FunctionType.PRIVATE,
|
|
65
83
|
isInternal: false,
|
|
66
84
|
isStatic: false,
|
|
67
85
|
parameters: [
|
|
@@ -120,6 +138,6 @@ export class DefaultDappEntrypoint implements EntrypointInterface {
|
|
|
120
138
|
],
|
|
121
139
|
returnTypes: [],
|
|
122
140
|
errorTypes: {},
|
|
123
|
-
} as
|
|
141
|
+
} as const;
|
|
124
142
|
}
|
|
125
143
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { FunctionType } from '@aztec/stdlib/abi';
|
|
2
|
+
import { HashedValues, TxContext, TxExecutionRequest } from '@aztec/stdlib/tx';
|
|
3
|
+
|
|
4
|
+
import type { EntrypointInterface, FeeOptions, TxExecutionOptions } from './interfaces.js';
|
|
5
|
+
import type { ExecutionPayload } from './payload.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Default implementation of the entrypoint interface. It calls a function on a contract directly
|
|
9
|
+
*/
|
|
10
|
+
export class DefaultEntrypoint implements EntrypointInterface {
|
|
11
|
+
constructor(private chainId: number, private protocolVersion: number) {}
|
|
12
|
+
|
|
13
|
+
async createTxExecutionRequest(
|
|
14
|
+
exec: ExecutionPayload,
|
|
15
|
+
fee: FeeOptions,
|
|
16
|
+
options: TxExecutionOptions,
|
|
17
|
+
): Promise<TxExecutionRequest> {
|
|
18
|
+
if (options.nonce || options.cancellable !== undefined) {
|
|
19
|
+
throw new Error('TxExecutionOptions are not supported in DefaultEntrypoint');
|
|
20
|
+
}
|
|
21
|
+
// Initial request with calls, authWitnesses and capsules
|
|
22
|
+
const { calls, authWitnesses, capsules, extraHashedArgs } = exec;
|
|
23
|
+
|
|
24
|
+
if (calls.length > 1) {
|
|
25
|
+
throw new Error(`Expected a single call, got ${calls.length}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const call = calls[0];
|
|
29
|
+
|
|
30
|
+
// Hash the arguments for the function call
|
|
31
|
+
const hashedArguments = [await HashedValues.fromValues(call.args)];
|
|
32
|
+
|
|
33
|
+
if (call.type !== FunctionType.PRIVATE) {
|
|
34
|
+
throw new Error('Public entrypoints are not allowed');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Assemble the tx request
|
|
38
|
+
return new TxExecutionRequest(
|
|
39
|
+
call.to,
|
|
40
|
+
call.selector,
|
|
41
|
+
hashedArguments[0].hash,
|
|
42
|
+
new TxContext(this.chainId, this.protocolVersion, fee.gasSettings),
|
|
43
|
+
[...hashedArguments, ...extraHashedArgs],
|
|
44
|
+
authWitnesses,
|
|
45
|
+
capsules,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/encoding.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
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
|
+
/* eslint-disable camelcase */
|
|
16
|
+
/** Encoded function call for an Aztec entrypoint */
|
|
17
|
+
export type EncodedFunctionCall = {
|
|
18
|
+
/** Arguments hash for the call */
|
|
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
|
+
/* eslint-enable camelcase */
|
|
30
|
+
|
|
31
|
+
/** Type that represents function calls ready to be sent to a circuit for execution */
|
|
32
|
+
export type EncodedCalls = {
|
|
33
|
+
/** Function calls in the expected format (Noir's convention) */
|
|
34
|
+
encodedFunctionCalls: EncodedFunctionCall[];
|
|
35
|
+
/** The hashed args for the call, ready to be injected in the execution cache */
|
|
36
|
+
hashedArguments: HashedValues[];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Entrypoints derive their arguments from the calls that they'll ultimate make.
|
|
41
|
+
* This utility class helps in creating the payload for the entrypoint by taking into
|
|
42
|
+
* account how the calls are encoded and hashed.
|
|
43
|
+
* */
|
|
44
|
+
export abstract class EncodedCallsForEntrypoint implements EncodedCalls {
|
|
45
|
+
constructor(
|
|
46
|
+
/** Function calls in the expected format (Noir's convention) */
|
|
47
|
+
public encodedFunctionCalls: EncodedFunctionCall[],
|
|
48
|
+
/** The hashed args for the call, ready to be injected in the execution cache */
|
|
49
|
+
public hashedArguments: HashedValues[],
|
|
50
|
+
/** The index of the generator to use for hashing */
|
|
51
|
+
public generatorIndex: number,
|
|
52
|
+
/** The nonce for the payload, used to emit a nullifier identifying the call */
|
|
53
|
+
public nonce: Fr,
|
|
54
|
+
) {}
|
|
55
|
+
|
|
56
|
+
/* eslint-disable camelcase */
|
|
57
|
+
/**
|
|
58
|
+
* The function calls to execute. This uses snake_case naming so that it is compatible with Noir encoding
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
61
|
+
get function_calls() {
|
|
62
|
+
return this.encodedFunctionCalls;
|
|
63
|
+
}
|
|
64
|
+
/* eslint-enable camelcase */
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Serializes the payload to an array of fields
|
|
68
|
+
* @returns The fields of the payload
|
|
69
|
+
*/
|
|
70
|
+
abstract toFields(): Fr[];
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Hashes the payload
|
|
74
|
+
* @returns The hash of the payload
|
|
75
|
+
*/
|
|
76
|
+
hash() {
|
|
77
|
+
return poseidon2HashWithSeparator(this.toFields(), this.generatorIndex);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Serializes the function calls to an array of fields. */
|
|
81
|
+
protected functionCallsToFields() {
|
|
82
|
+
return this.encodedFunctionCalls.flatMap(call => [
|
|
83
|
+
call.args_hash,
|
|
84
|
+
call.function_selector,
|
|
85
|
+
call.target_address,
|
|
86
|
+
new Fr(call.is_public),
|
|
87
|
+
new Fr(call.is_static),
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Encodes a set of function calls for a dapp entrypoint
|
|
93
|
+
* @param functionCalls - The function calls to execute
|
|
94
|
+
* @returns The encoded calls
|
|
95
|
+
*/
|
|
96
|
+
static async fromFunctionCalls(functionCalls: FunctionCall[]) {
|
|
97
|
+
const encoded = await encode(functionCalls);
|
|
98
|
+
return new EncodedAppEntrypointCalls(encoded.encodedFunctionCalls, encoded.hashedArguments, 0, Fr.random());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Encodes the functions for the app-portion of a transaction from a set of function calls and a nonce
|
|
103
|
+
* @param functionCalls - The function calls to execute
|
|
104
|
+
* @param nonce - The nonce for the payload, used to emit a nullifier identifying the call
|
|
105
|
+
* @returns The encoded calls
|
|
106
|
+
*/
|
|
107
|
+
static async fromAppExecution(
|
|
108
|
+
functionCalls: FunctionCall[] | Tuple<FunctionCall, typeof APP_MAX_CALLS>,
|
|
109
|
+
nonce = Fr.random(),
|
|
110
|
+
) {
|
|
111
|
+
if (functionCalls.length > APP_MAX_CALLS) {
|
|
112
|
+
throw new Error(`Expected at most ${APP_MAX_CALLS} function calls, got ${functionCalls.length}`);
|
|
113
|
+
}
|
|
114
|
+
const paddedCalls = padArrayEnd(functionCalls, FunctionCall.empty(), APP_MAX_CALLS);
|
|
115
|
+
const encoded = await encode(paddedCalls);
|
|
116
|
+
return new EncodedAppEntrypointCalls(
|
|
117
|
+
encoded.encodedFunctionCalls,
|
|
118
|
+
encoded.hashedArguments,
|
|
119
|
+
GeneratorIndex.SIGNATURE_PAYLOAD,
|
|
120
|
+
nonce,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Creates an encoded set of functions to pay the fee for a transaction
|
|
126
|
+
* @param functionCalls - The calls generated by the payment method
|
|
127
|
+
* @param isFeePayer - Whether the sender should be appointed as fee payer
|
|
128
|
+
* @returns The encoded calls
|
|
129
|
+
*/
|
|
130
|
+
static async fromFeeCalls(
|
|
131
|
+
functionCalls: FunctionCall[] | Tuple<FunctionCall, typeof FEE_MAX_CALLS>,
|
|
132
|
+
isFeePayer: boolean,
|
|
133
|
+
) {
|
|
134
|
+
const paddedCalls = padArrayEnd(functionCalls, FunctionCall.empty(), FEE_MAX_CALLS);
|
|
135
|
+
const encoded = await encode(paddedCalls);
|
|
136
|
+
return new EncodedFeeEntrypointCalls(
|
|
137
|
+
encoded.encodedFunctionCalls,
|
|
138
|
+
encoded.hashedArguments,
|
|
139
|
+
GeneratorIndex.FEE_PAYLOAD,
|
|
140
|
+
Fr.random(),
|
|
141
|
+
isFeePayer,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Encoded calls for app phase execution. */
|
|
147
|
+
export class EncodedAppEntrypointCalls extends EncodedCallsForEntrypoint {
|
|
148
|
+
constructor(
|
|
149
|
+
encodedFunctionCalls: EncodedFunctionCall[],
|
|
150
|
+
hashedArguments: HashedValues[],
|
|
151
|
+
generatorIndex: number,
|
|
152
|
+
nonce: Fr,
|
|
153
|
+
) {
|
|
154
|
+
super(encodedFunctionCalls, hashedArguments, generatorIndex, nonce);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
override toFields(): Fr[] {
|
|
158
|
+
return [...this.functionCallsToFields(), this.nonce];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Encoded calls for fee payment */
|
|
163
|
+
export class EncodedFeeEntrypointCalls extends EncodedCallsForEntrypoint {
|
|
164
|
+
#isFeePayer: boolean;
|
|
165
|
+
|
|
166
|
+
constructor(
|
|
167
|
+
encodedFunctionCalls: EncodedFunctionCall[],
|
|
168
|
+
hashedArguments: HashedValues[],
|
|
169
|
+
generatorIndex: number,
|
|
170
|
+
nonce: Fr,
|
|
171
|
+
isFeePayer: boolean,
|
|
172
|
+
) {
|
|
173
|
+
super(encodedFunctionCalls, hashedArguments, generatorIndex, nonce);
|
|
174
|
+
this.#isFeePayer = isFeePayer;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
override toFields(): Fr[] {
|
|
178
|
+
return [...this.functionCallsToFields(), this.nonce, new Fr(this.#isFeePayer)];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/* eslint-disable camelcase */
|
|
182
|
+
/** Whether the sender should be appointed as fee payer. */
|
|
183
|
+
get is_fee_payer() {
|
|
184
|
+
return this.#isFeePayer;
|
|
185
|
+
}
|
|
186
|
+
/* eslint-enable camelcase */
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Computes a hash of a combined set of app and fee calls.
|
|
191
|
+
* @param appCalls - A set of app calls.
|
|
192
|
+
* @param feeCalls - A set of calls used to pay fees.
|
|
193
|
+
* @returns A hash of a combined call set.
|
|
194
|
+
*/
|
|
195
|
+
export async function computeCombinedPayloadHash(
|
|
196
|
+
appPayload: EncodedAppEntrypointCalls,
|
|
197
|
+
feePayload: EncodedFeeEntrypointCalls,
|
|
198
|
+
): Promise<Fr> {
|
|
199
|
+
return poseidon2HashWithSeparator(
|
|
200
|
+
[await appPayload.hash(), await feePayload.hash()],
|
|
201
|
+
GeneratorIndex.COMBINED_PAYLOAD,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Encodes FunctionCalls for execution, following Noir's convention */
|
|
206
|
+
export async function encode(calls: FunctionCall[]): Promise<EncodedCalls> {
|
|
207
|
+
const hashedArguments: HashedValues[] = [];
|
|
208
|
+
for (const call of calls) {
|
|
209
|
+
hashedArguments.push(await HashedValues.fromValues(call.args));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/* eslint-disable camelcase */
|
|
213
|
+
const encodedFunctionCalls: EncodedFunctionCall[] = calls.map((call, index) => ({
|
|
214
|
+
args_hash: hashedArguments[index].hash,
|
|
215
|
+
function_selector: call.selector.toField(),
|
|
216
|
+
target_address: call.to.toField(),
|
|
217
|
+
is_public: call.type == FunctionType.PUBLIC,
|
|
218
|
+
is_static: call.isStatic,
|
|
219
|
+
}));
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
encodedFunctionCalls,
|
|
223
|
+
hashedArguments,
|
|
224
|
+
};
|
|
225
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
/** The nonce to use for the transaction. */
|
|
17
|
+
nonce?: Fr;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates transaction execution requests out of a set of function calls, a fee payment method and
|
|
22
|
+
* general options for the transaction
|
|
23
|
+
*/
|
|
24
|
+
export interface EntrypointInterface {
|
|
25
|
+
/**
|
|
26
|
+
* Generates an execution request out of set of function calls.
|
|
27
|
+
* @param exec - The execution intents to be run.
|
|
28
|
+
* @param fee - The fee options for the transaction.
|
|
29
|
+
* @param options - Nonce and whether the transaction is cancellable.
|
|
30
|
+
* @returns The authenticated transaction execution request.
|
|
31
|
+
*/
|
|
32
|
+
createTxExecutionRequest(
|
|
33
|
+
exec: ExecutionPayload,
|
|
34
|
+
fee: FeeOptions,
|
|
35
|
+
options: TxExecutionOptions,
|
|
36
|
+
): Promise<TxExecutionRequest>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Creates authorization witnesses. */
|
|
40
|
+
export interface AuthWitnessProvider {
|
|
41
|
+
/**
|
|
42
|
+
* Computes an authentication witness from either a message hash
|
|
43
|
+
* @param messageHash - The message hash to approve
|
|
44
|
+
* @returns The authentication witness
|
|
45
|
+
*/
|
|
46
|
+
createAuthWit(messageHash: Fr | Buffer): Promise<AuthWitness>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Holds information about how the fee for a transaction is to be paid.
|
|
51
|
+
*/
|
|
52
|
+
export interface FeePaymentMethod {
|
|
53
|
+
/** The asset used to pay the fee. */
|
|
54
|
+
getAsset(): Promise<AztecAddress>;
|
|
55
|
+
/**
|
|
56
|
+
* Returns the data to be added to the final execution request
|
|
57
|
+
* to pay the fee in the given asset
|
|
58
|
+
* @param gasSettings - The gas limits and max fees.
|
|
59
|
+
* @returns The function calls to pay the fee.
|
|
60
|
+
*/
|
|
61
|
+
getExecutionPayload(gasSettings: GasSettings): Promise<ExecutionPayload>;
|
|
62
|
+
/**
|
|
63
|
+
* The expected fee payer for this tx.
|
|
64
|
+
* @param gasSettings - The gas limits and max fees.
|
|
65
|
+
*/
|
|
66
|
+
getFeePayer(gasSettings: GasSettings): Promise<AztecAddress>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Fee payment options for a transaction.
|
|
71
|
+
*/
|
|
72
|
+
export type FeeOptions = {
|
|
73
|
+
/** The fee payment method to use */
|
|
74
|
+
paymentMethod: FeePaymentMethod;
|
|
75
|
+
/** The gas settings */
|
|
76
|
+
gasSettings: GasSettings;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// docs:start:user_fee_options
|
|
80
|
+
/** Fee options as set by a user. */
|
|
81
|
+
export type UserFeeOptions = {
|
|
82
|
+
/** The fee payment method to use */
|
|
83
|
+
paymentMethod?: FeePaymentMethod;
|
|
84
|
+
/** The gas settings */
|
|
85
|
+
gasSettings?: Partial<FieldsOf<GasSettings>>;
|
|
86
|
+
/** Percentage to pad the base fee by, if empty, defaults to 0.5 */
|
|
87
|
+
baseFeePadding?: number;
|
|
88
|
+
/** Whether to run an initial simulation of the tx with high gas limit to figure out actual gas settings. */
|
|
89
|
+
estimateGas?: boolean;
|
|
90
|
+
/** Percentage to pad the estimated gas limits by, if empty, defaults to 0.1. Only relevant if estimateGas is set. */
|
|
91
|
+
estimatedGasPadding?: number;
|
|
92
|
+
};
|
|
93
|
+
// docs:end:user_fee_options
|