Skip to content
Open
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
83 changes: 80 additions & 3 deletions src/hooks/WalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as bip39 from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { ec as EC } from 'elliptic';
import { getEcdsaKAccount, getEcdsaKWallet } from '@aztec/accounts/ecdsa';
import { getSchnorrAccount, getSchnorrWallet } from '@aztec/accounts/schnorr';
import { getSchnorrAccount, getSchnorrWallet, SchnorrAccountContractArtifact } from '@aztec/accounts/schnorr';
import {
AccountWallet,
AztecAddress,
Expand All @@ -13,12 +13,20 @@ import {
GrumpkinScalar,
Note,
Tx,
TxHash
TxHash,
getContractClassFromArtifact,
deriveKeys
} from '@aztec/aztec.js';
import {
computeInitializationHash,
computeContractClassId,
computeContractAddressFromInstance,
computePartialAddress
} from "@aztec/circuits.js/contract";
import pxe from './PXEService.ts';
import { deriveSigningKey } from '@aztec/circuits.js';
import { SimulatedTx } from '@aztec/circuit-types';
import { FunctionType, type NoteSelector } from '@aztec/foundation/abi';
import { FunctionType, type NoteSelector, FunctionAbi } from '@aztec/foundation/abi';
import { TokenContract, TokenContractArtifact } from '@aztec/noir-contracts.js/Token';
import { Fr as FieldFr } from '@aztec/foundation/fields';
import { addPendingTransaction } from './TransactionService.ts';
Expand Down Expand Up @@ -250,3 +258,72 @@ export const addPendingShieldNoteToPXE = async (constractAddress: string, amount

await wallet.addNote(extendedNote);
}

export const accountsDiscovery = async (walletDeployer: AztecAddress, keyPair: EC.KeyPair): Promise<AztecAddress[]> => {
let salt = 0;
let discovery = true;
let accounts: AztecAddress[] = [];

while (discovery) {
const {contractAddress} = calculateContractHash(walletDeployer, keyPair, salt);
discovery = await pxe.isContractPubliclyDeployed(contractAddress);

if (discovery) {
salt++;
accounts.push(contractAddress);
} else {
localStorage.setItem("salt", salt.toString());
}
}

return accounts;
}


export const calculateContractHash = (deployer: AztecAddress, keyPair: EC.KeyPair, salt: number) => {
const contractClass = getContractClassFromArtifact(SchnorrAccountContractArtifact);
const hashedContractClassId = computeContractClassId(contractClass);
const hashedSalt = new Fr(salt);

const constructorArtifact = SchnorrAccountContractArtifact.functions.find(f => f.name === "constructor");
let constructorAbi: FunctionAbi | undefined;
if (constructorArtifact) {
constructorAbi = {
name: constructorArtifact.name,
parameters: constructorArtifact.parameters,
functionType: constructorArtifact.functionType,
isStatic: constructorArtifact.isStatic,
returnTypes: constructorArtifact.returnTypes,
isInternal: constructorArtifact.isInternal,
isInitializer: constructorArtifact.isInitializer,
};
}
const reduced_public_x = BigInt(keyPair.getPublic().getX().toString()) % Fr.MODULUS;
const reduced_public_y = BigInt(keyPair.getPublic().getY().toString()) % Fr.MODULUS;

const initializationHash = computeInitializationHash(constructorAbi, [reduced_public_x, reduced_public_y]);

const secretKey = Fr.fromString(keyPair.getPrivate('hex').toString());
const publicKeysHash = deriveKeys(secretKey).publicKeys.hash();

const contractAddress = computeContractAddressFromInstance({
version: 1,
contractClassId: hashedContractClassId,
initializationHash: initializationHash,
salt: hashedSalt,
deployer: deployer,
publicKeysHash: publicKeysHash
});

const partialAddress = computePartialAddress({
contractClassId: hashedContractClassId,
initializationHash: initializationHash,
salt: hashedSalt,
deployer: deployer
});

return {
contractAddress,
partialAddress
};
}