@aztec/aztec.js 0.7.10 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -23
- package/dest/abis/ecdsa_account_contract.json +20 -8
- package/dest/abis/schnorr_account_contract.json +20 -8
- package/dest/abis/schnorr_single_key_account_contract.json +19 -7
- package/dest/account/defaults/default_entrypoint.js +3 -3
- package/dest/account/index.d.ts +33 -11
- package/dest/account/index.d.ts.map +1 -1
- package/dest/account/index.js +40 -18
- package/dest/account/manager/index.d.ts +8 -8
- package/dest/account/manager/index.d.ts.map +1 -1
- package/dest/account/manager/index.js +12 -12
- package/dest/{utils/account.d.ts → account/utils.d.ts} +10 -10
- package/dest/account/utils.d.ts.map +1 -0
- package/dest/account/utils.js +40 -0
- package/dest/contract/base_contract_interaction.d.ts +5 -5
- package/dest/contract/base_contract_interaction.d.ts.map +1 -1
- package/dest/contract/base_contract_interaction.js +6 -6
- package/dest/contract/contract.d.ts +1 -1
- package/dest/contract/contract.js +1 -1
- package/dest/contract/contract_base.d.ts +3 -3
- package/dest/contract/contract_base.d.ts.map +1 -1
- package/dest/contract/contract_base.js +2 -2
- package/dest/contract/contract_function_interaction.d.ts +2 -2
- package/dest/contract/contract_function_interaction.d.ts.map +1 -1
- package/dest/contract/index.d.ts +37 -0
- package/dest/contract/index.d.ts.map +1 -1
- package/dest/contract/index.js +38 -1
- package/dest/contract/sent_tx.d.ts +6 -6
- package/dest/contract/sent_tx.d.ts.map +1 -1
- package/dest/contract/sent_tx.js +9 -9
- package/dest/contract_deployer/contract_deployer.d.ts +4 -4
- package/dest/contract_deployer/contract_deployer.d.ts.map +1 -1
- package/dest/contract_deployer/contract_deployer.js +5 -5
- package/dest/contract_deployer/deploy_method.d.ts +5 -5
- package/dest/contract_deployer/deploy_method.d.ts.map +1 -1
- package/dest/contract_deployer/deploy_method.js +7 -7
- package/dest/contract_deployer/deploy_sent_tx.d.ts +2 -2
- package/dest/contract_deployer/deploy_sent_tx.d.ts.map +1 -1
- package/dest/contract_deployer/deploy_sent_tx.js +3 -3
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -3
- package/dest/pxe_client.d.ts +4 -0
- package/dest/pxe_client.d.ts.map +1 -0
- package/dest/pxe_client.js +20 -0
- package/dest/sandbox/index.d.ts +8 -8
- package/dest/sandbox/index.d.ts.map +1 -1
- package/dest/sandbox/index.js +13 -13
- package/dest/utils/abi_types.d.ts +9 -1
- package/dest/utils/abi_types.d.ts.map +1 -1
- package/dest/utils/authwit.d.ts +12 -0
- package/dest/utils/authwit.d.ts.map +1 -0
- package/dest/utils/authwit.js +20 -0
- package/dest/utils/cheat_codes.d.ts +8 -8
- package/dest/utils/cheat_codes.d.ts.map +1 -1
- package/dest/utils/cheat_codes.js +11 -11
- package/dest/utils/index.d.ts +1 -1
- package/dest/utils/index.d.ts.map +1 -1
- package/dest/utils/index.js +2 -2
- package/dest/utils/l1_contracts.d.ts +1 -30
- package/dest/utils/l1_contracts.d.ts.map +1 -1
- package/dest/utils/l1_contracts.js +5 -6
- package/dest/utils/l2_contracts.d.ts +3 -3
- package/dest/utils/l2_contracts.d.ts.map +1 -1
- package/dest/utils/l2_contracts.js +4 -4
- package/dest/wallet/account_wallet.d.ts +2 -2
- package/dest/wallet/account_wallet.d.ts.map +1 -1
- package/dest/wallet/account_wallet.js +4 -4
- package/dest/wallet/base_wallet.d.ts +7 -5
- package/dest/wallet/base_wallet.d.ts.map +1 -1
- package/dest/wallet/base_wallet.js +35 -29
- package/dest/wallet/index.d.ts +2 -2
- package/dest/wallet/index.d.ts.map +1 -1
- package/dest/wallet/signerless_wallet.js +1 -1
- package/package.json +69 -6
- package/src/abis/ecdsa_account_contract.json +20 -8
- package/src/abis/schnorr_account_contract.json +20 -8
- package/src/abis/schnorr_single_key_account_contract.json +19 -7
- package/src/account/defaults/default_entrypoint.ts +2 -2
- package/src/account/index.ts +40 -18
- package/src/account/manager/index.ts +11 -11
- package/src/{utils/account.ts → account/utils.ts} +12 -12
- package/src/contract/base_contract_interaction.ts +7 -7
- package/src/contract/contract.ts +1 -1
- package/src/contract/contract_base.ts +2 -2
- package/src/contract/contract_function_interaction.ts +2 -2
- package/src/contract/index.ts +37 -0
- package/src/contract/sent_tx.ts +9 -9
- package/src/contract_deployer/contract_deployer.ts +4 -4
- package/src/contract_deployer/deploy_method.ts +8 -8
- package/src/contract_deployer/deploy_sent_tx.ts +4 -4
- package/src/index.ts +4 -2
- package/src/{aztec_rpc_client.ts → pxe_client.ts} +5 -3
- package/src/sandbox/index.ts +12 -12
- package/src/utils/abi_types.ts +7 -1
- package/src/utils/authwit.ts +24 -0
- package/src/utils/cheat_codes.ts +10 -10
- package/src/utils/index.ts +1 -1
- package/src/utils/l1_contracts.ts +5 -44
- package/src/utils/l2_contracts.ts +4 -4
- package/src/wallet/account_wallet.ts +4 -4
- package/src/wallet/base_wallet.ts +42 -28
- package/src/wallet/index.ts +2 -2
- package/src/wallet/signerless_wallet.ts +1 -1
- package/.eslintrc.cjs +0 -1
- package/.tsbuildinfo +0 -1
- package/dest/aztec_rpc_client.d.ts +0 -4
- package/dest/aztec_rpc_client.d.ts.map +0 -1
- package/dest/aztec_rpc_client.js +0 -19
- package/dest/contract/checker.test.d.ts +0 -2
- package/dest/contract/checker.test.d.ts.map +0 -1
- package/dest/contract/checker.test.js +0 -169
- package/dest/contract/contract.test.d.ts +0 -2
- package/dest/contract/contract.test.d.ts.map +0 -1
- package/dest/contract/contract.test.js +0 -147
- package/dest/contract/sent_tx.test.d.ts +0 -2
- package/dest/contract/sent_tx.test.d.ts.map +0 -1
- package/dest/contract/sent_tx.test.js +0 -42
- package/dest/contract_deployer/contract_deployer.test.d.ts +0 -2
- package/dest/contract_deployer/contract_deployer.test.d.ts.map +0 -1
- package/dest/contract_deployer/contract_deployer.test.js +0 -46
- package/dest/main.js +0 -2
- package/dest/main.js.LICENSE.txt +0 -10
- package/dest/utils/account.d.ts.map +0 -1
- package/dest/utils/account.js +0 -40
- package/package.local.json +0 -5
- package/src/contract/checker.test.ts +0 -178
- package/src/contract/contract.test.ts +0 -172
- package/src/contract/sent_tx.test.ts +0 -52
- package/src/contract_deployer/contract_deployer.test.ts +0 -55
- package/tsconfig.dest.json +0 -15
- package/tsconfig.json +0 -20
- package/webpack.config.js +0 -74
package/src/account/index.ts
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The `account` module provides utilities for managing accounts. The most common methods to use
|
|
3
|
+
* are {@link getEcdsaAccount} and {@link getSchnorrAccount}, which return {@link AccountManager} instances
|
|
4
|
+
* using the default ECDSA or Schnorr account implementation respectively. The {@link AccountManager} class then
|
|
5
|
+
* allows to deploy and register a fresh account, or to obtain a `Wallet` instance out of an account already deployed.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const encryptionPrivateKey = GrumpkinScalar.random();
|
|
9
|
+
* const signingPrivateKey = GrumpkinScalar.random();
|
|
10
|
+
* const wallet = getSchnorrAccount(pxe, encryptionPrivateKey, signingPrivateKey).waitDeploy();
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* For testing purposes, consider using the {@link createAccount} and {@link createAccounts} methods,
|
|
14
|
+
* which create, register, and deploy random accounts, and return their associated `Wallet`s.
|
|
15
|
+
*
|
|
16
|
+
* For implementing your own account contract, the recommended way is to extend from the base
|
|
17
|
+
* {@link BaseAccountContract} class.
|
|
18
|
+
* Read more in {@link https://docs.aztec.network/dev_docs/wallets/writing_an_account_contract | Writing an account contract}.
|
|
19
|
+
*
|
|
20
|
+
* @packageDocumentation
|
|
21
|
+
*/
|
|
22
|
+
import { CompleteAddress, GrumpkinPrivateKey, PXE } from '@aztec/types';
|
|
2
23
|
|
|
3
24
|
import { AccountContract, AccountWallet, AztecAddress, Fr } from '../index.js';
|
|
4
25
|
import { EcdsaAccountContract } from './contract/ecdsa_account_contract.js';
|
|
@@ -8,6 +29,7 @@ import { AccountManager } from './manager/index.js';
|
|
|
8
29
|
|
|
9
30
|
export * from './contract/index.js';
|
|
10
31
|
export * from './defaults/index.js';
|
|
32
|
+
export * from './utils.js';
|
|
11
33
|
export { AccountInterface, AuthWitnessProvider } from './interface.js';
|
|
12
34
|
export { AccountManager, CompleteAddress };
|
|
13
35
|
|
|
@@ -16,49 +38,49 @@ export type Salt = Fr | number | bigint;
|
|
|
16
38
|
|
|
17
39
|
/**
|
|
18
40
|
* Creates an Account that relies on an ECDSA signing key for authentication.
|
|
19
|
-
* @param
|
|
41
|
+
* @param pxe - An PXE server instance.
|
|
20
42
|
* @param encryptionPrivateKey - Grumpkin key used for note encryption.
|
|
21
43
|
* @param signingPrivateKey - Secp256k1 key used for signing transactions.
|
|
22
44
|
* @param saltOrAddress - Deployment salt or complete address if account contract is already deployed.
|
|
23
45
|
*/
|
|
24
46
|
export function getEcdsaAccount(
|
|
25
|
-
|
|
47
|
+
pxe: PXE,
|
|
26
48
|
encryptionPrivateKey: GrumpkinPrivateKey,
|
|
27
49
|
signingPrivateKey: Buffer,
|
|
28
50
|
saltOrAddress?: Salt | CompleteAddress,
|
|
29
51
|
): AccountManager {
|
|
30
|
-
return new AccountManager(
|
|
52
|
+
return new AccountManager(pxe, encryptionPrivateKey, new EcdsaAccountContract(signingPrivateKey), saltOrAddress);
|
|
31
53
|
}
|
|
32
54
|
|
|
33
55
|
/**
|
|
34
56
|
* Creates an Account that relies on a Grumpkin signing key for authentication.
|
|
35
|
-
* @param
|
|
57
|
+
* @param pxe - An PXE server instance.
|
|
36
58
|
* @param encryptionPrivateKey - Grumpkin key used for note encryption.
|
|
37
59
|
* @param signingPrivateKey - Grumpkin key used for signing transactions.
|
|
38
60
|
* @param saltOrAddress - Deployment salt or complete address if account contract is already deployed.
|
|
39
61
|
*/
|
|
40
62
|
export function getSchnorrAccount(
|
|
41
|
-
|
|
63
|
+
pxe: PXE,
|
|
42
64
|
encryptionPrivateKey: GrumpkinPrivateKey,
|
|
43
65
|
signingPrivateKey: GrumpkinPrivateKey,
|
|
44
66
|
saltOrAddress?: Salt | CompleteAddress,
|
|
45
67
|
): AccountManager {
|
|
46
|
-
return new AccountManager(
|
|
68
|
+
return new AccountManager(pxe, encryptionPrivateKey, new SchnorrAccountContract(signingPrivateKey), saltOrAddress);
|
|
47
69
|
}
|
|
48
70
|
|
|
49
71
|
/**
|
|
50
72
|
* Creates an Account that uses the same Grumpkin key for encryption and authentication.
|
|
51
|
-
* @param
|
|
73
|
+
* @param pxe - An PXE server instance.
|
|
52
74
|
* @param encryptionAndSigningPrivateKey - Grumpkin key used for note encryption and signing transactions.
|
|
53
75
|
* @param saltOrAddress - Deployment salt or complete address if account contract is already deployed.
|
|
54
76
|
*/
|
|
55
77
|
export function getUnsafeSchnorrAccount(
|
|
56
|
-
|
|
78
|
+
pxe: PXE,
|
|
57
79
|
encryptionAndSigningPrivateKey: GrumpkinPrivateKey,
|
|
58
80
|
saltOrAddress?: Salt | CompleteAddress,
|
|
59
81
|
): AccountManager {
|
|
60
82
|
return new AccountManager(
|
|
61
|
-
|
|
83
|
+
pxe,
|
|
62
84
|
encryptionAndSigningPrivateKey,
|
|
63
85
|
new SingleKeyAccountContract(encryptionAndSigningPrivateKey),
|
|
64
86
|
saltOrAddress,
|
|
@@ -67,36 +89,36 @@ export function getUnsafeSchnorrAccount(
|
|
|
67
89
|
|
|
68
90
|
/**
|
|
69
91
|
* Gets a wallet for an already registered account using Schnorr signatures with a single key for encryption and authentication.
|
|
70
|
-
* @param
|
|
92
|
+
* @param pxe - An PXE server instance.
|
|
71
93
|
* @param address - Address for the account.
|
|
72
94
|
* @param signingPrivateKey - Grumpkin key used for note encryption and signing transactions.
|
|
73
95
|
* @returns A wallet for this account that can be used to interact with a contract instance.
|
|
74
96
|
*/
|
|
75
97
|
export function getUnsafeSchnorrWallet(
|
|
76
|
-
|
|
98
|
+
pxe: PXE,
|
|
77
99
|
address: AztecAddress,
|
|
78
100
|
signingKey: GrumpkinPrivateKey,
|
|
79
101
|
): Promise<AccountWallet> {
|
|
80
|
-
return getWallet(
|
|
102
|
+
return getWallet(pxe, address, new SingleKeyAccountContract(signingKey));
|
|
81
103
|
}
|
|
82
104
|
|
|
83
105
|
/**
|
|
84
106
|
* Gets a wallet for an already registered account.
|
|
85
|
-
* @param
|
|
107
|
+
* @param pxe - PXE Service instance.
|
|
86
108
|
* @param address - Address for the account.
|
|
87
109
|
* @param accountContract - Account contract implementation.
|
|
88
110
|
* @returns A wallet for this account that can be used to interact with a contract instance.
|
|
89
111
|
*/
|
|
90
112
|
export async function getWallet(
|
|
91
|
-
|
|
113
|
+
pxe: PXE,
|
|
92
114
|
address: AztecAddress,
|
|
93
115
|
accountContract: AccountContract,
|
|
94
116
|
): Promise<AccountWallet> {
|
|
95
|
-
const completeAddress = await
|
|
117
|
+
const completeAddress = await pxe.getRegisteredAccount(address);
|
|
96
118
|
if (!completeAddress) {
|
|
97
119
|
throw new Error(`Account ${address} not found`);
|
|
98
120
|
}
|
|
99
|
-
const nodeInfo = await
|
|
121
|
+
const nodeInfo = await pxe.getNodeInfo();
|
|
100
122
|
const entrypoint = await accountContract.getInterface(completeAddress, nodeInfo);
|
|
101
|
-
return new AccountWallet(
|
|
123
|
+
return new AccountWallet(pxe, entrypoint);
|
|
102
124
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Fr, PublicKey, getContractDeploymentInfo } from '@aztec/circuits.js';
|
|
2
|
-
import {
|
|
2
|
+
import { CompleteAddress, GrumpkinPrivateKey, PXE } from '@aztec/types';
|
|
3
3
|
|
|
4
4
|
import { AccountWallet, ContractDeployer, DeployMethod, WaitOpts, generatePublicKey } from '../../index.js';
|
|
5
5
|
import { AccountContract, Salt } from '../index.js';
|
|
@@ -8,7 +8,7 @@ import { DeployAccountSentTx } from './deploy_account_sent_tx.js';
|
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Manages a user account. Provides methods for calculating the account's address, deploying the account contract,
|
|
11
|
-
* and creating and registering the user wallet in the
|
|
11
|
+
* and creating and registering the user wallet in the PXE Service.
|
|
12
12
|
*/
|
|
13
13
|
export class AccountManager {
|
|
14
14
|
/** Deployment salt for the account contract. */
|
|
@@ -19,7 +19,7 @@ export class AccountManager {
|
|
|
19
19
|
private deployMethod?: DeployMethod;
|
|
20
20
|
|
|
21
21
|
constructor(
|
|
22
|
-
private
|
|
22
|
+
private pxe: PXE,
|
|
23
23
|
private encryptionPrivateKey: GrumpkinPrivateKey,
|
|
24
24
|
private accountContract: AccountContract,
|
|
25
25
|
saltOrAddress?: Salt | CompleteAddress,
|
|
@@ -43,7 +43,7 @@ export class AccountManager {
|
|
|
43
43
|
* @returns An entrypoint.
|
|
44
44
|
*/
|
|
45
45
|
public async getAccount(): Promise<AccountInterface> {
|
|
46
|
-
const nodeInfo = await this.
|
|
46
|
+
const nodeInfo = await this.pxe.getNodeInfo();
|
|
47
47
|
const completeAddress = await this.getCompleteAddress();
|
|
48
48
|
return this.accountContract.getInterface(completeAddress, nodeInfo);
|
|
49
49
|
}
|
|
@@ -74,18 +74,18 @@ export class AccountManager {
|
|
|
74
74
|
*/
|
|
75
75
|
public async getWallet(): Promise<AccountWallet> {
|
|
76
76
|
const entrypoint = await this.getAccount();
|
|
77
|
-
return new AccountWallet(this.
|
|
77
|
+
return new AccountWallet(this.pxe, entrypoint);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/**
|
|
81
|
-
* Registers this account in the
|
|
82
|
-
* the account on the
|
|
81
|
+
* Registers this account in the PXE Service and returns the associated wallet. Registering
|
|
82
|
+
* the account on the PXE Service is required for managing private state associated with it.
|
|
83
83
|
* Use the returned wallet to create Contract instances to be interacted with from this account.
|
|
84
84
|
* @returns A Wallet instance.
|
|
85
85
|
*/
|
|
86
86
|
public async register(): Promise<AccountWallet> {
|
|
87
87
|
const completeAddress = await this.getCompleteAddress();
|
|
88
|
-
await this.
|
|
88
|
+
await this.pxe.registerAccount(this.encryptionPrivateKey, completeAddress.partialAddress);
|
|
89
89
|
return this.getWallet();
|
|
90
90
|
}
|
|
91
91
|
|
|
@@ -100,7 +100,7 @@ export class AccountManager {
|
|
|
100
100
|
if (!this.salt) throw new Error(`Cannot deploy account contract without known salt.`);
|
|
101
101
|
await this.register();
|
|
102
102
|
const encryptionPublicKey = await this.getEncryptionPublicKey();
|
|
103
|
-
const deployer = new ContractDeployer(this.accountContract.getContractAbi(), this.
|
|
103
|
+
const deployer = new ContractDeployer(this.accountContract.getContractAbi(), this.pxe, encryptionPublicKey);
|
|
104
104
|
const args = await this.accountContract.getDeploymentArgs();
|
|
105
105
|
this.deployMethod = deployer.deploy(...args);
|
|
106
106
|
}
|
|
@@ -112,7 +112,7 @@ export class AccountManager {
|
|
|
112
112
|
* Uses the salt provided in the constructor or a randomly generated one.
|
|
113
113
|
* Note that if the Account is constructed with an explicit complete address
|
|
114
114
|
* it is assumed that the account contract has already been deployed and this method will throw.
|
|
115
|
-
* Registers the account in the
|
|
115
|
+
* Registers the account in the PXE Service before deploying the contract.
|
|
116
116
|
* @returns A SentTx object that can be waited to get the associated Wallet.
|
|
117
117
|
*/
|
|
118
118
|
public async deploy(): Promise<DeployAccountSentTx> {
|
|
@@ -127,7 +127,7 @@ export class AccountManager {
|
|
|
127
127
|
* Uses the salt provided in the constructor or a randomly generated one.
|
|
128
128
|
* Note that if the Account is constructed with an explicit complete address
|
|
129
129
|
* it is assumed that the account contract has already been deployed and this method will throw.
|
|
130
|
-
* Registers the account in the
|
|
130
|
+
* Registers the account in the PXE Service before deploying the contract.
|
|
131
131
|
* @param opts - Options to wait for the tx to be mined.
|
|
132
132
|
* @returns A Wallet instance.
|
|
133
133
|
*/
|
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
import { CompleteAddress, GrumpkinScalar } from '@aztec/circuits.js';
|
|
2
|
-
import {
|
|
2
|
+
import { PXE } from '@aztec/types';
|
|
3
3
|
|
|
4
4
|
import { getSchnorrAccount } from '../index.js';
|
|
5
5
|
import { AccountWallet } from '../wallet/account_wallet.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* Deploys and registers a new account using random private keys and returns the associated wallet. Useful for testing.
|
|
9
|
-
* @param
|
|
8
|
+
* Deploys and registers a new account using random private keys and returns the associated Schnorr account wallet. Useful for testing.
|
|
9
|
+
* @param pxe - PXE.
|
|
10
10
|
* @returns - A wallet for a fresh account.
|
|
11
11
|
*/
|
|
12
|
-
export function createAccount(
|
|
13
|
-
return getSchnorrAccount(
|
|
12
|
+
export function createAccount(pxe: PXE): Promise<AccountWallet> {
|
|
13
|
+
return getSchnorrAccount(pxe, GrumpkinScalar.random(), GrumpkinScalar.random()).waitDeploy();
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* Creates a random address and registers it as a recipient on the
|
|
18
|
-
* @param
|
|
17
|
+
* Creates a random address and registers it as a recipient on the pxe server. Useful for testing.
|
|
18
|
+
* @param pxe - PXE.
|
|
19
19
|
* @returns Complete address of the registered recipient.
|
|
20
20
|
*/
|
|
21
|
-
export async function createRecipient(
|
|
21
|
+
export async function createRecipient(pxe: PXE): Promise<CompleteAddress> {
|
|
22
22
|
const completeAddress = await CompleteAddress.random();
|
|
23
|
-
await
|
|
23
|
+
await pxe.registerRecipient(completeAddress);
|
|
24
24
|
return completeAddress;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Creates a given number of random accounts using the Schnorr account wallet.
|
|
29
|
-
* @param
|
|
29
|
+
* @param pxe - PXE.
|
|
30
30
|
* @param numberOfAccounts - How many accounts to create.
|
|
31
31
|
* @returns The created account wallets.
|
|
32
32
|
*/
|
|
33
|
-
export async function createAccounts(
|
|
33
|
+
export async function createAccounts(pxe: PXE, numberOfAccounts = 1): Promise<AccountWallet[]> {
|
|
34
34
|
const accounts = [];
|
|
35
35
|
|
|
36
36
|
// Prepare deployments
|
|
37
37
|
for (let i = 0; i < numberOfAccounts; ++i) {
|
|
38
|
-
const account = getSchnorrAccount(
|
|
38
|
+
const account = getSchnorrAccount(pxe, GrumpkinScalar.random(), GrumpkinScalar.random());
|
|
39
39
|
await account.getDeployMethod().then(d => d.simulate({ contractAddressSalt: account.salt }));
|
|
40
40
|
accounts.push(account);
|
|
41
41
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PXE, Tx, TxExecutionRequest } from '@aztec/types';
|
|
2
2
|
|
|
3
3
|
import { SentTx } from './sent_tx.js';
|
|
4
4
|
|
|
@@ -6,12 +6,12 @@ import { SentTx } from './sent_tx.js';
|
|
|
6
6
|
* Represents options for calling a (constrained) function in a contract.
|
|
7
7
|
* Allows the user to specify the sender address and nonce for a transaction.
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
9
|
+
export type SendMethodOptions = {
|
|
10
10
|
/**
|
|
11
11
|
* Wether to skip the simulation of the public part of the transaction.
|
|
12
12
|
*/
|
|
13
13
|
skipPublicSimulation?: boolean;
|
|
14
|
-
}
|
|
14
|
+
};
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Base class for an interaction with a contract, be it a deployment, a function call, or a batch.
|
|
@@ -21,7 +21,7 @@ export abstract class BaseContractInteraction {
|
|
|
21
21
|
protected tx?: Tx;
|
|
22
22
|
protected txRequest?: TxExecutionRequest;
|
|
23
23
|
|
|
24
|
-
constructor(protected
|
|
24
|
+
constructor(protected pxe: PXE) {}
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Create a transaction execution request ready to be simulated.
|
|
@@ -37,7 +37,7 @@ export abstract class BaseContractInteraction {
|
|
|
37
37
|
*/
|
|
38
38
|
public async simulate(options: SendMethodOptions = {}): Promise<Tx> {
|
|
39
39
|
const txRequest = this.txRequest ?? (await this.create(options));
|
|
40
|
-
this.tx = await this.
|
|
40
|
+
this.tx = await this.pxe.simulateTx(txRequest, !options.skipPublicSimulation);
|
|
41
41
|
return this.tx;
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -53,9 +53,9 @@ export abstract class BaseContractInteraction {
|
|
|
53
53
|
public send(options: SendMethodOptions = {}) {
|
|
54
54
|
const promise = (async () => {
|
|
55
55
|
const tx = this.tx ?? (await this.simulate(options));
|
|
56
|
-
return this.
|
|
56
|
+
return this.pxe.sendTx(tx);
|
|
57
57
|
})();
|
|
58
58
|
|
|
59
|
-
return new SentTx(this.
|
|
59
|
+
return new SentTx(this.pxe, promise);
|
|
60
60
|
}
|
|
61
61
|
}
|
package/src/contract/contract.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { ContractBase } from './contract_base.js';
|
|
|
10
10
|
* The Contract class represents a contract and provides utility methods for interacting with it.
|
|
11
11
|
* It enables the creation of ContractFunctionInteraction instances for each function in the contract's ABI,
|
|
12
12
|
* allowing users to call or send transactions to these functions. Additionally, the Contract class can be used
|
|
13
|
-
* to attach the contract instance to a deployed contract on-chain through the
|
|
13
|
+
* to attach the contract instance to a deployed contract on-chain through the PXE, which facilitates
|
|
14
14
|
* interaction with Aztec's privacy protocol.
|
|
15
15
|
*/
|
|
16
16
|
export class Contract extends ContractBase {
|
|
@@ -74,12 +74,12 @@ export class ContractBase {
|
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
* Attach the current contract instance to a portal contract and optionally add its dependencies.
|
|
77
|
-
* The function will return a promise that resolves when all contracts have been added to the
|
|
77
|
+
* The function will return a promise that resolves when all contracts have been added to the PXE.
|
|
78
78
|
* This is useful when you need to interact with a deployed contract that has multiple nested contracts.
|
|
79
79
|
*
|
|
80
80
|
* @param portalContract - The Ethereum address of the portal contract.
|
|
81
81
|
* @param dependencies - An optional array of additional DeployedContract instances to be attached.
|
|
82
|
-
* @returns A promise that resolves when all contracts are successfully added to the
|
|
82
|
+
* @returns A promise that resolves when all contracts are successfully added to the PXE.
|
|
83
83
|
*/
|
|
84
84
|
public attach(portalContract: EthAddress, dependencies: DeployedContract[] = []) {
|
|
85
85
|
const deployedContract: DeployedContract = {
|
|
@@ -11,12 +11,12 @@ export { SendMethodOptions };
|
|
|
11
11
|
* Represents the options for a view method in a contract function interaction.
|
|
12
12
|
* Allows specifying the address from which the view method should be called.
|
|
13
13
|
*/
|
|
14
|
-
export
|
|
14
|
+
export type ViewMethodOptions = {
|
|
15
15
|
/**
|
|
16
16
|
* The sender's Aztec address.
|
|
17
17
|
*/
|
|
18
18
|
from?: AztecAddress;
|
|
19
|
-
}
|
|
19
|
+
};
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* This is the class that is returned when calling e.g. `contract.methods.myMethod(arg0, arg1)`.
|
package/src/contract/index.ts
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `contract` module provides utilities for deploying and interacting with contracts, based on a
|
|
3
|
+
* `Wallet` instance and a compiled artifact. Refer to the {@link account} module for how to obtain a valid
|
|
4
|
+
* `Wallet` instance, and to the {@link https://docs.aztec.network/dev_docs/contracts/compiling | Compiling contracts}
|
|
5
|
+
* section of the documentation for how to generate an artifact out of your Noir source code.
|
|
6
|
+
*
|
|
7
|
+
* The {@link Contract} class is the main class in this module, and provides static methods for deploying
|
|
8
|
+
* a contract or interacting with an already deployed one. The `methods` property of the contract instance
|
|
9
|
+
* provides access to private, public, and view methods, that can be invoked in a transaction via `send()`,
|
|
10
|
+
* or can be queried via `view()`.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const contract = await Contract.deploy(wallet, MyContractAbi, [...constructorArgs]).send().deployed();
|
|
14
|
+
* console.log(`Contract deployed at ${contract.address}`);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const contract = await Contract.at(address, MyContractAbi, wallet);
|
|
19
|
+
* await contract.methods.mint(1000, owner).send().wait();
|
|
20
|
+
* console.log(`Total supply is now ${await contract.methods.totalSupply().view()}`);
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* The result of calling a method in a contract instance, such as `contract.methods.mint(1000, owner)`
|
|
24
|
+
* in the example, is a {@link ContractFunctionInteraction} instance. Usually this will be just sent as
|
|
25
|
+
* a transaction to the network via the `send` method, but you can also `simulate` it without sending,
|
|
26
|
+
* or obtaining the `request` for aggregating into a {@link BatchCall}.
|
|
27
|
+
*
|
|
28
|
+
* The result of `send`ing a transaction is a {@link SentTx} object, from which you can get the
|
|
29
|
+
* transaction hash, or simply `wait` until the transaction is mined and the local PXE Service
|
|
30
|
+
* has synchronised its changes.
|
|
31
|
+
*
|
|
32
|
+
* @remarks If you are using typescript, consider using the
|
|
33
|
+
* {@link https://docs.aztec.network/dev_docs/contracts/compiling#typescript-interfaces | autogenerated type-safe interfaces}
|
|
34
|
+
* for interacting with your contracts.
|
|
35
|
+
*
|
|
36
|
+
* @packageDocumentation
|
|
37
|
+
*/
|
|
1
38
|
export * from './contract.js';
|
|
2
39
|
export * from './contract_function_interaction.js';
|
|
3
40
|
export * from './sent_tx.js';
|
package/src/contract/sent_tx.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FieldsOf } from '@aztec/circuits.js';
|
|
2
2
|
import { retryUntil } from '@aztec/foundation/retry';
|
|
3
|
-
import {
|
|
3
|
+
import { PXE, TxHash, TxReceipt, TxStatus } from '@aztec/types';
|
|
4
4
|
|
|
5
5
|
import every from 'lodash.every';
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ export type WaitOpts = {
|
|
|
11
11
|
/** The time interval (in seconds) between retries to fetch the transaction receipt. Defaults to 1. */
|
|
12
12
|
interval?: number;
|
|
13
13
|
/**
|
|
14
|
-
* Whether to wait for the
|
|
14
|
+
* Whether to wait for the PXE Service to sync all notes up to the block in which this tx was mined.
|
|
15
15
|
* If false, then any queries that depend on state set by this transaction may return stale data. Defaults to true.
|
|
16
16
|
**/
|
|
17
17
|
waitForNotesSync?: boolean;
|
|
@@ -24,11 +24,11 @@ const DefaultWaitOpts: WaitOpts = {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* The SentTx class represents a sent transaction through the
|
|
27
|
+
* The SentTx class represents a sent transaction through the PXE, providing methods to fetch
|
|
28
28
|
* its hash, receipt, and mining status.
|
|
29
29
|
*/
|
|
30
30
|
export class SentTx {
|
|
31
|
-
constructor(protected
|
|
31
|
+
constructor(protected pxe: PXE, protected txHashPromise: Promise<TxHash>) {}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Retrieves the transaction hash of the SentTx instance.
|
|
@@ -43,13 +43,13 @@ export class SentTx {
|
|
|
43
43
|
/**
|
|
44
44
|
* Retrieve the transaction receipt associated with the current SentTx instance.
|
|
45
45
|
* The function fetches the transaction hash using 'getTxHash' and then queries
|
|
46
|
-
* the
|
|
46
|
+
* the PXE to get the corresponding transaction receipt.
|
|
47
47
|
*
|
|
48
48
|
* @returns A promise that resolves to a TxReceipt object representing the fetched transaction receipt.
|
|
49
49
|
*/
|
|
50
50
|
public async getReceipt(): Promise<TxReceipt> {
|
|
51
51
|
const txHash = await this.getTxHash();
|
|
52
|
-
return await this.
|
|
52
|
+
return await this.pxe.getTxReceipt(txHash);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/**
|
|
@@ -84,7 +84,7 @@ export class SentTx {
|
|
|
84
84
|
const txHash = await this.getTxHash();
|
|
85
85
|
return await retryUntil(
|
|
86
86
|
async () => {
|
|
87
|
-
const txReceipt = await this.
|
|
87
|
+
const txReceipt = await this.pxe.getTxReceipt(txHash);
|
|
88
88
|
// If receipt is not yet available, try again
|
|
89
89
|
if (txReceipt.status === TxStatus.PENDING) return undefined;
|
|
90
90
|
// If the tx was dropped, return it
|
|
@@ -92,8 +92,8 @@ export class SentTx {
|
|
|
92
92
|
// If we don't care about waiting for notes to be synced, return the receipt
|
|
93
93
|
const waitForNotesSync = opts?.waitForNotesSync ?? DefaultWaitOpts.waitForNotesSync;
|
|
94
94
|
if (!waitForNotesSync) return txReceipt;
|
|
95
|
-
// Check if all sync blocks on the
|
|
96
|
-
const { blocks, notes } = await this.
|
|
95
|
+
// Check if all sync blocks on the PXE Service are greater or equal than the block in which the tx was mined
|
|
96
|
+
const { blocks, notes } = await this.pxe.getSyncStatus();
|
|
97
97
|
const targetBlock = txReceipt.blockNumber!;
|
|
98
98
|
const areNotesSynced = blocks >= targetBlock && every(notes, block => block >= targetBlock);
|
|
99
99
|
return areNotesSynced ? txReceipt : undefined;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Point } from '@aztec/circuits.js';
|
|
2
2
|
import { ContractAbi } from '@aztec/foundation/abi';
|
|
3
|
-
import {
|
|
3
|
+
import { PXE, PublicKey } from '@aztec/types';
|
|
4
4
|
|
|
5
5
|
import { DeployMethod } from './deploy_method.js';
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ import { DeployMethod } from './deploy_method.js';
|
|
|
9
9
|
* @remarks Keeping this around even though we have Aztec.nr contract types because it can be useful for non-TS users.
|
|
10
10
|
*/
|
|
11
11
|
export class ContractDeployer {
|
|
12
|
-
constructor(private abi: ContractAbi, private
|
|
12
|
+
constructor(private abi: ContractAbi, private pxe: PXE, private publicKey?: PublicKey) {}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Deploy a contract using the provided ABI and constructor arguments.
|
|
@@ -18,9 +18,9 @@ export class ContractDeployer {
|
|
|
18
18
|
* be passed to the contract's constructor during deployment.
|
|
19
19
|
*
|
|
20
20
|
* @param args - The constructor arguments for the contract being deployed.
|
|
21
|
-
* @returns A DeployMethod instance configured with the ABI,
|
|
21
|
+
* @returns A DeployMethod instance configured with the ABI, PXE, and constructor arguments.
|
|
22
22
|
*/
|
|
23
23
|
public deploy(...args: any[]) {
|
|
24
|
-
return new DeployMethod(this.publicKey ?? Point.ZERO, this.
|
|
24
|
+
return new DeployMethod(this.publicKey ?? Point.ZERO, this.pxe, this.abi, args);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
import { ContractAbi, FunctionAbi, encodeArguments } from '@aztec/foundation/abi';
|
|
9
9
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
10
10
|
import { Fr } from '@aztec/foundation/fields';
|
|
11
|
-
import {
|
|
11
|
+
import { PXE, PackedArguments, PublicKey, Tx, TxExecutionRequest } from '@aztec/types';
|
|
12
12
|
|
|
13
13
|
import { BaseContractInteraction } from '../contract/base_contract_interaction.js';
|
|
14
14
|
import { Contract, ContractBase, SendMethodOptions } from '../contract/index.js';
|
|
@@ -18,7 +18,7 @@ import { DeploySentTx } from './deploy_sent_tx.js';
|
|
|
18
18
|
* Options for deploying a contract on the Aztec network.
|
|
19
19
|
* Allows specifying a portal contract, contract address salt, and additional send method options.
|
|
20
20
|
*/
|
|
21
|
-
export
|
|
21
|
+
export type DeployOptions = {
|
|
22
22
|
/**
|
|
23
23
|
* The Ethereum address of the Portal contract.
|
|
24
24
|
*/
|
|
@@ -27,7 +27,7 @@ export interface DeployOptions extends SendMethodOptions {
|
|
|
27
27
|
* An optional salt value used to deterministically calculate the contract address.
|
|
28
28
|
*/
|
|
29
29
|
contractAddressSalt?: Fr;
|
|
30
|
-
}
|
|
30
|
+
} & SendMethodOptions;
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Creates a TxRequest from a contract ABI, for contract deployment.
|
|
@@ -40,8 +40,8 @@ export class DeployMethod<TContract extends ContractBase = Contract> extends Bas
|
|
|
40
40
|
/** Constructor function to call. */
|
|
41
41
|
private constructorAbi: FunctionAbi;
|
|
42
42
|
|
|
43
|
-
constructor(private publicKey: PublicKey,
|
|
44
|
-
super(
|
|
43
|
+
constructor(private publicKey: PublicKey, protected pxe: PXE, private abi: ContractAbi, private args: any[] = []) {
|
|
44
|
+
super(pxe);
|
|
45
45
|
const constructorAbi = abi.functions.find(f => f.name === 'constructor');
|
|
46
46
|
if (!constructorAbi) throw new Error('Cannot find constructor in the ABI.');
|
|
47
47
|
this.constructorAbi = constructorAbi;
|
|
@@ -60,7 +60,7 @@ export class DeployMethod<TContract extends ContractBase = Contract> extends Bas
|
|
|
60
60
|
const portalContract = options.portalContract ?? EthAddress.ZERO;
|
|
61
61
|
const contractAddressSalt = options.contractAddressSalt ?? Fr.random();
|
|
62
62
|
|
|
63
|
-
const { chainId, protocolVersion } = await this.
|
|
63
|
+
const { chainId, protocolVersion } = await this.pxe.getNodeInfo();
|
|
64
64
|
|
|
65
65
|
const { completeAddress, constructorHash, functionTreeRoot } = await getContractDeploymentInfo(
|
|
66
66
|
this.abi,
|
|
@@ -103,7 +103,7 @@ export class DeployMethod<TContract extends ContractBase = Contract> extends Bas
|
|
|
103
103
|
this.completeAddress = completeAddress;
|
|
104
104
|
|
|
105
105
|
// TODO: Should we add the contracts to the DB here, or once the tx has been sent or mined?
|
|
106
|
-
await this.
|
|
106
|
+
await this.pxe.addContracts([{ abi: this.abi, completeAddress, portalContract }]);
|
|
107
107
|
|
|
108
108
|
return this.txRequest;
|
|
109
109
|
}
|
|
@@ -118,7 +118,7 @@ export class DeployMethod<TContract extends ContractBase = Contract> extends Bas
|
|
|
118
118
|
*/
|
|
119
119
|
public send(options: DeployOptions = {}): DeploySentTx<TContract> {
|
|
120
120
|
const txHashPromise = super.send(options).getTxHash();
|
|
121
|
-
return new DeploySentTx(this.abi, this.
|
|
121
|
+
return new DeploySentTx(this.abi, this.pxe, txHashPromise);
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
/**
|
|
@@ -2,7 +2,7 @@ import { FieldsOf } from '@aztec/circuits.js';
|
|
|
2
2
|
import { ContractAbi } from '@aztec/foundation/abi';
|
|
3
3
|
import { TxHash, TxReceipt } from '@aztec/types';
|
|
4
4
|
|
|
5
|
-
import { AztecAddress,
|
|
5
|
+
import { AztecAddress, Contract, ContractBase, PXE, SentTx, WaitOpts, Wallet } from '../index.js';
|
|
6
6
|
|
|
7
7
|
/** Options related to waiting for a deployment tx. */
|
|
8
8
|
export type DeployedWaitOpts = WaitOpts & {
|
|
@@ -20,7 +20,7 @@ export type DeployTxReceipt<TContract extends ContractBase = Contract> = FieldsO
|
|
|
20
20
|
* A contract deployment transaction sent to the network, extending SentTx with methods to create a contract instance.
|
|
21
21
|
*/
|
|
22
22
|
export class DeploySentTx<TContract extends ContractBase = Contract> extends SentTx {
|
|
23
|
-
constructor(private abi: ContractAbi, wallet:
|
|
23
|
+
constructor(private abi: ContractAbi, wallet: PXE | Wallet, txHashPromise: Promise<TxHash>) {
|
|
24
24
|
super(wallet, txHashPromise);
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -46,8 +46,8 @@ export class DeploySentTx<TContract extends ContractBase = Contract> extends Sen
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
protected getContractInstance(wallet?: Wallet, address?: AztecAddress): Promise<TContract> {
|
|
49
|
-
const isWallet = (
|
|
50
|
-
const contractWallet = wallet ?? (isWallet(this.
|
|
49
|
+
const isWallet = (pxe: PXE | Wallet): pxe is Wallet => !!(pxe as Wallet).createTxExecutionRequest;
|
|
50
|
+
const contractWallet = wallet ?? (isWallet(this.pxe) && this.pxe);
|
|
51
51
|
if (!contractWallet) throw new Error(`A wallet is required for creating a contract instance`);
|
|
52
52
|
if (!address) throw new Error(`Contract address is missing from transaction receipt`);
|
|
53
53
|
return Contract.at(address, this.abi, contractWallet) as Promise<TContract>;
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './contract/index.js';
|
|
2
2
|
export * from './contract_deployer/index.js';
|
|
3
3
|
export * from './utils/index.js';
|
|
4
|
-
export * from './
|
|
4
|
+
export * from './pxe_client.js';
|
|
5
5
|
export * from './account/index.js';
|
|
6
6
|
export * from './contract_deployer/deploy_method.js';
|
|
7
7
|
export * from './sandbox/index.js';
|
|
@@ -9,13 +9,15 @@ export * from './wallet/index.js';
|
|
|
9
9
|
|
|
10
10
|
export { AztecAddress, EthAddress, Point, Fr, GrumpkinScalar } from '@aztec/circuits.js';
|
|
11
11
|
export {
|
|
12
|
-
|
|
12
|
+
PXE,
|
|
13
13
|
ContractData,
|
|
14
14
|
ExtendedContractData as ExtendedContractData,
|
|
15
15
|
DeployedContract,
|
|
16
16
|
FunctionCall,
|
|
17
17
|
L2BlockL2Logs,
|
|
18
|
+
UnencryptedL2Log,
|
|
18
19
|
NodeInfo,
|
|
20
|
+
NotePreimage,
|
|
19
21
|
PackedArguments,
|
|
20
22
|
PublicKey,
|
|
21
23
|
GrumpkinPrivateKey,
|
|
@@ -2,11 +2,12 @@ import { AztecAddress, CompleteAddress, EthAddress, Fr, GrumpkinScalar, Point }
|
|
|
2
2
|
import { createJsonRpcClient, makeFetch } from '@aztec/foundation/json-rpc/client';
|
|
3
3
|
import {
|
|
4
4
|
AuthWitness,
|
|
5
|
-
AztecRPC,
|
|
6
5
|
ContractData,
|
|
7
6
|
ExtendedContractData,
|
|
8
7
|
L2BlockL2Logs,
|
|
8
|
+
L2Tx,
|
|
9
9
|
NotePreimage,
|
|
10
|
+
PXE,
|
|
10
11
|
Tx,
|
|
11
12
|
TxExecutionRequest,
|
|
12
13
|
TxHash,
|
|
@@ -15,8 +16,8 @@ import {
|
|
|
15
16
|
|
|
16
17
|
export { makeFetch } from '@aztec/foundation/json-rpc/client';
|
|
17
18
|
|
|
18
|
-
export const
|
|
19
|
-
createJsonRpcClient<
|
|
19
|
+
export const createPXEClient = (url: string, fetch = makeFetch([1, 2, 3], true)): PXE =>
|
|
20
|
+
createJsonRpcClient<PXE>(
|
|
20
21
|
url,
|
|
21
22
|
{
|
|
22
23
|
CompleteAddress,
|
|
@@ -31,6 +32,7 @@ export const createAztecRpcClient = (url: string, fetch = makeFetch([1, 2, 3], t
|
|
|
31
32
|
GrumpkinScalar,
|
|
32
33
|
NotePreimage,
|
|
33
34
|
AuthWitness,
|
|
35
|
+
L2Tx,
|
|
34
36
|
},
|
|
35
37
|
{ Tx, TxReceipt, L2BlockL2Logs },
|
|
36
38
|
false,
|