@docknetwork/wallet-sdk-web 0.0.7 → 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.
@@ -560559,140 +560559,386 @@ zoo`.split('\n');
560559
560559
  };
560560
560560
  }
560561
560561
 
560562
+ /**
560563
+ * @fileoverview Dock Wallet SDK for Web
560564
+ *
560565
+ * This module provides a simplified interface for initializing and working with
560566
+ * the Dock Wallet SDK in web environments. It includes functions for managing
560567
+ * DIDs, credentials, presentations, and cloud wallet synchronization.
560568
+ *
560569
+ * @module @docknetwork/wallet-sdk-web
560570
+ */
560571
+
560572
+
560573
+ /**
560574
+ * Initializes the Dock Wallet SDK with the provided configuration.
560575
+ *
560576
+ * This function sets up all necessary wallet providers and establishes a connection
560577
+ * to the cloud wallet for synchronization. It requires either a master key or mnemonic
560578
+ * for wallet recovery, but not both.
560579
+ *
560580
+ * @async
560581
+ * @param {Object} config - Configuration object for wallet initialization
560582
+ * @param {string} config.edvUrl - The Encrypted Data Vault (EDV) URL for cloud storage
560583
+ * @param {string} config.edvAuthKey - Authentication key for accessing the EDV
560584
+ * @param {string} [config.masterKey] - Master key for wallet access (required if mnemonic is not provided)
560585
+ * @param {string} [config.mnemonic] - BIP39 mnemonic for wallet recovery (required if masterKey is not provided)
560586
+ * @param {string} config.networkId - Network identifier ('testnet' or 'mainnet')
560587
+ * @param {string} [config.databasePath='truvera-web-wallet'] - Optional path for the local database
560588
+ *
560589
+ * @returns {Promise<Object>} Initialized wallet object with the following properties:
560590
+ * @returns {Object} returns.wallet - Core wallet instance
560591
+ * @returns {Object} returns.messageProvider - Provider for handling wallet messages
560592
+ * @returns {Object} returns.cloudWallet - Cloud wallet instance for synchronization
560593
+ * @returns {Object} returns.didProvider - Provider for DID operations
560594
+ * @returns {Object} returns.credentialProvider - Provider for credential operations
560595
+ * @returns {Function} returns.getCredentials - Function to retrieve all credentials
560596
+ * @returns {Function} returns.addCredential - Function to import a credential from an offer URI
560597
+ * @returns {Function} returns.getDID - Function to get the default DID
560598
+ * @returns {Function} returns.submitPresentation - Function to create and submit a presentation
560599
+ *
560600
+ * @throws {Error} When neither masterKey nor mnemonic is provided
560601
+ * @throws {Error} When both masterKey and mnemonic are provided
560602
+ * @throws {Error} When edvUrl is not provided
560603
+ * @throws {Error} When edvAuthKey is not provided
560604
+ * @throws {Error} When networkId is not provided
560605
+ * @throws {Error} When networkId is not 'testnet' or 'mainnet'
560606
+ * @throws {Error} When wallet initialization fails
560607
+ * @throws {Error} When cloud wallet initialization fails
560608
+ *
560609
+ * @example
560610
+ * // Initialize with mnemonic
560611
+ * const wallet = await initialize({
560612
+ * edvUrl: 'https://edv.example.com',
560613
+ * edvAuthKey: 'your-auth-key',
560614
+ * mnemonic: 'your twelve word mnemonic phrase here...',
560615
+ * networkId: 'testnet'
560616
+ * });
560617
+ *
560618
+ * @example
560619
+ * // Initialize with master key
560620
+ * const wallet = await initialize({
560621
+ * edvUrl: 'https://edv.example.com',
560622
+ * edvAuthKey: 'your-auth-key',
560623
+ * masterKey: 'your-master-key',
560624
+ * networkId: 'mainnet',
560625
+ * databasePath: 'custom-wallet-db'
560626
+ * });
560627
+ */
560562
560628
  async function initialize(_ref) {
560563
560629
  let {
560564
560630
  edvUrl,
560565
560631
  edvAuthKey,
560566
- networkId,
560567
560632
  masterKey,
560568
- mnemonic
560633
+ mnemonic,
560634
+ networkId,
560635
+ databasePath
560569
560636
  } = _ref;
560637
+ // Validate required parameters
560570
560638
  if (!masterKey && !mnemonic) {
560571
- throw new Error('Master key or mnemonic is required');
560639
+ throw new Error('Initialization failed: Either masterKey or mnemonic must be provided for wallet access');
560572
560640
  }
560573
560641
  if (masterKey && mnemonic) {
560574
- throw new Error('Master key and mnemonic cannot be used together');
560642
+ throw new Error('Initialization failed: Cannot provide both masterKey and mnemonic. Please use only one authentication method');
560575
560643
  }
560576
560644
  if (!edvUrl) {
560577
- throw new Error('EDV URL is required');
560645
+ throw new Error('Initialization failed: edvUrl is required. Please provide a valid Encrypted Data Vault URL');
560646
+ }
560647
+ if (typeof edvUrl !== 'string' || !edvUrl.trim()) {
560648
+ throw new Error('Initialization failed: edvUrl must be a non-empty string');
560578
560649
  }
560579
560650
  if (!edvAuthKey) {
560580
- throw new Error('EDV Auth Key is required');
560651
+ throw new Error('Initialization failed: edvAuthKey is required for EDV authentication');
560652
+ }
560653
+ if (typeof edvAuthKey !== 'string' || !edvAuthKey.trim()) {
560654
+ throw new Error('Initialization failed: edvAuthKey must be a non-empty string');
560581
560655
  }
560582
560656
  if (!networkId) {
560583
- throw new Error('Network ID is required');
560657
+ throw new Error('Initialization failed: networkId is required. Must be either "testnet" or "mainnet"');
560584
560658
  }
560585
- const dataStore = await createDataStore({
560586
- databasePath: 'dock-wallet',
560587
- defaultNetwork: networkId || 'testnet'
560588
- });
560589
- console.log('Data store created');
560590
- const wallet = await createWallet({
560591
- dataStore
560592
- });
560593
- console.log('Wallet created');
560594
- const didProvider = createDIDProvider({
560595
- wallet
560596
- });
560597
- console.log('DID provider created');
560598
- const credentialProvider = await createCredentialProvider({
560599
- wallet
560600
- });
560601
- console.log('Credential provider created');
560602
- createMessageProvider({
560603
- wallet,
560604
- didProvider
560605
- });
560606
- console.log('Message provider created');
560659
+ if (networkId !== 'testnet' && networkId !== 'mainnet') {
560660
+ throw new Error("Initialization failed: Invalid networkId \"".concat(networkId, "\". Must be either \"testnet\" or \"mainnet\""));
560661
+ }
560662
+
560663
+ // Initialize data store
560664
+ let dataStore;
560665
+ try {
560666
+ dataStore = await createDataStore({
560667
+ databasePath: databasePath || 'truvera-web-wallet',
560668
+ defaultNetwork: networkId
560669
+ });
560670
+ } catch (error) {
560671
+ throw new Error("Failed to create data store: ".concat(error.message));
560672
+ }
560673
+
560674
+ // Initialize wallet
560675
+ let wallet;
560676
+ try {
560677
+ wallet = await createWallet({
560678
+ dataStore
560679
+ });
560680
+ } catch (error) {
560681
+ throw new Error("Failed to create wallet: ".concat(error.message));
560682
+ }
560683
+
560684
+ // Initialize providers
560685
+ let didProvider, credentialProvider, messageProvider;
560686
+ try {
560687
+ didProvider = createDIDProvider({
560688
+ wallet
560689
+ });
560690
+ credentialProvider = await createCredentialProvider({
560691
+ wallet
560692
+ });
560693
+ messageProvider = createMessageProvider({
560694
+ wallet,
560695
+ didProvider
560696
+ });
560697
+ } catch (error) {
560698
+ throw new Error("Failed to initialize wallet providers: ".concat(error.message));
560699
+ }
560700
+
560701
+ // Recover or set master key
560607
560702
  if (mnemonic) {
560608
- masterKey = await recoverCloudWalletMasterKey(mnemonic);
560609
- } else if (masterKey) {
560610
- masterKey = masterKey;
560611
- } else {
560612
- throw new Error('Master key or mnemonic is required');
560703
+ try {
560704
+ masterKey = await recoverCloudWalletMasterKey(mnemonic);
560705
+ } catch (error) {
560706
+ throw new Error("Failed to recover master key from mnemonic: ".concat(error.message));
560707
+ }
560613
560708
  }
560614
- const cloudWallet = await initializeCloudWallet({
560615
- dataStore: dataStore,
560616
- edvUrl: edvUrl,
560617
- masterKey: masterKey,
560618
- authKey: edvAuthKey
560619
- });
560620
- console.log('Cloud wallet created');
560621
- console.log(credentialProvider);
560709
+
560710
+ // Initialize cloud wallet
560711
+ let cloudWallet;
560622
560712
  try {
560623
- const documents = await cloudWallet.pullDocuments();
560624
- } catch (err) {
560625
- console.error('Error pulling documents', err);
560713
+ cloudWallet = await initializeCloudWallet({
560714
+ dataStore: dataStore,
560715
+ edvUrl: edvUrl,
560716
+ masterKey: masterKey,
560717
+ authKey: edvAuthKey
560718
+ });
560719
+ } catch (error) {
560720
+ throw new Error("Failed to initialize cloud wallet: ".concat(error.message));
560721
+ }
560722
+
560723
+ // Pull documents from cloud (non-critical operation)
560724
+ try {
560725
+ await cloudWallet.pullDocuments();
560726
+ } catch (error) {
560727
+ console.warn('Warning: Failed to pull documents from cloud wallet. You may need to sync manually.', error.message);
560626
560728
  }
560627
560729
  return {
560628
- // Simplified API for common use cases
560629
560730
  wallet,
560731
+ messageProvider,
560732
+ cloudWallet,
560733
+ didProvider,
560734
+ credentialProvider,
560630
560735
  /**
560631
- * Get the list of credentials
560632
- * @returns
560736
+ * Retrieves all credentials stored in the wallet.
560737
+ *
560738
+ * @async
560739
+ * @returns {Promise<Array>} Array of credential objects
560740
+ * @throws {Error} When credential retrieval fails
560741
+ *
560742
+ * @example
560743
+ * const credentials = await wallet.getCredentials();
560744
+ * console.log(`Found ${credentials.length} credentials`);
560633
560745
  */
560634
560746
  getCredentials: async () => {
560635
- return await credentialProvider.getCredentials();
560747
+ try {
560748
+ return await credentialProvider.getCredentials();
560749
+ } catch (error) {
560750
+ throw new Error("Failed to retrieve credentials: ".concat(error.message));
560751
+ }
560636
560752
  },
560637
560753
  /**
560638
- * Import credential using an offer URI
560639
- * @param {*} uri
560640
- * @returns
560754
+ * Imports a credential using an OpenID4VC (OID4VCI) offer URI.
560755
+ *
560756
+ * @async
560757
+ * @param {string} uri - The credential offer URI (e.g., openid-credential-offer://...)
560758
+ * @returns {Promise<Object>} The imported credential object
560759
+ * @throws {Error} When the URI is invalid or credential import fails
560760
+ *
560761
+ * @example
560762
+ * const credential = await wallet.addCredential('openid-credential-offer://...');
560763
+ * console.log('Credential imported:', credential.id);
560641
560764
  */
560642
560765
  addCredential: async uri => {
560643
- return await credentialProvider.importCredentialFromURI({
560644
- uri,
560645
- didProvider
560646
- });
560766
+ if (!uri || typeof uri !== 'string') {
560767
+ throw new Error('Invalid credential offer URI: URI must be a non-empty string');
560768
+ }
560769
+ try {
560770
+ return await credentialProvider.importCredentialFromURI({
560771
+ uri,
560772
+ didProvider
560773
+ });
560774
+ } catch (error) {
560775
+ throw new Error("Failed to import credential from URI: ".concat(error.message));
560776
+ }
560647
560777
  },
560648
560778
  /**
560649
- * Get default DID
560650
- * @returns
560779
+ * Retrieves the default DID (Decentralized Identifier) for the wallet.
560780
+ *
560781
+ * @async
560782
+ * @returns {Promise<string>} The default DID string
560783
+ * @throws {Error} When DID retrieval fails or no DID exists
560784
+ *
560785
+ * @example
560786
+ * const did = await wallet.getDID();
560787
+ * console.log('Default DID:', did);
560651
560788
  */
560652
560789
  getDID: async () => {
560653
- return await didProvider.getDefaultDID();
560790
+ try {
560791
+ return await didProvider.getDefaultDID();
560792
+ } catch (error) {
560793
+ throw new Error("Failed to retrieve default DID: ".concat(error.message));
560794
+ }
560654
560795
  },
560655
560796
  /**
560656
- * Submit a presentation for the given credentials and proof request URL
560657
- * @param {*} param0
560658
- * @param {Object} param0.credentials - Map of credential id with attributesToReveal object
560659
- * @param {string} param0.proofRequestUrl - Proof request URL should be a valid URL to a proof request template
560660
- *
560797
+ * Creates and submits a verifiable presentation for the given credentials and proof request.
560798
+ *
560799
+ * This function processes a proof request from a verifier, selects the specified credentials,
560800
+ * creates a presentation with the requested attributes, and submits it to the verifier.
560801
+ *
560802
+ * @async
560803
+ * @param {Object} config - Configuration object for presentation submission
560804
+ * @param {Array<Object>} config.credentials - Array of credential objects to include in the presentation
560805
+ * @param {string} config.credentials[].id - The credential ID
560806
+ * @param {Object} config.credentials[].attributesToReveal - Map of attribute names to reveal
560807
+ * @param {Array<string>} config.credentials[].attributesToReveal - Array of attribute names to reveal from this credential
560808
+ * @param {string} config.proofRequestUrl - URL to the proof request template from the verifier
560809
+ *
560810
+ * @returns {Promise<Object>} The submission response from the verifier
560811
+ * @throws {Error} When credentials array is empty or invalid
560812
+ * @throws {Error} When proofRequestUrl is invalid
560813
+ * @throws {Error} When verification controller fails to start
560814
+ * @throws {Error} When presentation creation fails
560815
+ * @throws {Error} When presentation submission fails
560816
+ *
560661
560817
  * @example
560662
- * const presentation = await cloudWallet.createPresentation({
560663
- * credentials: {
560664
- * 'credentialId': { attributesToReveal: ['attribute1', 'attribute2'] },
560665
- * },
560666
- * proofRequestUrl: 'https://example.com/proof-request',
560818
+ * // Submit a presentation with selective disclosure
560819
+ * const result = await wallet.submitPresentation({
560820
+ * credentials: [
560821
+ * {
560822
+ * id: 'credential-123',
560823
+ * attributesToReveal: ['name', 'dateOfBirth']
560824
+ * },
560825
+ * {
560826
+ * id: 'credential-456',
560827
+ * attributesToReveal: ['email']
560828
+ * }
560829
+ * ],
560830
+ * proofRequestUrl: 'https://verifier.example.com/proof-request/abc123'
560667
560831
  * });
560668
- * @returns
560669
560832
  */
560670
560833
  submitPresentation: async _ref2 => {
560671
560834
  let {
560672
- // Map of credential id with attributesToReveal object
560673
560835
  credentials,
560674
- // Proof request URL should be a valid URL to a proof request template
560675
560836
  proofRequestUrl
560676
560837
  } = _ref2;
560677
- const verificationController = createVerificationController({
560678
- wallet,
560679
- credentialProvider,
560680
- didProvider
560681
- });
560682
- await verificationController.start({
560683
- template: proofRequestUrl
560684
- });
560838
+ // Validate inputs
560839
+ if (!credentials || !Array.isArray(credentials) || credentials.length === 0) {
560840
+ throw new Error('Invalid credentials: Must provide a non-empty array of credentials');
560841
+ }
560842
+ if (!proofRequestUrl || typeof proofRequestUrl !== 'string') {
560843
+ throw new Error('Invalid proofRequestUrl: Must be a valid URL string');
560844
+ }
560845
+
560846
+ // Validate each credential
560685
560847
  for (const credential of credentials) {
560686
- verificationController.selectedCredentials.set(credential.id, {
560687
- credential: credential,
560688
- attributesToReveal: credentials[credential.id].attributesToReveal
560848
+ if (!credential.id) {
560849
+ throw new Error('Invalid credential: Each credential must have an id property');
560850
+ }
560851
+ if (!credential.attributesToReveal) {
560852
+ throw new Error("Invalid credential ".concat(credential.id, ": Missing attributesToReveal property"));
560853
+ }
560854
+ }
560855
+ let verificationController;
560856
+ try {
560857
+ verificationController = createVerificationController({
560858
+ wallet,
560859
+ credentialProvider,
560860
+ didProvider
560689
560861
  });
560862
+ } catch (error) {
560863
+ throw new Error("Failed to create verification controller: ".concat(error.message));
560864
+ }
560865
+ try {
560866
+ await verificationController.start({
560867
+ template: proofRequestUrl
560868
+ });
560869
+ } catch (error) {
560870
+ throw new Error("Failed to start verification with proof request: ".concat(error.message));
560871
+ }
560872
+
560873
+ // Select credentials and attributes to reveal
560874
+ try {
560875
+ for (const credential of credentials) {
560876
+ verificationController.selectedCredentials.set(credential.id, {
560877
+ credential: credential,
560878
+ attributesToReveal: credential.attributesToReveal
560879
+ });
560880
+ }
560881
+ } catch (error) {
560882
+ throw new Error("Failed to select credentials: ".concat(error.message));
560883
+ }
560884
+ let presentation;
560885
+ try {
560886
+ presentation = await verificationController.createPresentation();
560887
+ } catch (error) {
560888
+ throw new Error("Failed to create presentation: ".concat(error.message));
560889
+ }
560890
+ try {
560891
+ return await verificationController.submitPresentation(presentation);
560892
+ } catch (error) {
560893
+ throw new Error("Failed to submit presentation: ".concat(error.message));
560690
560894
  }
560691
- const presentation = await verificationController.createPresentation();
560692
- return await verificationController.submitPresentation(presentation);
560693
560895
  }
560694
560896
  };
560695
560897
  }
560898
+
560899
+ /**
560900
+ * Dock Wallet SDK - Web Module
560901
+ *
560902
+ * Provides a comprehensive SDK for building decentralized identity wallets in web applications.
560903
+ * Includes high-level initialization functions and low-level building blocks for custom implementations.
560904
+ *
560905
+ * @namespace DockWalletSDK
560906
+ *
560907
+ * @property {Function} initialize - High-level function to initialize a complete wallet with all providers
560908
+ * @property {Function} createDataStore - Create a data store for wallet data persistence
560909
+ * @property {Function} createWallet - Create a core wallet instance
560910
+ * @property {Function} createCredentialProvider - Create a provider for credential management
560911
+ * @property {Function} createDIDProvider - Create a provider for DID operations
560912
+ * @property {Function} createMessageProvider - Create a provider for wallet messaging
560913
+ * @property {Function} initializeCloudWallet - Initialize cloud wallet synchronization
560914
+ * @property {Function} generateCloudWalletMasterKey - Generate a new master key for cloud wallet
560915
+ * @property {Function} recoverCloudWalletMasterKey - Recover master key from a BIP39 mnemonic
560916
+ * @property {Function} createVerificationController - Create a controller for verification presentations
560917
+ *
560918
+ * @example
560919
+ * // Quick start - use the high-level initialize function
560920
+ * import WalletSDK from '@docknetwork/wallet-sdk-web';
560921
+ *
560922
+ * const wallet = await WalletSDK.initialize({
560923
+ * edvUrl: 'https://edv.dock.io',
560924
+ * edvAuthKey: 'your-auth-key',
560925
+ * mnemonic: 'your mnemonic phrase...',
560926
+ * networkId: 'testnet'
560927
+ * });
560928
+ *
560929
+ * @example
560930
+ * // Advanced usage - build custom wallet with individual components
560931
+ * import WalletSDK from '@docknetwork/wallet-sdk-web';
560932
+ *
560933
+ * const dataStore = await WalletSDK.createDataStore({
560934
+ * databasePath: 'my-wallet',
560935
+ * defaultNetwork: 'mainnet'
560936
+ * });
560937
+ *
560938
+ * const wallet = await WalletSDK.createWallet({ dataStore });
560939
+ * const didProvider = WalletSDK.createDIDProvider({ wallet });
560940
+ * // ... configure additional providers as needed
560941
+ */
560696
560942
  var index = {
560697
560943
  initialize,
560698
560944
  createDataStore,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docknetwork/wallet-sdk-web",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "license": "https://github.com/docknetwork/wallet-sdk/LICENSE",
5
5
  "repository": {
6
6
  "type": "git",