Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example usage script. #162

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# @digitalbazaar/vc ChangeLog

## 7.0.1 - 2024-xx-xx

### Added
- Example roundtrip issue/verify script.

## 7.0.0 - 2024-08-01

### Added
Expand Down
130 changes: 130 additions & 0 deletions examples/rt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*!
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
davidlehn marked this conversation as resolved.
Show resolved Hide resolved
*/

// 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';

// setup document loader
import {CachedResolver} from '@digitalbazaar/did-io';
import {securityLoader} from '@digitalbazaar/security-document-loader';

//import {contexts as secContexts} from '@digitalbazaar/security-context';
import {contexts as diContexts} from '@digitalbazaar/data-integrity-context';

const loader = securityLoader();
//loader.addDocuments({documents: secContexts});
loader.addDocuments({documents: diContexts});
// 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 resolver = new CachedResolver();
const didKeyDriverMultikey = driver();

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

const documentLoader = loader.build();

async function main({credential, documentLoader}) {
// generate example ecdsa keypair

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;

// ensure issuer matches key controller
credential.issuer = ecdsaKeyPair.controller;

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

// setup documentLoader

// sign credential
const verifiableCredential = await vc.issue({
credential,
suite: signingSuite,
documentLoader
});

// setup ecdsa-rdfc-2019 verifying suite
const verifyingSuite = new DataIntegrityProof({
cryptosuite: ecdsaRdfc2019Cryptosuite
});

// verify signed credential
const verifyResult = await vc.verifyCredential({
credential: verifiableCredential,
suite: verifyingSuite,
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(JSON.stringify(verifyResult, null, 2));
}

// 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
});
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@
"@digitalbazaar/credentials-examples-context": "^1.0.0",
"@digitalbazaar/data-integrity": "^2.2.0",
"@digitalbazaar/data-integrity-context": "^2.0.1",
"@digitalbazaar/did-io": "^2.0.0",
"@digitalbazaar/did-method-key": "^5.2.0",
"@digitalbazaar/ecdsa-multikey": "^1.7.0",
"@digitalbazaar/ecdsa-rdfc-2019-cryptosuite": "^1.1.0",
"@digitalbazaar/ecdsa-sd-2023-cryptosuite": "^3.2.1",
"@digitalbazaar/ed25519-signature-2018": "^4.0.0",
"@digitalbazaar/ed25519-verification-key-2018": "^4.0.0",
"@digitalbazaar/multikey-context": "^2.0.1",
"@digitalbazaar/odrl-context": "^1.0.0",
"@digitalbazaar/security-document-loader": "^3.0.0",
"c8": "^10.1.2",
"chai": "^4.5.0",
"cross-env": "^7.0.3",
Expand Down
Loading