Skip to content

Instantly share code, notes, and snippets.

@mattcollier
Last active October 9, 2023 14:21
Show Gist options
  • Save mattcollier/75042d08b5b911df80676ea8b2b0c739 to your computer and use it in GitHub Desktop.
Save mattcollier/75042d08b5b911df80676ea8b2b0c739 to your computer and use it in GitHub Desktop.
jsigs-example
node_modules

jsigs-example

Run

npm i
npm start
require = require('esm')(module);
module.exports = require('./main.js');
import jsigs from 'jsonld-signatures';
const { purposes: { AssertionProofPurpose } } = jsigs;
import {Ed25519VerificationKey2020} from '@digitalbazaar/ed25519-verification-key-2020';
import {Ed25519Signature2020} from '@digitalbazaar/ed25519-signature-2020';
import { JsonLdDocumentLoader } from 'jsonld-document-loader';
import credContext from 'credentials-context';
const {contexts, constants: contextConstants} = credContext;
const jsonLdDocumentLoader = new JsonLdDocumentLoader();
jsonLdDocumentLoader.addStatic('https://www.w3.org/2018/credentials/v1', contexts.get('https://www.w3.org/2018/credentials/v1'));
jsonLdDocumentLoader.addStatic(Ed25519Signature2020.CONTEXT_URL, Ed25519Signature2020.CONTEXT);
// create the unsigned credential
const unsignedCredential = {
'@context': [
'https://www.w3.org/2018/credentials/v1',
{
AlumniCredential: 'https://schema.org#AlumniCredential',
alumniOf: 'https://schema.org#alumniOf',
},
],
id: 'http://example.edu/credentials/1872',
type: ['VerifiableCredential', 'AlumniCredential'],
issuer: 'https://example.edu/issuers/565049',
issuanceDate: '2010-01-01T19:23:24Z',
credentialSubject: {
id: 'https://example.edu/students/alice',
alumniOf: 'Example University',
},
};
// create the keypair to use when signing
const controller = 'https://example.edu/issuers/565049';
async function run() {
const keyPair = await Ed25519VerificationKey2020.from({
type: 'Ed25519VerificationKey2020',
controller,
id: `${controller}#z6MknCCLeeHBUaHu4aHSVLDCYQW9gjVJ7a63FpMvtuVMy53T`,
publicKeyMultibase: 'z6MknCCLeeHBUaHu4aHSVLDCYQW9gjVJ7a63FpMvtuVMy53T',
privateKeyMultibase: 'zrv2EET2WWZ8T1Jbg4fEH5cQxhbUS22XxdweypUbjWVzv1YD6VqYu' + 'W6LH7heQCNYQCuoKaDwvv2qCWz3uBzG2xesqmf',
});
const suite = new Ed25519Signature2020({ key: keyPair });
suite.date = '2010-01-01T19:23:24Z';
const signedCredential = await jsigs.sign(unsignedCredential, {
suite,
purpose: new AssertionProofPurpose(),
documentLoader: jsonLdDocumentLoader.documentLoader.bind(jsonLdDocumentLoader)
});
console.log(signedCredential);
}
run().catch(console.error);
{
"name": "t2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@digitalbazaar/ed25519-signature-2020": "^3.0.0",
"@digitalbazaar/ed25519-verification-key-2020": "^3.2.0",
"credentials-context": "^2.0.0",
"esm": "^3.2.25",
"jsonld-document-loader": "^1.2.0",
"jsonld-signatures": "^9.3.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment