@learncard/vc-api-plugin 1.1.24 → 1.1.26
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/README.md +12 -7
- package/package.json +57 -55
- package/src/helpers.ts +14 -0
- package/src/index.ts +125 -0
- package/src/types.ts +29 -0
package/README.md
CHANGED
|
@@ -9,25 +9,26 @@
|
|
|
9
9
|
The LearnCard Core is a pluggable, open-source, universal digital wallet to enable any individual or organization to seamlessly **issue, earn, store, share, and spend currency and credentials** built for the future of education and work.
|
|
10
10
|
|
|
11
11
|
## Documentation
|
|
12
|
+
|
|
12
13
|
All LearnCard documentation can be found at:
|
|
13
14
|
https://docs.learncard.com
|
|
14
15
|
|
|
15
16
|
## Install
|
|
16
17
|
|
|
17
18
|
```bash
|
|
18
|
-
|
|
19
|
+
bun add @learncard/core
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
## Usage
|
|
22
23
|
|
|
23
24
|
### Instantiation
|
|
24
25
|
|
|
25
|
-
Instantiate a wallet using `initLearnCard`. This method accepts a unique identifier string that is
|
|
26
|
+
Instantiate a wallet using `initLearnCard`. This method accepts a unique identifier string that is
|
|
26
27
|
up to 64 characters long. If it is less than 64 characters, `initLearnCard` will pad the start of
|
|
27
28
|
the string with 0's until it is 64 characters long.
|
|
28
29
|
|
|
29
30
|
```js
|
|
30
|
-
import { initLearnCard } from
|
|
31
|
+
import { initLearnCard } from '@learncard/core';
|
|
31
32
|
|
|
32
33
|
const wallet = await initLearnCard({ seed: 'a'.repeat(64) });
|
|
33
34
|
```
|
|
@@ -35,6 +36,7 @@ const wallet = await initLearnCard({ seed: 'a'.repeat(64) });
|
|
|
35
36
|
### Issuing/Verifying Credentials and Presentations
|
|
36
37
|
|
|
37
38
|
#### Issue a credential
|
|
39
|
+
|
|
38
40
|
```js
|
|
39
41
|
// Grab a test VC, or create your own!
|
|
40
42
|
const unsignedVc = await wallet.invoke.getTestVc();
|
|
@@ -43,6 +45,7 @@ const vc = await wallet.invoke.issueCredential(unsignedVc);
|
|
|
43
45
|
```
|
|
44
46
|
|
|
45
47
|
#### Verify a credential
|
|
48
|
+
|
|
46
49
|
```js
|
|
47
50
|
const result = await wallet.invoke.verifyCredential(vc, {}, true);
|
|
48
51
|
|
|
@@ -53,11 +56,13 @@ else console.log('This credential is valid!');
|
|
|
53
56
|
```
|
|
54
57
|
|
|
55
58
|
#### Issue a presentation
|
|
59
|
+
|
|
56
60
|
```js
|
|
57
61
|
const vp = await wallet.invoke.issuePresentation(vc);
|
|
58
62
|
```
|
|
59
63
|
|
|
60
64
|
#### Verify a presentation
|
|
65
|
+
|
|
61
66
|
```js
|
|
62
67
|
const result = await wallet.invoke.verifyPresentation(vp);
|
|
63
68
|
|
|
@@ -74,14 +79,14 @@ else console.log('This presentation is valid!');
|
|
|
74
79
|
To maintain co-ownership of credentials, it is best to store credentials in a public place, and then
|
|
75
80
|
store references to that public place. While this is not the only way to store credentials (and is
|
|
76
81
|
also definitely not a silver bullet! E.g. credentials containing private data), it is the opinion of
|
|
77
|
-
this library that it should be used by default. As a result, instantiating a wallet, will
|
|
78
|
-
automatically connect you to WeLibrary's ceramic node, and allow you to publish and retrieve
|
|
82
|
+
this library that it should be used by default. As a result, instantiating a wallet, will
|
|
83
|
+
automatically connect you to WeLibrary's ceramic node, and allow you to publish and retrieve
|
|
79
84
|
credentials there using IDX.
|
|
80
85
|
|
|
81
86
|
#### Publish Credential
|
|
82
87
|
|
|
83
88
|
After signing a VC, you may choose to publish that credential to Ceramic. Doing so will return a
|
|
84
|
-
stream ID, which you may share to the recipient. That stream ID can then be used to resolve the
|
|
89
|
+
stream ID, which you may share to the recipient. That stream ID can then be used to resolve the
|
|
85
90
|
issued credential. This means both the issuer and recipient may store the _stream ID_ instead of the
|
|
86
91
|
credential itself.
|
|
87
92
|
|
|
@@ -126,6 +131,7 @@ const vcs = await Promise.all(uris.map(async uri => wallet.read.get(uri)));
|
|
|
126
131
|
```
|
|
127
132
|
|
|
128
133
|
## Contributing
|
|
134
|
+
|
|
129
135
|
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
|
130
136
|
|
|
131
137
|
Please make sure to update tests as appropriate.
|
|
@@ -134,7 +140,6 @@ Please make sure to update tests as appropriate.
|
|
|
134
140
|
|
|
135
141
|
**[Learning Economy Foundation (LEF)](https://www.learningeconomy.io)** is a 501(c)(3) non-profit organization leveraging global standards and web3 protocols to bring quality skills and equal opportunity to every human on earth, and address the persistent inequities that exist around the globe in education and employment. We help you build the future of education and work with:
|
|
136
142
|
|
|
137
|
-
|
|
138
143
|
## License
|
|
139
144
|
|
|
140
145
|
MIT © [Learning Economy Foundation](https://github.com/Learning-Economy-Foundation)
|
package/package.json
CHANGED
|
@@ -1,56 +1,58 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
2
|
+
"name": "@learncard/vc-api-plugin",
|
|
3
|
+
"version": "1.1.26",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/vc-api-plugin.esm.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"development": "./src/index.ts",
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/vc-api-plugin.esm.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "node ./scripts/build.mjs && shx cp ./scripts/mixedEntypoint.js ./dist/index.cjs && tsc --p tsconfig.json && shx cp ./dist/index.d.ts ./dist/index.d.cts",
|
|
27
|
+
"test": "jest --passWithNoTests",
|
|
28
|
+
"test:watch": "jest --watch",
|
|
29
|
+
"test:coverage": "jest --silent --ci --coverage --coverageReporters=\"text\" --coverageReporters=\"text-summary\""
|
|
30
|
+
},
|
|
31
|
+
"author": "Learning Economy Foundation (www.learningeconomy.io)",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"homepage": "https://github.com/learningeconomy/LearnCard/tree/main/packages/plugins/vc-api/README.md",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/learningeconomy/LearnCard"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/learningeconomy/LearnCard/issues"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/jest": "^29.2.2",
|
|
43
|
+
"@types/node": "^17.0.31",
|
|
44
|
+
"aqu": "0.4.3",
|
|
45
|
+
"esbuild": "^0.27.1",
|
|
46
|
+
"esbuild-jest": "^0.5.0",
|
|
47
|
+
"esbuild-plugin-copy": "^1.3.0",
|
|
48
|
+
"jest": "^29.3.0",
|
|
49
|
+
"shx": "^0.3.4",
|
|
50
|
+
"ts-jest": "^29.0.3"
|
|
51
|
+
},
|
|
52
|
+
"types": "./dist/index.d.ts",
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@learncard/core": "9.4.26",
|
|
55
|
+
"@learncard/types": "5.17.5"
|
|
56
|
+
},
|
|
57
|
+
"sideEffects": false
|
|
58
|
+
}
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sends a POST request via fetch
|
|
3
|
+
*/
|
|
4
|
+
export const post = async (url: string, data: Record<string, any>): Promise<Response> =>
|
|
5
|
+
fetch(url, {
|
|
6
|
+
method: 'POST',
|
|
7
|
+
headers: { 'Content-Type': 'application/json' },
|
|
8
|
+
body: JSON.stringify(data),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns whether or not an HTTP status code is successful
|
|
13
|
+
*/
|
|
14
|
+
export const isSuccessful = (status: number) => status.toString().startsWith('2');
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UnsignedVCValidator,
|
|
3
|
+
VCValidator,
|
|
4
|
+
UnsignedVPValidator,
|
|
5
|
+
VPValidator,
|
|
6
|
+
VerificationCheckValidator,
|
|
7
|
+
} from '@learncard/types';
|
|
8
|
+
|
|
9
|
+
import { VCAPIPlugin } from './types';
|
|
10
|
+
import { isSuccessful, post } from './helpers';
|
|
11
|
+
|
|
12
|
+
export * from './types';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @group Plugins
|
|
16
|
+
*/
|
|
17
|
+
export const getVCAPIPlugin = async ({
|
|
18
|
+
url: _url,
|
|
19
|
+
did: _did,
|
|
20
|
+
}: {
|
|
21
|
+
url: string;
|
|
22
|
+
did?: string;
|
|
23
|
+
}): Promise<VCAPIPlugin> => {
|
|
24
|
+
const url = _url.endsWith('/') ? _url.slice(0, -1) : _url;
|
|
25
|
+
let did = _did;
|
|
26
|
+
|
|
27
|
+
if (!did) {
|
|
28
|
+
const res = await fetch(`${url}/did`);
|
|
29
|
+
|
|
30
|
+
if (res.status === 200) did = await res.text();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!did) throw new Error('Could not retrieve did from VC API! Please suppy a did.');
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
name: 'VC API',
|
|
37
|
+
displayName: 'VC API',
|
|
38
|
+
description:
|
|
39
|
+
'Handles issuing and signing credentials/presentations using an external VC API',
|
|
40
|
+
id: {
|
|
41
|
+
did: () => did!,
|
|
42
|
+
keypair: () => {
|
|
43
|
+
throw new Error('Cannot get keypair when using VC API');
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
methods: {
|
|
47
|
+
getSubjectDid: () => did!,
|
|
48
|
+
issueCredential: async (_learnCard, credential, options) => {
|
|
49
|
+
await UnsignedVCValidator.parseAsync(credential);
|
|
50
|
+
|
|
51
|
+
const response = await post(`${url}/credentials/issue`, { credential, options });
|
|
52
|
+
|
|
53
|
+
if (!isSuccessful(response.status)) {
|
|
54
|
+
throw new Error(`API Error: ${response.status} ${response.statusText}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return VCValidator.parseAsync(await response.json());
|
|
58
|
+
},
|
|
59
|
+
verifyCredential: async (_learnCard, verifiableCredential, options) => {
|
|
60
|
+
await VCValidator.parseAsync(verifiableCredential);
|
|
61
|
+
|
|
62
|
+
const response = await post(`${url}/credentials/verify`, {
|
|
63
|
+
verifiableCredential,
|
|
64
|
+
options,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (!isSuccessful(response.status)) {
|
|
68
|
+
throw new Error(`API Error: ${response.status} ${response.statusText}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return VerificationCheckValidator.parseAsync(await response.json());
|
|
72
|
+
},
|
|
73
|
+
issuePresentation: async (_learnCard, presentation, options) => {
|
|
74
|
+
await UnsignedVPValidator.parseAsync(presentation);
|
|
75
|
+
|
|
76
|
+
const response = await post(`${url}/presentations/issue`, {
|
|
77
|
+
presentation,
|
|
78
|
+
options,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (!isSuccessful(response.status)) {
|
|
82
|
+
throw new Error(`API Error: ${response.status} ${response.statusText}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return VPValidator.parseAsync(await response.json());
|
|
86
|
+
},
|
|
87
|
+
verifyPresentation: async (_learnCard, verifiablePresentation, options) => {
|
|
88
|
+
await VPValidator.parseAsync(verifiablePresentation);
|
|
89
|
+
|
|
90
|
+
const response = await post(`${url}/presentations/verify`, {
|
|
91
|
+
verifiablePresentation,
|
|
92
|
+
options,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (!isSuccessful(response.status)) {
|
|
96
|
+
throw new Error(`API Error: ${response.status} ${response.statusText}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return VerificationCheckValidator.parseAsync(await response.json());
|
|
100
|
+
},
|
|
101
|
+
getTestVc: (_learnCard, subject = 'did:example:d23dd687a7dc6787646f2eb98d0') => {
|
|
102
|
+
return {
|
|
103
|
+
'@context': ['https://www.w3.org/2018/credentials/v1'],
|
|
104
|
+
id: 'http://example.org/credentials/3731',
|
|
105
|
+
type: ['VerifiableCredential'],
|
|
106
|
+
issuer: did!,
|
|
107
|
+
issuanceDate: '2020-08-19T21:41:50Z',
|
|
108
|
+
credentialSubject: { id: subject },
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
getTestVp: async (_learnCard, _credential) => {
|
|
112
|
+
const credential =
|
|
113
|
+
_credential ||
|
|
114
|
+
(await _learnCard.invoke.issueCredential(_learnCard.invoke.getTestVc()));
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
'@context': ['https://www.w3.org/2018/credentials/v1'],
|
|
118
|
+
type: ['VerifiablePresentation'],
|
|
119
|
+
holder: did,
|
|
120
|
+
verifiableCredential: credential,
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { UnsignedVC, UnsignedVP, VC, VP, VerificationCheck } from '@learncard/types';
|
|
2
|
+
import { Plugin } from '@learncard/core';
|
|
3
|
+
|
|
4
|
+
/** @group VC-API Plugin */
|
|
5
|
+
export type APIOptions = {
|
|
6
|
+
created?: string;
|
|
7
|
+
challenge?: string;
|
|
8
|
+
domain?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** @group VC-API Plugin */
|
|
12
|
+
export type VCAPIPluginMethods = {
|
|
13
|
+
getSubjectDid: (type: string) => string;
|
|
14
|
+
issueCredential: (credential: UnsignedVC, signingOptions?: APIOptions) => Promise<VC>;
|
|
15
|
+
verifyCredential: (
|
|
16
|
+
credential: VC,
|
|
17
|
+
options?: Omit<APIOptions, 'created'>
|
|
18
|
+
) => Promise<VerificationCheck>;
|
|
19
|
+
issuePresentation: (presentation: UnsignedVP, signingOptions?: APIOptions) => Promise<VP>;
|
|
20
|
+
verifyPresentation: (
|
|
21
|
+
presentation: VP,
|
|
22
|
+
options?: Omit<APIOptions, 'created'>
|
|
23
|
+
) => Promise<VerificationCheck>;
|
|
24
|
+
getTestVc: (subject?: string) => UnsignedVC;
|
|
25
|
+
getTestVp: (credential?: VC) => Promise<UnsignedVP>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** @group VC-API Plugin */
|
|
29
|
+
export type VCAPIPlugin = Plugin<'VC API', 'id', VCAPIPluginMethods>;
|