Skip to content

Instantly share code, notes, and snippets.

@S3bb1
Forked from vongohren/index.js
Created May 7, 2021 11:12
Show Gist options
  • Save S3bb1/ddd74704c0f9980d93e4e6302439594c to your computer and use it in GitHub Desktop.
Save S3bb1/ddd74704c0f9980d93e4e6302439594c to your computer and use it in GitHub Desktop.
const {
BbsBlsSignature2020,
BbsBlsSignatureProof2020,
deriveProof
} = require("@mattrglobal/jsonld-signatures-bbs");
const { extendContextLoader, sign, verify, purposes } = require("jsonld-signatures");
const { resolver } = require('@transmute/did-key.js');
const { documentLoaders } = require("jsonld");
const bs58 = require('bs58');
const document = {
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://w3id.org/citizenship/v1",
"https://w3id.org/security/bbs/v1"
],
"id": "https://issuer.oidp.uscis.gov/credentials/83627465",
"type": ["VerifiableCredential", "PermanentResidentCard"],
"issuer": "did:example:489398593",
"identifier": "83627465",
"name": "Permanent Resident Card",
"description": "Government of Example Permanent Resident Card.",
"issuanceDate": "2019-12-03T12:19:52Z",
"expirationDate": "2029-12-03T12:19:52Z",
"credentialSubject": {
"id": "did:example:b34ca6cd37bbf23",
"type": ["PermanentResident", "Person"],
"givenName": "JOHN",
"familyName": "SMITH",
"gender": "Male",
"image": "data:image/png;base64,iVBORw0KGgokJggg==",
"residentSince": "2015-01-01",
"lprCategory": "C09",
"lprNumber": "999-999-999",
"commuterClassification": "C1",
"birthCountry": "Bahamas",
"birthDate": "1958-07-17"
}
}
const { Bls12381G2KeyPair } = require('@transmute/did-key-bls12381')
const main = async () => {
const keyPair = await Bls12381G2KeyPair.generate()
// Mattrs BbsBlsSignature2020 library needs private and public key in base58 format
// transmutes library dont encodes that as base58, so encode it with the buffers
keyPair.privateKeyBase58 = bs58.encode(keyPair.privateKeyBuffer)
keyPair.publicKeyBase58 = bs58.encode(keyPair.publicKeyBuffer)
document.issuer = keyPair.controller
// the keypair id must be an IRI, otherwise JSON-LD throws an error. transmutes id only contains the #id ... so add the controller before
keyPair.id = keyPair.controller + keyPair.id;
const didDoc = await resolver.resolve(keyPair.controller)
const docLoader = (url) => {
// when the loader requests the controller did document, return the did doument in plain
if(url === keyPair.controller) {
return {
contextUrl: null, // this is for a context via a link header
document: didDoc, // this is the actual document that was loaded
documentUrl: url // this is the actual context URL after redirects
};
}
// when the loader requests the specific key from the controller, return the Mattr Keypair compliant structure (with the base58 stuff)
if(url === keyPair.id) {
return {
contextUrl: null, // this is for a context via a link header
document: keyPair, // this is the actual document that was loaded
documentUrl: url // this is the actual context URL after redirects
};
}
return documentLoaders.node()(url);
}
const documentLoader = extendContextLoader(docLoader);
const signedDocument = await sign(document, {
suite: new BbsBlsSignature2020({ key: keyPair }),
purpose: new purposes.AssertionProofPurpose(),
documentLoader
});
console.log(signedDocument)
let verified = await verify(signedDocument, {
suite: new BbsBlsSignature2020(),
purpose: new purposes.AssertionProofPurpose(),
documentLoader
});
console.log(verified);
if(verified.results[0].error) {
console.log(verified.results[0].error)
console.log(verified.results[0].error.details.frame)
}
}
main()
{
"name": "diwala-did-testing",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@decentralized-identity/ion-tools": "^0.0.8",
"@mattrglobal/bbs-signatures": "^0.5.0",
"@mattrglobal/jsonld-signatures-bbs": "^0.9.0",
"@mattrglobal/node-bbs-signatures": "^0.11.0",
"@transmute/did-key-bls12381": "^0.2.1-unstable.42",
"@transmute/did-key.js": "^0.2.1-unstable.42",
"jsonld": "^5.2.0",
"jsonld-document-loader": "^1.2.0"
},
"scripts": {
"start": "node index.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment