Skip to content

Instantly share code, notes, and snippets.

@vongohren
Created February 2, 2022 17:49
Show Gist options
  • Save vongohren/d6317d408290d7aab4bdcee051f9dbd5 to your computer and use it in GitHub Desktop.
Save vongohren/d6317d408290d7aab4bdcee051f9dbd5 to your computer and use it in GitHub Desktop.
How I issue VCs
import { verifiable } from '@transmute/vc.js';
import { documentLoaders } from 'jsonld';
// import { log } from '../utils/logger';
import { Ed25519Signature2018, Ed25519VerificationKey2018 } from '@transmute/ed25519-signature-2018';
export const vcCreateWithSignature = async (signableTemplate, documentLoader, suite) => {
const output = await verifiable.credential.create({
credential: signableTemplate,
documentLoader: documentLoader,
suite: suite,
});
const items = output.items;
const signedDocument = items[0];
return signedDocument;
};
export const documents = {
'https://www.w3.org/2018/credentials/v1': credentialContext,
'https://w3id.org/traceability/v1': tracebilityContext,
'https://credreg.net/ctdlasn/schema/context/json': cregContext,
'https://w3id.org/security/bbs/v1': bbsPlussSecurity,
'https://w3id.org/security/v1': securityV1,
'https://w3id.org/security/v2': securityV2,
};
export const docLoader = (url: string) => {
//Its worth considering caching contextes that we use alot, to speed up things
//Ref: https://github.com/mattrglobal/jsonld-signatures-bbs/issues/115
//If you want too see the url that is loaded uncomment this
// console.log(url)
const context = documents[url];
if (context) {
return {
contextUrl: null, // this is for a context via a link header
document: context, // this is the actual document that was loaded
documentUrl: url, // this is the actual context URL after redirects
};
}
log(`Hitting document request for this url: ${url}`, { url }, 'WARNING');
return documentLoaders.node()(url);
};
const getSuite = (key) => {
// key is a import { Ed25519KeyPair } from '@transmute/did-key-ed25519'; type
const jwk = await key.export({
privateKey: true,
type: DIDKeyTypes.JSONWebKey2020,
});
const verificationKey = await Ed25519VerificationKey2018.from(jwk);
return new Ed25519Signature2018({ key: verificationKey });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment