-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tagged note structure (#4843)
Fixes #4572
- Loading branch information
Showing
8 changed files
with
141 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './encrypt_buffer.js'; | ||
export * from './note.js'; | ||
export * from './l1_note_payload.js'; | ||
export * from './tagged_note.js'; |
41 changes: 41 additions & 0 deletions
41
yarn-project/circuit-types/src/logs/l1_note_payload/tagged_note.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Grumpkin } from '@aztec/circuits.js/barretenberg'; | ||
import { GrumpkinScalar, Point } from '@aztec/foundation/fields'; | ||
|
||
import { L1NotePayload } from './l1_note_payload.js'; | ||
import { TaggedNote } from './tagged_note.js'; | ||
|
||
describe('L1 Note Payload', () => { | ||
let grumpkin: Grumpkin; | ||
|
||
beforeAll(() => { | ||
grumpkin = new Grumpkin(); | ||
}); | ||
|
||
it('convert to and from buffer', () => { | ||
const payload = L1NotePayload.random(); | ||
const taggedNote = new TaggedNote(payload); | ||
const buf = taggedNote.toBuffer(); | ||
expect(TaggedNote.fromBuffer(buf).notePayload).toEqual(taggedNote.notePayload); | ||
}); | ||
|
||
it('convert to and from encrypted buffer', () => { | ||
const payload = L1NotePayload.random(); | ||
const taggedNote = new TaggedNote(payload); | ||
const ownerPrivKey = GrumpkinScalar.random(); | ||
const ownerPubKey = grumpkin.mul(Grumpkin.generator, ownerPrivKey); | ||
const encrypted = taggedNote.toEncryptedBuffer(ownerPubKey, grumpkin); | ||
const decrypted = TaggedNote.fromEncryptedBuffer(encrypted, ownerPrivKey, grumpkin); | ||
expect(decrypted).not.toBeUndefined(); | ||
expect(decrypted?.notePayload).toEqual(payload); | ||
}); | ||
|
||
it('return undefined if unable to decrypt the encrypted buffer', () => { | ||
const payload = L1NotePayload.random(); | ||
const taggedNote = new TaggedNote(payload); | ||
const ownerPubKey = Point.random(); | ||
const encrypted = taggedNote.toEncryptedBuffer(ownerPubKey, grumpkin); | ||
const randomPrivKey = GrumpkinScalar.random(); | ||
const decrypted = TaggedNote.fromEncryptedBuffer(encrypted, randomPrivKey, grumpkin); | ||
expect(decrypted).toBeUndefined(); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
yarn-project/circuit-types/src/logs/l1_note_payload/tagged_note.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { GrumpkinPrivateKey, PublicKey } from '@aztec/circuits.js'; | ||
import { Grumpkin } from '@aztec/circuits.js/barretenberg'; | ||
import { Fr } from '@aztec/foundation/fields'; | ||
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; | ||
|
||
import { L1NotePayload } from './l1_note_payload.js'; | ||
|
||
// placeholder value until tagging is implemented | ||
const PLACEHOLDER_TAG = new Fr(33); | ||
|
||
/** | ||
* Encrypted note payload with a tag used for retrieval by clients. | ||
*/ | ||
export class TaggedNote { | ||
constructor(public notePayload: L1NotePayload, public tag = PLACEHOLDER_TAG) {} | ||
|
||
/** | ||
* Deserializes the TaggedNote object from a Buffer. | ||
* @param buffer - Buffer or BufferReader object to deserialize. | ||
* @returns An instance of TaggedNote. | ||
*/ | ||
static fromBuffer(buffer: Buffer | BufferReader): TaggedNote { | ||
const reader = BufferReader.asReader(buffer); | ||
const tag = Fr.fromBuffer(reader); | ||
const payload = L1NotePayload.fromBuffer(reader); | ||
return new TaggedNote(payload, tag); | ||
} | ||
|
||
/** | ||
* Serializes the TaggedNote object into a Buffer. | ||
* @returns Buffer representation of the TaggedNote object (unencrypted). | ||
*/ | ||
public toBuffer(): Buffer { | ||
return serializeToBuffer(this.tag, this.notePayload); | ||
} | ||
|
||
/** | ||
* Encrypt the L1NotePayload object using the owner's public key and the ephemeral private key, then attach the tag. | ||
* @param ownerPubKey - Public key of the owner of the TaggedNote object. | ||
* @param curve - The curve instance to use. | ||
* @returns The encrypted TaggedNote object. | ||
*/ | ||
public toEncryptedBuffer(ownerPubKey: PublicKey, curve: Grumpkin): Buffer { | ||
const encryptedL1NotePayload = this.notePayload.toEncryptedBuffer(ownerPubKey, curve); | ||
return serializeToBuffer(this.tag, encryptedL1NotePayload); | ||
} | ||
|
||
/** | ||
* Decrypts the L1NotePayload object using the owner's private key. | ||
* @param data - Encrypted TaggedNote object. | ||
* @param ownerPrivKey - Private key of the owner of the TaggedNote object. | ||
* @param curve - The curve instance to use. | ||
* @returns Instance of TaggedNote if the decryption was successful, undefined otherwise. | ||
*/ | ||
static fromEncryptedBuffer(data: Buffer, ownerPrivKey: GrumpkinPrivateKey, curve: Grumpkin): TaggedNote | undefined { | ||
const reader = BufferReader.asReader(data); | ||
const tag = Fr.fromBuffer(reader); | ||
|
||
const encryptedL1NotePayload = reader.readToEnd(); | ||
|
||
const payload = L1NotePayload.fromEncryptedBuffer(encryptedL1NotePayload, ownerPrivKey, curve); | ||
if (!payload) { | ||
return; | ||
} | ||
return new TaggedNote(payload, tag); | ||
} | ||
|
||
static random(): TaggedNote { | ||
return new TaggedNote(L1NotePayload.random()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters