Skip to content

Commit

Permalink
Move suites and utilities to a separate module.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Jan 1, 2019
1 parent a2f6191 commit 74e1a97
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 70 deletions.
10 changes: 4 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
* jsonld-signatures library.
*
* @author Dave Longley
*
* Copyright 2010-2017 Digital Bazaar, Inc.
/*!
* Copyright (c) 2010-2018 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';

if(require('semver').gte(process.version, '8.0.0')) {
module.exports = require('./jsonld-signatures');
} else {
Expand Down
86 changes: 22 additions & 64 deletions lib/jsonld-signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@
const Injector = require('./Injector');
const util = require('./util');

// TODO: only require dynamically as needed or according to build
const suites = {
EcdsaKoblitzSignature2016: require('./suites/EcdsaKoblitzSignature2016'),
Ed25519Signature2018: require('./suites/Ed25519Signature2018'),
LinkedDataSignature: require('./suites/LinkedDataSignature'),
LinkedDataSignature2015: require('./suites/LinkedDataSignature2015'),
GraphSignature2012: require('./suites/GraphSignature2012'),
RsaSignature2018: require('./suites/RsaSignature2018')
};
const {suites, getSuite, getSupportedAlgorithms} = require('./suites');

// load locally embedded contexts
const contexts = require('./contexts');
Expand Down Expand Up @@ -154,57 +146,28 @@ api.sign = util.callbackify(async function sign(input, options = {}) {
{...options, purpose, proofPurposeHandler, purposeParameters});
});

/**
* @param algorithm {string}
* @param injector {Injector}
* @throws {Error} On unsupported algorithm
* @private
* @returns {Suite} Suite instance for given algorithm
*/
function getSuite({algorithm, injector}) {
// no default algorithm; it must be specified
if(!algorithm) {
throw new TypeError('"options.algorithm" must be specified.');
}

const SUPPORTED_ALGORITHMS = _getSupportedAlgorithms();

if(SUPPORTED_ALGORITHMS.indexOf(algorithm) === -1) {
throw new Error(
'Unsupported algorithm "' + algorithm + '"; ' +
'"options.algorithm" must be one of: ' +
JSON.stringify(SUPPORTED_ALGORITHMS));
}

// TODO: won't work with static analysis?
// use signature suite
//const Suite = require('./suites/' + algorithm);
const Suite = suites[algorithm];
return new Suite(injector);
}

/**
* @param options
* @param [options.purpose]
* @param [options.purposeParameters]
* @param injector
* @throws {Error} On unsupported proof purpose handler
* @private
* @returns {{purpose: *, proofPurposeHandler: *, purposeParameters: *}}
*/
function getProofPurposeHandler({options, injector}) {
// instantiate a proofPurpose handler
const {purpose, purposeParameters} = options;
let proofPurposeHandler;
if(purpose) {
const ProofPurposeHandler = api.proofPurposes.use(purpose);
if(!ProofPurposeHandler) {
throw new Error(`Unsupported proof purpose handler "${purpose}".`);
}
proofPurposeHandler = new ProofPurposeHandler(injector);
/**
* @param options
* @param [options.purpose]
* @param [options.purposeParameters]
* @param injector
* @throws {Error} On unsupported proof purpose handler
* @private
* @returns {{purpose: *, proofPurposeHandler: *, purposeParameters: *}}
*/
function getProofPurposeHandler({options, injector}) {
// instantiate a proofPurpose handler
const {purpose, purposeParameters} = options;
let proofPurposeHandler;
if(purpose) {
const ProofPurposeHandler = api.proofPurposes.use(purpose);
if(!ProofPurposeHandler) {
throw new Error(`Unsupported proof purpose handler "${purpose}".`);
}
return {purpose, proofPurposeHandler, purposeParameters};
proofPurposeHandler = new ProofPurposeHandler(injector);
}
return {purpose, proofPurposeHandler, purposeParameters};
}

/**
* Verifies a JSON-LD digitally-signed object.
Expand Down Expand Up @@ -355,7 +318,7 @@ api.verify = util.callbackify(async function verify(input, options) {
// signatures

// create a promise for each signature to be verified
const SUPPORTED_ALGORITHMS = _getSupportedAlgorithms();
const SUPPORTED_ALGORITHMS = getSupportedAlgorithms();
const results = await Promise.all(proofs.map(async proof => {
try {
const algorithm = jsonld.getValues(proof.doc, 'type')[0] || '';
Expand Down Expand Up @@ -394,11 +357,6 @@ api.verify = util.callbackify(async function verify(input, options) {
};
});

function _getSupportedAlgorithms() {
// every suite is supported except the base class
return Object.keys(api.suites).filter(s => s !== 'LinkedDataSignature');
}

function _validateProofPurpose(purpose) {
if(!purpose) {
// TODO: Enable error in the next major release
Expand Down
47 changes: 47 additions & 0 deletions lib/suites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!
* Copyright (c) 2018 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';

const api = {};
module.exports = api;

// TODO: only require dynamically as needed or according to build
api.suites = {
EcdsaKoblitzSignature2016: require('./suites/EcdsaKoblitzSignature2016'),
Ed25519Signature2018: require('./suites/Ed25519Signature2018'),
LinkedDataSignature: require('./suites/LinkedDataSignature'),
LinkedDataSignature2015: require('./suites/LinkedDataSignature2015'),
GraphSignature2012: require('./suites/GraphSignature2012'),
RsaSignature2018: require('./suites/RsaSignature2018')
};

/**
* @param algorithm {string}
* @param injector {Injector}
* @throws {Error} On unsupported algorithm
* @returns {Suite} Suite instance for given algorithm
*/
api.getSuite = ({algorithm, injector}) => {
// no default algorithm; it must be specified
if(!algorithm) {
throw new TypeError('"options.algorithm" must be specified.');
}

const SUPPORTED_ALGORITHMS = api.getSupportedAlgorithms();

if(SUPPORTED_ALGORITHMS.indexOf(algorithm) === -1) {
throw new Error(
'Unsupported algorithm "' + algorithm + '"; ' +
'"options.algorithm" must be one of: ' +
JSON.stringify(SUPPORTED_ALGORITHMS));
}

const Suite = api.suites[algorithm];
return new Suite(injector);
};

api.getSupportedAlgorithms = () => {
// every suite is supported except the base class
return Object.keys(api.suites).filter(s => s !== 'LinkedDataSignature');
};

0 comments on commit 74e1a97

Please sign in to comment.