@aztec/wallets 0.0.1-commit.f8ca9b2f3

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 (37) hide show
  1. package/dest/embedded/account-contract-providers/bundle.d.ts +17 -0
  2. package/dest/embedded/account-contract-providers/bundle.d.ts.map +1 -0
  3. package/dest/embedded/account-contract-providers/bundle.js +23 -0
  4. package/dest/embedded/account-contract-providers/lazy.d.ts +17 -0
  5. package/dest/embedded/account-contract-providers/lazy.d.ts.map +1 -0
  6. package/dest/embedded/account-contract-providers/lazy.js +25 -0
  7. package/dest/embedded/account-contract-providers/types.d.ts +18 -0
  8. package/dest/embedded/account-contract-providers/types.d.ts.map +1 -0
  9. package/dest/embedded/account-contract-providers/types.js +6 -0
  10. package/dest/embedded/embedded_wallet.d.ts +42 -0
  11. package/dest/embedded/embedded_wallet.d.ts.map +1 -0
  12. package/dest/embedded/embedded_wallet.js +153 -0
  13. package/dest/embedded/embedded_wallet_browser.d.ts +5 -0
  14. package/dest/embedded/embedded_wallet_browser.d.ts.map +1 -0
  15. package/dest/embedded/embedded_wallet_browser.js +31 -0
  16. package/dest/embedded/entrypoints/browser.d.ts +9 -0
  17. package/dest/embedded/entrypoints/browser.d.ts.map +1 -0
  18. package/dest/embedded/entrypoints/browser.js +35 -0
  19. package/dest/embedded/entrypoints/node.d.ts +18 -0
  20. package/dest/embedded/entrypoints/node.d.ts.map +1 -0
  21. package/dest/embedded/entrypoints/node.js +44 -0
  22. package/dest/embedded/wallet_db.d.ts +34 -0
  23. package/dest/embedded/wallet_db.d.ts.map +1 -0
  24. package/dest/embedded/wallet_db.js +120 -0
  25. package/dest/testing.d.ts +12 -0
  26. package/dest/testing.d.ts.map +1 -0
  27. package/dest/testing.js +24 -0
  28. package/package.json +91 -0
  29. package/src/embedded/account-contract-providers/bundle.ts +35 -0
  30. package/src/embedded/account-contract-providers/lazy.ts +37 -0
  31. package/src/embedded/account-contract-providers/types.ts +18 -0
  32. package/src/embedded/embedded_wallet.ts +201 -0
  33. package/src/embedded/embedded_wallet_browser.ts +40 -0
  34. package/src/embedded/entrypoints/browser.ts +50 -0
  35. package/src/embedded/entrypoints/node.ts +80 -0
  36. package/src/embedded/wallet_db.ts +134 -0
  37. package/src/testing.ts +42 -0
@@ -0,0 +1,80 @@
1
+ import { createAztecNodeClient } from '@aztec/aztec.js/node';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { createStore as createWalletStore, openTmpStore } from '@aztec/kv-store/lmdb-v2';
4
+ import type { PXEConfig } from '@aztec/pxe/config';
5
+ import { getPXEConfig } from '@aztec/pxe/config';
6
+ import { type PXECreationOptions, createPXE } from '@aztec/pxe/server';
7
+ import type { AztecNode } from '@aztec/stdlib/interfaces/client';
8
+
9
+ import { BundleAccountContractsProvider } from '../account-contract-providers/bundle.js';
10
+ import { EmbeddedWallet, type EmbeddedWalletOptions } from '../embedded_wallet.js';
11
+ import { WalletDB } from '../wallet_db.js';
12
+
13
+ export type NodeEmbeddedWalletOptions = EmbeddedWalletOptions & {
14
+ /** Override PXE configuration. */
15
+ pxeConfig?: Partial<PXEConfig>;
16
+ /** Advanced PXE creation options (custom store, prover, simulator). */
17
+ pxeOptions?: PXECreationOptions;
18
+ };
19
+
20
+ export class NodeEmbeddedWallet extends EmbeddedWallet {
21
+ static async create(
22
+ nodeOrUrl: string | AztecNode,
23
+ options: NodeEmbeddedWalletOptions = {},
24
+ ): Promise<NodeEmbeddedWallet> {
25
+ const rootLogger = options.logger ?? createLogger('embedded-wallet');
26
+
27
+ const aztecNode = typeof nodeOrUrl === 'string' ? createAztecNodeClient(nodeOrUrl) : nodeOrUrl;
28
+
29
+ const pxeConfig = Object.assign(getPXEConfig(), {
30
+ proverEnabled: options.pxeConfig?.proverEnabled ?? false,
31
+ ...options.pxeConfig,
32
+ });
33
+
34
+ if (options.ephemeral) {
35
+ delete pxeConfig.dataDirectory;
36
+ }
37
+
38
+ const pxeOptions: PXECreationOptions = {
39
+ ...options.pxeOptions,
40
+ loggers: {
41
+ store: rootLogger.createChild('pxe:data'),
42
+ pxe: rootLogger.createChild('pxe:service'),
43
+ prover: rootLogger.createChild('pxe:prover'),
44
+ ...options.pxeOptions?.loggers,
45
+ },
46
+ };
47
+
48
+ const pxe = await createPXE(aztecNode, pxeConfig, pxeOptions);
49
+
50
+ const l1Contracts = await aztecNode.getL1ContractAddresses();
51
+ const rollupAddress = l1Contracts.rollupAddress;
52
+
53
+ const walletDBStore = options.ephemeral
54
+ ? await openTmpStore(
55
+ 'wallet_data',
56
+ true,
57
+ undefined,
58
+ undefined,
59
+ rootLogger.createChild('wallet:data').getBindings(),
60
+ )
61
+ : await createWalletStore(
62
+ 'wallet_data',
63
+ 1,
64
+ {
65
+ dataDirectory: pxeConfig.dataDirectory,
66
+ dataStoreMapSizeKb: pxeConfig.dataStoreMapSizeKb,
67
+ l1Contracts: { rollupAddress },
68
+ },
69
+ rootLogger.createChild('wallet:data').getBindings(),
70
+ );
71
+ const walletDB = WalletDB.init(walletDBStore, rootLogger.createChild('wallet:db').info);
72
+
73
+ return new NodeEmbeddedWallet(pxe, aztecNode, walletDB, new BundleAccountContractsProvider(), rootLogger);
74
+ }
75
+ }
76
+
77
+ export { NodeEmbeddedWallet as EmbeddedWallet };
78
+ export type { EmbeddedWalletOptions } from '../embedded_wallet.js';
79
+ export { WalletDB } from '../wallet_db.js';
80
+ export type { AccountType } from '../wallet_db.js';
@@ -0,0 +1,134 @@
1
+ import type { Aliased } from '@aztec/aztec.js/wallet';
2
+ import { Fq, Fr } from '@aztec/foundation/curves/bn254';
3
+ import type { LogFn } from '@aztec/foundation/log';
4
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
5
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
6
+
7
+ export const AccountTypes = ['schnorr', 'ecdsasecp256r1', 'ecdsasecp256k1'] as const;
8
+ export type AccountType = (typeof AccountTypes)[number];
9
+
10
+ function accountKey(field: string, address: AztecAddress | string): string {
11
+ return `${field}:${address.toString()}`;
12
+ }
13
+
14
+ export class WalletDB {
15
+ private constructor(
16
+ private accounts: AztecAsyncMap<string, Buffer>,
17
+ private aliases: AztecAsyncMap<string, Buffer>,
18
+ private userLog: LogFn,
19
+ ) {}
20
+
21
+ static init(store: AztecAsyncKVStore, userLog: LogFn) {
22
+ const accounts = store.openMap<string, Buffer>('accounts');
23
+ const aliases = store.openMap<string, Buffer>('aliases');
24
+ return new WalletDB(accounts, aliases, userLog);
25
+ }
26
+
27
+ async storeAccount(
28
+ address: AztecAddress,
29
+ {
30
+ type,
31
+ secretKey,
32
+ salt,
33
+ alias,
34
+ signingKey,
35
+ }: {
36
+ type: AccountType;
37
+ secretKey: Fr;
38
+ salt: Fr;
39
+ signingKey: Fq | Buffer;
40
+ alias: string | undefined;
41
+ },
42
+ log: LogFn = this.userLog,
43
+ ) {
44
+ if (alias) {
45
+ await this.aliases.set(`accounts:${alias}`, Buffer.from(address.toString()));
46
+ }
47
+ await this.accounts.set(accountKey('type', address), Buffer.from(type));
48
+ await this.accounts.set(accountKey('sk', address), secretKey.toBuffer());
49
+ await this.accounts.set(accountKey('salt', address), salt.toBuffer());
50
+ await this.accounts.set(
51
+ accountKey('signingKey', address),
52
+ 'toBuffer' in signingKey ? signingKey.toBuffer() : signingKey,
53
+ );
54
+ log(`Account stored in database${alias ? ` with alias ${alias}` : ''}`);
55
+ }
56
+
57
+ async storeSender(address: AztecAddress, alias: string, log: LogFn = this.userLog) {
58
+ await this.aliases.set(`senders:${alias}`, Buffer.from(address.toString()));
59
+ log(`Sender stored in database with alias ${alias}`);
60
+ }
61
+
62
+ async retrieveAccount(address: AztecAddress | string) {
63
+ const secretKeyBuffer = await this.accounts.getAsync(accountKey('sk', address));
64
+ if (!secretKeyBuffer) {
65
+ throw new Error(`Account "${address.toString()}" does not exist on this wallet.`);
66
+ }
67
+ const [saltBuffer, typeBuffer, signingKey] = await Promise.all([
68
+ this.accounts.getAsync(accountKey('salt', address)),
69
+ this.accounts.getAsync(accountKey('type', address)),
70
+ this.accounts.getAsync(accountKey('signingKey', address)),
71
+ ]);
72
+ const secretKey = Fr.fromBuffer(secretKeyBuffer);
73
+ const salt = Fr.fromBuffer(saltBuffer!);
74
+ const type = typeBuffer!.toString('utf8') as AccountType;
75
+ return { address, secretKey, salt, type, signingKey: signingKey! };
76
+ }
77
+
78
+ async listAccounts(): Promise<Aliased<AztecAddress>[]> {
79
+ // Read aliases and account addresses in parallel using range queries
80
+ const [aliasesByAddress, accountAddresses] = await Promise.all([
81
+ this.#readAccountAliases(),
82
+ this.#readAccountAddresses(),
83
+ ]);
84
+
85
+ return accountAddresses.map(addressStr => ({
86
+ alias: aliasesByAddress.get(addressStr) ?? '',
87
+ item: AztecAddress.fromString(addressStr),
88
+ }));
89
+ }
90
+
91
+ async listSenders(): Promise<Aliased<AztecAddress>[]> {
92
+ const result: Aliased<AztecAddress>[] = [];
93
+ for await (const [alias, item] of this.aliases.entriesAsync({ start: 'senders:', end: 'senders:\uffff' })) {
94
+ result.push({
95
+ alias: alias.slice('senders:'.length),
96
+ item: AztecAddress.fromString(item.toString()),
97
+ });
98
+ }
99
+ return result;
100
+ }
101
+
102
+ async #readAccountAliases(): Promise<Map<string, string>> {
103
+ const aliasesByAddress = new Map<string, string>();
104
+ for await (const [alias, item] of this.aliases.entriesAsync({ start: 'accounts:', end: 'accounts:\uffff' })) {
105
+ const address = item.toString();
106
+ aliasesByAddress.set(address, alias.slice('accounts:'.length));
107
+ }
108
+ return aliasesByAddress;
109
+ }
110
+
111
+ async #readAccountAddresses(): Promise<string[]> {
112
+ const addresses: string[] = [];
113
+ // Range query on 'type:' prefix — one entry per account, avoids scanning sk/salt/signingKey entries
114
+ for await (const [key] of this.accounts.entriesAsync({ start: 'type:', end: 'type:\uffff' })) {
115
+ addresses.push(key.slice('type:'.length));
116
+ }
117
+ return addresses;
118
+ }
119
+
120
+ async deleteAccount(address: AztecAddress) {
121
+ await Promise.all([
122
+ this.accounts.delete(accountKey('sk', address)),
123
+ this.accounts.delete(accountKey('salt', address)),
124
+ this.accounts.delete(accountKey('type', address)),
125
+ this.accounts.delete(accountKey('signingKey', address)),
126
+ ]);
127
+ // Clean up alias if one exists
128
+ const aliasesByAddress = await this.#readAccountAliases();
129
+ const alias = aliasesByAddress.get(address.toString());
130
+ if (alias) {
131
+ await this.aliases.delete(`accounts:${alias}`);
132
+ }
133
+ }
134
+ }
package/src/testing.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type { InitialAccountData } from '@aztec/accounts/testing';
2
+ import { getInitialTestAccountsData } from '@aztec/accounts/testing/lazy';
3
+ import type { WaitOpts } from '@aztec/aztec.js/contracts';
4
+ import type { AccountManager } from '@aztec/aztec.js/wallet';
5
+ import type { Fq, Fr } from '@aztec/foundation/curves/bn254';
6
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
7
+
8
+ interface WalletWithSchnorrAccounts {
9
+ createSchnorrAccount(secret: Fr, salt: Fr, signingKey?: Fq, alias?: string): Promise<AccountManager>;
10
+ }
11
+
12
+ export async function deployFundedSchnorrAccounts(
13
+ wallet: WalletWithSchnorrAccounts,
14
+ accountsData: InitialAccountData[],
15
+ waitOptions?: WaitOpts,
16
+ ) {
17
+ const accountManagers = [];
18
+ // Serial due to https://github.com/AztecProtocol/aztec-packages/issues/12045
19
+ for (let i = 0; i < accountsData.length; i++) {
20
+ const { secret, salt, signingKey } = accountsData[i];
21
+ const accountManager = await wallet.createSchnorrAccount(secret, salt, signingKey);
22
+ const deployMethod = await accountManager.getDeployMethod();
23
+ await deployMethod.send({
24
+ from: AztecAddress.ZERO,
25
+ skipClassPublication: i !== 0,
26
+ wait: waitOptions,
27
+ });
28
+ accountManagers.push(accountManager);
29
+ }
30
+ return accountManagers;
31
+ }
32
+
33
+ export async function registerInitialLocalNetworkAccountsInWallet(
34
+ wallet: WalletWithSchnorrAccounts,
35
+ ): Promise<AztecAddress[]> {
36
+ const testAccounts = await getInitialTestAccountsData();
37
+ return Promise.all(
38
+ testAccounts.map(async account => {
39
+ return (await wallet.createSchnorrAccount(account.secret, account.salt, account.signingKey)).address;
40
+ }),
41
+ );
42
+ }