@docknetwork/wallet-sdk-wasm 1.4.0 → 1.5.0

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.
Files changed (190) hide show
  1. package/lib/{account-3c88155a.js → account-bf00e5c0.js} +5 -5
  2. package/lib/{account-8f5cbc4d.js → account-e48dc9b6.js} +5 -5
  3. package/lib/core/polkadot-utils.js +18 -4
  4. package/lib/core/polkadot-utils.mjs +15 -2
  5. package/lib/index.js +14 -17
  6. package/lib/index.mjs +14 -17
  7. package/lib/modules/account.js +9 -5
  8. package/lib/modules/account.mjs +9 -5
  9. package/lib/modules/accounts.js +9 -5
  10. package/lib/modules/accounts.mjs +9 -5
  11. package/lib/modules/network-manager.js +4 -2
  12. package/lib/modules/network-manager.mjs +4 -2
  13. package/lib/modules/wallet.js +12 -8
  14. package/lib/modules/wallet.mjs +12 -8
  15. package/lib/rpc-server.js +14 -17
  16. package/lib/rpc-server.mjs +14 -17
  17. package/lib/services/blockchain/configs.js +7 -0
  18. package/lib/services/blockchain/configs.mjs +3 -0
  19. package/lib/services/{dock → blockchain}/index.android.js +3 -4
  20. package/lib/services/{dock → blockchain}/index.android.mjs +3 -4
  21. package/lib/services/{dock → blockchain}/index.ios.js +3 -4
  22. package/lib/services/{dock → blockchain}/index.ios.mjs +3 -4
  23. package/lib/services/blockchain/index.js +30 -0
  24. package/lib/services/blockchain/index.mjs +22 -0
  25. package/lib/services/{dock → blockchain}/service-rpc.js +5 -7
  26. package/lib/services/{dock → blockchain}/service-rpc.mjs +5 -7
  27. package/lib/services/blockchain/service.js +212 -0
  28. package/lib/services/blockchain/service.mjs +206 -0
  29. package/lib/services/credential/bbs-revocation.js +33 -61
  30. package/lib/services/credential/bbs-revocation.mjs +34 -62
  31. package/lib/services/credential/bound-check.js +0 -8
  32. package/lib/services/credential/bound-check.mjs +1 -5
  33. package/lib/services/credential/index.js +20 -23
  34. package/lib/services/credential/index.mjs +20 -23
  35. package/lib/services/credential/pex-helpers.js +134 -34
  36. package/lib/services/credential/pex-helpers.mjs +133 -35
  37. package/lib/services/credential/service.js +54 -55
  38. package/lib/services/credential/service.mjs +43 -41
  39. package/lib/services/credential/utils.js +12 -18
  40. package/lib/services/credential/utils.mjs +11 -14
  41. package/lib/services/dids/config.js +8 -0
  42. package/lib/services/dids/config.mjs +8 -0
  43. package/lib/services/dids/index.js +8 -7
  44. package/lib/services/dids/index.mjs +8 -7
  45. package/lib/services/dids/service-rpc.js +2 -2
  46. package/lib/services/dids/service-rpc.mjs +2 -2
  47. package/lib/services/dids/service.js +22 -32
  48. package/lib/services/dids/service.mjs +22 -32
  49. package/lib/services/edv/hmac.js +5 -0
  50. package/lib/services/edv/hmac.mjs +5 -0
  51. package/lib/services/edv/index.js +10 -8
  52. package/lib/services/edv/index.mjs +10 -8
  53. package/lib/services/edv/service-rpc.js +4 -0
  54. package/lib/services/edv/service-rpc.mjs +4 -0
  55. package/lib/services/edv/service.js +26 -10
  56. package/lib/services/edv/service.mjs +25 -10
  57. package/lib/services/example/service.js +3 -1
  58. package/lib/services/example/service.mjs +3 -1
  59. package/lib/services/index.js +12 -15
  60. package/lib/services/index.mjs +12 -15
  61. package/lib/services/keyring/configs.js +12 -0
  62. package/lib/services/keyring/configs.mjs +12 -0
  63. package/lib/services/keyring/service.js +5 -0
  64. package/lib/services/keyring/service.mjs +5 -0
  65. package/lib/services/pex/service.js +17 -15
  66. package/lib/services/pex/service.mjs +17 -15
  67. package/lib/services/substrate/api-utils.js +18 -6
  68. package/lib/services/substrate/api-utils.mjs +18 -6
  69. package/lib/services/substrate/index.js +13 -6
  70. package/lib/services/substrate/index.mjs +13 -6
  71. package/lib/services/substrate/service.js +16 -9
  72. package/lib/services/substrate/service.mjs +16 -9
  73. package/lib/services/test-utils.js +27 -18
  74. package/lib/services/test-utils.mjs +27 -18
  75. package/lib/services/trust-registry/index.js +16 -5
  76. package/lib/services/trust-registry/index.mjs +16 -5
  77. package/lib/services/trust-registry/service.js +26 -10
  78. package/lib/services/trust-registry/service.mjs +26 -10
  79. package/lib/services/util-crypto/configs.js +3 -0
  80. package/lib/services/util-crypto/configs.mjs +3 -0
  81. package/lib/services/util-crypto/service-rpc.js +6 -0
  82. package/lib/services/util-crypto/service-rpc.mjs +6 -0
  83. package/lib/services/util-crypto/service.js +5 -0
  84. package/lib/services/util-crypto/service.mjs +6 -1
  85. package/lib/setup-nodejs.js +14 -17
  86. package/lib/setup-nodejs.mjs +14 -17
  87. package/lib/setup-tests.js +14 -17
  88. package/lib/setup-tests.mjs +14 -17
  89. package/lib/src/core/polkadot-utils.d.ts +1 -0
  90. package/lib/src/core/polkadot-utils.d.ts.map +1 -1
  91. package/lib/src/modules/network-manager.d.ts +1 -0
  92. package/lib/src/modules/network-manager.d.ts.map +1 -1
  93. package/lib/src/services/blockchain/configs.d.ts +8 -0
  94. package/lib/src/services/blockchain/configs.d.ts.map +1 -0
  95. package/lib/src/services/blockchain/index.d.ts +2 -0
  96. package/lib/src/services/blockchain/index.d.ts.map +1 -0
  97. package/lib/src/services/{dock → blockchain}/service.d.ts +12 -11
  98. package/lib/src/services/blockchain/service.d.ts.map +1 -0
  99. package/lib/src/services/credential/bbs-revocation.d.ts.map +1 -1
  100. package/lib/src/services/credential/bound-check.d.ts +0 -4
  101. package/lib/src/services/credential/bound-check.d.ts.map +1 -1
  102. package/lib/src/services/credential/index.d.ts +2 -7
  103. package/lib/src/services/credential/index.d.ts.map +1 -1
  104. package/lib/src/services/credential/pex-helpers.d.ts +5 -3
  105. package/lib/src/services/credential/pex-helpers.d.ts.map +1 -1
  106. package/lib/src/services/credential/service.d.ts +2 -7
  107. package/lib/src/services/credential/service.d.ts.map +1 -1
  108. package/lib/src/services/credential/utils.d.ts.map +1 -1
  109. package/lib/src/services/dids/config.d.ts +5 -0
  110. package/lib/src/services/dids/config.d.ts.map +1 -1
  111. package/lib/src/services/dids/service-rpc.d.ts +1 -1
  112. package/lib/src/services/dids/service-rpc.d.ts.map +1 -1
  113. package/lib/src/services/dids/service.d.ts +9 -7
  114. package/lib/src/services/dids/service.d.ts.map +1 -1
  115. package/lib/src/services/edv/hmac.d.ts +1 -0
  116. package/lib/src/services/edv/hmac.d.ts.map +1 -1
  117. package/lib/src/services/edv/service.d.ts +5 -0
  118. package/lib/src/services/edv/service.d.ts.map +1 -1
  119. package/lib/src/services/example/service.d.ts.map +1 -1
  120. package/lib/src/services/keyring/configs.d.ts +6 -0
  121. package/lib/src/services/keyring/configs.d.ts.map +1 -1
  122. package/lib/src/services/pex/service.d.ts.map +1 -1
  123. package/lib/src/services/trust-registry/service.d.ts.map +1 -1
  124. package/lib/src/services/util-crypto/configs.d.ts +1 -0
  125. package/lib/src/services/util-crypto/configs.d.ts.map +1 -1
  126. package/lib/src/services/util-crypto/service.d.ts +1 -0
  127. package/lib/src/services/util-crypto/service.d.ts.map +1 -1
  128. package/lib/test/setup-test-state.js +10 -6
  129. package/lib/test/setup-test-state.mjs +10 -6
  130. package/lib/tsconfig.tsbuildinfo +1 -1
  131. package/package.json +5 -7
  132. package/src/core/polkadot-utils.js +14 -3
  133. package/src/modules/network-manager.ts +5 -2
  134. package/src/modules/wallet.ts +5 -5
  135. package/src/services/blockchain/configs.ts +11 -0
  136. package/src/services/blockchain/index.android.js +3 -0
  137. package/src/services/blockchain/index.ios.js +3 -0
  138. package/src/services/blockchain/index.js +1 -0
  139. package/src/services/blockchain/index.test.js +34 -0
  140. package/src/services/{dock → blockchain}/service-rpc.js +5 -6
  141. package/src/services/blockchain/service.ts +261 -0
  142. package/src/services/credential/bbs-revocation.ts +42 -90
  143. package/src/services/credential/bound-check.test.ts +2 -5
  144. package/src/services/credential/bound-check.ts +0 -5
  145. package/src/services/credential/index.test.js +148 -7
  146. package/src/services/credential/pex-helpers.js +132 -34
  147. package/src/services/credential/pex-helpers.test.js +284 -27
  148. package/src/services/credential/service.ts +44 -27
  149. package/src/services/credential/utils.js +5 -14
  150. package/src/services/dids/config.ts +15 -0
  151. package/src/services/dids/index.test.js +5 -46
  152. package/src/services/dids/service-rpc.ts +2 -2
  153. package/src/services/dids/service.ts +17 -34
  154. package/src/services/edv/hmac.ts +13 -0
  155. package/src/services/edv/index.test.js +22 -0
  156. package/src/services/edv/service-rpc.js +4 -0
  157. package/src/services/edv/service.ts +22 -0
  158. package/src/services/example/service.ts +3 -1
  159. package/src/services/index.js +2 -2
  160. package/src/services/keyring/configs.ts +21 -0
  161. package/src/services/keyring/service.ts +13 -0
  162. package/src/services/pex/service.ts +29 -18
  163. package/src/services/pex/tests/pex-service.test.js +163 -6
  164. package/src/services/substrate/api-utils.test.js +7 -7
  165. package/src/services/substrate/api-utils.ts +3 -3
  166. package/src/services/substrate/service.ts +6 -6
  167. package/src/services/test-utils.js +13 -11
  168. package/src/services/trust-registry/index.test.js +6 -4
  169. package/src/services/trust-registry/service.ts +13 -7
  170. package/src/services/util-crypto/configs.ts +4 -0
  171. package/src/services/util-crypto/service-rpc.js +6 -0
  172. package/src/services/util-crypto/service.ts +8 -0
  173. package/src/test/setup-test-state.js +2 -2
  174. package/lib/services/dock/configs.js +0 -18
  175. package/lib/services/dock/configs.mjs +0 -10
  176. package/lib/services/dock/index.js +0 -18
  177. package/lib/services/dock/index.mjs +0 -10
  178. package/lib/services/dock/service.js +0 -136
  179. package/lib/services/dock/service.mjs +0 -128
  180. package/lib/src/services/dock/configs.d.ts +0 -7
  181. package/lib/src/services/dock/configs.d.ts.map +0 -1
  182. package/lib/src/services/dock/index.d.ts +0 -2
  183. package/lib/src/services/dock/index.d.ts.map +0 -1
  184. package/lib/src/services/dock/service.d.ts.map +0 -1
  185. package/src/services/dock/configs.ts +0 -12
  186. package/src/services/dock/index.android.js +0 -3
  187. package/src/services/dock/index.ios.js +0 -3
  188. package/src/services/dock/index.js +0 -1
  189. package/src/services/dock/index.test.js +0 -48
  190. package/src/services/dock/service.ts +0 -159
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docknetwork/wallet-sdk-wasm",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "license": "https://github.com/docknetwork/react-native-sdk/LICENSE",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,6 @@
15
15
  "resolutions": {
16
16
  "@polkadot/wasm-crypto": "6.3.1",
17
17
  "@polkadot/rpc-core": "9.7.1",
18
- "@docknetwork/sdk": "8.6.0",
19
18
  "@polkadot/api": "9.7.1",
20
19
  "@polkadot/keyring": "10.1.11",
21
20
  "@polkadot/rpc-provider": "9.7.1",
@@ -27,7 +26,7 @@
27
26
  "dependencies": {
28
27
  "@astronautlabs/jsonpath": "^1.1.2",
29
28
  "@docknetwork/universal-wallet": "^2.0.1",
30
- "@docknetwork/wallet-sdk-dids": "^1.4.0",
29
+ "@docknetwork/wallet-sdk-dids": "^1.5.0",
31
30
  "@polkadot/api": "9.7.1",
32
31
  "@polkadot/keyring": "10.1.11",
33
32
  "@polkadot/rpc-core": "9.7.1",
@@ -38,7 +37,8 @@
38
37
  "@polkadot/util": "10.1.11",
39
38
  "@polkadot/util-crypto": "10.1.11",
40
39
  "@polkadot/wasm-crypto": "6.3.1",
41
- "@docknetwork/sdk": "8.6.0",
40
+ "@docknetwork/credential-sdk": "0.44.0",
41
+ "@docknetwork/dock-blockchain-modules": "0.26.2",
42
42
  "@sphereon/oid4vci-client": "^0.16.0",
43
43
  "@sphereon/oid4vci-common": "^0.16.0",
44
44
  "axios": "^0.25.0",
@@ -51,9 +51,7 @@
51
51
  "cwait": "1.1.2",
52
52
  "axios-retry": "^3.2.5",
53
53
  "@sphereon/pex": "^1.1.4",
54
- "@docknetwork/credential-sdk": "0.12.0",
55
- "@docknetwork/dock-blockchain-api": "0.6.1",
56
- "@docknetwork/dock-blockchain-modules": "0.8.0"
54
+ "@docknetwork/dock-blockchain-api": "0.6.1"
57
55
  },
58
56
  "devDependencies": {
59
57
  "@babel/node": "^7.16.0",
@@ -5,10 +5,8 @@ import {
5
5
  sr25519PairFromSeed,
6
6
  secp256k1PairFromSeed,
7
7
  } from '@polkadot/util-crypto';
8
-
8
+ import {Ed25519Keypair} from '@docknetwork/credential-sdk/keypairs';
9
9
  import {decodePair} from '@polkadot/keyring/pair/decode';
10
- import {getKeyPairType} from '@docknetwork/sdk/utils/misc';
11
-
12
10
  import * as bs58 from 'base58-universal';
13
11
 
14
12
  import {getKeypairFromDoc} from '@docknetwork/universal-wallet/methods/keypairs';
@@ -20,6 +18,19 @@ const polkadotTypesToKeys = {
20
18
  ecdsa: 'EcdsaSecp256k1VerificationKey2019',
21
19
  };
22
20
 
21
+ export function getKeyPairType(key) {
22
+ const keyType = key.type || key.constructor.VerKeyType;
23
+ if (keyType) {
24
+ return keyType;
25
+ }
26
+
27
+ if (key instanceof Ed25519Keypair) {
28
+ return 'Ed25519VerificationKey2018';
29
+ }
30
+
31
+ throw new Error(`Unknown key type for ${key.constructor.name}`);
32
+ }
33
+
23
34
  const TYPE_FROM_SEED = {
24
35
  ecdsa: secp256k1PairFromSeed,
25
36
  secp256k1: secp256k1PairFromSeed,
@@ -5,6 +5,7 @@ export type NetworkInfo = {
5
5
  name: string,
6
6
  substrateUrl: string | string[],
7
7
  addressPrefix: number,
8
+ cheqdApiUrl: string,
8
9
  };
9
10
 
10
11
  export type NetworkId = 'mainnet' | 'testnet' | 'local' | 'custom';
@@ -12,13 +13,15 @@ export type NetworkId = 'mainnet' | 'testnet' | 'local' | 'custom';
12
13
  export const SUBSTRATE_NETWORKS : Record<NetworkId, NetworkInfo> = {
13
14
  mainnet: {
14
15
  name: 'Dock PoS Mainnet',
15
- substrateUrl: ['wss://mainnet-node.dock.io', 'wss://mainnet-node-2.dock.io'],
16
+ substrateUrl: null,
16
17
  addressPrefix: 22,
18
+ cheqdApiUrl: 'https://mainnet.cheqd.docknode.io/',
17
19
  },
18
20
  testnet: {
19
21
  name: 'Dock PoS Testnet',
20
- substrateUrl: 'wss://knox-1.dock.io',
22
+ substrateUrl: null,
21
23
  addressPrefix: 21,
24
+ cheqdApiUrl: 'https://testnet.cheqd.docknode.io/',
22
25
  },
23
26
  local: {
24
27
  name: 'Local Node',
@@ -2,7 +2,7 @@
2
2
  import assert from 'assert';
3
3
  import {v4 as uuid} from 'uuid';
4
4
  import {getStorage} from '../core/storage';
5
- import {dockService} from '../services/dock';
5
+ import {blockchainService} from '../services/blockchain';
6
6
  import {keyringService} from '../services/keyring';
7
7
  import {utilCryptoService} from '../services/util-crypto';
8
8
  import {walletService} from '../services/wallet';
@@ -165,7 +165,7 @@ class Wallet {
165
165
  * Close wallet
166
166
  */
167
167
  async close() {
168
- await dockService.disconnect();
168
+ await blockchainService.disconnect();
169
169
  this.setStatus('closed');
170
170
  }
171
171
 
@@ -227,13 +227,13 @@ class Wallet {
227
227
  });
228
228
 
229
229
  Logger.debug(`Initializing network ${JSON.stringify(networkInfo)}`);
230
- const isDockConnected = await dockService.isApiConnected();
230
+ const isDockConnected = await blockchainService.isApiConnected();
231
231
 
232
232
  if (isDockConnected) {
233
- await dockService.disconnect();
233
+ await blockchainService.disconnect();
234
234
  }
235
235
 
236
- await dockService.init({
236
+ await blockchainService.init({
237
237
  address: networkInfo.substrateUrl,
238
238
  });
239
239
 
@@ -0,0 +1,11 @@
1
+ // @ts-nocheck
2
+ import assert from 'assert';
3
+
4
+ export const validation = {};
5
+
6
+ export type InitParams = {
7
+ substrateUrl: string,
8
+ cheqdApiUrl?: string,
9
+ networkId?: string,
10
+ cheqdMnemonic?: string,
11
+ };
@@ -0,0 +1,3 @@
1
+ import {BlockchainServiceRpc} from './service-rpc';
2
+
3
+ export const blockchainService = new BlockchainServiceRpc();
@@ -0,0 +1,3 @@
1
+ import {BlockchainServiceRpc} from './service-rpc';
2
+
3
+ export const blockchainService = new BlockchainServiceRpc();
@@ -0,0 +1 @@
1
+ export {blockchainService} from './service';
@@ -0,0 +1,34 @@
1
+ import {NetworkManager} from '../../modules/network-manager';
2
+ import {assertRpcService, mockDockSdkConnection} from '../test-utils';
3
+ import {validation} from './configs';
4
+ import {blockchainService as service} from './service';
5
+ import {BlockchainServiceRpc} from './service-rpc';
6
+
7
+ const doConnect = (
8
+ address = NetworkManager.getInstance().getNetworkInfo().substrateUrl,
9
+ ) =>
10
+ service.init({
11
+ address,
12
+ });
13
+
14
+ describe('DockService', () => {
15
+ it('ServiceRpc', () => {
16
+ assertRpcService(BlockchainServiceRpc, service, validation);
17
+ });
18
+
19
+ describe('init', () => {
20
+ afterEach(async () => {
21
+ await service.disconnect();
22
+ });
23
+
24
+ it('connect and disconnect substrate node', async () => {
25
+ const mock = mockDockSdkConnection();
26
+ const result = await doConnect();
27
+ expect(result).toBe(true);
28
+ expect(service.isBlockchainReady).toBeTruthy();
29
+ await service.disconnect();
30
+ expect(service.isBlockchainReady).toBeFalsy();
31
+ mock.clear();
32
+ });
33
+ });
34
+ });
@@ -1,12 +1,12 @@
1
1
  import {RpcService} from '../rpc-service-client';
2
- import {InitParams, validation} from './configs';
2
+ import {InitParams} from './configs';
3
3
 
4
4
  /**
5
5
  *
6
6
  */
7
- export class DockServiceRpc extends RpcService {
7
+ export class BlockchainServiceRpc extends RpcService {
8
8
  constructor() {
9
- super('dock');
9
+ super('blockchain');
10
10
  }
11
11
 
12
12
  /**
@@ -19,15 +19,14 @@ export class DockServiceRpc extends RpcService {
19
19
  /**
20
20
  *
21
21
  */
22
- ensureDockReady(): Promise<any> {
23
- return this.call('ensureDockReady');
22
+ ensureBlockchainReady(): Promise<any> {
23
+ return this.call('ensureBlockchainReady');
24
24
  }
25
25
 
26
26
  /**
27
27
  *
28
28
  */
29
29
  init(params: InitParams): Promise<any> {
30
- validation.init(params);
31
30
  return this.call('init', params);
32
31
  }
33
32
 
@@ -0,0 +1,261 @@
1
+ // @ts-nocheck
2
+
3
+ import {DirectSecp256k1HdWallet} from '@cosmjs/proto-signing';
4
+ import {CheqdAPI} from '@docknetwork/cheqd-blockchain-api';
5
+ import {CheqdCoreModules} from '@docknetwork/cheqd-blockchain-modules';
6
+ import {MultiApiCoreModules} from '@docknetwork/credential-sdk/modules';
7
+ import {
8
+ CoreResolver,
9
+ DIDKeyResolver,
10
+ ResolverRouter,
11
+ UniversalResolver,
12
+ WILDCARD,
13
+ } from '@docknetwork/credential-sdk/resolver';
14
+ import {initializeWasm} from '@docknetwork/crypto-wasm-ts/lib/index';
15
+
16
+ import {DockAPI} from '@docknetwork/dock-blockchain-api';
17
+ import {
18
+ DockCoreModules,
19
+ DockDIDModule,
20
+ } from '@docknetwork/dock-blockchain-modules';
21
+ import {EventEmitter} from 'events';
22
+ import {Logger} from '../../core/logger';
23
+ import {once} from '../../modules/event-manager';
24
+ import {utilCryptoService} from '../util-crypto';
25
+ import {InitParams} from './configs';
26
+
27
+ export const universalResolverUrl = 'https://uniresolver.truvera.io';
28
+
29
+ import {
30
+ CheqdAccumulatorCommon,
31
+ CheqdAccumulatorId,
32
+ CheqdAccumulatorPublicKey,
33
+ DockAccumulatorCommon,
34
+ DockAccumulatorId,
35
+ DockAccumulatorPublicKey,
36
+ } from '@docknetwork/credential-sdk/types';
37
+
38
+ class AnyDIDResolver extends ResolverRouter {
39
+ method = WILDCARD;
40
+ }
41
+
42
+ const {AccumulatorModule: AccumulatorModuleDock} = DockCoreModules;
43
+ const {AccumulatorModule: AccumulatorModuleCheqd} = CheqdCoreModules;
44
+
45
+ const TYPES_FOR_DID = {
46
+ dock: {
47
+ PublicKey: DockAccumulatorPublicKey,
48
+ AccumulatorId: DockAccumulatorId,
49
+ AccumulatorCommon: DockAccumulatorCommon,
50
+ AccumulatorModule: AccumulatorModuleDock,
51
+ },
52
+ cheqd: {
53
+ PublicKey: CheqdAccumulatorPublicKey,
54
+ AccumulatorId: CheqdAccumulatorId,
55
+ AccumulatorCommon: CheqdAccumulatorCommon,
56
+ AccumulatorModule: AccumulatorModuleCheqd,
57
+ },
58
+ };
59
+
60
+ /**
61
+ *
62
+ */
63
+ export class BlockchainService {
64
+ dock;
65
+ modules;
66
+ didModule;
67
+ cheqdApi;
68
+ isBlockchainReady = false;
69
+ resolver: any;
70
+ dockEnabled: boolean;
71
+ static Events = {
72
+ BLOCKCHAIN_READY: 'blockchain-ready',
73
+ };
74
+
75
+ rpcMethods = [
76
+ BlockchainService.prototype.disconnect,
77
+ BlockchainService.prototype.ensureBlockchainReady,
78
+ BlockchainService.prototype.init,
79
+ BlockchainService.prototype.isApiConnected,
80
+ BlockchainService.prototype.getAddress,
81
+ ];
82
+
83
+ constructor() {
84
+ this.name = 'blockchain';
85
+ this.dock = new DockAPI();
86
+ this.cheqdApi = new CheqdAPI();
87
+ this.didModule = new DockDIDModule(this.dock);
88
+ this.dockModules = new DockCoreModules(this.dock);
89
+ this.cheqdModules = new CheqdCoreModules(this.cheqdApi);
90
+ this.modules = new MultiApiCoreModules(
91
+ this.dockEnabled
92
+ ? [this.dockModules, this.cheqdModules]
93
+ : [this.cheqdModules],
94
+ );
95
+ this.emitter = new EventEmitter();
96
+ this.resolver = this.createDIDResolver();
97
+ }
98
+
99
+ getTypesForDIDOrAccumulator(didOrRegistryId) {
100
+ const didType = didOrRegistryId
101
+ .replace('dock:accumulator:', 'accumulator:dock:')
102
+ .split(':')[1];
103
+ const chainDidType = !this.dock && didType === 'dock' ? 'cheqd' : didType;
104
+ const types = TYPES_FOR_DID[chainDidType];
105
+ if (!types) {
106
+ throw new APIError(
107
+ `Unable to use DID type ${didType} for this operation`,
108
+ );
109
+ }
110
+ return types;
111
+ }
112
+
113
+ /**
114
+ *
115
+ * @returns
116
+ */
117
+ async ensureBlockchainReady() {
118
+ if (this.isBlockchainReady) {
119
+ return;
120
+ }
121
+
122
+ return once(this.emitter, BlockchainService.Events.BLOCKCHAIN_READY);
123
+ }
124
+
125
+ createDIDResolver() {
126
+ return new AnyDIDResolver([
127
+ new DIDKeyResolver(),
128
+ new CoreResolver(this.modules),
129
+ new UniversalResolver(universalResolverUrl),
130
+ ]);
131
+ }
132
+ /**
133
+ *
134
+ * @param {*} params
135
+ * @returns
136
+ */
137
+ async init(params: InitParams) {
138
+ if (this.dock.isConnected) {
139
+ await this.dock.disconnect();
140
+ }
141
+
142
+ if (this.cheqdApi && this.cheqdApi.isInitialized()) {
143
+ await this.cheqdApi.disconnect();
144
+ }
145
+
146
+ Logger.info(`Attempt to initialized substrate at: ${params.address}`);
147
+
148
+ this.dockEnabled = !!params.substrateUrl;
149
+
150
+ if (this.dockEnabled) {
151
+ await this.dock.init({
152
+ address: params.substrateUrl,
153
+ });
154
+ Logger.info(`Substrate initialized at: ${params.address}`);
155
+ }
156
+
157
+ this.modules = new MultiApiCoreModules(
158
+ this.dockEnabled
159
+ ? [this.dockModules, this.cheqdModules]
160
+ : [this.cheqdModules],
161
+ );
162
+
163
+ if (params?.cheqdApiUrl) {
164
+ const checkdApiUrl = params?.cheqdApiUrl;
165
+ const cheqdNetworkId = params?.networkId;
166
+ const cheqdMnemonic =
167
+ params?.cheqdMnemonic || (await utilCryptoService.mnemonicGenerate(12));
168
+
169
+ const wallet = await DirectSecp256k1HdWallet.fromMnemonic(cheqdMnemonic, {
170
+ prefix: 'cheqd',
171
+ });
172
+
173
+ const walletAccounts = await wallet.getAccounts();
174
+ const [{address}] = walletAccounts;
175
+ console.log('Using cheqd account:', address);
176
+
177
+ Logger.info(
178
+ `Attempt to initialized cheqd at: ${checkdApiUrl} with networkId: ${cheqdNetworkId}`,
179
+ );
180
+ Logger.info(`Using cheqd account: ${address}`);
181
+
182
+ try {
183
+ await this.cheqdApi.init({
184
+ wallet,
185
+ url: checkdApiUrl,
186
+ network: cheqdNetworkId,
187
+ });
188
+ Logger.info(`Cheqd initialized at: ${checkdApiUrl}`);
189
+ } catch (err) {
190
+ Logger.error(`Failed to initialize cheqd at: ${checkdApiUrl}`);
191
+ }
192
+ }
193
+
194
+ this.address = params.address;
195
+
196
+ this.resolver = this.createDIDResolver();
197
+
198
+ if (
199
+ process.env.NODE_ENV !== 'test' ||
200
+ process.env.API_MOCK_DISABLED === 'true'
201
+ ) {
202
+ await initializeWasm();
203
+ }
204
+
205
+ this._setBlockchainReady(true);
206
+
207
+ return true;
208
+ }
209
+
210
+ /**
211
+ *
212
+ * @returns
213
+ */
214
+ async disconnect() {
215
+ let result;
216
+
217
+ if (this.dockEnabled) {
218
+ result = await this.dock.disconnect();
219
+ }
220
+
221
+ if (this.cheqdApi && this.cheqdApi.isInitialized()) {
222
+ result = await this.cheqdApi.disconnect();
223
+ }
224
+
225
+ this._setBlockchainReady(false);
226
+
227
+ return result;
228
+ }
229
+
230
+ async waitBlockchainReady() {
231
+ return new Promise(resolve => {
232
+ if (this.isBlockchainReady) {
233
+ resolve();
234
+ } else {
235
+ this.emitter.once(BlockchainService.Events.BLOCKCHAIN_READY, resolve);
236
+ }
237
+ });
238
+ }
239
+
240
+ /**
241
+ *
242
+ * @returns
243
+ */
244
+ async isApiConnected() {
245
+ return this.cheqdApi.isInitialized();
246
+ }
247
+
248
+ async getAddress() {
249
+ return this.dock.address;
250
+ }
251
+
252
+ _setBlockchainReady(isBlockchainReady) {
253
+ this.isBlockchainReady = isBlockchainReady;
254
+
255
+ if (isBlockchainReady) {
256
+ this.emitter.emit(BlockchainService.Events.BLOCKCHAIN_READY);
257
+ }
258
+ }
259
+ }
260
+
261
+ export const blockchainService: BlockchainService = new BlockchainService();
@@ -9,7 +9,9 @@ import {
9
9
  } from '@docknetwork/crypto-wasm-ts';
10
10
 
11
11
  import {hexToU8a} from '@polkadot/util';
12
- import { dockService } from '../dock/service';
12
+ import {
13
+ blockchainService,
14
+ } from '../blockchain/service';
13
15
 
14
16
  const trimHexID = id => {
15
17
  if (id.substr(0, 2) !== '0x') {
@@ -19,100 +21,37 @@ const trimHexID = id => {
19
21
  return id.substr(2);
20
22
  };
21
23
 
22
- // "-32000: Client error: UnknownBlock: State already discarded for BlockId::Hash(<hash>)"
23
- // This means that the node has discarded old blocks to preserve space. This should not happen with a full node
24
- const UnknownBlockErrorCode = -32000;
25
-
26
- async function updateMembershipWitness({
27
- credential,
28
- membershipWitness,
29
- registryId,
30
- blockNo,
31
- }) {
32
- const revocationId = credential.credentialStatus.revocationId;
33
- const member = Accumulator.encodePositiveNumberAsAccumulatorMember(
34
- Number(revocationId),
35
- );
36
-
37
- let updates = [];
38
- try {
39
- // TODO: Ensure it will for cheqd
40
- // Will be handled on https://dock-team.atlassian.net/browse/DCKW-572
41
- updates = await dockService.modules.accumulator.dockOnly.getUpdatesFromBlock(
42
- registryId,
43
- blockNo,
44
- );
45
- } catch (err) {
46
- if (err.code === UnknownBlockErrorCode) {
47
- console.error(err);
48
- updates = [];
49
- } else {
50
- throw err;
51
- }
52
- }
53
-
54
- const additions = [];
55
- const removals = [];
56
-
57
- if (updates.length && updates[0].additions !== null) {
58
- for (const a of updates[0].additions) {
59
- additions.push(hexToU8a(a));
60
- }
61
- }
62
-
63
- if (updates.length && updates[0].removals !== null) {
64
- for (const a of updates[0].removals) {
65
- removals.push(hexToU8a(a));
66
- }
67
- }
68
-
69
-
70
- const witness = new VBMembershipWitness(hexToU8a(membershipWitness));
71
-
72
- if (updates.length) {
73
- const queriedWitnessInfo = new VBWitnessUpdateInfo(
74
- hexToU8a(updates[0].witnessUpdateInfo),
75
- )
76
-
77
- witness.updateUsingPublicInfoPostBatchUpdate(
78
- member,
79
- additions,
80
- removals,
81
- queriedWitnessInfo,
82
- );
83
- }
84
-
85
- return witness;
86
- }
87
-
88
24
  export const getWitnessDetails = async (credential, _membershipWitness) => {
89
25
  let witness = _membershipWitness;
90
26
  let blockNo;
91
27
 
92
28
  try {
93
- ({blockNo, witness} = JSON.parse(_membershipWitness));
29
+ ({witness, blockNo} = JSON.parse(_membershipWitness));
94
30
  } catch (err) {
95
31
  console.error(err);
96
32
  }
97
-
33
+
98
34
  const {credentialStatus} = credential;
99
- const registryId = credentialStatus?.id.replace('dock:accumulator:', '');
35
+ const registryId = credentialStatus?.id;
100
36
  const revocationIndex = credentialStatus.revocationId;
101
37
 
102
- const queriedAccumulator = await dockService.modules.accumulator.getAccumulator(
103
- registryId,
104
- false,
105
- );
38
+ const queriedAccumulator =
39
+ await blockchainService.modules.accumulator.getAccumulator(
40
+ registryId,
41
+ false,
42
+ );
106
43
 
107
44
  if (!queriedAccumulator) {
108
45
  throw new Error('Accumulator not found');
109
46
  }
110
47
 
111
- const accumulator = PositiveAccumulator.fromAccumulated(queriedAccumulator.accumulated.bytes);
48
+ const accumulator = PositiveAccumulator.fromAccumulated(
49
+ queriedAccumulator.accumulated.bytes,
50
+ );
112
51
 
113
52
  const encodedRevId = Encoder.defaultEncodeFunc()(revocationIndex.toString());
114
53
 
115
- const publicKey = await dockService.modules.accumulator.getPublicKey(
54
+ const publicKey = await blockchainService.modules.accumulator.getPublicKey(
116
55
  queriedAccumulator.keyRef[0],
117
56
  queriedAccumulator.keyRef[1],
118
57
  );
@@ -120,23 +59,36 @@ export const getWitnessDetails = async (credential, _membershipWitness) => {
120
59
  const params = dockAccumulatorParams();
121
60
  const pk = new AccumulatorPublicKey(publicKey.bytes);
122
61
 
123
- let membershipWitness = new VBMembershipWitness(
124
- hexToU8a(witness),
125
- );
62
+ const membershipWitness = new VBMembershipWitness(hexToU8a(witness));
126
63
 
127
- if(blockNo){
128
- try {
129
- const updatedWitness = await updateMembershipWitness({
130
- credential,
131
- membershipWitness: witness,
132
- registryId,
133
- blockNo,
134
- });
135
- membershipWitness = updatedWitness;
64
+ try {
65
+ const credentialStatusId = credential.credentialStatus.id;
66
+ const accumulatorId = blockchainService
67
+ .getTypesForDIDOrAccumulator(credentialStatusId)
68
+ .AccumulatorId.from(credentialStatusId);
69
+
70
+ const history =
71
+ await blockchainService.modules.accumulator.accumulatorHistory(
72
+ accumulatorId,
73
+ );
74
+
75
+ const blockNoIndex = history.updates.findIndex(
76
+ update => update.id.toString() === blockNo,
77
+ );
136
78
 
137
- } catch (err) {
138
- console.error(err);
79
+ const nextBlockNo = history.updates[blockNoIndex + 1]?.id?.toString();
80
+
81
+ if (nextBlockNo) {
82
+ await blockchainService.modules.accumulator.updateWitness(
83
+ registryId,
84
+ encodedRevId,
85
+ membershipWitness,
86
+ nextBlockNo,
87
+ queriedAccumulator.lastModified,
88
+ );
139
89
  }
90
+ } catch (err) {
91
+ console.error(err);
140
92
  }
141
93
 
142
94
  return {
@@ -1,16 +1,13 @@
1
1
  import {
2
2
  applyEnforceBounds,
3
- MAX_DATE_PLACEHOLDER,
4
- MIN_DATE_PLACEHOLDER,
5
- MAX_NUMBER,
6
3
  fetchProvingKey,
7
4
  isBase64OrDataUrl,
8
- MIN_INTEGER,
9
5
  } from './bound-check';
10
6
  import {PresentationBuilder} from '@docknetwork/crypto-wasm-ts/lib';
11
7
  import proofRequest from './proof-request.json';
12
8
  import { replaceResponseURL } from '@docknetwork/wallet-sdk-core/src/helpers';
13
9
  import assert from 'assert';
10
+ import { MAX_DATE_PLACEHOLDER, MAX_NUMBER, MIN_DATE_PLACEHOLDER, MIN_NUMBER } from './pex-helpers';
14
11
 
15
12
  const testAPIURL = process.env.TESTING_API_URL || null;
16
13
 
@@ -218,7 +215,7 @@ describe('Bound check', () => {
218
215
 
219
216
  const credIdx = 0;
220
217
  const attributeName = 'credentialSubject.income';
221
- const min = MIN_INTEGER;
218
+ const min = MIN_NUMBER;
222
219
  const max = 20000;
223
220
 
224
221
  expect(builder.enforceBounds).toHaveBeenCalledWith(