pull/1/head
pubkey 7 years ago
parent 811c30d6db
commit 839a73d8a5
  1. 46
      dist/es/hex.js
  2. 4
      dist/es/index.js
  3. 10
      dist/es/util.js
  4. 56
      dist/lib/hex.js
  5. 8
      dist/lib/index.js
  6. 14
      dist/lib/util.js

46
dist/es/hex.js vendored

@ -0,0 +1,46 @@
/**
* compress/decompress hex-strings to utf16 or base64
* thx @juvian
* @link https://stackoverflow.com/a/40471908/3443137
*/
import * as util from './util';
export function compress(hex) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
hex = util.removeTrailing0x(hex);
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) return new Buffer(hex, 'hex').toString('base64');
var string = '';
while (hex.length % 4 != 0) {
// we need it to be multiple of 4
hex = '0' + hex;
}
for (var i = 0; i < hex.length; i += 4) {
// get char from ascii code which goes from 0 to 65536
string += String.fromCharCode(parseInt(hex.substring(i, i + 4), 16));
}
return string;
}
export function decompress(compressedString) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) {
var ret = new Buffer(compressedString, 'base64').toString('hex');
return util.addTrailing0x(ret);
}
var hex = '';
for (var i = 0; i < compressedString.length; i++) {
// get character ascii code and convert to hexa string, adding necessary 0s
hex += ((i == 0 ? '' : '000') + compressedString.charCodeAt(i).toString(16)).slice(-4);
}
hex = hex.toLowerCase();
return util.addTrailing0x(hex);
}

4
dist/es/index.js vendored

@ -8,10 +8,11 @@ import sign from './sign';
import signTransaction from './sign-transaction';
import txDataByCompiled from './tx-data-by-compiled';
import * as hash from './hash';
import * as hex from './hex';
import * as vrs from './vrs';
import * as util from './util';
export { addressByPublicKey, createIdentity, decryptWithPrivateKey, encryptWithPublicKey, publicKeyByPrivateKey, recover, sign, signTransaction, txDataByCompiled, hash, vrs, util };
export { addressByPublicKey, createIdentity, decryptWithPrivateKey, encryptWithPublicKey, publicKeyByPrivateKey, recover, sign, signTransaction, txDataByCompiled, hash, hex, vrs, util };
export default {
addressByPublicKey: addressByPublicKey,
@ -24,6 +25,7 @@ export default {
signTransaction: signTransaction,
txDataByCompiled: txDataByCompiled,
hash: hash,
hex: hex,
vrs: vrs,
util: util
};

10
dist/es/util.js vendored

@ -1,3 +1,11 @@
import Web3 from 'web3';
export var web3 = new Web3();
export var web3 = new Web3();
export function removeTrailing0x(str) {
if (str.startsWith('0x')) return str.substring(2);else return str;
}
export function addTrailing0x(str) {
if (!str.startsWith('0x')) return '0x' + str;else return str;
}

56
dist/lib/hex.js vendored

@ -0,0 +1,56 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.compress = compress;
exports.decompress = decompress;
var _util = require('./util');
var util = _interopRequireWildcard(_util);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
function compress(hex) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
hex = util.removeTrailing0x(hex);
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) return new Buffer(hex, 'hex').toString('base64');
var string = '';
while (hex.length % 4 != 0) {
// we need it to be multiple of 4
hex = '0' + hex;
}
for (var i = 0; i < hex.length; i += 4) {
// get char from ascii code which goes from 0 to 65536
string += String.fromCharCode(parseInt(hex.substring(i, i + 4), 16));
}
return string;
} /**
* compress/decompress hex-strings to utf16 or base64
* thx @juvian
* @link https://stackoverflow.com/a/40471908/3443137
*/
function decompress(compressedString) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) {
var ret = new Buffer(compressedString, 'base64').toString('hex');
return util.addTrailing0x(ret);
}
var hex = '';
for (var i = 0; i < compressedString.length; i++) {
// get character ascii code and convert to hexa string, adding necessary 0s
hex += ((i == 0 ? '' : '000') + compressedString.charCodeAt(i).toString(16)).slice(-4);
}
hex = hex.toLowerCase();
return util.addTrailing0x(hex);
}

8
dist/lib/index.js vendored

@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.util = exports.vrs = exports.hash = exports.txDataByCompiled = exports.signTransaction = exports.sign = exports.recover = exports.publicKeyByPrivateKey = exports.encryptWithPublicKey = exports.decryptWithPrivateKey = exports.createIdentity = exports.addressByPublicKey = undefined;
exports.util = exports.vrs = exports.hex = exports.hash = exports.txDataByCompiled = exports.signTransaction = exports.sign = exports.recover = exports.publicKeyByPrivateKey = exports.encryptWithPublicKey = exports.decryptWithPrivateKey = exports.createIdentity = exports.addressByPublicKey = undefined;
var _addressByPublicKey = require('./address-by-public-key');
@ -45,6 +45,10 @@ var _hash = require('./hash');
var hash = _interopRequireWildcard(_hash);
var _hex = require('./hex');
var hex = _interopRequireWildcard(_hex);
var _vrs = require('./vrs');
var vrs = _interopRequireWildcard(_vrs);
@ -67,6 +71,7 @@ exports.sign = _sign2['default'];
exports.signTransaction = _signTransaction2['default'];
exports.txDataByCompiled = _txDataByCompiled2['default'];
exports.hash = hash;
exports.hex = hex;
exports.vrs = vrs;
exports.util = util;
exports['default'] = {
@ -80,6 +85,7 @@ exports['default'] = {
signTransaction: _signTransaction2['default'],
txDataByCompiled: _txDataByCompiled2['default'],
hash: hash,
hex: hex,
vrs: vrs,
util: util
};

14
dist/lib/util.js vendored

@ -1,9 +1,11 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.web3 = undefined;
exports.removeTrailing0x = removeTrailing0x;
exports.addTrailing0x = addTrailing0x;
var _web = require('web3');
@ -11,4 +13,12 @@ var _web2 = _interopRequireDefault(_web);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var web3 = exports.web3 = new _web2['default']();
var web3 = exports.web3 = new _web2['default']();
function removeTrailing0x(str) {
if (str.startsWith('0x')) return str.substring(2);else return str;
}
function addTrailing0x(str) {
if (!str.startsWith('0x')) return '0x' + str;else return str;
}
Loading…
Cancel
Save