@docknetwork/wallet-sdk-core 0.4.19

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 (97) hide show
  1. package/LICENSE +39 -0
  2. package/babel.config.js +16 -0
  3. package/jest.config.ts +39 -0
  4. package/lib/account-provider.d.ts +8 -0
  5. package/lib/account-provider.d.ts.map +1 -0
  6. package/lib/account-provider.js +15 -0
  7. package/lib/account-provider.js.map +1 -0
  8. package/lib/biometric-provider.d.ts +29 -0
  9. package/lib/biometric-provider.d.ts.map +1 -0
  10. package/lib/biometric-provider.js +54 -0
  11. package/lib/biometric-provider.js.map +1 -0
  12. package/lib/credential-provider.d.ts +58 -0
  13. package/lib/credential-provider.d.ts.map +1 -0
  14. package/lib/credential-provider.js +198 -0
  15. package/lib/credential-provider.js.map +1 -0
  16. package/lib/did-provider.d.ts +79 -0
  17. package/lib/did-provider.d.ts.map +1 -0
  18. package/lib/did-provider.js +215 -0
  19. package/lib/did-provider.js.map +1 -0
  20. package/lib/ecosystem-tools.d.ts +11 -0
  21. package/lib/ecosystem-tools.d.ts.map +1 -0
  22. package/lib/ecosystem-tools.js +33 -0
  23. package/lib/ecosystem-tools.js.map +1 -0
  24. package/lib/helpers.d.ts +8 -0
  25. package/lib/helpers.d.ts.map +1 -0
  26. package/lib/helpers.js +63 -0
  27. package/lib/helpers.js.map +1 -0
  28. package/lib/message-provider.d.ts +36 -0
  29. package/lib/message-provider.d.ts.map +1 -0
  30. package/lib/message-provider.js +172 -0
  31. package/lib/message-provider.js.map +1 -0
  32. package/lib/messages/message-helpers.d.ts +110 -0
  33. package/lib/messages/message-helpers.d.ts.map +1 -0
  34. package/lib/messages/message-helpers.js +106 -0
  35. package/lib/messages/message-helpers.js.map +1 -0
  36. package/lib/network-resolver.d.ts +19 -0
  37. package/lib/network-resolver.d.ts.map +1 -0
  38. package/lib/network-resolver.js +80 -0
  39. package/lib/network-resolver.js.map +1 -0
  40. package/lib/types.d.ts +63 -0
  41. package/lib/types.d.ts.map +1 -0
  42. package/lib/types.js +3 -0
  43. package/lib/types.js.map +1 -0
  44. package/lib/v1-helpers.d.ts +20 -0
  45. package/lib/v1-helpers.d.ts.map +1 -0
  46. package/lib/v1-helpers.js +147 -0
  47. package/lib/v1-helpers.js.map +1 -0
  48. package/lib/verification-controller.d.ts +48 -0
  49. package/lib/verification-controller.d.ts.map +1 -0
  50. package/lib/verification-controller.js +219 -0
  51. package/lib/verification-controller.js.map +1 -0
  52. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.d.ts +20 -0
  53. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.d.ts.map +1 -0
  54. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.js +140 -0
  55. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.js.map +1 -0
  56. package/lib/wallet-wasm.d.ts +10 -0
  57. package/lib/wallet-wasm.d.ts.map +1 -0
  58. package/lib/wallet-wasm.js +62 -0
  59. package/lib/wallet-wasm.js.map +1 -0
  60. package/lib/wallet.d.ts +11 -0
  61. package/lib/wallet.d.ts.map +1 -0
  62. package/lib/wallet.js +173 -0
  63. package/lib/wallet.js.map +1 -0
  64. package/package.json +34 -0
  65. package/setup-tests.ts +1 -0
  66. package/src/account-provider.ts +18 -0
  67. package/src/biometric-provider.ts +82 -0
  68. package/src/credential-provider.test.ts +164 -0
  69. package/src/credential-provider.ts +272 -0
  70. package/src/did-provider.test.ts +203 -0
  71. package/src/did-provider.ts +263 -0
  72. package/src/ecosystem-tools.ts +37 -0
  73. package/src/fixtures/any-credential-proof-request.json +30 -0
  74. package/src/fixtures/biometrics-credential-bbs-revocation.json +62 -0
  75. package/src/fixtures/customer-credential.json +39 -0
  76. package/src/fixtures/iiw-credential.json +59 -0
  77. package/src/fixtures/iiw-template.json +33 -0
  78. package/src/fixtures/university-degree-bbs.json +57 -0
  79. package/src/fixtures/university-degree-proof-request.json +32 -0
  80. package/src/helpers.ts +61 -0
  81. package/src/message-provider.test.ts +115 -0
  82. package/src/message-provider.ts +221 -0
  83. package/src/messages/message-helpers.ts +125 -0
  84. package/src/messages/relay-service-mocks/demo-app-messages.json +450 -0
  85. package/src/network-resolver.test.ts +142 -0
  86. package/src/network-resolver.ts +122 -0
  87. package/src/types.ts +75 -0
  88. package/src/v1-helpers.ts +160 -0
  89. package/src/verification-controller.test.ts +149 -0
  90. package/src/verification-controller.ts +276 -0
  91. package/src/wallet-to-wallet-verification/walletToWalletVerificationProvider.ts +216 -0
  92. package/src/wallet-wasm.ts +74 -0
  93. package/src/wallet.test.ts +72 -0
  94. package/src/wallet.ts +211 -0
  95. package/tsconfig.build.json +26 -0
  96. package/tsconfig.build.tsbuildinfo +1 -0
  97. package/tsconfig.json +30 -0
