Skip to content

Commit

Permalink
Add roundtrip example.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlehn committed Nov 30, 2023
1 parent ae641ff commit 24a2e4f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Support for VC 2.0 `validUntil` and `validAfter`.
- Support for VP 2.0 presentations issuance and verification.
- Test vectors for VC 2.0 VCs.
- Example roundtrip issue/verify script.

## 6.2.0 - 2023-11-14

Expand Down
120 changes: 120 additions & 0 deletions examples/rt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*!
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
*/

// Example round trip.
// - generate example ECDSA did:key
// - setup 'ecdsa-rdfc-2019 DataIntegrityProof
// - setup document loader
// - sign credential
// - verify credential

import * as EcdsaMultikey from '@digitalbazaar/ecdsa-multikey';
import {cryptosuite as ecdsaRdfc2019Cryptosuite} from
'@digitalbazaar/ecdsa-rdfc-2019-cryptosuite';
//import * as vc from '@digitalbazaar/vc';
import * as vc from '../lib/index.js';
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {driver} from '@digitalbazaar/did-method-key';

// document loader support
import {securityLoader} from '@digitalbazaar/security-document-loader';

//import secCtx from '@digitalbazaar/security-context';
import diCtx from '@digitalbazaar/data-integrity-context';

const loader = securityLoader();
//loader.addStatic(
// secCtx.SECURITY_CONTEXT_V2_URL,
// secCtx.contexts.get(secCtx.SECURITY_CONTEXT_V2_URL)
//);
loader.addStatic(diCtx.CONTEXT_URL, diCtx.CONTEXT);
// example static context
loader.addStatic(
'https://example.com/ex/v1',
/* eslint-disable quotes, quote-props, max-len */
{
"@context": {
"ExampleCredential": "https://example.com/ex#Example",
"example": "https://example.com/ex#example"
}
}
/* eslint-enable quotes, quote-props, max-len */
);

const documentLoader = loader.build();

async function main({credential, documentLoader}) {
// generate example ecdsa keypair
const didKeyDriverMultikey = driver();

didKeyDriverMultikey.use({
multibaseMultikeyHeader: 'zDna',
fromMultibase: EcdsaMultikey.from
});

const ecdsaKeyPair = await EcdsaMultikey.generate({curve: 'P-256'});

const {
didDocument, keyPairs, methodFor
} = await didKeyDriverMultikey.fromKeyPair({
verificationKeyPair: ecdsaKeyPair
});
ecdsaKeyPair.id = didDocument.assertionMethod[0];
ecdsaKeyPair.controller = didDocument.id;

// setup ecdsa-rdfc-2019 suite
const suite = new DataIntegrityProof({
signer: ecdsaKeyPair.signer(),
// date: '2023-01-01T01:01:01Z',
cryptosuite: ecdsaRdfc2019Cryptosuite
});

// setup documentLoader

// sign credential
const verifiableCredential = await vc.issue({
credential,
suite,
documentLoader
});
// verify signed credential
const verifyResult = await vc.verifyCredential({
credential: verifiableCredential,
suite,
documentLoader
});

console.log('INPUT CREDENTIAL:');
console.log(JSON.stringify(credential, null, 2));
console.log('SIGNED CREDENTIAL:');
console.log(JSON.stringify(verifiableCredential, null, 2));
console.log('VERIFY RESULT:');
console.log(verifyResult);
}

// sample unsigned credential
const credential =
// Use plain JSON style for data
/* eslint-disable quotes, quote-props, max-len */
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://example.com/ex/v1"
],
"id": "https://example.com/credentials/1872",
"type": ["VerifiableCredential", "ExampleCredential"],
"issuer": "https://example.edu/issuers/565049",
"issuanceDate": "2010-01-01T19:23:24Z",
"credentialSubject": {
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
"example": "Example Data"
}
}
/* eslint-enable quotes, quote-props, max-len */
;

await main({
credential,
documentLoader
});
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@
"dependencies": {
"@digitalbazaar/credentials-v2-context": "github:digitalbazaar/credentials-v2-context#initial",
"credentials-context": "^2.0.0",
"ed25519-signature-2018-context": "^1.1.0",
"jsonld": "^8.3.1",
"jsonld-signatures": "^11.2.1",
"ed25519-signature-2018-context": "^1.1.0"
"jsonld-signatures": "^11.2.1"
},
"devDependencies": {
"@digitalbazaar/credentials-examples-context": "^1.0.0",
"@digitalbazaar/data-integrity": "^2.0.0",
"@digitalbazaar/data-integrity-context": "^2.0.0",
"@digitalbazaar/did-method-key": "^5.1.0",
"@digitalbazaar/ecdsa-multikey": "^1.6.0",
"@digitalbazaar/ecdsa-rdfc-2019-cryptosuite": "^1.0.1",
"@digitalbazaar/ecdsa-sd-2023-cryptosuite": "^3.0.0",
"@digitalbazaar/ed25519-signature-2018": "^4.0.0",
"@digitalbazaar/ed25519-verification-key-2018": "^4.0.0",
"@digitalbazaar/multikey-context": "^1.0.0",
"@digitalbazaar/odrl-context": "^1.0.0",
"@digitalbazaar/security-document-loader": "^2.0.0",
"c8": "^8.0.1",
"chai": "^4.3.7",
"cross-env": "^7.0.3",
Expand Down

0 comments on commit 24a2e4f

Please sign in to comment.