UNPKG

ethereum-web-token

Version:

EWT bundles Ethereum function-calls into [JWT](https://jwt.io/)-like tokens. It simplifies the use of ECDSA signatures for webapps and the development of [Smart Oracles](https://github.com/codius/codius/wiki/Smart-Oracles:-A-Simple,-Powerful-Approach-to-S

125 lines (96 loc) 4.12 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);var _createClass2 = require('babel-runtime/helpers/createClass');var _createClass3 = _interopRequireDefault(_createClass2); var _pad = require('../util/pad');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var TYPES = ['raw', 'prefixed', 'fixedArray', 'array'];var Mediate = function () { function Mediate(type, value) {(0, _classCallCheck3.default)(this, Mediate); Mediate.validateType(type); this._type = type; this._value = value; }(0, _createClass3.default)(Mediate, [{ key: 'initLength', value: function initLength() { switch (this._type) { case 'raw': return this._value.length / 2; case 'array': case 'prefixed': return 32; case 'fixedArray': return this._value. reduce(function (total, mediate) { return total + mediate.initLength(); }, 0);} } }, { key: 'closingLength', value: function closingLength() { switch (this._type) { case 'raw': return 0; case 'prefixed': return this._value.length / 2; case 'array': return this._value. reduce(function (total, mediate) { return total + mediate.initLength(); }, 32); case 'fixedArray': return this._value. reduce(function (total, mediate) { return total + mediate.initLength() + mediate.closingLength(); }, 0);} } }, { key: 'init', value: function init( suffixOffset) {var _this = this; switch (this._type) { case 'raw': return this._value; case 'fixedArray': return this._value. map(function (mediate, idx) {return mediate.init(Mediate.offsetFor(_this._value, idx)).toString(16);}). join(''); case 'prefixed': case 'array': return (0, _pad.padU32)(suffixOffset);} } }, { key: 'closing', value: function closing( offset) {var _this2 = this; switch (this._type) { case 'raw': return ''; case 'prefixed': return this._value; case 'fixedArray': return this._value. map(function (mediate, idx) {return mediate.closing(Mediate.offsetFor(_this2._value, idx)).toString(16);}). join(''); case 'array': var prefix = (0, _pad.padU32)(this._value.length); var inits = this._value. map(function (mediate, idx) {return mediate.init(offset + Mediate.offsetFor(_this2._value, idx) + 32).toString(16);}). join(''); var closings = this._value. map(function (mediate, idx) {return mediate.closing(offset + Mediate.offsetFor(_this2._value, idx)).toString(16);}). join(''); return '' + prefix + inits + closings;} } }, { key: 'type', get: function get() { return this._type; } }, { key: 'value', get: function get() { return this._value; } }], [{ key: 'offsetFor', value: function offsetFor( mediates, position) { if (position < 0 || position >= mediates.length) { throw new Error('Invalid position ' + position + ' specified for Mediate.offsetFor'); } var initLength = mediates. reduce(function (total, mediate) { return total + mediate.initLength(); }, 0); return mediates. slice(0, position). reduce(function (total, mediate) { return total + mediate.closingLength(); }, initLength); } }, { key: 'validateType', value: function validateType( type) { if (TYPES.filter(function (_type) {return type === _type;}).length) { return true; } throw new Error('Invalid type ' + type + ' received for Mediate.validateType'); } }]);return Mediate;}();exports.default = Mediate;module.exports = exports['default'];