Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation. #1

Merged
merged 12 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand All @@ -28,7 +28,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand All @@ -44,7 +44,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand All @@ -60,7 +60,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
14 changes: 2 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
# @digitalbazaar/eddsa-rdfc-2022-cryptosuite Changelog
# @digitalbazaar/eddsa-jcs-2022-cryptosuite Changelog

## 1.1.0 - 2024-08-01

### Changed
- Use `rdf-canonize` directly to use `RDFC-1.0` algorithm.

## 1.0.1 - 2023-11-13

### Fixed
- Fix release version.

## 1.0.0 - 2023-11-13
## 1.0.0 - 2023-11-xx

### Added
- Initial version.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2023, Digital Bazaar, Inc.
Copyright (c) 2024, Digital Bazaar, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# EdDSA RDFC 2022 Data Integrity Cryptosuite _(@digitalbazaar/eddsa-rdfc-2022-cryptosuite)_
# EdDSA JCS 2022 Data Integrity Cryptosuite _(@digitalbazaar/eddsa-jcs-2022-cryptosuite)_

[![Build status](https://img.shields.io/github/actions/workflow/status/digitalbazaar/eddsa-rdfc-2022-cryptosuite/main.yml)](https://github.com/digitalbazaar/eddsa-rdfc-2022-cryptosuite/actions?query=workflow%3A%22Node.js+CI%22)
[![Coverage status](https://img.shields.io/codecov/c/github/digitalbazaar/eddsa-rdfc-2022-cryptosuite)](https://codecov.io/gh/digitalbazaar/eddsa-rdfc-2022-cryptosuite)
[![NPM Version](https://img.shields.io/npm/v/@digitalbazaar/eddsa-rdfc-2022-cryptosuite.svg)](https://npm.im/@digitalbazaar/eddsa-rdfc-2022-cryptosuite)
[![Build status](https://img.shields.io/github/actions/workflow/status/digitalbazaar/eddsa-jcs-2022-cryptosuite/main.yml)](https://github.com/digitalbazaar/eddsa-jcs-2022-cryptosuite/actions?query=workflow%3A%22Node.js+CI%22)
[![Coverage status](https://img.shields.io/codecov/c/github/digitalbazaar/eddsa-jcs-2022-cryptosuite)](https://codecov.io/gh/digitalbazaar/eddsa-jcs-2022-cryptosuite)
[![NPM Version](https://img.shields.io/npm/v/@digitalbazaar/eddsa-jcs-2022-cryptosuite.svg)](https://npm.im/@digitalbazaar/eddsa-jcs-2022-cryptosuite)

> EdDSA 2022 Data Integrity Cryptosuite for use with jsonld-signatures.
> EdDSA 2022 Data Integrity Cryptosuite.

## Table of Contents

Expand Down Expand Up @@ -35,14 +35,14 @@ TBD
To install from NPM:

```
npm install @digitalbazaar/eddsa-rdfc-2022-cryptosuite
npm install @digitalbazaar/eddsa-jcs-2022-cryptosuite
```

To install locally (for development):

```
git clone https://github.com/digitalbazaar/eddsa-rdfc-2022-cryptosuite.git
cd eddsa-rdfc-2022-cryptosuite
git clone https://github.com/digitalbazaar/eddsa-jcs-2022-cryptosuite.git
cd eddsa-jcs-2022-cryptosuite
npm install
```

Expand All @@ -54,8 +54,7 @@ a verifiable credential using this library:
```javascript
import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey';
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {cryptosuite as eddsaRdfc2022CryptoSuite} from
'@digitalbazaar/eddsa-rdfc-2022-cryptosuite';
import {createSignCryptosuite} from '@digitalbazaar/eddsa-jcs-2022-cryptosuite';
import jsigs from 'jsonld-signatures';
const {purposes: {AssertionProofPurpose}} = jsigs;

Expand Down Expand Up @@ -107,8 +106,9 @@ const controllerDoc = {
addDocumentToLoader({url: controllerDoc.id, document: controllerDoc});

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

// create signed credential
Expand Down Expand Up @@ -143,9 +143,17 @@ const signedCredential = await jsigs.sign(unsignedCredential, {
"type": "DataIntegrityProof",
"created": "2022-09-06T21:29:24Z",
"verificationMethod": "https://example.edu/issuers/565049#z6MkwXG2WjeQnNxSoynSGYU8V9j3QzP3JSqhdmkHc6SaVWoT",
"cryptosuite": "eddsa-rdfc-2022",
"cryptosuite": "eddsa-jcs-2022",
"proofPurpose": "assertionMethod",
"proofValue": "z4uwHCobmxKqQfZb7i8QRnNR9J4TR6u4Wkm4DB3ms337gfSpL4UwhTD7KKdPjyAaVJQ4y896FEnB1Vz3uEz14jWoC"
"@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"
],
"proofValue": "z3aKfEmARJBuiiBcmGtzPh5ZHaGm9EAehkyVRDJGRxnTJQwqpdoktM6CD8aJii1RobA34gjVcdSQ7cURYcXtEkav2"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
module.exports = function(config) {

Expand Down
22 changes: 5 additions & 17 deletions lib/canonize.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
/*!
* Copyright (c) 2023-2024 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import * as rdfCanonize from 'rdf-canonize';
import jsonld from 'jsonld';

export async function canonize(input, options) {
// convert to RDF dataset and do canonicalization
options = {
algorithm: 'RDFC-1.0',
format: 'application/n-quads',
base: null,
safe: true,
...options
};
const opts = {...options, produceGeneralizedRdf: false};
delete opts.format;
opts.produceGeneralizedRdf = false;
const dataset = await jsonld.toRDF(input, opts);
return rdfCanonize.canonize(dataset, options);
import canonicalize from 'canonicalize';

export async function canonize(input) {
return canonicalize(input);
}
2 changes: 1 addition & 1 deletion lib/createVerifier.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey';

Expand Down
102 changes: 95 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,102 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import {canonize} from './canonize.js';
import {createVerifier} from './createVerifier.js';
import {name} from './name.js';
import {requiredAlgorithm} from './requiredAlgorithm.js';
import {sha} from './sha.js';

export const cryptosuite = {
canonize,
createVerifier,
name,
requiredAlgorithm,
};
export function createSignCryptosuite() {
return {
name,
canonize,
requiredAlgorithm,
createVerifier: _throwSignUsageError,
createVerifyData: _createVerifyDataFn(
_modifySignProofOptionsAndDocument
),
};
}

export function createVerifyCryptosuite() {
return {
name,
canonize,
requiredAlgorithm,
createVerifier,
createVerifyData: _createVerifyDataFn(
_modifyVerifyProofOptionsAndDocument
),
};
}

function _createVerifyDataFn(modifyProofOptionsAndDocument) {
return async function({cryptosuite, document, proof} = {}) {
if(cryptosuite?.name !== name) {
throw new TypeError(`"cryptosuite.name" must be "${name}".`);
}
// determine digest algorithm from key algorithm
modifyProofOptionsAndDocument({proof, document});

// await both jcs proof hash and jcs document hash
const [proofHash, docHash] = await Promise.all([
// canonize and hash proof
_canonizeProof(proof, {cryptosuite}).then(
jcsProofOptions => sha({string: jcsProofOptions})),
// canonize and hash document
cryptosuite.canonize(document).then(
jcsDocument => sha({string: jcsDocument}))
]);

// concatenate hash of jcs proof options and hash of c14n document
return _concat(proofHash, docHash);
};
}

function _modifyVerifyProofOptionsAndDocument({proof, document}) {
// 4) If proofOptions.@context exists:
if(proof['@context']) {
let proofContext = proof['@context'];
proofContext = Array.isArray(proofContext) ? proofContext : [proofContext];
let docContext = document['@context'];
docContext = Array.isArray(docContext) ? docContext : [docContext];

// 4.1) Check that the securedDocument.@context starts with all values
// contained in the proofOptions.@context in the same order. Otherwise, set
// verified to false and skip to the last step.
for(let i = 0; i < proofContext.length; i++) {
if(proofContext[i] !== docContext[i]) {
throw new Error('document.@context does not start with proof.@context');
}
}
// 4.2) Set unsecuredDocument.@context equal to proofOptions.@context.
document['@context'] = proof['@context'];
}
}

function _modifySignProofOptionsAndDocument({proof, document}) {
// 2) If unsecuredDocument.@context is present, set proof.@context to
// unsecuredDocument.@context.
if(document['@context']) {
proof['@context'] = document['@context'];
}
}

async function _canonizeProof(proofOptions, {cryptosuite}) {
const proof = {...proofOptions};
// `proofValue` must not be included in the proof options
delete proof.proofValue;
return cryptosuite.canonize(proof);
}

function _concat(b1, b2) {
const rval = new Uint8Array(b1.length + b2.length);
rval.set(b1, 0);
rval.set(b2, b1.length);
return rval;
}

function _throwSignUsageError() {
throw new Error('This cryptosuite must only be used with "sign".');
}
4 changes: 2 additions & 2 deletions lib/name.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
export const name = 'eddsa-rdfc-2022';
export const name = 'eddsa-jcs-2022';
2 changes: 1 addition & 1 deletion lib/requiredAlgorithm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
export const requiredAlgorithm = 'Ed25519';
21 changes: 21 additions & 0 deletions lib/sha-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
/* eslint-env browser */
const crypto = self && (self.crypto || self.msCrypto);

const REQUIRED_HASH_ALGORITHM = 'SHA-256';

/**
* Hashes a string of data using SHA-256.
*
* @param {object} options - The options to use.
* @param {string} options.string - The string to hash.
*
* @returns {Uint8Array} The hash digest.
*/
export async function sha({string}) {
const algorithm = REQUIRED_HASH_ALGORITHM;
const bytes = new TextEncoder().encode(string);
return new Uint8Array(await crypto.subtle.digest(algorithm, bytes));
}
19 changes: 19 additions & 0 deletions lib/sha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import crypto from 'node:crypto';

const REQUIRED_HASH_ALGORITHM = 'sha256';

/**
* Hashes a string of data using SHA-256.
*
* @param {object} options - The options to use.
* @param {string} options.string - The string to hash.
*
* @returns {Uint8Array} The hash digest.
*/
export async function sha({string}) {
const algorithm = REQUIRED_HASH_ALGORITHM;
return new Uint8Array(crypto.createHash(algorithm).update(string).digest());
}
Loading
Loading