Skip to content

Commit dfc7284

Browse files
committed
Add roundtrip example.
1 parent d3abfab commit dfc7284

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add support for VC 2.0 Verifiable Presentations issuance and verification.
88
- Add support for VC 2.0 `validFrom` and `validUntil`.
99
- Add Test vectors for VC 2.0 VCs & VPs.
10+
- Example roundtrip issue/verify script.
1011

1112
### Changed
1213
- **BREAKING**: Default issuance now uses VC 2.0 context.

examples/rt.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*!
2+
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
3+
*/
4+
5+
// Example round trip.
6+
// - generate example ECDSA did:key
7+
// - setup 'ecdsa-rdfc-2019 DataIntegrityProof
8+
// - setup document loader
9+
// - sign credential
10+
// - verify credential
11+
12+
import * as EcdsaMultikey from '@digitalbazaar/ecdsa-multikey';
13+
import {cryptosuite as ecdsaRdfc2019Cryptosuite} from
14+
'@digitalbazaar/ecdsa-rdfc-2019-cryptosuite';
15+
//import * as vc from '@digitalbazaar/vc';
16+
import * as vc from '../lib/index.js';
17+
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
18+
import {driver} from '@digitalbazaar/did-method-key';
19+
20+
// document loader support
21+
import {securityLoader} from '@digitalbazaar/security-document-loader';
22+
23+
//import secCtx from '@digitalbazaar/security-context';
24+
import diCtx from '@digitalbazaar/data-integrity-context';
25+
26+
const loader = securityLoader();
27+
//loader.addStatic(
28+
// secCtx.SECURITY_CONTEXT_V2_URL,
29+
// secCtx.contexts.get(secCtx.SECURITY_CONTEXT_V2_URL)
30+
//);
31+
loader.addStatic(diCtx.CONTEXT_URL, diCtx.CONTEXT);
32+
// example static context
33+
loader.addStatic(
34+
'https://example.com/ex/v1',
35+
/* eslint-disable quotes, quote-props, max-len */
36+
{
37+
"@context": {
38+
"ExampleCredential": "https://example.com/ex#Example",
39+
"example": "https://example.com/ex#example"
40+
}
41+
}
42+
/* eslint-enable quotes, quote-props, max-len */
43+
);
44+
45+
const documentLoader = loader.build();
46+
47+
async function main({credential, documentLoader}) {
48+
// generate example ecdsa keypair
49+
const didKeyDriverMultikey = driver();
50+
51+
didKeyDriverMultikey.use({
52+
multibaseMultikeyHeader: 'zDna',
53+
fromMultibase: EcdsaMultikey.from
54+
});
55+
56+
const ecdsaKeyPair = await EcdsaMultikey.generate({curve: 'P-256'});
57+
58+
const {
59+
didDocument, keyPairs, methodFor
60+
} = await didKeyDriverMultikey.fromKeyPair({
61+
verificationKeyPair: ecdsaKeyPair
62+
});
63+
ecdsaKeyPair.id = didDocument.assertionMethod[0];
64+
ecdsaKeyPair.controller = didDocument.id;
65+
66+
// setup ecdsa-rdfc-2019 suite
67+
const suite = new DataIntegrityProof({
68+
signer: ecdsaKeyPair.signer(),
69+
// date: '2023-01-01T01:01:01Z',
70+
cryptosuite: ecdsaRdfc2019Cryptosuite
71+
});
72+
73+
// setup documentLoader
74+
75+
// sign credential
76+
const verifiableCredential = await vc.issue({
77+
credential,
78+
suite,
79+
documentLoader
80+
});
81+
// verify signed credential
82+
const verifyResult = await vc.verifyCredential({
83+
credential: verifiableCredential,
84+
suite,
85+
documentLoader
86+
});
87+
88+
console.log('INPUT CREDENTIAL:');
89+
console.log(JSON.stringify(credential, null, 2));
90+
console.log('SIGNED CREDENTIAL:');
91+
console.log(JSON.stringify(verifiableCredential, null, 2));
92+
console.log('VERIFY RESULT:');
93+
console.log(verifyResult);
94+
}
95+
96+
// sample unsigned credential
97+
const credential =
98+
// Use plain JSON style for data
99+
/* eslint-disable quotes, quote-props, max-len */
100+
{
101+
"@context": [
102+
"https://www.w3.org/2018/credentials/v1",
103+
"https://example.com/ex/v1"
104+
],
105+
"id": "https://example.com/credentials/1872",
106+
"type": ["VerifiableCredential", "ExampleCredential"],
107+
"issuer": "https://example.edu/issuers/565049",
108+
"issuanceDate": "2010-01-01T19:23:24Z",
109+
"credentialSubject": {
110+
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
111+
"example": "Example Data"
112+
}
113+
}
114+
/* eslint-enable quotes, quote-props, max-len */
115+
;
116+
117+
await main({
118+
credential,
119+
documentLoader
120+
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@
4040
"@digitalbazaar/credentials-examples-context": "^1.0.0",
4141
"@digitalbazaar/data-integrity": "^2.0.0",
4242
"@digitalbazaar/data-integrity-context": "^2.0.0",
43+
"@digitalbazaar/did-method-key": "^5.1.0",
4344
"@digitalbazaar/ecdsa-multikey": "^1.6.0",
45+
"@digitalbazaar/ecdsa-rdfc-2019-cryptosuite": "^1.0.1",
4446
"@digitalbazaar/ecdsa-sd-2023-cryptosuite": "^3.0.0",
4547
"@digitalbazaar/ed25519-signature-2018": "^4.0.0",
4648
"@digitalbazaar/ed25519-verification-key-2018": "^4.0.0",
4749
"@digitalbazaar/multikey-context": "^1.0.0",
4850
"@digitalbazaar/odrl-context": "^1.0.0",
51+
"@digitalbazaar/security-document-loader": "^2.0.0",
4952
"c8": "^8.0.1",
5053
"chai": "^4.3.7",
5154
"cross-env": "^7.0.3",

0 commit comments

Comments
 (0)