@aztec/wallet-sdk 3.0.0-nightly.20251122

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 (38) hide show
  1. package/README.md +424 -0
  2. package/dest/base-wallet/base_wallet.d.ts +94 -0
  3. package/dest/base-wallet/base_wallet.d.ts.map +1 -0
  4. package/dest/base-wallet/base_wallet.js +225 -0
  5. package/dest/base-wallet/index.d.ts +2 -0
  6. package/dest/base-wallet/index.d.ts.map +1 -0
  7. package/dest/base-wallet/index.js +1 -0
  8. package/dest/manager/index.d.ts +8 -0
  9. package/dest/manager/index.d.ts.map +1 -0
  10. package/dest/manager/index.js +5 -0
  11. package/dest/manager/types.d.ts +64 -0
  12. package/dest/manager/types.d.ts.map +1 -0
  13. package/dest/manager/types.js +3 -0
  14. package/dest/manager/wallet_manager.d.ts +27 -0
  15. package/dest/manager/wallet_manager.d.ts.map +1 -0
  16. package/dest/manager/wallet_manager.js +77 -0
  17. package/dest/providers/extension/extension_provider.d.ts +17 -0
  18. package/dest/providers/extension/extension_provider.d.ts.map +1 -0
  19. package/dest/providers/extension/extension_provider.js +56 -0
  20. package/dest/providers/extension/extension_wallet.d.ts +23 -0
  21. package/dest/providers/extension/extension_wallet.d.ts.map +1 -0
  22. package/dest/providers/extension/extension_wallet.js +96 -0
  23. package/dest/providers/extension/index.d.ts +4 -0
  24. package/dest/providers/extension/index.d.ts.map +1 -0
  25. package/dest/providers/extension/index.js +2 -0
  26. package/dest/providers/types.d.ts +67 -0
  27. package/dest/providers/types.d.ts.map +1 -0
  28. package/dest/providers/types.js +3 -0
  29. package/package.json +95 -0
  30. package/src/base-wallet/base_wallet.ts +342 -0
  31. package/src/base-wallet/index.ts +1 -0
  32. package/src/manager/index.ts +24 -0
  33. package/src/manager/types.ts +69 -0
  34. package/src/manager/wallet_manager.ts +86 -0
  35. package/src/providers/extension/extension_provider.ts +72 -0
  36. package/src/providers/extension/extension_wallet.ts +124 -0
  37. package/src/providers/extension/index.ts +3 -0
  38. package/src/providers/types.ts +71 -0
