From 8c41664f3aa1000f08ad36b9bc67dbbd2e8dd71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Thu, 2 Nov 2023 14:11:43 +0100 Subject: [PATCH] feat!: adding all the (note, nonce) pairs in `PXE.addNote` and hiding `PXE.getNoteNonces` (#3196) Changes: 1. Until now we have added only 1 `NoteDao` to a db in the `PXE.addNote` endpoint even when multiple nonces were found. 2. `PXE.getNoteNonces` no longer needs to be exposed so I hide it here. # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist). --- .../aztec.js/src/wallet/base_wallet.ts | 3 - .../pxe/src/pxe_service/pxe_service.ts | 70 +++++++++++-------- yarn-project/types/src/interfaces/pxe.ts | 8 --- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/yarn-project/aztec.js/src/wallet/base_wallet.ts b/yarn-project/aztec.js/src/wallet/base_wallet.ts index 2a1f5235b4e..48e259aeca1 100644 --- a/yarn-project/aztec.js/src/wallet/base_wallet.ts +++ b/yarn-project/aztec.js/src/wallet/base_wallet.ts @@ -80,9 +80,6 @@ export abstract class BaseWallet implements Wallet { addNote(note: ExtendedNote): Promise { return this.pxe.addNote(note); } - getNoteNonces(note: ExtendedNote): Promise { - return this.pxe.getNoteNonces(note); - } getBlock(number: number): Promise { return this.pxe.getBlock(number); } diff --git a/yarn-project/pxe/src/pxe_service/pxe_service.ts b/yarn-project/pxe/src/pxe_service/pxe_service.ts index 8d8a9b69156..44fa8db2e41 100644 --- a/yarn-project/pxe/src/pxe_service/pxe_service.ts +++ b/yarn-project/pxe/src/pxe_service/pxe_service.ts @@ -221,45 +221,53 @@ export class PXEService implements PXE { throw new Error('Unknown account.'); } - const [nonce] = await this.getNoteNonces(note); - if (!nonce) { + const nonces = await this.getNoteNonces(note); + if (nonces.length === 0) { throw new Error(`Cannot find the note in tx: ${note.txHash}.`); } - const { innerNoteHash, siloedNoteHash, uniqueSiloedNoteHash, innerNullifier } = - await this.simulator.computeNoteHashAndNullifier(note.contractAddress, nonce, note.storageSlot, note.note); + for (const nonce of nonces) { + const { innerNoteHash, siloedNoteHash, uniqueSiloedNoteHash, innerNullifier } = + await this.simulator.computeNoteHashAndNullifier(note.contractAddress, nonce, note.storageSlot, note.note); - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386) - // This can always be `uniqueSiloedNoteHash` once notes added from public also include nonces. - const noteHashToLookUp = nonce.isZero() ? siloedNoteHash : uniqueSiloedNoteHash; - const index = await this.node.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, noteHashToLookUp); - if (index === undefined) { - throw new Error('Note does not exist.'); - } + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386) + // This can always be `uniqueSiloedNoteHash` once notes added from public also include nonces. + const noteHashToLookUp = nonce.isZero() ? siloedNoteHash : uniqueSiloedNoteHash; + const index = await this.node.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, noteHashToLookUp); + if (index === undefined) { + throw new Error('Note does not exist.'); + } - const wasm = await CircuitsWasm.get(); - const siloedNullifier = siloNullifier(wasm, note.contractAddress, innerNullifier!); - const nullifierIndex = await this.node.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, siloedNullifier); - if (nullifierIndex !== undefined) { - throw new Error('The note has been destroyed.'); - } + const wasm = await CircuitsWasm.get(); + const siloedNullifier = siloNullifier(wasm, note.contractAddress, innerNullifier!); + const nullifierIndex = await this.node.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, siloedNullifier); + if (nullifierIndex !== undefined) { + throw new Error('The note has been destroyed.'); + } - await this.db.addNote( - new NoteDao( - note.note, - note.contractAddress, - note.storageSlot, - note.txHash, - nonce, - innerNoteHash, - siloedNullifier, - index, - publicKey, - ), - ); + await this.db.addNote( + new NoteDao( + note.note, + note.contractAddress, + note.storageSlot, + note.txHash, + nonce, + innerNoteHash, + siloedNullifier, + index, + publicKey, + ), + ); + } } - public async getNoteNonces(note: ExtendedNote): Promise { + /** + * Finds the nonce(s) for a given note. + * @param note - The note to find the nonces for. + * @returns The nonces of the note. + * @remarks More than a single nonce may be returned since there might be more than one nonce for a given note. + */ + private async getNoteNonces(note: ExtendedNote): Promise { const tx = await this.node.getTx(note.txHash); if (!tx) { throw new Error(`Unknown tx: ${note.txHash}`); diff --git a/yarn-project/types/src/interfaces/pxe.ts b/yarn-project/types/src/interfaces/pxe.ts index 5efef5b63ae..c7dd53a76b0 100644 --- a/yarn-project/types/src/interfaces/pxe.ts +++ b/yarn-project/types/src/interfaces/pxe.ts @@ -177,14 +177,6 @@ export interface PXE { */ addNote(note: ExtendedNote): Promise; - /** - * Finds the nonce(s) for a given note. - * @param note - The note to find the nonces for. - * @returns The nonces of the note. - * @remarks More than a single nonce may be returned since there might be more than one nonce for a given note. - */ - getNoteNonces(note: ExtendedNote): Promise; - /** * Get the given block. * @param number - The block number being requested.