@docknetwork/wallet-sdk-web 0.0.5 → 0.0.8
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 +10 -0
- package/dist/wallet-sdk-web.esm.js +331 -85
- package/dist/wallet-sdk-web.iife.js +331 -85
- package/package.json +1 -1
- package/src/index.js +307 -77
package/src/index.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Dock Wallet SDK for Web
|
|
3
|
+
*
|
|
4
|
+
* This module provides a simplified interface for initializing and working with
|
|
5
|
+
* the Dock Wallet SDK in web environments. It includes functions for managing
|
|
6
|
+
* DIDs, credentials, presentations, and cloud wallet synchronization.
|
|
7
|
+
*
|
|
8
|
+
* @module @docknetwork/wallet-sdk-web
|
|
9
|
+
*/
|
|
10
|
+
|
|
1
11
|
import { createDataStore } from '@docknetwork/wallet-sdk-data-store-web/src/index';
|
|
2
12
|
import { initializeCloudWallet, generateCloudWalletMasterKey, recoverCloudWalletMasterKey } from '@docknetwork/wallet-sdk-core/src/cloud-wallet';
|
|
3
13
|
import { createWallet } from '@docknetwork/wallet-sdk-core/src/wallet';
|
|
@@ -6,145 +16,365 @@ import { createDIDProvider } from '@docknetwork/wallet-sdk-core/src/did-provider
|
|
|
6
16
|
import { createMessageProvider } from '@docknetwork/wallet-sdk-core/src/message-provider';
|
|
7
17
|
import { createVerificationController } from '@docknetwork/wallet-sdk-core/src/verification-controller';
|
|
8
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Initializes the Dock Wallet SDK with the provided configuration.
|
|
21
|
+
*
|
|
22
|
+
* This function sets up all necessary wallet providers and establishes a connection
|
|
23
|
+
* to the cloud wallet for synchronization. It requires either a master key or mnemonic
|
|
24
|
+
* for wallet recovery, but not both.
|
|
25
|
+
*
|
|
26
|
+
* @async
|
|
27
|
+
* @param {Object} config - Configuration object for wallet initialization
|
|
28
|
+
* @param {string} config.edvUrl - The Encrypted Data Vault (EDV) URL for cloud storage
|
|
29
|
+
* @param {string} config.edvAuthKey - Authentication key for accessing the EDV
|
|
30
|
+
* @param {string} [config.masterKey] - Master key for wallet access (required if mnemonic is not provided)
|
|
31
|
+
* @param {string} [config.mnemonic] - BIP39 mnemonic for wallet recovery (required if masterKey is not provided)
|
|
32
|
+
* @param {string} config.networkId - Network identifier ('testnet' or 'mainnet')
|
|
33
|
+
* @param {string} [config.databasePath='truvera-web-wallet'] - Optional path for the local database
|
|
34
|
+
*
|
|
35
|
+
* @returns {Promise<Object>} Initialized wallet object with the following properties:
|
|
36
|
+
* @returns {Object} returns.wallet - Core wallet instance
|
|
37
|
+
* @returns {Object} returns.messageProvider - Provider for handling wallet messages
|
|
38
|
+
* @returns {Object} returns.cloudWallet - Cloud wallet instance for synchronization
|
|
39
|
+
* @returns {Object} returns.didProvider - Provider for DID operations
|
|
40
|
+
* @returns {Object} returns.credentialProvider - Provider for credential operations
|
|
41
|
+
* @returns {Function} returns.getCredentials - Function to retrieve all credentials
|
|
42
|
+
* @returns {Function} returns.addCredential - Function to import a credential from an offer URI
|
|
43
|
+
* @returns {Function} returns.getDID - Function to get the default DID
|
|
44
|
+
* @returns {Function} returns.submitPresentation - Function to create and submit a presentation
|
|
45
|
+
*
|
|
46
|
+
* @throws {Error} When neither masterKey nor mnemonic is provided
|
|
47
|
+
* @throws {Error} When both masterKey and mnemonic are provided
|
|
48
|
+
* @throws {Error} When edvUrl is not provided
|
|
49
|
+
* @throws {Error} When edvAuthKey is not provided
|
|
50
|
+
* @throws {Error} When networkId is not provided
|
|
51
|
+
* @throws {Error} When networkId is not 'testnet' or 'mainnet'
|
|
52
|
+
* @throws {Error} When wallet initialization fails
|
|
53
|
+
* @throws {Error} When cloud wallet initialization fails
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // Initialize with mnemonic
|
|
57
|
+
* const wallet = await initialize({
|
|
58
|
+
* edvUrl: 'https://edv.example.com',
|
|
59
|
+
* edvAuthKey: 'your-auth-key',
|
|
60
|
+
* mnemonic: 'your twelve word mnemonic phrase here...',
|
|
61
|
+
* networkId: 'testnet'
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // Initialize with master key
|
|
66
|
+
* const wallet = await initialize({
|
|
67
|
+
* edvUrl: 'https://edv.example.com',
|
|
68
|
+
* edvAuthKey: 'your-auth-key',
|
|
69
|
+
* masterKey: 'your-master-key',
|
|
70
|
+
* networkId: 'mainnet',
|
|
71
|
+
* databasePath: 'custom-wallet-db'
|
|
72
|
+
* });
|
|
73
|
+
*/
|
|
9
74
|
async function initialize({
|
|
10
75
|
edvUrl,
|
|
11
76
|
edvAuthKey,
|
|
12
|
-
networkId,
|
|
13
77
|
masterKey,
|
|
14
|
-
mnemonic
|
|
78
|
+
mnemonic,
|
|
79
|
+
networkId,
|
|
80
|
+
databasePath
|
|
15
81
|
}) {
|
|
16
82
|
|
|
83
|
+
// Validate required parameters
|
|
17
84
|
if (!masterKey && !mnemonic) {
|
|
18
|
-
throw new Error('
|
|
85
|
+
throw new Error('Initialization failed: Either masterKey or mnemonic must be provided for wallet access');
|
|
19
86
|
}
|
|
20
87
|
|
|
21
88
|
if (masterKey && mnemonic) {
|
|
22
|
-
throw new Error('
|
|
89
|
+
throw new Error('Initialization failed: Cannot provide both masterKey and mnemonic. Please use only one authentication method');
|
|
23
90
|
}
|
|
24
91
|
|
|
25
92
|
if (!edvUrl) {
|
|
26
|
-
throw new Error('
|
|
93
|
+
throw new Error('Initialization failed: edvUrl is required. Please provide a valid Encrypted Data Vault URL');
|
|
27
94
|
}
|
|
28
95
|
|
|
29
96
|
if (!edvAuthKey) {
|
|
30
|
-
throw new Error('
|
|
97
|
+
throw new Error('Initialization failed: edvAuthKey is required for EDV authentication');
|
|
31
98
|
}
|
|
32
99
|
|
|
33
|
-
if (
|
|
34
|
-
throw new Error('
|
|
100
|
+
if (networkId !== 'testnet' && networkId !== 'mainnet') {
|
|
101
|
+
throw new Error('Initialization failed: networkId is required. Must be either "testnet" or "mainnet"');
|
|
35
102
|
}
|
|
36
103
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
console.log('DID provider created');
|
|
104
|
+
// Initialize data store
|
|
105
|
+
let dataStore;
|
|
106
|
+
try {
|
|
107
|
+
dataStore = await createDataStore({
|
|
108
|
+
databasePath: databasePath || 'truvera-web-wallet',
|
|
109
|
+
defaultNetwork: networkId,
|
|
110
|
+
});
|
|
111
|
+
} catch (error) {
|
|
112
|
+
throw new Error(`Failed to create data store: ${error.message}`);
|
|
113
|
+
}
|
|
48
114
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
115
|
+
// Initialize wallet
|
|
116
|
+
let wallet;
|
|
117
|
+
try {
|
|
118
|
+
wallet = await createWallet({ dataStore });
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw new Error(`Failed to create wallet: ${error.message}`);
|
|
121
|
+
}
|
|
53
122
|
|
|
54
|
-
|
|
55
|
-
|
|
123
|
+
// Initialize providers
|
|
124
|
+
let didProvider, credentialProvider, messageProvider;
|
|
125
|
+
try {
|
|
126
|
+
didProvider = createDIDProvider({ wallet });
|
|
127
|
+
credentialProvider = await createCredentialProvider({ wallet });
|
|
128
|
+
messageProvider = createMessageProvider({ wallet, didProvider });
|
|
129
|
+
} catch (error) {
|
|
130
|
+
throw new Error(`Failed to initialize wallet providers: ${error.message}`);
|
|
131
|
+
}
|
|
56
132
|
|
|
133
|
+
// Recover or set master key
|
|
57
134
|
if (mnemonic) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
135
|
+
try {
|
|
136
|
+
masterKey = await recoverCloudWalletMasterKey(mnemonic);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
throw new Error(`Failed to recover master key from mnemonic: ${error.message}`);
|
|
139
|
+
}
|
|
63
140
|
}
|
|
64
141
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
142
|
+
// Initialize cloud wallet
|
|
143
|
+
let cloudWallet;
|
|
144
|
+
try {
|
|
145
|
+
cloudWallet = await initializeCloudWallet({
|
|
146
|
+
dataStore: dataStore,
|
|
147
|
+
edvUrl: edvUrl,
|
|
148
|
+
masterKey: masterKey,
|
|
149
|
+
authKey: edvAuthKey,
|
|
150
|
+
});
|
|
151
|
+
} catch (error) {
|
|
152
|
+
throw new Error(`Failed to initialize cloud wallet: ${error.message}`);
|
|
153
|
+
}
|
|
74
154
|
|
|
155
|
+
// Pull documents from cloud (non-critical operation)
|
|
75
156
|
try {
|
|
76
|
-
|
|
77
|
-
} catch (
|
|
78
|
-
console.
|
|
157
|
+
await cloudWallet.pullDocuments();
|
|
158
|
+
} catch (error) {
|
|
159
|
+
console.warn('Warning: Failed to pull documents from cloud wallet. You may need to sync manually.', error.message);
|
|
79
160
|
}
|
|
80
161
|
|
|
81
162
|
return {
|
|
82
|
-
// Simplified API for common use cases
|
|
83
163
|
wallet,
|
|
164
|
+
messageProvider,
|
|
165
|
+
cloudWallet,
|
|
166
|
+
didProvider,
|
|
167
|
+
credentialProvider,
|
|
84
168
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
169
|
+
* Retrieves all credentials stored in the wallet.
|
|
170
|
+
*
|
|
171
|
+
* @async
|
|
172
|
+
* @returns {Promise<Array>} Array of credential objects
|
|
173
|
+
* @throws {Error} When credential retrieval fails
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* const credentials = await wallet.getCredentials();
|
|
177
|
+
* console.log(`Found ${credentials.length} credentials`);
|
|
87
178
|
*/
|
|
88
179
|
getCredentials: async () => {
|
|
89
|
-
|
|
180
|
+
try {
|
|
181
|
+
return await credentialProvider.getCredentials();
|
|
182
|
+
} catch (error) {
|
|
183
|
+
throw new Error(`Failed to retrieve credentials: ${error.message}`);
|
|
184
|
+
}
|
|
90
185
|
},
|
|
186
|
+
|
|
91
187
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @
|
|
188
|
+
* Imports a credential using an OpenID4VC (OID4VCI) offer URI.
|
|
189
|
+
*
|
|
190
|
+
* @async
|
|
191
|
+
* @param {string} uri - The credential offer URI (e.g., openid-credential-offer://...)
|
|
192
|
+
* @returns {Promise<Object>} The imported credential object
|
|
193
|
+
* @throws {Error} When the URI is invalid or credential import fails
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* const credential = await wallet.addCredential('openid-credential-offer://...');
|
|
197
|
+
* console.log('Credential imported:', credential.id);
|
|
95
198
|
*/
|
|
96
199
|
addCredential: async (uri) => {
|
|
97
|
-
|
|
200
|
+
if (!uri || typeof uri !== 'string') {
|
|
201
|
+
throw new Error('Invalid credential offer URI: URI must be a non-empty string');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
return await credentialProvider.importCredentialFromURI({ uri, didProvider });
|
|
206
|
+
} catch (error) {
|
|
207
|
+
throw new Error(`Failed to import credential from URI: ${error.message}`);
|
|
208
|
+
}
|
|
98
209
|
},
|
|
210
|
+
|
|
99
211
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
212
|
+
* Retrieves the default DID (Decentralized Identifier) for the wallet.
|
|
213
|
+
*
|
|
214
|
+
* @async
|
|
215
|
+
* @returns {Promise<string>} The default DID string
|
|
216
|
+
* @throws {Error} When DID retrieval fails or no DID exists
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* const did = await wallet.getDID();
|
|
220
|
+
* console.log('Default DID:', did);
|
|
102
221
|
*/
|
|
103
222
|
getDID: async () => {
|
|
104
|
-
|
|
223
|
+
try {
|
|
224
|
+
return await didProvider.getDefaultDID();
|
|
225
|
+
} catch (error) {
|
|
226
|
+
throw new Error(`Failed to retrieve default DID: ${error.message}`);
|
|
227
|
+
}
|
|
105
228
|
},
|
|
106
229
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
230
|
+
* Creates and submits a verifiable presentation for the given credentials and proof request.
|
|
231
|
+
*
|
|
232
|
+
* This function processes a proof request from a verifier, selects the specified credentials,
|
|
233
|
+
* creates a presentation with the requested attributes, and submits it to the verifier.
|
|
234
|
+
*
|
|
235
|
+
* @async
|
|
236
|
+
* @param {Object} config - Configuration object for presentation submission
|
|
237
|
+
* @param {Array<Object>} config.credentials - Array of credential objects to include in the presentation
|
|
238
|
+
* @param {string} config.credentials[].id - The credential ID
|
|
239
|
+
* @param {Object} config.credentials[].attributesToReveal - Map of attribute names to reveal
|
|
240
|
+
* @param {Array<string>} config.credentials[].attributesToReveal - Array of attribute names to reveal from this credential
|
|
241
|
+
* @param {string} config.proofRequestUrl - URL to the proof request template from the verifier
|
|
242
|
+
*
|
|
243
|
+
* @returns {Promise<Object>} The submission response from the verifier
|
|
244
|
+
* @throws {Error} When credentials array is empty or invalid
|
|
245
|
+
* @throws {Error} When proofRequestUrl is invalid
|
|
246
|
+
* @throws {Error} When verification controller fails to start
|
|
247
|
+
* @throws {Error} When presentation creation fails
|
|
248
|
+
* @throws {Error} When presentation submission fails
|
|
249
|
+
*
|
|
112
250
|
* @example
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
251
|
+
* // Submit a presentation with selective disclosure
|
|
252
|
+
* const result = await wallet.submitPresentation({
|
|
253
|
+
* credentials: [
|
|
254
|
+
* {
|
|
255
|
+
* id: 'credential-123',
|
|
256
|
+
* attributesToReveal: ['name', 'dateOfBirth']
|
|
257
|
+
* },
|
|
258
|
+
* {
|
|
259
|
+
* id: 'credential-456',
|
|
260
|
+
* attributesToReveal: ['email']
|
|
261
|
+
* }
|
|
262
|
+
* ],
|
|
263
|
+
* proofRequestUrl: 'https://verifier.example.com/proof-request/abc123'
|
|
118
264
|
* });
|
|
119
|
-
* @returns
|
|
120
265
|
*/
|
|
121
266
|
submitPresentation: async ({
|
|
122
|
-
// Map of credential id with attributesToReveal object
|
|
123
267
|
credentials,
|
|
124
|
-
// Proof request URL should be a valid URL to a proof request template
|
|
125
268
|
proofRequestUrl,
|
|
126
269
|
}) => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
270
|
+
// Validate inputs
|
|
271
|
+
if (!credentials || !Array.isArray(credentials) || credentials.length === 0) {
|
|
272
|
+
throw new Error('Invalid credentials: Must provide a non-empty array of credentials');
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (!proofRequestUrl || typeof proofRequestUrl !== 'string') {
|
|
276
|
+
throw new Error('Invalid proofRequestUrl: Must be a valid URL string');
|
|
277
|
+
}
|
|
132
278
|
|
|
133
|
-
|
|
279
|
+
// Validate each credential
|
|
134
280
|
for (const credential of credentials) {
|
|
135
|
-
|
|
136
|
-
credential: credential
|
|
137
|
-
|
|
281
|
+
if (!credential.id) {
|
|
282
|
+
throw new Error('Invalid credential: Each credential must have an id property');
|
|
283
|
+
}
|
|
284
|
+
if (!credential.attributesToReveal) {
|
|
285
|
+
throw new Error(`Invalid credential ${credential.id}: Missing attributesToReveal property`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
let verificationController;
|
|
290
|
+
try {
|
|
291
|
+
verificationController = createVerificationController({
|
|
292
|
+
wallet,
|
|
293
|
+
credentialProvider,
|
|
294
|
+
didProvider,
|
|
138
295
|
});
|
|
296
|
+
} catch (error) {
|
|
297
|
+
throw new Error(`Failed to create verification controller: ${error.message}`);
|
|
139
298
|
}
|
|
140
|
-
const presentation = await verificationController.createPresentation();
|
|
141
299
|
|
|
142
|
-
|
|
300
|
+
try {
|
|
301
|
+
await verificationController.start({ template: proofRequestUrl });
|
|
302
|
+
} catch (error) {
|
|
303
|
+
throw new Error(`Failed to start verification with proof request: ${error.message}`);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Select credentials and attributes to reveal
|
|
307
|
+
try {
|
|
308
|
+
for (const credential of credentials) {
|
|
309
|
+
verificationController.selectedCredentials.set(credential.id, {
|
|
310
|
+
credential: credential,
|
|
311
|
+
attributesToReveal: credential.attributesToReveal,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
} catch (error) {
|
|
315
|
+
throw new Error(`Failed to select credentials: ${error.message}`);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
let presentation;
|
|
319
|
+
try {
|
|
320
|
+
presentation = await verificationController.createPresentation();
|
|
321
|
+
} catch (error) {
|
|
322
|
+
throw new Error(`Failed to create presentation: ${error.message}`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
try {
|
|
326
|
+
return await verificationController.submitPresentation(presentation);
|
|
327
|
+
} catch (error) {
|
|
328
|
+
throw new Error(`Failed to submit presentation: ${error.message}`);
|
|
329
|
+
}
|
|
143
330
|
}
|
|
144
331
|
}
|
|
145
332
|
|
|
146
333
|
}
|
|
147
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Dock Wallet SDK - Web Module
|
|
337
|
+
*
|
|
338
|
+
* Provides a comprehensive SDK for building decentralized identity wallets in web applications.
|
|
339
|
+
* Includes high-level initialization functions and low-level building blocks for custom implementations.
|
|
340
|
+
*
|
|
341
|
+
* @namespace DockWalletSDK
|
|
342
|
+
*
|
|
343
|
+
* @property {Function} initialize - High-level function to initialize a complete wallet with all providers
|
|
344
|
+
* @property {Function} createDataStore - Create a data store for wallet data persistence
|
|
345
|
+
* @property {Function} createWallet - Create a core wallet instance
|
|
346
|
+
* @property {Function} createCredentialProvider - Create a provider for credential management
|
|
347
|
+
* @property {Function} createDIDProvider - Create a provider for DID operations
|
|
348
|
+
* @property {Function} createMessageProvider - Create a provider for wallet messaging
|
|
349
|
+
* @property {Function} initializeCloudWallet - Initialize cloud wallet synchronization
|
|
350
|
+
* @property {Function} generateCloudWalletMasterKey - Generate a new master key for cloud wallet
|
|
351
|
+
* @property {Function} recoverCloudWalletMasterKey - Recover master key from a BIP39 mnemonic
|
|
352
|
+
* @property {Function} createVerificationController - Create a controller for verification presentations
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* // Quick start - use the high-level initialize function
|
|
356
|
+
* import WalletSDK from '@docknetwork/wallet-sdk-web';
|
|
357
|
+
*
|
|
358
|
+
* const wallet = await WalletSDK.initialize({
|
|
359
|
+
* edvUrl: 'https://edv.dock.io',
|
|
360
|
+
* edvAuthKey: 'your-auth-key',
|
|
361
|
+
* mnemonic: 'your mnemonic phrase...',
|
|
362
|
+
* networkId: 'testnet'
|
|
363
|
+
* });
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* // Advanced usage - build custom wallet with individual components
|
|
367
|
+
* import WalletSDK from '@docknetwork/wallet-sdk-web';
|
|
368
|
+
*
|
|
369
|
+
* const dataStore = await WalletSDK.createDataStore({
|
|
370
|
+
* databasePath: 'my-wallet',
|
|
371
|
+
* defaultNetwork: 'mainnet'
|
|
372
|
+
* });
|
|
373
|
+
*
|
|
374
|
+
* const wallet = await WalletSDK.createWallet({ dataStore });
|
|
375
|
+
* const didProvider = WalletSDK.createDIDProvider({ wallet });
|
|
376
|
+
* // ... configure additional providers as needed
|
|
377
|
+
*/
|
|
148
378
|
export default {
|
|
149
379
|
initialize,
|
|
150
380
|
createDataStore,
|