UNPKG

crypto-conditions

Version:

Implementation of crypto-conditions in JavaScript

155 lines (129 loc) 5.53 kB
'use strict'; /** * @module types */ var _Reflect$construct = require("@babel/runtime-corejs3/core-js-stable/reflect/construct"); var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); _Object$defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/getPrototypeOf")); var _baseSha = _interopRequireDefault(require("./base-sha256")); var _missingDataError = _interopRequireDefault(require("../errors/missing-data-error")); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } /** * PREIMAGE-SHA-256: Hashlock condition using SHA-256. * * This type of condition is also called a hashlock. By creating a hash * of a difficult-to-guess 256-bit random or pseudo-random integer it * is possible to create a condition which the creator can trivially * fulfill by publishing the random value. However, for anyone else, * the condition is cryptgraphically hard to fulfill, because they * would have to find a preimage for the given condition hash. * * PREIMAGE-SHA-256 is assigned the type ID 0. It relies on the SHA-256 * and PREIMAGE feature suites which corresponds to a feature bitmask * of 0x03. */ var PreimageSha256 = /*#__PURE__*/function (_BaseSha) { (0, _inherits2.default)(PreimageSha256, _BaseSha); var _super = _createSuper(PreimageSha256); function PreimageSha256() { (0, _classCallCheck2.default)(this, PreimageSha256); return _super.call(this); } /** * Generate the contents of the condition hash. * * @return {Buffer} Hash payload. * * @private */ (0, _createClass2.default)(PreimageSha256, [{ key: "getFingerprintContents", value: function getFingerprintContents() { if (!this.preimage) { throw new _missingDataError.default('Could not calculate hash, no preimage provided'); } return this.preimage; } /** * Provide a preimage. * * The preimage is the only input to a SHA256 hashlock condition. * * Note that the preimage should contain enough (pseudo-random) data in order * to be difficult to guess. A sufficiently large secret seed and a * cryptographically secure pseudo-random number generator (CSPRNG) can be * used to avoid having to store each individual preimage. * * @param {Buffer} preimage Secret data that will be hashed to form the condition. */ }, { key: "setPreimage", value: function setPreimage(preimage) { if (!Buffer.isBuffer(preimage)) { throw new TypeError('Preimage must be a buffer, was: ' + preimage); } this.preimage = preimage; } }, { key: "parseJson", value: function parseJson(json) { this.preimage = Buffer.from(json.preimage, 'base64'); } }, { key: "getAsn1JsonPayload", value: function getAsn1JsonPayload() { return { preimage: this.preimage }; } /** * Calculate the cost of fulfilling this condition. * * The cost of the preimage condition equals the size of the preimage in * bytes. * * @return {Number} Expected maximum cost to fulfill this condition * @private */ }, { key: "calculateCost", value: function calculateCost() { if (!this.preimage) { throw new _missingDataError.default('Preimage must be specified'); } return this.preimage.length; } /** * Validate this fulfillment. * * For a SHA256 hashlock fulfillment, successful parsing implies that the * fulfillment is valid, so this method is a no-op. * * @param {Buffer} Message (ignored in this condition type) * @return {Boolean} Validation result */ }, { key: "validate", value: function validate(message) { return true; } }]); return PreimageSha256; }(_baseSha.default); PreimageSha256.TYPE_ID = 0; PreimageSha256.TYPE_NAME = 'preimage-sha-256'; PreimageSha256.TYPE_ASN1_CONDITION = 'preimageSha256Condition'; PreimageSha256.TYPE_ASN1_FULFILLMENT = 'preimageSha256Fulfillment'; PreimageSha256.TYPE_CATEGORY = 'simple'; var _default = PreimageSha256; exports.default = _default;