Skip to content

Commit

Permalink
feat(aztec.js)!: Move accounts out of aztec.js into new package (Azte…
Browse files Browse the repository at this point in the history
…cProtocol#3844)

Extracts account contract implementations and related classes, as well
as the default base classes, to a new `@aztec/accounts` package. Updates
docs to reference the new package, and includes it in the API ref.

This means that all scripts that relied on using accounts from the
Sandbox, creating new accounts, or using accounts already deployed, will
now need to import this package.

Since `accounts` is now needed along with `aztec.js` for most stuff,
including web, this PR also adds a new build step for end-to-end tests
to exercise web builds. Instead of building aztec.js via webpack as part
of aztec.js, we create a new "web application" as part of the end-to-end
tests, which includes both aztec.js and accounts, and test that. This
flow should be closer to how end-users will interact with aztec.js on
the web.

Fixes AztecProtocol#3807
  • Loading branch information
spalladino committed Jan 8, 2024
1 parent f3d37d7 commit afd7b6d
Show file tree
Hide file tree
Showing 103 changed files with 773 additions and 357 deletions.
2 changes: 1 addition & 1 deletion boxes/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Aztec Starter Kits

A collection of projects that act as starter kits for Aztec.
These depend on aztec.js and aztec.nr, and point to the monorepos local development versions.
These depend on accounts, aztec.js and aztec.nr, and point to the monorepos local development versions.
These projects depend on the sandbox. They're built and tested after the sandbox by docker compose files.
1 change: 1 addition & 0 deletions boxes/blank-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"rootDir": "./src"
},
"dependencies": {
"@aztec/accounts": "^0.16.9",
"@aztec/aztec-ui": "^0.1.14",
"@aztec/aztec.js": "^0.16.9",
"classnames": "^2.3.2",
Expand Down
2 changes: 1 addition & 1 deletion boxes/blank-react/src/app/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand Down
12 changes: 3 additions & 9 deletions boxes/blank-react/src/scripts/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import {
AccountWallet,
CompleteAddress,
Fr,
FunctionArtifact,
PXE,
encodeArguments,
getSandboxAccountsWallets,
} from '@aztec/aztec.js';
import { AccountWallet, CompleteAddress, Fr, FunctionArtifact, PXE, encodeArguments } from '@aztec/aztec.js';

import { getSandboxAccountsWallets } from '@aztec/accounts/testing';

export function convertArgs(functionAbi: FunctionArtifact, args: any): Fr[] {
const untypedArgs = functionAbi.parameters.map(param => {
Expand Down
1 change: 1 addition & 0 deletions boxes/blank/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"rootDir": "./src"
},
"dependencies": {
"@aztec/accounts": "^0.16.9",
"@aztec/aztec.js": "^0.16.9",
"serve": "^14.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion boxes/blank/src/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand Down
3 changes: 2 additions & 1 deletion boxes/blank/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
TxReceipt,
createPXEClient,
encodeArguments,
getSandboxAccountsWallets,
} from '@aztec/aztec.js';

import { getSandboxAccountsWallets } from '@aztec/accounts/testing';

// docs:end:imports

export const contractArtifact: ContractArtifact = BlankContractArtifact;
Expand Down
1 change: 1 addition & 0 deletions boxes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"token"
],
"resolutions": {
"@aztec/accounts": "portal:../yarn-project/accounts",
"@aztec/aztec.js": "portal:../yarn-project/aztec.js",
"@aztec/circuits.js": "portal:../yarn-project/circuits.js",
"@aztec/foundation": "portal:../yarn-project/foundation",
Expand Down
1 change: 1 addition & 0 deletions boxes/token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"rootDir": "./src"
},
"dependencies": {
"@aztec/accounts": "^0.16.9",
"@aztec/aztec-ui": "^0.1.14",
"@aztec/aztec.js": "^0.16.9",
"classnames": "^2.3.2",
Expand Down
2 changes: 1 addition & 1 deletion boxes/token/src/app/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand Down
12 changes: 3 additions & 9 deletions boxes/token/src/scripts/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import {
AccountWallet,
CompleteAddress,
Fr,
FunctionArtifact,
PXE,
encodeArguments,
getSandboxAccountsWallets,
} from '@aztec/aztec.js';
import { AccountWallet, CompleteAddress, Fr, FunctionArtifact, PXE, encodeArguments } from '@aztec/aztec.js';

import { getSandboxAccountsWallets } from '@aztec/accounts/testing';

function convertBasicArg(paramType: string, value: any) {
switch (paramType) {
Expand Down
4 changes: 3 additions & 1 deletion boxes/token/src/tests/token.contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
computeMessageSecretHash,
createDebugLogger,
createPXEClient,
getSandboxAccountsWallets,
waitForSandbox,
} from '@aztec/aztec.js';

import { getSandboxAccountsWallets } from '@aztec/accounts/testing';

import { afterEach, beforeAll, expect, jest } from '@jest/globals';

// assumes sandbox is running locally, which this script does not trigger
Expand Down
6 changes: 5 additions & 1 deletion boxes/token/src/tests/token_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export class TokenSimulator {
private balancePublic: Map<AztecAddress, bigint> = new Map();
public totalSupply: bigint = 0n;

constructor(protected token: TokenContract, protected logger: DebugLogger, protected accounts: AztecAddress[]) {}
constructor(
protected token: TokenContract,
protected logger: DebugLogger,
protected accounts: AztecAddress[],
) {}

public mintPrivate(amount: bigint) {
this.totalSupply += amount;
Expand Down
24 changes: 16 additions & 8 deletions boxes/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ __metadata:
languageName: node
linkType: hard

"@aztec/accounts@portal:../yarn-project/accounts::locator=%40aztec%2Fboxes%40workspace%3A.":
version: 0.0.0-use.local
resolution: "@aztec/accounts@portal:../yarn-project/accounts::locator=%40aztec%2Fboxes%40workspace%3A."
dependencies:
"@aztec/aztec.js": "workspace:^"
"@aztec/circuits.js": "workspace:^"
"@aztec/ethereum": "workspace:^"
"@aztec/foundation": "workspace:^"
"@aztec/types": "workspace:^"
tslib: "npm:^2.4.0"
languageName: node
linkType: soft

"@aztec/aztec-ui@npm:^0.1.14":
version: 0.1.14
resolution: "@aztec/aztec-ui@npm:0.1.14"
Expand Down Expand Up @@ -58,7 +71,6 @@ __metadata:
"@aztec/types": "workspace:^"
lodash.every: "npm:^4.6.0"
lodash.partition: "npm:^4.6.0"
lodash.zip: "npm:^4.2.0"
tslib: "npm:^2.4.0"
languageName: node
linkType: soft
Expand All @@ -80,6 +92,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@aztec/box-blank-react@workspace:blank-react"
dependencies:
"@aztec/accounts": "npm:^0.16.9"
"@aztec/aztec-ui": "npm:^0.1.14"
"@aztec/aztec.js": "npm:^0.16.9"
"@types/jest": "npm:^29.5.0"
Expand Down Expand Up @@ -130,6 +143,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@aztec/box-blank@workspace:blank"
dependencies:
"@aztec/accounts": "npm:^0.16.9"
"@aztec/aztec.js": "npm:^0.16.9"
"@types/jest": "npm:^29.5.0"
"@types/mocha": "npm:^10.0.3"
Expand Down Expand Up @@ -163,6 +177,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@aztec/box-token@workspace:token"
dependencies:
"@aztec/accounts": "npm:^0.16.9"
"@aztec/aztec-ui": "npm:^0.1.14"
"@aztec/aztec.js": "npm:^0.16.9"
"@jest/globals": "npm:^29.6.4"
Expand Down Expand Up @@ -9472,13 +9487,6 @@ __metadata:
languageName: node
linkType: hard

"lodash.zip@npm:^4.2.0":
version: 4.2.0
resolution: "lodash.zip@npm:4.2.0"
checksum: e596da80a6138e369998b50c78b51ed6cf984b4f239e59056aa18dca5972a213c491c511caf5888a2dec603c67265caf942099bec554a86a5c7ff1937d57f0e4
languageName: node
linkType: hard

"lodash@npm:^4.17.11, lodash@npm:^4.17.15, lodash@npm:^4.17.21":
version: 4.17.21
resolution: "lodash@npm:4.17.21"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mkdir src
3. Add necessary yarn packages (and optionally add typescript too)

```sh
yarn add @aztec/aztec.js @aztec/noir-contracts typescript @types/node
yarn add @aztec/aztec.js @aztec/accounts @aztec/noir-contracts typescript @types/node
```

4. [Optional] If creating a typescript file, add a `tsconfig.json` file into the project root, here is an example:
Expand Down Expand Up @@ -88,6 +88,7 @@ yarn add @aztec/aztec.js @aztec/noir-contracts typescript @types/node
"start": "yarn build && DEBUG='token' node ./dest/index.js"
},
"dependencies": {
"@aztec/accounts": "latest",
"@aztec/aztec.js": "latest",
"@aztec/noir-contracts": "latest",
"@types/node": "^20.6.3",
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/dev_docs/tutorials/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Testing with Typescript

Testing is an integral part of any piece of software, and especially important for any blockchain application. In this page we will cover how to interact with your Noir contracts in a testing environment to write automated tests for your apps.

We will be using typescript to write our tests, and rely on the [`aztec.js`](https://www.npmjs.com/package/@aztec/aztec.js) client library to interact with a local Aztec network. We will use [`jest`](https://jestjs.io/) as a testing library, though feel free to use whatever you work with. Configuring the nodejs testing framework is out of scope for this guide.
We will be using typescript to write our tests, and rely on the [`aztec.js`](https://www.npmjs.com/package/@aztec/aztec.js) client library to interact with a local Aztec network, along with the [`accounts`](https://www.npmjs.com/package/@aztec/accounts) package for setting up test accounts. We will use [`jest`](https://jestjs.io/) as a testing library, though feel free to use whatever you work with. Configuring the nodejs testing framework is out of scope for this guide.

## A simple example

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/dev_docs/tutorials/token_portal/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Inside the `packages` directory, run

```bash
mkdir src && cd src && yarn init -yp
yarn add @aztec/aztec.js @aztec/noir-contracts @aztec/types @aztec/foundation @aztec/l1-artifacts viem "@types/node@^20.8.2"
yarn add @aztec/aztec.js @aztec/accounts @aztec/noir-contracts @aztec/types @aztec/foundation @aztec/l1-artifacts viem "@types/node@^20.8.2"
yarn add -D jest @jest/globals ts-jest
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ Open `cross_chain_messaging.test.ts` and paste the initial description of the te

```typescript
import { expect, jest} from '@jest/globals'
import { AccountWallet, AztecAddress, DebugLogger, EthAddress, Fr, computeAuthWitMessageHash, createDebugLogger, createPXEClient, getSandboxAccountsWallets, waitForSandbox } from '@aztec/aztec.js';
import { AccountWallet, AztecAddress, DebugLogger, EthAddress, Fr, computeAuthWitMessageHash, createDebugLogger, createPXEClient, waitForSandbox } from '@aztec/aztec.js';
import { getSandboxAccountsWallets } from '@aztec/accounts/testing';
import { TokenContract } from '@aztec/noir-contracts/Token';
import { TokenBridgeContract } from '@aztec/noir-contracts/TokenBridge';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ import {
computeAuthWitMessageHash,
createDebugLogger,
createPXEClient,
getSandboxAccountsWallets,
waitForSandbox,
} from "@aztec/aztec.js";
import { getSandboxAccountsWallets } from '@aztec/accounts/testing';
import {
Chain,
HttpTransport,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Create a new file `src/deploy.mjs`:
// src/deploy.mjs
import { writeFileSync } from 'fs';
import { Contract, ContractDeployer, createPXEClient, getSandboxAccountsWallets } from '@aztec/aztec.js';
import { getSandboxAccountsWallets } from '@aztec/accounts/testing';
import TokenContractArtifact from "../contracts/token/target/Token.json" assert { type: "json" };

#include_code dapp-deploy yarn-project/end-to-end/src/sample-dapp/deploy.mjs raw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ Balance of 0x0e1f60e8566e2c6d32378bdcadb7c63696e853281be798c107266b8c3a88ea9b: 0

Now that we can see the balance for each user, let's transfer tokens from one account to another. To do this, we will first need access to a `Wallet` object. This wraps access to an PXE and also provides an interface to craft and sign transactions on behalf of one of the user accounts.

We can initialize a wallet using one of the `getAccount` methods from `aztec.js`, along with the corresponding signing and encryption keys:
We can initialize a wallet using one of the `getAccount` methods from the `accounts` package, along with the corresponding signing and encryption keys:

```js
import { getSchnorrAccount } from "@aztec/aztec.js";
import { getSchnorrAccount } from "@aztec/accounts/schnorr";
const wallet = await getSchnorrAccount(
client,
ENCRYPTION_PRIVATE_KEY,
SIGNING_PRIVATE_KEY
).getWallet();
```

For ease of use, `aztec.js` also ships with a helper `getSandboxAccountsWallets` method that returns a wallet for each of the pre-initialized accounts in the Sandbox, so you can send transactions as any of them.
For ease of use, `accounts` also ships with a helper `getSandboxAccountsWallets` method that returns a wallet for each of the pre-initialized accounts in the Sandbox, so you can send transactions as any of them.

```js
import { getSandboxAccountsWallets } from '@aztec/aztec.js';
import { getSandboxAccountsWallets } from '@aztec/accounts/testing';
```

We'll use one of these wallets to initialize the `Contract` instance that represents our private token contract, so every transaction sent through it will be sent through that wallet.
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/dev_docs/tutorials/writing_dapp/project_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ cd sample-dapp
yarn init -yp
```

3. Add the `aztec.js` library as a dependency:
3. Add the `aztec.js` and `accounts` libraries as a dependency:

```sh
yarn add @aztec/aztec.js
yarn add @aztec/aztec.js @aztec/accounts
```

## Next steps
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/dev_docs/wallets/writing_an_account_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ For our account contract, we will take the hash of the action to authorize, requ

## The typescript side of things

Now that we have a valid account contract, we need to write the typescript glue code that will take care of formatting and authenticating transactions so they can be processed by our contract, as well as deploying the contract during account setup. This takes the form of implementing the `AccountContract` interface:
Now that we have a valid account contract, we need to write the typescript glue code that will take care of formatting and authenticating transactions so they can be processed by our contract, as well as deploying the contract during account setup. This takes the form of implementing the `AccountContract` interface from `@aztec/aztec.js`:

#include_code account-contract-interface yarn-project/aztec.js/src/account_contract/account_contract.ts typescript
#include_code account-contract-interface yarn-project/aztec.js/src/account/contract.ts typescript

However, if you are using the default `AccountActions` module, then you can leverage the `BaseAccountContract` class and just implement the logic for generating an auth witness that matches the one you wrote in Noir:
However, if you are using the default `AccountActions` module, then you can leverage the `DefaultAccountContract` class from `@aztec/accounts` and just implement the logic for generating an auth witness that matches the one you wrote in Noir:

#include_code account-contract yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts typescript

Expand Down
17 changes: 17 additions & 0 deletions docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ const config = {
disableSources: true,
},
],
[
"docusaurus-plugin-typedoc",
{
id: "apis/accounts",
entryPoints: [
"../yarn-project/accounts/src/defaults/index.ts",
"../yarn-project/accounts/src/ecdsa/index.ts",
"../yarn-project/accounts/src/schnorr/index.ts",
"../yarn-project/accounts/src/single_key/index.ts",
"../yarn-project/accounts/src/testing/index.ts",
],
tsconfig: "../yarn-project/accounts/tsconfig.json",
entryPointStrategy: "resolve",
out: "apis/accounts",
disableSources: true,
},
],
// ["./src/plugins/plugin-embed-code", {}],
],
themeConfig:
Expand Down
25 changes: 13 additions & 12 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ const sidebars = {
type: "doc",
id: "concepts/foundation/state_model/main",
},
items: [
"concepts/foundation/state_model/storage_slots"
]
items: ["concepts/foundation/state_model/storage_slots"],
},
{
label: "Accounts",
Expand Down Expand Up @@ -113,8 +111,8 @@ const sidebars = {
id: "concepts/foundation/communication/public_private_calls/main",
},
items: [
"concepts/foundation/communication/public_private_calls/slow_updates_tree"
]
"concepts/foundation/communication/public_private_calls/slow_updates_tree",
],
},
"concepts/foundation/communication/cross_chain_calls",
],
Expand Down Expand Up @@ -324,9 +322,7 @@ const sidebars = {
type: "doc",
id: "dev_docs/contracts/syntax/storage/main",
},
items: [
"dev_docs/contracts/syntax/storage/storage_slots",
]
items: ["dev_docs/contracts/syntax/storage/storage_slots"],
},
"dev_docs/contracts/syntax/events",
"dev_docs/contracts/syntax/functions",
Expand Down Expand Up @@ -361,10 +357,10 @@ const sidebars = {
{
label: "Common Patterns",
type: "category",
link: {
type: "doc",
id: "dev_docs/contracts/resources/common_patterns/main",
},
link: {
type: "doc",
id: "dev_docs/contracts/resources/common_patterns/main",
},
items: [
"dev_docs/contracts/resources/common_patterns/authwit",
// "dev_docs/contracts/resources/common_patterns/sending_tokens_to_user",
Expand Down Expand Up @@ -462,6 +458,11 @@ const sidebars = {
type: "category",
items: [{ dirName: "apis/aztec-js", type: "autogenerated" }],
},
{
label: "Accounts",
type: "category",
items: [{ dirName: "apis/accounts", type: "autogenerated" }],
},
],
},

Expand Down

0 comments on commit afd7b6d

Please sign in to comment.