@aztec/aztec.js 0.1.0-alpha13
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/.eslintrc.cjs +1 -0
- package/.tsbuildinfo +1 -0
- package/README.md +44 -0
- package/dest/account_impl/account_collection.d.ts +24 -0
- package/dest/account_impl/account_collection.d.ts.map +1 -0
- package/dest/account_impl/account_collection.js +36 -0
- package/dest/account_impl/account_contract.d.ts +45 -0
- package/dest/account_impl/account_contract.d.ts.map +1 -0
- package/dest/account_impl/account_contract.js +108 -0
- package/dest/account_impl/index.d.ts +11 -0
- package/dest/account_impl/index.d.ts.map +1 -0
- package/dest/account_impl/index.js +3 -0
- package/dest/auth/ecdsa.d.ts +13 -0
- package/dest/auth/ecdsa.d.ts.map +1 -0
- package/dest/auth/ecdsa.js +17 -0
- package/dest/auth/index.d.ts +25 -0
- package/dest/auth/index.d.ts.map +1 -0
- package/dest/auth/index.js +13 -0
- package/dest/aztec_rpc_client/aztec_rpc_client.d.ts +25 -0
- package/dest/aztec_rpc_client/aztec_rpc_client.d.ts.map +1 -0
- package/dest/aztec_rpc_client/aztec_rpc_client.js +20 -0
- package/dest/aztec_rpc_client/index.d.ts +3 -0
- package/dest/aztec_rpc_client/index.d.ts.map +1 -0
- package/dest/aztec_rpc_client/index.js +3 -0
- package/dest/aztec_rpc_client/wallet.d.ts +44 -0
- package/dest/aztec_rpc_client/wallet.d.ts.map +1 -0
- package/dest/aztec_rpc_client/wallet.js +69 -0
- package/dest/contract/checker.d.ts +11 -0
- package/dest/contract/checker.d.ts.map +1 -0
- package/dest/contract/checker.js +98 -0
- package/dest/contract/checker.test.d.ts +2 -0
- package/dest/contract/checker.test.d.ts.map +1 -0
- package/dest/contract/checker.test.js +168 -0
- package/dest/contract/contract.d.ts +69 -0
- package/dest/contract/contract.d.ts.map +1 -0
- package/dest/contract/contract.js +64 -0
- package/dest/contract/contract.test.d.ts +2 -0
- package/dest/contract/contract.test.d.ts.map +1 -0
- package/dest/contract/contract.test.js +141 -0
- package/dest/contract/contract_function_interaction.d.ts +81 -0
- package/dest/contract/contract_function_interaction.d.ts.map +1 -0
- package/dest/contract/contract_function_interaction.js +96 -0
- package/dest/contract/index.d.ts +4 -0
- package/dest/contract/index.d.ts.map +1 -0
- package/dest/contract/index.js +4 -0
- package/dest/contract/sent_tx.d.ts +36 -0
- package/dest/contract/sent_tx.d.ts.map +1 -0
- package/dest/contract/sent_tx.js +50 -0
- package/dest/contract_deployer/contract_deployer.d.ts +23 -0
- package/dest/contract_deployer/contract_deployer.d.ts.map +1 -0
- package/dest/contract_deployer/contract_deployer.js +25 -0
- package/dest/contract_deployer/contract_deployer.test.d.ts +2 -0
- package/dest/contract_deployer/contract_deployer.test.d.ts.map +1 -0
- package/dest/contract_deployer/contract_deployer.test.js +48 -0
- package/dest/contract_deployer/deploy_method.d.ts +72 -0
- package/dest/contract_deployer/deploy_method.d.ts.map +1 -0
- package/dest/contract_deployer/deploy_method.js +114 -0
- package/dest/contract_deployer/index.d.ts +2 -0
- package/dest/contract_deployer/index.d.ts.map +1 -0
- package/dest/contract_deployer/index.js +2 -0
- package/dest/index.d.ts +8 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +9 -0
- package/dest/utils/account.d.ts +9 -0
- package/dest/utils/account.d.ts.map +1 -0
- package/dest/utils/account.js +38 -0
- package/dest/utils/index.d.ts +4 -0
- package/dest/utils/index.d.ts.map +1 -0
- package/dest/utils/index.js +4 -0
- package/dest/utils/pub_key.d.ts +18 -0
- package/dest/utils/pub_key.d.ts.map +1 -0
- package/dest/utils/pub_key.js +25 -0
- package/dest/utils/secrets.d.ts +8 -0
- package/dest/utils/secrets.d.ts.map +1 -0
- package/dest/utils/secrets.js +12 -0
- package/package.json +14 -0
- package/src/account_impl/account_collection.ts +41 -0
- package/src/account_impl/account_contract.ts +173 -0
- package/src/account_impl/index.ts +12 -0
- package/src/auth/ecdsa.ts +18 -0
- package/src/auth/index.ts +32 -0
- package/src/aztec_rpc_client/aztec_rpc_client.ts +59 -0
- package/src/aztec_rpc_client/index.ts +2 -0
- package/src/aztec_rpc_client/wallet.ts +87 -0
- package/src/contract/checker.test.ts +177 -0
- package/src/contract/checker.ts +117 -0
- package/src/contract/contract.test.ts +160 -0
- package/src/contract/contract.ts +80 -0
- package/src/contract/contract_function_interaction.ts +141 -0
- package/src/contract/index.ts +3 -0
- package/src/contract/sent_tx.ts +55 -0
- package/src/contract_deployer/contract_deployer.test.ts +56 -0
- package/src/contract_deployer/contract_deployer.ts +24 -0
- package/src/contract_deployer/deploy_method.ts +165 -0
- package/src/contract_deployer/index.ts +1 -0
- package/src/index.ts +9 -0
- package/src/utils/account.ts +59 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/pub_key.ts +26 -0
- package/src/utils/secrets.ts +12 -0
- package/tsconfig.json +26 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AztecRPC, TxReceipt, TxHash, TxStatus } from '@aztec/aztec-rpc';
|
|
2
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The SentTx class represents a sent transaction through the AztecRPCClient, providing methods to fetch
|
|
6
|
+
* its hash, receipt, and mining status.
|
|
7
|
+
*/
|
|
8
|
+
export class SentTx {
|
|
9
|
+
constructor(private arc: AztecRPC, private txHashPromise: Promise<TxHash>) {}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Retrieves the transaction hash of the SentTx instance.
|
|
13
|
+
* The function internally awaits for the 'txHashPromise' to resolve, and then returns the resolved transaction hash.
|
|
14
|
+
*
|
|
15
|
+
* @returns A promise that resolves to the transaction hash of the SentTx instance.
|
|
16
|
+
*/
|
|
17
|
+
public async getTxHash() {
|
|
18
|
+
return await this.txHashPromise;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Retrieve the transaction receipt associated with the current SentTx instance.
|
|
23
|
+
* The function fetches the transaction hash using 'getTxHash' and then queries
|
|
24
|
+
* the AztecRPCClient to get the corresponding transaction receipt.
|
|
25
|
+
*
|
|
26
|
+
* @returns A promise that resolves to a TxReceipt object representing the fetched transaction receipt.
|
|
27
|
+
*/
|
|
28
|
+
public async getReceipt(): Promise<TxReceipt> {
|
|
29
|
+
const txHash = await this.getTxHash();
|
|
30
|
+
return await this.arc.getTxReceipt(txHash);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Checks whether the transaction is mined or not within the specified timeout and retry interval.
|
|
35
|
+
* Resolves to true if the transaction status is 'MINED', false otherwise.
|
|
36
|
+
* Throws an error if the transaction receipt cannot be fetched after the given timeout.
|
|
37
|
+
*
|
|
38
|
+
* @param timeout - The maximum time (in seconds) to wait for the transaction to be mined. A value of 0 means no timeout.
|
|
39
|
+
* @param interval - The time interval (in seconds) between retries to fetch the transaction receipt.
|
|
40
|
+
* @returns A Promise that resolves to a boolean indicating if the transaction is mined or not.
|
|
41
|
+
*/
|
|
42
|
+
public async isMined(timeout = 0, interval = 1): Promise<boolean> {
|
|
43
|
+
const txHash = await this.getTxHash();
|
|
44
|
+
const receipt = await retryUntil(
|
|
45
|
+
async () => {
|
|
46
|
+
const txReceipt = await this.arc.getTxReceipt(txHash);
|
|
47
|
+
return txReceipt.status != TxStatus.PENDING ? txReceipt : undefined;
|
|
48
|
+
},
|
|
49
|
+
'isMined',
|
|
50
|
+
timeout,
|
|
51
|
+
interval,
|
|
52
|
+
);
|
|
53
|
+
return receipt.status === TxStatus.MINED;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AztecRPC, Tx, TxHash, TxReceipt } from '@aztec/aztec-rpc';
|
|
2
|
+
import { AztecAddress, EthAddress, Fr, Point } from '@aztec/circuits.js';
|
|
3
|
+
import { ContractAbi, FunctionType } from '@aztec/foundation/abi';
|
|
4
|
+
import { PublicKey } from '@aztec/key-store';
|
|
5
|
+
import { randomBytes } from 'crypto';
|
|
6
|
+
import { MockProxy, mock } from 'jest-mock-extended';
|
|
7
|
+
import { ContractDeployer } from './contract_deployer.js';
|
|
8
|
+
|
|
9
|
+
describe.skip('Contract Deployer', () => {
|
|
10
|
+
let arc: MockProxy<AztecRPC>;
|
|
11
|
+
|
|
12
|
+
const abi: ContractAbi = {
|
|
13
|
+
name: 'MyContract',
|
|
14
|
+
functions: [
|
|
15
|
+
{
|
|
16
|
+
name: 'constructor',
|
|
17
|
+
functionType: FunctionType.SECRET,
|
|
18
|
+
parameters: [],
|
|
19
|
+
returnTypes: [],
|
|
20
|
+
bytecode: '0af',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const publicKey: PublicKey = Point.random();
|
|
26
|
+
const portalContract = new EthAddress(randomBytes(EthAddress.SIZE_IN_BYTES));
|
|
27
|
+
const contractAddressSalt = Fr.random();
|
|
28
|
+
const account = AztecAddress.random();
|
|
29
|
+
const args = [12, 345n];
|
|
30
|
+
|
|
31
|
+
const mockTx = { type: 'Tx' } as any as Tx;
|
|
32
|
+
const mockTxHash = { type: 'TxHash' } as any as TxHash;
|
|
33
|
+
const mockTxReceipt = { type: 'TxReceipt' } as any as TxReceipt;
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
arc = mock<AztecRPC>();
|
|
37
|
+
arc.sendTx.mockResolvedValue(mockTxHash);
|
|
38
|
+
arc.getTxReceipt.mockResolvedValue(mockTxReceipt);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should create and send a contract deployment tx', async () => {
|
|
42
|
+
const deployer = new ContractDeployer(abi, arc, publicKey);
|
|
43
|
+
const sentTx = deployer.deploy(args[0], args[1]).send({
|
|
44
|
+
portalContract,
|
|
45
|
+
contractAddressSalt,
|
|
46
|
+
from: account,
|
|
47
|
+
});
|
|
48
|
+
const txHash = await sentTx.getTxHash();
|
|
49
|
+
const receipt = await sentTx.getReceipt();
|
|
50
|
+
|
|
51
|
+
expect(txHash).toBe(mockTxHash);
|
|
52
|
+
expect(receipt).toBe(mockTxReceipt);
|
|
53
|
+
expect(arc.sendTx).toHaveBeenCalledTimes(1);
|
|
54
|
+
expect(arc.sendTx).toHaveBeenCalledWith(mockTx);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AztecRPC, Point } from '@aztec/aztec-rpc';
|
|
2
|
+
import { ContractAbi } from '@aztec/foundation/abi';
|
|
3
|
+
import { DeployMethod } from './deploy_method.js';
|
|
4
|
+
import { PublicKey } from '@aztec/key-store';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A class for deploying contract.
|
|
8
|
+
*/
|
|
9
|
+
export class ContractDeployer {
|
|
10
|
+
constructor(private abi: ContractAbi, private arc: AztecRPC, private publicKey?: PublicKey) {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Deploy a contract using the provided ABI and constructor arguments.
|
|
14
|
+
* This function creates a new DeployMethod instance that can be used to send deployment transactions
|
|
15
|
+
* and query deployment status. The method accepts any number of constructor arguments, which will
|
|
16
|
+
* be passed to the contract's constructor during deployment.
|
|
17
|
+
*
|
|
18
|
+
* @param args - The constructor arguments for the contract being deployed.
|
|
19
|
+
* @returns A DeployMethod instance configured with the ABI, AztecRPCClient, and constructor arguments.
|
|
20
|
+
*/
|
|
21
|
+
public deploy(...args: any[]) {
|
|
22
|
+
return new DeployMethod(this.publicKey ?? Point.ZERO, this.arc, this.abi, args);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { AztecRPC, getContractDeploymentInfo } from '@aztec/aztec-rpc';
|
|
2
|
+
import { CircuitsWasm, ContractDeploymentData, TxContext } from '@aztec/circuits.js';
|
|
3
|
+
import { ContractAbi } from '@aztec/foundation/abi';
|
|
4
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
5
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
6
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
7
|
+
import { PublicKey } from '@aztec/key-store';
|
|
8
|
+
import { ExecutionRequest, PackedArguments, PartialContractAddress, Tx, TxExecutionRequest } from '@aztec/types';
|
|
9
|
+
import { BaseWallet, Wallet } from '../aztec_rpc_client/wallet.js';
|
|
10
|
+
import { Contract, ContractFunctionInteraction, SendMethodOptions } from '../contract/index.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Options for deploying a contract on the Aztec network.
|
|
14
|
+
* Allows specifying a portal contract, contract address salt, and additional send method options.
|
|
15
|
+
*/
|
|
16
|
+
export interface DeployOptions extends SendMethodOptions {
|
|
17
|
+
/**
|
|
18
|
+
* The Ethereum address of the Portal contract.
|
|
19
|
+
*/
|
|
20
|
+
portalContract?: EthAddress;
|
|
21
|
+
/**
|
|
22
|
+
* An optional salt value used to deterministically calculate the contract address.
|
|
23
|
+
*/
|
|
24
|
+
contractAddressSalt?: Fr;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Simple wallet implementation for use when deploying contracts only.
|
|
29
|
+
*/
|
|
30
|
+
class DeployerWallet extends BaseWallet {
|
|
31
|
+
getAddress(): AztecAddress {
|
|
32
|
+
return AztecAddress.ZERO;
|
|
33
|
+
}
|
|
34
|
+
async createAuthenticatedTxRequest(
|
|
35
|
+
executions: ExecutionRequest[],
|
|
36
|
+
txContext: TxContext,
|
|
37
|
+
): Promise<TxExecutionRequest> {
|
|
38
|
+
if (executions.length !== 1) {
|
|
39
|
+
throw new Error(`Deployer wallet can only run one execution at a time (requested ${executions.length})`);
|
|
40
|
+
}
|
|
41
|
+
const [execution] = executions;
|
|
42
|
+
const wasm = await CircuitsWasm.get();
|
|
43
|
+
const packedArguments = await PackedArguments.fromArgs(execution.args, wasm);
|
|
44
|
+
return Promise.resolve(
|
|
45
|
+
new TxExecutionRequest(execution.to, execution.functionData, packedArguments.hash, txContext, [packedArguments]),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates a TxRequest from a contract ABI, for contract deployment.
|
|
52
|
+
* Extends the ContractFunctionInteraction class.
|
|
53
|
+
*/
|
|
54
|
+
export class DeployMethod extends ContractFunctionInteraction {
|
|
55
|
+
/**
|
|
56
|
+
* The partially computed contract address. Known after creation of the deployment transaction.
|
|
57
|
+
*/
|
|
58
|
+
public partialContractAddress?: PartialContractAddress = undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The complete contract address.
|
|
62
|
+
*/
|
|
63
|
+
public completeContractAddress?: AztecAddress = undefined;
|
|
64
|
+
|
|
65
|
+
constructor(private publicKey: PublicKey, arc: AztecRPC, private abi: ContractAbi, args: any[] = []) {
|
|
66
|
+
const constructorAbi = abi.functions.find(f => f.name === 'constructor');
|
|
67
|
+
if (!constructorAbi) {
|
|
68
|
+
throw new Error('Cannot find constructor in the ABI.');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
super(new DeployerWallet(arc), AztecAddress.ZERO, constructorAbi, args);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Create a contract deployment transaction, given the deployment options.
|
|
76
|
+
* This function internally calls `request()` and `sign()` methods to prepare
|
|
77
|
+
* the transaction for deployment. The resulting signed transaction can be
|
|
78
|
+
* later sent using the `send()` method.
|
|
79
|
+
*
|
|
80
|
+
* @param options - An object containing optional deployment settings, including portalContract, contractAddressSalt, and from.
|
|
81
|
+
* @returns A Promise resolving to an object containing the signed transaction data and other relevant information.
|
|
82
|
+
*/
|
|
83
|
+
public async create(options: DeployOptions = {}) {
|
|
84
|
+
const { portalContract, contractAddressSalt } = Object.assign(
|
|
85
|
+
{ portalContract: EthAddress.ZERO, contractAddressSalt: Fr.random() },
|
|
86
|
+
options,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const { address, constructorHash, functionTreeRoot, partialAddress } = await getContractDeploymentInfo(
|
|
90
|
+
this.abi,
|
|
91
|
+
this.args,
|
|
92
|
+
contractAddressSalt,
|
|
93
|
+
this.publicKey,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const contractDeploymentData = new ContractDeploymentData(
|
|
97
|
+
this.publicKey,
|
|
98
|
+
constructorHash,
|
|
99
|
+
functionTreeRoot,
|
|
100
|
+
contractAddressSalt,
|
|
101
|
+
portalContract,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const { chainId, version } = await this.wallet.getNodeInfo();
|
|
105
|
+
|
|
106
|
+
const txContext = new TxContext(false, false, true, contractDeploymentData, new Fr(chainId), new Fr(version));
|
|
107
|
+
const executionRequest = this.getExecutionRequest(address, AztecAddress.ZERO);
|
|
108
|
+
const txRequest = await this.wallet.createAuthenticatedTxRequest([executionRequest], txContext);
|
|
109
|
+
|
|
110
|
+
this.txRequest = txRequest;
|
|
111
|
+
this.partialContractAddress = partialAddress;
|
|
112
|
+
this.completeContractAddress = address;
|
|
113
|
+
|
|
114
|
+
// TODO: Should we add the contracts to the DB here, or once the tx has been sent or mined?
|
|
115
|
+
await this.wallet.addContracts([{ abi: this.abi, address, portalContract }]);
|
|
116
|
+
|
|
117
|
+
return this.txRequest;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Send the contract deployment transaction using the provided options.
|
|
122
|
+
* This function extends the 'send' method from the ContractFunctionInteraction class,
|
|
123
|
+
* allowing us to send a transaction specifically for contract deployment.
|
|
124
|
+
*
|
|
125
|
+
* @param options - An object containing various deployment options such as portalContract, contractAddressSalt, and from.
|
|
126
|
+
* @returns A Promise that resolves to the transaction receipt upon successful deployment.
|
|
127
|
+
*/
|
|
128
|
+
public send(options: DeployOptions = {}) {
|
|
129
|
+
return super.send(options);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Simulate the request.
|
|
134
|
+
* @param options - Deployment options.
|
|
135
|
+
* @returns The simulated tx.
|
|
136
|
+
*/
|
|
137
|
+
public async simulate(options: DeployOptions): Promise<Tx> {
|
|
138
|
+
const txRequest = this.txRequest ?? (await this.create(options));
|
|
139
|
+
|
|
140
|
+
// We need to tell the rpc server which account state to use to simulate
|
|
141
|
+
// the tx. In the context of a deployment, we need to use an account state
|
|
142
|
+
// that matches the account contract being deployed. But if what we deploy is
|
|
143
|
+
// an "application" contract, then there's no account state associated with it,
|
|
144
|
+
// so we just let the rpc server use whichever it wants. This is an accident
|
|
145
|
+
// of all simulations happening over an account state, which should not be necessary.
|
|
146
|
+
const rpcServerRegisteredAccounts = await this.wallet.getAccounts();
|
|
147
|
+
const deploymentAddress = this.completeContractAddress!;
|
|
148
|
+
const accountStateAddress = rpcServerRegisteredAccounts.includes(deploymentAddress) ? deploymentAddress : undefined;
|
|
149
|
+
|
|
150
|
+
this.tx = await this.wallet.simulateTx(txRequest, accountStateAddress);
|
|
151
|
+
return this.tx;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Creates a contract abstraction given a wallet.
|
|
156
|
+
* @param withWallet - The wallet to provide to the contract abstraction
|
|
157
|
+
* @returns - The generated contract abstraction.
|
|
158
|
+
*/
|
|
159
|
+
public getContract(withWallet: Wallet) {
|
|
160
|
+
if (!this.completeContractAddress) {
|
|
161
|
+
throw new Error(`Cannot get a contract instance for a contract not yet deployed`);
|
|
162
|
+
}
|
|
163
|
+
return new Contract(this.completeContractAddress, this.abi, withWallet);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './contract_deployer.js';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './contract/index.js';
|
|
2
|
+
export * from './contract_deployer/index.js';
|
|
3
|
+
export * from './utils/index.js';
|
|
4
|
+
export * from './aztec_rpc_client/index.js';
|
|
5
|
+
export * from './auth/index.js';
|
|
6
|
+
export * from './account_impl/index.js';
|
|
7
|
+
|
|
8
|
+
// TODO - only export necessary stuffs
|
|
9
|
+
export * from '@aztec/aztec-rpc';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { AztecRPC, TxStatus, getContractDeploymentInfo } from '@aztec/aztec-rpc';
|
|
2
|
+
import { AztecAddress, CircuitsWasm, Fr, Point } from '@aztec/circuits.js';
|
|
3
|
+
import { randomBytes } from '@aztec/foundation/crypto';
|
|
4
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
5
|
+
import { EcdsaAccountContractAbi } from '@aztec/noir-contracts/examples';
|
|
6
|
+
import { AccountWallet, Wallet } from '../aztec_rpc_client/wallet.js';
|
|
7
|
+
import {
|
|
8
|
+
AccountCollection,
|
|
9
|
+
AccountContract,
|
|
10
|
+
ContractDeployer,
|
|
11
|
+
EcdsaAuthProvider,
|
|
12
|
+
generatePublicKey,
|
|
13
|
+
} from '../index.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates an Aztec Account.
|
|
17
|
+
* @returns The account's address & public key.
|
|
18
|
+
*/
|
|
19
|
+
export async function createAccounts(
|
|
20
|
+
aztecRpcClient: AztecRPC,
|
|
21
|
+
privateKey?: Buffer,
|
|
22
|
+
numberOfAccounts = 1,
|
|
23
|
+
logger = createDebugLogger('aztec:aztec.js:accounts'),
|
|
24
|
+
): Promise<Wallet> {
|
|
25
|
+
const accountImpls = new AccountCollection();
|
|
26
|
+
const results: [AztecAddress, Point][] = [];
|
|
27
|
+
const wasm = await CircuitsWasm.get();
|
|
28
|
+
for (let i = 0; i < numberOfAccounts; ++i) {
|
|
29
|
+
// TODO(#662): Let the aztec rpc server generate the keypair rather than hardcoding the private key
|
|
30
|
+
const privKey = i == 0 && privateKey ? privateKey : randomBytes(32);
|
|
31
|
+
const accountAbi = EcdsaAccountContractAbi;
|
|
32
|
+
const publicKey = await generatePublicKey(privKey);
|
|
33
|
+
const salt = Fr.random();
|
|
34
|
+
const deploymentInfo = await getContractDeploymentInfo(accountAbi, [], salt, publicKey);
|
|
35
|
+
await aztecRpcClient.addAccount(privKey, deploymentInfo.address, deploymentInfo.partialAddress, accountAbi);
|
|
36
|
+
const contractDeployer = new ContractDeployer(accountAbi, aztecRpcClient, publicKey);
|
|
37
|
+
const tx = contractDeployer.deploy().send({ contractAddressSalt: salt });
|
|
38
|
+
await tx.isMined(0, 0.1);
|
|
39
|
+
const receipt = await tx.getReceipt();
|
|
40
|
+
if (receipt.status !== TxStatus.MINED) {
|
|
41
|
+
throw new Error(`Deployment tx not mined (status is ${receipt.status})`);
|
|
42
|
+
}
|
|
43
|
+
const address = receipt.contractAddress!;
|
|
44
|
+
logger(`Created account ${address.toString()} with public key ${publicKey.toString()}`);
|
|
45
|
+
accountImpls.registerAccount(
|
|
46
|
+
address,
|
|
47
|
+
new AccountContract(
|
|
48
|
+
address,
|
|
49
|
+
publicKey,
|
|
50
|
+
new EcdsaAuthProvider(privKey),
|
|
51
|
+
deploymentInfo.partialAddress,
|
|
52
|
+
accountAbi,
|
|
53
|
+
wasm,
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
results.push([address, publicKey]);
|
|
57
|
+
}
|
|
58
|
+
return new AccountWallet(aztecRpcClient, accountImpls);
|
|
59
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
|
|
2
|
+
import { Point } from '../index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a Point type to a public key represented by BigInt coordinates
|
|
6
|
+
* @param point - The Point to convert.
|
|
7
|
+
* @returns An object with x & y coordinates represented as bigints.
|
|
8
|
+
*/
|
|
9
|
+
export function pointToPublicKey(point: Point) {
|
|
10
|
+
const x = point.x.toBigInt();
|
|
11
|
+
const y = point.y.toBigInt();
|
|
12
|
+
return {
|
|
13
|
+
x,
|
|
14
|
+
y,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Method for generating a public grumpkin key from a private key.
|
|
20
|
+
* @param privateKey - The private key.
|
|
21
|
+
* @returns The generated public key.
|
|
22
|
+
*/
|
|
23
|
+
export async function generatePublicKey(privateKey: Buffer): Promise<Point> {
|
|
24
|
+
const grumpkin = await Grumpkin.new();
|
|
25
|
+
return Point.fromBuffer(grumpkin.mul(grumpkin.generator(), privateKey));
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CircuitsWasm, Fr } from '@aztec/circuits.js';
|
|
2
|
+
import { computeSecretMessageHash } from '@aztec/circuits.js/abis';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Given a secret, it computes its pedersen hash - used to send l1 to l2 messages
|
|
6
|
+
* @param secret - the secret to hash - secret could be generated however you want e.g. `Fr.random()`
|
|
7
|
+
* @returns the hash
|
|
8
|
+
*/
|
|
9
|
+
export async function computeMessageSecretHash(secret: Fr): Promise<Fr> {
|
|
10
|
+
const wasm = await CircuitsWasm.get();
|
|
11
|
+
return computeSecretMessageHash(wasm, secret);
|
|
12
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "..",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dest",
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"tsBuildInfoFile": ".tsbuildinfo"
|
|
7
|
+
},
|
|
8
|
+
"references": [
|
|
9
|
+
{
|
|
10
|
+
"path": "../aztec-rpc"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"path": "../circuits.js"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"path": "../foundation"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"path": "../key-store"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"path": "../noir-contracts"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"include": ["src"]
|
|
26
|
+
}
|