-
Notifications
You must be signed in to change notification settings - Fork 593
Expand file tree
/
Copy pathtx_context.ts
More file actions
104 lines (89 loc) · 3 KB
/
tx_context.ts
File metadata and controls
104 lines (89 loc) · 3 KB
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
import { Fr } from '@aztec/foundation/fields';
import { schemas } from '@aztec/foundation/schemas';
import { BufferReader, FieldReader, serializeToBuffer, serializeToFields } from '@aztec/foundation/serialize';
import { type FieldsOf } from '@aztec/foundation/types';
import { z } from 'zod';
import { TX_CONTEXT_LENGTH } from '../constants.gen.js';
import { GasSettings } from './gas_settings.js';
/**
* Transaction context.
*/
export class TxContext {
public chainId: Fr;
public version: Fr;
constructor(
/** Chain ID of the transaction. Here for replay protection. */
chainId: Fr | number | bigint,
/** Version of the transaction. Here for replay protection. */
version: Fr | number | bigint,
/** Gas limits for this transaction. */
public gasSettings: GasSettings,
) {
this.chainId = new Fr(chainId);
this.version = new Fr(version);
}
static get schema() {
return z
.object({
chainId: schemas.Fr,
version: schemas.Fr,
gasSettings: GasSettings.schema,
})
.transform(TxContext.from);
}
getSize() {
return this.chainId.size + this.version.size + this.gasSettings.getSize();
}
clone() {
return new TxContext(this.chainId, this.version, this.gasSettings.clone());
}
/**
* Serialize as a buffer.
* @returns The buffer.
*/
toBuffer() {
return serializeToBuffer(...TxContext.getFields(this));
}
static fromFields(fields: Fr[] | FieldReader): TxContext {
const reader = FieldReader.asReader(fields);
return new TxContext(reader.readField(), reader.readField(), reader.readObject(GasSettings));
}
toFields(): Fr[] {
const fields = serializeToFields(...TxContext.getFields(this));
if (fields.length !== TX_CONTEXT_LENGTH) {
throw new Error(`Invalid number of fields for TxContext. Expected ${TX_CONTEXT_LENGTH}, got ${fields.length}`);
}
return fields;
}
/**
* 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(Fr.fromBuffer(reader), Fr.fromBuffer(reader), reader.readObject(GasSettings));
}
static empty(chainId: Fr | number = 0, version: Fr | number = 0) {
return new TxContext(new Fr(chainId), new Fr(version), GasSettings.empty());
}
isEmpty(): boolean {
return this.chainId.isZero() && this.version.isZero() && this.gasSettings.isEmpty();
}
/**
* 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.chainId, fields.version, fields.gasSettings] as const;
}
}