Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Aug 15, 2024
1 parent 3d61bdf commit 10eb33e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
8 changes: 8 additions & 0 deletions yarn-project/circuit-types/src/interfaces/pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type CompleteAddress,
type Fq,
type Fr,
type L1_TO_L2_MSG_TREE_HEIGHT,
type PartialAddress,
type Point,
} from '@aztec/circuits.js';
Expand All @@ -19,6 +20,7 @@ import { type L2Block } from '../l2_block.js';
import { type GetUnencryptedLogsResponse, type L1EventPayload, type LogFilter } from '../logs/index.js';
import { type IncomingNotesFilter } from '../notes/incoming_notes_filter.js';
import { type ExtendedNote, type OutgoingNotesFilter, type UniqueNote } from '../notes/index.js';
import { type SiblingPath } from '../sibling_path/sibling_path.js';
import { type NoteProcessorStats } from '../stats/stats.js';
import { type SimulatedTx, type Tx, type TxHash, type TxReceipt } from '../tx/index.js';
import { type TxEffect } from '../tx_effect.js';
Expand Down Expand Up @@ -239,6 +241,12 @@ export interface PXE {
*/
getIncomingNotes(filter: IncomingNotesFilter): Promise<UniqueNote[]>;

getL1ToL2MembershipWitness(
contractAddress: AztecAddress,
messageHash: Fr,
secret: Fr,
): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined>;

/**
* Gets outgoing notes of accounts registered in this PXE based on the provided filter.
* @param filter - The filter to apply to the notes.
Expand Down
25 changes: 25 additions & 0 deletions yarn-project/cli/src/cmds/pxe/get_l1_to_l2_message_witness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type AztecAddress, type Fr, createCompatibleClient } from '@aztec/aztec.js';
import { type DebugLogger, type LogFn } from '@aztec/foundation/log';

export async function getL1ToL2MessageWitness(
rpcUrl: string,
contractAddress: AztecAddress,
messageHash: Fr,
secret: Fr,
debugLogger: DebugLogger,
log: LogFn,
) {
const client = await createCompatibleClient(rpcUrl, debugLogger);
const messageWitness = await client.getL1ToL2MembershipWitness(contractAddress, messageHash, secret);

log(
messageWitness === undefined
? `
L1 to L2 Message not found.
`
: `
L1 to L2 message index: ${messageWitness[0]}
L1 to L2 message sibling path: ${messageWitness[1]}
`,
);
}
13 changes: 13 additions & 0 deletions yarn-project/cli/src/cmds/pxe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
logJson,
parseAztecAddress,
parseEthereumAddress,
parseField,
parseFieldFromHexString,
parseOptionalAztecAddress,
parseOptionalInteger,
Expand Down Expand Up @@ -165,6 +166,18 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL
await blockNumber(options.rpcUrl, debugLogger, log);
});

program
.command('get-l1-to-l2-message-witness')
.description('Gets a L1 to L2 message witness.')
.requiredOption('-ca, --contract-address <address>', 'Aztec address of the contract.', parseAztecAddress)
.requiredOption('--message-hash <messageHash>', 'The message hash.', parseField)
.requiredOption('-secret <secret>', 'The secret used to claim the L1 to L2 message', parseField)
.addOption(pxeOption)
.action(async ({ contractAddress, messageHash, secret, rpcUrl }) => {
const { getL1ToL2MessageWitness } = await import('./get_l1_to_l2_message_witness.js');
await getL1ToL2MessageWitness(rpcUrl, contractAddress, messageHash, secret, debugLogger, log);
});

program
.command('get-node-info')
.description('Gets the information of an aztec node at a URL.')
Expand Down
34 changes: 33 additions & 1 deletion yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
type PXE,
type PXEInfo,
type PrivateKernelProver,
type SiblingPath,
SimulatedTx,
SimulationError,
TaggedLog,
Expand All @@ -32,11 +33,12 @@ import {
import {
AztecAddress,
type CompleteAddress,
type L1_TO_L2_MSG_TREE_HEIGHT,
type PartialAddress,
computeContractClassId,
getContractClassFromArtifact,
} from '@aztec/circuits.js';
import { computeNoteHashNonce, siloNullifier } from '@aztec/circuits.js/hash';
import { computeL1ToL2MessageNullifier, computeNoteHashNonce, siloNullifier } from '@aztec/circuits.js/hash';
import {
type ContractArtifact,
type DecodedReturn,
Expand All @@ -56,6 +58,7 @@ import { getCanonicalMultiCallEntrypointAddress } from '@aztec/protocol-contract
import {
type AcirSimulator,
type ExecutionResult,
MessageLoadOracleInputs,
accumulateReturnValues,
collectEnqueuedPublicFunctionCalls,
collectPublicTeardownFunctionCall,
Expand Down Expand Up @@ -355,6 +358,35 @@ export class PXEService implements PXE {
return Promise.all(extendedNotes);
}

async getL1ToL2MembershipWitness(
contractAddress: AztecAddress,
messageHash: Fr,
secret: Fr,
): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
let nullifierIndex: bigint | undefined;
let messageIndex = 0n;
let startIndex = 0n;
let siblingPath: SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>;

// We iterate over messages until we find one whose nullifier is not in the nullifier tree --> we need to check
// for nullifiers because messages can have duplicates.
do {
const response = await this.node.getL1ToL2MessageMembershipWitness('latest', messageHash, startIndex);
if (!response) {
throw new Error(`No non-nullified L1 to L2 message found for message hash ${messageHash.toString()}`);
}
[messageIndex, siblingPath] = response;

const messageNullifier = computeL1ToL2MessageNullifier(contractAddress, messageHash, secret, messageIndex);

nullifierIndex = await this.node.findLeafIndex('latest', MerkleTreeId.NULLIFIER_TREE, messageNullifier);

startIndex = messageIndex + 1n;
} while (nullifierIndex !== undefined);

return [messageIndex, siblingPath];
}

public async addNote(note: ExtendedNote, scope?: AztecAddress) {
const owner = await this.db.getCompleteAddress(note.owner);
if (!owner) {
Expand Down

0 comments on commit 10eb33e

Please sign in to comment.