-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move suites and utilities to a separate module.
- Loading branch information
Showing
3 changed files
with
73 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |