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

refactor: quick account manager refactor #9357

Merged
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
47 changes: 15 additions & 32 deletions yarn-project/aztec.js/src/account_manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { CompleteAddress, type PXE } from '@aztec/circuit-types';
import {
type ContractInstanceWithAddress,
type PublicKeys,
deriveKeys,
getContractInstanceFromDeployParams,
} from '@aztec/circuits.js';
import { type ContractInstanceWithAddress, deriveKeys, getContractInstanceFromDeployParams } from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';

import { type AccountContract } from '../account/contract.js';
Expand Down Expand Up @@ -34,27 +29,26 @@ export class AccountManager {
/** Deployment salt for the account contract. */
public readonly salt: Fr;

// TODO(@spalladino): Does it make sense to have both completeAddress and instance?
private completeAddress?: CompleteAddress;
private instance?: ContractInstanceWithAddress;
private publicKeys?: PublicKeys;
private instance: ContractInstanceWithAddress;

constructor(private pxe: PXE, private secretKey: Fr, private accountContract: AccountContract, salt?: Salt) {
this.salt = salt !== undefined ? new Fr(salt) : Fr.random();
}

protected getPublicKeysHash() {
if (!this.publicKeys) {
this.publicKeys = deriveKeys(this.secretKey).publicKeys;
}
return this.publicKeys.hash();
const { publicKeys } = deriveKeys(secretKey);

this.instance = getContractInstanceFromDeployParams(this.accountContract.getContractArtifact(), {
constructorArgs: this.accountContract.getDeploymentArgs(),
salt: this.salt,
publicKeys,
});
}

protected getPublicKeys() {
if (!this.publicKeys) {
this.publicKeys = deriveKeys(this.secretKey).publicKeys;
}
return this.publicKeys;
return this.instance.publicKeys;
}

protected getPublicKeysHash() {
return this.getPublicKeys().hash();
}

/**
Expand All @@ -73,11 +67,7 @@ export class AccountManager {
* @returns The address, partial address, and encryption public key.
*/
public getCompleteAddress(): CompleteAddress {
if (!this.completeAddress) {
const instance = this.getInstance();
this.completeAddress = CompleteAddress.fromSecretKeyAndInstance(this.secretKey, instance);
}
return this.completeAddress;
return CompleteAddress.fromSecretKeyAndInstance(this.secretKey, this.instance);
}

/**
Expand All @@ -95,13 +85,6 @@ export class AccountManager {
* @returns ContractInstance instance.
*/
public getInstance(): ContractInstanceWithAddress {
if (!this.instance) {
this.instance = getContractInstanceFromDeployParams(this.accountContract.getContractArtifact(), {
constructorArgs: this.accountContract.getDeploymentArgs(),
salt: this.salt,
publicKeys: this.getPublicKeys(),
});
}
return this.instance;
}

Expand Down
Loading