Skip to content

Commit

Permalink
refactor: using PublicKey type alias in more places (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Jul 24, 2023
1 parent 8f5bf88 commit 115f490
Show file tree
Hide file tree
Showing 32 changed files with 92 additions and 88 deletions.
8 changes: 4 additions & 4 deletions yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PartialContractAddress } from '@aztec/circuits.js';
import { PartialContractAddress, PublicKey } from '@aztec/circuits.js';
import { FunctionAbi } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';

import { CommitmentsDB } from '../index.js';

Expand Down Expand Up @@ -61,8 +61,8 @@ export interface CommitmentDataOracleInputs {
* The database oracle interface.
*/
export interface DBOracle extends CommitmentsDB {
getPublicKey(address: AztecAddress): Promise<[Point, PartialContractAddress]>;
getSecretKey(contractAddress: AztecAddress, pubKey: Point): Promise<Buffer>;
getPublicKey(address: AztecAddress): Promise<[PublicKey, PartialContractAddress]>;
getSecretKey(contractAddress: AztecAddress, pubKey: PublicKey): Promise<Buffer>;
getNotes(contractAddress: AztecAddress, storageSlot: Fr): Promise<NoteData[]>;
getFunctionABI(contractAddress: AztecAddress, functionSelector: Buffer): Promise<FunctionAbi>;
getPortalContractAddress(contractAddress: AztecAddress): Promise<EthAddress>;
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PublicKey,
} from '@aztec/circuits.js';
import { FunctionType, encodeArguments } from '@aztec/foundation/abi';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import {
AztecNode,
Expand Down Expand Up @@ -158,9 +158,9 @@ export class AztecRPCServer implements AztecRPC {
* Throws an error if the account is not found in the key store.
*
* @param address - The AztecAddress instance representing the account to get public key for.
* @returns A Promise resolving to the Point instance representing the public key.
* @returns A Promise resolving to the PublicKey instance representing the public key.
*/
public async getPublicKey(address: AztecAddress): Promise<Point> {
public async getPublicKey(address: AztecAddress): Promise<PublicKey> {
const result = await this.db.getPublicKeyAndPartialAddress(address);
if (!result) {
throw new Error(`Unable to retrieve public key for address ${address.toString()}`);
Expand All @@ -173,9 +173,9 @@ export class AztecRPCServer implements AztecRPC {
* Throws an error if the account is not found in the key store.
*
* @param address - The AztecAddress instance representing the account to get public key and partial address for.
* @returns A Promise resolving to the Point instance representing the public key.
* @returns A Promise resolving to the PublicKey instance representing the public key.
*/
public async getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[Point, PartialContractAddress]> {
public async getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[PublicKey, PartialContractAddress]> {
const result = await this.db.getPublicKeyAndPartialAddress(address);
if (!result) {
throw new Error(`Unable to get public key for address ${address.toString()}`);
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/aztec-rpc/src/database/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PartialContractAddress } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';
import { ContractDatabase, MerkleTreeId, PublicKey, TxHash } from '@aztec/types';

import { NoteSpendingInfoDao } from './note_spending_info_dao.js';
Expand All @@ -19,7 +19,7 @@ export interface Database extends ContractDatabase {
getNoteSpendingInfo(contract: AztecAddress, storageSlot: Fr): Promise<NoteSpendingInfoDao[]>;
addNoteSpendingInfo(noteSpendingInfoDao: NoteSpendingInfoDao): Promise<void>;
addNoteSpendingInfoBatch(noteSpendingInfoDaos: NoteSpendingInfoDao[]): Promise<void>;
removeNullifiedNoteSpendingInfo(nullifiers: Fr[], account: Point): Promise<NoteSpendingInfoDao[]>;
removeNullifiedNoteSpendingInfo(nullifiers: Fr[], account: PublicKey): Promise<NoteSpendingInfoDao[]>;

getTreeRoots(): Record<MerkleTreeId, Fr>;
setTreeRoots(roots: Record<MerkleTreeId, Fr>): Promise<void>;
Expand All @@ -29,6 +29,6 @@ export interface Database extends ContractDatabase {
publicKey: PublicKey,
partialAddress: PartialContractAddress,
): Promise<void>;
getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[Point, PartialContractAddress] | undefined>;
getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[PublicKey, PartialContractAddress] | undefined>;
getAccounts(): Promise<AztecAddress[]>;
}
10 changes: 5 additions & 5 deletions yarn-project/aztec-rpc/src/database/memory_db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PartialContractAddress } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { MerkleTreeId, PublicKey, TxHash } from '@aztec/types';

Expand Down Expand Up @@ -127,10 +127,10 @@ export class MemoryDB extends MemoryContractDatabase implements Database {
* noteSpendingInfoTable with the remaining records. It returns an array of removed NoteSpendingInfoDao instances.
*
* @param nullifiers - An array of Fr instances representing nullifiers to be matched.
* @param account - A Point instance representing the account for which the records are being removed.
* @param account - A PublicKey instance representing the account for which the records are being removed.
* @returns A Promise resolved with an array of removed NoteSpendingInfoDao instances.
*/
public removeNullifiedNoteSpendingInfo(nullifiers: Fr[], account: Point) {
public removeNullifiedNoteSpendingInfo(nullifiers: Fr[], account: PublicKey) {
const nullifierSet = new Set(nullifiers.map(nullifier => nullifier.toString()));
const [remaining, removed] = this.noteSpendingInfoTable.reduce(
(acc: [NoteSpendingInfoDao[], NoteSpendingInfoDao[]], noteSpendingInfo) => {
Expand Down Expand Up @@ -180,14 +180,14 @@ export class MemoryDB extends MemoryContractDatabase implements Database {

addPublicKeyAndPartialAddress(
address: AztecAddress,
publicKey: Point,
publicKey: PublicKey,
partialAddress: PartialContractAddress,
): Promise<void> {
this.publicKeys.set(address.toBigInt(), [publicKey, partialAddress]);
return Promise.resolve();
}

getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[Point, Fr] | undefined> {
getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[PublicKey, Fr] | undefined> {
return Promise.resolve(this.publicKeys.get(address.toBigInt()));
}

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-rpc/src/database/note_spending_info_dao.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AztecAddress, Fr } from '@aztec/circuits.js';
import { AztecAddress, Fr, PublicKey } from '@aztec/circuits.js';
import { Point } from '@aztec/foundation/fields';
import { NotePreimage } from '@aztec/types';

Expand Down Expand Up @@ -39,7 +39,7 @@ export interface NoteSpendingInfoDao {
/**
* The public key that was used to encrypt the data.
*/
publicKey: Point;
publicKey: PublicKey;
}

export const createRandomNoteSpendingInfoDao = ({
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/aztec-rpc/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
EthAddress,
Fr,
PartialContractAddress,
Point,
PrivateHistoricTreeRoots,
PublicKey,
} from '@aztec/circuits.js';
import { siloCommitment } from '@aztec/circuits.js/abis';
import { FunctionAbi } from '@aztec/foundation/abi';
Expand Down Expand Up @@ -37,7 +37,7 @@ export class SimulatorOracle implements DBOracle {
* @returns A Promise that resolves to the secret key as a Buffer.
* @throws An Error if the input address does not match the public key address of the key pair.
*/
getSecretKey(_contractAddress: AztecAddress, pubKey: Point): Promise<Buffer> {
getSecretKey(_contractAddress: AztecAddress, pubKey: PublicKey): Promise<Buffer> {
return this.keyStore.getAccountPrivateKey(pubKey);
}

Expand All @@ -46,7 +46,7 @@ export class SimulatorOracle implements DBOracle {
* @param address - Address to fetch the pubkey for.
* @returns A public key and the corresponding partial contract address, such that the hash of the two resolves to the input address.
*/
async getPublicKey(address: AztecAddress): Promise<[Point, PartialContractAddress]> {
async getPublicKey(address: AztecAddress): Promise<[PublicKey, PartialContractAddress]> {
const result = await this.db.getPublicKeyAndPartialAddress(address);
if (!result) throw new Error(`Unknown public key for address ${address.toString()}`);
return result;
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/aztec.js/src/aztec_rpc_client/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AztecAddress, Fr, PartialContractAddress, Point, PublicKey, TxContext } from '@aztec/circuits.js';
import { AztecAddress, Fr, PartialContractAddress, PublicKey, TxContext } from '@aztec/circuits.js';
import {
AztecRPC,
ContractData,
Expand Down Expand Up @@ -43,7 +43,7 @@ export abstract class BaseWallet implements Wallet {
getAccounts(): Promise<AztecAddress[]> {
return this.rpc.getAccounts();
}
getPublicKey(address: AztecAddress): Promise<Point> {
getPublicKey(address: AztecAddress): Promise<PublicKey> {
return this.rpc.getPublicKey(address);
}
addContracts(contracts: DeployedContract[]): Promise<void> {
Expand Down Expand Up @@ -85,7 +85,7 @@ export abstract class BaseWallet implements Wallet {
getNodeInfo(): Promise<NodeInfo> {
return this.rpc.getNodeInfo();
}
getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[Point, PartialContractAddress]> {
getPublicKeyAndPartialAddress(address: AztecAddress): Promise<[PublicKey, PartialContractAddress]> {
return this.rpc.getPublicKeyAndPartialAddress(address);
}
isAccountSynchronised(account: AztecAddress) {
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/aztec.js/src/utils/pub_key.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PublicKey } from '@aztec/circuits.js';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';

import { Point } from '../index.js';
Expand All @@ -7,7 +8,7 @@ import { Point } from '../index.js';
* @param privateKey - The private key.
* @returns The generated public key.
*/
export async function generatePublicKey(privateKey: Buffer): Promise<Point> {
export async function generatePublicKey(privateKey: Buffer): Promise<PublicKey> {
const grumpkin = await Grumpkin.new();
return Point.fromBuffer(grumpkin.mul(grumpkin.generator(), privateKey));
}
16 changes: 10 additions & 6 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
FunctionData,
FunctionLeafPreimage,
NewContractData,
Point,
PublicCallStackItem,
PublicKey,
TxRequest,
Vector,
} from '../index.js';
Expand Down Expand Up @@ -140,11 +140,11 @@ export function computeFunctionLeaf(wasm: IWasmModule, fnLeaf: FunctionLeafPreim
/**
* Computes a function tree root from function leaves.
* @param wasm - A module providing low-level wasm access.
* @param fnLeves - The function leaves to be included in the contract function tree.
* @param fnLeaves - The function leaves to be included in the contract function tree.
* @returns The function tree root.
*/
export function computeFunctionTreeRoot(wasm: IWasmModule, fnLeves: Fr[]) {
const inputVector = serializeBufferArrayToVector(fnLeves.map(fr => fr.toBuffer()));
export function computeFunctionTreeRoot(wasm: IWasmModule, fnLeaves: Fr[]) {
const inputVector = serializeBufferArrayToVector(fnLeaves.map(fr => fr.toBuffer()));
wasm.call('pedersen__init');
const result = wasmSyncCall(wasm, 'abis__compute_function_tree_root', inputVector, 32);
return Fr.fromBuffer(result);
Expand Down Expand Up @@ -185,7 +185,7 @@ export function hashConstructor(
*/
export function computeContractAddress(
wasm: IWasmModule,
deployerPubKey: Point,
deployerPubKey: PublicKey,
contractAddrSalt: Fr,
fnTreeRoot: Fr,
constructorHash: Fr,
Expand Down Expand Up @@ -232,7 +232,11 @@ export function computePartialContractAddress(
* @param constructorHash - The hash of the constructor.
* @returns The partially constructed contract address.
*/
export function computeContractAddressFromPartial(wasm: IWasmModule, pubKey: Point, partialAddress: Fr): AztecAddress {
export function computeContractAddressFromPartial(
wasm: IWasmModule,
pubKey: PublicKey,
partialAddress: Fr,
): AztecAddress {
wasm.call('pedersen__init');
const result = inputBuffersToOutputBuffer(
wasm,
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/circuits.js/src/structs/tx_context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BufferReader } from '@aztec/foundation/serialize';

import { FieldsOf } from '../index.js';
import { FieldsOf, PublicKey } from '../index.js';
import { serializeToBuffer } from '../utils/serialize.js';
import { AztecAddress, EthAddress, Fr, Point } from './index.js';

Expand All @@ -16,8 +16,8 @@ export class ContractDeploymentData {

constructor(
/** Public key of the contract deployer (used when deploying account contracts). */
public deployerPublicKey: Point,
/** Hash of the constuctor verification key. */
public deployerPublicKey: PublicKey,
/** Hash of the constructor verification key. */
public constructorVkHash: Fr,
/** Function tree root. */
public functionTreeRoot: Fr,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/cross_chain/test_harness.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AztecNodeService } from '@aztec/aztec-node';
import { AztecRPCServer } from '@aztec/aztec-rpc';
import { Wallet, computeMessageSecretHash } from '@aztec/aztec.js';
import { AztecAddress, EthAddress, Fr, Point } from '@aztec/circuits.js';
import { AztecAddress, EthAddress, Fr, PublicKey } from '@aztec/circuits.js';
import { DeployL1Contracts } from '@aztec/ethereum';
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { sha256ToField } from '@aztec/foundation/crypto';
Expand Down Expand Up @@ -109,7 +109,7 @@ export class CrossChainTestHarness {
/** Another Aztec Address to use in tests. */
public receiver: AztecAddress,
/** The owners public key. */
public ownerPub: Point,
public ownerPub: PublicKey,
) {}

async generateClaimSecret(): Promise<[Fr, Fr]> {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/e2e_p2p_network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getConfigEnvVars as getRpcConfig,
} from '@aztec/aztec-rpc';
import { ContractDeployer, SentTx } from '@aztec/aztec.js';
import { AztecAddress, CircuitsWasm, Fr, Point, getContractDeploymentInfo } from '@aztec/circuits.js';
import { AztecAddress, CircuitsWasm, Fr, PublicKey, getContractDeploymentInfo } from '@aztec/circuits.js';
import { computeContractAddressFromPartial } from '@aztec/circuits.js/abis';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { DebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('e2e_p2p_network', () => {
aztecRpcServer: AztecRPCServer,
account: AztecAddress,
numTxs: number,
publicKey: Point,
publicKey: PublicKey,
) => {
const txs: SentTx[] = [];
for (let i = 0; i < numTxs; i++) {
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/key-store/src/key_pair.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Curve } from '@aztec/circuits.js/barretenberg';
import { randomBytes } from '@aztec/foundation/crypto';
import { Point } from '@aztec/foundation/fields';
import { KeyPair } from '@aztec/types';
import { KeyPair, PublicKey } from '@aztec/types';

/**
* The ConstantKeyPair class is an implementation of the KeyPair interface, which allows generation and management of a constant public and private key pair. It provides methods for creating a random instance of the key pair, retrieving the public key, getting the private key. This class ensures the persistence and consistency of the generated keys, making it suitable for cryptographic operations where constant key pairs are required.
Expand Down Expand Up @@ -34,11 +34,11 @@ export class ConstantKeyPair implements KeyPair {
return new ConstantKeyPair(publicKey, privateKey);
}

constructor(private publicKey: Point, private privateKey: Buffer) {}
constructor(private publicKey: PublicKey, private privateKey: Buffer) {}

/**
* Retrieve the public key from the KeyPair instance.
* The returned public key is a Point object which represents a point on the elliptic curve secp256k1.
* The returned public key is a PublicKey object which represents a point on the elliptic curve secp256k1.
*
* @returns The public key as an elliptic curve point.
*/
Expand Down
11 changes: 5 additions & 6 deletions yarn-project/key-store/src/test_key_store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Point } from '@aztec/circuits.js';
import { Curve } from '@aztec/circuits.js/barretenberg';
import { KeyPair, KeyStore, PublicKey } from '@aztec/types';

Expand Down Expand Up @@ -37,20 +36,20 @@ export class TestKeyStore implements KeyStore {
* @param curve - The curve to use for generating the public key.
* @returns A promise that resolves to the newly created account's AztecAddress.
*/
public createAccount(): Promise<Point> {
public createAccount(): Promise<PublicKey> {
const keyPair = ConstantKeyPair.random(this.curve);
this.accounts.push(keyPair);
return Promise.resolve(keyPair.getPublicKey());
}

/**
* Retrieves the public addresses of all accounts stored in the TestKeyStore.
* The returned addresses are instances of `AztecAddress` and can be used for subsequent operations
* Retrieves the public keys of all accounts stored in the TestKeyStore.
* The returned addresses are instances of `PublicKey` and can be used for subsequent operations
* such as signing transactions or fetching public/private keys.
*
* @returns A Promise that resolves to an array of AztecAddress instances.
* @returns A Promise that resolves to an array of public keys instances.
*/
public getAccounts() {
public getAccounts(): Promise<PublicKey[]> {
return Promise.resolve(this.accounts.map(a => a.getPublicKey()));
}

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/noir-compiler/src/typegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function generateDeploy(input: ContractAbi) {
/**
* Creates a tx to deploy a new instance of this contract using the specified public key to derive the address.
*/
public static deployWithPublicKey(rpc: AztecRPC, publicKey: Point, ${args}) {
public static deployWithPublicKey(rpc: AztecRPC, publicKey: PublicKey, ${args}) {
return new DeployMethod(publicKey, rpc, ${abiName}, Array.from(arguments).slice(2));
}
`;
Expand Down Expand Up @@ -127,7 +127,7 @@ export function generateType(input: ContractAbi, abiImportPath?: string) {
/* eslint-disable */
import { AztecAddress, Contract, ContractFunctionInteraction, ContractMethod, DeployMethod, Wallet } from '@aztec/aztec.js';
import { Fr, Point } from '@aztec/foundation/fields';
import { AztecRPC } from '@aztec/types';
import { AztecRPC, PublicKey } from '@aztec/types';
import { ContractAbi } from '@aztec/foundation/abi';
${abiImportStatement}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/noir-contracts/src/types/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@aztec/aztec.js';
import { ContractAbi } from '@aztec/foundation/abi';
import { Fr, Point } from '@aztec/foundation/fields';
import { AztecRPC } from '@aztec/types';
import { AztecRPC, PublicKey } from '@aztec/types';

import { ChildContractAbi } from '../artifacts/index.js';

Expand All @@ -38,7 +38,7 @@ export class ChildContract extends Contract {
/**
* Creates a tx to deploy a new instance of this contract using the specified public key to derive the address.
*/
public static deployWithPublicKey(rpc: AztecRPC, publicKey: Point) {
public static deployWithPublicKey(rpc: AztecRPC, publicKey: PublicKey) {
return new DeployMethod(publicKey, rpc, ChildContractAbi, Array.from(arguments).slice(2));
}

Expand Down
Loading

0 comments on commit 115f490

Please sign in to comment.