Skip to content

Commit

Permalink
feat: expose getL2ToL1Membership on the pxe (#11215)
Browse files Browse the repository at this point in the history
This PR adds a new API to the PXE to calculate the membership witness
for an L2 to L1 message by forwarding the request to a node (this
obviously leaks privacy). This API is necessary in order to complete a
withdrawal back to L1.
  • Loading branch information
alexghr authored Jan 15, 2025
1 parent 52b3a87 commit ffd3625
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
type L2AmountClaimWithRecipient,
type L2Claim,
type WrappedFieldLike,
type IntentAction,
} from './utils/index.js';

export { NoteSelector } from '@aztec/foundation/abi';
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec.js/src/wallet/base_wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,7 @@ export abstract class BaseWallet implements Wallet {
): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>]> {
return this.pxe.getL1ToL2MembershipWitness(contractAddress, messageHash, secret);
}
getL2ToL1MembershipWitness(blockNumber: number, l2Tol1Message: Fr): Promise<[bigint, SiblingPath<number>]> {
return this.pxe.getL2ToL1MembershipWitness(blockNumber, l2Tol1Message);
}
}
10 changes: 10 additions & 0 deletions yarn-project/circuit-types/src/interfaces/pxe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ describe('PXESchema', () => {
expect(result).toEqual([expect.any(BigInt), expect.any(SiblingPath)]);
});

it('getL2ToL1MembershipWitness', async () => {
const result = await context.client.getL2ToL1MembershipWitness(42, Fr.random());
expect(result).toEqual([expect.any(BigInt), expect.any(SiblingPath)]);
});

it('addNote', async () => {
await context.client.addNote(ExtendedNote.random(), address);
});
Expand Down Expand Up @@ -423,6 +428,11 @@ class MockPXE implements PXE {
expect(secret).toBeInstanceOf(Fr);
return Promise.resolve([1n, SiblingPath.random(L1_TO_L2_MSG_TREE_HEIGHT)]);
}
getL2ToL1MembershipWitness(blockNumber: number, l2Tol1Message: Fr): Promise<[bigint, SiblingPath<number>]> {
expect(typeof blockNumber).toEqual('number');
expect(l2Tol1Message).toBeInstanceOf(Fr);
return Promise.resolve([1n, SiblingPath.random<number>(4)]);
}
addNote(note: ExtendedNote, scope?: AztecAddress | undefined): Promise<void> {
expect(note).toBeInstanceOf(ExtendedNote);
expect(scope).toEqual(this.address);
Expand Down
12 changes: 12 additions & 0 deletions yarn-project/circuit-types/src/interfaces/pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ export interface PXE {
secret: Fr,
): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>]>;

/**
* Gets the membership witness for a message that was emitted at a particular block
* @param blockNumber - The block number in which to search for the message
* @param l2Tol1Message - The message to search for
* @returns The membership witness for the message
*/
getL2ToL1MembershipWitness(blockNumber: number, l2Tol1Message: Fr): Promise<[bigint, SiblingPath<number>]>;

/**
* Adds a note to the database.
* @throws If the note hash of the note doesn't exist in the tree.
Expand Down Expand Up @@ -488,6 +496,10 @@ export const PXESchema: ApiSchemaFor<PXE> = {
.function()
.args(schemas.AztecAddress, schemas.Fr, schemas.Fr)
.returns(z.tuple([schemas.BigInt, SiblingPath.schemaFor(L1_TO_L2_MSG_TREE_HEIGHT)])),
getL2ToL1MembershipWitness: z
.function()
.args(z.number(), schemas.Fr)
.returns(z.tuple([schemas.BigInt, SiblingPath.schema])),
addNote: z.function().args(ExtendedNote.schema, optional(schemas.AztecAddress)).returns(z.void()),
addNullifiedNote: z.function().args(ExtendedNote.schema).returns(z.void()),
getBlock: z
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ export class PXEService implements PXE {
return await getNonNullifiedL1ToL2MessageWitness(this.node, contractAddress, messageHash, secret);
}

public getL2ToL1MembershipWitness(blockNumber: number, l2Tol1Message: Fr): Promise<[bigint, SiblingPath<number>]> {
return this.node.getL2ToL1MessageMembershipWitness(blockNumber, l2Tol1Message);
}

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

0 comments on commit ffd3625

Please sign in to comment.