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

111 lines (48 loc) 4.4 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _stringify = require('babel-runtime/core-js/json/stringify');var _stringify2 = _interopRequireDefault(_stringify);var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);var _createClass2 = require('babel-runtime/helpers/createClass');var _createClass3 = _interopRequireDefault(_createClass2);var _ethAbi = require('../ethAbi');var _ethAbi2 = _interopRequireDefault(_ethAbi); var _ethereumjsUtil = require('ethereumjs-util');var _ethereumjsUtil2 = _interopRequireDefault(_ethereumjsUtil); var _signature = require('../util/signature');var _signature2 = _interopRequireDefault(_signature); var _ecdsaSigFormatter = require('ecdsa-sig-formatter');var _ecdsaSigFormatter2 = _interopRequireDefault(_ecdsaSigFormatter); var _base64url = require('base64url');var _base64url2 = _interopRequireDefault(_base64url); var _types = require('../util/types');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var Token = function () { function Token(values, functionAbi) {(0, _classCallCheck3.default)(this, Token); this._header = { type: 'EWT', alg: 'ES256k' }; this._abi = new _ethAbi2.default([functionAbi]); this._values = values; var body = {}; body[functionAbi.name] = []; functionAbi.inputs.forEach(function (input, index) { var item = {}; item[input.type] = values[index]; body[functionAbi.name].push(item); //improve some mappings for convenience values[index] = (0, _types.checkConversion)(values[index], input.type); }); this._body = body; }(0, _createClass3.default)(Token, [{ key: 'sign', value: function sign( signerKey) { var tokenParts = []; // add in the header var encodedHeader = _base64url2.default.encode((0, _stringify2.default)(this._header)); tokenParts.push(encodedHeader); // add the sig var priv = new Buffer(signerKey.replace('0x', ''), 'hex'); var tokens = this._abi.encodeTokens(this._abi.functions[0].inputParamTypes(), this._values); var encoded = new Buffer(this._abi.functions[0].encodeCall(tokens), 'hex'); var hash = _ethereumjsUtil2.default.sha3(encoded); var sig = _ethereumjsUtil2.default.ecsign(hash, priv); var signature = new _signature2.default({ r: sig.r, s: sig.s }, 'secp256k1'); var derSignature = new Buffer(signature.toDER()); // add the body this._body.v = sig.v - 27; var encodedBody = _base64url2.default.encode((0, _stringify2.default)(this._body)); tokenParts.push(encodedBody); tokenParts.push(_ecdsaSigFormatter2.default.derToJose(derSignature, 'ES256')); // return the token return tokenParts.join('.'); } }, { key: 'values', get: function get() {return this._values;} }, { key: 'abi', get: function get() {return this._abi;} }, { key: 'body', get: function get() {var types = this._abi.functions[0].inputParamTypes();var tokens = this._abi.encodeTokens(types, this._values);var raw = this._abi.functions[0].encodeCall(tokens);return { raw: raw, length: raw.length / 2, body: (0, _stringify2.default)(this._body) };} }, { key: 'compressed', get: function get() {var types = this._abi.functions[0].inputParamTypes();var tokens = this._abi.encodeTokens(types, this._values);var encoded = this._abi.functions[0].encodeCall(tokens);var newLength = 4;for (var i = 0; i < types.length; i++) {if (types[i]._type == 'address') {newLength += 20;types[i]._length = 20;} else if (types[i]._type == 'uint' || types[i]._type == 'int') {newLength += types[i]._length / 8;types[i]._length = types[i]._length / 8;} else if (types[i]._length) {newLength += types[i]._length;} else {newLength += 32;}}var nonceBuffer = new Buffer(newLength);nonceBuffer.fill(0);nonceBuffer.write(encoded.substring(0, 8), 'hex');var pos = 8,writePos = 4;for (i = 0; i < types.length; i++) {pos += (32 - types[i]._length) * 2;var write = encoded.substring(pos, pos + types[i]._length * 2);pos += types[i]._length * 2;nonceBuffer.write(write, writePos, types[i]._length, 'hex');writePos += types[i]._length;}return tokens;} }]);return Token;}();exports.default = Token;module.exports = exports['default'];