UNPKG

parsec-lib

Version:

transaction and block implementation

105 lines (66 loc) 10.8 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.MAX_COLOR = exports.OUT_LENGTH = undefined;var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {return typeof obj;} : function (obj) {return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;};var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}(); /** * Copyright (c) 2018-present, Parsec Labs (parseclabs.org) * * This source code is licensed under the GNU Affero General Public License, * version 3, found in the LICENSE file in the root directory of this source * tree. */ var _assert = require('assert');var _assert2 = _interopRequireDefault(_assert); var _bn = require('bn.js');var _bn2 = _interopRequireDefault(_bn);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}} // 32 bytes - value, 20 bytes - address, 2 bytes color var OUT_LENGTH = exports.OUT_LENGTH = 54; var MAX_COLOR = exports.MAX_COLOR = Math.pow(2, 16); var valueHex = function valueHex(value) {return ( (typeof value === 'string' ? new _bn2.default(value, 10) : new _bn2.default(value)).toString(16));};var Output = function () { function Output(valueOrObject, address, color) {_classCallCheck(this, Output); if ((typeof valueOrObject === 'undefined' ? 'undefined' : _typeof(valueOrObject)) === 'object') { this.value = valueOrObject.value; this.color = valueOrObject.color; this.address = valueOrObject.address; } else { this.value = valueOrObject; this.address = address; this.color = color; } if (this.color < 0 || this.color > MAX_COLOR) { throw new Error('Color out of range (0\u2013' + MAX_COLOR + ')'); } }_createClass(Output, [{ key: 'isNFT', value: function isNFT() { return Output.isNFT(this.color); } /* eslint-disable class-methods-use-this */ }, { key: 'getSize', value: function getSize() { // transfer output return OUT_LENGTH; } /* eslint-enable class-methods-use-this */ }, { key: 'toJSON', value: function toJSON() { var rsp = { address: this.address, value: this.value, color: this.color }; return rsp; } /** * Instantiate output from json object. * @param {Object} json * @returns {Outpoint} */ }, { key: 'toRaw', value: function toRaw() { var dataBuf = Buffer.alloc(this.getSize()); dataBuf.write(valueHex(this.value).padStart(64, '0'), 0, 32, 'hex'); dataBuf.writeUInt16BE(this.color, 32); dataBuf.write(this.address.replace('0x', ''), 34, 'hex'); return dataBuf; } }], [{ key: 'fromJSON', value: function fromJSON(json) {(0, _assert2.default)(json, 'Output data is required.');return new Output(json);} /** * Instantiate output from serialized data. * @param {Buffer} data * @param {offset} offset to read from in buffer * @returns {Output} */ }, { key: 'fromRaw', value: function fromRaw(buf) {var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; // const value = readUint64(buf, offset); // console.log('from', buf.slice(0, 32).toString('hex')); var color = buf.readUInt16BE(offset + 32);var valueString = buf.slice(offset, offset + 32).toString('hex');var value = Output.isNFT(color) ? new _bn2.default(valueString, 16).toString(10) : parseInt(valueString, 16);var address = '0x' + buf.slice(offset + 34, offset + 54).toString('hex'); // transfer output return new Output(value, address, color);} }, { key: 'isNFT', value: function isNFT(color) {return color > Math.pow(2, 15);} }]);return Output;}();exports.default = Output;