Skip to content

Commit

Permalink
Merge pull request #320 from docknetwork/feat/bbs-jsonld
Browse files Browse the repository at this point in the history
  • Loading branch information
cykoder authored Nov 3, 2022
2 parents 40f4071 + e5fa547 commit 03ef36d
Show file tree
Hide file tree
Showing 19 changed files with 2,111 additions and 1,325 deletions.
134 changes: 134 additions & 0 deletions example/bbs-dock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import VerifiableCredential from '../src/verifiable-credential';
import { MultiResolver } from '../src/resolver';
import Bls12381G2KeyPairDock2022 from '../src/utils/vc/crypto/Bls12381G2KeyPairDock2022';

const keypairOpts = {
"id": "did:example:489398593#keys-1",
"controller": "did:example:489398593",
"privateKeyBase58": "23fKPbbSJ7tCJVDYynRZQ1wPs6GannD2dEMjKZnFtKxy",
"publicKeyBase58": "GzJYyMYXQyAdmRK3JcfQnHJtz5TyRiYyYtfj6SZD9LQ4zDapQ3qeC1AaBwCtzhQ8En6EGiLe7ke2RaeLNTxJ9GVompK6j6kHB8e5m2ya5qdCB2bbNMVii99QfmYsbj36J3B"
};

class ExampleDIDResolver {
async resolve(did) {
if (did.indexOf('#keys-1') !== -1) {
return {
...keypairOpts,
privateKeyBase58: undefined,
};
}
return {
"@context": "https://w3id.org/security/v2",
"id": did,
"assertionMethod": [`${did}#keys-1`]
};
}
}

const resolver = new MultiResolver({
example: new ExampleDIDResolver(),
});

const residentCardSchema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://ld.dock.io/examples/resident-card-schema.json",
"title": "Resident Card Example",
"type": "object",
"properties": {
"credentialSubject": {
"type": "object",
"properties": {
"givenName": {
"title": "Given Name",
"type": "string"
},
"familyName": {
"title": "Family Name",
"type": "string"
},
"lprNumber": {
"title": "LPR Number",
"type": "integer",
"minimum": 0
}
},
"required": []
}
}
};

const embeddedSchema = {
id: `data:application/json;charset=utf-8,${encodeURIComponent(JSON.stringify(residentCardSchema))}`,
type: 'JsonSchemaValidator2018',
};

// Defining schema allows to specify custom encoding
const example = {
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://w3id.org/citizenship/v1",
"https://ld.dock.io/security/bbs/v1"
],
"id": "https://issuer.oidp.uscis.gov/credentials/83627465",
"type": ["VerifiableCredential", "PermanentResidentCard"],
"issuer": {
"id": "did:example:489398593"
},
"credentialSchema": embeddedSchema,
"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",
"lprNumber": 1234,
}
};

async function signAndVerify(credentialJSON) {
// Incrementally build a verifiable credential
const credential = new VerifiableCredential();
credential.setFromJSON(credentialJSON);
credential.setIssuer(keypairOpts.controller);

// Create the keypair
const keyPair = new Bls12381G2KeyPairDock2022(keypairOpts);

// Sign and print the results
console.log('Signing credential:', JSON.stringify(credential.toJSON(), null, 2));
await credential.sign({ keypair: keyPair, type: keyPair.type });
console.log('Signed credential:', JSON.stringify(credential.toJSON(), null, 2));

// Verify the credential
const verifyResult = await credential.verify({
resolver,
forceRevocationCheck: false,
});

if (verifyResult.verified) {
console.log('Credential has been verified!', verifyResult.results);
} else {
throw verifyResult.error;
}

return credential.toJSON();
}

async function main() {
let exitCode = 0;
try {
await signAndVerify(example);
} catch (e) {
console.error(e);
exitCode = 1;
}

// Exit
process.exit(exitCode);
}

