Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: adding all the (note, nonce) pairs in PXE.addNote and hiding PXE.getNoteNonces #3196

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions yarn-project/aztec.js/src/wallet/base_wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ export abstract class BaseWallet implements Wallet {
addNote(note: ExtendedNote): Promise<void> {
return this.pxe.addNote(note);
}
getNoteNonces(note: ExtendedNote): Promise<Fr[]> {
return this.pxe.getNoteNonces(note);
}
getBlock(number: number): Promise<L2Block | undefined> {
return this.pxe.getBlock(number);
}
Expand Down
70 changes: 39 additions & 31 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fr[]> {
/**
* 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<Fr[]> {
const tx = await this.node.getTx(note.txHash);
if (!tx) {
throw new Error(`Unknown tx: ${note.txHash}`);
Expand Down
8 changes: 0 additions & 8 deletions yarn-project/types/src/interfaces/pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,6 @@ export interface PXE {
*/
addNote(note: ExtendedNote): Promise<void>;

/**
* 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<Fr[]>;

/**
* Get the given block.
* @param number - The block number being requested.
Expand Down