@@ -0,0 +1,272 @@
1
+ import {credentialServiceRPC} from '@docknetwork/wallet-sdk-wasm/src/services/credential';
2
+ import {IWallet} from './types';
3
+ import assert from 'assert';
4
+ import { dockService } from '@docknetwork/wallet-sdk-wasm/src/services/dock';
5
+
6
+ export type Credential = any;
7
+
8
+ export interface ICredentialProvider {
9
+ getCredentials(type?: string): Credential[];
10
+ getById(id: string): Credential;
11
+ getMembershipWitness(credential: any): Promise<any>;
12
+ isBBSPlusCredential(credential: any): boolean;
13
+ isValid(credential: any, forceFetch?: boolean): Promise<boolean>;
14
+ addCredential(credential: any): Promise<Credential>;
15
+ syncCredentialStatus(params: SyncCredentialStatusParams): Promise<CredentialStatusDocument[]>;
16
+ getCredentialStatus(credential: Credential): Promise<{status: string, error?: string}>;
17
+ }
18
+
19
+ export function isBBSPlusCredential(credential) {
20
+ return (
21
+ (typeof credential?.proof?.type === 'string' &&
22
+ credential.proof.type.includes('BBS+SignatureDock')) ||
23
+ (Array.isArray(credential['@context']) &&
24
+ credential['@context'].find(
25
+ context => context.bs && context.bs.indexOf('bbs') > -1,
26
+ ))
27
+ );
28
+ }
29
+
30
+ export function isCredentialExpired(credential) {
31
+ return !!credential.expirationDate && new Date(credential.expirationDate) < new Date();
32
+ }
33
+
34
+ /**
35
+ * Uses Dock SDK to verify a credential
36
+ * @param credential
37
+ * @returns
38
+ */
39
+ export async function isValid({
40
+ credential,
41
+ wallet,
42
+ }: {
43
+ credential: Credential;
44
+ wallet: IWallet;
45
+ }) {
46
+ assert(!!credential, 'credential is required');
47
+
48
+ try {
49
+ if (isCredentialExpired(credential)) {
50
+ return {
51
+ status: CredentialStatus.Expired,
52
+ };
53
+ }
54
+
55
+ const membershipWitness = credential[ACUMM_WITNESS_PROP_KEY] || await getMembershipWitness({
56
+ credentialId: credential.id,
57
+ wallet,
58
+ });
59
+
60
+ delete credential[ACUMM_WITNESS_PROP_KEY];
61
+
62
+ const verificationResult = await credentialServiceRPC.verifyCredential({
63
+ credential,
64
+ membershipWitness,
65
+ });
66
+
67
+ const { verified, error } = verificationResult;
68
+
69
+ if (!verified) {
70
+ if (typeof error === 'string' && error.toLowerCase().includes('revocation')) {
71
+ return {
72
+ status: CredentialStatus.Revoked,
73
+ error,
74
+ };
75
+ }
76
+
77
+ return {
78
+ status: CredentialStatus.Invalid,
79
+ error: error,
80
+ };
81
+ }
82
+
83
+ return {
84
+ status: CredentialStatus.Verified,
85
+ };
86
+ } catch (err) {
87
+ console.error(err);
88
+
89
+ return {
90
+ status: CredentialStatus.Invalid,
91
+ error: err.toString(),
92
+ };
93
+ }
94
+ }
95
+
96
+ export const ACUMM_WITNESS_PROP_KEY = '$$accum__witness$$';
97
+
98
+ export async function addCredential({wallet, credential}) {
99
+ const acummWitness = credential[ACUMM_WITNESS_PROP_KEY];
100
+
101
+ if (acummWitness) {
102
+ delete credential[ACUMM_WITNESS_PROP_KEY];
103
+ }
104
+
105
+ const response = await wallet.addDocument(credential);
106
+
107
+ if (acummWitness) {
108
+ await wallet.addDocument({
109
+ type: 'AccumulatorWitness',
110
+ id: `${credential.id}#witness`,
111
+ value: acummWitness,
112
+ initialWitness: acummWitness,
113
+ });
114
+ }
115
+
116
+ syncCredentialStatus({ wallet, credentialIds: [credential.id] });
117
+
118
+ return response;
119
+ }
120
+
121
+ async function getMembershipWitness({credentialId, wallet}) {
122
+ const document = await wallet.getDocumentById(`${credentialId}#witness`);
123
+ return document?.value;
124
+ }
125
+
126
+
127
+ export const CredentialStatus = {
128
+ Invalid: 'invalid',
129
+ Expired: 'expired',
130
+ Verified: 'verified',
131
+ Revoked: 'revoked',
132
+ Pending: 'pending',
133
+ };
134
+
135
+ type SyncCredentialStatusParams = {
136
+ // Optional credential IDs to sync
137
+ credentialIds?: string[],
138
+ // Skip the cache and re-fetch from the chain
139
+ forceFetch?: boolean,
140
+ };
141
+
142
+ type CredentialStatusDocument = {
143
+ id: string;
144
+ status: string;
145
+ error: string;
146
+ }
147
+
148
+ /**
149
+ * Fetch credential status from the chain and update the wallet
150
+ * Store a new document <credentialId>#status in the wallet
151
+ * Returns a list of CredentialStatusDocument
152
+ *
153
+ * @param param0
154
+ * @returns CredentialStatusDocument[]
155
+ */
156
+ async function syncCredentialStatus({ wallet, credentialIds, forceFetch }: SyncCredentialStatusParams & {
157
+ wallet: IWallet;
158
+ } ): Promise<CredentialStatusDocument[]> {
159
+ let credentials;
160
+
161
+ if (credentialIds && credentialIds.length) {
162
+ credentials = await wallet.getDocumentsById(credentialIds);
163
+ } else {
164
+ credentials = await wallet.getDocumentsByType('VerifiableCredential');
165
+ }
166
+
167
+ let statusDocs = [];
168
+
169
+ let isApiConnected;
170
+
171
+ for (const credential of credentials) {
172
+ let shouldFetch = !!forceFetch;
173
+ let statusDoc = await wallet.getDocumentById(`${credential.id}#status`);
174
+
175
+ if (!statusDoc) {
176
+ shouldFetch = true;
177
+ statusDoc = {
178
+ type: 'CredentialStatus',
179
+ id: `${credential.id}#status`,
180
+ createdAt: new Date().toISOString(),
181
+ updatedAt: new Date().toISOString(),
182
+ status: null,
183
+ };
184
+
185
+ await wallet.addDocument(statusDoc);
186
+ }
187
+
188
+ statusDocs.push(statusDoc);
189
+
190
+ if (!statusDoc.status || statusDoc.status === CredentialStatus.Pending) {
191
+ shouldFetch = true;
192
+ }
193
+
194
+ if (!shouldFetch) {
195
+ // check if latest fetch was more than 24 hours ago
196
+ const updatedAt = new Date(statusDoc.updatedAt);
197
+ const diff = new Date().getTime() - updatedAt.getTime();
198
+ const hours = Math.floor(diff / 1000 / 60 / 60);
199
+ if (hours > 24) {
200
+ shouldFetch = true;
201
+ }
202
+ }
203
+
204
+ if (!shouldFetch) {
205
+ continue;
206
+ }
207
+
208
+ if (!statusDoc.status) {
209
+ statusDoc.status = CredentialStatus.Pending;
210
+ statusDoc.updatedAt = new Date().toISOString();
211
+ await wallet.updateDocument(statusDoc);
212
+ }
213
+
214
+ if (!isApiConnected) {
215
+ await dockService.ensureDockReady();
216
+ isApiConnected = true;
217
+ }
218
+
219
+ const result = await isValid({ credential, wallet });
220
+ statusDoc.status = result?.status
221
+ statusDoc.error = result?.error;
222
+ statusDoc.updatedAt = new Date().toISOString();
223
+
224
+ await wallet.updateDocument(statusDoc);
225
+ }
226
+
227
+ return statusDocs;
228
+ }
229
+
230
+ export function createCredentialProvider({
231
+ wallet,
232
+ }: {
233
+ wallet: IWallet;
234
+ }): ICredentialProvider {
235
+ function getCredentials(type: string = 'VerifiableCredential') {
236
+ return wallet.getDocumentsByType(type) as any;
237
+ }
238
+
239
+ return {
240
+ getCredentials,
241
+ getMembershipWitness: async (credentialId: string) =>
242
+ getMembershipWitness({credentialId, wallet}),
243
+ getById: (id: string) => wallet.getDocumentById(id),
244
+ isBBSPlusCredential,
245
+ isValid: async credential =>
246
+ isValid({
247
+ credential,
248
+ wallet,
249
+ }) as any,
250
+ getCredentialStatus: async (credential: Credential) => {
251
+ assert(!!credential, 'credential is required');
252
+
253
+ if (isCredentialExpired(credential)) {
254
+ return {
255
+ status: CredentialStatus.Expired,
256
+ };
257
+ }
258
+
259
+ const statusDoc = await wallet.getDocumentById(`${credential.id}#status`);
260
+
261
+ return {
262
+ status: statusDoc?.status || CredentialStatus.Pending,
263
+ error: statusDoc?.error,
264
+ }
265
+ },
266
+ syncCredentialStatus: async (props: SyncCredentialStatusParams) => {
267
+ return syncCredentialStatus({ wallet, ...props });
268
+ },
269
+ addCredential: credential => addCredential({wallet, credential}),
270
+ // TODO: move import credential from json or URL to this provider
271
+ };
272
+ }
@@ -0,0 +1,203 @@
1
+ import {IWallet} from './types';
2
+ import {createWallet} from './wallet';
3
+ import {createDIDock, createDIDKey, createDIDProvider, IDIDProvider} from './did-provider';
4
+ import {createAccountProvider} from './account-provider';
5
+ import {didServiceRPC} from '@docknetwork/wallet-sdk-wasm/src/services/dids';
6
+
7
+ describe('DID Provider', () => {
8
+ let wallet: IWallet;
9
+ let didProvider: IDIDProvider;
10
+ let accountProvider;
11
+ const didBackupFile = {
12
+ '@context': [
13
+ 'https://www.w3.org/2018/credentials/v1',
14
+ 'https://w3id.org/wallet/v1',
15
+ ],
16
+ id: 'did:key:z6LSjTbRETJjUCDiQopbeCgZKRisy7mdchwiMBPTQktcibGh#encrypted-wallet',
17
+ type: ['VerifiableCredential', 'EncryptedWallet'],
18
+ issuer: 'did:key:z6LSjTbRETJjUCDiQopbeCgZKRisy7mdchwiMBPTQktcibGh',
19
+ issuanceDate: '2022-07-19T20:59:44.798Z',
20
+ credentialSubject: {
21
+ id: 'did:key:z6LSjTbRETJjUCDiQopbeCgZKRisy7mdchwiMBPTQktcibGh',
22
+ encryptedWalletContents: {
23
+ protected: 'eyJlbmMiOiJYQzIwUCJ9',
24
+ recipients: [
25
+ {
26
+ header: {
27
+ kid: 'did:key:z6LSjTbRETJjUCDiQopbeCgZKRisy7mdchwiMBPTQktcibGh#z6LSjTbRETJjUCDiQopbeCgZKRisy7mdchwiMBPTQktcibGh',
28
+ alg: 'ECDH-ES+A256KW',
29
+ epk: {
30
+ kty: 'OKP',
31
+ crv: 'X25519',
32
+ x: '-ABoa59NY2qVI66NZ8EbqxCwp02sft5onyKhfa2yfUU',
33
+ },
34
+ apu: '-ABoa59NY2qVI66NZ8EbqxCwp02sft5onyKhfa2yfUU',
35
+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nXYk10bd-M50jIWfrlrX-pdSObjolCVEtuUt2lZvZahe0r2bg87Zbk3eFU9bI8eVtdosvSGtP9ZrKfe5BjrfMAC0XsKfWwoKT0JXznXD0Brw11PBQwsslusQOPI6HqskmmaE3NCkKB9a2Wnzs1eO1_Ompqbx_J7uoBphNMzlnrOQL74UVRifDqTFc_o0-rhp3EaXnlDnuOCbYwbOO7Ah3jX5OdU49Vnm-VHIB5_MAtYeEonVaMdSyXa1LXboy-LespvK9P7x1Zfnk5FW9SQCEa1cp7_dXD4h5ho7shKTzPLxbFShKQ_twsoP7JeMdZd1MNCtt_7B9Be-uRfGPwV2XQijME0xtq_8OMhbxFAJh-6MLVZqqKlDSw',
44
+ tag: 'kKoF2f10Da0kBqX2brBZug',
45
+ },
46
+ },
47
+ };
48
+ const password = 'test';
49
+
50
+ beforeEach(async () => {
51
+ wallet = await createWallet({
52
+ databasePath: ':memory:',
53
+ });
54
+ accountProvider = createAccountProvider({wallet});
55
+ didProvider = createDIDProvider({wallet});
56
+ });
57
+
58
+ describe('importDID', () => {
59
+ it('expect to import DID', async () => {
60
+ await didProvider.importDID({
61
+ encryptedJSONWallet: didBackupFile,
62
+ password,
63
+ });
64
+
65
+ const documents = await wallet.getAllDocuments();
66
+ const keyDocument = documents.find(
67
+ item => item.type === 'Ed25519VerificationKey2018',
68
+ );
69
+ const didResolution = documents.find(
70
+ item => item.type === 'DIDResolutionResponse',
71
+ );
72
+ expect(documents.length).toBe(4);
73
+ expect(didResolution).toBeDefined();
74
+ expect(keyDocument).toBeDefined();
75
+ });
76
+ it('expect to avoid duplicated DID', async () => {
77
+ await didProvider.importDID({
78
+ encryptedJSONWallet: didBackupFile,
79
+ password,
80
+ });
81
+
82
+ await expect(
83
+ didProvider.importDID({
84
+ encryptedJSONWallet: didBackupFile,
85
+ password,
86
+ }),
87
+ ).rejects.toThrowError('DID already exists in wallet');
88
+ });
89
+ });
90
+
91
+ describe('create DID Dock', () => {
92
+ it('expect to create a DID Dock', async () => {
93
+ const account = await accountProvider.create({
94
+ name: 'test',
95
+ });
96
+
97
+ jest.spyOn(didServiceRPC, 'registerDidDock').mockResolvedValueOnce({
98
+ dockDID: 'did:dock:abcde',
99
+ keyPairWalletId: account.address,
100
+ });
101
+
102
+ jest.spyOn(didServiceRPC, 'generateKeyDoc').mockResolvedValueOnce({
103
+ id: 'did:dock:abcde#key-1',
104
+ type: 'KeyDocument',
105
+ });
106
+
107
+ jest.spyOn(didServiceRPC, 'getDidDockDocument').mockResolvedValueOnce({
108
+ id: 'did:dock:abcde#key-2',
109
+ type: 'DidDocument',
110
+ });
111
+
112
+ await didProvider.createDIDock({
113
+ address: account.address,
114
+ name: 'Test DID',
115
+ });
116
+
117
+ const keyDocuments = await wallet.getDocumentsByType('KeyDocument');
118
+ const didDocument = await wallet.getDocumentsByType(
119
+ 'DIDResolutionResponse',
120
+ );
121
+
122
+ expect(keyDocuments.length).toBe(1);
123
+ expect(didDocument.length).toBe(2);
124
+ });
125
+ it('expect to assert parameters', async () => {
126
+ await expect(
127
+ didProvider.createDIDock({
128
+ address: null,
129
+ name: 'Test DID',
130
+ }),
131
+ ).rejects.toThrowError('address is required');
132
+
133
+ await expect(
134
+ didProvider.createDIDock({
135
+ address: 'some-address',
136
+ name: '',
137
+ }),
138
+ ).rejects.toThrowError('name is required');
139
+
140
+ await expect(
141
+ createDIDock({
142
+ address: 'some-address',
143
+ name: 'Some name',
144
+ wallet: null,
145
+ }),
146
+ ).rejects.toThrowError('wallet is required');
147
+ });
148
+ });
149
+
150
+ describe('create DID Key', () => {
151
+ it('expect to create a DID Key', async () => {
152
+
153
+ jest.spyOn(didServiceRPC, 'generateKeyDoc').mockResolvedValueOnce({
154
+ id: 'did:key:abcde#key-1',
155
+ type: 'KeyDocument',
156
+ });
157
+
158
+ jest.spyOn(didServiceRPC, 'keypairToDIDKeyDocument').mockResolvedValueOnce({
159
+ didDocument: {
160
+ id: 'did:key:abcde#key-2',
161
+ type: 'DidDocument',
162
+ }
163
+ });
164
+
165
+ jest.spyOn(didServiceRPC, 'getDIDResolution').mockResolvedValueOnce({
166
+ id: new Date().getTime().toString(),
167
+ type: 'DIDResolutionResponse',
168
+ didDocument: {
169
+ id: 'did:key:abcde#key-2',
170
+ type: 'DidDocument',
171
+ },
172
+ correlation: [],
173
+ });
174
+
175
+ await didProvider.createDIDKey({
176
+ name: 'Test DID',
177
+ });
178
+
179
+ const keyDocuments = await wallet.getDocumentsByType('KeyDocument');
180
+ const didDocument = await wallet.getDocumentsByType(
181
+ 'DIDResolutionResponse',
182
+ );
183
+
184
+ expect(keyDocuments.length).toBe(1);
185
+ expect(didDocument.length).toBe(2);
186
+ });
187
+ it('expect to assert parameters', async () => {
188
+
189
+ await expect(
190
+ didProvider.createDIDKey({
191
+ name: ''
192
+ }),
193
+ ).rejects.toThrowError('name is required');
194
+
195
+ await expect(
196
+ createDIDKey({
197
+ name: 'Some name',
198
+ wallet: null,
199
+ }),
200
+ ).rejects.toThrowError('wallet is required');
201
+ });
202
+ });
203
+ });