Skip to content

Commit

Permalink
create note class
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Mar 15, 2024
1 parent 21eac64 commit 3bd7e77
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class KeyPair {
return keypair;
}

static async randomAsync(): Promise<KeyPair> {
static async random(): Promise<KeyPair> {
return await KeyPair.newAsync(Field.random());
}

Expand Down
53 changes: 53 additions & 0 deletions src/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { BigNumberish } from 'ethers';

import { Field } from './field';
import { hash } from './hash';
import { KeyPair } from './keypair';

export class Note {
public amount: Field;

constructor(
amount: Field | BigNumberish,
public keypair: KeyPair,
public blinding: Field = Field.random()
) {
this.amount = Field.from(amount);
}

static async random(amount?: Field, keypair?: KeyPair, blinding?: Field) {
return new Note(
amount ?? Field.random(),
keypair ?? (await KeyPair.random()),
blinding ?? Field.random()
);
}

static zero() {
return new Note(Field.zero(), KeyPair.zero(), Field.zero());
}

async commitment(): Promise<Field> {
return hash([this.amount, this.keypair.publicKey!, this.blinding]);
}

async commitmentHex(): Promise<string> {
return (await this.commitment()).hex();
}

async nullifier(merkle_index: Field): Promise<Field> {
if (merkle_index === undefined) {
throw new Error('merkle_index is required');
}
const commitment = await this.commitment();
return hash([
commitment,
merkle_index,
await this.keypair.sign([commitment, merkle_index]),
]);
}

async nullifierHex(merkle_index: Field): Promise<string> {
return (await this.nullifier(merkle_index)).hex();
}
}

0 comments on commit 3bd7e77

Please sign in to comment.