main();
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/sdk",
"version": "2.5.1",
"version": "2.6.0",
"main": "index.js",
"license": "MIT",
"repository": {
Expand All @@ -17,8 +17,7 @@
"@babel/preset-env": "^7.15.6",
"@comunica/actor-init-sparql-rdfjs": "^1.22.3",
"@comunica/types": "^1.0.0",
"@docknetwork/crypto-wasm": "0.14.0",
"@docknetwork/crypto-wasm-ts": "0.23.0",
"@docknetwork/crypto-wasm": "0.16.0",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.1.3",
Expand Down Expand Up @@ -52,6 +51,7 @@
"scripts": {
"jest": "NODE_OPTIONS='--experimental-vm-modules' jest",
"exc": "npx babel-node",
"bbs-dock-example": "npx babel-node example/bbs-dock.js",
"dock-did-example": "npx babel-node example/dock-did.js",
"staking_payouts": "npx babel-node scripts/staking_payouts.js",
"did-resolver-example": "npx babel-node example/resolver.js",
Expand Down Expand Up @@ -86,9 +86,10 @@
"type-check": "tsc --allowJs --checkJs --noEmit --moduleResolution node --resolveJsonModule --target ES6 --skipLibCheck true --allowSyntheticDefaultImports true"
},
"dependencies": {
"@docknetwork/crypto-wasm-ts": "0.26.0",
"@docknetwork/node-types": "^0.13.0",
"@juanelas/base64": "^1.0.5",
"@polkadot/api": "^9.4.3",
"@polkadot/api": "9.4.3",
"@polkadot/keyring": "^10.1.9",
"@polkadot/rpc-provider": "9.4.3",
"@polkadot/types": "9.4.3",
Expand All @@ -102,9 +103,10 @@
"deep-equal": "2.0.5",
"dotenv": "16.0.1",
"elliptic": "6.5.4",
"flat": "^5.0.2",
"js-sha256": "0.9.0",
"json-canonicalize": "1.0.4",
"jsonld": "5.2.0",
"jsonld": "^5.0.0",
"jsonld-signatures": "^9.3.1",
"jsonschema": "1.4.1",
"key-did-resolver": "^1.4.0",
Expand Down
4 changes: 2 additions & 2 deletions src/modules/did/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ class DIDModule {
* @return {Promise<object>} The DID document.
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
async getDocument(did, { getBbsPlusSigKeys = false } = {}) {
async getDocument(did, { getBbsPlusSigKeys = true } = {}) {
const hexId = getHexIdentifierFromDID(did);

let didDetails = await this.api.rpc.core_mods.didDetails(hexId, 15);
Expand Down Expand Up @@ -830,7 +830,7 @@ class DIDModule {
throw new Error(`Curve type should have been Bls12381 but was ${pkObj.curveType}`);
}
const keyIndex = queryKeys[currentIter][1];
keys.push([keyIndex, 'Bls12381G2KeyDock2022', hexToU8a(pkObj.bytes)]);
keys.push([keyIndex, 'Bls12381G2VerificationKeyDock2022', hexToU8a(pkObj.bytes)]);
assertion.push(keyIndex);
}
currentIter++;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/vc/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ import didV1Context from './contexts/did-v1-updated.json';
import credV1Context from './contexts/credential-v1-updated.json';
import schema from './contexts/schema.json';
import odrl from './contexts/odrl.json';
import bbsV1Context from './contexts/bbs-v1.json';
import dockBBSV1Context from './contexts/dock-bbs-v1.json';

// Lookup of following URLs will lead to loading data from the context directory, this is done as the Sr25519 keys are not
// supported in any W3C standard and vc-js has them stored locally. This is a temporary solution.
export default new Map([
[
'https://ld.dock.io/security/bbs/v1',
dockBBSV1Context,
],
[
'https://w3id.org/security/bbs/v1',
bbsV1Context,
],
[
'https://w3c-ccg.github.io/ldp-bbs2020/contexts/v1/',
bbsV1Context,
],
[
'https://www.w3.org/2018/credentials/v1',
credV1Context,
Expand Down
129 changes: 129 additions & 0 deletions src/utils/vc/contexts/bbs-v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"@context": {
"@version": 1.1,
"id": "@id",
"type": "@type",
"BbsBlsSignature2020": {
"@id": "https://w3id.org/security#BbsBlsSignature2020",
"@context": {
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",
"challenge": "https://w3id.org/security#challenge",
"created": {
"@id": "http://purl.org/dc/terms/created",
"@type": "http://www.w3.org/2001/XMLSchema#dateTime"
},
"domain": "https://w3id.org/security#domain",
"proofValue": "https://w3id.org/security#proofValue",
"nonce": "https://w3id.org/security#nonce",
"proofPurpose": {
"@id": "https://w3id.org/security#proofPurpose",
"@type": "@vocab",
"@context": {
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",
"assertionMethod": {
"@id": "https://w3id.org/security#assertionMethod",
"@type": "@id",
"@container": "@set"
},
"authentication": {
"@id": "https://w3id.org/security#authenticationMethod",
"@type": "@id",
"@container": "@set"
}
}
},
"verificationMethod": {
"@id": "https://w3id.org/security#verificationMethod",
"@type": "@id"
}
}
},
"BbsBlsSignatureProof2020": {
"@id": "https://w3id.org/security#BbsBlsSignatureProof2020",
"@context": {
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",

"challenge": "https://w3id.org/security#challenge",
"created": {
"@id": "http://purl.org/dc/terms/created",
"@type": "http://www.w3.org/2001/XMLSchema#dateTime"
},
"domain": "https://w3id.org/security#domain",
"nonce": "https://w3id.org/security#nonce",
"proofPurpose": {
"@id": "https://w3id.org/security#proofPurpose",
"@type": "@vocab",
"@context": {
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",
"sec": "https://w3id.org/security#",
"assertionMethod": {
"@id": "https://w3id.org/security#assertionMethod",
"@type": "@id",
"@container": "@set"
},
"authentication": {
"@id": "https://w3id.org/security#authenticationMethod",
"@type": "@id",
"@container": "@set"
}
}
},
"proofValue": "https://w3id.org/security#proofValue",
"verificationMethod": {
"@id": "https://w3id.org/security#verificationMethod",
"@type": "@id"
}
}
},
"Bls12381G1Key2020": {
"@id": "https://w3id.org/security#Bls12381G1Key2020",
"@context": {
"@protected": true,
"id": "@id",
"type": "@type",
"controller": {
"@id": "https://w3id.org/security#controller",
"@type": "@id"
},
"revoked": {
"@id": "https://w3id.org/security#revoked",
"@type": "http://www.w3.org/2001/XMLSchema#dateTime"
},
"publicKeyBase58": {
"@id": "https://w3id.org/security#publicKeyBase58"
}
}
},
"Bls12381G2Key2020": {
"@id": "https://w3id.org/security#Bls12381G2Key2020",
"@context": {
"@protected": true,
"id": "@id",
"type": "@type",
"controller": {
"@id": "https://w3id.org/security#controller",
"@type": "@id"
},
"revoked": {
"@id": "https://w3id.org/security#revoked",
"@type": "http://www.w3.org/2001/XMLSchema#dateTime"
},
"publicKeyBase58": {
"@id": "https://w3id.org/security#publicKeyBase58"
}
}
}
}
}
Loading

0 comments on commit 03ef36d

Please sign in to comment.