Skip to content

Commit

Permalink
Fix readme instructions, update concat() params
Browse files Browse the repository at this point in the history
  • Loading branch information
wes-smith committed Feb 6, 2024
1 parent 9caa12a commit 337802f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 110 deletions.
103 changes: 3 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,106 +48,9 @@ npm install

## Usage

The following code snippet provides a complete example of digitally signing
a verifiable credential using this library:

```javascript
import * as EcdsaMultikey from '@digitalbazaar/ecdsa-multikey';
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {cryptosuite as ecdsaRdfc2019Cryptosuite} from
'@digitalbazaar/ecdsa-xi-2023-cryptosuite';
import jsigs from 'jsonld-signatures';
const {purposes: {AssertionProofPurpose}} = jsigs;


// 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';
const keyPair = await EcdsaMultikey.from({
'@context': 'https://w3id.org/security/multikey/v1',
id: 'https://example.edu/issuers/565049#zDnaekGZTbQBerwcehBSXLqAg6s55hVEBms1zFy89VHXtJSa9',
type: 'Multikey',
controller: 'https://example.edu/issuers/565049',
publicKeyMultibase: 'zDnaekGZTbQBerwcehBSXLqAg6s55hVEBms1zFy89VHXtJSa9',
secretKeyMultibase: 'z42tqZ5smVag3DtDhjY9YfVwTMyVHW6SCHJi2ZMrD23DGYS3'
});

// export public key and add to document loader
const publicKey = await keyPair.export({publicKey: true, includeContext: true});
addDocumentToLoader({url: publicKey.id, document: publicKey});

// create key's controller document
const controllerDoc = {
'@context': [
'https://www.w3.org/ns/did/v1',
'https://w3id.org/security/multikey/v1'
],
id: controller,
assertionMethod: [publicKey]
};
addDocumentToLoader({url: controllerDoc.id, document: controllerDoc});

// create suite
const suite = new DataIntegrityProof({
signer: keyPair.signer(), cryptosuite: ecdsaRdfc2019Cryptosuite
});

// create signed credential
const signedCredential = await jsigs.sign(unsignedCredential, {
suite,
purpose: new AssertionProofPurpose(),
documentLoader
});

// results in the following signed VC
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
{
"AlumniCredential": "https://schema.org#AlumniCredential",
"alumniOf": "https://schema.org#alumniOf"
},
"https://w3id.org/security/data-integrity/v2"
],
"id": "https://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"
},
"proof": {
"type": "DataIntegrityProof",
"created": "2023-03-01T21:29:24Z",
"verificationMethod": "https://example.edu/issuers/565049#zDnaekGZTbQBerwcehBSXLqAg6s55hVEBms1zFy89VHXtJSa9",
"cryptosuite": "ecdsa-xi-2023",
"proofPurpose": "assertionMethod",
"proofValue": "z5grbn9Tp8xC7p6LpmUdxxRdAx37azC2GQDdHBqq7ivFsaFUJtC81b8puwe2NmaEUYpxXQooXNnXL3M2NqySrzC5Z"
}
}
```
See [testSignVerify.js](https://github.com/digitalbazaar/ecdsa-xi-2023-cryptosuite/blob/main/test/testSignVerify.js) for an example of how to use this library for credential signing and verification.



## Contribute

Expand Down
4 changes: 2 additions & 2 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function createCryptosuite({extraInformation = new Uint8Array()} = {}) {
async function _createSignData({cryptosuite, document, proof, documentLoader}) {
const options = {documentLoader};

//create hash from `extraInformation`
// create hash from `extraInformation`
const hasher = createHasher();
const externalHash = await hasher.hash(cryptosuite.options.extraInformation);

Expand All @@ -36,7 +36,7 @@ async function _createSignData({cryptosuite, document, proof, documentLoader}) {
const docHash = await hasher.hash(stringToUtf8Bytes(docCanon));

// current order of hashing: proof hash + document hash + external hash
const hashConcat = concat(concat(proofHash, docHash), externalHash);
const hashConcat = concat(proofHash, docHash, externalHash);

return hashConcat;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*!
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
export function concat(b1, b2) {
const rval = new Uint8Array(b1.length + b2.length);
export function concat(b1, b2, b3) {
const rval = new Uint8Array(b1.length + b2.length + b3.length);
rval.set(b1, 0);
rval.set(b2, b1.length);
rval.set(b3, b2.length);
return rval;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digitalbazaar/ecdsa-xi-2023-cryptosuite",
"version": "1.0.2-0",
"version": "0.0.1-0",
"description": "An ECDSA-XI-2023 Data Integrity cryptosuite for use with jsonld-signatures.",
"homepage": "https://github.com/digitalbazaar/ecdsa-xi-2023-cryptosuite",
"repository": {
Expand Down
7 changes: 4 additions & 3 deletions test/EcdsaXi2023Cryptosuite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {loader} from './documentLoader.js';

const documentLoader = loader.build();
const extraInformation = new Uint8Array([12, 52, 75, 63, 74, 85, 21, 5, 62, 10,
const extraInformation = new Uint8Array([
12, 52, 75, 63, 74, 85, 21, 5, 62, 10,
12, 52, 75, 63, 74, 85, 21, 5, 62, 100,
12, 52, 75, 63, 74, 85, 21, 5, 62, 100,
12, 52, 75, 63, 74, 85, 21, 5, 62, 100,
Expand Down Expand Up @@ -55,12 +56,12 @@ describe('EcdsaXi2023Cryptosuite', () => {
expect(error).to.not.exist;
expect(result).to.exist;
/* eslint-disable max-len */
const expectedResult = `<https://example.edu/credentials/1872> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://schema.org#AlumniCredential> .
const expectedResult = `<https://example.edu/credentials/1872> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://example.org/examples#AlumniCredential> .
<https://example.edu/credentials/1872> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.w3.org/2018/credentials#VerifiableCredential> .
<https://example.edu/credentials/1872> <https://www.w3.org/2018/credentials#credentialSubject> <https://example.edu/students/alice> .
<https://example.edu/credentials/1872> <https://www.w3.org/2018/credentials#issuanceDate> "2010-01-01T19:23:24Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
<https://example.edu/credentials/1872> <https://www.w3.org/2018/credentials#issuer> <https://example.edu/issuers/565049> .
<https://example.edu/students/alice> <https://schema.org#alumniOf> "Example University" .\n`;
<https://example.edu/students/alice> <https://schema.org/alumniOf> "Example University" .\n`;
/* eslint-enable max-len */
result.should.equal(expectedResult);
});
Expand Down
4 changes: 2 additions & 2 deletions test/mock-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const credential = {
'@context': [
'https://www.w3.org/2018/credentials/v1',
{
AlumniCredential: 'https://schema.org#AlumniCredential',
alumniOf: 'https://schema.org#alumniOf'
AlumniCredential: 'https://example.org/examples#AlumniCredential',
alumniOf: 'https://schema.org/alumniOf'
},
'https://w3id.org/security/data-integrity/v2'
],
Expand Down

0 comments on commit 337802f

Please sign in to comment.