npm i
npm start
-
-
Save mattcollier/75042d08b5b911df80676ea8b2b0c739 to your computer and use it in GitHub Desktop.
jsigs-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require = require('esm')(module); | |
module.exports = require('./main.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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