@aztec/accounts 0.22.0 → 0.24.0

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.
Files changed (34) hide show
  1. package/dest/artifacts/EcdsaAccount.json +564 -317
  2. package/dest/artifacts/SchnorrAccount.json +709 -461
  3. package/dest/artifacts/SchnorrSingleKeyAccount.json +836 -587
  4. package/dest/defaults/account_entrypoint.d.ts +3 -3
  5. package/dest/defaults/account_entrypoint.d.ts.map +1 -1
  6. package/dest/defaults/account_entrypoint.js +59 -47
  7. package/dest/defaults/account_interface.d.ts +3 -3
  8. package/dest/defaults/account_interface.d.ts.map +1 -1
  9. package/dest/defaults/account_interface.js +4 -4
  10. package/dest/defaults/entrypoint_payload.d.ts +13 -8
  11. package/dest/defaults/entrypoint_payload.d.ts.map +1 -1
  12. package/dest/defaults/entrypoint_payload.js +27 -10
  13. package/package.json +7 -7
  14. package/src/artifacts/EcdsaAccount.json +3273 -0
  15. package/src/artifacts/SchnorrAccount.json +3265 -0
  16. package/src/artifacts/SchnorrSingleKeyAccount.json +3180 -0
  17. package/src/defaults/account_contract.ts +25 -0
  18. package/src/defaults/account_entrypoint.ts +141 -0
  19. package/src/defaults/account_interface.ts +39 -0
  20. package/src/defaults/constants.ts +4 -0
  21. package/src/defaults/entrypoint_payload.ts +112 -0
  22. package/src/defaults/index.ts +13 -0
  23. package/src/ecdsa/account_contract.ts +38 -0
  24. package/src/ecdsa/artifact.ts +5 -0
  25. package/src/ecdsa/index.ts +42 -0
  26. package/src/schnorr/account_contract.ts +38 -0
  27. package/src/schnorr/artifact.ts +5 -0
  28. package/src/schnorr/index.ts +47 -0
  29. package/src/single_key/account_contract.ts +45 -0
  30. package/src/single_key/artifact.ts +7 -0
  31. package/src/single_key/index.ts +52 -0
  32. package/src/testing/configuration.ts +81 -0
  33. package/src/testing/create_account.ts +39 -0
  34. package/src/testing/index.ts +11 -0
@@ -0,0 +1,25 @@
1
+ import { AccountContract, AccountInterface, AuthWitnessProvider } from '@aztec/aztec.js/account';
2
+ import { CompleteAddress } from '@aztec/circuit-types';
3
+ import { ContractArtifact } from '@aztec/foundation/abi';
4
+ import { NodeInfo } from '@aztec/types/interfaces';
5
+
6
+ import { DefaultAccountInterface } from '../defaults/account_interface.js';
7
+
8
+ /**
9
+ * Base class for implementing an account contract. Requires that the account uses the
10
+ * default entrypoint method signature.
11
+ */
12
+ export abstract class DefaultAccountContract implements AccountContract {
13
+ abstract getAuthWitnessProvider(address: CompleteAddress): AuthWitnessProvider;
14
+ abstract getDeploymentArgs(): any[];
15
+
16
+ constructor(private artifact: ContractArtifact) {}
17
+
18
+ getContractArtifact(): ContractArtifact {
19
+ return this.artifact;
20
+ }
21
+
22
+ getInterface(address: CompleteAddress, nodeInfo: NodeInfo): AccountInterface {
23
+ return new DefaultAccountInterface(this.getAuthWitnessProvider(address), address, nodeInfo);
24
+ }
25
+ }
@@ -0,0 +1,141 @@
1
+ import { AuthWitnessProvider, EntrypointInterface, FeeOptions } from '@aztec/aztec.js/account';
2
+ import { FunctionCall, PackedArguments, TxExecutionRequest } from '@aztec/circuit-types';
3
+ import { AztecAddress, Fr, FunctionData, GeneratorIndex, TxContext } from '@aztec/circuits.js';
4
+ import { FunctionAbi, encodeArguments } from '@aztec/foundation/abi';
5
+
6
+ import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from './constants.js';
7
+ import { buildAppPayload, buildFeePayload, hashPayload } from './entrypoint_payload.js';
8
+
9
+ /**
10
+ * Implementation for an entrypoint interface that follows the default entrypoint signature
11
+ * for an account, which accepts an AppPayload and a FeePayload as defined in noir-libs/aztec-noir/src/entrypoint module
12
+ */
13
+ export class DefaultAccountEntrypoint implements EntrypointInterface {
14
+ constructor(
15
+ private address: AztecAddress,
16
+ private auth: AuthWitnessProvider,
17
+ private chainId: number = DEFAULT_CHAIN_ID,
18
+ private version: number = DEFAULT_VERSION,
19
+ ) {}
20
+
21
+ async createTxExecutionRequest(executions: FunctionCall[], feeOpts?: FeeOptions): Promise<TxExecutionRequest> {
22
+ const { payload: appPayload, packedArguments: appPackedArguments } = buildAppPayload(executions);
23
+ const { payload: feePayload, packedArguments: feePackedArguments } = buildFeePayload(feeOpts);
24
+
25
+ const abi = this.getEntrypointAbi();
26
+ const entrypointPackedArgs = PackedArguments.fromArgs(encodeArguments(abi, [appPayload, feePayload]));
27
+
28
+ const appAuthWitness = await this.auth.createAuthWitness(
29
+ Fr.fromBuffer(hashPayload(appPayload, GeneratorIndex.SIGNATURE_PAYLOAD)),
30
+ );
31
+ const feeAuthWitness = await this.auth.createAuthWitness(
32
+ Fr.fromBuffer(hashPayload(feePayload, GeneratorIndex.FEE_PAYLOAD)),
33
+ );
34
+
35
+ const txRequest = TxExecutionRequest.from({
36
+ argsHash: entrypointPackedArgs.hash,
37
+ origin: this.address,
38
+ functionData: FunctionData.fromAbi(abi),
39
+ txContext: TxContext.empty(this.chainId, this.version),
40
+ packedArguments: [...appPackedArguments, ...feePackedArguments, entrypointPackedArgs],
41
+ authWitnesses: [appAuthWitness, feeAuthWitness],
42
+ });
43
+
44
+ return txRequest;
45
+ }
46
+
47
+ private getEntrypointAbi() {
48
+ return {
49
+ name: 'entrypoint',
50
+ functionType: 'secret',
51
+ isInternal: false,
52
+ parameters: [
53
+ {
54
+ name: 'app_payload',
55
+ type: {
56
+ kind: 'struct',
57
+ path: 'authwit::entrypoint::app::AppPayload',
58
+ fields: [
59
+ {
60
+ name: 'function_calls',
61
+ type: {
62
+ kind: 'array',
63
+ length: 4,
64
+ type: {
65
+ kind: 'struct',
66
+ path: 'authwit::entrypoint::function_call::FunctionCall',
67
+ fields: [
68
+ { name: 'args_hash', type: { kind: 'field' } },
69
+ {
70
+ name: 'function_selector',
71
+ type: {
72
+ kind: 'struct',
73
+ path: 'authwit::aztec::protocol_types::abis::function_selector::FunctionSelector',
74
+ fields: [{ name: 'inner', type: { kind: 'integer', sign: 'unsigned', width: 32 } }],
75
+ },
76
+ },
77
+ {
78
+ name: 'target_address',
79
+ type: {
80
+ kind: 'struct',
81
+ path: 'authwit::aztec::protocol_types::address::AztecAddress',
82
+ fields: [{ name: 'inner', type: { kind: 'field' } }],
83
+ },
84
+ },
85
+ { name: 'is_public', type: { kind: 'boolean' } },
86
+ ],
87
+ },
88
+ },
89
+ },
90
+ { name: 'nonce', type: { kind: 'field' } },
91
+ ],
92
+ },
93
+ visibility: 'public',
94
+ },
95
+ {
96
+ name: 'fee_payload',
97
+ type: {
98
+ kind: 'struct',
99
+ path: 'authwit::entrypoint::fee::FeePayload',
100
+ fields: [
101
+ {
102
+ name: 'function_calls',
103
+ type: {
104
+ kind: 'array',
105
+ length: 2,
106
+ type: {
107
+ kind: 'struct',
108
+ path: 'authwit::entrypoint::function_call::FunctionCall',
109
+ fields: [
110
+ { name: 'args_hash', type: { kind: 'field' } },
111
+ {
112
+ name: 'function_selector',
113
+ type: {
114
+ kind: 'struct',
115
+ path: 'authwit::aztec::protocol_types::abis::function_selector::FunctionSelector',
116
+ fields: [{ name: 'inner', type: { kind: 'integer', sign: 'unsigned', width: 32 } }],
117
+ },
118
+ },
119
+ {
120
+ name: 'target_address',
121
+ type: {
122
+ kind: 'struct',
123
+ path: 'authwit::aztec::protocol_types::address::AztecAddress',
124
+ fields: [{ name: 'inner', type: { kind: 'field' } }],
125
+ },
126
+ },
127
+ { name: 'is_public', type: { kind: 'boolean' } },
128
+ ],
129
+ },
130
+ },
131
+ },
132
+ { name: 'nonce', type: { kind: 'field' } },
133
+ ],
134
+ },
135
+ visibility: 'public',
136
+ },
137
+ ],
138
+ returnTypes: [],
139
+ } as FunctionAbi;
140
+ }
141
+ }
@@ -0,0 +1,39 @@
1
+ import { AccountInterface, AuthWitnessProvider, EntrypointInterface, FeeOptions } from '@aztec/aztec.js/account';
2
+ import { AuthWitness, FunctionCall, TxExecutionRequest } from '@aztec/circuit-types';
3
+ import { CompleteAddress, Fr } from '@aztec/circuits.js';
4
+ import { NodeInfo } from '@aztec/types/interfaces';
5
+
6
+ import { DefaultAccountEntrypoint } from './account_entrypoint.js';
7
+
8
+ /**
9
+ * Default implementation for an account interface. Requires that the account uses the default
10
+ * entrypoint signature, which accept an AppPayload and a FeePayload as defined in noir-libs/aztec-noir/src/entrypoint module
11
+ */
12
+ export class DefaultAccountInterface implements AccountInterface {
13
+ private entrypoint: EntrypointInterface;
14
+
15
+ constructor(
16
+ private authWitnessProvider: AuthWitnessProvider,
17
+ private address: CompleteAddress,
18
+ nodeInfo: Pick<NodeInfo, 'chainId' | 'protocolVersion'>,
19
+ ) {
20
+ this.entrypoint = new DefaultAccountEntrypoint(
21
+ address.address,
22
+ authWitnessProvider,
23
+ nodeInfo.chainId,
24
+ nodeInfo.protocolVersion,
25
+ );
26
+ }
27
+
28
+ createTxExecutionRequest(executions: FunctionCall[], fee?: FeeOptions): Promise<TxExecutionRequest> {
29
+ return this.entrypoint.createTxExecutionRequest(executions, fee);
30
+ }
31
+
32
+ createAuthWitness(message: Fr): Promise<AuthWitness> {
33
+ return this.authWitnessProvider.createAuthWitness(message);
34
+ }
35
+
36
+ getCompleteAddress(): CompleteAddress {
37
+ return this.address;
38
+ }
39
+ }
@@ -0,0 +1,4 @@
1
+ /** Default L1 chain ID to use when constructing txs (matches hardhat and anvil's default). */
2
+ export const DEFAULT_CHAIN_ID = 31337;
3
+ /** Default protocol version to use. */
4
+ export const DEFAULT_VERSION = 1;
@@ -0,0 +1,112 @@
1
+ import { FeeOptions } from '@aztec/aztec.js/account';
2
+ import { Fr } from '@aztec/aztec.js/fields';
3
+ import { FunctionCall, PackedArguments, emptyFunctionCall } from '@aztec/circuit-types';
4
+ import { padArrayEnd } from '@aztec/foundation/collection';
5
+ import { pedersenHash } from '@aztec/foundation/crypto';
6
+
7
+ // These must match the values defined in:
8
+ // - noir-projects/aztec-nr/aztec/src/entrypoint/app.nr
9
+ const ACCOUNT_MAX_CALLS = 4;
10
+ // - and noir-projects/aztec-nr/aztec/src/entrypoint/fee.nr
11
+ const FEE_MAX_CALLS = 2;
12
+
13
+ /** Encoded function call for account contract entrypoint */
14
+ type EntrypointFunctionCall = {
15
+ // eslint-disable-next-line camelcase
16
+ /** Arguments hash for the call */
17
+ args_hash: Fr;
18
+ // eslint-disable-next-line camelcase
19
+ /** Selector of the function to call */
20
+ function_selector: Fr;
21
+ // eslint-disable-next-line camelcase
22
+ /** Address of the contract to call */
23
+ target_address: Fr;
24
+ // eslint-disable-next-line camelcase
25
+ /** Whether the function is public or private */
26
+ is_public: boolean;
27
+ };
28
+
29
+ /** Encoded payload for the account contract entrypoint */
30
+ type EntrypointPayload = {
31
+ // eslint-disable-next-line camelcase
32
+ /** Encoded function calls to execute */
33
+ function_calls: EntrypointFunctionCall[];
34
+ /** A nonce for replay protection */
35
+ nonce: Fr;
36
+ };
37
+
38
+ /** Represents a generic payload to be executed in the context of an account contract */
39
+ export type PayloadWithArguments = {
40
+ /** The payload to be run */
41
+ payload: EntrypointPayload;
42
+ /** The packed arguments for the function calls */
43
+ packedArguments: PackedArguments[];
44
+ };
45
+
46
+ /**
47
+ * Builds a payload to be sent to the account contract
48
+ * @param calls - The function calls to run
49
+ * @param maxCalls - The maximum number of call expected to be run. Used for padding
50
+ * @returns A payload object and packed arguments
51
+ */
52
+ function buildPayload(calls: FunctionCall[], maxCalls: number): PayloadWithArguments {
53
+ const nonce = Fr.random();
54
+
55
+ const paddedCalls = padArrayEnd(calls, emptyFunctionCall(), maxCalls);
56
+ const packedArguments: PackedArguments[] = [];
57
+ for (const call of paddedCalls) {
58
+ packedArguments.push(PackedArguments.fromArgs(call.args));
59
+ }
60
+
61
+ const formattedCalls: EntrypointFunctionCall[] = paddedCalls.map((call, index) => ({
62
+ // eslint-disable-next-line camelcase
63
+ args_hash: packedArguments[index].hash,
64
+ // eslint-disable-next-line camelcase
65
+ function_selector: call.functionData.selector.toField(),
66
+ // eslint-disable-next-line camelcase
67
+ target_address: call.to.toField(),
68
+ // eslint-disable-next-line camelcase
69
+ is_public: !call.functionData.isPrivate,
70
+ }));
71
+
72
+ return {
73
+ payload: {
74
+ // eslint-disable-next-line camelcase
75
+ function_calls: formattedCalls,
76
+ nonce,
77
+ },
78
+ packedArguments,
79
+ };
80
+ }
81
+
82
+ /** Assembles an entrypoint app payload from a set of private and public function calls */
83
+ export function buildAppPayload(calls: FunctionCall[]): PayloadWithArguments {
84
+ return buildPayload(calls, ACCOUNT_MAX_CALLS);
85
+ }
86
+
87
+ /** Creates the payload for paying the fee for a transaction */
88
+ export function buildFeePayload(feeOpts?: FeeOptions): PayloadWithArguments {
89
+ const calls = feeOpts?.paymentMethod.getFunctionCalls(new Fr(feeOpts.maxFee)) ?? [];
90
+ return buildPayload(calls, FEE_MAX_CALLS);
91
+ }
92
+
93
+ /** Hashes a payload to a 32-byte buffer */
94
+ export function hashPayload(payload: EntrypointPayload, generatorIndex: number) {
95
+ return pedersenHash(
96
+ flattenPayload(payload).map(fr => fr.toBuffer()),
97
+ generatorIndex,
98
+ );
99
+ }
100
+
101
+ /** Flattens an payload */
102
+ function flattenPayload(payload: EntrypointPayload) {
103
+ return [
104
+ ...payload.function_calls.flatMap(call => [
105
+ call.args_hash,
106
+ call.function_selector,
107
+ call.target_address,
108
+ new Fr(call.is_public),
109
+ ]),
110
+ payload.nonce,
111
+ ];
112
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The `@aztec/accounts/defaults` export provides the base class {@link DefaultAccountContract} for implementing account contracts that use the default entrypoint payload module.
3
+ *
4
+ * Read more in {@link https://docs.aztec.network/developers/wallets/writing_an_account_contract | Writing an account contract}.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+
9
+ export * from './entrypoint_payload.js';
10
+ export * from './account_entrypoint.js';
11
+ export * from './account_interface.js';
12
+ export * from './account_contract.js';
13
+ export * from './constants.js';
@@ -0,0 +1,38 @@
1
+ import { AuthWitnessProvider } from '@aztec/aztec.js/account';
2
+ import { AuthWitness, CompleteAddress } from '@aztec/circuit-types';
3
+ import { Ecdsa } from '@aztec/circuits.js/barretenberg';
4
+ import { ContractArtifact } from '@aztec/foundation/abi';
5
+ import { Fr } from '@aztec/foundation/fields';
6
+
7
+ import { DefaultAccountContract } from '../defaults/account_contract.js';
8
+ import { EcdsaAccountContractArtifact } from './artifact.js';
9
+
10
+ /**
11
+ * Account contract that authenticates transactions using ECDSA signatures
12
+ * verified against a secp256k1 public key stored in an immutable encrypted note.
13
+ */
14
+ export class EcdsaAccountContract extends DefaultAccountContract {
15
+ constructor(private signingPrivateKey: Buffer) {
16
+ super(EcdsaAccountContractArtifact as ContractArtifact);
17
+ }
18
+
19
+ getDeploymentArgs() {
20
+ const signingPublicKey = new Ecdsa().computePublicKey(this.signingPrivateKey);
21
+ return [signingPublicKey.subarray(0, 32), signingPublicKey.subarray(32, 64)];
22
+ }
23
+
24
+ getAuthWitnessProvider(_address: CompleteAddress): AuthWitnessProvider {
25
+ return new EcdsaAuthWitnessProvider(this.signingPrivateKey);
26
+ }
27
+ }
28
+
29
+ /** Creates auth witnesses using ECDSA signatures. */
30
+ class EcdsaAuthWitnessProvider implements AuthWitnessProvider {
31
+ constructor(private signingPrivateKey: Buffer) {}
32
+
33
+ createAuthWitness(message: Fr): Promise<AuthWitness> {
34
+ const ecdsa = new Ecdsa();
35
+ const signature = ecdsa.constructSignature(message.toBuffer(), this.signingPrivateKey);
36
+ return Promise.resolve(new AuthWitness(message, [...signature.r, ...signature.s]));
37
+ }
38
+ }
@@ -0,0 +1,5 @@
1
+ import { NoirCompiledContract, loadContractArtifact } from '@aztec/aztec.js';
2
+
3
+ import EcdsaAccountContractJson from '../artifacts/EcdsaAccount.json' assert { type: 'json' };
4
+
5
+ export const EcdsaAccountContractArtifact = loadContractArtifact(EcdsaAccountContractJson as NoirCompiledContract);
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The `@aztec/accounts/ecdsa` export provides an ECDSA account contract implementation, that uses an ECDSA private key for authentication, and a Grumpkin key for encryption.
3
+ * Consider using this account type when working with integrations with Ethereum wallets.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { AccountManager, Salt } from '@aztec/aztec.js/account';
8
+ import { AccountWallet, getWallet } from '@aztec/aztec.js/wallet';
9
+ import { GrumpkinPrivateKey, PXE } from '@aztec/circuit-types';
10
+ import { AztecAddress } from '@aztec/circuits.js';
11
+
12
+ import { EcdsaAccountContract } from './account_contract.js';
13
+
14
+ export { EcdsaAccountContractArtifact } from './artifact.js';
15
+ export { EcdsaAccountContract };
16
+
17
+ /**
18
+ * Creates an Account that relies on an ECDSA signing key for authentication.
19
+ * @param pxe - An PXE server instance.
20
+ * @param encryptionPrivateKey - Grumpkin key used for note encryption.
21
+ * @param signingPrivateKey - Secp256k1 key used for signing transactions.
22
+ * @param salt - Deployment salt.
23
+ */
24
+ export function getEcdsaAccount(
25
+ pxe: PXE,
26
+ encryptionPrivateKey: GrumpkinPrivateKey,
27
+ signingPrivateKey: Buffer,
28
+ salt?: Salt,
29
+ ): AccountManager {
30
+ return new AccountManager(pxe, encryptionPrivateKey, new EcdsaAccountContract(signingPrivateKey), salt);
31
+ }
32
+
33
+ /**
34
+ * Gets a wallet for an already registered account using ECDSA signatures.
35
+ * @param pxe - An PXE server instance.
36
+ * @param address - Address for the account.
37
+ * @param signingPrivateKey - ECDSA key used for signing transactions.
38
+ * @returns A wallet for this account that can be used to interact with a contract instance.
39
+ */
40
+ export function getEcdsaWallet(pxe: PXE, address: AztecAddress, signingPrivateKey: Buffer): Promise<AccountWallet> {
41
+ return getWallet(pxe, address, new EcdsaAccountContract(signingPrivateKey));
42
+ }
@@ -0,0 +1,38 @@
1
+ import { AuthWitnessProvider } from '@aztec/aztec.js/account';
2
+ import { AuthWitness, CompleteAddress, GrumpkinPrivateKey } from '@aztec/circuit-types';
3
+ import { Schnorr } from '@aztec/circuits.js/barretenberg';
4
+ import { ContractArtifact } from '@aztec/foundation/abi';
5
+ import { Fr } from '@aztec/foundation/fields';
6
+
7
+ import { DefaultAccountContract } from '../defaults/account_contract.js';
8
+ import { SchnorrAccountContractArtifact } from './artifact.js';
9
+
10
+ /**
11
+ * Account contract that authenticates transactions using Schnorr signatures
12
+ * verified against a Grumpkin public key stored in an immutable encrypted note.
13
+ */
14
+ export class SchnorrAccountContract extends DefaultAccountContract {
15
+ constructor(private signingPrivateKey: GrumpkinPrivateKey) {
16
+ super(SchnorrAccountContractArtifact as ContractArtifact);
17
+ }
18
+
19
+ getDeploymentArgs() {
20
+ const signingPublicKey = new Schnorr().computePublicKey(this.signingPrivateKey);
21
+ return [signingPublicKey.x, signingPublicKey.y];
22
+ }
23
+
24
+ getAuthWitnessProvider(_address: CompleteAddress): AuthWitnessProvider {
25
+ return new SchnorrAuthWitnessProvider(this.signingPrivateKey);
26
+ }
27
+ }
28
+
29
+ /** Creates auth witnesses using Schnorr signatures. */
30
+ class SchnorrAuthWitnessProvider implements AuthWitnessProvider {
31
+ constructor(private signingPrivateKey: GrumpkinPrivateKey) {}
32
+
33
+ createAuthWitness(message: Fr): Promise<AuthWitness> {
34
+ const schnorr = new Schnorr();
35
+ const signature = schnorr.constructSignature(message.toBuffer(), this.signingPrivateKey).toBuffer();
36
+ return Promise.resolve(new AuthWitness(message, [...signature]));
37
+ }
38
+ }
@@ -0,0 +1,5 @@
1
+ import { NoirCompiledContract, loadContractArtifact } from '@aztec/aztec.js';
2
+
3
+ import SchnorrAccountContractJson from '../artifacts/SchnorrAccount.json' assert { type: 'json' };
4
+
5
+ export const SchnorrAccountContractArtifact = loadContractArtifact(SchnorrAccountContractJson as NoirCompiledContract);
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The `@aztec/accounts/schnorr` export provides an account contract implementation that uses Schnorr signatures with a Grumpkin key for authentication, and a separate Grumpkin key for encryption.
3
+ * This is the suggested account contract type for most use cases within Aztec.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { AccountManager, Salt } from '@aztec/aztec.js/account';
8
+ import { AccountWallet, getWallet } from '@aztec/aztec.js/wallet';
9
+ import { GrumpkinPrivateKey, PXE } from '@aztec/circuit-types';
10
+ import { AztecAddress } from '@aztec/circuits.js';
11
+
12
+ import { SchnorrAccountContract } from './account_contract.js';
13
+
14
+ export { SchnorrAccountContract };
15
+
16
+ export { SchnorrAccountContractArtifact } from './artifact.js';
17
+
18
+ /**
19
+ * Creates an Account Manager that relies on a Grumpkin signing key for authentication.
20
+ * @param pxe - An PXE server instance.
21
+ * @param encryptionPrivateKey - Grumpkin key used for note encryption.
22
+ * @param signingPrivateKey - Grumpkin key used for signing transactions.
23
+ * @param salt - Deployment salt.
24
+ */
25
+ export function getSchnorrAccount(
26
+ pxe: PXE,
27
+ encryptionPrivateKey: GrumpkinPrivateKey,
28
+ signingPrivateKey: GrumpkinPrivateKey,
29
+ salt?: Salt,
30
+ ): AccountManager {
31
+ return new AccountManager(pxe, encryptionPrivateKey, new SchnorrAccountContract(signingPrivateKey), salt);
32
+ }
33
+
34
+ /**
35
+ * Gets a wallet for an already registered account using Schnorr signatures.
36
+ * @param pxe - An PXE server instance.
37
+ * @param address - Address for the account.
38
+ * @param signingPrivateKey - Grumpkin key used for signing transactions.
39
+ * @returns A wallet for this account that can be used to interact with a contract instance.
40
+ */
41
+ export function getSchnorrWallet(
42
+ pxe: PXE,
43
+ address: AztecAddress,
44
+ signingPrivateKey: GrumpkinPrivateKey,
45
+ ): Promise<AccountWallet> {
46
+ return getWallet(pxe, address, new SchnorrAccountContract(signingPrivateKey));
47
+ }
@@ -0,0 +1,45 @@
1
+ import { generatePublicKey } from '@aztec/aztec.js';
2
+ import { AuthWitnessProvider } from '@aztec/aztec.js/account';
3
+ import { AuthWitness, CompleteAddress, GrumpkinPrivateKey } from '@aztec/circuit-types';
4
+ import { PartialAddress } from '@aztec/circuits.js';
5
+ import { Schnorr } from '@aztec/circuits.js/barretenberg';
6
+ import { ContractArtifact } from '@aztec/foundation/abi';
7
+ import { Fr } from '@aztec/foundation/fields';
8
+
9
+ import { DefaultAccountContract } from '../defaults/account_contract.js';
10
+ import { SchnorrSingleKeyAccountContractArtifact } from './artifact.js';
11
+
12
+ /**
13
+ * Account contract that authenticates transactions using Schnorr signatures verified against
14
+ * the note encryption key, relying on a single private key for both encryption and authentication.
15
+ */
16
+ export class SingleKeyAccountContract extends DefaultAccountContract {
17
+ constructor(private encryptionPrivateKey: GrumpkinPrivateKey) {
18
+ super(SchnorrSingleKeyAccountContractArtifact as ContractArtifact);
19
+ }
20
+
21
+ getDeploymentArgs(): any[] {
22
+ return [];
23
+ }
24
+
25
+ getAuthWitnessProvider({ partialAddress }: CompleteAddress): AuthWitnessProvider {
26
+ return new SingleKeyAuthWitnessProvider(this.encryptionPrivateKey, partialAddress);
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Creates auth witnesses using Schnorr signatures and including the partial address and public key
32
+ * in the witness, so verifiers do not need to store the public key and can instead validate it
33
+ * by reconstructing the current address.
34
+ */
35
+ class SingleKeyAuthWitnessProvider implements AuthWitnessProvider {
36
+ constructor(private privateKey: GrumpkinPrivateKey, private partialAddress: PartialAddress) {}
37
+
38
+ createAuthWitness(message: Fr): Promise<AuthWitness> {
39
+ const schnorr = new Schnorr();
40
+ const signature = schnorr.constructSignature(message.toBuffer(), this.privateKey);
41
+ const publicKey = generatePublicKey(this.privateKey);
42
+ const witness = [...publicKey.toFields(), ...signature.toBuffer(), this.partialAddress];
43
+ return Promise.resolve(new AuthWitness(message, witness));
44
+ }
45
+ }
@@ -0,0 +1,7 @@
1
+ import { NoirCompiledContract, loadContractArtifact } from '@aztec/aztec.js';
2
+
3
+ import SchnorrSingleKeyAccountContractJson from '../artifacts/SchnorrSingleKeyAccount.json' assert { type: 'json' };
4
+
5
+ export const SchnorrSingleKeyAccountContractArtifact = loadContractArtifact(
6
+ SchnorrSingleKeyAccountContractJson as NoirCompiledContract,
7
+ );
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The `@aztec/accounts/single_key` export provides a testing account contract implementation that uses a single Grumpkin key for both authentication and encryption.
3
+ * It is not recommended to use this account type in production.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { AccountManager, Salt } from '@aztec/aztec.js/account';
8
+ import { AccountWallet, getWallet } from '@aztec/aztec.js/wallet';
9
+ import { GrumpkinPrivateKey, PXE } from '@aztec/circuit-types';
10
+ import { AztecAddress } from '@aztec/circuits.js';
11
+
12
+ import { SingleKeyAccountContract } from './account_contract.js';
13
+
14
+ export { SingleKeyAccountContract };
15
+
16
+ export { SchnorrSingleKeyAccountContractArtifact as SingleKeyAccountContractArtifact } from './artifact.js';
17
+
18
+ /**
19
+ * Creates an Account that uses the same Grumpkin key for encryption and authentication.
20
+ * @param pxe - An PXE server instance.
21
+ * @param encryptionAndSigningPrivateKey - Grumpkin key used for note encryption and signing transactions.
22
+ * @param salt - Deployment salt .
23
+ */
24
+ export function getSingleKeyAccount(
25
+ pxe: PXE,
26
+ encryptionAndSigningPrivateKey: GrumpkinPrivateKey,
27
+ salt?: Salt,
28
+ ): AccountManager {
29
+ return new AccountManager(
30
+ pxe,
31
+ encryptionAndSigningPrivateKey,
32
+ new SingleKeyAccountContract(encryptionAndSigningPrivateKey),
33
+ salt,
34
+ );
35
+ }
36
+
37
+ /**
38
+ * Gets a wallet for an already registered account using Schnorr signatures with a single key for encryption and authentication.
39
+ * @param pxe - An PXE server instance.
40
+ * @param address - Address for the account.
41
+ * @param signingPrivateKey - Grumpkin key used for note encryption and signing transactions.
42
+ * @returns A wallet for this account that can be used to interact with a contract instance.
43
+ */
44
+ export function getSingleKeyWallet(
45
+ pxe: PXE,
46
+ address: AztecAddress,
47
+ signingKey: GrumpkinPrivateKey,
48
+ ): Promise<AccountWallet> {
49
+ return getWallet(pxe, address, new SingleKeyAccountContract(signingKey));
50
+ }
51
+
52
+ export { getSingleKeyAccount as getUnsafeSchnorrAccount, getSingleKeyWallet as getUnsafeSchnorrWallet };