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

51 lines (32 loc) 1.86 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports. isArray = isArray;exports. isFunction = isFunction;exports. isString = isString;exports. isInstanceOf = isInstanceOf;exports. checkConversion = checkConversion;var BN = require('bn.js');function isArray(test) {return Object.prototype.toString.call(test) === '[object Array]';}function isFunction(test) {return Object.prototype.toString.call(test) === '[object Function]';}function isString(test) {return Object.prototype.toString.call(test) === '[object String]';}function isInstanceOf(test, clazz) {return test instanceof clazz;}var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;var hexRegex = /^[0-9A-Fa-f]+$/i;function checkConversion(value, type) { if (type.indexOf('bytes') > -1 && uuidRegex.test(value)) { return value.replace(new RegExp('-'.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"), 'g'), ''); } if (type.indexOf('uint') > -1 && uuidRegex.test(value)) { return new BN(value.replace(new RegExp('-'.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"), 'g'), ''), 16); } //replace date if (type.indexOf('uint') > -1 && typeof value == 'string' && Date.parse(value) != NaN) { return new Date(value).getTime() / 1000 | 0; } //taken care of leading '0x' for byte arrays and addresses if ((type.indexOf('bytes') > -1 || type == 'address') && value.indexOf('0x') == 0) { return value.replace('0x', ''); } //manage strings that have been passed to byte arrays if (type.indexOf('bytes') > -1 && type.indexOf('[]') == -1 && !hexRegex.test(value)) { var arr = []; for (var i = 0, l = value.length; i < l; i++) { console.log(value); var hex = Number(value.charCodeAt(i)).toString(16); arr.push(hex); } return arr.join(''); } return value; }