exonum-client-cis
Version:
Light Client for Exonum CIS Blockchain
150 lines (125 loc) • 3.85 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hash = hash;
exports.sign = sign;
exports.verifySignature = verifySignature;
exports.keyPair = keyPair;
exports.fromSeed = fromSeed;
exports.randomUint64 = randomUint64;
exports.IMPLS = exports.CRYPTO = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var NIST_CRYPTO = _interopRequireWildcard(require("./nist"));
var GOST_CRYPTO = _interopRequireWildcard(require("./gost"));
var _IMPLS;
/**
* Defines a different sets of available cryptographic primitives.
*
* This constant is used in global library configuration.
*/
var CRYPTO = {
NIST: 0,
GOST: 1
};
exports.CRYPTO = CRYPTO;
var IMPLS = (_IMPLS = {}, (0, _defineProperty2["default"])(_IMPLS, CRYPTO.GOST, GOST_CRYPTO), (0, _defineProperty2["default"])(_IMPLS, CRYPTO.NIST, NIST_CRYPTO), _IMPLS);
/** Helper function that returns set of cryptographic primitives that matches configuration. */
exports.IMPLS = IMPLS;
function currentCrypto() {
var CONFIG = require('../config.js').CONFIG;
return IMPLS[CONFIG.crypto];
} // Define getters for backend-specific constants.
// This is mandatory.
Object.defineProperties(module.exports, {
PUBLIC_KEY_LENGTH: {
enumerable: true,
get: function get() {
return currentCrypto().PUBLIC_KEY_LENGTH;
}
},
HASH_LENGTH: {
enumerable: true,
get: function get() {
return currentCrypto().HASH_LENGTH;
}
},
SECRET_KEY_LENGTH: {
enumerable: true,
get: function get() {
return currentCrypto().SECRET_KEY_LENGTH;
}
},
SIGNATURE_LENGTH: {
enumerable: true,
get: function get() {
return currentCrypto().SIGNATURE_LENGTH;
}
}
});
/**
* Get hash.
*
* Hash algorithm depends on the value of the global library configuration.
*
* @param {Object|Array|Uint8Array} data - object of NewType type or array of 8-bit integers
* @param {Type|Transaction} [type] - optional, used only if data of {Object} type is passed
* @return {string}
*/
function hash(data, type) {
return currentCrypto().hash(data, type);
}
/**
* Get signature.
*
* Signature algorithm depends on the value of the global library configuration.
*
* @param {string} secretKey
* @param {Object|Array} data - object of NewType type or array of 8-bit integers
* @param {Type|Transaction} [type] - optional, used only if data of {Object} type is passed
* @return {Promise<string>}
*/
function sign(secretKey, data, type) {
return currentCrypto().sign(secretKey, data, type);
}
/**
* Verifies signature.
*
* Signature algorithm depends on the value of the global library configuration.
*
* @param {string} signature
* @param {string} publicKey
* @param {Object|Array} data - object of NewType type or array of 8-bit integers
* @param {Type|Transaction} [type] - optional, used only if data of {Object} type is passed
* @return {Promise<boolean>}
*/
function verifySignature(signature, publicKey, data, type) {
return currentCrypto().verifySignature(signature, publicKey, data, type);
}
/**
* Generate random pair of publicKey and secretKey
* @return {Promise<Object>}
* publicKey {string}
* secretKey {string}
*/
function keyPair() {
return currentCrypto().keyPair();
}
/**
* Returns a new signing key pair generated deterministically from a seed
* @return {Object}
* publicKey {string}
* secretKey {Promise<String>}
*/
function fromSeed(seed) {
return currentCrypto().fromSeed(seed);
}
/**
* Get random number of cryptographic quality
* @returns {string}
*/
function randomUint64() {
return currentCrypto().randomUint64();
}