diff --git a/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.cpp b/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.cpp index b7ffe3102b5..a441fa43793 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.cpp @@ -30,6 +30,25 @@ WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message, write(output_sig_v, sig.v); } +WASM_EXPORT void ecdsa__construct_signature_(uint8_t const* message_buf, + uint8_t const* private_key, + uint8_t* output_sig_r, + uint8_t* output_sig_s, + uint8_t* output_sig_v) +{ + using serialize::write; + auto priv_key = from_buffer(private_key); + secp256k1::g1::affine_element pub_key = secp256k1::g1::one * priv_key; + ecdsa_key_pair key_pair = { priv_key, pub_key }; + + auto message = from_buffer(message_buf); + + auto sig = ecdsa_construct_signature(message, key_pair); + write(output_sig_r, sig.r); + write(output_sig_s, sig.s); + write(output_sig_v, sig.v); +} + WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message, size_t msg_len, uint8_t const* sig_r, @@ -48,6 +67,21 @@ WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message write(output_pub_key, recovered_pub_key); } +WASM_EXPORT void ecdsa__recover_public_key_from_signature_( + uint8_t const* message_buf, uint8_t const* sig_r, uint8_t const* sig_s, uint8_t* sig_v, uint8_t* output_pub_key) +{ + std::array r, s; + std::copy(sig_r, sig_r + 32, r.begin()); + std::copy(sig_s, sig_s + 32, s.begin()); + const uint8_t v = *sig_v; + + auto message = from_buffer(message_buf); + ecdsa_signature sig = { r, s, v }; + auto recovered_pub_key = + ecdsa_recover_public_key(message, sig); + write(output_pub_key, recovered_pub_key); +} + WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message, size_t msg_len, uint8_t const* pub_key, @@ -65,3 +99,21 @@ WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message, return ecdsa_verify_signature( std::string((char*)message, msg_len), pubk, sig); } + +WASM_EXPORT void ecdsa__verify_signature_(uint8_t const* message_buf, + uint8_t const* pub_key, + uint8_t const* sig_r, + uint8_t const* sig_s, + uint8_t const* sig_v, + bool* result) +{ + auto pubk = from_buffer(pub_key); + std::array r, s; + std::copy(sig_r, sig_r + 32, r.begin()); + std::copy(sig_s, sig_s + 32, s.begin()); + const uint8_t v = *sig_v; + + auto message = from_buffer(message_buf); + ecdsa_signature sig = { r, s, v }; + *result = ecdsa_verify_signature(message, pubk, sig); +} diff --git a/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.h b/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.h index 1240b9aee7a..7a5832c03ac 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.h +++ b/barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.h @@ -1,5 +1,5 @@ -#include #include "barretenberg/common/wasm_export.hpp" +#include WASM_EXPORT void ecdsa__compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf); @@ -10,6 +10,12 @@ WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message, uint8_t* output_sig_s, uint8_t* output_sig_v); +WASM_EXPORT void ecdsa__construct_signature_(uint8_t const* message_buf, + uint8_t const* private_key, + uint8_t* output_sig_r, + uint8_t* output_sig_s, + uint8_t* output_sig_v); + WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message, size_t msg_len, uint8_t const* sig_r, @@ -17,9 +23,15 @@ WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message uint8_t* sig_v, uint8_t* output_pub_key); +WASM_EXPORT void ecdsa__recover_public_key_from_signature_( + uint8_t const* message_buf, uint8_t const* sig_r, uint8_t const* sig_s, uint8_t* sig_v, uint8_t* output_pub_key); + WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message, size_t msg_len, uint8_t const* pub_key, uint8_t const* sig_r, uint8_t const* sig_s, uint8_t const* sig_v); + +WASM_EXPORT bool ecdsa__verify_signature_( + uint8_t const* message, uint8_t const* pub_key, uint8_t const* sig_r, uint8_t const* sig_s, uint8_t const* sig_v); diff --git a/boxes/boxes/react/package.json b/boxes/boxes/react/package.json index ea36cec4694..34236ccb62c 100644 --- a/boxes/boxes/react/package.json +++ b/boxes/boxes/react/package.json @@ -7,7 +7,7 @@ "main": "./dist/index.js", "scripts": { "compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --silence-warnings", - "codegen": "${AZTEC_BUILDER:-aztec-builder} codegen src/contracts/target -o artifacts", + "codegen": "${AZTEC_BUILDER:-aztec} codegen src/contracts/target -o artifacts", "clean": "rm -rf ./dist .tsbuildinfo ./artifacts ./src/contracts/target", "prep": "yarn clean && yarn compile && yarn codegen", "dev": "yarn prep && webpack serve --mode development", @@ -38,7 +38,6 @@ "dependencies": { "@aztec/accounts": "latest", "@aztec/aztec.js": "latest", - "@aztec/builder": "latest", "classnames": "^2.3.2", "formik": "^2.4.3", "react": "^18.2.0", diff --git a/boxes/boxes/react/src/config.ts b/boxes/boxes/react/src/config.ts index 12abd35546d..850b164eaac 100644 --- a/boxes/boxes/react/src/config.ts +++ b/boxes/boxes/react/src/config.ts @@ -6,27 +6,24 @@ import { SingleKeyAccountContract } from '@aztec/accounts/single_key'; const SECRET_KEY = Fr.random(); export class PrivateEnv { - pxe; - accountContract; - account: AccountManager; + private constructor(private accountManager: AccountManager) {} - constructor( - private secretKey: Fr, - private pxeURL: string, - ) { - this.pxe = createPXEClient(this.pxeURL); + static async create(secretKey: Fr, pxeURL: string) { + const pxe = createPXEClient(pxeURL); const encryptionPrivateKey = deriveMasterIncomingViewingSecretKey(secretKey); - this.accountContract = new SingleKeyAccountContract(encryptionPrivateKey); - this.account = new AccountManager(this.pxe, this.secretKey, this.accountContract); + const accountContract = new SingleKeyAccountContract(encryptionPrivateKey); + const accountManager = await AccountManager.create(pxe, secretKey, accountContract); + + return new PrivateEnv(accountManager); } async getWallet() { // taking advantage that register is no-op if already registered - return await this.account.register(); + return await this.accountManager.register(); } } -export const deployerEnv = new PrivateEnv(SECRET_KEY, process.env.PXE_URL || 'http://localhost:8080'); +export const deployerEnv = await PrivateEnv.create(SECRET_KEY, process.env.PXE_URL || 'http://localhost:8080'); const IGNORE_FUNCTIONS = ['constructor', 'compute_note_hash_and_optionally_a_nullifier']; export const filteredInterface = BoxReactContractArtifact.functions.filter(f => !IGNORE_FUNCTIONS.includes(f.name)); diff --git a/boxes/boxes/vanilla/package.json b/boxes/boxes/vanilla/package.json index 84178f61f45..519c6d6e377 100644 --- a/boxes/boxes/vanilla/package.json +++ b/boxes/boxes/vanilla/package.json @@ -6,7 +6,7 @@ "type": "module", "scripts": { "compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --silence-warnings", - "codegen": "${AZTEC_BUILDER:-aztec-builder} codegen src/contracts/target -o artifacts", + "codegen": "${AZTEC_BUILDER:-aztec} codegen src/contracts/target -o artifacts", "clean": "rm -rf ./dest .tsbuildinfo ./artifacts ./src/contracts/target", "prep": "yarn clean && yarn compile && yarn codegen && tsc -b", "dev": "yarn prep && webpack serve --mode development", @@ -18,8 +18,7 @@ }, "dependencies": { "@aztec/accounts": "latest", - "@aztec/aztec.js": "latest", - "@aztec/builder": "latest" + "@aztec/aztec.js": "latest" }, "devDependencies": { "@playwright/test": "^1.49.0", diff --git a/boxes/boxes/vanilla/src/index.ts b/boxes/boxes/vanilla/src/index.ts index c9531636681..10846d40c36 100644 --- a/boxes/boxes/vanilla/src/index.ts +++ b/boxes/boxes/vanilla/src/index.ts @@ -7,7 +7,7 @@ const secretKey = Fr.random(); const pxe = createPXEClient(process.env.PXE_URL || 'http://localhost:8080'); const encryptionPrivateKey = deriveMasterIncomingViewingSecretKey(secretKey); -const account = new AccountManager(pxe, secretKey, new SingleKeyAccountContract(encryptionPrivateKey)); +const account = await AccountManager.create(pxe, secretKey, new SingleKeyAccountContract(encryptionPrivateKey)); let contract: any = null; let wallet: Wallet | null = null; @@ -21,11 +21,7 @@ document.querySelector('#deploy').addEventListener('click', async ({ target }: a setWait(true); wallet = await account.register(); - contract = await VanillaContract.deploy( - wallet, - Fr.random(), - wallet.getCompleteAddress().address - ) + contract = await VanillaContract.deploy(wallet, Fr.random(), wallet.getCompleteAddress().address) .send({ contractAddressSalt: Fr.random() }) .deployed(); alert(`Contract deployed at ${contract.address}`); @@ -41,13 +37,7 @@ document.querySelector('#set').addEventListener('submit', async (e: Event) => { const { value } = document.querySelector('#number') as HTMLInputElement; const { address: owner } = wallet.getCompleteAddress(); - await contract.methods - .setNumber( - parseInt(value), - owner, - ) - .send() - .wait(); + await contract.methods.setNumber(parseInt(value), owner).send().wait(); setWait(false); alert('Number set!'); diff --git a/boxes/boxes/vite/src/config.ts b/boxes/boxes/vite/src/config.ts index dbd86001d89..41856ff88a3 100644 --- a/boxes/boxes/vite/src/config.ts +++ b/boxes/boxes/vite/src/config.ts @@ -26,7 +26,7 @@ const SECRET_KEY = Fr.random(); export class PrivateEnv { pxe; accountContract; - account: AccountManager; + accountManager: AccountManager; constructor( private secretKey: Fr, @@ -74,16 +74,16 @@ export class PrivateEnv { this.secretKey, ); this.accountContract = new SchnorrAccountContract(encryptionPrivateKey); - this.account = new AccountManager( + this.accountManager = await AccountManager.create( this.pxe, this.secretKey, this.accountContract, ); - await this.account.deploy().wait(); + await this.accountManager.deploy().wait(); } async getWallet() { - return await this.account.register(); + return await this.accountManager.register(); } } diff --git a/boxes/yarn.lock b/boxes/yarn.lock index 60a32655c1a..b3c34120554 100644 --- a/boxes/yarn.lock +++ b/boxes/yarn.lock @@ -33,19 +33,6 @@ __metadata: languageName: node linkType: soft -"@aztec/builder@npm:latest": - version: 0.52.0 - resolution: "@aztec/builder@npm:0.52.0" - dependencies: - "@aztec/foundation": "npm:0.52.0" - "@aztec/types": "npm:0.52.0" - commander: "npm:^12.1.0" - bin: - aztec-builder: dest/bin/cli.js - checksum: 10c0/2207259255fc3e2ffbbd08829f2a4adc9070befaf09e0541213beaf378632a501c29104e447f310aebbf65a21e3cb77b99259a4122e9253640ee232ce4413675 - languageName: node - linkType: hard - "@aztec/circuit-types@link:../yarn-project/circuit-types::locator=aztec-app%40workspace%3A.": version: 0.0.0-use.local resolution: "@aztec/circuit-types@link:../yarn-project/circuit-types::locator=aztec-app%40workspace%3A." @@ -94,7 +81,6 @@ __metadata: dependencies: "@aztec/accounts": "npm:latest" "@aztec/aztec.js": "npm:latest" - "@aztec/builder": "npm:latest" "@playwright/test": "npm:1.49.0" "@types/jest": "npm:^29.5.0" "@types/node": "npm:^20.5.9" @@ -145,19 +131,12 @@ __metadata: languageName: node linkType: soft -"@aztec/types@link:../yarn-project/types::locator=aztec-app%40workspace%3A.": - version: 0.0.0-use.local - resolution: "@aztec/types@link:../yarn-project/types::locator=aztec-app%40workspace%3A." - languageName: node - linkType: soft - "@aztec/vanilla@workspace:boxes/vanilla": version: 0.0.0-use.local resolution: "@aztec/vanilla@workspace:boxes/vanilla" dependencies: "@aztec/accounts": "npm:latest" "@aztec/aztec.js": "npm:latest" - "@aztec/builder": "npm:latest" "@playwright/test": "npm:^1.49.0" "@types/node": "npm:^20.11.17" assert: "npm:^2.1.0" diff --git a/docs/docs/migration_notes.md b/docs/docs/migration_notes.md index 1e597de2ebc..f8a4865b6c5 100644 --- a/docs/docs/migration_notes.md +++ b/docs/docs/migration_notes.md @@ -7,6 +7,17 @@ keywords: [sandbox, aztec, notes, migration, updating, upgrading] Aztec is in full-speed development. Literally every version breaks compatibility with the previous ones. This page attempts to target errors and difficulties you might encounter when upgrading, and how to resolve them. ## 0.72.0 +### Some functions in `aztec.js` and `@aztec/accounts` are now async +In our efforts to make libraries more browser-friendly and providing with more bundling options for `bb.js` (like a non top-level-await version), some functions are being made async, in particular those that access our cryptographic functions. + +```diff +- AztecAddress.random(); ++ await AztecAddress.random(); + +- getSchnorrAccount(); ++ await getSchnorrAccount(); +``` + ### Public logs replace unencrypted logs Any log emitted from public is now known as a public log, rather than an unencrypted log. This means methods relating to these logs have been renamed e.g. in the pxe, archiver, txe: ```diff diff --git a/gaztec/src/components/sidebar/components/createAccountDialog.tsx b/gaztec/src/components/sidebar/components/createAccountDialog.tsx index 47d3c88539f..4d85686b9e5 100644 --- a/gaztec/src/components/sidebar/components/createAccountDialog.tsx +++ b/gaztec/src/components/sidebar/components/createAccountDialog.tsx @@ -40,7 +40,7 @@ export function CreateAccountDialog({ const createAccount = async () => { setDeployingAccount(true); const salt = Fr.random(); - const account = getSchnorrAccount( + const account = await getSchnorrAccount( pxe, secretKey, deriveSigningKey(secretKey), diff --git a/gaztec/src/components/sidebar/sidebar.tsx b/gaztec/src/components/sidebar/sidebar.tsx index fe9e291c5d6..7b4b6d24b7d 100644 --- a/gaztec/src/components/sidebar/sidebar.tsx +++ b/gaztec/src/components/sidebar/sidebar.tsx @@ -220,7 +220,7 @@ export function SidebarComponent() { } const accountAddress = AztecAddress.fromString(event.target.value); const accountData = await walletDB.retrieveAccount(accountAddress); - const account = getSchnorrAccount( + const account = await getSchnorrAccount( pxe, accountData.secretKey, deriveSigningKey(accountData.secretKey), diff --git a/yarn-project/accounts/src/defaults/account_contract.ts b/yarn-project/accounts/src/defaults/account_contract.ts index 6854af1a10b..e71238ef83e 100644 --- a/yarn-project/accounts/src/defaults/account_contract.ts +++ b/yarn-project/accounts/src/defaults/account_contract.ts @@ -11,7 +11,7 @@ import { DefaultAccountInterface } from '../defaults/account_interface.js'; */ export abstract class DefaultAccountContract implements AccountContract { abstract getAuthWitnessProvider(address: CompleteAddress): AuthWitnessProvider; - abstract getDeploymentArgs(): any[] | undefined; + abstract getDeploymentArgs(): Promise; constructor(private artifact: ContractArtifact) {} diff --git a/yarn-project/accounts/src/ecdsa/ecdsa_k/account_contract.ts b/yarn-project/accounts/src/ecdsa/ecdsa_k/account_contract.ts index 3b5f1b54f11..de1cfaca3b8 100644 --- a/yarn-project/accounts/src/ecdsa/ecdsa_k/account_contract.ts +++ b/yarn-project/accounts/src/ecdsa/ecdsa_k/account_contract.ts @@ -16,8 +16,8 @@ export class EcdsaKAccountContract extends DefaultAccountContract { super(EcdsaKAccountContractArtifact as ContractArtifact); } - getDeploymentArgs() { - const signingPublicKey = new Ecdsa().computePublicKey(this.signingPrivateKey); + async getDeploymentArgs() { + const signingPublicKey = await new Ecdsa().computePublicKey(this.signingPrivateKey); return [signingPublicKey.subarray(0, 32), signingPublicKey.subarray(32, 64)]; } @@ -30,9 +30,9 @@ export class EcdsaKAccountContract extends DefaultAccountContract { class EcdsaKAuthWitnessProvider implements AuthWitnessProvider { constructor(private signingPrivateKey: Buffer) {} - createAuthWit(messageHash: Fr): Promise { + async createAuthWit(messageHash: Fr): Promise { const ecdsa = new Ecdsa(); - const signature = ecdsa.constructSignature(messageHash.toBuffer(), this.signingPrivateKey); + const signature = await ecdsa.constructSignature(messageHash.toBuffer(), this.signingPrivateKey); return Promise.resolve(new AuthWitness(messageHash, [...signature.r, ...signature.s])); } } diff --git a/yarn-project/accounts/src/ecdsa/ecdsa_k/index.ts b/yarn-project/accounts/src/ecdsa/ecdsa_k/index.ts index 660cfe33913..2e2e1ad3ab5 100644 --- a/yarn-project/accounts/src/ecdsa/ecdsa_k/index.ts +++ b/yarn-project/accounts/src/ecdsa/ecdsa_k/index.ts @@ -21,8 +21,13 @@ export { EcdsaKAccountContract }; * @param signingPrivateKey - Secp256k1 key used for signing transactions. * @param salt - Deployment salt. */ -export function getEcdsaKAccount(pxe: PXE, secretKey: Fr, signingPrivateKey: Buffer, salt?: Salt): AccountManager { - return new AccountManager(pxe, secretKey, new EcdsaKAccountContract(signingPrivateKey), salt); +export function getEcdsaKAccount( + pxe: PXE, + secretKey: Fr, + signingPrivateKey: Buffer, + salt?: Salt, +): Promise { + return AccountManager.create(pxe, secretKey, new EcdsaKAccountContract(signingPrivateKey), salt); } /** diff --git a/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/account_contract.ts b/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/account_contract.ts index 60812c83680..f897a6e414f 100644 --- a/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/account_contract.ts +++ b/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/account_contract.ts @@ -22,7 +22,7 @@ export class EcdsaRSSHAccountContract extends DefaultAccountContract { } getDeploymentArgs() { - return [this.signingPublicKey.subarray(0, 32), this.signingPublicKey.subarray(32, 64)]; + return Promise.resolve([this.signingPublicKey.subarray(0, 32), this.signingPublicKey.subarray(32, 64)]); } getAuthWitnessProvider(_address: CompleteAddress): AuthWitnessProvider { diff --git a/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/index.ts b/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/index.ts index fb368a3c7a9..af953cb953d 100644 --- a/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/index.ts +++ b/yarn-project/accounts/src/ecdsa/ssh_ecdsa_r/index.ts @@ -21,8 +21,13 @@ export { EcdsaRSSHAccountContract }; * @param signingPublicKey - Secp2561 key used to identify its corresponding private key in the SSH Agent. * @param salt - Deployment salt. */ -export function getEcdsaRSSHAccount(pxe: PXE, secretKey: Fr, signingPublicKey: Buffer, salt?: Salt): AccountManager { - return new AccountManager(pxe, secretKey, new EcdsaRSSHAccountContract(signingPublicKey), salt); +export function getEcdsaRSSHAccount( + pxe: PXE, + secretKey: Fr, + signingPublicKey: Buffer, + salt?: Salt, +): Promise { + return AccountManager.create(pxe, secretKey, new EcdsaRSSHAccountContract(signingPublicKey), salt); } /** diff --git a/yarn-project/accounts/src/schnorr/account_contract.ts b/yarn-project/accounts/src/schnorr/account_contract.ts index 7bb4c6dda1f..3f52a537a2a 100644 --- a/yarn-project/accounts/src/schnorr/account_contract.ts +++ b/yarn-project/accounts/src/schnorr/account_contract.ts @@ -16,8 +16,8 @@ export class SchnorrAccountContract extends DefaultAccountContract { super(SchnorrAccountContractArtifact as ContractArtifact); } - getDeploymentArgs() { - const signingPublicKey = new Schnorr().computePublicKey(this.signingPrivateKey); + async getDeploymentArgs() { + const signingPublicKey = await new Schnorr().computePublicKey(this.signingPrivateKey); return [signingPublicKey.x, signingPublicKey.y]; } @@ -30,9 +30,9 @@ export class SchnorrAccountContract extends DefaultAccountContract { class SchnorrAuthWitnessProvider implements AuthWitnessProvider { constructor(private signingPrivateKey: GrumpkinScalar) {} - createAuthWit(messageHash: Fr): Promise { + async createAuthWit(messageHash: Fr): Promise { const schnorr = new Schnorr(); - const signature = schnorr.constructSignature(messageHash.toBuffer(), this.signingPrivateKey).toBuffer(); - return Promise.resolve(new AuthWitness(messageHash, [...signature])); + const signature = await schnorr.constructSignature(messageHash.toBuffer(), this.signingPrivateKey); + return new AuthWitness(messageHash, [...signature.toBuffer()]); } } diff --git a/yarn-project/accounts/src/schnorr/index.ts b/yarn-project/accounts/src/schnorr/index.ts index b80e306587e..b210c789c49 100644 --- a/yarn-project/accounts/src/schnorr/index.ts +++ b/yarn-project/accounts/src/schnorr/index.ts @@ -27,8 +27,8 @@ export function getSchnorrAccount( secretKey: Fr, signingPrivateKey: GrumpkinScalar, salt?: Salt, -): AccountManager { - return new AccountManager(pxe, secretKey, new SchnorrAccountContract(signingPrivateKey), salt); +): Promise { + return AccountManager.create(pxe, secretKey, new SchnorrAccountContract(signingPrivateKey), salt); } /** diff --git a/yarn-project/accounts/src/single_key/account_contract.ts b/yarn-project/accounts/src/single_key/account_contract.ts index ed2de53ebfe..39509a7d1df 100644 --- a/yarn-project/accounts/src/single_key/account_contract.ts +++ b/yarn-project/accounts/src/single_key/account_contract.ts @@ -16,8 +16,8 @@ export class SingleKeyAccountContract extends DefaultAccountContract { super(SchnorrSingleKeyAccountContractArtifact as ContractArtifact); } - getDeploymentArgs(): undefined { - return undefined; + getDeploymentArgs() { + return Promise.resolve(undefined); } getAuthWitnessProvider(account: CompleteAddress): AuthWitnessProvider { @@ -33,9 +33,9 @@ export class SingleKeyAccountContract extends DefaultAccountContract { class SingleKeyAuthWitnessProvider implements AuthWitnessProvider { constructor(private privateKey: GrumpkinScalar, private account: CompleteAddress) {} - createAuthWit(messageHash: Fr): Promise { + async createAuthWit(messageHash: Fr): Promise { const schnorr = new Schnorr(); - const signature = schnorr.constructSignature(messageHash.toBuffer(), this.privateKey); + const signature = await schnorr.constructSignature(messageHash.toBuffer(), this.privateKey); const witness = [...this.account.publicKeys.toFields(), ...signature.toBuffer(), this.account.partialAddress]; return Promise.resolve(new AuthWitness(messageHash, witness)); } diff --git a/yarn-project/accounts/src/single_key/index.ts b/yarn-project/accounts/src/single_key/index.ts index da92a710ac7..732f44f0059 100644 --- a/yarn-project/accounts/src/single_key/index.ts +++ b/yarn-project/accounts/src/single_key/index.ts @@ -21,9 +21,9 @@ export { SchnorrSingleKeyAccountContractArtifact as SingleKeyAccountContractArti * @param secretKey - Secret key used to derive all the keystore keys (in this case also used to get signing key). * @param salt - Deployment salt. */ -export function getSingleKeyAccount(pxe: PXE, secretKey: Fr, salt?: Salt): AccountManager { +export function getSingleKeyAccount(pxe: PXE, secretKey: Fr, salt?: Salt) { const encryptionPrivateKey = deriveMasterIncomingViewingSecretKey(secretKey); - return new AccountManager(pxe, secretKey, new SingleKeyAccountContract(encryptionPrivateKey), salt); + return AccountManager.create(pxe, secretKey, new SingleKeyAccountContract(encryptionPrivateKey), salt); } /** diff --git a/yarn-project/accounts/src/testing/configuration.ts b/yarn-project/accounts/src/testing/configuration.ts index 6f74f3a4562..70f45c402e6 100644 --- a/yarn-project/accounts/src/testing/configuration.ts +++ b/yarn-project/accounts/src/testing/configuration.ts @@ -29,9 +29,15 @@ export const INITIAL_TEST_ACCOUNT_SALTS = [Fr.ZERO, Fr.ZERO, Fr.ZERO]; */ export function getInitialTestAccountsWallets(pxe: PXE): Promise { return Promise.all( - INITIAL_TEST_SECRET_KEYS.map((encryptionKey, i) => - getSchnorrAccount(pxe, encryptionKey!, INITIAL_TEST_SIGNING_KEYS[i]!, INITIAL_TEST_ACCOUNT_SALTS[i]).getWallet(), - ), + INITIAL_TEST_SECRET_KEYS.map(async (encryptionKey, i) => { + const account = await getSchnorrAccount( + pxe, + encryptionKey!, + INITIAL_TEST_SIGNING_KEYS[i]!, + INITIAL_TEST_ACCOUNT_SALTS[i], + ); + return account.getWallet(); + }), ); } @@ -50,10 +56,11 @@ export async function getDeployedTestAccountsWallets(pxe: PXE): Promise registered.publicKeys.masterIncomingViewingPublicKey.equals(publicKey)) != undefined ); - }).map(secretKey => { + }).map(async secretKey => { const signingKey = deriveSigningKey(secretKey); // TODO(#5726): use actual salt here instead of hardcoding Fr.ZERO - return getSchnorrAccount(pxe, secretKey, signingKey, Fr.ZERO).getWallet(); + const account = await getSchnorrAccount(pxe, secretKey, signingKey, Fr.ZERO); + return account.getWallet(); }), ); } @@ -64,13 +71,20 @@ export async function getDeployedTestAccountsWallets(pxe: PXE): Promise { - const account = getSchnorrAccount(pxe, secretKey, INITIAL_TEST_SIGNING_KEYS[i], INITIAL_TEST_ACCOUNT_SALTS[i]); - return { - account, - secretKey, - }; - }); + const accounts = await Promise.all( + INITIAL_TEST_SECRET_KEYS.map(async (secretKey, i) => { + const account = await getSchnorrAccount( + pxe, + secretKey, + INITIAL_TEST_SIGNING_KEYS[i], + INITIAL_TEST_ACCOUNT_SALTS[i], + ); + return { + account, + secretKey, + }; + }), + ); // Register contract class to avoid duplicate nullifier errors const { l1ChainId: chainId, protocolVersion } = await pxe.getNodeInfo(); const deployWallet = new SignerlessWallet(pxe, new DefaultMultiCallEntrypoint(chainId, protocolVersion)); @@ -80,7 +94,7 @@ export async function deployInitialTestAccounts(pxe: PXE) { accounts.map(async x => { const deployMethod = await x.account.getDeployMethod(); const tx = await deployMethod.prove({ - contractAddressSalt: x.account.salt, + contractAddressSalt: new Fr(x.account.salt), universalDeploy: true, }); return tx; diff --git a/yarn-project/accounts/src/testing/create_account.ts b/yarn-project/accounts/src/testing/create_account.ts index 3dc7568bbb7..37f8156b98f 100644 --- a/yarn-project/accounts/src/testing/create_account.ts +++ b/yarn-project/accounts/src/testing/create_account.ts @@ -10,10 +10,11 @@ import { getSchnorrAccount } from '../schnorr/index.js'; * @param pxe - PXE. * @returns - A wallet for a fresh account. */ -export function createAccount(pxe: PXE): Promise { +export async function createAccount(pxe: PXE): Promise { const secretKey = Fr.random(); const signingKey = deriveSigningKey(secretKey); - return getSchnorrAccount(pxe, secretKey, signingKey).waitSetup(); + const account = await getSchnorrAccount(pxe, secretKey, signingKey); + return account.waitSetup(); } /** @@ -40,7 +41,7 @@ export async function createAccounts( const accountsAndDeployments = await Promise.all( secrets.map(async (secret, index) => { const signingKey = deriveSigningKey(secret); - const account = getSchnorrAccount(pxe, secret, signingKey); + const account = await getSchnorrAccount(pxe, secret, signingKey); // only register the contract class once let skipClassRegistration = true; @@ -53,7 +54,7 @@ export async function createAccounts( const deployMethod = await account.getDeployMethod(); const provenTx = await deployMethod.prove({ - contractAddressSalt: account.salt, + contractAddressSalt: new Fr(account.salt), skipClassRegistration, skipPublicDeployment: true, universalDeploy: true, diff --git a/yarn-project/aztec.js/src/account/contract.ts b/yarn-project/aztec.js/src/account/contract.ts index 8408c936563..4be2ac57f1c 100644 --- a/yarn-project/aztec.js/src/account/contract.ts +++ b/yarn-project/aztec.js/src/account/contract.ts @@ -18,7 +18,7 @@ export interface AccountContract { /** * Returns the deployment arguments for this instance, or undefined if this contract does not require deployment. */ - getDeploymentArgs(): any[] | undefined; + getDeploymentArgs(): Promise; /** * Returns the account interface for this account contract given a deployment at the provided address. diff --git a/yarn-project/aztec.js/src/account_manager/index.ts b/yarn-project/aztec.js/src/account_manager/index.ts index a9f5e4cc328..873d88e2333 100644 --- a/yarn-project/aztec.js/src/account_manager/index.ts +++ b/yarn-project/aztec.js/src/account_manager/index.ts @@ -25,21 +25,28 @@ export type DeployAccountOptions = Pick< * and creating and registering the user wallet in the PXE Service. */ export class AccountManager { - /** Deployment salt for the account contract. */ - public readonly salt: Fr; - - private instance: ContractInstanceWithAddress; - - constructor(private pxe: PXE, private secretKey: Fr, private accountContract: AccountContract, salt?: Salt) { - this.salt = salt !== undefined ? new Fr(salt) : Fr.random(); - + private constructor( + private pxe: PXE, + private secretKey: Fr, + private accountContract: AccountContract, + private instance: ContractInstanceWithAddress, + /** + * Deployment salt for the account contract + */ + public readonly salt: Salt, + ) {} + + static async create(pxe: PXE, secretKey: Fr, accountContract: AccountContract, salt?: Salt) { const { publicKeys } = deriveKeys(secretKey); + salt = salt !== undefined ? new Fr(salt) : Fr.random(); - this.instance = getContractInstanceFromDeployParams(this.accountContract.getContractArtifact(), { - constructorArgs: this.accountContract.getDeploymentArgs(), - salt: this.salt, + const instance = getContractInstanceFromDeployParams(accountContract.getContractArtifact(), { + constructorArgs: await accountContract.getDeploymentArgs(), + salt: salt, publicKeys, }); + + return new AccountManager(pxe, secretKey, accountContract, instance, salt); } protected getPublicKeys() { @@ -136,7 +143,7 @@ export class AccountManager { // We use a signerless wallet with the multi call entrypoint in order to make multiple calls in one go // If we used getWallet, the deployment would get routed via the account contract entrypoint // and it can't be used unless the contract is initialized - const args = this.accountContract.getDeploymentArgs() ?? []; + const args = (await this.accountContract.getDeploymentArgs()) ?? []; return new DeployAccountMethod( this.accountContract.getAuthWitnessProvider(this.getCompleteAddress()), this.getPublicKeys(), @@ -160,7 +167,7 @@ export class AccountManager { const sentTx = this.getDeployMethod() .then(deployMethod => deployMethod.send({ - contractAddressSalt: this.salt, + contractAddressSalt: new Fr(this.salt), skipClassRegistration: opts?.skipClassRegistration ?? true, skipPublicDeployment: opts?.skipPublicDeployment ?? true, skipInitialization: opts?.skipInitialization ?? false, @@ -180,14 +187,14 @@ export class AccountManager { * @returns A Wallet instance. */ public async waitSetup(opts: WaitOpts = DefaultWaitOpts): Promise { - await (this.isDeployable() ? this.deploy().wait(opts) : this.register()); + await ((await this.isDeployable()) ? this.deploy().wait(opts) : this.register()); return this.getWallet(); } /** * Returns whether this account contract has a constructor and needs deployment. */ - public isDeployable() { - return this.accountContract.getDeploymentArgs() !== undefined; + public async isDeployable() { + return (await this.accountContract.getDeploymentArgs()) !== undefined; } } diff --git a/yarn-project/aztec/src/examples/token.ts b/yarn-project/aztec/src/examples/token.ts index 282ae050b86..230214eaf22 100644 --- a/yarn-project/aztec/src/examples/token.ts +++ b/yarn-project/aztec/src/examples/token.ts @@ -24,8 +24,10 @@ const TRANSFER_AMOUNT = 33n; async function main() { logger.info('Running token contract test on HTTP interface.'); - aliceWallet = await getSingleKeyAccount(pxe, alicePrivateKey).waitSetup(); - bobWallet = await getSingleKeyAccount(pxe, bobPrivateKey).waitSetup(); + const aliceAccount = await getSingleKeyAccount(pxe, alicePrivateKey); + aliceWallet = await aliceAccount.waitSetup(); + const bobAccount = await getSingleKeyAccount(pxe, bobPrivateKey); + bobWallet = await bobAccount.waitSetup(); const alice = aliceWallet.getCompleteAddress(); const bob = bobWallet.getCompleteAddress(); diff --git a/yarn-project/bot/src/factory.ts b/yarn-project/bot/src/factory.ts index d5f4eb73fe3..261dc870daa 100644 --- a/yarn-project/bot/src/factory.ts +++ b/yarn-project/bot/src/factory.ts @@ -62,7 +62,7 @@ export class BotFactory { private async setupAccount() { const salt = Fr.ONE; const signingKey = deriveSigningKey(this.config.senderPrivateKey); - const account = getSchnorrAccount(this.pxe, this.config.senderPrivateKey, signingKey, salt); + const account = await getSchnorrAccount(this.pxe, this.config.senderPrivateKey, signingKey, salt); const isInit = await this.pxe.isContractInitialized(account.getAddress()); if (isInit) { this.log.info(`Account at ${account.getAddress().toString()} already initialized`); diff --git a/yarn-project/circuit-types/src/logs/l1_payload/encrypted_log_payload.ts b/yarn-project/circuit-types/src/logs/l1_payload/encrypted_log_payload.ts index cfecd5a9ec8..e210d8b5639 100644 --- a/yarn-project/circuit-types/src/logs/l1_payload/encrypted_log_payload.ts +++ b/yarn-project/circuit-types/src/logs/l1_payload/encrypted_log_payload.ts @@ -100,7 +100,7 @@ export class EncryptedLogPayload { const addressPoint = await recipient.toAddressPoint(); const ephPk = derivePublicKeyFromSecretKey(ephSk); - const incomingHeaderCiphertext = encrypt(this.contractAddress.toBuffer(), ephSk, addressPoint); + const incomingHeaderCiphertext = await encrypt(this.contractAddress.toBuffer(), ephSk, addressPoint); if (incomingHeaderCiphertext.length !== HEADER_SIZE) { throw new Error(`Invalid incoming header size: ${incomingHeaderCiphertext.length}`); @@ -125,7 +125,7 @@ export class EncryptedLogPayload { this.incomingBodyPlaintext, rand(numPaddedBytes), ]); - const incomingBodyCiphertext = encrypt(paddedIncomingBodyPlaintextWithLength, ephSk, addressPoint); + const incomingBodyCiphertext = await encrypt(paddedIncomingBodyPlaintextWithLength, ephSk, addressPoint); const encryptedPayload = serializeToBuffer(overhead, incomingBodyCiphertext); @@ -162,13 +162,13 @@ export class EncryptedLogPayload { const reader = BufferReader.asReader(fieldsToEncryptedBytes(payload.slice(1))); const overhead = await Overhead.fromBuffer(reader); - const { contractAddress } = this.#decryptOverhead(overhead, { addressSecret }); + const { contractAddress } = await this.#decryptOverhead(overhead, { addressSecret }); let ciphertext = reader.readToEnd(); if (ciphertextLength && ciphertext.length !== ciphertextLength) { ciphertext = trimCiphertext(ciphertext, ciphertextLength); } - const incomingBodyPlaintext = this.#decryptIncomingBody(ciphertext, addressSecret, overhead.ephPk); + const incomingBodyPlaintext = await this.#decryptIncomingBody(ciphertext, addressSecret, overhead.ephPk); return new EncryptedLogPayload(tag, contractAddress, incomingBodyPlaintext); } catch (e: any) { @@ -196,11 +196,11 @@ export class EncryptedLogPayload { return serializeToBuffer(this.tag, this.contractAddress.toBuffer(), this.incomingBodyPlaintext); } - static #decryptOverhead(overhead: Overhead, { addressSecret }: { addressSecret: GrumpkinScalar }) { + static async #decryptOverhead(overhead: Overhead, { addressSecret }: { addressSecret: GrumpkinScalar }) { let contractAddress = AztecAddress.ZERO; if (addressSecret) { - const incomingHeader = decrypt(overhead.incomingHeader, addressSecret, overhead.ephPk); + const incomingHeader = await decrypt(overhead.incomingHeader, addressSecret, overhead.ephPk); contractAddress = AztecAddress.fromBuffer(incomingHeader); } @@ -209,8 +209,8 @@ export class EncryptedLogPayload { }; } - static #decryptIncomingBody(ciphertext: Buffer, secret: GrumpkinScalar, publicKey: PublicKey) { - const decrypted = decrypt(ciphertext, secret, publicKey); + static async #decryptIncomingBody(ciphertext: Buffer, secret: GrumpkinScalar, publicKey: PublicKey) { + const decrypted = await decrypt(ciphertext, secret, publicKey); const length = decrypted.readUint16BE(0); return decrypted.subarray(2, 2 + length); } diff --git a/yarn-project/circuit-types/src/logs/l1_payload/encryption_util.ts b/yarn-project/circuit-types/src/logs/l1_payload/encryption_util.ts index f8e447a6773..3881ba76853 100644 --- a/yarn-project/circuit-types/src/logs/l1_payload/encryption_util.ts +++ b/yarn-project/circuit-types/src/logs/l1_payload/encryption_util.ts @@ -17,7 +17,7 @@ export function encrypt( secret: GrumpkinScalar, publicKey: PublicKey, deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveAESSecret, -): Buffer { +): Promise { const aesSecret = deriveSecret(secret, publicKey); const key = aesSecret.subarray(0, 16); const iv = aesSecret.subarray(16, 32); @@ -39,7 +39,7 @@ export function decrypt( secret: GrumpkinScalar, publicKey: PublicKey, deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveAESSecret, -): Buffer { +): Promise { const aesSecret = deriveSecret(secret, publicKey); const key = aesSecret.subarray(0, 16); const iv = aesSecret.subarray(16, 32); diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.test.ts b/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.test.ts index 2f6ca3d5e94..496c1b0a93c 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.test.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.test.ts @@ -26,7 +26,7 @@ describe('aes128', () => { return paddedBuffer.subarray(0, paddedBuffer.length - paddingToRemove); }; - it('should correctly encrypt input', () => { + it('should correctly encrypt input', async () => { const data = randomBytes(32); const key = randomBytes(16); const iv = randomBytes(16); @@ -37,12 +37,12 @@ describe('aes128', () => { cipher.setAutoPadding(false); const expected = Buffer.concat([cipher.update(paddedData), cipher.final()]); - const result: Buffer = aes128.encryptBufferCBC(data, iv, key); + const result: Buffer = await aes128.encryptBufferCBC(data, iv, key); expect(result).toEqual(expected); }); - it('should correctly decrypt input', () => { + it('should correctly decrypt input', async () => { const data = randomBytes(32); const key = randomBytes(16); const iv = randomBytes(16); @@ -57,7 +57,7 @@ describe('aes128', () => { decipher.setAutoPadding(false); const expected = removePadding(Buffer.concat([decipher.update(ciphertext), decipher.final()])); - const result: Buffer = aes128.decryptBufferCBC(ciphertext, iv, key); + const result: Buffer = await aes128.decryptBufferCBC(ciphertext, iv, key); expect(result).toEqual(expected); }); diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.ts b/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.ts index 824e83b4b7e..01cc276166e 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/aes128/index.ts @@ -1,4 +1,4 @@ -import { BarretenbergSync, RawBuffer } from '@aztec/bb.js'; +import { BarretenbergLazy, RawBuffer } from '@aztec/bb.js'; import { Buffer } from 'buffer'; @@ -13,7 +13,7 @@ export class Aes128 { * @param key - Key to encrypt with. * @returns Encrypted data. */ - public encryptBufferCBC(data: Uint8Array, iv: Uint8Array, key: Uint8Array) { + public async encryptBufferCBC(data: Uint8Array, iv: Uint8Array, key: Uint8Array) { const rawLength = data.length; const numPaddingBytes = 16 - (rawLength % 16); const paddingBuffer = Buffer.alloc(numPaddingBytes); @@ -22,9 +22,9 @@ export class Aes128 { paddingBuffer.fill(numPaddingBytes); const input = Buffer.concat([data, paddingBuffer]); - const api = BarretenbergSync.getSingleton(); + const api = await BarretenbergLazy.getSingleton(); return Buffer.from( - api.aesEncryptBufferCbc(new RawBuffer(input), new RawBuffer(iv), new RawBuffer(key), input.length), + await api.aesEncryptBufferCbc(new RawBuffer(input), new RawBuffer(iv), new RawBuffer(key), input.length), ); } @@ -35,10 +35,10 @@ export class Aes128 { * @param key - Key to decrypt with. * @returns Decrypted data. */ - public decryptBufferCBC(data: Uint8Array, iv: Uint8Array, key: Uint8Array) { - const api = BarretenbergSync.getSingleton(); + public async decryptBufferCBC(data: Uint8Array, iv: Uint8Array, key: Uint8Array) { + const api = await BarretenbergLazy.getSingleton(); const paddedBuffer = Buffer.from( - api.aesDecryptBufferCbc(new RawBuffer(data), new RawBuffer(iv), new RawBuffer(key), data.length), + await api.aesDecryptBufferCbc(new RawBuffer(data), new RawBuffer(iv), new RawBuffer(key), data.length), ); const paddingToRemove = paddedBuffer[paddedBuffer.length - 1]; return paddedBuffer.subarray(0, paddedBuffer.length - paddingToRemove); diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.test.ts b/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.test.ts index 57a61da3124..599865dfd0a 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.test.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.test.ts @@ -9,35 +9,50 @@ describe('ecdsa', () => { ecdsa = new Ecdsa(); }); - it('should verify signature', () => { + it('should verify signature', async () => { // prettier-ignore const privateKey = Buffer.from([ 0x0b, 0x9b, 0x3a, 0xde, 0xe6, 0xb3, 0xd8, 0x1b, 0x28, 0xa0, 0x88, 0x6b, 0x2a, 0x84, 0x15, 0xc7, 0xda, 0x31, 0x29, 0x1a, 0x5e, 0x96, 0xbb, 0x7a, 0x56, 0x63, 0x9e, 0x17, 0x7d, 0x30, 0x1b, 0xeb, ]); - const pubKey = ecdsa.computePublicKey(privateKey); + const pubKey = await ecdsa.computePublicKey(privateKey); const msg = new TextEncoder().encode('The quick brown dog jumped over the lazy fox.'); - const signature = ecdsa.constructSignature(msg, privateKey); - const verified = ecdsa.verifySignature(msg, pubKey, signature); + const signature = await ecdsa.constructSignature(msg, privateKey); + const verified = await ecdsa.verifySignature(msg, pubKey, signature); expect(verified).toBe(true); }); - it('should recover public key from signature', () => { + it('should not verify invalid signature', async () => { // prettier-ignore const privateKey = Buffer.from([ 0x0b, 0x9b, 0x3a, 0xde, 0xe6, 0xb3, 0xd8, 0x1b, 0x28, 0xa0, 0x88, 0x6b, 0x2a, 0x84, 0x15, 0xc7, 0xda, 0x31, 0x29, 0x1a, 0x5e, 0x96, 0xbb, 0x7a, 0x56, 0x63, 0x9e, 0x17, 0x7d, 0x30, 0x1b, 0xeb, ]); - const pubKey = ecdsa.computePublicKey(privateKey); + const pubKey = await ecdsa.computePublicKey(privateKey); + const msg = new TextEncoder().encode('The quick brown dog jumped over the lazy fox.'); + const signature = await ecdsa.constructSignature(msg, privateKey); + signature.r[0] = 0x00; + const verified = await ecdsa.verifySignature(msg, pubKey, signature); + + expect(verified).toBe(false); + }); + + it('should recover public key from signature', async () => { + // prettier-ignore + const privateKey = Buffer.from([ + 0x0b, 0x9b, 0x3a, 0xde, 0xe6, 0xb3, 0xd8, 0x1b, 0x28, 0xa0, 0x88, 0x6b, 0x2a, 0x84, 0x15, 0xc7, + 0xda, 0x31, 0x29, 0x1a, 0x5e, 0x96, 0xbb, 0x7a, 0x56, 0x63, 0x9e, 0x17, 0x7d, 0x30, 0x1b, 0xeb, + ]); + const pubKey = await ecdsa.computePublicKey(privateKey); const msg = new TextEncoder().encode('The quick brown dog jumped over the lazy fox...'); - const signature = ecdsa.constructSignature(msg, privateKey); + const signature = await ecdsa.constructSignature(msg, privateKey); // First, recover the public key - const recoveredPubKey = ecdsa.recoverPublicKey(msg, signature); + const recoveredPubKey = await ecdsa.recoverPublicKey(msg, signature); // Then, verify the signature using the recovered public key - const verified = ecdsa.verifySignature(msg, recoveredPubKey, signature); + const verified = await ecdsa.verifySignature(msg, recoveredPubKey, signature); expect(recoveredPubKey).toEqual(pubKey); expect(verified).toBe(true); diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.ts b/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.ts index 3b7dd7d3d73..1c9d7c54e60 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.ts @@ -1,5 +1,7 @@ -import { BarretenbergSync } from '@aztec/bb.js'; +import { BarretenbergLazy } from '@aztec/bb.js'; +import { numToInt32BE } from '@aztec/foundation/serialize'; +import { concatenateUint8Arrays } from '../../serialize.js'; import { EcdsaSignature } from './signature.js'; export * from './signature.js'; @@ -9,17 +11,15 @@ export * from './signature.js'; * TODO: Replace with codegen api on bb.js. */ export class Ecdsa { - private wasm = BarretenbergSync.getSingleton().getWasm(); - /** * Computes a secp256k1 public key from a private key. * @param privateKey - Secp256k1 private key. * @returns A secp256k1 public key. */ - public computePublicKey(privateKey: Buffer): Buffer { - this.wasm.writeMemory(0, privateKey); - this.wasm.call('ecdsa__compute_public_key', 0, 32); - return Buffer.from(this.wasm.getMemorySlice(32, 96)); + public async computePublicKey(privateKey: Buffer): Promise { + const api = await BarretenbergLazy.getSingleton(); + const [result] = await api.getWasm().callWasmExport('ecdsa__compute_public_key', [privateKey], [64]); + return Buffer.from(result); } /** @@ -28,17 +28,13 @@ export class Ecdsa { * @param privateKey - The secp256k1 private key of the signer. * @returns An ECDSA signature of the form (r, s, v). */ - public constructSignature(msg: Uint8Array, privateKey: Buffer) { - const mem = this.wasm.call('bbmalloc', msg.length); - this.wasm.writeMemory(0, privateKey); - this.wasm.writeMemory(mem, msg); - this.wasm.call('ecdsa__construct_signature', mem, msg.length, 0, 32, 64, 96); - - return new EcdsaSignature( - Buffer.from(this.wasm.getMemorySlice(32, 64)), - Buffer.from(this.wasm.getMemorySlice(64, 96)), - Buffer.from(this.wasm.getMemorySlice(96, 97)), - ); + public async constructSignature(msg: Uint8Array, privateKey: Buffer) { + const api = await BarretenbergLazy.getSingleton(); + const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]); + const [r, s, v] = await api + .getWasm() + .callWasmExport('ecdsa__construct_signature_', [messageArray, privateKey], [32, 32, 1]); + return new EcdsaSignature(Buffer.from(r), Buffer.from(s), Buffer.from(v)); } /** @@ -47,15 +43,13 @@ export class Ecdsa { * @param sig - The ECDSA signature. * @returns The secp256k1 public key of the signer. */ - public recoverPublicKey(msg: Uint8Array, sig: EcdsaSignature): Buffer { - const mem = this.wasm.call('bbmalloc', msg.length); - this.wasm.writeMemory(0, sig.r); - this.wasm.writeMemory(32, sig.s); - this.wasm.writeMemory(64, sig.v); - this.wasm.writeMemory(mem, msg); - this.wasm.call('ecdsa__recover_public_key_from_signature', mem, msg.length, 0, 32, 64, 65); - - return Buffer.from(this.wasm.getMemorySlice(65, 129)); + public async recoverPublicKey(msg: Uint8Array, sig: EcdsaSignature): Promise { + const api = await BarretenbergLazy.getSingleton(); + const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]); + const [result] = await api + .getWasm() + .callWasmExport('ecdsa__recover_public_key_from_signature_', [messageArray, sig.r, sig.s, sig.v], [64]); + return Buffer.from(result); } /** @@ -65,13 +59,12 @@ export class Ecdsa { * @param sig - The ECDSA signature. * @returns True or false. */ - public verifySignature(msg: Uint8Array, pubKey: Buffer, sig: EcdsaSignature) { - const mem = this.wasm.call('bbmalloc', msg.length); - this.wasm.writeMemory(0, pubKey); - this.wasm.writeMemory(64, sig.r); - this.wasm.writeMemory(96, sig.s); - this.wasm.writeMemory(128, sig.v); - this.wasm.writeMemory(mem, msg); - return this.wasm.call('ecdsa__verify_signature', mem, msg.length, 0, 64, 96, 128) ? true : false; + public async verifySignature(msg: Uint8Array, pubKey: Buffer, sig: EcdsaSignature) { + const api = await BarretenbergLazy.getSingleton(); + const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]); + const [result] = await api + .getWasm() + .callWasmExport('ecdsa__verify_signature_', [messageArray, pubKey, sig.r, sig.s, sig.v], [1]); + return result[0] === 1; } } diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.test.ts b/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.test.ts index 94763421d66..37fc2dacffc 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.test.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.test.ts @@ -11,30 +11,30 @@ describe('schnorr', () => { schnorr = new Schnorr(); }); - it('should verify signature', () => { + it('should verify signature', async () => { // prettier-ignore const privateKey = GrumpkinScalar.fromBuffer(Buffer.from([ 0x0b, 0x9b, 0x3a, 0xde, 0xe6, 0xb3, 0xd8, 0x1b, 0x28, 0xa0, 0x88, 0x6b, 0x2a, 0x84, 0x15, 0xc7, 0xda, 0x31, 0x29, 0x1a, 0x5e, 0x96, 0xbb, 0x7a, 0x56, 0x63, 0x9e, 0x17, 0x7d, 0x30, 0x1b, 0xeb, ])); - const pubKey = schnorr.computePublicKey(privateKey); + const pubKey = await schnorr.computePublicKey(privateKey); const msg = new TextEncoder().encode('The quick brown dog jumped over the lazy fox.'); - const signature = schnorr.constructSignature(msg, privateKey); - const verified = schnorr.verifySignature(msg, pubKey, signature); + const signature = await schnorr.constructSignature(msg, privateKey); + const verified = await schnorr.verifySignature(msg, pubKey, signature); expect(verified).toBe(true); }); - it('should fail invalid signature', () => { + it('should fail invalid signature', async () => { // prettier-ignore const privateKey = GrumpkinScalar.fromBuffer(Buffer.from([ 0x0b, 0x9b, 0x3a, 0xde, 0xe6, 0xb3, 0xd8, 0x1b, 0x28, 0xa0, 0x88, 0x6b, 0x2a, 0x84, 0x15, 0xc7, 0xda, 0x31, 0x29, 0x1a, 0x5e, 0x96, 0xbb, 0x7a, 0x56, 0x63, 0x9e, 0x17, 0x7d, 0x30, 0x1b, 0xeb, ])); - const pubKey = schnorr.computePublicKey(privateKey); + const pubKey = await schnorr.computePublicKey(privateKey); const msg = new TextEncoder().encode('The quick brown dog jumped over the lazy fox.'); - const signature = schnorr.constructSignature(msg, GrumpkinScalar.random()); - const verified = schnorr.verifySignature(msg, pubKey, signature); + const signature = await schnorr.constructSignature(msg, GrumpkinScalar.random()); + const verified = await schnorr.verifySignature(msg, pubKey, signature); expect(verified).toBe(false); }); diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.ts b/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.ts index 92c33f47e56..8688e6380c9 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/schnorr/index.ts @@ -1,8 +1,9 @@ -import { BarretenbergSync } from '@aztec/bb.js'; +import { BarretenbergLazy } from '@aztec/bb.js'; import { type GrumpkinScalar, Point } from '@aztec/foundation/fields'; -import { numToUInt32BE } from '@aztec/foundation/serialize'; +import { numToInt32BE } from '@aztec/foundation/serialize'; import { type PublicKey } from '../../../types/public_key.js'; +import { concatenateUint8Arrays } from '../../serialize.js'; import { SchnorrSignature } from './signature.js'; export * from './signature.js'; @@ -11,17 +12,15 @@ export * from './signature.js'; * Schnorr signature construction and helper operations. */ export class Schnorr { - private wasm = BarretenbergSync.getSingleton().getWasm(); - /** * Computes a grumpkin public key from a private key. * @param privateKey - The private key. * @returns A grumpkin public key. */ - public computePublicKey(privateKey: GrumpkinScalar): PublicKey { - this.wasm.writeMemory(0, privateKey.toBuffer()); - this.wasm.call('schnorr_compute_public_key', 0, 32); - return Point.fromBuffer(Buffer.from(this.wasm.getMemorySlice(32, 96))); + public async computePublicKey(privateKey: GrumpkinScalar): Promise { + const api = await BarretenbergLazy.getSingleton(); + const [result] = await api.getWasm().callWasmExport('schnorr_compute_public_key', [privateKey.toBuffer()], [64]); + return Point.fromBuffer(Buffer.from(result)); } /** @@ -30,13 +29,13 @@ export class Schnorr { * @param privateKey - The private key of the signer. * @returns A Schnorr signature of the form (s, e). */ - public constructSignature(msg: Uint8Array, privateKey: GrumpkinScalar) { - const mem = this.wasm.call('bbmalloc', msg.length + 4); - this.wasm.writeMemory(0, privateKey.toBuffer()); - this.wasm.writeMemory(mem, Buffer.concat([numToUInt32BE(msg.length), msg])); - this.wasm.call('schnorr_construct_signature', mem, 0, 32, 64); - - return new SchnorrSignature(Buffer.from(this.wasm.getMemorySlice(32, 96))); + public async constructSignature(msg: Uint8Array, privateKey: GrumpkinScalar) { + const api = await BarretenbergLazy.getSingleton(); + const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]); + const [s, e] = await api + .getWasm() + .callWasmExport('schnorr_construct_signature', [messageArray, privateKey.toBuffer()], [32, 32]); + return new SchnorrSignature(Buffer.from([...s, ...e])); } /** @@ -46,14 +45,12 @@ export class Schnorr { * @param sig - The Schnorr signature. * @returns True or false. */ - public verifySignature(msg: Uint8Array, pubKey: PublicKey, sig: SchnorrSignature) { - const mem = this.wasm.call('bbmalloc', msg.length + 4); - this.wasm.writeMemory(0, pubKey.toBuffer()); - this.wasm.writeMemory(64, sig.s); - this.wasm.writeMemory(96, sig.e); - this.wasm.writeMemory(mem, Buffer.concat([numToUInt32BE(msg.length), msg])); - this.wasm.call('schnorr_verify_signature', mem, 0, 64, 96, 128); - const result = this.wasm.getMemorySlice(128, 129); - return !Buffer.alloc(1, 0).equals(result); + public async verifySignature(msg: Uint8Array, pubKey: PublicKey, sig: SchnorrSignature) { + const api = await BarretenbergLazy.getSingleton(); + const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]); + const [result] = await api + .getWasm() + .callWasmExport('schnorr_verify_signature', [messageArray, pubKey.toBuffer(), sig.s, sig.e], [1]); + return result[0] === 1; } } diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.test.ts b/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.test.ts index 9da5a362323..b3f6adecbc4 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.test.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.test.ts @@ -12,10 +12,10 @@ describe('secp256k1', () => { ecdsa = new Ecdsa(); }); - it('should correctly compute public key', () => { + it('should correctly compute public key', async () => { const privateKey = randomBytes(32); - const lhs = secp256k1.mul(Secp256k1.generator, privateKey); - const rhs = ecdsa.computePublicKey(privateKey); + const lhs = await secp256k1.mul(Secp256k1.generator, privateKey); + const rhs = await ecdsa.computePublicKey(privateKey); expect(lhs).toEqual(rhs); }); }); diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.ts b/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.ts index f4afdd82346..1b0088020f5 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.ts @@ -1,11 +1,9 @@ -import { BarretenbergSync } from '@aztec/bb.js'; +import { BarretenbergLazy } from '@aztec/bb.js'; /** * Secp256k1 elliptic curve operations. */ export class Secp256k1 { - private wasm = BarretenbergSync.getSingleton().getWasm(); - // prettier-ignore static generator = Buffer.from([ 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, @@ -28,20 +26,22 @@ export class Secp256k1 { * @param scalar - Scalar to multiply by. * @returns Result of the multiplication. */ - public mul(point: Uint8Array, scalar: Uint8Array) { - this.wasm.writeMemory(0, point); - this.wasm.writeMemory(64, scalar); - this.wasm.call('ecc_secp256k1__mul', 0, 64, 96); - return Buffer.from(this.wasm.getMemorySlice(96, 160)); + public async mul(point: Uint8Array, scalar: Uint8Array) { + const api = await BarretenbergLazy.getSingleton(); + const [result] = await api.getWasm().callWasmExport('ecc_secp256k1__mul', [point, scalar], [64]); + return Buffer.from(result); } /** * Gets a random field element. * @returns Random field element. */ - public getRandomFr() { - this.wasm.call('ecc_secp256k1__get_random_scalar_mod_circuit_modulus', 0); - return Buffer.from(this.wasm.getMemorySlice(0, 32)); + public async getRandomFr() { + const api = await BarretenbergLazy.getSingleton(); + const [result] = await api + .getWasm() + .callWasmExport('ecc_secp256k1__get_random_scalar_mod_circuit_modulus', [], [32]); + return Buffer.from(result); } /** @@ -49,9 +49,11 @@ export class Secp256k1 { * @param uint512Buf - The buffer to convert. * @returns Buffer representation of the field element. */ - public reduce512BufferToFr(uint512Buf: Buffer) { - this.wasm.writeMemory(0, uint512Buf); - this.wasm.call('ecc_secp256k1__reduce512_buffer_mod_circuit_modulus', 0, 64); - return Buffer.from(this.wasm.getMemorySlice(64, 96)); + public async reduce512BufferToFr(uint512Buf: Buffer) { + const api = await BarretenbergLazy.getSingleton(); + const [result] = await api + .getWasm() + .callWasmExport('ecc_secp256k1__reduce512_buffer_mod_circuit_modulus', [uint512Buf], [32]); + return Buffer.from(result); } } diff --git a/yarn-project/circuits.js/src/barretenberg/serialize.ts b/yarn-project/circuits.js/src/barretenberg/serialize.ts index 04cc095a092..4268f7bf95e 100644 --- a/yarn-project/circuits.js/src/barretenberg/serialize.ts +++ b/yarn-project/circuits.js/src/barretenberg/serialize.ts @@ -72,3 +72,14 @@ export function deserializeField(buf: Buffer, offset = 0) { const adv = 32; return { elem: buf.slice(offset, offset + adv), adv }; } + +export function concatenateUint8Arrays(arrayOfUint8Arrays: Uint8Array[]) { + const totalLength = arrayOfUint8Arrays.reduce((prev, curr) => prev + curr.length, 0); + const result = new Uint8Array(totalLength); + let length = 0; + for (const array of arrayOfUint8Arrays) { + result.set(array, length); + length += array.length; + } + return result; +} diff --git a/yarn-project/cli/src/cmds/devnet/bootstrap_network.ts b/yarn-project/cli/src/cmds/devnet/bootstrap_network.ts index fe8059e0203..99362f430bf 100644 --- a/yarn-project/cli/src/cmds/devnet/bootstrap_network.ts +++ b/yarn-project/cli/src/cmds/devnet/bootstrap_network.ts @@ -46,7 +46,7 @@ export async function bootstrapNetwork( const pxe = await createCompatibleClient(pxeUrl, debugLog); // setup a one-off account contract - const account = getSchnorrAccount(pxe, Fr.random(), Fq.random(), Fr.random()); + const account = await getSchnorrAccount(pxe, Fr.random(), Fq.random(), Fr.random()); const wallet = await account.deploy().getWallet(); const l1Clients = createL1Clients( diff --git a/yarn-project/end-to-end/src/composed/docs_examples.test.ts b/yarn-project/end-to-end/src/composed/docs_examples.test.ts index 774ca89bb8a..05e463459f9 100644 --- a/yarn-project/end-to-end/src/composed/docs_examples.test.ts +++ b/yarn-project/end-to-end/src/composed/docs_examples.test.ts @@ -22,7 +22,8 @@ describe('docs_examples', () => { // docs:end:define_account_vars // docs:start:create_wallet - const wallet = await getSchnorrAccount(pxe, secretKey, signingPrivateKey).waitSetup(); + const account = await getSchnorrAccount(pxe, secretKey, signingPrivateKey); + const wallet = await account.waitSetup(); // docs:end:create_wallet // docs:start:deploy_contract diff --git a/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts b/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts index df8cbcc47b0..d96a40e0fc4 100644 --- a/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts +++ b/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts @@ -62,7 +62,8 @@ describe('Aztec persistence', () => { deployL1ContractsValues = initialContext.deployL1ContractsValues; ownerSecretKey = Fr.random(); - const ownerWallet = await getUnsafeSchnorrAccount(initialContext.pxe, ownerSecretKey, Fr.ZERO).waitSetup(); + const ownerAccount = await getUnsafeSchnorrAccount(initialContext.pxe, ownerSecretKey, Fr.ZERO); + const ownerWallet = await ownerAccount.waitSetup(); ownerAddress = ownerWallet.getCompleteAddress(); ownerSalt = ownerWallet.salt; @@ -162,7 +163,8 @@ describe('Aztec persistence', () => { }); it('allows spending of private notes', async () => { - const otherWallet = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO).waitSetup(); + const otherAccount = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO); + const otherWallet = await otherAccount.waitSetup(); const initialOwnerBalance = await contract.methods.balance_of_private(ownerWallet.getAddress()).simulate(); @@ -204,7 +206,8 @@ describe('Aztec persistence', () => { }); it('pxe does not know of the deployed contract', async () => { - const wallet = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO).waitSetup(); + const account = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO); + const wallet = await account.waitSetup(); await expect(TokenBlacklistContract.at(contractAddress, wallet)).rejects.toThrow(/has not been registered/); }); @@ -214,7 +217,8 @@ describe('Aztec persistence', () => { instance: contractInstance, }); - const wallet = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO).waitSetup(); + const account = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO); + const wallet = await account.waitSetup(); const contract = await TokenBlacklistContract.at(contractAddress, wallet); await expect(contract.methods.balance_of_private(ownerAddress.address).simulate()).resolves.toEqual(0n); }); @@ -225,7 +229,8 @@ describe('Aztec persistence', () => { instance: contractInstance, }); - const wallet = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO).waitSetup(); + const account = await getUnsafeSchnorrAccount(context.pxe, Fr.random(), Fr.ZERO); + const wallet = await account.waitSetup(); const contract = await TokenBlacklistContract.at(contractAddress, wallet); await expect(contract.methods.total_supply().simulate()).resolves.toBeGreaterThan(0n); @@ -237,7 +242,7 @@ describe('Aztec persistence', () => { instance: contractInstance, }); - const ownerAccount = getUnsafeSchnorrAccount(context.pxe, ownerSecretKey, ownerSalt); + const ownerAccount = await getUnsafeSchnorrAccount(context.pxe, ownerSecretKey, ownerSalt); await ownerAccount.register(); const ownerWallet = await ownerAccount.getWallet(); const contract = await TokenBlacklistContract.at(contractAddress, ownerWallet); @@ -266,7 +271,7 @@ describe('Aztec persistence', () => { instance: contractInstance, }); - const ownerAccount = getUnsafeSchnorrAccount(temporaryContext.pxe, ownerSecretKey, ownerSalt); + const ownerAccount = await getUnsafeSchnorrAccount(temporaryContext.pxe, ownerSecretKey, ownerSalt); await ownerAccount.register(); const ownerWallet = await ownerAccount.getWallet(); diff --git a/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts b/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts index 4b7b34e47a6..d1498dba6ff 100644 --- a/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts +++ b/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts @@ -55,6 +55,7 @@ end-to-end-1 | at Object. (composed/e2e_sandbox_example.test.t import { getSchnorrAccount } from '@aztec/accounts/schnorr'; import { getDeployedTestAccountsWallets } from '@aztec/accounts/testing'; import { Fr, GrumpkinScalar, type PXE, createLogger, createPXEClient, waitForPXE } from '@aztec/aztec.js'; +import { timesParallel } from '@aztec/foundation/collection'; import { format } from 'util'; @@ -182,15 +183,13 @@ describe('e2e_sandbox_example', () => { // Creates new accounts using an account contract that verifies schnorr signatures // Returns once the deployment transactions have settled const createSchnorrAccounts = async (numAccounts: number, pxe: PXE) => { - const accountManagers = Array(numAccounts) - .fill(0) - .map(() => - getSchnorrAccount( - pxe, - Fr.random(), // secret key - GrumpkinScalar.random(), // signing private key - ), - ); + const accountManagers = await timesParallel(numAccounts, () => + getSchnorrAccount( + pxe, + Fr.random(), // secret key + GrumpkinScalar.random(), // signing private key + ), + ); return await Promise.all( accountManagers.map(async x => { await x.waitSetup({}); diff --git a/yarn-project/end-to-end/src/devnet/e2e_smoke.test.ts b/yarn-project/end-to-end/src/devnet/e2e_smoke.test.ts index 334af344c63..b1ac1e01519 100644 --- a/yarn-project/end-to-end/src/devnet/e2e_smoke.test.ts +++ b/yarn-project/end-to-end/src/devnet/e2e_smoke.test.ts @@ -146,7 +146,7 @@ describe('End-to-end tests for devnet', () => { it('deploys an account while paying with FeeJuice', async () => { const privateKey = Fr.random(); const l1Account = await cli<{ privateKey: string; address: string }>('create-l1-account'); - const l2Account = getSchnorrAccount(pxe, privateKey, deriveSigningKey(privateKey), Fr.ZERO); + const l2Account = await getSchnorrAccount(pxe, privateKey, deriveSigningKey(privateKey), Fr.ZERO); await expect(getL1Balance(l1Account.address)).resolves.toEqual(0n); await expect(getL1Balance(l1Account.address, feeJuiceL1)).resolves.toEqual(0n); diff --git a/yarn-project/end-to-end/src/e2e_2_pxes.test.ts b/yarn-project/end-to-end/src/e2e_2_pxes.test.ts index 5317a5ff336..342fb690ac3 100644 --- a/yarn-project/end-to-end/src/e2e_2_pxes.test.ts +++ b/yarn-project/end-to-end/src/e2e_2_pxes.test.ts @@ -178,13 +178,13 @@ describe('e2e_2_pxes', () => { // setup an account that is shared across PXEs const sharedSecretKey = Fr.random(); - const sharedAccountOnA = getUnsafeSchnorrAccount(pxeA, sharedSecretKey, Fr.random()); + const sharedAccountOnA = await getUnsafeSchnorrAccount(pxeA, sharedSecretKey, Fr.random()); const sharedAccountAddress = sharedAccountOnA.getCompleteAddress(); const sharedWalletOnA = await sharedAccountOnA.waitSetup(); await sharedWalletOnA.registerSender(walletA.getAddress()); - const sharedAccountOnB = getUnsafeSchnorrAccount(pxeB, sharedSecretKey, sharedAccountOnA.salt); + const sharedAccountOnB = await getUnsafeSchnorrAccount(pxeB, sharedSecretKey, sharedAccountOnA.salt); await sharedAccountOnB.register(); const sharedWalletOnB = await sharedAccountOnB.getWallet(); diff --git a/yarn-project/end-to-end/src/e2e_account_contracts.test.ts b/yarn-project/end-to-end/src/e2e_account_contracts.test.ts index 1f6385417aa..c14ac9fa8a0 100644 --- a/yarn-project/end-to-end/src/e2e_account_contracts.test.ts +++ b/yarn-project/end-to-end/src/e2e_account_contracts.test.ts @@ -66,7 +66,7 @@ function itShouldBehaveLikeAnAccountContract( describe('e2e_account_contracts', () => { const walletSetup = async (pxe: PXE, secretKey: Fr, accountContract: AccountContract) => { - const account = new AccountManager(pxe, secretKey, accountContract); + const account = await AccountManager.create(pxe, secretKey, accountContract); return await account.waitSetup(); }; diff --git a/yarn-project/end-to-end/src/e2e_blacklist_token_contract/blacklist_token_contract_test.ts b/yarn-project/end-to-end/src/e2e_blacklist_token_contract/blacklist_token_contract_test.ts index 152cb475163..68e19afdba0 100644 --- a/yarn-project/end-to-end/src/e2e_blacklist_token_contract/blacklist_token_contract_test.ts +++ b/yarn-project/end-to-end/src/e2e_blacklist_token_contract/blacklist_token_contract_test.ts @@ -92,12 +92,16 @@ export class BlacklistTokenContractTest { jest.setTimeout(120_000); await this.snapshotManager.snapshot('3_accounts', addAccounts(3, this.logger), async ({ accountKeys }, { pxe }) => { - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); - this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); + this.wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(pxe, ak[0], ak[1], 1); + return account.getWallet(); + }), + ); this.admin = this.wallets[0]; this.other = this.wallets[1]; this.blacklisted = this.wallets[2]; - this.accounts = accountManagers.map(a => a.getCompleteAddress()); + this.accounts = this.wallets.map(w => w.getCompleteAddress()); }); await this.snapshotManager.snapshot( diff --git a/yarn-project/end-to-end/src/e2e_block_building.test.ts b/yarn-project/end-to-end/src/e2e_block_building.test.ts index dc2afd3d7af..2236261d002 100644 --- a/yarn-project/end-to-end/src/e2e_block_building.test.ts +++ b/yarn-project/end-to-end/src/e2e_block_building.test.ts @@ -408,7 +408,7 @@ describe('e2e_block_building', () => { // account setup const privateKey = new Fr(7n); const keys = deriveKeys(privateKey); - const account = getSchnorrAccount(pxe, privateKey, keys.masterIncomingViewingSecretKey); + const account = await getSchnorrAccount(pxe, privateKey, keys.masterIncomingViewingSecretKey); await account.deploy().wait(); const thisWallet = await account.getWallet(); const sender = thisWallet.getAddress(); @@ -436,7 +436,7 @@ describe('e2e_block_building', () => { // account setup const privateKey = new Fr(7n); const keys = deriveKeys(privateKey); - const account = getSchnorrAccount(pxe, privateKey, keys.masterIncomingViewingSecretKey); + const account = await getSchnorrAccount(pxe, privateKey, keys.masterIncomingViewingSecretKey); await account.deploy().wait(); const thisWallet = await account.getWallet(); const sender = thisWallet.getAddress(); @@ -492,7 +492,7 @@ describe('e2e_block_building', () => { })); await sleep(1000); - const account = getSchnorrAccount(pxe, Fr.random(), Fq.random(), Fr.random()); + const account = await getSchnorrAccount(pxe, Fr.random(), Fq.random(), Fr.random()); await account.waitSetup(); }); diff --git a/yarn-project/end-to-end/src/e2e_card_game.test.ts b/yarn-project/end-to-end/src/e2e_card_game.test.ts index 9ba38b9c435..d8e1e4aa8ce 100644 --- a/yarn-project/end-to-end/src/e2e_card_game.test.ts +++ b/yarn-project/end-to-end/src/e2e_card_game.test.ts @@ -119,7 +119,7 @@ describe('e2e_card_game', () => { for (let i = 0; i < secretKeysToRegister.length; i++) { logger.info(`Deploying account contract ${i}/${secretKeysToRegister.length}...`); const encryptionPrivateKey = secretKeysToRegister[i]; - const account = getSchnorrAccount(pxe, encryptionPrivateKey, GrumpkinScalar.random()); + const account = await getSchnorrAccount(pxe, encryptionPrivateKey, GrumpkinScalar.random()); const wallet = await account.waitSetup({ interval: 0.1 }); wallets.push(wallet); } diff --git a/yarn-project/end-to-end/src/e2e_cross_chain_messaging/cross_chain_messaging_test.ts b/yarn-project/end-to-end/src/e2e_cross_chain_messaging/cross_chain_messaging_test.ts index 966137857cd..a26894f7e2d 100644 --- a/yarn-project/end-to-end/src/e2e_cross_chain_messaging/cross_chain_messaging_test.ts +++ b/yarn-project/end-to-end/src/e2e_cross_chain_messaging/cross_chain_messaging_test.ts @@ -86,9 +86,13 @@ export class CrossChainMessagingTest { '3_accounts', addAccounts(3, this.logger), async ({ accountKeys }, { pxe, aztecNodeConfig, aztecNode, deployL1ContractsValues }) => { - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); - this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); - this.accounts = accountManagers.map(a => a.getCompleteAddress()); + this.wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(pxe, ak[0], ak[1], 1); + return account.getWallet(); + }), + ); + this.accounts = this.wallets.map(w => w.getCompleteAddress()); this.wallets.forEach((w, i) => this.logger.verbose(`Wallet ${i} address: ${w.getAddress()}`)); this.rollup = getContract({ diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract/deploy_test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract/deploy_test.ts index 4dbbc665577..6b2e8513436 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract/deploy_test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract/deploy_test.ts @@ -49,8 +49,12 @@ export class DeployTest { 'initial_account', addAccounts(1, this.logger), async ({ accountKeys }, { pxe }) => { - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); - this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); + this.wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(pxe, ak[0], ak[1], 1); + return account.getWallet(); + }), + ); this.wallets.forEach((w, i) => this.logger.verbose(`Wallet ${i} address: ${w.getAddress()}`)); this.wallet = this.wallets[0]; }, diff --git a/yarn-project/end-to-end/src/e2e_fees/account_init.test.ts b/yarn-project/end-to-end/src/e2e_fees/account_init.test.ts index e997538a6d1..1348b5001da 100644 --- a/yarn-project/end-to-end/src/e2e_fees/account_init.test.ts +++ b/yarn-project/end-to-end/src/e2e_fees/account_init.test.ts @@ -77,7 +77,7 @@ describe('e2e_fees account_init', () => { beforeEach(async () => { bobsSecretKey = Fr.random(); bobsPrivateSigningKey = Fq.random(); - bobsAccountManager = getSchnorrAccount(pxe, bobsSecretKey, bobsPrivateSigningKey, Fr.random()); + bobsAccountManager = await getSchnorrAccount(pxe, bobsSecretKey, bobsPrivateSigningKey, Fr.random()); bobsCompleteAddress = bobsAccountManager.getCompleteAddress(); bobsAddress = bobsCompleteAddress.address; bobsWallet = await bobsAccountManager.getWallet(); @@ -167,7 +167,7 @@ describe('e2e_fees account_init', () => { // bob generates the private keys for his account on his own const bobsPublicKeys = deriveKeys(bobsSecretKey).publicKeys; - const bobsSigningPubKey = new Schnorr().computePublicKey(bobsPrivateSigningKey); + const bobsSigningPubKey = await new Schnorr().computePublicKey(bobsPrivateSigningKey); const bobsInstance = bobsAccountManager.getInstance(); // and deploys bob's account, paying the fee from her balance diff --git a/yarn-project/end-to-end/src/e2e_fees/fees_test.ts b/yarn-project/end-to-end/src/e2e_fees/fees_test.ts index 7a5c2de8cbf..18af1e1914d 100644 --- a/yarn-project/end-to-end/src/e2e_fees/fees_test.ts +++ b/yarn-project/end-to-end/src/e2e_fees/fees_test.ts @@ -136,7 +136,7 @@ export class FeesTest { this.aztecNode = aztecNode; this.gasSettings = GasSettings.default({ maxFeesPerGas: (await this.aztecNode.getCurrentBaseFees()).mul(2) }); this.cheatCodes = await CheatCodes.create(aztecNodeConfig.l1RpcUrl, pxe); - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); + const accountManagers = await Promise.all(accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1))); await Promise.all(accountManagers.map(a => a.register())); this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); this.wallets.forEach((w, i) => this.logger.verbose(`Wallet ${i} address: ${w.getAddress()}`)); diff --git a/yarn-project/end-to-end/src/e2e_l1_with_wall_time.test.ts b/yarn-project/end-to-end/src/e2e_l1_with_wall_time.test.ts index 52eab07fa8a..4a312cc80c6 100644 --- a/yarn-project/end-to-end/src/e2e_l1_with_wall_time.test.ts +++ b/yarn-project/end-to-end/src/e2e_l1_with_wall_time.test.ts @@ -43,10 +43,10 @@ describe('e2e_l1_with_wall_time', () => { const submitTxsTo = async (pxe: PXEService, numTxs: number) => { const provenTxs = []; for (let i = 0; i < numTxs; i++) { - const accountManager = getSchnorrAccount(pxe, Fr.random(), GrumpkinScalar.random(), Fr.random()); + const accountManager = await getSchnorrAccount(pxe, Fr.random(), GrumpkinScalar.random(), Fr.random()); const deployMethod = await accountManager.getDeployMethod(); const tx = await deployMethod.prove({ - contractAddressSalt: accountManager.salt, + contractAddressSalt: new Fr(accountManager.salt), skipClassRegistration: true, skipPublicDeployment: true, universalDeploy: true, diff --git a/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts b/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts index 77480e552e6..0ec6529038a 100644 --- a/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts +++ b/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts @@ -33,7 +33,7 @@ describe('e2e_multiple_accounts_1_enc_key', () => { for (let i = 0; i < numAccounts; i++) { logger.info(`Deploying account contract ${i}/3...`); const signingPrivateKey = GrumpkinScalar.random(); - const account = getSchnorrAccount(pxe, encryptionPrivateKey, signingPrivateKey); + const account = await getSchnorrAccount(pxe, encryptionPrivateKey, signingPrivateKey); const wallet = await account.waitSetup({ interval: 0.1 }); const completeAddress = account.getCompleteAddress(); wallets.push(wallet); diff --git a/yarn-project/end-to-end/src/e2e_nested_contract/nested_contract_test.ts b/yarn-project/end-to-end/src/e2e_nested_contract/nested_contract_test.ts index 733ecde7318..8b6bf5e1f78 100644 --- a/yarn-project/end-to-end/src/e2e_nested_contract/nested_contract_test.ts +++ b/yarn-project/end-to-end/src/e2e_nested_contract/nested_contract_test.ts @@ -35,8 +35,12 @@ export class NestedContractTest { */ async applyBaseSnapshots() { await this.snapshotManager.snapshot('3_accounts', addAccounts(3, this.logger), async ({ accountKeys }, { pxe }) => { - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); - this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); + this.wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(pxe, ak[0], ak[1], 1); + return account.getWallet(); + }), + ); this.accounts = await pxe.getRegisteredAccounts(); this.wallets.forEach((w, i) => this.logger.verbose(`Wallet ${i} address: ${w.getAddress()}`)); diff --git a/yarn-project/end-to-end/src/e2e_p2p/p2p_network.ts b/yarn-project/end-to-end/src/e2e_p2p/p2p_network.ts index ce7d27170c6..4ffe59423a6 100644 --- a/yarn-project/end-to-end/src/e2e_p2p/p2p_network.ts +++ b/yarn-project/end-to-end/src/e2e_p2p/p2p_network.ts @@ -243,9 +243,13 @@ export class P2PNetworkTest { 'setup-account', addAccounts(1, this.logger, false), async ({ accountKeys }, ctx) => { - const accountManagers = accountKeys.map(ak => getSchnorrAccount(ctx.pxe, ak[0], ak[1], 1)); - await Promise.all(accountManagers.map(a => a.register())); - const wallets = await Promise.all(accountManagers.map(a => a.getWallet())); + const wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(ctx.pxe, ak[0], ak[1], 1); + return account.getWallet(); + }), + ); + this.wallet = wallets[0]; }, ); diff --git a/yarn-project/end-to-end/src/e2e_p2p/shared.ts b/yarn-project/end-to-end/src/e2e_p2p/shared.ts index 07347e224dc..f98d4f4d622 100644 --- a/yarn-project/end-to-end/src/e2e_p2p/shared.ts +++ b/yarn-project/end-to-end/src/e2e_p2p/shared.ts @@ -65,10 +65,10 @@ export const createPXEServiceAndSubmitTransactions = async ( const submitTxsTo = async (logger: Logger, pxe: PXEService, numTxs: number) => { const provenTxs = []; for (let i = 0; i < numTxs; i++) { - const accountManager = getSchnorrAccount(pxe, Fr.random(), GrumpkinScalar.random(), Fr.random()); + const accountManager = await getSchnorrAccount(pxe, Fr.random(), GrumpkinScalar.random(), Fr.random()); const deployMethod = await accountManager.getDeployMethod(); const tx = await deployMethod.prove({ - contractAddressSalt: accountManager.salt, + contractAddressSalt: new Fr(accountManager.salt), skipClassRegistration: true, skipPublicDeployment: true, universalDeploy: true, diff --git a/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts b/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts index 38fd15d980b..c7ecec3c7e6 100644 --- a/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts +++ b/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts @@ -112,9 +112,13 @@ export class FullProverTest { async applyBaseSnapshots() { await this.snapshotManager.snapshot('2_accounts', addAccounts(2, this.logger), async ({ accountKeys }, { pxe }) => { this.keys = accountKeys; - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], SALT)); - this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); - this.accounts = accountManagers.map(a => a.getCompleteAddress()); + this.wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(pxe, ak[0], ak[1], SALT); + return account.getWallet(); + }), + ); + this.accounts = this.wallets.map(w => w.getCompleteAddress()); this.wallets.forEach((w, i) => this.logger.verbose(`Wallet ${i} address: ${w.getAddress()}`)); }); @@ -227,7 +231,7 @@ export class FullProverTest { await this.pxe.registerAccount(this.keys[i][0], this.wallets[i].getCompleteAddress().partialAddress); } - const account = getSchnorrAccount(result.pxe, this.keys[0][0], this.keys[0][1], SALT); + const account = await getSchnorrAccount(result.pxe, this.keys[0][0], this.keys[0][1], SALT); await result.pxe.registerContract({ instance: account.getInstance(), diff --git a/yarn-project/end-to-end/src/e2e_synching.test.ts b/yarn-project/end-to-end/src/e2e_synching.test.ts index 8b7d393faa9..40dc9a1a8b9 100644 --- a/yarn-project/end-to-end/src/e2e_synching.test.ts +++ b/yarn-project/end-to-end/src/e2e_synching.test.ts @@ -146,13 +146,13 @@ class TestVariant { async deployWallets(numberOfAccounts: number) { // Create accounts such that we can send from many to not have colliding nullifiers const { accountKeys } = await addAccounts(numberOfAccounts, this.logger, false)({ pxe: this.pxe }); - const accountManagers = accountKeys.map(ak => getSchnorrAccount(this.pxe, ak[0], ak[1], 1)); return await Promise.all( - accountManagers.map(async (a, i) => { - const partialAddress = a.getCompleteAddress().partialAddress; + accountKeys.map(async (ak, i) => { + const account = await getSchnorrAccount(this.pxe, ak[0], ak[1], 1); + const partialAddress = account.getCompleteAddress().partialAddress; await this.pxe.registerAccount(accountKeys[i][0], partialAddress); - const wallet = await a.getWallet(); + const wallet = await account.getWallet(); this.logger.verbose(`Wallet ${i} address: ${wallet.getAddress()} registered`); return wallet; }), @@ -192,11 +192,11 @@ class TestVariant { if (this.txComplexity == TxComplexity.Deployment) { const txs = []; for (let i = 0; i < this.txCount; i++) { - const accountManager = getSchnorrAccount(this.pxe, Fr.random(), GrumpkinScalar.random(), Fr.random()); + const accountManager = await getSchnorrAccount(this.pxe, Fr.random(), GrumpkinScalar.random(), Fr.random()); this.contractAddresses.push(accountManager.getAddress()); const deployMethod = await accountManager.getDeployMethod(); const tx = deployMethod.send({ - contractAddressSalt: accountManager.salt, + contractAddressSalt: new Fr(accountManager.salt), skipClassRegistration: true, skipPublicDeployment: true, universalDeploy: true, diff --git a/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts b/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts index c2d625f60dc..cfd09bfedfc 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts @@ -46,9 +46,13 @@ export class TokenContractTest { jest.setTimeout(120_000); await this.snapshotManager.snapshot('3_accounts', addAccounts(3, this.logger), async ({ accountKeys }, { pxe }) => { - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); - this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); - this.accounts = accountManagers.map(a => a.getCompleteAddress()); + this.wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(pxe, ak[0], ak[1], 1); + return account.getWallet(); + }), + ); + this.accounts = this.wallets.map(w => w.getCompleteAddress()); }); await this.snapshotManager.snapshot( diff --git a/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts b/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts index 4ecc05a64fe..65f92922c27 100644 --- a/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts +++ b/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts @@ -553,7 +553,7 @@ export const addAccounts = logger.verbose('Simulating account deployment...'); const provenTxs = await Promise.all( accountKeys.map(async ([secretKey, signPk], index) => { - const account = getSchnorrAccount(pxe, secretKey, signPk, 1); + const account = await getSchnorrAccount(pxe, secretKey, signPk, 1); // only register the contract class once let skipClassRegistration = true; @@ -566,7 +566,7 @@ export const addAccounts = const deployMethod = await account.getDeployMethod(); const provenTx = await deployMethod.prove({ - contractAddressSalt: account.salt, + contractAddressSalt: new Fr(account.salt), skipClassRegistration, skipPublicDeployment: true, universalDeploy: true, diff --git a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts index 3ee8bc29725..9005af5888f 100644 --- a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts +++ b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts @@ -22,17 +22,17 @@ class SchnorrHardcodedKeyAccountContract extends DefaultAccountContract { super(SchnorrHardcodedAccountContractArtifact); } - getDeploymentArgs(): undefined { + getDeploymentArgs() { // This contract has no constructor - return undefined; + return Promise.resolve(undefined); } getAuthWitnessProvider(_address: CompleteAddress): AuthWitnessProvider { const privateKey = this.privateKey; return { - createAuthWit(messageHash: Fr): Promise { + async createAuthWit(messageHash: Fr): Promise { const signer = new Schnorr(); - const signature = signer.constructSignature(messageHash.toBuffer(), privateKey); + const signature = await signer.constructSignature(messageHash.toBuffer(), privateKey); return Promise.resolve(new AuthWitness(messageHash, [...signature.toBuffer()])); }, }; @@ -53,7 +53,7 @@ describe('guides/writing_an_account_contract', () => { const { pxe, logger } = context; // docs:start:account-contract-deploy const secretKey = Fr.random(); - const account = new AccountManager(pxe, secretKey, new SchnorrHardcodedKeyAccountContract()); + const account = await AccountManager.create(pxe, secretKey, new SchnorrHardcodedKeyAccountContract()); const wallet = await account.waitSetup(); const address = wallet.getCompleteAddress().address; // docs:end:account-contract-deploy @@ -75,7 +75,7 @@ describe('guides/writing_an_account_contract', () => { // docs:start:account-contract-fails const wrongKey = GrumpkinScalar.random(); const wrongAccountContract = new SchnorrHardcodedKeyAccountContract(wrongKey); - const wrongAccount = new AccountManager(pxe, secretKey, wrongAccountContract, account.salt); + const wrongAccount = await AccountManager.create(pxe, secretKey, wrongAccountContract, account.salt); const wrongWallet = await wrongAccount.getWallet(); const tokenWithWrongWallet = token.withWallet(wrongWallet); diff --git a/yarn-project/end-to-end/src/prover-coordination/e2e_prover_coordination.test.ts b/yarn-project/end-to-end/src/prover-coordination/e2e_prover_coordination.test.ts index 7e70f26a566..55376d28df3 100644 --- a/yarn-project/end-to-end/src/prover-coordination/e2e_prover_coordination.test.ts +++ b/yarn-project/end-to-end/src/prover-coordination/e2e_prover_coordination.test.ts @@ -70,9 +70,12 @@ describe('e2e_prover_coordination', () => { ); await snapshotManager.snapshot('setup', addAccounts(2, logger), async ({ accountKeys }, ctx) => { - const accountManagers = accountKeys.map(ak => getSchnorrAccount(ctx.pxe, ak[0], ak[1], 1)); - await Promise.all(accountManagers.map(a => a.register())); - const wallets = await Promise.all(accountManagers.map(a => a.getWallet())); + const wallets = await Promise.all( + accountKeys.map(async ak => { + const account = await getSchnorrAccount(ctx.pxe, ak[0], ak[1], 1); + return account.getWallet(); + }), + ); wallets.forEach((w, i) => logger.verbose(`Wallet ${i} address: ${w.getAddress()}`)); wallet = wallets[0]; recipient = wallets[1].getAddress(); diff --git a/yarn-project/end-to-end/src/shared/browser.ts b/yarn-project/end-to-end/src/shared/browser.ts index b1b2f3fd863..fff8c7a874a 100644 --- a/yarn-project/end-to-end/src/shared/browser.ts +++ b/yarn-project/end-to-end/src/shared/browser.ts @@ -126,7 +126,7 @@ export const browserTestSuite = ( const { Fr, createPXEClient, getUnsafeSchnorrAccount } = window.AztecJs; const pxe = createPXEClient(rpcUrl!); const secretKey = Fr.fromHexString(secretKeyString); - const account = getUnsafeSchnorrAccount(pxe, secretKey); + const account = await getUnsafeSchnorrAccount(pxe, secretKey); await account.waitSetup(); const completeAddress = account.getCompleteAddress(); const addressString = completeAddress.address.toString(); @@ -194,7 +194,8 @@ export const browserTestSuite = ( getUnsafeSchnorrAccount, } = window.AztecJs; const pxe = createPXEClient(rpcUrl!); - const newReceiverAccount = await getUnsafeSchnorrAccount(pxe, AztecJs.Fr.random()).waitSetup(); + const newReceiverAccountManager = await getUnsafeSchnorrAccount(pxe, AztecJs.Fr.random()); + const newReceiverAccount = await newReceiverAccountManager.waitSetup(); const receiverAddress = newReceiverAccount.getCompleteAddress().address; const [wallet] = await getDeployedTestAccountsWallets(pxe); const contract = await Contract.at(AztecAddress.fromString(contractAddress), TokenContractArtifact, wallet); @@ -234,12 +235,13 @@ export const browserTestSuite = ( // we need to ensure that a known account is present in order to create a wallet const knownAccounts = await getDeployedTestAccountsWallets(pxe); if (!knownAccounts.length) { - const newAccount = await getSchnorrAccount( + const newAccountManager = await getSchnorrAccount( pxe, INITIAL_TEST_SECRET_KEYS[0], INITIAL_TEST_SIGNING_KEYS[0], INITIAL_TEST_ACCOUNT_SALTS[0], - ).waitSetup(); + ); + const newAccount = await newAccountManager.waitSetup(); knownAccounts.push(newAccount); } const owner = knownAccounts[0]; diff --git a/yarn-project/end-to-end/src/spartan/setup_test_wallets.ts b/yarn-project/end-to-end/src/spartan/setup_test_wallets.ts index ae3abd9b625..b78f8ec3a75 100644 --- a/yarn-project/end-to-end/src/spartan/setup_test_wallets.ts +++ b/yarn-project/end-to-end/src/spartan/setup_test_wallets.ts @@ -31,7 +31,7 @@ export async function setupTestWalletsWithTokens( { const { accountKeys } = await addAccounts(1, logger, false)({ pxe }); - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); + const accountManagers = await Promise.all(accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1))); const partialAddress = accountManagers[0].getCompleteAddress().partialAddress; await pxe.registerAccount(accountKeys[0][0], partialAddress); @@ -40,7 +40,7 @@ export async function setupTestWalletsWithTokens( } const { accountKeys } = await addAccounts(WALLET_COUNT, logger, false)({ pxe }); - const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); + const accountManagers = await Promise.all(accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1))); const wallets = await Promise.all( accountManagers.map(async (a, i) => { diff --git a/yarn-project/foundation/src/fields/fields.ts b/yarn-project/foundation/src/fields/fields.ts index cd1cc15ccaf..a9c0e49d6f2 100644 --- a/yarn-project/foundation/src/fields/fields.ts +++ b/yarn-project/foundation/src/fields/fields.ts @@ -320,14 +320,13 @@ export class Fr extends BaseField { */ async sqrt(): Promise { const wasm = (await BarretenbergLazy.getSingleton()).getWasm(); - const [buf] = await wasm.callWasmExport('bn254_fr_sqrt', [this.toBuffer()], [Fr.SIZE_IN_BYTES * 2 + 1]); + const [buf] = await wasm.callWasmExport('bn254_fr_sqrt', [this.toBuffer()], [Fr.SIZE_IN_BYTES + 1]); const isSqrt = buf[0] === 1; if (!isSqrt) { // Field element is not a quadratic residue mod p so it has no square root. return null; } - const reader = BufferReader.asReader(buf.slice(1)); - return new Fr(reader.readBytes(Fr.SIZE_IN_BYTES)); + return new Fr(Buffer.from(buf.slice(1))); } toJSON() { diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index 38e5b02762c..63f8766e4b5 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -264,8 +264,8 @@ export class TXE implements TypedOracle { const account = await this.txeDatabase.getAccount(address); const privateKey = await this.keyStore.getMasterSecretKey(account.publicKeys.masterIncomingViewingPublicKey); const schnorr = new Schnorr(); - const signature = schnorr.constructSignature(messageHash.toBuffer(), privateKey).toBuffer(); - const authWitness = new AuthWitness(messageHash, [...signature]); + const signature = await schnorr.constructSignature(messageHash.toBuffer(), privateKey); + const authWitness = new AuthWitness(messageHash, [...signature.toBuffer()]); return this.txeDatabase.addAuthWitness(authWitness.requestHash, authWitness.witness); }