@aztec/wallet-sdk 0.0.1-commit.21caa21 → 0.0.1-commit.2ed92850
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +228 -331
- package/dest/base-wallet/base_wallet.d.ts +23 -13
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +59 -24
- package/dest/crypto.d.ts +192 -0
- package/dest/crypto.d.ts.map +1 -0
- package/dest/crypto.js +394 -0
- package/dest/emoji_alphabet.d.ts +35 -0
- package/dest/emoji_alphabet.d.ts.map +1 -0
- package/dest/emoji_alphabet.js +299 -0
- package/dest/extension/handlers/background_connection_handler.d.ts +158 -0
- package/dest/extension/handlers/background_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/background_connection_handler.js +258 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts +56 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/content_script_connection_handler.js +174 -0
- package/dest/extension/handlers/index.d.ts +12 -0
- package/dest/extension/handlers/index.d.ts.map +1 -0
- package/dest/extension/handlers/index.js +10 -0
- package/dest/extension/handlers/internal_message_types.d.ts +63 -0
- package/dest/extension/handlers/internal_message_types.d.ts.map +1 -0
- package/dest/extension/handlers/internal_message_types.js +22 -0
- package/dest/extension/provider/extension_provider.d.ts +107 -0
- package/dest/extension/provider/extension_provider.d.ts.map +1 -0
- package/dest/extension/provider/extension_provider.js +160 -0
- package/dest/extension/provider/extension_wallet.d.ts +131 -0
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -0
- package/dest/extension/provider/extension_wallet.js +271 -0
- package/dest/extension/provider/index.d.ts +3 -0
- package/dest/extension/provider/index.d.ts.map +1 -0
- package/dest/manager/index.d.ts +2 -7
- package/dest/manager/index.d.ts.map +1 -1
- package/dest/manager/index.js +0 -4
- package/dest/manager/types.d.ts +108 -5
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/types.js +17 -1
- package/dest/manager/wallet_manager.d.ts +50 -7
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +178 -29
- package/dest/types.d.ts +123 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +11 -0
- package/package.json +15 -12
- package/src/base-wallet/base_wallet.ts +82 -41
- package/src/crypto.ts +499 -0
- package/src/emoji_alphabet.ts +317 -0
- package/src/extension/handlers/background_connection_handler.ts +423 -0
- package/src/extension/handlers/content_script_connection_handler.ts +246 -0
- package/src/extension/handlers/index.ts +25 -0
- package/src/extension/handlers/internal_message_types.ts +69 -0
- package/src/extension/provider/extension_provider.ts +233 -0
- package/src/extension/provider/extension_wallet.ts +321 -0
- package/src/extension/provider/index.ts +7 -0
- package/src/manager/index.ts +3 -15
- package/src/manager/types.ts +112 -4
- package/src/manager/wallet_manager.ts +204 -31
- package/src/types.ts +132 -0
- package/dest/providers/extension/extension_provider.d.ts +0 -17
- package/dest/providers/extension/extension_provider.d.ts.map +0 -1
- package/dest/providers/extension/extension_provider.js +0 -56
- package/dest/providers/extension/extension_wallet.d.ts +0 -23
- package/dest/providers/extension/extension_wallet.d.ts.map +0 -1
- package/dest/providers/extension/extension_wallet.js +0 -96
- package/dest/providers/extension/index.d.ts +0 -4
- package/dest/providers/extension/index.d.ts.map +0 -1
- package/dest/providers/types.d.ts +0 -67
- package/dest/providers/types.d.ts.map +0 -1
- package/dest/providers/types.js +0 -3
- package/src/providers/extension/extension_provider.ts +0 -72
- package/src/providers/extension/extension_wallet.ts +0 -124
- package/src/providers/extension/index.ts +0 -3
- package/src/providers/types.ts +0 -71
- /package/dest/{providers/extension → extension/provider}/index.js +0 -0
package/src/manager/types.ts
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
2
|
import type { Wallet } from '@aztec/aztec.js/wallet';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* A pending connection that requires user verification before finalizing.
|
|
6
|
+
*
|
|
7
|
+
* After key exchange, both dApp and wallet compute a verification hash independently.
|
|
8
|
+
* The dApp should display this hash (typically as emojis) and let the user confirm
|
|
9
|
+
* it matches what's shown in their wallet before calling `confirm()`.
|
|
10
|
+
*
|
|
11
|
+
* This protects the dApp from connecting to a malicious wallet that might be
|
|
12
|
+
* intercepting the connection (MITM attack).
|
|
13
|
+
*/
|
|
14
|
+
export interface PendingConnection {
|
|
15
|
+
/**
|
|
16
|
+
* The verification hash computed from the shared secret.
|
|
17
|
+
* Use `hashToEmoji()` to convert to a visual representation for user verification.
|
|
18
|
+
* The user should confirm this matches what their wallet displays.
|
|
19
|
+
*/
|
|
20
|
+
verificationHash: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Confirms the connection after user verifies the emojis match.
|
|
24
|
+
* @returns The connected wallet instance
|
|
25
|
+
*/
|
|
26
|
+
confirm(): Promise<Wallet>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Cancels the pending connection.
|
|
30
|
+
* Call this if the user indicates the emojis don't match.
|
|
31
|
+
*/
|
|
32
|
+
cancel(): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
4
35
|
/**
|
|
5
36
|
* Configuration for extension wallets
|
|
6
37
|
*/
|
|
@@ -34,7 +65,12 @@ export interface WalletManagerConfig {
|
|
|
34
65
|
/**
|
|
35
66
|
* Type of wallet provider
|
|
36
67
|
*/
|
|
37
|
-
export type WalletProviderType = 'extension' | 'web'
|
|
68
|
+
export type WalletProviderType = 'extension' | 'web';
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Callback type for wallet disconnect events at the provider level.
|
|
72
|
+
*/
|
|
73
|
+
export type ProviderDisconnectionCallback = () => void;
|
|
38
74
|
|
|
39
75
|
/**
|
|
40
76
|
* A wallet provider that can connect to create a wallet instance.
|
|
@@ -52,10 +88,46 @@ export interface WalletProvider {
|
|
|
52
88
|
/** Additional metadata */
|
|
53
89
|
metadata?: Record<string, unknown>;
|
|
54
90
|
/**
|
|
55
|
-
*
|
|
91
|
+
* Establishes a secure channel with this wallet provider.
|
|
92
|
+
*
|
|
93
|
+
* This performs the ECDH key exchange and returns a pending connection with the
|
|
94
|
+
* verification hash. The channel is encrypted but NOT yet verified - the dApp
|
|
95
|
+
* should display the hash (as emojis) to the user and let them confirm it
|
|
96
|
+
* matches their wallet before calling `confirm()`.
|
|
97
|
+
*
|
|
56
98
|
* @param appId - Application identifier for the requesting dapp
|
|
99
|
+
* @returns A pending connection with verification hash and confirm/cancel methods
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const pending = await provider.establishSecureChannel('my-app');
|
|
104
|
+
*
|
|
105
|
+
* // Show emojis to user for verification
|
|
106
|
+
* const emojis = hashToEmoji(pending.verificationHash);
|
|
107
|
+
* showDialog(`Verify these match your wallet: ${emojis}`);
|
|
108
|
+
*
|
|
109
|
+
* // User confirms emojis match
|
|
110
|
+
* const wallet = await pending.confirm();
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
establishSecureChannel(appId: string): Promise<PendingConnection>;
|
|
114
|
+
/**
|
|
115
|
+
* Disconnects the current wallet and cleans up resources.
|
|
116
|
+
* After calling this, the wallet returned from confirm() should no longer be used.
|
|
117
|
+
* @returns A promise that resolves when disconnection is complete
|
|
118
|
+
*/
|
|
119
|
+
disconnect?(): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* Registers a callback to be invoked when the wallet disconnects unexpectedly.
|
|
122
|
+
* @param callback - Function to call when wallet disconnects
|
|
123
|
+
* @returns A function to unregister the callback
|
|
124
|
+
*/
|
|
125
|
+
onDisconnect?(callback: ProviderDisconnectionCallback): () => void;
|
|
126
|
+
/**
|
|
127
|
+
* Returns whether the provider's wallet connection has been disconnected.
|
|
128
|
+
* @returns true if the wallet is no longer connected
|
|
57
129
|
*/
|
|
58
|
-
|
|
130
|
+
isDisconnected?(): boolean;
|
|
59
131
|
}
|
|
60
132
|
|
|
61
133
|
/**
|
|
@@ -64,6 +136,42 @@ export interface WalletProvider {
|
|
|
64
136
|
export interface DiscoverWalletsOptions {
|
|
65
137
|
/** Chain information to filter by */
|
|
66
138
|
chainInfo: ChainInfo;
|
|
67
|
-
/**
|
|
139
|
+
/** Application ID making the request */
|
|
140
|
+
appId: string;
|
|
141
|
+
/** Discovery timeout in milliseconds. Default: 60000 (60s) */
|
|
68
142
|
timeout?: number;
|
|
143
|
+
/**
|
|
144
|
+
* Callback invoked when a wallet provider is discovered.
|
|
145
|
+
* Use this to show wallets to users as they approve them, rather than
|
|
146
|
+
* waiting for the full timeout.
|
|
147
|
+
*/
|
|
148
|
+
onWalletDiscovered?: (provider: WalletProvider) => void;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* A cancellable discovery session.
|
|
153
|
+
*
|
|
154
|
+
* Returned by `WalletManager.getAvailableWallets()` to allow consumers to
|
|
155
|
+
* cancel discovery when no longer needed (e.g., network changes, user navigates away).
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const discovery = WalletManager.configure({...}).getAvailableWallets({...});
|
|
160
|
+
*
|
|
161
|
+
* // Iterate over discovered wallets
|
|
162
|
+
* for await (const wallet of discovery.wallets) {
|
|
163
|
+
* console.log(`Found: ${wallet.name}`);
|
|
164
|
+
* }
|
|
165
|
+
*
|
|
166
|
+
* // Cancel discovery when no longer needed
|
|
167
|
+
* discovery.cancel();
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export interface DiscoverySession {
|
|
171
|
+
/** Async iterator that yields wallet providers as they're discovered */
|
|
172
|
+
wallets: AsyncIterable<WalletProvider>;
|
|
173
|
+
/** Promise that resolves when discovery completes or is cancelled */
|
|
174
|
+
done: Promise<void>;
|
|
175
|
+
/** Cancel discovery immediately and clean up resources */
|
|
176
|
+
cancel: () => void;
|
|
69
177
|
}
|
|
@@ -1,8 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
|
+
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
3
|
+
|
|
4
|
+
import { type DiscoveredWallet, ExtensionProvider, ExtensionWallet } from '../extension/provider/index.js';
|
|
5
|
+
import { WalletMessageType } from '../types.js';
|
|
6
|
+
import type {
|
|
7
|
+
DiscoverWalletsOptions,
|
|
8
|
+
DiscoverySession,
|
|
9
|
+
ExtensionWalletConfig,
|
|
10
|
+
PendingConnection,
|
|
11
|
+
ProviderDisconnectionCallback,
|
|
12
|
+
WalletManagerConfig,
|
|
13
|
+
WalletProvider,
|
|
14
|
+
} from './types.js';
|
|
3
15
|
|
|
4
16
|
/**
|
|
5
|
-
* Manager for wallet discovery, configuration, and connection
|
|
17
|
+
* Manager for wallet discovery, configuration, and connection.
|
|
18
|
+
*
|
|
19
|
+
* This is the main entry point for dApps to discover and connect to wallets.
|
|
20
|
+
*
|
|
21
|
+
* @example Basic usage with async iterator
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const discovery = WalletManager.configure({ extensions: { enabled: true } })
|
|
24
|
+
* .getAvailableWallets({ chainInfo, appId: 'my-app' });
|
|
25
|
+
*
|
|
26
|
+
* // Iterate over discovered wallets
|
|
27
|
+
* for await (const provider of discovery.wallets) {
|
|
28
|
+
* console.log(`Found wallet: ${provider.name}`);
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* // Or cancel early when done
|
|
32
|
+
* discovery.cancel();
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example With callback for discovered wallets
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const discovery = manager.getAvailableWallets({
|
|
38
|
+
* chainInfo,
|
|
39
|
+
* appId: 'my-app',
|
|
40
|
+
* onWalletDiscovered: (provider) => console.log(`Found: ${provider.name}`),
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Wait for discovery to complete or cancel it
|
|
44
|
+
* await discovery.done;
|
|
45
|
+
* ```
|
|
6
46
|
*/
|
|
7
47
|
export class WalletManager {
|
|
8
48
|
private config: WalletManagerConfig = {
|
|
@@ -27,41 +67,177 @@ export class WalletManager {
|
|
|
27
67
|
|
|
28
68
|
/**
|
|
29
69
|
* Discovers all available wallets for a given chain and version.
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
70
|
+
*
|
|
71
|
+
* Returns a `DiscoverySession` with:
|
|
72
|
+
* - `wallets`: AsyncIterable to iterate over discovered wallets
|
|
73
|
+
* - `done`: Promise that resolves when discovery completes or is cancelled
|
|
74
|
+
* - `cancel()`: Function to stop discovery immediately
|
|
75
|
+
*
|
|
76
|
+
* If `onWalletDiscovered` callback is provided, wallets are also streamed via callback.
|
|
77
|
+
*
|
|
78
|
+
* @param options - Discovery options including chain info, appId, and timeout
|
|
79
|
+
* @returns A cancellable discovery session
|
|
33
80
|
*/
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
const
|
|
81
|
+
getAvailableWallets(options: DiscoverWalletsOptions): DiscoverySession {
|
|
82
|
+
const { chainInfo, appId } = options;
|
|
83
|
+
const abortController = new AbortController();
|
|
84
|
+
|
|
85
|
+
const pendingProviders: WalletProvider[] = [];
|
|
86
|
+
let pendingResolve: ((result: IteratorResult<WalletProvider>) => void) | null = null;
|
|
87
|
+
let completed = false;
|
|
88
|
+
|
|
89
|
+
const { promise: donePromise, resolve: resolveDone } = promiseWithResolvers<void>();
|
|
90
|
+
|
|
91
|
+
const markComplete = () => {
|
|
92
|
+
completed = true;
|
|
93
|
+
resolveDone();
|
|
94
|
+
if (pendingResolve) {
|
|
95
|
+
const resolve = pendingResolve;
|
|
96
|
+
pendingResolve = null;
|
|
97
|
+
resolve({ value: undefined, done: true });
|
|
98
|
+
}
|
|
99
|
+
};
|
|
37
100
|
|
|
38
|
-
// Discover extension wallets
|
|
39
101
|
if (this.config.extensions?.enabled) {
|
|
40
|
-
const extensions = await ExtensionProvider.discoverExtensions(chainInfo, options.timeout);
|
|
41
102
|
const extensionConfig = this.config.extensions;
|
|
42
103
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
104
|
+
void ExtensionProvider.discoverWallets(chainInfo, {
|
|
105
|
+
appId,
|
|
106
|
+
timeout: options.timeout,
|
|
107
|
+
signal: abortController.signal,
|
|
108
|
+
onWalletDiscovered: discoveredWallet => {
|
|
109
|
+
const provider = this.createProviderFromDiscoveredWallet(discoveredWallet, chainInfo, extensionConfig);
|
|
110
|
+
if (!provider) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Call user's callback if provided
|
|
115
|
+
options.onWalletDiscovered?.(provider);
|
|
48
116
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
117
|
+
// Also queue for async iterator
|
|
118
|
+
if (pendingResolve) {
|
|
119
|
+
const resolve = pendingResolve;
|
|
120
|
+
pendingResolve = null;
|
|
121
|
+
resolve({ value: provider, done: false });
|
|
122
|
+
} else {
|
|
123
|
+
pendingProviders.push(provider);
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
}).then(markComplete);
|
|
127
|
+
} else {
|
|
128
|
+
markComplete();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const wallets: AsyncIterable<WalletProvider> = {
|
|
132
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
133
|
+
[Symbol.asyncIterator](): AsyncIterator<WalletProvider> {
|
|
134
|
+
return {
|
|
135
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
136
|
+
next(): Promise<IteratorResult<WalletProvider>> {
|
|
137
|
+
if (pendingProviders.length > 0) {
|
|
138
|
+
return Promise.resolve({ value: pendingProviders.shift()!, done: false });
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (completed) {
|
|
142
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return new Promise(resolve => {
|
|
146
|
+
pendingResolve = resolve;
|
|
147
|
+
});
|
|
56
148
|
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
149
|
+
};
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
wallets,
|
|
155
|
+
done: donePromise,
|
|
156
|
+
cancel: () => abortController.abort(),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Creates a WalletProvider from a discovered wallet.
|
|
162
|
+
* Returns null if the wallet is not allowed by config.
|
|
163
|
+
* @param discoveredWallet - The discovered wallet from extension discovery.
|
|
164
|
+
* @param chainInfo - Network information.
|
|
165
|
+
* @param extensionConfig - Extension wallet configuration.
|
|
166
|
+
*/
|
|
167
|
+
private createProviderFromDiscoveredWallet(
|
|
168
|
+
discoveredWallet: DiscoveredWallet,
|
|
169
|
+
chainInfo: ChainInfo,
|
|
170
|
+
extensionConfig: ExtensionWalletConfig,
|
|
171
|
+
): WalletProvider | null {
|
|
172
|
+
const { info } = discoveredWallet;
|
|
173
|
+
|
|
174
|
+
if (!this.isExtensionAllowed(info.id, extensionConfig)) {
|
|
175
|
+
return null;
|
|
60
176
|
}
|
|
61
177
|
|
|
62
|
-
|
|
178
|
+
let extensionWallet: ExtensionWallet | null = null;
|
|
179
|
+
|
|
180
|
+
const provider: WalletProvider = {
|
|
181
|
+
id: info.id,
|
|
182
|
+
type: 'extension',
|
|
183
|
+
name: info.name,
|
|
184
|
+
icon: info.icon,
|
|
185
|
+
metadata: {
|
|
186
|
+
version: info.version,
|
|
187
|
+
},
|
|
188
|
+
establishSecureChannel: async (connectAppId: string): Promise<PendingConnection> => {
|
|
189
|
+
const connection = await discoveredWallet.establishSecureChannel();
|
|
190
|
+
|
|
191
|
+
provider.metadata = {
|
|
192
|
+
...provider.metadata,
|
|
193
|
+
verificationHash: connection.info.verificationHash,
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
verificationHash: connection.info.verificationHash!,
|
|
198
|
+
confirm: () => {
|
|
199
|
+
return Promise.resolve(
|
|
200
|
+
ExtensionWallet.create(
|
|
201
|
+
connection.info.id,
|
|
202
|
+
connection.port,
|
|
203
|
+
connection.sharedKey,
|
|
204
|
+
chainInfo,
|
|
205
|
+
connectAppId,
|
|
206
|
+
),
|
|
207
|
+
);
|
|
208
|
+
},
|
|
209
|
+
cancel: () => {
|
|
210
|
+
// Send disconnect to terminate the session on the extension side
|
|
211
|
+
// but keep the port open so we can retry key exchange
|
|
212
|
+
connection.port.postMessage({
|
|
213
|
+
type: WalletMessageType.DISCONNECT,
|
|
214
|
+
requestId: discoveredWallet.requestId,
|
|
215
|
+
});
|
|
216
|
+
// Don't close the port - allow retry with fresh key exchange
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
disconnect: async () => {
|
|
221
|
+
if (extensionWallet) {
|
|
222
|
+
await extensionWallet.disconnect();
|
|
223
|
+
extensionWallet = null;
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
onDisconnect: (callback: ProviderDisconnectionCallback) => {
|
|
227
|
+
if (extensionWallet) {
|
|
228
|
+
return extensionWallet.onDisconnect(callback);
|
|
229
|
+
}
|
|
230
|
+
return () => {};
|
|
231
|
+
},
|
|
232
|
+
isDisconnected: () => {
|
|
233
|
+
if (extensionWallet) {
|
|
234
|
+
return extensionWallet.isDisconnected();
|
|
235
|
+
}
|
|
236
|
+
return true;
|
|
237
|
+
},
|
|
238
|
+
};
|
|
63
239
|
|
|
64
|
-
return
|
|
240
|
+
return provider;
|
|
65
241
|
}
|
|
66
242
|
|
|
67
243
|
/**
|
|
@@ -70,17 +246,14 @@ export class WalletManager {
|
|
|
70
246
|
* @param config - Extension wallet configuration containing allow/block lists
|
|
71
247
|
*/
|
|
72
248
|
private isExtensionAllowed(extensionId: string, config: ExtensionWalletConfig): boolean {
|
|
73
|
-
// Check block list first
|
|
74
249
|
if (config.blockList && config.blockList.includes(extensionId)) {
|
|
75
250
|
return false;
|
|
76
251
|
}
|
|
77
252
|
|
|
78
|
-
// If allow list exists, extension must be in it
|
|
79
253
|
if (config.allowList && config.allowList.length > 0) {
|
|
80
254
|
return config.allowList.includes(extensionId);
|
|
81
255
|
}
|
|
82
256
|
|
|
83
|
-
// If no allow list, extension is allowed (unless blocked)
|
|
84
257
|
return true;
|
|
85
258
|
}
|
|
86
259
|
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
|
+
|
|
3
|
+
import type { ExportedPublicKey } from './crypto.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Message types for wallet SDK communication.
|
|
7
|
+
* All types are prefixed with 'aztec-wallet-' for namespacing.
|
|
8
|
+
*/
|
|
9
|
+
export enum WalletMessageType {
|
|
10
|
+
/** Discovery request to find installed wallets */
|
|
11
|
+
DISCOVERY = 'aztec-wallet-discovery',
|
|
12
|
+
/** Discovery response from a wallet */
|
|
13
|
+
DISCOVERY_RESPONSE = 'aztec-wallet-discovery-response',
|
|
14
|
+
/** Disconnect message (unencrypted control message, bidirectional) */
|
|
15
|
+
DISCONNECT = 'aztec-wallet-disconnect',
|
|
16
|
+
/** Key exchange request sent over MessageChannel */
|
|
17
|
+
KEY_EXCHANGE_REQUEST = 'aztec-wallet-key-exchange-request',
|
|
18
|
+
/** Key exchange response sent over MessageChannel */
|
|
19
|
+
KEY_EXCHANGE_RESPONSE = 'aztec-wallet-key-exchange-response',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Information about an installed Aztec wallet.
|
|
24
|
+
* Used during discovery phase before key exchange.
|
|
25
|
+
*/
|
|
26
|
+
export interface WalletInfo {
|
|
27
|
+
/** Unique identifier for the wallet */
|
|
28
|
+
id: string;
|
|
29
|
+
/** Display name of the wallet */
|
|
30
|
+
name: string;
|
|
31
|
+
/** URL to the wallet's icon */
|
|
32
|
+
icon?: string;
|
|
33
|
+
/** Wallet version */
|
|
34
|
+
version: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Full information about a connected Aztec wallet including crypto material.
|
|
39
|
+
* Available after key exchange completes.
|
|
40
|
+
*/
|
|
41
|
+
export interface ConnectedWalletInfo extends WalletInfo {
|
|
42
|
+
/** Wallet's ECDH public key for secure channel establishment */
|
|
43
|
+
publicKey: ExportedPublicKey;
|
|
44
|
+
/**
|
|
45
|
+
* Verification hash for verification.
|
|
46
|
+
* Both dApp and wallet independently compute this from the ECDH shared secret.
|
|
47
|
+
* Use {@link hashToEmoji} to convert to a visual representation for user verification.
|
|
48
|
+
*/
|
|
49
|
+
verificationHash?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Message format for wallet communication (internal, before encryption)
|
|
54
|
+
*/
|
|
55
|
+
export interface WalletMessage {
|
|
56
|
+
/** Unique message ID for tracking responses */
|
|
57
|
+
messageId: string;
|
|
58
|
+
/** The wallet method to call */
|
|
59
|
+
type: string;
|
|
60
|
+
/** Arguments for the method */
|
|
61
|
+
args: unknown[];
|
|
62
|
+
/** Chain information */
|
|
63
|
+
chainInfo: ChainInfo;
|
|
64
|
+
/** Application ID making the request */
|
|
65
|
+
appId: string;
|
|
66
|
+
/** Wallet ID to target a specific wallet */
|
|
67
|
+
walletId: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Response message from wallet
|
|
72
|
+
*/
|
|
73
|
+
export interface WalletResponse {
|
|
74
|
+
/** Message ID matching the request */
|
|
75
|
+
messageId: string;
|
|
76
|
+
/** Result data (if successful) */
|
|
77
|
+
result?: unknown;
|
|
78
|
+
/** Error data (if failed) */
|
|
79
|
+
error?: unknown;
|
|
80
|
+
/** Wallet ID that sent the response */
|
|
81
|
+
walletId: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Discovery message for finding installed wallets (public, unencrypted).
|
|
86
|
+
*/
|
|
87
|
+
export interface DiscoveryRequest {
|
|
88
|
+
/** Message type for discovery */
|
|
89
|
+
type: WalletMessageType.DISCOVERY;
|
|
90
|
+
/** Request ID */
|
|
91
|
+
requestId: string;
|
|
92
|
+
/** Application ID making the request */
|
|
93
|
+
appId: string;
|
|
94
|
+
/** Chain information to check if wallet supports this network */
|
|
95
|
+
chainInfo: ChainInfo;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Discovery response from a wallet (public, unencrypted).
|
|
100
|
+
*/
|
|
101
|
+
export interface DiscoveryResponse {
|
|
102
|
+
/** Message type for discovery response */
|
|
103
|
+
type: WalletMessageType.DISCOVERY_RESPONSE;
|
|
104
|
+
/** Request ID matching the discovery request */
|
|
105
|
+
requestId: string;
|
|
106
|
+
/** Basic wallet information */
|
|
107
|
+
walletInfo: WalletInfo;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Key exchange request sent over MessageChannel after discovery approval.
|
|
112
|
+
*/
|
|
113
|
+
export interface KeyExchangeRequest {
|
|
114
|
+
/** Message type */
|
|
115
|
+
type: WalletMessageType.KEY_EXCHANGE_REQUEST;
|
|
116
|
+
/** Request ID matching the discovery request */
|
|
117
|
+
requestId: string;
|
|
118
|
+
/** dApp's ECDH public key for deriving shared secret */
|
|
119
|
+
publicKey: ExportedPublicKey;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Key exchange response sent over MessageChannel.
|
|
124
|
+
*/
|
|
125
|
+
export interface KeyExchangeResponse {
|
|
126
|
+
/** Message type */
|
|
127
|
+
type: WalletMessageType.KEY_EXCHANGE_RESPONSE;
|
|
128
|
+
/** Request ID matching the discovery request */
|
|
129
|
+
requestId: string;
|
|
130
|
+
/** Wallet's ECDH public key for deriving shared secret */
|
|
131
|
+
publicKey: ExportedPublicKey;
|
|
132
|
+
}
|
|
@@ -1,17 +0,0 @@
|
|
|
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=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3Byb3ZpZGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvcHJvdmlkZXJzL2V4dGVuc2lvbi9leHRlbnNpb25fcHJvdmlkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsU0FBUyxFQUFFLE1BQU0seUJBQXlCLENBQUM7QUFJekQsT0FBTyxLQUFLLEVBQXVDLFVBQVUsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUVuRjs7R0FFRztBQUNILHFCQUFhLGlCQUFpQjtJQUM1QixPQUFPLENBQUMsTUFBTSxDQUFDLG9CQUFvQixDQUFzQztJQUN6RSxPQUFPLENBQUMsTUFBTSxDQUFDLG1CQUFtQixDQUFTO0lBRTNDOzs7OztPQUtHO0lBQ0gsT0FBYSxrQkFBa0IsQ0FBQyxTQUFTLEVBQUUsU0FBUyxFQUFFLE9BQU8sR0FBRSxNQUFhLEdBQUcsT0FBTyxDQUFDLFVBQVUsRUFBRSxDQUFDLENBbURuRztDQUNGIn0=
|
|
@@ -1 +0,0 @@
|
|
|
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;IACH,OAAa,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,GAAE,MAAa,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAmDnG;CACF"}
|
|
@@ -1,56 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
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=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3dhbGxldC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL3Byb3ZpZGVycy9leHRlbnNpb24vZXh0ZW5zaW9uX3dhbGxldC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUN6RCxPQUFPLEVBQUUsS0FBSyxNQUFNLEVBQWdCLE1BQU0sd0JBQXdCLENBQUM7QUFzQm5FOzs7R0FHRztBQUNILHFCQUFhLGVBQWU7SUFJeEIsT0FBTyxDQUFDLFNBQVM7SUFDakIsT0FBTyxDQUFDLEtBQUs7SUFDYixPQUFPLENBQUMsV0FBVztJQUxyQixPQUFPLENBQUMsUUFBUSxDQUFvRDtJQUVwRSxPQUFPLGVBSUg7SUFFSjs7Ozs7O09BTUc7SUFDSCxNQUFNLENBQUMsTUFBTSxDQUFDLFNBQVMsRUFBRSxTQUFTLEVBQUUsS0FBSyxFQUFFLE1BQU0sRUFBRSxXQUFXLEVBQUUsTUFBTSxHQUFHLE1BQU0sQ0E2RDlFO0lBRUQsT0FBTyxDQUFDLFdBQVc7Q0FpQnBCIn0=
|
|
@@ -1 +0,0 @@
|
|
|
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,eAIH;IAEJ;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CA6D9E;IAED,OAAO,CAAC,WAAW;CAiBpB"}
|