-
Notifications
You must be signed in to change notification settings - Fork 240
/
tx_context.ts
190 lines (176 loc) · 5.52 KB
/
tx_context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { BufferReader } from '@aztec/foundation/serialize';
import { FieldsOf, PublicKey } from '../index.js';
import { serializeToBuffer } from '../utils/serialize.js';
import { AztecAddress, EthAddress, Fr, Point } from './index.js';
/**
* Contract deployment data in a TxContext
* cpp/src/aztec3/circuits/abis/contract_deployment_data.hpp.
*
* Not to be confused with NewContractData.
*/
export class ContractDeploymentData {
/** Ethereum address of the portal contract on L1. */
public portalContractAddress: EthAddress;
constructor(
/** Public key of the contract deployer (used when deploying account contracts). */
public deployerPublicKey: PublicKey,
/** Hash of the constructor verification key. */
public constructorVkHash: Fr,
/** Function tree root. */
public functionTreeRoot: Fr,
/** Contract address salt (used when deriving a contract address). */
public contractAddressSalt: Fr,
/**
* Ethereum address of the portal contract on L1.
* TODO(AD): union type kludge due to cbind compiler having special needs
*/
portalContractAddress: EthAddress | AztecAddress,
) {
this.portalContractAddress = EthAddress.fromField(portalContractAddress.toField());
}
toBuffer() {
return serializeToBuffer(
this.deployerPublicKey,
this.constructorVkHash,
this.functionTreeRoot,
this.contractAddressSalt,
this.portalContractAddress,
);
}
/**
* Returns an empty ContractDeploymentData.
* @returns The empty ContractDeploymentData.
*/
public static empty(): ContractDeploymentData {
return new ContractDeploymentData(Point.ZERO, Fr.ZERO, Fr.ZERO, Fr.ZERO, EthAddress.ZERO);
}
isEmpty() {
return (
this.deployerPublicKey.isZero() &&
this.constructorVkHash.isZero() &&
this.functionTreeRoot.isZero() &&
this.contractAddressSalt.isZero() &&
this.portalContractAddress.isZero()
);
}
/**
* Deserializes contract deployment data rom a buffer or reader.
* @param buffer - Buffer to read from.
* @returns The deserialized ContractDeploymentData.
*/
static fromBuffer(buffer: Buffer | BufferReader): ContractDeploymentData {
const reader = BufferReader.asReader(buffer);
return new ContractDeploymentData(
reader.readObject(Point),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
new EthAddress(reader.readBytes(32)),
);
}
}
/**
* Transaction context.
* @see cpp/src/aztec3/circuits/abis/tx_context.hpp.
*/
export class TxContext {
constructor(
/**
* Whether this is a fee paying tx. If not other tx in a bundle will pay the fee.
* TODO(#3417): Remove fee and rebate payment fields.
*/
public isFeePaymentTx: boolean,
/**
* Indicates whether this a gas rebate payment tx.
*
* NOTE: The following is a WIP and it is likely to change in the future.
* Explanation: Each tx is actually 3 txs in one: a fee-paying tx, the actual tx you want to execute, and a rebate
* tx. The fee-paying tx pays some `max_fee = gas_price * gas_limit`. Then the actual tx will cost an amount of gas
* to execute (actual_fee = gas_price * gas_used). Then the rebate tx returns `max_fee - actual_fee` back to
* the user.
*/
public isRebatePaymentTx: boolean,
/**
* Whether this is a contract deployment tx.
*/
public isContractDeploymentTx: boolean,
/**
* Contract deployment data.
*/
public contractDeploymentData: ContractDeploymentData,
/**
* Chain ID of the transaction. Here for replay protection.
*/
public chainId: Fr,
/**
* Version of the transaction. Here for replay protection.
*/
public version: Fr,
) {}
/**
* Serialize as a buffer.
* @returns The buffer.
*/
toBuffer() {
return serializeToBuffer(
this.isFeePaymentTx,
this.isRebatePaymentTx,
this.isContractDeploymentTx,
this.contractDeploymentData,
this.chainId,
this.version,
);
}
/**
* Deserializes TxContext from a buffer or reader.
* @param buffer - Buffer to read from.
* @returns The TxContext.
*/
static fromBuffer(buffer: Buffer | BufferReader): TxContext {
const reader = BufferReader.asReader(buffer);
return new TxContext(
reader.readBoolean(),
reader.readBoolean(),
reader.readBoolean(),
reader.readObject(ContractDeploymentData),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
);
}
static empty(chainId: Fr | number = 0, version: Fr | number = 0) {
return new TxContext(false, false, false, ContractDeploymentData.empty(), new Fr(chainId), new Fr(version));
}
isEmpty(): boolean {
return (
!this.isFeePaymentTx &&
!this.isRebatePaymentTx &&
!this.isContractDeploymentTx &&
this.contractDeploymentData.isEmpty() &&
this.chainId.isZero() &&
this.version.isZero()
);
}
/**
* Create a new instance from a fields dictionary.
* @param fields - The dictionary.
* @returns A new instance.
*/
static from(fields: FieldsOf<TxContext>): TxContext {
return new TxContext(...TxContext.getFields(fields));
}
/**
* Serialize into a field array. Low-level utility.
* @param fields - Object with fields.
* @returns The array.
*/
static getFields(fields: FieldsOf<TxContext>) {
return [
fields.isFeePaymentTx,
fields.isRebatePaymentTx,
fields.isContractDeploymentTx,
fields.contractDeploymentData,
fields.chainId,
fields.version,
] as const;
}
}