Skip to content

Commit

Permalink
refactor: quick account manager refactor (#9357)
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 authored Oct 24, 2024
1 parent 31b9999 commit 648d043
Showing 1 changed file with 15 additions and 32 deletions.
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

0 comments on commit 648d043

Please sign in to comment.