@aztec/stdlib 0.79.0 → 0.81.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/abi/abi.d.ts +7 -5
- package/dest/abi/abi.d.ts.map +1 -1
- package/dest/abi/abi.js +8 -3
- package/dest/abi/contract_artifact.d.ts +16 -1
- package/dest/abi/contract_artifact.d.ts.map +1 -1
- package/dest/abi/contract_artifact.js +72 -11
- package/dest/avm/avm.d.ts +173 -405
- package/dest/avm/avm.d.ts.map +1 -1
- package/dest/avm/avm.js +34 -27
- package/dest/avm/avm_proving_request.d.ts +84 -186
- package/dest/avm/avm_proving_request.d.ts.map +1 -1
- package/dest/avm/public_data_write.d.ts +1 -0
- package/dest/avm/public_data_write.d.ts.map +1 -1
- package/dest/avm/public_data_write.js +3 -0
- package/dest/block/body.d.ts +1 -0
- package/dest/block/body.d.ts.map +1 -1
- package/dest/block/body.js +3 -0
- package/dest/block/index.d.ts +1 -0
- package/dest/block/index.d.ts.map +1 -1
- package/dest/block/index.js +1 -0
- package/dest/block/l2_block.d.ts +2 -1
- package/dest/block/l2_block.d.ts.map +1 -1
- package/dest/block/l2_block.js +3 -0
- package/dest/block/l2_block_downloader/l2_block_stream.d.ts +3 -3
- package/dest/block/l2_block_downloader/l2_block_stream.d.ts.map +1 -1
- package/dest/block/l2_block_downloader/l2_block_stream.js +2 -2
- package/dest/block/l2_block_source.d.ts +3 -0
- package/dest/block/l2_block_source.d.ts.map +1 -1
- package/dest/block/published_l2_block.d.ts +89 -0
- package/dest/block/published_l2_block.d.ts.map +1 -0
- package/dest/block/published_l2_block.js +32 -0
- package/dest/contract/contract_class.d.ts.map +1 -1
- package/dest/contract/contract_class.js +2 -0
- package/dest/contract/contract_instance.d.ts +2 -2
- package/dest/contract/contract_instance.d.ts.map +1 -1
- package/dest/contract/contract_instance.js +3 -2
- package/dest/contract/interfaces/contract_data_source.d.ts +3 -9
- package/dest/contract/interfaces/contract_data_source.d.ts.map +1 -1
- package/dest/database-version/version_manager.js +1 -1
- package/dest/interfaces/archiver.d.ts.map +1 -1
- package/dest/interfaces/archiver.js +4 -3
- package/dest/interfaces/aztec-node.d.ts +1 -1
- package/dest/interfaces/aztec-node.d.ts.map +1 -1
- package/dest/interfaces/aztec-node.js +2 -0
- package/dest/interfaces/proving-job.d.ts +106 -208
- package/dest/interfaces/proving-job.d.ts.map +1 -1
- package/dest/logs/contract_class_log.d.ts +1 -0
- package/dest/logs/contract_class_log.d.ts.map +1 -1
- package/dest/logs/contract_class_log.js +3 -0
- package/dest/logs/l1_payload/index.d.ts +1 -1
- package/dest/logs/l1_payload/index.d.ts.map +1 -1
- package/dest/logs/l1_payload/index.js +1 -1
- package/dest/logs/private_log.d.ts +2 -1
- package/dest/logs/private_log.d.ts.map +1 -1
- package/dest/logs/private_log.js +14 -3
- package/dest/logs/public_log.d.ts.map +1 -1
- package/dest/logs/public_log.js +4 -1
- package/dest/logs/tx_scoped_l2_log.d.ts +16 -28
- package/dest/logs/tx_scoped_l2_log.d.ts.map +1 -1
- package/dest/logs/tx_scoped_l2_log.js +28 -19
- package/dest/note/extended_note.d.ts +10 -21
- package/dest/note/extended_note.d.ts.map +1 -1
- package/dest/note/extended_note.js +19 -28
- package/dest/note/notes_filter.d.ts +2 -2
- package/dest/note/notes_filter.d.ts.map +1 -1
- package/dest/note/notes_filter.js +1 -1
- package/dest/p2p/consensus_payload.d.ts +3 -1
- package/dest/p2p/consensus_payload.d.ts.map +1 -1
- package/dest/p2p/consensus_payload.js +3 -0
- package/dest/proofs/proof.d.ts +0 -1
- package/dest/proofs/proof.d.ts.map +1 -1
- package/dest/proofs/proof.js +2 -7
- package/dest/rollup/block_root_rollup.d.ts +1 -1
- package/dest/tests/factories.d.ts +3 -2
- package/dest/tests/factories.d.ts.map +1 -1
- package/dest/tests/factories.js +14 -8
- package/dest/tests/mocks.d.ts +2 -2
- package/dest/tests/mocks.d.ts.map +1 -1
- package/dest/tests/mocks.js +5 -5
- package/dest/trees/nullifier_membership_witness.d.ts +7 -7
- package/dest/trees/public_data_witness.d.ts +7 -7
- package/dest/tx/capsule.d.ts +2 -0
- package/dest/tx/capsule.d.ts.map +1 -1
- package/dest/tx/capsule.js +2 -0
- package/dest/tx/tx_effect.d.ts +1 -0
- package/dest/tx/tx_effect.d.ts.map +1 -1
- package/dest/tx/tx_effect.js +4 -1
- package/package.json +6 -6
- package/src/abi/abi.ts +16 -9
- package/src/abi/contract_artifact.ts +87 -10
- package/src/avm/avm.ts +42 -39
- package/src/avm/public_data_write.ts +4 -0
- package/src/block/body.ts +6 -0
- package/src/block/index.ts +1 -0
- package/src/block/l2_block.ts +4 -0
- package/src/block/l2_block_downloader/l2_block_stream.ts +5 -5
- package/src/block/l2_block_source.ts +4 -0
- package/src/block/published_l2_block.ts +45 -0
- package/src/contract/contract_class.ts +2 -0
- package/src/contract/contract_instance.ts +13 -5
- package/src/contract/interfaces/contract_data_source.ts +3 -10
- package/src/database-version/version_manager.ts +1 -1
- package/src/interfaces/archiver.ts +9 -6
- package/src/interfaces/aztec-node.ts +4 -1
- package/src/logs/contract_class_log.ts +8 -0
- package/src/logs/l1_payload/index.ts +1 -1
- package/src/logs/private_log.ts +13 -3
- package/src/logs/public_log.ts +11 -1
- package/src/logs/tx_scoped_l2_log.ts +28 -25
- package/src/note/extended_note.ts +17 -36
- package/src/note/notes_filter.ts +3 -3
- package/src/p2p/consensus_payload.ts +9 -0
- package/src/proofs/proof.ts +2 -8
- package/src/tests/factories.ts +15 -14
- package/src/tests/mocks.ts +5 -9
- package/src/tx/capsule.ts +2 -0
- package/src/tx/tx_effect.ts +23 -1
- package/dest/logs/l1_payload/l1_note_payload.d.ts +0 -82
- package/dest/logs/l1_payload/l1_note_payload.d.ts.map +0 -1
- package/dest/logs/l1_payload/l1_note_payload.js +0 -129
- package/src/logs/l1_payload/l1_note_payload.ts +0 -182
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
-
import { type Fq, Fr } from '@aztec/foundation/fields';
|
|
4
|
-
import { BufferReader } from '@aztec/foundation/serialize';
|
|
5
|
-
import { NoteSelector } from '../../abi/note_selector.js';
|
|
6
|
-
import { AztecAddress } from '../../aztec-address/index.js';
|
|
7
|
-
import type { PrivateLog } from '../private_log.js';
|
|
8
|
-
import { PublicLog } from '../public_log.js';
|
|
9
|
-
/**
|
|
10
|
-
* A class which wraps note data which is pushed on L1.
|
|
11
|
-
* @remarks This data is required to compute a nullifier/to spend a note. Along with that this class contains
|
|
12
|
-
* the necessary functionality to encrypt and decrypt the data.
|
|
13
|
-
*/
|
|
14
|
-
export declare class L1NotePayload {
|
|
15
|
-
/**
|
|
16
|
-
* Address of the contract this tx is interacting with.
|
|
17
|
-
*/
|
|
18
|
-
contractAddress: AztecAddress;
|
|
19
|
-
/**
|
|
20
|
-
* Storage slot of the underlying note.
|
|
21
|
-
*/
|
|
22
|
-
storageSlot: Fr;
|
|
23
|
-
/**
|
|
24
|
-
* Type identifier for the underlying note, required to determine how to compute its hash and nullifier.
|
|
25
|
-
*/
|
|
26
|
-
noteTypeId: NoteSelector;
|
|
27
|
-
/**
|
|
28
|
-
* Note values delivered encrypted.
|
|
29
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
30
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
31
|
-
*/
|
|
32
|
-
privateNoteValues: Fr[];
|
|
33
|
-
/**
|
|
34
|
-
* Note values delivered in plaintext.
|
|
35
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
36
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
37
|
-
*/
|
|
38
|
-
publicNoteValues: Fr[];
|
|
39
|
-
constructor(
|
|
40
|
-
/**
|
|
41
|
-
* Address of the contract this tx is interacting with.
|
|
42
|
-
*/
|
|
43
|
-
contractAddress: AztecAddress,
|
|
44
|
-
/**
|
|
45
|
-
* Storage slot of the underlying note.
|
|
46
|
-
*/
|
|
47
|
-
storageSlot: Fr,
|
|
48
|
-
/**
|
|
49
|
-
* Type identifier for the underlying note, required to determine how to compute its hash and nullifier.
|
|
50
|
-
*/
|
|
51
|
-
noteTypeId: NoteSelector,
|
|
52
|
-
/**
|
|
53
|
-
* Note values delivered encrypted.
|
|
54
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
55
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
56
|
-
*/
|
|
57
|
-
privateNoteValues: Fr[],
|
|
58
|
-
/**
|
|
59
|
-
* Note values delivered in plaintext.
|
|
60
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
61
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
62
|
-
*/
|
|
63
|
-
publicNoteValues: Fr[]);
|
|
64
|
-
static fromIncomingBodyPlaintextContractAndPublicValues(plaintext: Buffer, contractAddress: AztecAddress, publicNoteValues: Fr[]): L1NotePayload | undefined;
|
|
65
|
-
static decryptAsIncoming(log: PrivateLog, sk: Fq): Promise<L1NotePayload | undefined>;
|
|
66
|
-
static decryptAsIncomingFromPublic(log: PublicLog, sk: Fq): Promise<L1NotePayload | undefined>;
|
|
67
|
-
/**
|
|
68
|
-
* Serializes the L1NotePayload object into a Buffer.
|
|
69
|
-
* @returns Buffer representation of the L1NotePayload object.
|
|
70
|
-
*/
|
|
71
|
-
toIncomingBodyPlaintext(): Buffer;
|
|
72
|
-
/**
|
|
73
|
-
* Create a random L1NotePayload object (useful for testing purposes).
|
|
74
|
-
* @param contract - The address of a contract the note was emitted from.
|
|
75
|
-
* @returns A random L1NotePayload object.
|
|
76
|
-
*/
|
|
77
|
-
static random(contract?: AztecAddress): Promise<L1NotePayload>;
|
|
78
|
-
equals(other: L1NotePayload): boolean;
|
|
79
|
-
toBuffer(): Buffer;
|
|
80
|
-
static fromBuffer(buffer: Buffer | BufferReader): L1NotePayload;
|
|
81
|
-
}
|
|
82
|
-
//# sourceMappingURL=l1_note_payload.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"l1_note_payload.d.ts","sourceRoot":"","sources":["../../../src/logs/l1_payload/l1_note_payload.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAqB,MAAM,6BAA6B,CAAC;AAE9E,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAG7C;;;;GAIG;AACH,qBAAa,aAAa;IAEtB;;OAEG;IACI,eAAe,EAAE,YAAY;IACpC;;OAEG;IACI,WAAW,EAAE,EAAE;IACtB;;OAEG;IACI,UAAU,EAAE,YAAY;IAC/B;;;;OAIG;IACI,iBAAiB,EAAE,EAAE,EAAE;IAC9B;;;;OAIG;IACI,gBAAgB,EAAE,EAAE,EAAE;;IAvB7B;;OAEG;IACI,eAAe,EAAE,YAAY;IACpC;;OAEG;IACI,WAAW,EAAE,EAAE;IACtB;;OAEG;IACI,UAAU,EAAE,YAAY;IAC/B;;;;OAIG;IACI,iBAAiB,EAAE,EAAE,EAAE;IAC9B;;;;OAIG;IACI,gBAAgB,EAAE,EAAE,EAAE;IAG/B,MAAM,CAAC,gDAAgD,CACrD,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,YAAY,EAC7B,gBAAgB,EAAE,EAAE,EAAE,GACrB,aAAa,GAAG,SAAS;WAgBf,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;WAa9E,2BAA2B,CAAC,GAAG,EAAE,SAAS,EAAE,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;IAkBpG;;;OAGG;IACH,uBAAuB;IAKvB;;;;OAIG;WACU,MAAM,CAAC,QAAQ,CAAC,EAAE,YAAY;IAgBpC,MAAM,CAAC,KAAK,EAAE,aAAa;IAUlC,QAAQ;IAUR,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;CAUhD"}
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { randomInt } from '@aztec/foundation/crypto';
|
|
2
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
-
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
4
|
-
import { NoteSelector } from '../../abi/note_selector.js';
|
|
5
|
-
import { AztecAddress } from '../../aztec-address/index.js';
|
|
6
|
-
import { Vector } from '../../types/index.js';
|
|
7
|
-
import { EncryptedLogPayload } from './encrypted_log_payload.js';
|
|
8
|
-
/**
|
|
9
|
-
* A class which wraps note data which is pushed on L1.
|
|
10
|
-
* @remarks This data is required to compute a nullifier/to spend a note. Along with that this class contains
|
|
11
|
-
* the necessary functionality to encrypt and decrypt the data.
|
|
12
|
-
*/ export class L1NotePayload {
|
|
13
|
-
contractAddress;
|
|
14
|
-
storageSlot;
|
|
15
|
-
noteTypeId;
|
|
16
|
-
privateNoteValues;
|
|
17
|
-
publicNoteValues;
|
|
18
|
-
constructor(/**
|
|
19
|
-
* Address of the contract this tx is interacting with.
|
|
20
|
-
*/ contractAddress, /**
|
|
21
|
-
* Storage slot of the underlying note.
|
|
22
|
-
*/ storageSlot, /**
|
|
23
|
-
* Type identifier for the underlying note, required to determine how to compute its hash and nullifier.
|
|
24
|
-
*/ noteTypeId, /**
|
|
25
|
-
* Note values delivered encrypted.
|
|
26
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
27
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
28
|
-
*/ privateNoteValues, /**
|
|
29
|
-
* Note values delivered in plaintext.
|
|
30
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
31
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
32
|
-
*/ publicNoteValues){
|
|
33
|
-
this.contractAddress = contractAddress;
|
|
34
|
-
this.storageSlot = storageSlot;
|
|
35
|
-
this.noteTypeId = noteTypeId;
|
|
36
|
-
this.privateNoteValues = privateNoteValues;
|
|
37
|
-
this.publicNoteValues = publicNoteValues;
|
|
38
|
-
}
|
|
39
|
-
static fromIncomingBodyPlaintextContractAndPublicValues(plaintext, contractAddress, publicNoteValues) {
|
|
40
|
-
try {
|
|
41
|
-
const reader = BufferReader.asReader(plaintext);
|
|
42
|
-
const fields = reader.readArray(plaintext.length / Fr.SIZE_IN_BYTES, Fr);
|
|
43
|
-
const storageSlot = fields[0];
|
|
44
|
-
const noteTypeId = NoteSelector.fromField(fields[1]);
|
|
45
|
-
const privateNoteValues = fields.slice(2);
|
|
46
|
-
return new L1NotePayload(contractAddress, storageSlot, noteTypeId, privateNoteValues, publicNoteValues);
|
|
47
|
-
} catch (e) {
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
static async decryptAsIncoming(log, sk) {
|
|
52
|
-
const decryptedLog = await EncryptedLogPayload.decryptAsIncoming(log.fields, sk);
|
|
53
|
-
if (!decryptedLog) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
return this.fromIncomingBodyPlaintextContractAndPublicValues(decryptedLog.incomingBodyPlaintext, decryptedLog.contractAddress, /* publicValues */ []);
|
|
57
|
-
}
|
|
58
|
-
static async decryptAsIncomingFromPublic(log, sk) {
|
|
59
|
-
const { privateValues, publicValues } = parseLogFromPublic(log);
|
|
60
|
-
if (!privateValues) {
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
const decryptedLog = await EncryptedLogPayload.decryptAsIncoming(privateValues, sk);
|
|
64
|
-
if (!decryptedLog) {
|
|
65
|
-
return undefined;
|
|
66
|
-
}
|
|
67
|
-
return this.fromIncomingBodyPlaintextContractAndPublicValues(decryptedLog.incomingBodyPlaintext, decryptedLog.contractAddress, publicValues);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Serializes the L1NotePayload object into a Buffer.
|
|
71
|
-
* @returns Buffer representation of the L1NotePayload object.
|
|
72
|
-
*/ toIncomingBodyPlaintext() {
|
|
73
|
-
const fields = [
|
|
74
|
-
this.storageSlot,
|
|
75
|
-
this.noteTypeId.toField(),
|
|
76
|
-
...this.privateNoteValues
|
|
77
|
-
];
|
|
78
|
-
return serializeToBuffer(fields);
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Create a random L1NotePayload object (useful for testing purposes).
|
|
82
|
-
* @param contract - The address of a contract the note was emitted from.
|
|
83
|
-
* @returns A random L1NotePayload object.
|
|
84
|
-
*/ static async random(contract) {
|
|
85
|
-
const numPrivateNoteValues = randomInt(2) + 1;
|
|
86
|
-
const privateNoteValues = Array.from({
|
|
87
|
-
length: numPrivateNoteValues
|
|
88
|
-
}, ()=>Fr.random());
|
|
89
|
-
const numPublicNoteValues = randomInt(2) + 1;
|
|
90
|
-
const publicNoteValues = Array.from({
|
|
91
|
-
length: numPublicNoteValues
|
|
92
|
-
}, ()=>Fr.random());
|
|
93
|
-
return new L1NotePayload(contract ?? await AztecAddress.random(), Fr.random(), NoteSelector.random(), privateNoteValues, publicNoteValues);
|
|
94
|
-
}
|
|
95
|
-
equals(other) {
|
|
96
|
-
return this.contractAddress.equals(other.contractAddress) && this.storageSlot.equals(other.storageSlot) && this.noteTypeId.equals(other.noteTypeId) && this.privateNoteValues.every((value, index)=>value.equals(other.privateNoteValues[index])) && this.publicNoteValues.every((value, index)=>value.equals(other.publicNoteValues[index]));
|
|
97
|
-
}
|
|
98
|
-
toBuffer() {
|
|
99
|
-
return serializeToBuffer(this.contractAddress, this.storageSlot, this.noteTypeId, new Vector(this.privateNoteValues), new Vector(this.publicNoteValues));
|
|
100
|
-
}
|
|
101
|
-
static fromBuffer(buffer) {
|
|
102
|
-
const reader = BufferReader.asReader(buffer);
|
|
103
|
-
return new L1NotePayload(reader.readObject(AztecAddress), reader.readObject(Fr), reader.readObject(NoteSelector), reader.readVector(Fr), reader.readVector(Fr));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Parse the given log into an array of public values and an encrypted log.
|
|
108
|
-
*
|
|
109
|
-
* @param log - Log to be parsed.
|
|
110
|
-
* @returns An object containing the public values, encrypted log, and ciphertext length.
|
|
111
|
-
*/ function parseLogFromPublic(log) {
|
|
112
|
-
// Extract lengths from the log
|
|
113
|
-
// See aztec_nr/aztec/src/macros/note/mod.nr to see how the "finalization_log" is encoded.
|
|
114
|
-
// Each length is stored in 2 bytes with a 0 separator byte between them:
|
|
115
|
-
// [ publicLen[0], publicLen[1], 0, privateLen[0], privateLen[1] ]
|
|
116
|
-
// Search the codebase for "disgusting encoding" to see other hardcoded instances of this encoding, that you might need to change if you ever find yourself here.
|
|
117
|
-
const lengths = log.log[0].toBuffer().subarray(-5);
|
|
118
|
-
const publicValuesLength = lengths.readUint16BE();
|
|
119
|
-
const privateValuesLength = lengths.readUint16BE(3);
|
|
120
|
-
// Now we get the fields corresponding to the values generated from private.
|
|
121
|
-
// Note: +1 for the length values in position 0
|
|
122
|
-
const privateValues = log.log.slice(1, 1 + privateValuesLength);
|
|
123
|
-
// At last we load the public values
|
|
124
|
-
const publicValues = log.log.slice(1 + privateValuesLength, 1 + privateValuesLength + publicValuesLength);
|
|
125
|
-
return {
|
|
126
|
-
publicValues,
|
|
127
|
-
privateValues
|
|
128
|
-
};
|
|
129
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { randomInt } from '@aztec/foundation/crypto';
|
|
2
|
-
import { type Fq, Fr } from '@aztec/foundation/fields';
|
|
3
|
-
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
4
|
-
|
|
5
|
-
import { NoteSelector } from '../../abi/note_selector.js';
|
|
6
|
-
import { AztecAddress } from '../../aztec-address/index.js';
|
|
7
|
-
import { Vector } from '../../types/index.js';
|
|
8
|
-
import type { PrivateLog } from '../private_log.js';
|
|
9
|
-
import { PublicLog } from '../public_log.js';
|
|
10
|
-
import { EncryptedLogPayload } from './encrypted_log_payload.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A class which wraps note data which is pushed on L1.
|
|
14
|
-
* @remarks This data is required to compute a nullifier/to spend a note. Along with that this class contains
|
|
15
|
-
* the necessary functionality to encrypt and decrypt the data.
|
|
16
|
-
*/
|
|
17
|
-
export class L1NotePayload {
|
|
18
|
-
constructor(
|
|
19
|
-
/**
|
|
20
|
-
* Address of the contract this tx is interacting with.
|
|
21
|
-
*/
|
|
22
|
-
public contractAddress: AztecAddress,
|
|
23
|
-
/**
|
|
24
|
-
* Storage slot of the underlying note.
|
|
25
|
-
*/
|
|
26
|
-
public storageSlot: Fr,
|
|
27
|
-
/**
|
|
28
|
-
* Type identifier for the underlying note, required to determine how to compute its hash and nullifier.
|
|
29
|
-
*/
|
|
30
|
-
public noteTypeId: NoteSelector,
|
|
31
|
-
/**
|
|
32
|
-
* Note values delivered encrypted.
|
|
33
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
34
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
35
|
-
*/
|
|
36
|
-
public privateNoteValues: Fr[],
|
|
37
|
-
/**
|
|
38
|
-
* Note values delivered in plaintext.
|
|
39
|
-
* @dev Note that to recreate the correct note we need to merge privateNoteValues and publicNoteValues. To do that
|
|
40
|
-
* we need access to the contract ABI (that is done in the NoteProcessor).
|
|
41
|
-
*/
|
|
42
|
-
public publicNoteValues: Fr[],
|
|
43
|
-
) {}
|
|
44
|
-
|
|
45
|
-
static fromIncomingBodyPlaintextContractAndPublicValues(
|
|
46
|
-
plaintext: Buffer,
|
|
47
|
-
contractAddress: AztecAddress,
|
|
48
|
-
publicNoteValues: Fr[],
|
|
49
|
-
): L1NotePayload | undefined {
|
|
50
|
-
try {
|
|
51
|
-
const reader = BufferReader.asReader(plaintext);
|
|
52
|
-
const fields = reader.readArray(plaintext.length / Fr.SIZE_IN_BYTES, Fr);
|
|
53
|
-
|
|
54
|
-
const storageSlot = fields[0];
|
|
55
|
-
const noteTypeId = NoteSelector.fromField(fields[1]);
|
|
56
|
-
|
|
57
|
-
const privateNoteValues = fields.slice(2);
|
|
58
|
-
|
|
59
|
-
return new L1NotePayload(contractAddress, storageSlot, noteTypeId, privateNoteValues, publicNoteValues);
|
|
60
|
-
} catch (e) {
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
static async decryptAsIncoming(log: PrivateLog, sk: Fq): Promise<L1NotePayload | undefined> {
|
|
66
|
-
const decryptedLog = await EncryptedLogPayload.decryptAsIncoming(log.fields, sk);
|
|
67
|
-
if (!decryptedLog) {
|
|
68
|
-
return undefined;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return this.fromIncomingBodyPlaintextContractAndPublicValues(
|
|
72
|
-
decryptedLog.incomingBodyPlaintext,
|
|
73
|
-
decryptedLog.contractAddress,
|
|
74
|
-
/* publicValues */ [],
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
static async decryptAsIncomingFromPublic(log: PublicLog, sk: Fq): Promise<L1NotePayload | undefined> {
|
|
79
|
-
const { privateValues, publicValues } = parseLogFromPublic(log);
|
|
80
|
-
if (!privateValues) {
|
|
81
|
-
return undefined;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const decryptedLog = await EncryptedLogPayload.decryptAsIncoming(privateValues, sk);
|
|
85
|
-
if (!decryptedLog) {
|
|
86
|
-
return undefined;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return this.fromIncomingBodyPlaintextContractAndPublicValues(
|
|
90
|
-
decryptedLog.incomingBodyPlaintext,
|
|
91
|
-
decryptedLog.contractAddress,
|
|
92
|
-
publicValues,
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Serializes the L1NotePayload object into a Buffer.
|
|
98
|
-
* @returns Buffer representation of the L1NotePayload object.
|
|
99
|
-
*/
|
|
100
|
-
toIncomingBodyPlaintext() {
|
|
101
|
-
const fields = [this.storageSlot, this.noteTypeId.toField(), ...this.privateNoteValues];
|
|
102
|
-
return serializeToBuffer(fields);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Create a random L1NotePayload object (useful for testing purposes).
|
|
107
|
-
* @param contract - The address of a contract the note was emitted from.
|
|
108
|
-
* @returns A random L1NotePayload object.
|
|
109
|
-
*/
|
|
110
|
-
static async random(contract?: AztecAddress) {
|
|
111
|
-
const numPrivateNoteValues = randomInt(2) + 1;
|
|
112
|
-
const privateNoteValues = Array.from({ length: numPrivateNoteValues }, () => Fr.random());
|
|
113
|
-
|
|
114
|
-
const numPublicNoteValues = randomInt(2) + 1;
|
|
115
|
-
const publicNoteValues = Array.from({ length: numPublicNoteValues }, () => Fr.random());
|
|
116
|
-
|
|
117
|
-
return new L1NotePayload(
|
|
118
|
-
contract ?? (await AztecAddress.random()),
|
|
119
|
-
Fr.random(),
|
|
120
|
-
NoteSelector.random(),
|
|
121
|
-
privateNoteValues,
|
|
122
|
-
publicNoteValues,
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public equals(other: L1NotePayload) {
|
|
127
|
-
return (
|
|
128
|
-
this.contractAddress.equals(other.contractAddress) &&
|
|
129
|
-
this.storageSlot.equals(other.storageSlot) &&
|
|
130
|
-
this.noteTypeId.equals(other.noteTypeId) &&
|
|
131
|
-
this.privateNoteValues.every((value, index) => value.equals(other.privateNoteValues[index])) &&
|
|
132
|
-
this.publicNoteValues.every((value, index) => value.equals(other.publicNoteValues[index]))
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
toBuffer() {
|
|
137
|
-
return serializeToBuffer(
|
|
138
|
-
this.contractAddress,
|
|
139
|
-
this.storageSlot,
|
|
140
|
-
this.noteTypeId,
|
|
141
|
-
new Vector(this.privateNoteValues),
|
|
142
|
-
new Vector(this.publicNoteValues),
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
147
|
-
const reader = BufferReader.asReader(buffer);
|
|
148
|
-
return new L1NotePayload(
|
|
149
|
-
reader.readObject(AztecAddress),
|
|
150
|
-
reader.readObject(Fr),
|
|
151
|
-
reader.readObject(NoteSelector),
|
|
152
|
-
reader.readVector(Fr),
|
|
153
|
-
reader.readVector(Fr),
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Parse the given log into an array of public values and an encrypted log.
|
|
160
|
-
*
|
|
161
|
-
* @param log - Log to be parsed.
|
|
162
|
-
* @returns An object containing the public values, encrypted log, and ciphertext length.
|
|
163
|
-
*/
|
|
164
|
-
function parseLogFromPublic(log: PublicLog) {
|
|
165
|
-
// Extract lengths from the log
|
|
166
|
-
// See aztec_nr/aztec/src/macros/note/mod.nr to see how the "finalization_log" is encoded.
|
|
167
|
-
// Each length is stored in 2 bytes with a 0 separator byte between them:
|
|
168
|
-
// [ publicLen[0], publicLen[1], 0, privateLen[0], privateLen[1] ]
|
|
169
|
-
// Search the codebase for "disgusting encoding" to see other hardcoded instances of this encoding, that you might need to change if you ever find yourself here.
|
|
170
|
-
const lengths = log.log[0].toBuffer().subarray(-5);
|
|
171
|
-
const publicValuesLength = lengths.readUint16BE();
|
|
172
|
-
const privateValuesLength = lengths.readUint16BE(3);
|
|
173
|
-
|
|
174
|
-
// Now we get the fields corresponding to the values generated from private.
|
|
175
|
-
// Note: +1 for the length values in position 0
|
|
176
|
-
const privateValues = log.log.slice(1, 1 + privateValuesLength);
|
|
177
|
-
|
|
178
|
-
// At last we load the public values
|
|
179
|
-
const publicValues = log.log.slice(1 + privateValuesLength, 1 + privateValuesLength + publicValuesLength);
|
|
180
|
-
|
|
181
|
-
return { publicValues, privateValues };
|
|
182
|
-
}
|