@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,122 @@
1
+ import {
2
+ DataStore,
3
+ DocumentNetworkResolver,
4
+ DocumentResolverProps,
5
+ DocumentResolverResult,
6
+ WalletDocument,
7
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
8
+ import {utilCryptoService} from '@docknetwork/wallet-sdk-wasm/src/services/util-crypto';
9
+
10
+ type ResolverResult = string | null;
11
+
12
+ export const dockDocumentNetworkResolver: DocumentNetworkResolver = async ({
13
+ document,
14
+ dataStore,
15
+ }: DocumentResolverProps): Promise<DocumentResolverResult> => {
16
+ let networkId;
17
+ let isFallback = false;
18
+
19
+ for (const resolver of resolvers) {
20
+ networkId = await resolver({document, dataStore});
21
+
22
+ if (networkId) {
23
+ break;
24
+ }
25
+ }
26
+
27
+ if (!networkId) {
28
+ isFallback = true;
29
+ networkId = dataStore.networkId;
30
+ }
31
+
32
+ return {
33
+ networkId: networkId,
34
+ isFallback,
35
+ };
36
+ };
37
+
38
+ /**
39
+ *
40
+ * Given an Api URL, resolve the network ID
41
+ * For now it will be applied for creds and certs
42
+ * It can be extended to resolve other external URLs
43
+ *
44
+ */
45
+ export function resolveApiNetwork({
46
+ url,
47
+ dataStore,
48
+ }: {
49
+ url: string;
50
+ dataStore: DataStore;
51
+ }) {
52
+ for (const network of dataStore.networks) {
53
+ for (const hostname of network.credentialHostnames) {
54
+ if (url.indexOf(hostname) > -1) {
55
+ return network.id;
56
+ }
57
+ }
58
+ }
59
+
60
+ return null;
61
+ }
62
+
63
+ export async function credentialResolver({
64
+ document,
65
+ dataStore,
66
+ }: DocumentResolverProps): Promise<ResolverResult> {
67
+ if (!document) {
68
+ return null;
69
+ }
70
+
71
+ if (!document.type?.includes('VerifiableCredential')) {
72
+ return null;
73
+ }
74
+
75
+ // TODO: create fallback with DID resolution
76
+
77
+ return resolveApiNetwork({
78
+ url: document.id,
79
+ dataStore,
80
+ });
81
+ }
82
+
83
+ export async function didResolver({
84
+ document,
85
+ dataStore,
86
+ }: DocumentResolverProps): Promise<ResolverResult> {
87
+ // TODO: Define DID resolver
88
+ return null;
89
+ }
90
+
91
+ export async function accountResolver({
92
+ document,
93
+ dataStore,
94
+ }: DocumentResolverProps): Promise<ResolverResult> {
95
+ if (!document) {
96
+ return null;
97
+ }
98
+
99
+ const isAddress = Array.isArray(document.type) ? document.type.includes('Address') : document.type === 'Address';
100
+
101
+ if (!isAddress) {
102
+ return null;
103
+ }
104
+
105
+ const addressPrefixList = dataStore.networks.map(
106
+ network => network?.configs?.addressPrefix,
107
+ );
108
+
109
+ const addressPrefix = await utilCryptoService.getAddressPrefix({
110
+ address: document.id,
111
+ startPrefix: Math.min(...addressPrefixList),
112
+ endPrefix: Math.max(...addressPrefixList),
113
+ });
114
+
115
+ const network = dataStore.networks.find(
116
+ item => item.configs?.addressPrefix === addressPrefix,
117
+ );
118
+
119
+ return network?.id;
120
+ }
121
+
122
+ const resolvers = [credentialResolver, accountResolver, didResolver];
package/src/types.ts ADDED
@@ -0,0 +1,75 @@
1
+ import {
2
+ DataStore,
3
+ DataStoreConfigs,
4
+ DocumentResolverResult,
5
+ WalletDocument,
6
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
7
+ import {Accounts} from '@docknetwork/wallet-sdk-wasm/src/modules/accounts';
8
+ import {EventEmitter} from 'events';
9
+
10
+ export interface IV1Wallet {
11
+ accounts: Accounts;
12
+
13
+ getStatus: () => string;
14
+ setStatus: (status: string) => void;
15
+ eventManager: EventEmitter;
16
+ waitForEvent: (eventName: string) => Promise<any>;
17
+ resolveCorrelations: (id: string) => Promise<WalletDocument[]>;
18
+ query: ({
19
+ id,
20
+ type,
21
+ }?: {
22
+ id?: string;
23
+ type?: string;
24
+ }) => Promise<WalletDocument[]>;
25
+
26
+ ensureNetwork: () => Promise<void>;
27
+ sync: () => Promise<void>;
28
+ remove: (id: string) => Promise<void>;
29
+ create: (json: any) => Promise<WalletDocument>;
30
+ update: (json: any) => Promise<WalletDocument>;
31
+ upsert: (json: any) => Promise<WalletDocument>;
32
+ deleteWallet: () => Promise<void>;
33
+ }
34
+
35
+ export type IWallet = {
36
+ getDocumentById: (id: string) => Promise<WalletDocument>;
37
+ getDocumentsById: (idList: string[]) => Promise<WalletDocument[]>;
38
+ getDocumentsByType: (type: string) => Promise<WalletDocument[]>;
39
+ getAllDocuments: () => Promise<WalletDocument[]>;
40
+ addDocument: (json: any) => Promise<WalletDocument>;
41
+ upsertDocument: (json: any) => Promise<WalletDocument>;
42
+ updateDocument: (json: any) => Promise<WalletDocument>;
43
+ getDocumentCorrelations: (documentId: string) => Promise<WalletDocument[]>;
44
+ getAccountKeyPair: (accountId: string) => Promise<any>;
45
+ /**
46
+ * Remove document by id
47
+ * @param id
48
+ */
49
+ removeDocument: (id: string) => Promise<void>;
50
+ /**
51
+ * Import data from a Universal Wallet 2020 JSON
52
+ * https://w3c-ccg.github.io/universal-wallet-interop-spec/
53
+ *
54
+ * @param json
55
+ */
56
+ importUniversalWalletJSON: (json: any, password: string) => Promise<void>;
57
+ getDocumentsFromEncryptedWallet: (
58
+ json: any,
59
+ password: string,
60
+ ) => Promise<any>;
61
+ exportDocuments: (params: {documents: any; password: string}) => Promise<any>;
62
+ /**
63
+ * Create a Universal Wallet 2020 JSON representation of the wallet
64
+ * https://w3c-ccg.github.io/universal-wallet-interop-spec/
65
+ * @returns JSON representation of the wallet
66
+ */
67
+ exportUniversalWalletJSON: (password: string) => any;
68
+
69
+ setNetwork: (networkId: string) => Promise<void>;
70
+ getNetworkId: () => string;
71
+ resolveDocumentNetwork: (document: any) => Promise<DocumentResolverResult>;
72
+ dataStore: DataStore;
73
+ } & IV1Wallet;
74
+
75
+ export type CreateWalletProps = DataStoreConfigs & {};
@@ -0,0 +1,160 @@
1
+ // TODO: [wallet-sdk] Cleanup wallet-sdk unnused wasm services https://dock-team.atlassian.net/browse/DCKA-1658
2
+ import {IV1Wallet, IWallet} from './types';
3
+ import {createAccountProvider} from './account-provider';
4
+ import {WalletDocument} from '@docknetwork/wallet-sdk-data-store/src/types';
5
+ import * as walletServiceConfigs from '@docknetwork/wallet-sdk-wasm/src/services/wallet/configs';
6
+ import {keyringService} from '@docknetwork/wallet-sdk-wasm/src/services/keyring/index';
7
+ import {walletService} from '@docknetwork/wallet-sdk-wasm/src/services/wallet';
8
+ import {v4 as uuid} from 'uuid';
9
+ import {EventEmitter} from 'events';
10
+
11
+ export async function toV1Wallet(wallet: IWallet): Promise<IWallet> {
12
+ const accounts = await createAccountProvider({
13
+ wallet,
14
+ });
15
+
16
+ const v1Wallet = {
17
+ accounts: accounts,
18
+ create(json: any): Promise<WalletDocument> {
19
+ return wallet.addDocument(document);
20
+ },
21
+ deleteWallet(): Promise<void> {
22
+ return wallet.deleteWallet();
23
+ },
24
+ exportDocuments: (params: {documents: any}) => {
25
+ return walletService.exportDocuments(params);
26
+ },
27
+ ensureNetwork(): Promise<void> {
28
+ return Promise.resolve(undefined);
29
+ },
30
+ async query({
31
+ id,
32
+ type,
33
+ }: {id?: string; type?: string} | undefined): Promise<WalletDocument[]> {
34
+ if (id) {
35
+ return [await wallet.getDocumentById(id)].filter(item => !!item);
36
+ }
37
+
38
+ if (type) {
39
+ return wallet.getDocumentsByType(type);
40
+ }
41
+
42
+ return wallet.getAllDocuments();
43
+ },
44
+ remove(id: string): Promise<void> {
45
+ return wallet.removeDocument(id);
46
+ },
47
+ add(json: any): Promise<WalletDocument> {
48
+ return wallet.addDocument(json);
49
+ },
50
+ resolveCorrelations(id: string): Promise<WalletDocument[]> {
51
+ return wallet.getDocumentCorrelations(id);
52
+ },
53
+ sync(): Promise<void> {
54
+ return Promise.resolve(undefined);
55
+ },
56
+ update(json: any): Promise<WalletDocument> {
57
+ return wallet.updateDocument(json);
58
+ },
59
+ upsert(json: any): Promise<WalletDocument> {
60
+ return wallet.upsertDocument(json);
61
+ },
62
+ };
63
+
64
+ const newWallet = {
65
+ ...v1Wallet,
66
+ ...wallet,
67
+ };
68
+
69
+ (accounts as any).wallet = newWallet;
70
+
71
+ return newWallet;
72
+ }
73
+
74
+ export type KeypairType = 'sr25519' | 'ed25519' | 'ecdsa';
75
+
76
+ /**
77
+ * Wallet service methods
78
+ * @param wallet
79
+ */
80
+ export function toV1WalletService(wallet: IWallet) {
81
+ return {
82
+ getDocumentById: id => {
83
+ return wallet.getDocumentById(id);
84
+ },
85
+ // accounts are not required in a wallet
86
+ // Ideally should move this code to the accounts provider file
87
+ // and make account provider available on its own package
88
+ createAccountDocuments: async (params: {
89
+ name: string;
90
+ json?: string;
91
+ password?: string;
92
+ mnemonic?: string;
93
+ type?: KeypairType;
94
+ derivePath?: string;
95
+ hasBackup?: boolean;
96
+ }) => {
97
+ walletServiceConfigs.validation.createAccountDocuments(params);
98
+
99
+ const {
100
+ name,
101
+ type = 'sr25519',
102
+ derivePath,
103
+ mnemonic,
104
+ json,
105
+ password,
106
+ hasBackup
107
+ } = params;
108
+
109
+ const keyringJson = json
110
+ ? await keyringService.addFromJson({jsonData: json, password})
111
+ : await keyringService.getKeyringPairJSON({mnemonic, derivePath, type});
112
+ const correlations: any[] = [];
113
+
114
+ correlations.push({
115
+ '@context': ['https://w3id.org/wallet/v1'],
116
+ id: uuid(),
117
+ type: 'KeyringPair',
118
+ value: keyringJson,
119
+ });
120
+
121
+ correlations.push({
122
+ '@context': ['https://w3id.org/wallet/v1'],
123
+ id: uuid(),
124
+ type: 'Currency',
125
+ value: 0,
126
+ symbol: 'DOCK',
127
+ });
128
+
129
+ if (mnemonic) {
130
+ correlations.push({
131
+ '@context': ['https://w3id.org/wallet/v1'],
132
+ id: uuid(),
133
+ type: 'Mnemonic',
134
+ value: mnemonic,
135
+ });
136
+ }
137
+
138
+ const correlationsDocs = [];
139
+
140
+ for (let i = 0; i < correlations.length; i++) {
141
+ const doc = correlations[i];
142
+ const addedDoc = await wallet.addDocument(doc);
143
+ correlationsDocs.push(addedDoc);
144
+ }
145
+
146
+ const addressDocument = await wallet.addDocument({
147
+ '@context': ['https://w3id.org/wallet/v1'],
148
+ id: keyringJson.address,
149
+ type: 'Address',
150
+ value: keyringJson.address,
151
+ address: keyringJson.address,
152
+ name,
153
+ hasBackup,
154
+ correlation: correlations.map(doc => doc.id),
155
+ });
156
+
157
+ return [addressDocument, ...correlationsDocs];
158
+ },
159
+ };
160
+ }
@@ -0,0 +1,149 @@
1
+ import {IWallet} from './types';
2
+ import {
3
+ createVerificationController,
4
+ VerificationStatus,
5
+ } from './verification-controller';
6
+ import {createWallet} from './wallet';
7
+ import customerCredentialJSON from './fixtures/customer-credential.json';
8
+ import universityDegreeBBS from './fixtures/university-degree-bbs.json';
9
+ import iiwCredential from './fixtures/iiw-credential.json';
10
+ import iiwTemplate from './fixtures/iiw-template.json';
11
+ import anyCredentialProofRequest from './fixtures/any-credential-proof-request.json';
12
+ import universityDegreeProofRequest from './fixtures/university-degree-proof-request.json';
13
+ import {createDIDProvider, IDIDProvider} from './did-provider';
14
+ import {WalletEvents} from '@docknetwork/wallet-sdk-wasm/src/modules/wallet';
15
+
16
+ describe('Verification provider', () => {
17
+ let wallet: IWallet;
18
+ let didProvider: IDIDProvider;
19
+
20
+ beforeAll(async () => {
21
+ wallet = await createWallet({
22
+ databasePath: ':memory:',
23
+ defaultNetwork: 'testnet',
24
+ });
25
+
26
+ await wallet.waitForEvent(WalletEvents.networkConnected);
27
+
28
+ didProvider = createDIDProvider({
29
+ wallet,
30
+ });
31
+
32
+ await didProvider.ensureDID();
33
+
34
+ await wallet.addDocument(customerCredentialJSON);
35
+ await wallet.addDocument(universityDegreeBBS);
36
+ await wallet.addDocument(iiwCredential);
37
+ });
38
+
39
+ it('expect to create verification controller', async () => {
40
+ const controller = createVerificationController({
41
+ wallet,
42
+ });
43
+
44
+ await controller.start({
45
+ template: anyCredentialProofRequest,
46
+ });
47
+
48
+ const currentDID = await didProvider.getAll();
49
+
50
+ expect(controller.getSelectedDID()).toBe(currentDID[0].didDocument.id);
51
+ expect(controller.getStatus()).toEqual(
52
+ VerificationStatus.SelectingCredentials,
53
+ );
54
+ expect(controller.selectedCredentials.size).toEqual(0);
55
+ expect(controller.getTemplateJSON()).toEqual(anyCredentialProofRequest);
56
+ expect(controller.getFilteredCredentials()).toEqual([
57
+ customerCredentialJSON,
58
+ universityDegreeBBS,
59
+ ]);
60
+ });
61
+
62
+ it('expect to generate presentation for a selected credential', async () => {
63
+ const controller = createVerificationController({
64
+ wallet,
65
+ didProvider,
66
+ });
67
+
68
+ await controller.start({
69
+ template: anyCredentialProofRequest,
70
+ });
71
+
72
+ const credentials = controller.getFilteredCredentials();
73
+
74
+ // select the first credential in the filtered list
75
+ controller.selectedCredentials.set(credentials[0].id, {
76
+ credential: credentials[0],
77
+ });
78
+
79
+ const presentation = await controller.createPresentation();
80
+
81
+ expect(presentation.credentials[0]).toStrictEqual(credentials[0]);
82
+ expect(presentation.type).toEqual(['VerifiablePresentation']);
83
+
84
+ // validate the presentation
85
+ const result = await controller.evaluatePresentation(presentation);
86
+
87
+ expect(result.isValid).toBe(true);
88
+ });
89
+
90
+ it('expect to generate presentation iiw credential', async () => {
91
+ const controller = createVerificationController({
92
+ wallet,
93
+ didProvider,
94
+ });
95
+
96
+ await controller.start({
97
+ template: iiwTemplate,
98
+ });
99
+
100
+ const credentials = controller.getFilteredCredentials();
101
+ const selectedCredentialId = iiwCredential.id;
102
+ const selectedCredential = credentials.find(
103
+ item => item.id === selectedCredentialId,
104
+ );
105
+ // select the first credential in the filtered list
106
+ controller.selectedCredentials.set(selectedCredential.id, {
107
+ credential: selectedCredential,
108
+ attributesToReveal: [
109
+ 'credentialSubject.holderName',
110
+ 'credentialSubject.booleanYesNO',
111
+ ],
112
+ });
113
+
114
+ const presentation = await controller.createPresentation();
115
+
116
+ expect(presentation.credentials[0].id).toEqual(selectedCredential.id);
117
+ expect(presentation.type).toEqual(['VerifiablePresentation']);
118
+
119
+ // validate the presentation
120
+ const result = await controller.evaluatePresentation(presentation);
121
+
122
+ expect(result.isValid).toBe(true);
123
+ });
124
+
125
+ it('expect to generate presentation for a bbs credential, selecting specific attributes to share', async () => {
126
+ const controller = createVerificationController({
127
+ wallet,
128
+ didProvider,
129
+ });
130
+
131
+ await controller.start({
132
+ template: universityDegreeProofRequest,
133
+ });
134
+
135
+ const credentials = controller.getFilteredCredentials();
136
+
137
+ expect(credentials.length).toBe(1);
138
+ // select the first credential in the filtered list
139
+ controller.selectedCredentials.set(credentials[0].id, {
140
+ credential: credentials[0],
141
+ attributesToReveal: ['name', 'credentialSubject.dateOfBirth'],
142
+ });
143
+
144
+ const presentation = await controller.createPresentation();
145
+
146
+ const result = await controller.evaluatePresentation(presentation);
147
+ expect(result.isValid).toBe(true);
148
+ });
149
+ });