@endpass/utils
Advanced tools
Comparing version 1.0.11 to 1.1.0
@@ -1,12 +0,16 @@ | ||
module.exports.uniq = arr => | ||
arr.reduce((acc, item) => (acc.includes(item) ? acc : acc.concat(item)), []); | ||
'use strict'; | ||
module.exports.mapArrayByProp = (arr, prop) => | ||
arr.reduce((acc, item) => { | ||
const target = item[prop]; | ||
module.exports.uniq = function (arr) { | ||
return arr.reduce(function (acc, item) { | ||
return acc.includes(item) ? acc : acc.concat(item); | ||
}, []); | ||
}; | ||
module.exports.mapArrayByProp = function (arr, prop) { | ||
return arr.reduce(function (acc, item) { | ||
var target = item[prop]; | ||
if (target) { | ||
return Object.assign(acc, { | ||
[target]: item, | ||
}); | ||
acc[target] = item; | ||
return acc; | ||
} | ||
@@ -16,1 +20,2 @@ | ||
}, {}); | ||
}; |
@@ -1,18 +0,17 @@ | ||
const get = require('lodash/get'); | ||
const identity = require('lodash/identity'); | ||
'use strict'; | ||
module.exports = ( | ||
object, | ||
path, | ||
predicate = identity, | ||
timer = 250, | ||
) => | ||
new Promise(resolve => { | ||
var get = require('lodash/get'); | ||
var identity = require('lodash/identity'); | ||
module.exports = function (object, path) { | ||
var predicate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : identity; | ||
var timer = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 250; | ||
return new Promise(function (resolve) { | ||
/* eslint-disable-next-line */ | ||
const interval = setInterval(() => { | ||
const value = get(object, path); | ||
var interval = setInterval(function () { | ||
var value = get(object, path); | ||
if (predicate(value)) { | ||
clearInterval(interval); | ||
return resolve(value); | ||
@@ -22,1 +21,2 @@ } | ||
}); | ||
}; |
30
date.js
@@ -1,11 +0,29 @@ | ||
const dayjs = require('dayjs'); | ||
const relativeTime = require('dayjs/plugin/relativeTime'); | ||
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var dayjs = _interopDefault(require('dayjs')); | ||
var relativeTime = _interopDefault(require('dayjs/plugin/relativeTime')); | ||
/** | ||
* Setting up dayjs globally | ||
*/ | ||
dayjs.extend(relativeTime); | ||
module.exports.formateDate = date => dayjs(date).format('YYYY-MM-DD H:mm'); | ||
module.exports.fromNow = date => dayjs(date).fromNow(); | ||
module.exports = { | ||
formateDate: function formateDate(date) { | ||
var template = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD H:mm'; | ||
return dayjs(date).format(template); | ||
}, | ||
fromNow: function fromNow(date) { | ||
return dayjs(date).fromNow(); | ||
}, | ||
fromTo: function fromTo(fromDate, toDate) { | ||
var start = dayjs(fromDate); | ||
return start.to(dayjs(toDate)); | ||
}, | ||
addToDate: function addToDate(date, value) { | ||
var unit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 's'; | ||
return dayjs(date).add(value, unit).toDate(); | ||
} | ||
}; |
@@ -1,3 +0,5 @@ | ||
module.exports = (item, value) => { | ||
'use strict'; | ||
module.exports = function (item, value) { | ||
return item instanceof Object ? item[value] : item; | ||
} | ||
}; |
@@ -1,1 +0,5 @@ | ||
module.exports.stripHexPrefix = hex => hex.replace(/^0x/, ''); | ||
'use strict'; | ||
module.exports.stripHexPrefix = function (hex) { | ||
return hex.replace(/^0x/, ''); | ||
}; |
19
index.js
'use strict'; | ||
module.exports.arrays = require(__dirname + '/arrays'); | ||
module.exports.asyncCheckProperty = require(__dirname + '/asyncCheckProperty'); | ||
module.exports.date = require(__dirname + '/date'); | ||
module.exports.getOptionParameter = require(__dirname + '/getOptionParameter'); | ||
module.exports.hex = require(__dirname + '/hex'); | ||
module.exports.keystore = require(__dirname + '/keystore'); | ||
module.exports.numbers = require(__dirname + '/numbers'); | ||
module.exports.objects = require(__dirname + '/objects'); | ||
module.exports.strings = require(__dirname + '/strings'); | ||
module.exports.testUtils = require(__dirname + '/testUtils'); | ||
module.exports.arrays = require('./arrays'); | ||
module.exports.asyncCheckProperty = require('./asyncCheckProperty'); | ||
module.exports.date = require('./date'); | ||
module.exports.getOptionParameter = require('./getOptionParameter'); | ||
module.exports.hex = require('./hex'); | ||
module.exports.keystore = require('./keystore'); | ||
module.exports.numbers = require('./numbers'); | ||
module.exports.objects = require('./objects'); | ||
module.exports.strings = require('./strings'); |
@@ -1,7 +0,10 @@ | ||
const keythereum = require('keythereum'); | ||
const bs58check = require('bs58check'); | ||
const EthWallet = require('ethereumjs-wallet'); | ||
const HDKey = require('ethereumjs-wallet/hdkey'); | ||
'use strict'; | ||
// Monkey patch keythereum to skip generating address for private keys | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var keythereum = _interopDefault(require('keythereum')); | ||
var bs58check = _interopDefault(require('bs58check')); | ||
var EthWallet = _interopDefault(require('ethereumjs-wallet')); | ||
var HDKey = _interopDefault(require('ethereumjs-wallet/hdkey')); | ||
// This allows us to encrypt private keys of arbitrary length, and | ||
@@ -11,3 +14,4 @@ // conforms better to the Ethereum keystore V3 spec, which does not include | ||
// See https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition#alterations-from-version-1 | ||
keythereum.privateKeyToAddress = function(pk) { | ||
keythereum.privateKeyToAddress = function (pk) { | ||
return ''; | ||
@@ -19,24 +23,15 @@ }; | ||
// The exported keystore does NOT include an address | ||
encrypt(password, privateKey) { | ||
encrypt: function encrypt(password, privateKey) { | ||
// Generate random salt and iv for each encryption | ||
const dk = keythereum.create(); | ||
const options = { | ||
var dk = keythereum.create(); | ||
var options = { | ||
kdf: ENV.kdfParams.kdf, | ||
kdfparams: ENV.kdfParams, | ||
kdfparams: ENV.kdfParams | ||
}; | ||
const encrypted = keythereum.dump( | ||
password, | ||
privateKey, | ||
dk.salt, | ||
dk.iv, | ||
options, | ||
); | ||
var encrypted = keythereum.dump(password, privateKey, dk.salt, dk.iv, options); | ||
delete encrypted.address; | ||
return encrypted; | ||
}, | ||
// Decrypts a V3 keystore object into a private key Buffer | ||
decrypt(password, json) { | ||
decrypt: function decrypt(password, json) { | ||
if (!password) { | ||
@@ -52,49 +47,38 @@ throw new Error('Password is empty'); | ||
}, | ||
// Encrypts an ethereumjs Wallet | ||
encryptWallet(password, wallet) { | ||
const json = this.encrypt(password, wallet.getPrivateKey()); | ||
encryptWallet: function encryptWallet(password, wallet) { | ||
var json = this.encrypt(password, wallet.getPrivateKey()); | ||
json.address = wallet.getChecksumAddressString(); | ||
return json; | ||
}, | ||
// Decrypts a keystore into an ethereumjs Wallet | ||
decryptWallet(password, json) { | ||
const privateKey = this.decrypt(password, json); | ||
decryptWallet: function decryptWallet(password, json) { | ||
var privateKey = this.decrypt(password, json); | ||
return EthWallet.fromPrivateKey(privateKey); | ||
}, | ||
// Encrypts an ethereumjs Wallet | ||
encryptHDWallet(password, wallet) { | ||
const xPrv = this.decodeBase58(wallet.privateExtendedKey()); | ||
const json = this.encrypt(password, xPrv); | ||
encryptHDWallet: function encryptHDWallet(password, wallet) { | ||
var xPrv = this.decodeBase58(wallet.privateExtendedKey()); | ||
var json = this.encrypt(password, xPrv); | ||
json.address = wallet.publicExtendedKey(); | ||
return json; | ||
}, | ||
// Decrypts a keystore into an ethereumjs Wallet | ||
decryptHDWallet(password, json) { | ||
const xPrv = this.decrypt(password, json); | ||
const xPrvString = this.encodeBase58(xPrv); | ||
decryptHDWallet: function decryptHDWallet(password, json) { | ||
var xPrv = this.decrypt(password, json); | ||
var xPrvString = this.encodeBase58(xPrv); | ||
return HDKey.fromExtendedKey(xPrvString); | ||
}, | ||
// Encode a buffer to Base58Check | ||
// If already a string, silently return it | ||
encodeBase58(key) { | ||
encodeBase58: function encodeBase58(key) { | ||
if (typeof key === 'string') { | ||
return key; | ||
} | ||
return bs58check.encode(key); | ||
}, | ||
// Decode from Base58Check string | ||
// If not a string, silently return it | ||
decodeBase58(key) { | ||
decodeBase58: function decodeBase58(key) { | ||
if (typeof key !== 'string') { | ||
@@ -106,23 +90,18 @@ return key; | ||
}, | ||
// Returns true if the key is an extended public key (xpub) | ||
// Accepts string or buffer | ||
isExtendedPublicKey(key) { | ||
const keyString = this.encodeBase58(key); | ||
isExtendedPublicKey: function isExtendedPublicKey(key) { | ||
var keyString = this.encodeBase58(key); | ||
return keyString.slice(0, 4) === 'xpub'; | ||
}, | ||
// Returns true if the key is an extended private key (xprv) | ||
// Accepts string or buffer | ||
isExtendedPrivateKey(key) { | ||
const keyString = this.encodeBase58(key); | ||
isExtendedPrivateKey: function isExtendedPrivateKey(key) { | ||
var keyString = this.encodeBase58(key); | ||
return keyString.slice(0, 4) === 'xprv'; | ||
}, | ||
// Simple sanity check to ensure a valid V3 keystore | ||
isV3(json) { | ||
isV3: function isV3(json) { | ||
return json && json.crypto && json.crypto.ciphertext; | ||
}, | ||
} | ||
}; |
@@ -1,1 +0,5 @@ | ||
module.exports.isNumeric = n => !isNaN(parseFloat(n)) && isFinite(n); | ||
'use strict'; | ||
module.exports.isNumeric = function (n) { | ||
return !isNaN(parseFloat(n)) && isFinite(n); | ||
}; |
@@ -1,14 +0,32 @@ | ||
const get = require('lodash/get'); | ||
'use strict'; | ||
module.exports.merge = (...obj) => | ||
obj.reduce((acc, item) => Object.assign(acc, item), {}); | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
module.exports.getFrom = (target, ...paths) => { | ||
const existPath = paths.find(path => get(target, path)); | ||
var get = _interopDefault(require('lodash/get')); | ||
if (!existPath) { | ||
return null; | ||
module.exports = { | ||
merge: function merge() { | ||
for (var _len = arguments.length, obj = new Array(_len), _key = 0; _key < _len; _key++) { | ||
obj[_key] = arguments[_key]; | ||
} | ||
return obj.reduce(function (acc, item) { | ||
return Object.assign(acc, item); | ||
}, {}); | ||
}, | ||
getFrom: function getFrom(target) { | ||
for (var _len2 = arguments.length, paths = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
paths[_key2 - 1] = arguments[_key2]; | ||
} | ||
var existPath = paths.find(function (path) { | ||
return get(target, path); | ||
}); | ||
if (!existPath) { | ||
return null; | ||
} | ||
return get(target, existPath); | ||
} | ||
return get(target, existPath); | ||
}; |
{ | ||
"name": "@endpass/utils", | ||
"version": "1.0.11", | ||
"description": "Utilis and helper functions", | ||
"version": "1.1.0", | ||
"description": "Utils and helper functions", | ||
"author": "Endpass, Inc", | ||
@@ -14,4 +14,7 @@ "homepage": "http://endpass.com/", | ||
"scripts": { | ||
"build": "yarn clear && yarn build:dist", | ||
"build:dist": "rollup -c ./pack/rollup.config.js", | ||
"clear": "rimraf ./dist", | ||
"test": "npm run unit", | ||
"unit": "jest --config jest.conf.js" | ||
"unit": "jest --config ./pack/jest.conf.js" | ||
}, | ||
@@ -30,10 +33,18 @@ "bugs": { | ||
"devDependencies": { | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-stage-2": "^6.24.1", | ||
"jest": "^23.6.0" | ||
"@babel/core": "^7.2.2", | ||
"@babel/plugin-transform-runtime": "^7.2.0", | ||
"@babel/preset-env": "^7.3.1", | ||
"@babel/runtime": "^7.3.1", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^24.0.0", | ||
"find": "^0.2.9", | ||
"jest": "^24.0.0", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.1.2", | ||
"rollup-plugin-alias": "^1.5.1", | ||
"rollup-plugin-babel": "^4.3.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "891e523397e0e56dab6127917f0b11dde807ae49" | ||
} | ||
} |
@@ -1,4 +0,11 @@ | ||
module.exports.getShortStringWithEllipsis = (string, symbolsCount = 4) => | ||
`${string.slice(0, symbolsCount)}...${string.slice(-symbolsCount)}`; | ||
'use strict'; | ||
module.exports.matchString = (a, b) => new RegExp(b, 'i').test(a); | ||
module.exports = { | ||
getShortStringWithEllipsis: function getShortStringWithEllipsis(string) { | ||
var symbolsCount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 4; | ||
return "".concat(string.slice(0, symbolsCount), "...").concat(string.slice(-symbolsCount)); | ||
}, | ||
matchString: function matchString(a, b) { | ||
return new RegExp(b, 'i').test(a); | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
0
14685
12
22
387
2