UNPKG

exonum-client-cis

Version:

Light Client for Exonum CIS Blockchain

225 lines (182 loc) 6.6 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); 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.SECRET_KEY_LENGTH = exports.HASH_LENGTH = void 0; var _bigInteger = _interopRequireDefault(require("big-integer")); var _sha = _interopRequireDefault(require("sha.js")); var _tweetnacl = _interopRequireDefault(require("tweetnacl")); var _generic = require("../types/generic"); var _message = require("../types/message"); var validate = _interopRequireWildcard(require("../types/validate")); var convert = _interopRequireWildcard(require("../types/convert")); /** * Byte size of a hash. * @type {number} */ var HASH_LENGTH = 32; exports.HASH_LENGTH = HASH_LENGTH; var SECRET_KEY_LENGTH = 64; /** * Get SHA256 hash * @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} */ exports.SECRET_KEY_LENGTH = SECRET_KEY_LENGTH; function hash(data, type) { var buffer; if ((0, _generic.isType)(type)) { buffer = type.serialize(data); } else if ((0, _message.isTransaction)(type)) { var payloadBuffer = data.schema.encode(data.payload).finish(); var chainIdBytesBuffer = data.chainIdBytes; var authorBuffer = convert.hexadecimalToUint8Array(data.author); return (0, _sha["default"])('sha256').update(chainIdBytesBuffer).update(payloadBuffer).update(authorBuffer).digest('hex'); } else { if (type !== undefined) { throw new TypeError('Wrong type of data.'); } if (data instanceof Uint8Array) { buffer = data; } else { if (!Array.isArray(data)) { throw new TypeError('Invalid data parameter.'); } buffer = new Uint8Array(data); } } return (0, _sha["default"])('sha256').update(buffer, 'utf8').digest('hex'); } /** * Get ED25519 signature * @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) { var secretKeyUint8Array; var buffer; var signature; if (!validate.validateHexadecimal(secretKey, 64)) { throw new TypeError('secretKey of wrong type is passed. Hexadecimal expected.'); } secretKeyUint8Array = convert.hexadecimalToUint8Array(secretKey); if ((0, _generic.isType)(type)) { buffer = new Uint8Array(type.serialize(data)); } else if ((0, _message.isTransaction)(type)) { var chainIdBuffer = new Uint8Array(type.chain_id_bytes); var dataBuffer = new Uint8Array(type.serialize(data)); buffer = new Uint8Array(chainIdBuffer.length + dataBuffer.length); buffer.set(chainIdBuffer); buffer.set(dataBuffer, chainIdBuffer.length); } else { if (type !== undefined) { throw new TypeError('Wrong type of data.'); } if (data instanceof Uint8Array) { buffer = data; } else { if (!Array.isArray(data)) { throw new TypeError('Invalid data parameter.'); } buffer = new Uint8Array(data); } } signature = _tweetnacl["default"].sign.detached(buffer, secretKeyUint8Array); return new Promise(function (resolve) { return resolve(convert.uint8ArrayToHexadecimal(signature)); }); } /** * Verifies ED25519 signature * @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) { var signatureUint8Array; var publicKeyUint8Array; var buffer; if (!validate.validateHexadecimal(signature, 64)) { throw new TypeError('Signature of wrong type is passed. Hexadecimal expected.'); } signatureUint8Array = convert.hexadecimalToUint8Array(signature); if (!validate.validateHexadecimal(publicKey)) { throw new TypeError('publicKey of wrong type is passed. Hexadecimal expected.'); } publicKeyUint8Array = convert.hexadecimalToUint8Array(publicKey); if ((0, _generic.isType)(type)) { buffer = new Uint8Array(type.serialize(data)); } else if ((0, _message.isTransaction)(type)) { var chainIdBuffer = (0, _message.chainIdToBytes)(type.chainId); var dataBuffer = new Uint8Array(type.serialize(data)); buffer = new Uint8Array(chainIdBuffer.length + dataBuffer.length); buffer.set(chainIdBuffer); buffer.set(dataBuffer, chainIdBuffer.length); } else if (type === undefined) { if (data instanceof Uint8Array) { buffer = data; } else if (Array.isArray(data)) { buffer = new Uint8Array(data); } } else { throw new TypeError('Wrong type of data.'); } return new Promise(function (resolve) { return resolve(_tweetnacl["default"].sign.detached.verify(buffer, signatureUint8Array, publicKeyUint8Array)); }); } /** * Generate random pair of publicKey and secretKey * @return {Object} * publicKey {string} * secretKey {string} */ function keyPair() { var pair = _tweetnacl["default"].sign.keyPair(); var publicKey = convert.uint8ArrayToHexadecimal(pair.publicKey); var secretKey = convert.uint8ArrayToHexadecimal(pair.secretKey); return new Promise(function (resolve) { return resolve({ publicKey: publicKey, secretKey: secretKey }); }); } /** * Returns a new signing key pair generated deterministically from a 32-byte seed * @return {Object} * publicKey {string} * secretKey {Promise<string>} */ function fromSeed(seed) { var pair = _tweetnacl["default"].sign.keyPair.fromSeed(seed); var publicKey = convert.uint8ArrayToHexadecimal(pair.publicKey); var secretKey = convert.uint8ArrayToHexadecimal(pair.secretKey); return new Promise(function (resolve) { return resolve({ publicKey: publicKey, secretKey: secretKey }); }); } /** * Get random number of cryptographic quality * @returns {string} */ function randomUint64() { var buffer = _tweetnacl["default"].randomBytes(8); return _bigInteger["default"].fromArray(Array.from(buffer), 256).toString(); }