@@ -0,0 +1,225 @@
1
+ import { GAS_ESTIMATION_DA_GAS_LIMIT, GAS_ESTIMATION_L2_GAS_LIMIT, GAS_ESTIMATION_TEARDOWN_DA_GAS_LIMIT, GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT } from '@aztec/constants';
2
+ import { AccountFeePaymentMethodOptions } from '@aztec/entrypoints/account';
3
+ import { Fr } from '@aztec/foundation/fields';
4
+ import { createLogger } from '@aztec/foundation/log';
5
+ import { decodeFromAbi } from '@aztec/stdlib/abi';
6
+ import { computePartialAddress, getContractClassFromArtifact } from '@aztec/stdlib/contract';
7
+ import { SimulationError } from '@aztec/stdlib/errors';
8
+ import { Gas, GasSettings } from '@aztec/stdlib/gas';
9
+ import { mergeExecutionPayloads } from '@aztec/stdlib/tx';
10
+ import { inspect } from 'util';
11
+ /**
12
+ * A base class for Wallet implementations
13
+ */ export class BaseWallet {
14
+ pxe;
15
+ aztecNode;
16
+ log;
17
+ baseFeePadding;
18
+ cancellableTransactions;
19
+ // Protected because we want to force wallets to instantiate their own PXE.
20
+ constructor(pxe, aztecNode){
21
+ this.pxe = pxe;
22
+ this.aztecNode = aztecNode;
23
+ this.log = createLogger('wallet-sdk:base_wallet');
24
+ this.baseFeePadding = 0.5;
25
+ this.cancellableTransactions = false;
26
+ }
27
+ /**
28
+ * Returns the list of aliased contacts associated with the wallet.
29
+ * This base implementation directly returns PXE's senders, but note that in general contacts are a superset of senders.
30
+ * - Senders: Addresses we check during synching in case they sent us notes,
31
+ * - Contacts: more general concept akin to a phone's contact list.
32
+ * @returns The aliased collection of AztecAddresses that form this wallet's address book
33
+ */ async getAddressBook() {
34
+ const senders = await this.pxe.getSenders();
35
+ return senders.map((sender)=>({
36
+ item: sender,
37
+ alias: ''
38
+ }));
39
+ }
40
+ async getChainInfo() {
41
+ const { l1ChainId, rollupVersion } = await this.aztecNode.getNodeInfo();
42
+ return {
43
+ chainId: new Fr(l1ChainId),
44
+ version: new Fr(rollupVersion)
45
+ };
46
+ }
47
+ async createTxExecutionRequestFromPayloadAndFee(executionPayload, from, feeOptions) {
48
+ const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
49
+ const executionOptions = {
50
+ txNonce: Fr.random(),
51
+ cancellable: this.cancellableTransactions,
52
+ feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions
53
+ };
54
+ const finalExecutionPayload = feeExecutionPayload ? mergeExecutionPayloads([
55
+ feeExecutionPayload,
56
+ executionPayload
57
+ ]) : executionPayload;
58
+ const fromAccount = await this.getAccountFromAddress(from);
59
+ return fromAccount.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, executionOptions);
60
+ }
61
+ async createAuthWit(from, messageHashOrIntent) {
62
+ const account = await this.getAccountFromAddress(from);
63
+ return account.createAuthWit(messageHashOrIntent);
64
+ }
65
+ async batch(methods) {
66
+ const results = [];
67
+ for (const method of methods){
68
+ const { name, args } = method;
69
+ // Type safety is guaranteed by the BatchedMethod type, which ensures that:
70
+ // 1. `name` is a valid batchable method name
71
+ // 2. `args` matches the parameter types of that specific method
72
+ // 3. The return type is correctly mapped in BatchResults<T>
73
+ // We use dynamic dispatch here for simplicity, but the types are enforced at the call site.
74
+ const fn = this[name];
75
+ const result = await fn.apply(this, args);
76
+ // Wrap result with method name for discriminated union deserialization
77
+ results.push({
78
+ name,
79
+ result
80
+ });
81
+ }
82
+ return results;
83
+ }
84
+ /**
85
+ * Completes partial user-provided fee options with wallet defaults.
86
+ * @param from - The address where the transaction is being sent from
87
+ * @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
88
+ * @param gasSettings - User-provided partial gas settings
89
+ * @returns - Complete fee options that can be used to create a transaction execution request
90
+ */ async completeFeeOptions(from, feePayer, gasSettings) {
91
+ const maxFeesPerGas = gasSettings?.maxFeesPerGas ?? (await this.aztecNode.getCurrentBaseFees()).mul(1 + this.baseFeePadding);
92
+ let accountFeePaymentMethodOptions;
93
+ // The transaction does not include a fee payment method, so we set the flag
94
+ // for the account to use its fee juice balance
95
+ if (!feePayer) {
96
+ accountFeePaymentMethodOptions = AccountFeePaymentMethodOptions.PREEXISTING_FEE_JUICE;
97
+ } else {
98
+ // The transaction includes fee payment method, so we check if we are the fee payer for it
99
+ // (this can only happen if the embedded payment method is FeeJuiceWithClaim)
100
+ accountFeePaymentMethodOptions = from.equals(feePayer) ? AccountFeePaymentMethodOptions.FEE_JUICE_WITH_CLAIM : AccountFeePaymentMethodOptions.EXTERNAL;
101
+ }
102
+ const fullGasSettings = GasSettings.default({
103
+ ...gasSettings,
104
+ maxFeesPerGas
105
+ });
106
+ this.log.debug(`Using L2 gas settings`, fullGasSettings);
107
+ return {
108
+ gasSettings: fullGasSettings,
109
+ walletFeePaymentMethod: undefined,
110
+ accountFeePaymentMethodOptions
111
+ };
112
+ }
113
+ /**
114
+ * Completes partial user-provided fee options with unreasonably high gas limits
115
+ * for gas estimation. Uses the same logic as completeFeeOptions but sets high limits
116
+ * to avoid running out of gas during estimation.
117
+ * @param from - The address where the transaction is being sent from
118
+ * @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
119
+ * @param gasSettings - User-provided partial gas settings
120
+ */ async completeFeeOptionsForEstimation(from, feePayer, gasSettings) {
121
+ const defaultFeeOptions = await this.completeFeeOptions(from, feePayer, gasSettings);
122
+ const { gasSettings: { maxFeesPerGas, maxPriorityFeesPerGas } } = defaultFeeOptions;
123
+ // Use unrealistically high gas limits for estimation to avoid running out of gas.
124
+ // They will be tuned down after the simulation.
125
+ const gasSettingsForEstimation = new GasSettings(new Gas(GAS_ESTIMATION_DA_GAS_LIMIT, GAS_ESTIMATION_L2_GAS_LIMIT), new Gas(GAS_ESTIMATION_TEARDOWN_DA_GAS_LIMIT, GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT), maxFeesPerGas, maxPriorityFeesPerGas);
126
+ return {
127
+ ...defaultFeeOptions,
128
+ gasSettings: gasSettingsForEstimation
129
+ };
130
+ }
131
+ registerSender(address, _alias = '') {
132
+ return this.pxe.registerSender(address);
133
+ }
134
+ async registerContract(instance, artifact, secretKey) {
135
+ const { contractInstance: existingInstance } = await this.pxe.getContractMetadata(instance.address);
136
+ if (existingInstance) {
137
+ // Instance already registered in the wallet
138
+ if (artifact) {
139
+ const thisContractClass = await getContractClassFromArtifact(artifact);
140
+ if (!thisContractClass.id.equals(existingInstance.currentContractClassId)) {
141
+ // wallet holds an outdated version of this contract
142
+ await this.pxe.updateContract(instance.address, artifact);
143
+ instance.currentContractClassId = thisContractClass.id;
144
+ }
145
+ }
146
+ // If no artifact provided, we just use the existing registration
147
+ } else {
148
+ // Instance not registered yet
149
+ if (!artifact) {
150
+ // Try to get the artifact from the wallet's contract class storage
151
+ const classMetadata = await this.pxe.getContractClassMetadata(instance.currentContractClassId, true);
152
+ if (!classMetadata.artifact) {
153
+ throw new Error(`Cannot register contract at ${instance.address.toString()}: artifact is required but not provided, and wallet does not have the artifact for contract class ${instance.currentContractClassId.toString()}`);
154
+ }
155
+ artifact = classMetadata.artifact;
156
+ }
157
+ await this.pxe.registerContract({
158
+ artifact,
159
+ instance
160
+ });
161
+ }
162
+ if (secretKey) {
163
+ await this.pxe.registerAccount(secretKey, await computePartialAddress(instance));
164
+ }
165
+ return instance;
166
+ }
167
+ async simulateTx(executionPayload, opts) {
168
+ const feeOptions = opts.fee?.estimateGas ? await this.completeFeeOptionsForEstimation(opts.from, executionPayload.feePayer, opts.fee?.gasSettings) : await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
169
+ const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
170
+ return this.pxe.simulateTx(txRequest, true, opts?.skipTxValidation, opts?.skipFeeEnforcement ?? true);
171
+ }
172
+ async profileTx(executionPayload, opts) {
173
+ const feeOptions = await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
174
+ const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
175
+ return this.pxe.profileTx(txRequest, opts.profileMode, opts.skipProofGeneration ?? true);
176
+ }
177
+ async sendTx(executionPayload, opts) {
178
+ const feeOptions = await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
179
+ const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
180
+ const provenTx = await this.pxe.proveTx(txRequest);
181
+ const tx = await provenTx.toTx();
182
+ const txHash = tx.getTxHash();
183
+ if (await this.aztecNode.getTxEffect(txHash)) {
184
+ throw new Error(`A settled tx with equal hash ${txHash.toString()} exists.`);
185
+ }
186
+ this.log.debug(`Sending transaction ${txHash}`);
187
+ await this.aztecNode.sendTx(tx).catch((err)=>{
188
+ throw this.contextualizeError(err, inspect(tx));
189
+ });
190
+ this.log.info(`Sent transaction ${txHash}`);
191
+ return txHash;
192
+ }
193
+ contextualizeError(err, ...context) {
194
+ let contextStr = '';
195
+ if (context.length > 0) {
196
+ contextStr = `\nContext:\n${context.join('\n')}`;
197
+ }
198
+ if (err instanceof SimulationError) {
199
+ err.setAztecContext(contextStr);
200
+ } else {
201
+ this.log.error(err.name, err);
202
+ this.log.debug(contextStr);
203
+ }
204
+ return err;
205
+ }
206
+ simulateUtility(call, authwits) {
207
+ return this.pxe.simulateUtility(call, authwits);
208
+ }
209
+ getContractClassMetadata(id, includeArtifact = false) {
210
+ return this.pxe.getContractClassMetadata(id, includeArtifact);
211
+ }
212
+ getContractMetadata(address) {
213
+ return this.pxe.getContractMetadata(address);
214
+ }
215
+ getTxReceipt(txHash) {
216
+ return this.aztecNode.getTxReceipt(txHash);
217
+ }
218
+ async getPrivateEvents(contractAddress, eventDef, from, limit, recipients = []) {
219
+ const events = await this.pxe.getPrivateEvents(contractAddress, eventDef.eventSelector, from, limit, recipients);
220
+ const decodedEvents = events.map((event /** PrivateEvent */ )=>decodeFromAbi([
221
+ eventDef.abiType
222
+ ], event.packedEvent));
223
+ return decodedEvents;
224
+ }
225
+ }
@@ -0,0 +1,2 @@
1
+ export { BaseWallet, type FeeOptions } from './base_wallet.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base-wallet/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1 @@
1
+ export { BaseWallet } from './base_wallet.js';
@@ -0,0 +1,8 @@
1
+ export { WalletManager } from './wallet_manager.js';
2
+ export type { WalletManagerConfig, ExtensionWalletConfig, WebWalletConfig, WalletProviderType, WalletProvider, DiscoverWalletsOptions, } from './types.js';
3
+ export type { WalletInfo, WalletMessage, WalletResponse, DiscoveryRequest, DiscoveryResponse, } from '../providers/types.js';
4
+ export { ChainInfoSchema } from '@aztec/aztec.js/account';
5
+ export type { ChainInfo } from '@aztec/aztec.js/account';
6
+ export { WalletSchema } from '@aztec/aztec.js/wallet';
7
+ export { jsonStringify } from '@aztec/foundation/json-rpc';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/manager/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EACV,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAGpB,YAAY,EACV,UAAU,EACV,aAAa,EACb,cAAc,EACd,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,YAAY,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { WalletManager } from './wallet_manager.js';
2
+ // Re-export commonly needed utilities for wallet integration
3
+ export { ChainInfoSchema } from '@aztec/aztec.js/account';
4
+ export { WalletSchema } from '@aztec/aztec.js/wallet';
5
+ export { jsonStringify } from '@aztec/foundation/json-rpc';
@@ -0,0 +1,64 @@
1
+ import type { ChainInfo } from '@aztec/aztec.js/account';
2
+ import type { Wallet } from '@aztec/aztec.js/wallet';
3
+ /**
4
+ * Configuration for extension wallets
5
+ */
6
+ export interface ExtensionWalletConfig {
7
+ /** Whether extension wallets are enabled */
8
+ enabled: boolean;
9
+ /** Optional list of allowed extension IDs (whitelist) */
10
+ allowList?: string[];
11
+ /** Optional list of blocked extension IDs (blacklist) */
12
+ blockList?: string[];
13
+ }
14
+ /**
15
+ * Configuration for web wallets
16
+ */
17
+ export interface WebWalletConfig {
18
+ /** URLs of web wallet services */
19
+ urls: string[];
20
+ }
21
+ /**
22
+ * Configuration for the WalletManager
23
+ */
24
+ export interface WalletManagerConfig {
25
+ /** Extension wallet configuration */
26
+ extensions?: ExtensionWalletConfig;
27
+ /** Web wallet configuration */
28
+ webWallets?: WebWalletConfig;
29
+ }
30
+ /**
31
+ * Type of wallet provider
32
+ */
33
+ export type WalletProviderType = 'extension' | 'web' | 'embedded';
34
+ /**
35
+ * A wallet provider that can connect to create a wallet instance.
36
+ * Chain information is already baked in from the discovery process.
37
+ */
38
+ export interface WalletProvider {
39
+ /** Unique identifier for the provider */
40
+ id: string;
41
+ /** Type of wallet provider */
42
+ type: WalletProviderType;
43
+ /** Display name */
44
+ name: string;
45
+ /** Icon URL */
46
+ icon?: string;
47
+ /** Additional metadata */
48
+ metadata?: Record<string, unknown>;
49
+ /**
50
+ * Connect to this wallet provider with an application ID
51
+ * @param appId - Application identifier for the requesting dapp
52
+ */
53
+ connect(appId: string): Promise<Wallet>;
54
+ }
55
+ /**
56
+ * Options for discovering wallets
57
+ */
58
+ export interface DiscoverWalletsOptions {
59
+ /** Chain information to filter by */
60
+ chainInfo: ChainInfo;
61
+ /** Discovery timeout in milliseconds */
62
+ timeout?: number;
63
+ }
64
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/manager/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,4CAA4C;IAC5C,OAAO,EAAE,OAAO,CAAC;IACjB,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,kCAAkC;IAClC,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,qCAAqC;IACrC,UAAU,CAAC,EAAE,qBAAqB,CAAC;IACnC,+BAA+B;IAC/B,UAAU,CAAC,EAAE,eAAe,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,WAAW,GAAG,KAAK,GAAG,UAAU,CAAC;AAElE;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,yCAAyC;IACzC,EAAE,EAAE,MAAM,CAAC;IACX,8BAA8B;IAC9B,IAAI,EAAE,kBAAkB,CAAC;IACzB,mBAAmB;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,eAAe;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,0BAA0B;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC;;;OAGG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACzC;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,qCAAqC;IACrC,SAAS,EAAE,SAAS,CAAC;IACrB,wCAAwC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
@@ -0,0 +1,3 @@
1
+ /**
2
+ * Options for discovering wallets
3
+ */ export { };
@@ -0,0 +1,27 @@
1
+ import type { DiscoverWalletsOptions, WalletManagerConfig, WalletProvider } from './types.js';
2
+ /**
3
+ * Manager for wallet discovery, configuration, and connection
4
+ */
5
+ export declare class WalletManager {
6
+ private config;
7
+ private constructor();
8
+ /**
9
+ * Configures the WalletManager with provider settings
10
+ * @param config - Configuration options for wallet providers
11
+ */
12
+ static configure(config: WalletManagerConfig): WalletManager;
13
+ /**
14
+ * Discovers all available wallets for a given chain and version.
15
+ * Only returns wallets that support the requested chain and version.
16
+ * @param options - Discovery options including chain info and timeout
17
+ * @returns Array of wallet providers with baked-in chain info
18
+ */
19
+ getAvailableWallets(options: DiscoverWalletsOptions): Promise<WalletProvider[]>;
20
+ /**
21
+ * Checks if an extension is allowed based on allow/block lists
22
+ * @param extensionId - The extension ID to check
23
+ * @param config - Extension wallet configuration containing allow/block lists
24
+ */
25
+ private isExtensionAllowed;
26
+ }
27
+ //# sourceMappingURL=wallet_manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet_manager.d.ts","sourceRoot":"","sources":["../../src/manager/wallet_manager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAyB,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAErH;;GAEG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAGZ;IAEF,OAAO;IAEP;;;OAGG;IACH,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa;IAS5D;;;;;OAKG;IACG,mBAAmB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAiCrF;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;CAc3B"}
@@ -0,0 +1,77 @@
1
+ import { ExtensionProvider, ExtensionWallet } from '../providers/extension/index.js';
2
+ /**
3
+ * Manager for wallet discovery, configuration, and connection
4
+ */ export class WalletManager {
5
+ config = {
6
+ extensions: {
7
+ enabled: true
8
+ },
9
+ webWallets: {
10
+ urls: []
11
+ }
12
+ };
13
+ constructor(){}
14
+ /**
15
+ * Configures the WalletManager with provider settings
16
+ * @param config - Configuration options for wallet providers
17
+ */ static configure(config) {
18
+ const instance = new WalletManager();
19
+ instance.config = {
20
+ extensions: config.extensions ?? {
21
+ enabled: true
22
+ },
23
+ webWallets: config.webWallets ?? {
24
+ urls: []
25
+ }
26
+ };
27
+ return instance;
28
+ }
29
+ /**
30
+ * Discovers all available wallets for a given chain and version.
31
+ * Only returns wallets that support the requested chain and version.
32
+ * @param options - Discovery options including chain info and timeout
33
+ * @returns Array of wallet providers with baked-in chain info
34
+ */ async getAvailableWallets(options) {
35
+ const providers = [];
36
+ const { chainInfo } = options;
37
+ // Discover extension wallets
38
+ if (this.config.extensions?.enabled) {
39
+ const extensions = await ExtensionProvider.discoverExtensions(chainInfo, options.timeout);
40
+ const extensionConfig = this.config.extensions;
41
+ for (const ext of extensions){
42
+ // Apply allow/block lists
43
+ if (!this.isExtensionAllowed(ext.id, extensionConfig)) {
44
+ continue;
45
+ }
46
+ providers.push({
47
+ id: ext.id,
48
+ type: 'extension',
49
+ name: ext.name,
50
+ icon: ext.icon,
51
+ metadata: {
52
+ version: ext.version
53
+ },
54
+ connect: (appId)=>Promise.resolve(ExtensionWallet.create(chainInfo, appId, ext.id))
55
+ });
56
+ }
57
+ }
58
+ // TODO: Add web wallet discovery when implemented
59
+ return providers;
60
+ }
61
+ /**
62
+ * Checks if an extension is allowed based on allow/block lists
63
+ * @param extensionId - The extension ID to check
64
+ * @param config - Extension wallet configuration containing allow/block lists
65
+ */ isExtensionAllowed(extensionId, config) {
66
+ // Check block list first
67
+ if (config.blockList && config.blockList.includes(extensionId)) {
68
+ return false;
69
+ }
70
+ // If allow list exists, extension must be in it
71
+ if (config.allowList && config.allowList.length > 0) {
72
+ return config.allowList.includes(extensionId);
73
+ }
74
+ // If no allow list, extension is allowed (unless blocked)
75
+ return true;
76
+ }
77
+ }
@@ -0,0 +1,17 @@
1
+ import type { ChainInfo } from '@aztec/aztec.js/account';
2
+ import type { WalletInfo } from '../types.js';
3
+ /**
4
+ * Provider for discovering and managing Aztec wallet extensions
5
+ */
6
+ export declare class ExtensionProvider {
7
+ private static discoveredExtensions;
8
+ private static discoveryInProgress;
9
+ /**
10
+ * Discovers all installed Aztec wallet extensions
11
+ * @param chainInfo - Chain information to check if extensions support this network
12
+ * @param timeout - How long to wait for extensions to respond (ms)
13
+ * @returns Array of discovered extension information
14
+ */
15
+ static discoverExtensions(chainInfo: ChainInfo, timeout?: number): Promise<WalletInfo[]>;
16
+ }
17
+ //# sourceMappingURL=extension_provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension_provider.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/extension_provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAIzD,OAAO,KAAK,EAAuC,UAAU,EAAE,MAAM,aAAa,CAAC;AAEnF;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAAsC;IACzE,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAS;IAE3C;;;;;OAKG;WACU,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,GAAE,MAAa,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;CAoDrG"}
@@ -0,0 +1,56 @@
1
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
2
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
3
+ /**
4
+ * Provider for discovering and managing Aztec wallet extensions
5
+ */ export class ExtensionProvider {
6
+ static discoveredExtensions = new Map();
7
+ static discoveryInProgress = false;
8
+ /**
9
+ * Discovers all installed Aztec wallet extensions
10
+ * @param chainInfo - Chain information to check if extensions support this network
11
+ * @param timeout - How long to wait for extensions to respond (ms)
12
+ * @returns Array of discovered extension information
13
+ */ static async discoverExtensions(chainInfo, timeout = 1000) {
14
+ // If discovery is in progress, wait for it to complete
15
+ if (this.discoveryInProgress) {
16
+ await new Promise((resolve)=>setTimeout(resolve, timeout));
17
+ return Array.from(this.discoveredExtensions.values());
18
+ }
19
+ this.discoveryInProgress = true;
20
+ this.discoveredExtensions.clear();
21
+ const { promise, resolve } = promiseWithResolvers();
22
+ const requestId = globalThis.crypto.randomUUID();
23
+ const responses = [];
24
+ // Set up listener for discovery responses
25
+ const handleMessage = (event)=>{
26
+ if (event.source !== window) {
27
+ return;
28
+ }
29
+ let data;
30
+ try {
31
+ data = JSON.parse(event.data);
32
+ } catch {
33
+ return;
34
+ }
35
+ if (data.type === 'aztec-wallet-discovery-response' && data.requestId === requestId) {
36
+ responses.push(data.walletInfo);
37
+ this.discoveredExtensions.set(data.walletInfo.id, data.walletInfo);
38
+ }
39
+ };
40
+ window.addEventListener('message', handleMessage);
41
+ // Send discovery message
42
+ const discoveryMessage = {
43
+ type: 'aztec-wallet-discovery',
44
+ requestId,
45
+ chainInfo
46
+ };
47
+ window.postMessage(jsonStringify(discoveryMessage), '*');
48
+ // Wait for responses
49
+ setTimeout(()=>{
50
+ window.removeEventListener('message', handleMessage);
51
+ this.discoveryInProgress = false;
52
+ resolve(responses);
53
+ }, timeout);
54
+ return promise;
55
+ }
56
+ }
@@ -0,0 +1,23 @@
1
+ import type { ChainInfo } from '@aztec/aztec.js/account';
2
+ import { type Wallet } from '@aztec/aztec.js/wallet';
3
+ /**
4
+ * A wallet implementation that communicates with browser extension wallets
5
+ * Supports multiple extensions by targeting specific extension IDs
6
+ */
7
+ export declare class ExtensionWallet {
8
+ private chainInfo;
9
+ private appId;
10
+ private extensionId;
11
+ private inFlight;
12
+ private constructor();
13
+ /**
14
+ * Creates an ExtensionWallet instance that proxies wallet calls to a browser extension
15
+ * @param chainInfo - The chain information (chainId and version)
16
+ * @param appId - Application identifier for the requesting dapp
17
+ * @param extensionId - Specific extension ID to communicate with
18
+ * @returns A Proxy object that implements the Wallet interface
19
+ */
20
+ static create(chainInfo: ChainInfo, appId: string, extensionId: string): Wallet;
21
+ private postMessage;
22
+ }
23
+ //# sourceMappingURL=extension_wallet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension_wallet.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/extension_wallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,wBAAwB,CAAC;AAsBnE;;;GAGG;AACH,qBAAa,eAAe;IAIxB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,WAAW;IALrB,OAAO,CAAC,QAAQ,CAAoD;IAEpE,OAAO;IAMP;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM;IA+D/E,OAAO,CAAC,WAAW;CAiBpB"}
@@ -0,0 +1,96 @@
1
+ import { WalletSchema } from '@aztec/aztec.js/wallet';
2
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
3
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
4
+ import { schemaHasMethod } from '@aztec/foundation/schemas';
5
+ /**
6
+ * A wallet implementation that communicates with browser extension wallets
7
+ * Supports multiple extensions by targeting specific extension IDs
8
+ */ export class ExtensionWallet {
9
+ chainInfo;
10
+ appId;
11
+ extensionId;
12
+ inFlight;
13
+ constructor(chainInfo, appId, extensionId){
14
+ this.chainInfo = chainInfo;
15
+ this.appId = appId;
16
+ this.extensionId = extensionId;
17
+ this.inFlight = new Map();
18
+ }
19
+ /**
20
+ * Creates an ExtensionWallet instance that proxies wallet calls to a browser extension
21
+ * @param chainInfo - The chain information (chainId and version)
22
+ * @param appId - Application identifier for the requesting dapp
23
+ * @param extensionId - Specific extension ID to communicate with
24
+ * @returns A Proxy object that implements the Wallet interface
25
+ */ static create(chainInfo, appId, extensionId) {
26
+ const wallet = new ExtensionWallet(chainInfo, appId, extensionId);
27
+ // Set up message listener for responses from extensions
28
+ window.addEventListener('message', (event)=>{
29
+ if (event.source !== window) {
30
+ return;
31
+ }
32
+ let data;
33
+ try {
34
+ data = JSON.parse(event.data);
35
+ } catch {
36
+ return;
37
+ }
38
+ // Ignore request messages (only process responses)
39
+ if ('type' in data) {
40
+ return;
41
+ }
42
+ const { messageId, result, error, walletId: responseWalletId } = data;
43
+ if (!messageId || !responseWalletId) {
44
+ return;
45
+ }
46
+ if (wallet.extensionId !== responseWalletId) {
47
+ return;
48
+ }
49
+ if (!wallet.inFlight.has(messageId)) {
50
+ return;
51
+ }
52
+ const { resolve, reject } = wallet.inFlight.get(messageId);
53
+ if (error) {
54
+ reject(new Error(jsonStringify(error)));
55
+ } else {
56
+ resolve(result);
57
+ }
58
+ wallet.inFlight.delete(messageId);
59
+ });
60
+ // Create a Proxy that intercepts wallet method calls and forwards them to the extension
61
+ return new Proxy(wallet, {
62
+ get: (target, prop)=>{
63
+ if (schemaHasMethod(WalletSchema, prop.toString())) {
64
+ return async (...args)=>{
65
+ const result = await target.postMessage({
66
+ type: prop.toString(),
67
+ args
68
+ });
69
+ return WalletSchema[prop.toString()].returnType().parseAsync(result);
70
+ };
71
+ } else {
72
+ return target[prop];
73
+ }
74
+ }
75
+ });
76
+ }
77
+ postMessage(call) {
78
+ const messageId = globalThis.crypto.randomUUID();
79
+ const message = {
80
+ type: call.type,
81
+ args: call.args,
82
+ messageId,
83
+ chainInfo: this.chainInfo,
84
+ appId: this.appId,
85
+ walletId: this.extensionId
86
+ };
87
+ window.postMessage(jsonStringify(message), '*');
88
+ const { promise, resolve, reject } = promiseWithResolvers();
89
+ this.inFlight.set(messageId, {
90
+ promise,
91
+ resolve,
92
+ reject
93
+ });
94
+ return promise;
95
+ }
96
+ }
@@ -0,0 +1,4 @@
1
+ export { ExtensionWallet } from './extension_wallet.js';
2
+ export { ExtensionProvider } from './extension_provider.js';
3
+ export type { WalletInfo, WalletMessage, WalletResponse, DiscoveryRequest, DiscoveryResponse } from '../types.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { ExtensionWallet } from './extension_wallet.js';
2
+ export { ExtensionProvider } from './extension_provider.js';