@docknetwork/wallet-sdk-wasm 1.5.14 → 1.7.6
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/generate-docs.js +49 -0
- package/jsdoc.conf.json +29 -6
- package/lib/index.js +9 -1
- package/lib/index.mjs +9 -1
- package/lib/modules/network-manager.js +15 -12
- package/lib/modules/network-manager.mjs +15 -12
- package/lib/rpc-server.js +11 -1
- package/lib/rpc-server.mjs +11 -1
- package/lib/services/blockchain/cached-did-resolver.js +113 -0
- package/lib/services/blockchain/cached-did-resolver.mjs +109 -0
- package/lib/services/blockchain/index.js +11 -0
- package/lib/services/blockchain/index.mjs +11 -0
- package/lib/services/blockchain/service-rpc.js +12 -0
- package/lib/services/blockchain/service-rpc.mjs +12 -0
- package/lib/services/blockchain/service.js +161 -19
- package/lib/services/blockchain/service.mjs +162 -20
- package/lib/services/credential/bbs-revocation.js +11 -0
- package/lib/services/credential/bbs-revocation.mjs +11 -0
- package/lib/services/credential/bound-check.js +1 -1
- package/lib/services/credential/bound-check.mjs +1 -1
- package/lib/services/credential/config.js +4 -1
- package/lib/services/credential/config.mjs +4 -1
- package/lib/services/credential/delegatable-credentials.js +300 -0
- package/lib/services/credential/delegatable-credentials.mjs +263 -0
- package/lib/services/credential/index.js +53 -0
- package/lib/services/credential/index.mjs +18 -0
- package/lib/services/credential/pex-helpers.js +4 -4
- package/lib/services/credential/pex-helpers.mjs +4 -4
- package/lib/services/credential/sd-jwt.js +214 -0
- package/lib/services/credential/sd-jwt.mjs +200 -0
- package/lib/services/credential/service-rpc.js +9 -0
- package/lib/services/credential/service-rpc.mjs +9 -0
- package/lib/services/credential/service.js +324 -7
- package/lib/services/credential/service.mjs +324 -7
- package/lib/services/edv/index.js +1 -0
- package/lib/services/edv/index.mjs +1 -0
- package/lib/services/edv/service-rpc.js +23 -0
- package/lib/services/edv/service-rpc.mjs +23 -0
- package/lib/services/edv/service.js +226 -2
- package/lib/services/edv/service.mjs +223 -3
- package/lib/services/index.js +14 -0
- package/lib/services/index.mjs +14 -0
- package/lib/services/pex/config.js +4 -0
- package/lib/services/pex/config.mjs +4 -0
- package/lib/services/pex/service-rpc.js +4 -0
- package/lib/services/pex/service-rpc.mjs +4 -0
- package/lib/services/pex/service.js +7 -0
- package/lib/services/pex/service.mjs +7 -0
- package/lib/services/relay-service/service.js +124 -1
- package/lib/services/relay-service/service.mjs +124 -1
- package/lib/services/rpc-service-client.js +0 -3
- package/lib/services/rpc-service-client.mjs +0 -3
- package/lib/services/storage/index.js +19 -2
- package/lib/services/storage/index.mjs +24 -1
- package/lib/services/storage/service-rpc.js +7 -3
- package/lib/services/storage/service-rpc.mjs +7 -3
- package/lib/services/storage/service.js +4 -0
- package/lib/services/storage/service.mjs +4 -0
- package/lib/setup-nodejs.js +9 -1
- package/lib/setup-nodejs.mjs +9 -1
- package/lib/setup-tests.js +9 -1
- package/lib/setup-tests.mjs +9 -1
- package/lib/src/modules/event-manager.d.ts +0 -1
- package/lib/src/modules/event-manager.d.ts.map +1 -1
- package/lib/src/modules/network-manager.d.ts +2 -4
- package/lib/src/modules/network-manager.d.ts.map +1 -1
- package/lib/src/services/blockchain/cached-did-resolver.d.ts +28 -0
- package/lib/src/services/blockchain/cached-did-resolver.d.ts.map +1 -0
- package/lib/src/services/blockchain/cached-did-resolver.test.d.ts +2 -0
- package/lib/src/services/blockchain/cached-did-resolver.test.d.ts.map +1 -0
- package/lib/src/services/blockchain/configs.d.ts +1 -2
- package/lib/src/services/blockchain/configs.d.ts.map +1 -1
- package/lib/src/services/blockchain/service.d.ts +117 -19
- package/lib/src/services/blockchain/service.d.ts.map +1 -1
- package/lib/src/services/credential/bbs-revocation.d.ts +1 -1
- package/lib/src/services/credential/bbs-revocation.d.ts.map +1 -1
- package/lib/src/services/credential/bound-check.d.ts.map +1 -1
- package/lib/src/services/credential/config.d.ts.map +1 -1
- package/lib/src/services/credential/delegatable-credentials.d.ts +272 -0
- package/lib/src/services/credential/delegatable-credentials.d.ts.map +1 -0
- package/lib/src/services/credential/index.d.ts +4 -0
- package/lib/src/services/credential/index.d.ts.map +1 -1
- package/lib/src/services/credential/pex-helpers.d.ts +2 -2
- package/lib/src/services/credential/pex-helpers.d.ts.map +1 -1
- package/lib/src/services/credential/sd-jwt.test.d.ts +2 -0
- package/lib/src/services/credential/sd-jwt.test.d.ts.map +1 -0
- package/lib/src/services/credential/service.d.ts +274 -4
- package/lib/src/services/credential/service.d.ts.map +1 -1
- package/lib/src/services/dids/keypair-utils.d.ts +2 -2
- package/lib/src/services/dids/keypair-utils.d.ts.map +1 -1
- package/lib/src/services/dids/service.d.ts +35 -3
- package/lib/src/services/dids/service.d.ts.map +1 -1
- package/lib/src/services/edv/service.d.ts +201 -2
- package/lib/src/services/edv/service.d.ts.map +1 -1
- package/lib/src/services/pex/config.d.ts +1 -0
- package/lib/src/services/pex/config.d.ts.map +1 -1
- package/lib/src/services/pex/service.d.ts +1 -0
- package/lib/src/services/pex/service.d.ts.map +1 -1
- package/lib/src/services/relay-service/service.d.ts +148 -8
- package/lib/src/services/relay-service/service.d.ts.map +1 -1
- package/lib/src/services/rpc-service-client.d.ts +2 -2
- package/lib/src/services/rpc-service-client.d.ts.map +1 -1
- package/lib/src/services/storage/index.d.ts +1 -1
- package/lib/src/services/storage/index.d.ts.map +1 -1
- package/lib/src/services/storage/service-rpc.d.ts +9 -0
- package/lib/src/services/storage/service-rpc.d.ts.map +1 -0
- package/lib/src/services/storage/service.d.ts +1 -0
- package/lib/src/services/storage/service.d.ts.map +1 -1
- package/lib/src/services/util-crypto/service.d.ts +2 -2
- package/lib/src/services/util-crypto/service.d.ts.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/wallet/rpc-storage-interface.js +13 -3
- package/lib/wallet/rpc-storage-interface.mjs +11 -1
- package/lib/wallet/rpc-storage-wallet.js +10 -0
- package/lib/wallet/rpc-storage-wallet.mjs +10 -0
- package/package.json +29 -16
- package/rollup.config.mjs +5 -3
- package/src/globals.d.ts +3 -0
- package/src/modules/network-manager.ts +15 -14
- package/src/services/blockchain/cached-did-resolver.test.ts +288 -0
- package/src/services/blockchain/cached-did-resolver.ts +126 -0
- package/src/services/blockchain/configs.ts +1 -2
- package/src/services/blockchain/service-rpc.js +12 -0
- package/src/services/blockchain/service.ts +167 -20
- package/src/services/credential/bound-check.ts +1 -1
- package/src/services/credential/config.ts +7 -1
- package/src/services/credential/delegatable-credentials.ts +409 -0
- package/src/services/credential/index.ts +16 -0
- package/src/services/credential/pex-helpers.js +4 -4
- package/src/services/credential/pex-helpers.test.js +2 -2
- package/src/services/credential/sd-jwt.test.ts +718 -0
- package/src/services/credential/sd-jwt.ts +231 -0
- package/src/services/credential/service-rpc.js +9 -0
- package/src/services/credential/service.ts +328 -7
- package/src/services/edv/index.test.js +229 -0
- package/src/services/edv/service-rpc.js +23 -0
- package/src/services/edv/service.ts +272 -1
- package/src/services/pex/config.ts +4 -0
- package/src/services/pex/service-rpc.js +4 -0
- package/src/services/pex/service.ts +13 -0
- package/src/services/pex/tests/pex-service.test.js +210 -0
- package/src/services/relay-service/service.ts +130 -1
- package/src/services/rpc-service-client.js +0 -3
- package/src/services/storage/index.js +15 -1
- package/src/services/storage/service-rpc.js +7 -3
- package/src/services/storage/service.ts +5 -0
|
@@ -13,19 +13,43 @@ var core_logger = require('../../core/logger.js');
|
|
|
13
13
|
var modules_eventManager = require('../../modules/event-manager.js');
|
|
14
14
|
var services_utilCrypto_service = require('../util-crypto/service.js');
|
|
15
15
|
var types = require('@docknetwork/credential-sdk/types');
|
|
16
|
+
var services_blockchain_cachedDidResolver = require('./cached-did-resolver.js');
|
|
16
17
|
require('assert');
|
|
17
18
|
require('@docknetwork/credential-sdk/utils');
|
|
18
19
|
require('@scure/bip39');
|
|
19
20
|
require('@scure/bip39/wordlists/english');
|
|
20
21
|
require('../util-crypto/configs.js');
|
|
22
|
+
require('../storage/index.js');
|
|
23
|
+
require('../storage/service.js');
|
|
24
|
+
require('../storage/service-rpc.js');
|
|
25
|
+
require('../rpc-service-client.js');
|
|
26
|
+
require('../../rpc-client.js');
|
|
27
|
+
require('json-rpc-2.0');
|
|
28
|
+
require('../../core/crypto.js');
|
|
29
|
+
require('crypto');
|
|
30
|
+
require('../../logger.js');
|
|
31
|
+
require('../../rpc-util.js');
|
|
21
32
|
|
|
22
33
|
// @ts-nocheck
|
|
34
|
+
/**
|
|
35
|
+
* Universal resolver URL for DID resolution fallback
|
|
36
|
+
* @constant {string}
|
|
37
|
+
*/
|
|
23
38
|
const universalResolverUrl = 'https://uniresolver.truvera.io';
|
|
39
|
+
/**
|
|
40
|
+
* Resolver that accepts any DID method using wildcard matching
|
|
41
|
+
* @class
|
|
42
|
+
* @extends ResolverRouter
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
24
45
|
class AnyDIDResolver extends resolver.ResolverRouter {
|
|
25
46
|
method = resolver.WILDCARD;
|
|
26
47
|
}
|
|
27
48
|
/**
|
|
28
|
-
*
|
|
49
|
+
* Main blockchain service class for managing blockchain connections and DID resolution
|
|
50
|
+
* @class
|
|
51
|
+
* @description Provides methods for connecting to Cheqd blockchain, resolving DIDs,
|
|
52
|
+
* and managing blockchain-related operations
|
|
29
53
|
*/
|
|
30
54
|
class BlockchainService {
|
|
31
55
|
dock;
|
|
@@ -33,7 +57,16 @@ class BlockchainService {
|
|
|
33
57
|
cheqdApi;
|
|
34
58
|
cheqdApiUrl;
|
|
35
59
|
isBlockchainReady = false;
|
|
36
|
-
|
|
60
|
+
_resolver;
|
|
61
|
+
get resolver() {
|
|
62
|
+
return this._resolver;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Event names emitted by the blockchain service
|
|
66
|
+
* @static
|
|
67
|
+
* @readonly
|
|
68
|
+
* @property {string} BLOCKCHAIN_READY - Emitted when blockchain connection is established
|
|
69
|
+
*/
|
|
37
70
|
static Events = {
|
|
38
71
|
BLOCKCHAIN_READY: 'blockchain-ready',
|
|
39
72
|
};
|
|
@@ -43,15 +76,33 @@ class BlockchainService {
|
|
|
43
76
|
BlockchainService.prototype.init,
|
|
44
77
|
BlockchainService.prototype.isApiConnected,
|
|
45
78
|
BlockchainService.prototype.getAddress,
|
|
79
|
+
BlockchainService.prototype.resolveDID,
|
|
80
|
+
BlockchainService.prototype.getCachedDIDs,
|
|
81
|
+
BlockchainService.prototype.clearCache,
|
|
82
|
+
BlockchainService.prototype.getCacheEntry,
|
|
46
83
|
];
|
|
84
|
+
/**
|
|
85
|
+
* Creates a new BlockchainService instance
|
|
86
|
+
* @constructor
|
|
87
|
+
*/
|
|
47
88
|
constructor() {
|
|
48
89
|
this.name = 'blockchain';
|
|
49
90
|
this.cheqdApi = new cheqdBlockchainApi.CheqdAPI();
|
|
50
91
|
this.cheqdModules = new cheqdBlockchainModules.CheqdCoreModules(this.cheqdApi);
|
|
51
92
|
this.modules = new modules.MultiApiCoreModules([this.cheqdModules]);
|
|
52
93
|
this.emitter = new events.EventEmitter();
|
|
53
|
-
|
|
94
|
+
// Blockchain is not ready yet, but we can use fallback resolvers
|
|
95
|
+
this._resolver = this.createDIDResolver();
|
|
54
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Gets the types and modules needed for DID or accumulator operations
|
|
99
|
+
* @param {string} didOrRegistryId - DID or registry identifier
|
|
100
|
+
* @returns {Object} Object containing accumulator-related types and modules
|
|
101
|
+
* @returns {typeof AccumulatorPublicKey} returns.PublicKey - Accumulator public key type
|
|
102
|
+
* @returns {typeof AccumulatorId} returns.AccumulatorId - Accumulator ID type
|
|
103
|
+
* @returns {typeof AccumulatorCommon} returns.AccumulatorCommon - Common accumulator type
|
|
104
|
+
* @returns {Object} returns.AccumulatorModule - Accumulator module instance
|
|
105
|
+
*/
|
|
55
106
|
getTypesForDIDOrAccumulator(didOrRegistryId) {
|
|
56
107
|
return {
|
|
57
108
|
PublicKey: types.AccumulatorPublicKey,
|
|
@@ -61,8 +112,11 @@ class BlockchainService {
|
|
|
61
112
|
};
|
|
62
113
|
}
|
|
63
114
|
/**
|
|
64
|
-
*
|
|
65
|
-
* @returns
|
|
115
|
+
* Ensures the blockchain connection is ready before proceeding
|
|
116
|
+
* @returns {Promise<void>} Resolves when blockchain is ready
|
|
117
|
+
* @example
|
|
118
|
+
* await blockchainService.ensureBlockchainReady();
|
|
119
|
+
* // Blockchain is now connected and ready
|
|
66
120
|
*/
|
|
67
121
|
async ensureBlockchainReady() {
|
|
68
122
|
if (await this.isApiConnected()) {
|
|
@@ -70,17 +124,62 @@ class BlockchainService {
|
|
|
70
124
|
}
|
|
71
125
|
return modules_eventManager.once(this.emitter, BlockchainService.Events.BLOCKCHAIN_READY);
|
|
72
126
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Gets the cached DIDs
|
|
129
|
+
* @returns {Promise<string[]>} Cached DIDs
|
|
130
|
+
*/
|
|
131
|
+
getCachedDIDs() {
|
|
132
|
+
return this.resolver.getCachedDIDs();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Gets the cached DID resolution data
|
|
136
|
+
* @param {string} did - The DID to get the cache entry for
|
|
137
|
+
* @returns {Promise<any>} Cached DID resolution data
|
|
138
|
+
*/
|
|
139
|
+
getCacheEntry(did) {
|
|
140
|
+
return this.resolver.getCacheEntry(did);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Clears cached data for a specific DID
|
|
144
|
+
* @param {string} did - The DID to clear from cache
|
|
145
|
+
* @returns {void}
|
|
146
|
+
*/
|
|
147
|
+
clearCache(did) {
|
|
148
|
+
return this.resolver.clearCache(did);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Creates a DID resolver with caching support
|
|
152
|
+
* @private
|
|
153
|
+
* @returns {CachedDIDResolver} Cached DID resolver instance
|
|
154
|
+
*/
|
|
155
|
+
createDIDResolver(isBlockchainReady) {
|
|
156
|
+
let resolvers = [
|
|
77
157
|
new resolver.UniversalResolver(universalResolverUrl),
|
|
78
|
-
|
|
158
|
+
new resolver.DIDKeyResolver(),
|
|
159
|
+
];
|
|
160
|
+
// Add blockchain resolvers if the blockchain is ready
|
|
161
|
+
if (isBlockchainReady) {
|
|
162
|
+
resolvers = [
|
|
163
|
+
new resolver.CoreResolver(this.modules),
|
|
164
|
+
...resolvers
|
|
165
|
+
];
|
|
166
|
+
}
|
|
167
|
+
const router = new AnyDIDResolver(resolvers);
|
|
168
|
+
return new services_blockchain_cachedDidResolver.CachedDIDResolver(router);
|
|
79
169
|
}
|
|
80
170
|
/**
|
|
81
|
-
*
|
|
82
|
-
* @param {
|
|
83
|
-
* @
|
|
171
|
+
* Initializes the blockchain service with connection parameters
|
|
172
|
+
* @param {InitParams} params - Initialization parameters
|
|
173
|
+
* @param {string} params.cheqdApiUrl - URL of the Cheqd API endpoint
|
|
174
|
+
* @param {string} [params.networkId] - Cheqd network identifier
|
|
175
|
+
* @param {string} [params.cheqdMnemonic] - Mnemonic for Cheqd wallet (auto-generated if not provided)
|
|
176
|
+
* @returns {Promise<boolean>} True if initialization successful
|
|
177
|
+
* @throws {Error} If cheqdApiUrl is not provided
|
|
178
|
+
* @example
|
|
179
|
+
* await blockchainService.init({
|
|
180
|
+
* cheqdApiUrl: 'https://api.cheqd.network',
|
|
181
|
+
* networkId: 'mainnet'
|
|
182
|
+
* });
|
|
84
183
|
*/
|
|
85
184
|
async init(params) {
|
|
86
185
|
if (!params?.cheqdApiUrl) {
|
|
@@ -105,15 +204,17 @@ class BlockchainService {
|
|
|
105
204
|
try {
|
|
106
205
|
await this.cheqdApi.init({
|
|
107
206
|
wallet,
|
|
108
|
-
|
|
207
|
+
urls: Array.isArray(checkdApiUrl) ? checkdApiUrl : [checkdApiUrl],
|
|
109
208
|
network: cheqdNetworkId,
|
|
110
209
|
});
|
|
111
210
|
core_logger.Logger.info(`Cheqd initialized at: ${checkdApiUrl}`);
|
|
112
211
|
}
|
|
113
212
|
catch (err) {
|
|
213
|
+
debugger;
|
|
114
214
|
core_logger.Logger.error(`Failed to initialize cheqd at: ${checkdApiUrl}`, err);
|
|
115
215
|
}
|
|
116
|
-
|
|
216
|
+
// Re-create the resolver with the new blockchain connection
|
|
217
|
+
this._resolver = this.createDIDResolver(true);
|
|
117
218
|
if (process.env.NODE_ENV !== 'test' ||
|
|
118
219
|
process.env.API_MOCK_DISABLED === 'true') {
|
|
119
220
|
await index.initializeWasm();
|
|
@@ -122,8 +223,10 @@ class BlockchainService {
|
|
|
122
223
|
return true;
|
|
123
224
|
}
|
|
124
225
|
/**
|
|
125
|
-
*
|
|
126
|
-
* @returns
|
|
226
|
+
* Disconnects from the blockchain
|
|
227
|
+
* @returns {Promise<void>} Resolves when disconnection is complete
|
|
228
|
+
* @example
|
|
229
|
+
* await blockchainService.disconnect();
|
|
127
230
|
*/
|
|
128
231
|
async disconnect() {
|
|
129
232
|
let result;
|
|
@@ -133,6 +236,11 @@ class BlockchainService {
|
|
|
133
236
|
this._setBlockchainReady(false);
|
|
134
237
|
return result;
|
|
135
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Waits for the blockchain to be ready
|
|
241
|
+
* @returns {Promise<void>} Resolves when blockchain is ready
|
|
242
|
+
* @private
|
|
243
|
+
*/
|
|
136
244
|
async waitBlockchainReady() {
|
|
137
245
|
return new Promise(resolve => {
|
|
138
246
|
if (this.isBlockchainReady) {
|
|
@@ -143,19 +251,39 @@ class BlockchainService {
|
|
|
143
251
|
}
|
|
144
252
|
});
|
|
145
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* Resolves a DID to its document
|
|
256
|
+
* @param {string} did - The DID to resolve
|
|
257
|
+
* @returns {Promise<Object>} The resolved DID document
|
|
258
|
+
* @example
|
|
259
|
+
* const didDoc = await blockchainService.resolveDID('did:key:z6Mk...');
|
|
260
|
+
*/
|
|
146
261
|
async resolveDID(did) {
|
|
147
262
|
return this.resolver.resolve(did);
|
|
148
263
|
}
|
|
149
264
|
/**
|
|
150
|
-
*
|
|
151
|
-
* @returns
|
|
265
|
+
* Checks if the blockchain API is connected
|
|
266
|
+
* @returns {Promise<boolean>} True if connected, false otherwise
|
|
267
|
+
* @example
|
|
268
|
+
* const isConnected = await blockchainService.isApiConnected();
|
|
152
269
|
*/
|
|
153
270
|
async isApiConnected() {
|
|
154
271
|
return this.cheqdApi.isInitialized();
|
|
155
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Gets the current Cheqd API URL
|
|
275
|
+
* @returns {Promise<string>} The Cheqd API URL
|
|
276
|
+
* @example
|
|
277
|
+
* const apiUrl = await blockchainService.getAddress();
|
|
278
|
+
*/
|
|
156
279
|
async getAddress() {
|
|
157
280
|
return this.cheqdApiUrl;
|
|
158
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Sets the blockchain ready state and emits events
|
|
284
|
+
* @private
|
|
285
|
+
* @param {boolean} isBlockchainReady - Whether blockchain is ready
|
|
286
|
+
*/
|
|
159
287
|
_setBlockchainReady(isBlockchainReady) {
|
|
160
288
|
this.isBlockchainReady = isBlockchainReady;
|
|
161
289
|
if (isBlockchainReady) {
|
|
@@ -163,6 +291,20 @@ class BlockchainService {
|
|
|
163
291
|
}
|
|
164
292
|
}
|
|
165
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Singleton instance of the blockchain service
|
|
296
|
+
* @type {BlockchainService}
|
|
297
|
+
* @example
|
|
298
|
+
* import { blockchainService } from '@docknetwork/wallet-sdk-wasm/services/blockchain';
|
|
299
|
+
*
|
|
300
|
+
* // Initialize the service
|
|
301
|
+
* await blockchainService.init({
|
|
302
|
+
* cheqdApiUrl: 'https://api.cheqd.network'
|
|
303
|
+
* });
|
|
304
|
+
*
|
|
305
|
+
* // Resolve a DID
|
|
306
|
+
* const didDoc = await blockchainService.resolveDID('did:key:z6Mk...');
|
|
307
|
+
*/
|
|
166
308
|
const blockchainService = new BlockchainService();
|
|
167
309
|
|
|
168
310
|
exports.BlockchainService = BlockchainService;
|
|
@@ -2,26 +2,50 @@ import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
|
|
|
2
2
|
import { CheqdAPI } from '@docknetwork/cheqd-blockchain-api';
|
|
3
3
|
import { CheqdCoreModules } from '@docknetwork/cheqd-blockchain-modules';
|
|
4
4
|
import { MultiApiCoreModules } from '@docknetwork/credential-sdk/modules';
|
|
5
|
-
import { DIDKeyResolver, CoreResolver,
|
|
5
|
+
import { UniversalResolver, DIDKeyResolver, CoreResolver, ResolverRouter, WILDCARD } from '@docknetwork/credential-sdk/resolver';
|
|
6
6
|
import { initializeWasm } from '@docknetwork/crypto-wasm-ts/lib/index';
|
|
7
7
|
import { EventEmitter } from 'events';
|
|
8
8
|
import { Logger } from '../../core/logger.mjs';
|
|
9
9
|
import { once } from '../../modules/event-manager.mjs';
|
|
10
10
|
import { utilCryptoService } from '../util-crypto/service.mjs';
|
|
11
11
|
import { AccumulatorPublicKey, AccumulatorId, AccumulatorCommon } from '@docknetwork/credential-sdk/types';
|
|
12
|
+
import { CachedDIDResolver } from './cached-did-resolver.mjs';
|
|
12
13
|
import 'assert';
|
|
13
14
|
import '@docknetwork/credential-sdk/utils';
|
|
14
15
|
import '@scure/bip39';
|
|
15
16
|
import '@scure/bip39/wordlists/english';
|
|
16
17
|
import '../util-crypto/configs.mjs';
|
|
18
|
+
import '../storage/index.mjs';
|
|
19
|
+
import '../storage/service.mjs';
|
|
20
|
+
import '../storage/service-rpc.mjs';
|
|
21
|
+
import '../rpc-service-client.mjs';
|
|
22
|
+
import '../../rpc-client.mjs';
|
|
23
|
+
import 'json-rpc-2.0';
|
|
24
|
+
import '../../core/crypto.mjs';
|
|
25
|
+
import 'crypto';
|
|
26
|
+
import '../../logger.mjs';
|
|
27
|
+
import '../../rpc-util.mjs';
|
|
17
28
|
|
|
18
29
|
// @ts-nocheck
|
|
30
|
+
/**
|
|
31
|
+
* Universal resolver URL for DID resolution fallback
|
|
32
|
+
* @constant {string}
|
|
33
|
+
*/
|
|
19
34
|
const universalResolverUrl = 'https://uniresolver.truvera.io';
|
|
35
|
+
/**
|
|
36
|
+
* Resolver that accepts any DID method using wildcard matching
|
|
37
|
+
* @class
|
|
38
|
+
* @extends ResolverRouter
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
20
41
|
class AnyDIDResolver extends ResolverRouter {
|
|
21
42
|
method = WILDCARD;
|
|
22
43
|
}
|
|
23
44
|
/**
|
|
24
|
-
*
|
|
45
|
+
* Main blockchain service class for managing blockchain connections and DID resolution
|
|
46
|
+
* @class
|
|
47
|
+
* @description Provides methods for connecting to Cheqd blockchain, resolving DIDs,
|
|
48
|
+
* and managing blockchain-related operations
|
|
25
49
|
*/
|
|
26
50
|
class BlockchainService {
|
|
27
51
|
dock;
|
|
@@ -29,7 +53,16 @@ class BlockchainService {
|
|
|
29
53
|
cheqdApi;
|
|
30
54
|
cheqdApiUrl;
|
|
31
55
|
isBlockchainReady = false;
|
|
32
|
-
|
|
56
|
+
_resolver;
|
|
57
|
+
get resolver() {
|
|
58
|
+
return this._resolver;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Event names emitted by the blockchain service
|
|
62
|
+
* @static
|
|
63
|
+
* @readonly
|
|
64
|
+
* @property {string} BLOCKCHAIN_READY - Emitted when blockchain connection is established
|
|
65
|
+
*/
|
|
33
66
|
static Events = {
|
|
34
67
|
BLOCKCHAIN_READY: 'blockchain-ready',
|
|
35
68
|
};
|
|
@@ -39,15 +72,33 @@ class BlockchainService {
|
|
|
39
72
|
BlockchainService.prototype.init,
|
|
40
73
|
BlockchainService.prototype.isApiConnected,
|
|
41
74
|
BlockchainService.prototype.getAddress,
|
|
75
|
+
BlockchainService.prototype.resolveDID,
|
|
76
|
+
BlockchainService.prototype.getCachedDIDs,
|
|
77
|
+
BlockchainService.prototype.clearCache,
|
|
78
|
+
BlockchainService.prototype.getCacheEntry,
|
|
42
79
|
];
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new BlockchainService instance
|
|
82
|
+
* @constructor
|
|
83
|
+
*/
|
|
43
84
|
constructor() {
|
|
44
85
|
this.name = 'blockchain';
|
|
45
86
|
this.cheqdApi = new CheqdAPI();
|
|
46
87
|
this.cheqdModules = new CheqdCoreModules(this.cheqdApi);
|
|
47
88
|
this.modules = new MultiApiCoreModules([this.cheqdModules]);
|
|
48
89
|
this.emitter = new EventEmitter();
|
|
49
|
-
|
|
90
|
+
// Blockchain is not ready yet, but we can use fallback resolvers
|
|
91
|
+
this._resolver = this.createDIDResolver();
|
|
50
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Gets the types and modules needed for DID or accumulator operations
|
|
95
|
+
* @param {string} didOrRegistryId - DID or registry identifier
|
|
96
|
+
* @returns {Object} Object containing accumulator-related types and modules
|
|
97
|
+
* @returns {typeof AccumulatorPublicKey} returns.PublicKey - Accumulator public key type
|
|
98
|
+
* @returns {typeof AccumulatorId} returns.AccumulatorId - Accumulator ID type
|
|
99
|
+
* @returns {typeof AccumulatorCommon} returns.AccumulatorCommon - Common accumulator type
|
|
100
|
+
* @returns {Object} returns.AccumulatorModule - Accumulator module instance
|
|
101
|
+
*/
|
|
51
102
|
getTypesForDIDOrAccumulator(didOrRegistryId) {
|
|
52
103
|
return {
|
|
53
104
|
PublicKey: AccumulatorPublicKey,
|
|
@@ -57,8 +108,11 @@ class BlockchainService {
|
|
|
57
108
|
};
|
|
58
109
|
}
|
|
59
110
|
/**
|
|
60
|
-
*
|
|
61
|
-
* @returns
|
|
111
|
+
* Ensures the blockchain connection is ready before proceeding
|
|
112
|
+
* @returns {Promise<void>} Resolves when blockchain is ready
|
|
113
|
+
* @example
|
|
114
|
+
* await blockchainService.ensureBlockchainReady();
|
|
115
|
+
* // Blockchain is now connected and ready
|
|
62
116
|
*/
|
|
63
117
|
async ensureBlockchainReady() {
|
|
64
118
|
if (await this.isApiConnected()) {
|
|
@@ -66,17 +120,62 @@ class BlockchainService {
|
|
|
66
120
|
}
|
|
67
121
|
return once(this.emitter, BlockchainService.Events.BLOCKCHAIN_READY);
|
|
68
122
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Gets the cached DIDs
|
|
125
|
+
* @returns {Promise<string[]>} Cached DIDs
|
|
126
|
+
*/
|
|
127
|
+
getCachedDIDs() {
|
|
128
|
+
return this.resolver.getCachedDIDs();
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Gets the cached DID resolution data
|
|
132
|
+
* @param {string} did - The DID to get the cache entry for
|
|
133
|
+
* @returns {Promise<any>} Cached DID resolution data
|
|
134
|
+
*/
|
|
135
|
+
getCacheEntry(did) {
|
|
136
|
+
return this.resolver.getCacheEntry(did);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Clears cached data for a specific DID
|
|
140
|
+
* @param {string} did - The DID to clear from cache
|
|
141
|
+
* @returns {void}
|
|
142
|
+
*/
|
|
143
|
+
clearCache(did) {
|
|
144
|
+
return this.resolver.clearCache(did);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Creates a DID resolver with caching support
|
|
148
|
+
* @private
|
|
149
|
+
* @returns {CachedDIDResolver} Cached DID resolver instance
|
|
150
|
+
*/
|
|
151
|
+
createDIDResolver(isBlockchainReady) {
|
|
152
|
+
let resolvers = [
|
|
73
153
|
new UniversalResolver(universalResolverUrl),
|
|
74
|
-
|
|
154
|
+
new DIDKeyResolver(),
|
|
155
|
+
];
|
|
156
|
+
// Add blockchain resolvers if the blockchain is ready
|
|
157
|
+
if (isBlockchainReady) {
|
|
158
|
+
resolvers = [
|
|
159
|
+
new CoreResolver(this.modules),
|
|
160
|
+
...resolvers
|
|
161
|
+
];
|
|
162
|
+
}
|
|
163
|
+
const router = new AnyDIDResolver(resolvers);
|
|
164
|
+
return new CachedDIDResolver(router);
|
|
75
165
|
}
|
|
76
166
|
/**
|
|
77
|
-
*
|
|
78
|
-
* @param {
|
|
79
|
-
* @
|
|
167
|
+
* Initializes the blockchain service with connection parameters
|
|
168
|
+
* @param {InitParams} params - Initialization parameters
|
|
169
|
+
* @param {string} params.cheqdApiUrl - URL of the Cheqd API endpoint
|
|
170
|
+
* @param {string} [params.networkId] - Cheqd network identifier
|
|
171
|
+
* @param {string} [params.cheqdMnemonic] - Mnemonic for Cheqd wallet (auto-generated if not provided)
|
|
172
|
+
* @returns {Promise<boolean>} True if initialization successful
|
|
173
|
+
* @throws {Error} If cheqdApiUrl is not provided
|
|
174
|
+
* @example
|
|
175
|
+
* await blockchainService.init({
|
|
176
|
+
* cheqdApiUrl: 'https://api.cheqd.network',
|
|
177
|
+
* networkId: 'mainnet'
|
|
178
|
+
* });
|
|
80
179
|
*/
|
|
81
180
|
async init(params) {
|
|
82
181
|
if (!params?.cheqdApiUrl) {
|
|
@@ -101,15 +200,17 @@ class BlockchainService {
|
|
|
101
200
|
try {
|
|
102
201
|
await this.cheqdApi.init({
|
|
103
202
|
wallet,
|
|
104
|
-
|
|
203
|
+
urls: Array.isArray(checkdApiUrl) ? checkdApiUrl : [checkdApiUrl],
|
|
105
204
|
network: cheqdNetworkId,
|
|
106
205
|
});
|
|
107
206
|
Logger.info(`Cheqd initialized at: ${checkdApiUrl}`);
|
|
108
207
|
}
|
|
109
208
|
catch (err) {
|
|
209
|
+
debugger;
|
|
110
210
|
Logger.error(`Failed to initialize cheqd at: ${checkdApiUrl}`, err);
|
|
111
211
|
}
|
|
112
|
-
|
|
212
|
+
// Re-create the resolver with the new blockchain connection
|
|
213
|
+
this._resolver = this.createDIDResolver(true);
|
|
113
214
|
if (process.env.NODE_ENV !== 'test' ||
|
|
114
215
|
process.env.API_MOCK_DISABLED === 'true') {
|
|
115
216
|
await initializeWasm();
|
|
@@ -118,8 +219,10 @@ class BlockchainService {
|
|
|
118
219
|
return true;
|
|
119
220
|
}
|
|
120
221
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @returns
|
|
222
|
+
* Disconnects from the blockchain
|
|
223
|
+
* @returns {Promise<void>} Resolves when disconnection is complete
|
|
224
|
+
* @example
|
|
225
|
+
* await blockchainService.disconnect();
|
|
123
226
|
*/
|
|
124
227
|
async disconnect() {
|
|
125
228
|
let result;
|
|
@@ -129,6 +232,11 @@ class BlockchainService {
|
|
|
129
232
|
this._setBlockchainReady(false);
|
|
130
233
|
return result;
|
|
131
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Waits for the blockchain to be ready
|
|
237
|
+
* @returns {Promise<void>} Resolves when blockchain is ready
|
|
238
|
+
* @private
|
|
239
|
+
*/
|
|
132
240
|
async waitBlockchainReady() {
|
|
133
241
|
return new Promise(resolve => {
|
|
134
242
|
if (this.isBlockchainReady) {
|
|
@@ -139,19 +247,39 @@ class BlockchainService {
|
|
|
139
247
|
}
|
|
140
248
|
});
|
|
141
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* Resolves a DID to its document
|
|
252
|
+
* @param {string} did - The DID to resolve
|
|
253
|
+
* @returns {Promise<Object>} The resolved DID document
|
|
254
|
+
* @example
|
|
255
|
+
* const didDoc = await blockchainService.resolveDID('did:key:z6Mk...');
|
|
256
|
+
*/
|
|
142
257
|
async resolveDID(did) {
|
|
143
258
|
return this.resolver.resolve(did);
|
|
144
259
|
}
|
|
145
260
|
/**
|
|
146
|
-
*
|
|
147
|
-
* @returns
|
|
261
|
+
* Checks if the blockchain API is connected
|
|
262
|
+
* @returns {Promise<boolean>} True if connected, false otherwise
|
|
263
|
+
* @example
|
|
264
|
+
* const isConnected = await blockchainService.isApiConnected();
|
|
148
265
|
*/
|
|
149
266
|
async isApiConnected() {
|
|
150
267
|
return this.cheqdApi.isInitialized();
|
|
151
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Gets the current Cheqd API URL
|
|
271
|
+
* @returns {Promise<string>} The Cheqd API URL
|
|
272
|
+
* @example
|
|
273
|
+
* const apiUrl = await blockchainService.getAddress();
|
|
274
|
+
*/
|
|
152
275
|
async getAddress() {
|
|
153
276
|
return this.cheqdApiUrl;
|
|
154
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Sets the blockchain ready state and emits events
|
|
280
|
+
* @private
|
|
281
|
+
* @param {boolean} isBlockchainReady - Whether blockchain is ready
|
|
282
|
+
*/
|
|
155
283
|
_setBlockchainReady(isBlockchainReady) {
|
|
156
284
|
this.isBlockchainReady = isBlockchainReady;
|
|
157
285
|
if (isBlockchainReady) {
|
|
@@ -159,6 +287,20 @@ class BlockchainService {
|
|
|
159
287
|
}
|
|
160
288
|
}
|
|
161
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Singleton instance of the blockchain service
|
|
292
|
+
* @type {BlockchainService}
|
|
293
|
+
* @example
|
|
294
|
+
* import { blockchainService } from '@docknetwork/wallet-sdk-wasm/services/blockchain';
|
|
295
|
+
*
|
|
296
|
+
* // Initialize the service
|
|
297
|
+
* await blockchainService.init({
|
|
298
|
+
* cheqdApiUrl: 'https://api.cheqd.network'
|
|
299
|
+
* });
|
|
300
|
+
*
|
|
301
|
+
* // Resolve a DID
|
|
302
|
+
* const didDoc = await blockchainService.resolveDID('did:key:z6Mk...');
|
|
303
|
+
*/
|
|
162
304
|
const blockchainService = new BlockchainService();
|
|
163
305
|
|
|
164
306
|
export { BlockchainService, blockchainService, universalResolverUrl };
|
|
@@ -20,6 +20,17 @@ require('@scure/bip39');
|
|
|
20
20
|
require('@scure/bip39/wordlists/english');
|
|
21
21
|
require('../util-crypto/configs.js');
|
|
22
22
|
require('@docknetwork/credential-sdk/types');
|
|
23
|
+
require('../blockchain/cached-did-resolver.js');
|
|
24
|
+
require('../storage/index.js');
|
|
25
|
+
require('../storage/service.js');
|
|
26
|
+
require('../storage/service-rpc.js');
|
|
27
|
+
require('../rpc-service-client.js');
|
|
28
|
+
require('../../rpc-client.js');
|
|
29
|
+
require('json-rpc-2.0');
|
|
30
|
+
require('../../core/crypto.js');
|
|
31
|
+
require('crypto');
|
|
32
|
+
require('../../logger.js');
|
|
33
|
+
require('../../rpc-util.js');
|
|
23
34
|
|
|
24
35
|
const getWitnessDetails = async (credential, _membershipWitness) => {
|
|
25
36
|
let witness = _membershipWitness;
|
|
@@ -16,6 +16,17 @@ import '@scure/bip39';
|
|
|
16
16
|
import '@scure/bip39/wordlists/english';
|
|
17
17
|
import '../util-crypto/configs.mjs';
|
|
18
18
|
import '@docknetwork/credential-sdk/types';
|
|
19
|
+
import '../blockchain/cached-did-resolver.mjs';
|
|
20
|
+
import '../storage/index.mjs';
|
|
21
|
+
import '../storage/service.mjs';
|
|
22
|
+
import '../storage/service-rpc.mjs';
|
|
23
|
+
import '../rpc-service-client.mjs';
|
|
24
|
+
import '../../rpc-client.mjs';
|
|
25
|
+
import 'json-rpc-2.0';
|
|
26
|
+
import '../../core/crypto.mjs';
|
|
27
|
+
import 'crypto';
|
|
28
|
+
import '../../logger.mjs';
|
|
29
|
+
import '../../rpc-util.mjs';
|
|
19
30
|
|
|
20
31
|
const getWitnessDetails = async (credential, _membershipWitness) => {
|
|
21
32
|
let witness = _membershipWitness;
|
|
@@ -48,7 +48,7 @@ async function fetchBlobFromUrl(url) {
|
|
|
48
48
|
}
|
|
49
49
|
function blobFromBase64(base64String) {
|
|
50
50
|
const cleanedBase64 = base64String.replace(/^data:application\/octet-stream;base64,/, '');
|
|
51
|
-
return base64url__default["default"].toBuffer(cleanedBase64);
|
|
51
|
+
return new Uint8Array(base64url__default["default"].toBuffer(cleanedBase64));
|
|
52
52
|
}
|
|
53
53
|
async function isBase64OrDataUrl(str) {
|
|
54
54
|
return ((await services_utilCrypto_service.utilCryptoService.isBase64(str)) ||
|
|
@@ -40,7 +40,7 @@ async function fetchBlobFromUrl(url) {
|
|
|
40
40
|
}
|
|
41
41
|
function blobFromBase64(base64String) {
|
|
42
42
|
const cleanedBase64 = base64String.replace(/^data:application\/octet-stream;base64,/, '');
|
|
43
|
-
return base64url.toBuffer(cleanedBase64);
|
|
43
|
+
return new Uint8Array(base64url.toBuffer(cleanedBase64));
|
|
44
44
|
}
|
|
45
45
|
async function isBase64OrDataUrl(str) {
|
|
46
46
|
return ((await utilCryptoService.isBase64(str)) ||
|
|
@@ -20,7 +20,10 @@ const validation = {
|
|
|
20
20
|
},
|
|
21
21
|
verifyCredential: params => {
|
|
22
22
|
const { credential } = params;
|
|
23
|
-
assert__default["default"](typeof credential === 'object', 'invalid credential');
|
|
23
|
+
assert__default["default"](typeof credential === 'object' || typeof credential === 'string', 'invalid credential');
|
|
24
|
+
if (typeof credential === 'object') {
|
|
25
|
+
assert__default["default"](Object.keys(credential).length > 0, 'invalid credential');
|
|
26
|
+
}
|
|
24
27
|
},
|
|
25
28
|
createBBSPresentation: params => {
|
|
26
29
|
const { credentials } = params;
|
|
@@ -12,7 +12,10 @@ const validation = {
|
|
|
12
12
|
},
|
|
13
13
|
verifyCredential: params => {
|
|
14
14
|
const { credential } = params;
|
|
15
|
-
assert(typeof credential === 'object', 'invalid credential');
|
|
15
|
+
assert(typeof credential === 'object' || typeof credential === 'string', 'invalid credential');
|
|
16
|
+
if (typeof credential === 'object') {
|
|
17
|
+
assert(Object.keys(credential).length > 0, 'invalid credential');
|
|
18
|
+
}
|
|
16
19
|
},
|
|
17
20
|
createBBSPresentation: params => {
|
|
18
21
|
const { credentials } = params;
|