@aztec/wallet-sdk 0.0.1-commit.d1f2d6c → 0.0.1-commit.d431d1c
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 +294 -217
- package/dest/base-wallet/base_wallet.d.ts +4 -4
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +4 -9
- package/dest/crypto.d.ts +50 -59
- package/dest/crypto.d.ts.map +1 -1
- package/dest/crypto.js +108 -202
- package/dest/manager/index.d.ts +8 -2
- package/dest/manager/index.d.ts.map +1 -1
- package/dest/manager/index.js +6 -0
- package/dest/manager/types.d.ts +6 -88
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/types.js +1 -17
- package/dest/manager/wallet_manager.d.ts +7 -50
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +44 -174
- package/dest/providers/extension/extension_provider.d.ts +63 -0
- package/dest/providers/extension/extension_provider.d.ts.map +1 -0
- package/dest/providers/extension/extension_provider.js +124 -0
- package/dest/providers/extension/extension_wallet.d.ts +155 -0
- package/dest/providers/extension/extension_wallet.d.ts.map +1 -0
- package/dest/{extension/provider → providers/extension}/extension_wallet.js +95 -48
- package/dest/providers/extension/index.d.ts +6 -0
- package/dest/providers/extension/index.d.ts.map +1 -0
- package/dest/{extension/provider → providers/extension}/index.js +2 -0
- package/dest/types.d.ts +12 -43
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +2 -3
- package/package.json +9 -10
- package/src/base-wallet/base_wallet.ts +8 -15
- package/src/crypto.ts +113 -237
- package/src/manager/index.ts +10 -2
- package/src/manager/types.ts +5 -91
- package/src/manager/wallet_manager.ts +46 -192
- package/src/providers/extension/extension_provider.ts +167 -0
- package/src/{extension/provider → providers/extension}/extension_wallet.ts +110 -52
- package/src/providers/extension/index.ts +5 -0
- package/src/types.ts +10 -44
- package/dest/emoji_alphabet.d.ts +0 -35
- package/dest/emoji_alphabet.d.ts.map +0 -1
- package/dest/emoji_alphabet.js +0 -299
- package/dest/extension/handlers/background_connection_handler.d.ts +0 -158
- package/dest/extension/handlers/background_connection_handler.d.ts.map +0 -1
- package/dest/extension/handlers/background_connection_handler.js +0 -258
- package/dest/extension/handlers/content_script_connection_handler.d.ts +0 -56
- package/dest/extension/handlers/content_script_connection_handler.d.ts.map +0 -1
- package/dest/extension/handlers/content_script_connection_handler.js +0 -174
- package/dest/extension/handlers/index.d.ts +0 -12
- package/dest/extension/handlers/index.d.ts.map +0 -1
- package/dest/extension/handlers/index.js +0 -10
- package/dest/extension/handlers/internal_message_types.d.ts +0 -63
- package/dest/extension/handlers/internal_message_types.d.ts.map +0 -1
- package/dest/extension/handlers/internal_message_types.js +0 -22
- package/dest/extension/provider/extension_provider.d.ts +0 -107
- package/dest/extension/provider/extension_provider.d.ts.map +0 -1
- package/dest/extension/provider/extension_provider.js +0 -160
- package/dest/extension/provider/extension_wallet.d.ts +0 -131
- package/dest/extension/provider/extension_wallet.d.ts.map +0 -1
- package/dest/extension/provider/index.d.ts +0 -3
- package/dest/extension/provider/index.d.ts.map +0 -1
- package/src/emoji_alphabet.ts +0 -317
- package/src/extension/handlers/background_connection_handler.ts +0 -423
- package/src/extension/handlers/content_script_connection_handler.ts +0 -246
- package/src/extension/handlers/index.ts +0 -25
- package/src/extension/handlers/internal_message_types.ts +0 -69
- package/src/extension/provider/extension_provider.ts +0 -233
- package/src/extension/provider/index.ts +0 -7
|
@@ -1,34 +1,6 @@
|
|
|
1
|
-
import type { DiscoverWalletsOptions,
|
|
1
|
+
import type { DiscoverWalletsOptions, WalletManagerConfig, WalletProvider } from './types.js';
|
|
2
2
|
/**
|
|
3
|
-
* Manager for wallet discovery, configuration, and connection
|
|
4
|
-
*
|
|
5
|
-
* This is the main entry point for dApps to discover and connect to wallets.
|
|
6
|
-
*
|
|
7
|
-
* @example Basic usage with async iterator
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const discovery = WalletManager.configure({ extensions: { enabled: true } })
|
|
10
|
-
* .getAvailableWallets({ chainInfo, appId: 'my-app' });
|
|
11
|
-
*
|
|
12
|
-
* // Iterate over discovered wallets
|
|
13
|
-
* for await (const provider of discovery.wallets) {
|
|
14
|
-
* console.log(`Found wallet: ${provider.name}`);
|
|
15
|
-
* }
|
|
16
|
-
*
|
|
17
|
-
* // Or cancel early when done
|
|
18
|
-
* discovery.cancel();
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* @example With callback for discovered wallets
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const discovery = manager.getAvailableWallets({
|
|
24
|
-
* chainInfo,
|
|
25
|
-
* appId: 'my-app',
|
|
26
|
-
* onWalletDiscovered: (provider) => console.log(`Found: ${provider.name}`),
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* // Wait for discovery to complete or cancel it
|
|
30
|
-
* await discovery.done;
|
|
31
|
-
* ```
|
|
3
|
+
* Manager for wallet discovery, configuration, and connection
|
|
32
4
|
*/
|
|
33
5
|
export declare class WalletManager {
|
|
34
6
|
private config;
|
|
@@ -40,26 +12,11 @@ export declare class WalletManager {
|
|
|
40
12
|
static configure(config: WalletManagerConfig): WalletManager;
|
|
41
13
|
/**
|
|
42
14
|
* Discovers all available wallets for a given chain and version.
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* - `done`: Promise that resolves when discovery completes or is cancelled
|
|
47
|
-
* - `cancel()`: Function to stop discovery immediately
|
|
48
|
-
*
|
|
49
|
-
* If `onWalletDiscovered` callback is provided, wallets are also streamed via callback.
|
|
50
|
-
*
|
|
51
|
-
* @param options - Discovery options including chain info, appId, and timeout
|
|
52
|
-
* @returns A cancellable discovery session
|
|
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
|
|
53
18
|
*/
|
|
54
|
-
getAvailableWallets(options: DiscoverWalletsOptions):
|
|
55
|
-
/**
|
|
56
|
-
* Creates a WalletProvider from a discovered wallet.
|
|
57
|
-
* Returns null if the wallet is not allowed by config.
|
|
58
|
-
* @param discoveredWallet - The discovered wallet from extension discovery.
|
|
59
|
-
* @param chainInfo - Network information.
|
|
60
|
-
* @param extensionConfig - Extension wallet configuration.
|
|
61
|
-
*/
|
|
62
|
-
private createProviderFromDiscoveredWallet;
|
|
19
|
+
getAvailableWallets(options: DiscoverWalletsOptions): Promise<WalletProvider[]>;
|
|
63
20
|
/**
|
|
64
21
|
* Checks if an extension is allowed based on allow/block lists
|
|
65
22
|
* @param extensionId - The extension ID to check
|
|
@@ -67,4 +24,4 @@ export declare class WalletManager {
|
|
|
67
24
|
*/
|
|
68
25
|
private isExtensionAllowed;
|
|
69
26
|
}
|
|
70
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
27
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid2FsbGV0X21hbmFnZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9tYW5hZ2VyL3dhbGxldF9tYW5hZ2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sS0FBSyxFQUNWLHNCQUFzQixFQUd0QixtQkFBbUIsRUFDbkIsY0FBYyxFQUNmLE1BQU0sWUFBWSxDQUFDO0FBRXBCOztHQUVHO0FBQ0gscUJBQWEsYUFBYTtJQUN4QixPQUFPLENBQUMsTUFBTSxDQUdaO0lBRUYsT0FBTyxlQUFpQjtJQUV4Qjs7O09BR0c7SUFDSCxNQUFNLENBQUMsU0FBUyxDQUFDLE1BQU0sRUFBRSxtQkFBbUIsR0FBRyxhQUFhLENBTzNEO0lBRUQ7Ozs7O09BS0c7SUFDRyxtQkFBbUIsQ0FBQyxPQUFPLEVBQUUsc0JBQXNCLEdBQUcsT0FBTyxDQUFDLGNBQWMsRUFBRSxDQUFDLENBdURwRjtJQUVEOzs7O09BSUc7SUFDSCxPQUFPLENBQUMsa0JBQWtCO0NBVzNCIn0=
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet_manager.d.ts","sourceRoot":"","sources":["../../src/manager/wallet_manager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"wallet_manager.d.ts","sourceRoot":"","sources":["../../src/manager/wallet_manager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,sBAAsB,EAGtB,mBAAmB,EACnB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAGZ;IAEF,OAAO,eAAiB;IAExB;;;OAGG;IACH,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,CAO3D;IAED;;;;;OAKG;IACG,mBAAmB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC,CAuDpF;IAED;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;CAW3B"}
|
|
@@ -1,36 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ExtensionProvider, ExtensionWallet } from '../extension/provider/index.js';
|
|
3
|
-
import { WalletMessageType } from '../types.js';
|
|
1
|
+
import { ExtensionProvider, ExtensionWallet } from '../providers/extension/index.js';
|
|
4
2
|
/**
|
|
5
|
-
* Manager for wallet discovery, configuration, and connection
|
|
6
|
-
*
|
|
7
|
-
* This is the main entry point for dApps to discover and connect to wallets.
|
|
8
|
-
*
|
|
9
|
-
* @example Basic usage with async iterator
|
|
10
|
-
* ```typescript
|
|
11
|
-
* const discovery = WalletManager.configure({ extensions: { enabled: true } })
|
|
12
|
-
* .getAvailableWallets({ chainInfo, appId: 'my-app' });
|
|
13
|
-
*
|
|
14
|
-
* // Iterate over discovered wallets
|
|
15
|
-
* for await (const provider of discovery.wallets) {
|
|
16
|
-
* console.log(`Found wallet: ${provider.name}`);
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* // Or cancel early when done
|
|
20
|
-
* discovery.cancel();
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* @example With callback for discovered wallets
|
|
24
|
-
* ```typescript
|
|
25
|
-
* const discovery = manager.getAvailableWallets({
|
|
26
|
-
* chainInfo,
|
|
27
|
-
* appId: 'my-app',
|
|
28
|
-
* onWalletDiscovered: (provider) => console.log(`Found: ${provider.name}`),
|
|
29
|
-
* });
|
|
30
|
-
*
|
|
31
|
-
* // Wait for discovery to complete or cancel it
|
|
32
|
-
* await discovery.done;
|
|
33
|
-
* ```
|
|
3
|
+
* Manager for wallet discovery, configuration, and connection
|
|
34
4
|
*/ export class WalletManager {
|
|
35
5
|
config = {
|
|
36
6
|
extensions: {
|
|
@@ -58,157 +28,57 @@ import { WalletMessageType } from '../types.js';
|
|
|
58
28
|
}
|
|
59
29
|
/**
|
|
60
30
|
* Discovers all available wallets for a given chain and version.
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
* If `onWalletDiscovered` callback is provided, wallets are also streamed via callback.
|
|
68
|
-
*
|
|
69
|
-
* @param options - Discovery options including chain info, appId, and timeout
|
|
70
|
-
* @returns A cancellable discovery session
|
|
71
|
-
*/ getAvailableWallets(options) {
|
|
72
|
-
const { chainInfo, appId } = options;
|
|
73
|
-
const abortController = new AbortController();
|
|
74
|
-
const pendingProviders = [];
|
|
75
|
-
let pendingResolve = null;
|
|
76
|
-
let completed = false;
|
|
77
|
-
const { promise: donePromise, resolve: resolveDone } = promiseWithResolvers();
|
|
78
|
-
const markComplete = ()=>{
|
|
79
|
-
completed = true;
|
|
80
|
-
resolveDone();
|
|
81
|
-
if (pendingResolve) {
|
|
82
|
-
const resolve = pendingResolve;
|
|
83
|
-
pendingResolve = null;
|
|
84
|
-
resolve({
|
|
85
|
-
value: undefined,
|
|
86
|
-
done: true
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
};
|
|
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;
|
|
90
37
|
if (this.config.extensions?.enabled) {
|
|
38
|
+
const discoveredWallets = await ExtensionProvider.discoverExtensions(chainInfo, options.timeout);
|
|
91
39
|
const extensionConfig = this.config.extensions;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
signal: abortController.signal,
|
|
96
|
-
onWalletDiscovered: (discoveredWallet)=>{
|
|
97
|
-
const provider = this.createProviderFromDiscoveredWallet(discoveredWallet, chainInfo, extensionConfig);
|
|
98
|
-
if (!provider) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
// Call user's callback if provided
|
|
102
|
-
options.onWalletDiscovered?.(provider);
|
|
103
|
-
// Also queue for async iterator
|
|
104
|
-
if (pendingResolve) {
|
|
105
|
-
const resolve = pendingResolve;
|
|
106
|
-
pendingResolve = null;
|
|
107
|
-
resolve({
|
|
108
|
-
value: provider,
|
|
109
|
-
done: false
|
|
110
|
-
});
|
|
111
|
-
} else {
|
|
112
|
-
pendingProviders.push(provider);
|
|
113
|
-
}
|
|
40
|
+
for (const { info, port, sharedKey } of discoveredWallets){
|
|
41
|
+
if (!this.isExtensionAllowed(info.id, extensionConfig)) {
|
|
42
|
+
continue;
|
|
114
43
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
44
|
+
let extensionWallet = null;
|
|
45
|
+
const provider = {
|
|
46
|
+
id: info.id,
|
|
47
|
+
type: 'extension',
|
|
48
|
+
name: info.name,
|
|
49
|
+
icon: info.icon,
|
|
50
|
+
metadata: {
|
|
51
|
+
version: info.version,
|
|
52
|
+
verificationHash: info.verificationHash
|
|
53
|
+
},
|
|
54
|
+
connect: (appId)=>{
|
|
55
|
+
extensionWallet = ExtensionWallet.create(info, chainInfo, port, sharedKey, appId);
|
|
56
|
+
return Promise.resolve(extensionWallet.getWallet());
|
|
57
|
+
},
|
|
58
|
+
disconnect: async ()=>{
|
|
59
|
+
if (extensionWallet) {
|
|
60
|
+
await extensionWallet.disconnect();
|
|
61
|
+
extensionWallet = null;
|
|
130
62
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
});
|
|
63
|
+
},
|
|
64
|
+
onDisconnect: (callback)=>{
|
|
65
|
+
if (extensionWallet) {
|
|
66
|
+
return extensionWallet.onDisconnect(callback);
|
|
136
67
|
}
|
|
137
|
-
return
|
|
138
|
-
pendingResolve = resolve;
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
return {
|
|
145
|
-
wallets,
|
|
146
|
-
done: donePromise,
|
|
147
|
-
cancel: ()=>abortController.abort()
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Creates a WalletProvider from a discovered wallet.
|
|
152
|
-
* Returns null if the wallet is not allowed by config.
|
|
153
|
-
* @param discoveredWallet - The discovered wallet from extension discovery.
|
|
154
|
-
* @param chainInfo - Network information.
|
|
155
|
-
* @param extensionConfig - Extension wallet configuration.
|
|
156
|
-
*/ createProviderFromDiscoveredWallet(discoveredWallet, chainInfo, extensionConfig) {
|
|
157
|
-
const { info } = discoveredWallet;
|
|
158
|
-
if (!this.isExtensionAllowed(info.id, extensionConfig)) {
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
let extensionWallet = null;
|
|
162
|
-
const provider = {
|
|
163
|
-
id: info.id,
|
|
164
|
-
type: 'extension',
|
|
165
|
-
name: info.name,
|
|
166
|
-
icon: info.icon,
|
|
167
|
-
metadata: {
|
|
168
|
-
version: info.version
|
|
169
|
-
},
|
|
170
|
-
establishSecureChannel: async (connectAppId)=>{
|
|
171
|
-
const connection = await discoveredWallet.establishSecureChannel();
|
|
172
|
-
provider.metadata = {
|
|
173
|
-
...provider.metadata,
|
|
174
|
-
verificationHash: connection.info.verificationHash
|
|
175
|
-
};
|
|
176
|
-
return {
|
|
177
|
-
verificationHash: connection.info.verificationHash,
|
|
178
|
-
confirm: ()=>{
|
|
179
|
-
return Promise.resolve(ExtensionWallet.create(connection.info.id, connection.port, connection.sharedKey, chainInfo, connectAppId));
|
|
68
|
+
return ()=>{};
|
|
180
69
|
},
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
requestId: discoveredWallet.requestId
|
|
187
|
-
});
|
|
188
|
-
// Don't close the port - allow retry with fresh key exchange
|
|
70
|
+
isDisconnected: ()=>{
|
|
71
|
+
if (extensionWallet) {
|
|
72
|
+
return extensionWallet.isDisconnected();
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
189
75
|
}
|
|
190
76
|
};
|
|
191
|
-
|
|
192
|
-
disconnect: async ()=>{
|
|
193
|
-
if (extensionWallet) {
|
|
194
|
-
await extensionWallet.disconnect();
|
|
195
|
-
extensionWallet = null;
|
|
196
|
-
}
|
|
197
|
-
},
|
|
198
|
-
onDisconnect: (callback)=>{
|
|
199
|
-
if (extensionWallet) {
|
|
200
|
-
return extensionWallet.onDisconnect(callback);
|
|
201
|
-
}
|
|
202
|
-
return ()=>{};
|
|
203
|
-
},
|
|
204
|
-
isDisconnected: ()=>{
|
|
205
|
-
if (extensionWallet) {
|
|
206
|
-
return extensionWallet.isDisconnected();
|
|
207
|
-
}
|
|
208
|
-
return true;
|
|
77
|
+
providers.push(provider);
|
|
209
78
|
}
|
|
210
|
-
}
|
|
211
|
-
|
|
79
|
+
}
|
|
80
|
+
// TODO: Add web wallet discovery when implemented
|
|
81
|
+
return providers;
|
|
212
82
|
}
|
|
213
83
|
/**
|
|
214
84
|
* Checks if an extension is allowed based on allow/block lists
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
|
+
import { type WalletInfo } from '../../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A discovered wallet with its secure channel components.
|
|
5
|
+
* Returned by {@link ExtensionProvider.discoverExtensions}.
|
|
6
|
+
*/
|
|
7
|
+
export interface DiscoveredWallet {
|
|
8
|
+
/** Basic wallet information (id, name, icon, version, publicKey, verificationHash) */
|
|
9
|
+
info: WalletInfo;
|
|
10
|
+
/** The MessagePort for private communication with the wallet */
|
|
11
|
+
port: MessagePort;
|
|
12
|
+
/** The derived AES-256-GCM shared key for encryption */
|
|
13
|
+
sharedKey: CryptoKey;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Provider for discovering Aztec wallet extensions.
|
|
17
|
+
*
|
|
18
|
+
* This class handles the discovery phase of wallet communication:
|
|
19
|
+
* 1. Broadcasts a discovery request with the dApp's public key
|
|
20
|
+
* 2. Receives responses from installed wallets with their public keys
|
|
21
|
+
* 3. Derives shared secrets and computes verification hashes
|
|
22
|
+
* 4. Returns discovered wallets with their secure channel components
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
27
|
+
* // Display wallets to user with optional emoji verification
|
|
28
|
+
* for (const wallet of wallets) {
|
|
29
|
+
* const emoji = hashToEmoji(wallet.info.verificationHash!);
|
|
30
|
+
* console.log(`${wallet.info.name}: ${emoji}`);
|
|
31
|
+
* }
|
|
32
|
+
* // User selects a wallet after verifying
|
|
33
|
+
* const wallet = await ExtensionWallet.create(wallets[0], chainInfo, 'my-app');
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare class ExtensionProvider {
|
|
37
|
+
private static discoveryInProgress;
|
|
38
|
+
/**
|
|
39
|
+
* Discovers all installed Aztec wallet extensions and establishes secure channel components.
|
|
40
|
+
*
|
|
41
|
+
* This method:
|
|
42
|
+
* 1. Generates an ECDH key pair for this discovery session
|
|
43
|
+
* 2. Broadcasts a discovery request with the dApp's public key
|
|
44
|
+
* 3. Receives responses from wallets with their public keys and MessagePorts
|
|
45
|
+
* 4. Derives shared secrets and computes verification hashes
|
|
46
|
+
*
|
|
47
|
+
* @param chainInfo - Chain information to check if extensions support this network
|
|
48
|
+
* @param timeout - How long to wait for extensions to respond (ms)
|
|
49
|
+
* @returns Array of discovered wallets with their secure channel components
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const wallets = await ExtensionProvider.discoverExtensions({
|
|
54
|
+
* chainId: Fr(31337),
|
|
55
|
+
* version: Fr(0)
|
|
56
|
+
* });
|
|
57
|
+
* // Access wallet info and secure channel
|
|
58
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
static discoverExtensions(chainInfo: ChainInfo, timeout?: number): Promise<DiscoveredWallet[]>;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3Byb3ZpZGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvcHJvdmlkZXJzL2V4dGVuc2lvbi9leHRlbnNpb25fcHJvdmlkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsU0FBUyxFQUFFLE1BQU0seUJBQXlCLENBQUM7QUFLekQsT0FBTyxFQUFpRCxLQUFLLFVBQVUsRUFBcUIsTUFBTSxnQkFBZ0IsQ0FBQztBQUVuSDs7O0dBR0c7QUFDSCxNQUFNLFdBQVcsZ0JBQWdCO0lBQy9CLHNGQUFzRjtJQUN0RixJQUFJLEVBQUUsVUFBVSxDQUFDO0lBQ2pCLGdFQUFnRTtJQUNoRSxJQUFJLEVBQUUsV0FBVyxDQUFDO0lBQ2xCLHdEQUF3RDtJQUN4RCxTQUFTLEVBQUUsU0FBUyxDQUFDO0NBQ3RCO0FBV0Q7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBb0JHO0FBQ0gscUJBQWEsaUJBQWlCO0lBQzVCLE9BQU8sQ0FBQyxNQUFNLENBQUMsbUJBQW1CLENBQVM7SUFFM0M7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7T0FzQkc7SUFDSCxPQUFhLGtCQUFrQixDQUFDLFNBQVMsRUFBRSxTQUFTLEVBQUUsT0FBTyxHQUFFLE1BQWEsR0FBRyxPQUFPLENBQUMsZ0JBQWdCLEVBQUUsQ0FBQyxDQXlGekc7Q0FDRiJ9
|
|
@@ -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;AAKzD,OAAO,EAAiD,KAAK,UAAU,EAAqB,MAAM,gBAAgB,CAAC;AAEnH;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,sFAAsF;IACtF,IAAI,EAAE,UAAU,CAAC;IACjB,gEAAgE;IAChE,IAAI,EAAE,WAAW,CAAC;IAClB,wDAAwD;IACxD,SAAS,EAAE,SAAS,CAAC;CACtB;AAWD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAS;IAE3C;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,OAAa,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,GAAE,MAAa,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAyFzG;CACF"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
2
|
+
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
3
|
+
import { deriveSharedKey, exportPublicKey, generateKeyPair, hashSharedSecret, importPublicKey } from '../../crypto.js';
|
|
4
|
+
import { WalletMessageType } from '../../types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Provider for discovering Aztec wallet extensions.
|
|
7
|
+
*
|
|
8
|
+
* This class handles the discovery phase of wallet communication:
|
|
9
|
+
* 1. Broadcasts a discovery request with the dApp's public key
|
|
10
|
+
* 2. Receives responses from installed wallets with their public keys
|
|
11
|
+
* 3. Derives shared secrets and computes verification hashes
|
|
12
|
+
* 4. Returns discovered wallets with their secure channel components
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
17
|
+
* // Display wallets to user with optional emoji verification
|
|
18
|
+
* for (const wallet of wallets) {
|
|
19
|
+
* const emoji = hashToEmoji(wallet.info.verificationHash!);
|
|
20
|
+
* console.log(`${wallet.info.name}: ${emoji}`);
|
|
21
|
+
* }
|
|
22
|
+
* // User selects a wallet after verifying
|
|
23
|
+
* const wallet = await ExtensionWallet.create(wallets[0], chainInfo, 'my-app');
|
|
24
|
+
* ```
|
|
25
|
+
*/ export class ExtensionProvider {
|
|
26
|
+
static discoveryInProgress = false;
|
|
27
|
+
/**
|
|
28
|
+
* Discovers all installed Aztec wallet extensions and establishes secure channel components.
|
|
29
|
+
*
|
|
30
|
+
* This method:
|
|
31
|
+
* 1. Generates an ECDH key pair for this discovery session
|
|
32
|
+
* 2. Broadcasts a discovery request with the dApp's public key
|
|
33
|
+
* 3. Receives responses from wallets with their public keys and MessagePorts
|
|
34
|
+
* 4. Derives shared secrets and computes verification hashes
|
|
35
|
+
*
|
|
36
|
+
* @param chainInfo - Chain information to check if extensions support this network
|
|
37
|
+
* @param timeout - How long to wait for extensions to respond (ms)
|
|
38
|
+
* @returns Array of discovered wallets with their secure channel components
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const wallets = await ExtensionProvider.discoverExtensions({
|
|
43
|
+
* chainId: Fr(31337),
|
|
44
|
+
* version: Fr(0)
|
|
45
|
+
* });
|
|
46
|
+
* // Access wallet info and secure channel
|
|
47
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
48
|
+
* ```
|
|
49
|
+
*/ static async discoverExtensions(chainInfo, timeout = 1000) {
|
|
50
|
+
// If discovery is already in progress, wait and return empty
|
|
51
|
+
// (caller should retry or handle appropriately)
|
|
52
|
+
if (this.discoveryInProgress) {
|
|
53
|
+
await new Promise((resolve)=>setTimeout(resolve, timeout));
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
this.discoveryInProgress = true;
|
|
57
|
+
// Generate key pair for this discovery session
|
|
58
|
+
const keyPair = await generateKeyPair();
|
|
59
|
+
const exportedPublicKey = await exportPublicKey(keyPair.publicKey);
|
|
60
|
+
const { promise, resolve } = promiseWithResolvers();
|
|
61
|
+
const requestId = globalThis.crypto.randomUUID();
|
|
62
|
+
const responses = [];
|
|
63
|
+
// Set up listener for discovery responses
|
|
64
|
+
const handleMessage = (event)=>{
|
|
65
|
+
if (event.source !== window) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
let data;
|
|
69
|
+
try {
|
|
70
|
+
data = JSON.parse(event.data);
|
|
71
|
+
} catch {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (data.type === WalletMessageType.DISCOVERY_RESPONSE && data.requestId === requestId) {
|
|
75
|
+
// Get the MessagePort from the event
|
|
76
|
+
const port = event.ports?.[0];
|
|
77
|
+
if (!port) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
// Derive shared key from wallet's public key
|
|
81
|
+
const walletPublicKey = data.walletInfo.publicKey;
|
|
82
|
+
if (!walletPublicKey) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
void (async ()=>{
|
|
86
|
+
try {
|
|
87
|
+
const importedWalletKey = await importPublicKey(walletPublicKey);
|
|
88
|
+
const sharedKey = await deriveSharedKey(keyPair.privateKey, importedWalletKey);
|
|
89
|
+
// Compute verification hash
|
|
90
|
+
const verificationHash = await hashSharedSecret(sharedKey);
|
|
91
|
+
// Create wallet info with verification hash
|
|
92
|
+
const walletInfo = {
|
|
93
|
+
...data.walletInfo,
|
|
94
|
+
verificationHash
|
|
95
|
+
};
|
|
96
|
+
responses.push({
|
|
97
|
+
info: walletInfo,
|
|
98
|
+
port,
|
|
99
|
+
sharedKey
|
|
100
|
+
});
|
|
101
|
+
} catch {
|
|
102
|
+
// Failed to derive key, skip this wallet
|
|
103
|
+
}
|
|
104
|
+
})();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
window.addEventListener('message', handleMessage);
|
|
108
|
+
// Send discovery message with our public key
|
|
109
|
+
const discoveryMessage = {
|
|
110
|
+
type: WalletMessageType.DISCOVERY,
|
|
111
|
+
requestId,
|
|
112
|
+
chainInfo,
|
|
113
|
+
publicKey: exportedPublicKey
|
|
114
|
+
};
|
|
115
|
+
window.postMessage(jsonStringify(discoveryMessage), '*');
|
|
116
|
+
// Wait for responses
|
|
117
|
+
setTimeout(()=>{
|
|
118
|
+
window.removeEventListener('message', handleMessage);
|
|
119
|
+
this.discoveryInProgress = false;
|
|
120
|
+
resolve(responses);
|
|
121
|
+
}, timeout);
|
|
122
|
+
return promise;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
|
+
import { type Wallet } from '@aztec/aztec.js/wallet';
|
|
3
|
+
import { type WalletInfo } from '../../types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Callback type for wallet disconnect events.
|
|
6
|
+
*/
|
|
7
|
+
export type DisconnectCallback = () => void;
|
|
8
|
+
/**
|
|
9
|
+
* A wallet implementation that communicates with browser extension wallets
|
|
10
|
+
* using a secure encrypted MessageChannel.
|
|
11
|
+
*
|
|
12
|
+
* This class uses a pre-established secure channel from the discovery phase:
|
|
13
|
+
*
|
|
14
|
+
* 1. **MessageChannel**: A private communication channel created during discovery,
|
|
15
|
+
* not visible to other scripts on the page (unlike window.postMessage).
|
|
16
|
+
*
|
|
17
|
+
* 2. **ECDH Key Exchange**: The shared secret was derived during discovery using
|
|
18
|
+
* Elliptic Curve Diffie-Hellman key exchange.
|
|
19
|
+
*
|
|
20
|
+
* 3. **AES-GCM Encryption**: All messages are encrypted using AES-256-GCM,
|
|
21
|
+
* providing both confidentiality and authenticity.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* // Discovery returns wallets with secure channel components
|
|
26
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
27
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
28
|
+
*
|
|
29
|
+
* // User can verify emoji if desired
|
|
30
|
+
* console.log('Verify:', hashToEmoji(info.verificationHash!));
|
|
31
|
+
*
|
|
32
|
+
* // Create wallet using the discovered components
|
|
33
|
+
* const wallet = await ExtensionWallet.create(info, chainInfo, port, sharedKey, 'my-dapp');
|
|
34
|
+
*
|
|
35
|
+
* // All subsequent calls are encrypted
|
|
36
|
+
* const accounts = await wallet.getAccounts();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare class ExtensionWallet {
|
|
40
|
+
private chainInfo;
|
|
41
|
+
private appId;
|
|
42
|
+
private extensionId;
|
|
43
|
+
private port;
|
|
44
|
+
private sharedKey;
|
|
45
|
+
/** Map of pending requests awaiting responses, keyed by message ID */
|
|
46
|
+
private inFlight;
|
|
47
|
+
private disconnected;
|
|
48
|
+
private disconnectCallbacks;
|
|
49
|
+
/**
|
|
50
|
+
* Private constructor - use {@link ExtensionWallet.create} to instantiate.
|
|
51
|
+
* @param chainInfo - The chain information (chainId and version)
|
|
52
|
+
* @param appId - Application identifier for the requesting dApp
|
|
53
|
+
* @param extensionId - The unique identifier of the target wallet extension
|
|
54
|
+
* @param port - The MessagePort for private communication with the wallet
|
|
55
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
56
|
+
*/
|
|
57
|
+
private constructor();
|
|
58
|
+
/** Cached Wallet proxy instance */
|
|
59
|
+
private walletProxy;
|
|
60
|
+
/**
|
|
61
|
+
* Creates an ExtensionWallet instance that communicates with a browser extension
|
|
62
|
+
* over a secure encrypted MessageChannel.
|
|
63
|
+
*
|
|
64
|
+
* @param walletInfo - The wallet info from ExtensionProvider.discoverExtensions()
|
|
65
|
+
* @param chainInfo - The chain information (chainId and version) for request context
|
|
66
|
+
* @param port - The MessagePort for private communication with the wallet
|
|
67
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
68
|
+
* @param appId - Application identifier used to identify the requesting dApp to the wallet
|
|
69
|
+
* @returns The ExtensionWallet instance. Use {@link getWallet} to get the Wallet interface.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
74
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
75
|
+
* const extensionWallet = ExtensionWallet.create(
|
|
76
|
+
* info,
|
|
77
|
+
* { chainId: Fr(31337), version: Fr(0) },
|
|
78
|
+
* port,
|
|
79
|
+
* sharedKey,
|
|
80
|
+
* 'my-defi-app'
|
|
81
|
+
* );
|
|
82
|
+
*
|
|
83
|
+
* // Register disconnect handler
|
|
84
|
+
* extensionWallet.onDisconnect(() => console.log('Disconnected!'));
|
|
85
|
+
*
|
|
86
|
+
* // Get the Wallet interface for dApp usage
|
|
87
|
+
* const wallet = extensionWallet.getWallet();
|
|
88
|
+
* const accounts = await wallet.getAccounts();
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
static create(walletInfo: WalletInfo, chainInfo: ChainInfo, port: MessagePort, sharedKey: CryptoKey, appId: string): ExtensionWallet;
|
|
92
|
+
/**
|
|
93
|
+
* Returns a Wallet interface that proxies all method calls through the secure channel.
|
|
94
|
+
*
|
|
95
|
+
* The returned Wallet can be used directly by dApps - all method calls are automatically
|
|
96
|
+
* encrypted and sent to the wallet extension.
|
|
97
|
+
*
|
|
98
|
+
* @returns A Wallet implementation that encrypts all communication
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const extensionWallet = ExtensionWallet.create(info, chainInfo, port, sharedKey, 'my-app');
|
|
103
|
+
* const wallet = extensionWallet.getWallet();
|
|
104
|
+
* const accounts = await wallet.getAccounts();
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
getWallet(): Wallet;
|
|
108
|
+
private handleEncryptedResponse;
|
|
109
|
+
private postMessage;
|
|
110
|
+
/**
|
|
111
|
+
* Handles wallet disconnection.
|
|
112
|
+
* Rejects all pending requests and notifies registered callbacks.
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
private handleDisconnect;
|
|
116
|
+
/**
|
|
117
|
+
* Registers a callback to be invoked when the wallet disconnects.
|
|
118
|
+
*
|
|
119
|
+
* @param callback - Function to call when wallet disconnects
|
|
120
|
+
* @returns A function to unregister the callback
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const wallet = await ExtensionWallet.create(...);
|
|
125
|
+
* const unsubscribe = wallet.onDisconnect(() => {
|
|
126
|
+
* console.log('Wallet disconnected! Please reconnect.');
|
|
127
|
+
* // Clean up UI, prompt user to reconnect, etc.
|
|
128
|
+
* });
|
|
129
|
+
* // Later: unsubscribe(); to stop receiving notifications
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
onDisconnect(callback: DisconnectCallback): () => void;
|
|
133
|
+
/**
|
|
134
|
+
* Returns whether the wallet has been disconnected.
|
|
135
|
+
*
|
|
136
|
+
* @returns true if the wallet is no longer connected
|
|
137
|
+
*/
|
|
138
|
+
isDisconnected(): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Disconnects from the wallet and cleans up resources.
|
|
141
|
+
*
|
|
142
|
+
* This method notifies the wallet extension that the session is ending,
|
|
143
|
+
* allowing it to clean up its state. After calling this method, the wallet
|
|
144
|
+
* instance can no longer be used and any pending requests will be rejected.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const wallet = await provider.connect('my-app');
|
|
149
|
+
* // ... use wallet ...
|
|
150
|
+
* await wallet.disconnect(); // Clean disconnect when done
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
disconnect(): Promise<void>;
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3dhbGxldC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL3Byb3ZpZGVycy9leHRlbnNpb24vZXh0ZW5zaW9uX3dhbGxldC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUN6RCxPQUFPLEVBQUUsS0FBSyxNQUFNLEVBQWdCLE1BQU0sd0JBQXdCLENBQUM7QUFPbkUsT0FBTyxFQUFFLEtBQUssVUFBVSxFQUE4RCxNQUFNLGdCQUFnQixDQUFDO0FBYTdHOztHQUVHO0FBQ0gsTUFBTSxNQUFNLGtCQUFrQixHQUFHLE1BQU0sSUFBSSxDQUFDO0FBRTVDOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7R0E4Qkc7QUFDSCxxQkFBYSxlQUFlO0lBZXhCLE9BQU8sQ0FBQyxTQUFTO0lBQ2pCLE9BQU8sQ0FBQyxLQUFLO0lBQ2IsT0FBTyxDQUFDLFdBQVc7SUFDbkIsT0FBTyxDQUFDLElBQUk7SUFDWixPQUFPLENBQUMsU0FBUztJQWxCbkIsc0VBQXNFO0lBQ3RFLE9BQU8sQ0FBQyxRQUFRLENBQW9EO0lBQ3BFLE9BQU8sQ0FBQyxZQUFZLENBQVM7SUFDN0IsT0FBTyxDQUFDLG1CQUFtQixDQUE0QjtJQUV2RDs7Ozs7OztPQU9HO0lBQ0gsT0FBTyxlQU1IO0lBRUosbUNBQW1DO0lBQ25DLE9BQU8sQ0FBQyxXQUFXLENBQXVCO0lBRTFDOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7T0E4Qkc7SUFDSCxNQUFNLENBQUMsTUFBTSxDQUNYLFVBQVUsRUFBRSxVQUFVLEVBQ3RCLFNBQVMsRUFBRSxTQUFTLEVBQ3BCLElBQUksRUFBRSxXQUFXLEVBQ2pCLFNBQVMsRUFBRSxTQUFTLEVBQ3BCLEtBQUssRUFBRSxNQUFNLEdBQ1osZUFBZSxDQVdqQjtJQUVEOzs7Ozs7Ozs7Ozs7OztPQWNHO0lBQ0gsU0FBUyxJQUFJLE1BQU0sQ0F1QmxCO1lBVWEsdUJBQXVCO1lBMER2QixXQUFXO0lBMkJ6Qjs7OztPQUlHO0lBQ0gsT0FBTyxDQUFDLGdCQUFnQjtJQStCeEI7Ozs7Ozs7Ozs7Ozs7OztPQWVHO0lBQ0gsWUFBWSxDQUFDLFFBQVEsRUFBRSxrQkFBa0IsR0FBRyxNQUFNLElBQUksQ0FRckQ7SUFFRDs7OztPQUlHO0lBQ0gsY0FBYyxJQUFJLE9BQU8sQ0FFeEI7SUFFRDs7Ozs7Ozs7Ozs7OztPQWFHO0lBQ0csVUFBVSxJQUFJLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0EyQmhDO0NBQ0YifQ==
|