Added slice and missing Math libraries for limited environments.

This commit is contained in:
Richard Moore 2017-11-08 20:19:41 -05:00
parent b4bc01f2c3
commit 04f0a33489
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
5 changed files with 30 additions and 9 deletions

@ -31,6 +31,15 @@ function getChecksumAddress(address) {
return '0x' + address.join('');
}
// Shims for environments that are missing some required constants and functions
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
function log10(x) {
if (Math.log10) { return Math.log10(x); }
return Math.log(x) / Math.LN10;
}
// See: https://en.wikipedia.org/wiki/International_Bank_Account_Number
var ibanChecksum = (function() {
@ -40,7 +49,7 @@ var ibanChecksum = (function() {
for (var i = 0; i < 26; i++) { ibanLookup[String.fromCharCode(65 + i)] = String(10 + i); }
// How many decimal digits can we process? (for 64-bit float, this is 15)
var safeDigits = Math.floor(Math.log10(Number.MAX_SAFE_INTEGER));
var safeDigits = Math.floor(log10(MAX_SAFE_INTEGER));
return function(address) {
address = address.toUpperCase();

@ -1,6 +1,7 @@
'use strict';
var defineProperty = require('./properties.js').defineProperty;
var convert = require('./convert');
var defineProperty = require('./properties').defineProperty;
var crypto = global.crypto || global.msCrypto;
if (!crypto || !crypto.getRandomValues) {
@ -32,7 +33,7 @@ function randomBytes(length) {
var result = new Uint8Array(length);
crypto.getRandomValues(result);
return result;
return convert.arrayify(result);
};
if (crypto._weakCrypto === true) {

@ -6,6 +6,17 @@
var defineProperty = require('./properties.js').defineProperty;
var throwError = require('./throw-error');
function addSlice(array) {
if (array.slice) { return array; }
array.slice = function() {
var args = Array.prototype.slice.call(arguments);
return new Uint8Array(Array.prototype.slice.apply(array, args));
}
return array;
}
function isArrayish(value) {
if (!value || parseInt(value.length) != value.length || typeof(value) === 'string') {
return false;
@ -36,11 +47,11 @@ function arrayify(value, name) {
result.push(parseInt(value.substr(i, 2), 16));
}
return new Uint8Array(result);
return addSlice(new Uint8Array(result));
}
if (isArrayish(value)) {
return new Uint8Array(value);
return addSlice(new Uint8Array(value));
}
throwError('invalid arrayify value', { name: name, input: value });
@ -62,7 +73,7 @@ function concat(objects) {
offset += arrays[i].length;
}
return result;
return addSlice(result);
}
function stripZeros(value) {
value = arrayify(value);
@ -88,7 +99,7 @@ function padZeros(value, length) {
var result = new Uint8Array(length);
result.set(value, length - value.length);
return result;
return addSlice(result);
}

@ -1,6 +1,6 @@
{
"name": "ethers-utils",
"version": "2.1.4",
"version": "2.1.5",
"description": "Utilities for the Ethers Ethereum library.",
"bugs": {
"url": "http://github.com/ethers-io/ethers.js/issues",

@ -39,7 +39,7 @@ function pbkdf2(password, salt, iterations, keylen, createHmac) {
var destPos = (i - 1) * hLen
var len = (i === l ? r : hLen)
//T.copy(DK, destPos, 0, len)
DK.set(T.slice(0, len), destPos);
DK.set(Array.prototype.slice.call(T, 0, len), destPos);
}