-
Notifications
You must be signed in to change notification settings - Fork 596
Expand file tree
/
Copy pathtx_request.ts
More file actions
87 lines (77 loc) · 2.69 KB
/
tx_request.ts
File metadata and controls
87 lines (77 loc) · 2.69 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
import { DomainSeparator, TX_REQUEST_LENGTH } from '@aztec/constants';
import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto/poseidon';
import { Fr } from '@aztec/foundation/curves/bn254';
import { BufferReader, serializeToBuffer, serializeToFields } from '@aztec/foundation/serialize';
import type { FieldsOf } from '@aztec/foundation/types';
import { AztecAddress } from '../aztec-address/index.js';
import { FunctionData } from './function_data.js';
import { TxContext } from './tx_context.js';
/**
* Transaction request.
*/
export class TxRequest {
// docs:start:constructor
constructor(
/** Sender. */
public origin: AztecAddress,
/** Pedersen hash of function arguments. */
public argsHash: Fr,
/** Transaction context. */
public txContext: TxContext,
/** Function data representing the function to call. */
public functionData: FunctionData,
/** A salt to make the hash difficult to predict. The hash is used as the first nullifier if there is no nullifier emitted throughout the tx. */
public salt: Fr,
) {}
// docs:end:constructor
static getFields(fields: FieldsOf<TxRequest>) {
return [fields.origin, fields.argsHash, fields.txContext, fields.functionData, fields.salt] as const;
}
static from(fields: FieldsOf<TxRequest>): TxRequest {
return new TxRequest(...TxRequest.getFields(fields));
}
/**
* Serialize as a buffer.
* @returns The buffer.
*/
toBuffer() {
return serializeToBuffer([...TxRequest.getFields(this)]);
}
toFields(): Fr[] {
const fields = serializeToFields(...TxRequest.getFields(this));
if (fields.length !== TX_REQUEST_LENGTH) {
throw new Error(`Invalid number of fields for TxRequest. Expected ${TX_REQUEST_LENGTH}, got ${fields.length}`);
}
return fields;
}
/**
* Deserializes from a buffer or reader, corresponding to a write in cpp.
* @param buffer - Buffer to read from.
* @returns The deserialized TxRequest object.
*/
static fromBuffer(buffer: Buffer | BufferReader): TxRequest {
const reader = BufferReader.asReader(buffer);
return new TxRequest(
reader.readObject(AztecAddress),
Fr.fromBuffer(reader),
reader.readObject(TxContext),
reader.readObject(FunctionData),
Fr.fromBuffer(reader),
);
}
hash() {
return poseidon2HashWithSeparator(this.toFields(), DomainSeparator.TX_REQUEST);
}
static empty() {
return new TxRequest(AztecAddress.ZERO, Fr.zero(), TxContext.empty(), FunctionData.empty(), Fr.zero());
}
isEmpty() {
return (
this.origin.isZero() &&
this.argsHash.isZero() &&
this.txContext.isEmpty() &&
this.functionData.isEmpty() &&
this.salt.isZero()
);
}
}