From a14424ae5f45784db5e75c2d6a9098dc96914c9e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 11 Dec 2017 12:43:19 +0100 Subject: Move isAddress to shared utils and remove all dependencies on ethereum-address --- packages/utils/src/address_utils.ts | 34 ++++++++++++++++++++++++++++++++++ packages/utils/src/index.ts | 1 + 2 files changed, 35 insertions(+) create mode 100644 packages/utils/src/address_utils.ts (limited to 'packages/utils/src') diff --git a/packages/utils/src/address_utils.ts b/packages/utils/src/address_utils.ts new file mode 100644 index 000000000..bdbcd3a89 --- /dev/null +++ b/packages/utils/src/address_utils.ts @@ -0,0 +1,34 @@ +import * as jsSHA3 from 'js-sha3'; + +const BASIC_ADDRESS_REGEX = /^(0x)?[0-9a-f]{40}$/i; +const SAME_CASE_ADDRESS_REGEX = /^(0x)?([0-9a-f]{40}|[0-9A-F]{40})$/; + +export const addressUtils = { + isChecksumAddress(address: string): boolean { + // Check each case + const unprefixedAddress = address.replace('0x', ''); + const addressHash = jsSHA3.keccak256(unprefixedAddress.toLowerCase()); + + for (let i = 0; i < 40; i++) { + // The nth letter should be uppercase if the nth digit of casemap is 1 + if ((parseInt(addressHash[i], 16) > 7 && unprefixedAddress[i].toUpperCase() !== unprefixedAddress[i]) || + (parseInt(addressHash[i], 16) <= 7 && unprefixedAddress[i].toLowerCase() !== unprefixedAddress[i])) { + return false; + } + } + return true; + }, + isAddress(address: string): boolean { + if (!BASIC_ADDRESS_REGEX.test(address)) { + // Check if it has the basic requirements of an address + return false; + } else if (SAME_CASE_ADDRESS_REGEX.test(address)) { + // If it's all small caps or all all caps, return true + return true; + } else { + // Otherwise check each case + const isValidChecksummedAddress = addressUtils.isChecksumAddress(address); + return isValidChecksummedAddress; + } + }, +}; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index a61f04ad2..edfd192b2 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1 +1,2 @@ export {promisify} from './promisify'; +export {addressUtils} from './address_utils'; -- cgit v1.2.3 From cb596c1413938ce23901135f8235bb813cc6e784 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 8 Dec 2017 18:01:40 +0300 Subject: Move more shared utils into utils package and reuse them --- packages/utils/src/bignumber_config.ts | 11 +++++++++++ packages/utils/src/class_utils.ts | 18 ++++++++++++++++++ packages/utils/src/index.ts | 3 +++ packages/utils/src/interval_utils.ts | 20 ++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 packages/utils/src/bignumber_config.ts create mode 100644 packages/utils/src/class_utils.ts create mode 100644 packages/utils/src/interval_utils.ts (limited to 'packages/utils/src') diff --git a/packages/utils/src/bignumber_config.ts b/packages/utils/src/bignumber_config.ts new file mode 100644 index 000000000..2d5214e6f --- /dev/null +++ b/packages/utils/src/bignumber_config.ts @@ -0,0 +1,11 @@ +import BigNumber from 'bignumber.js'; + +export const bigNumberConfigs = { + configure() { + // By default BigNumber's `toString` method converts to exponential notation if the value has + // more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number + BigNumber.config({ + EXPONENTIAL_AT: 1000, + }); + }, +}; diff --git a/packages/utils/src/class_utils.ts b/packages/utils/src/class_utils.ts new file mode 100644 index 000000000..04e60ee57 --- /dev/null +++ b/packages/utils/src/class_utils.ts @@ -0,0 +1,18 @@ +import * as _ from 'lodash'; + +export const classUtils = { + // This is useful for classes that have nested methods. Nested methods don't get bound out of the box. + bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void { + for (const key of Object.getOwnPropertyNames(self)) { + const val = self[key]; + if (!_.includes(exclude, key)) { + if (_.isFunction(val)) { + self[key] = val.bind(thisArg || self); + } else if (_.isObject(val)) { + classUtils.bindAll(val, exclude, self); + } + } + } + return self; + }, +}; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index edfd192b2..403bc7236 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,2 +1,5 @@ export {promisify} from './promisify'; export {addressUtils} from './address_utils'; +export {classUtils} from './class_utils'; +export {intervalUtils} from './interval_utils'; +export {bigNumberConfigs} from './bignumber_config'; diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts new file mode 100644 index 000000000..62b79f2f5 --- /dev/null +++ b/packages/utils/src/interval_utils.ts @@ -0,0 +1,20 @@ +import * as _ from 'lodash'; + +export const intervalUtils = { + setAsyncExcludingInterval(fn: () => Promise, intervalMs: number) { + let locked = false; + const intervalId = setInterval(async () => { + if (locked) { + return; + } else { + locked = true; + await fn(); + locked = false; + } + }, intervalMs); + return intervalId; + }, + clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void { + clearInterval(intervalId); + }, +}; -- cgit v1.2.3 From e744e4cd989bd3ae1070c59f7baa8097f18b8b06 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 22 Dec 2017 15:05:32 +0100 Subject: Apply prettier config --- packages/utils/src/address_utils.ts | 6 ++++-- packages/utils/src/index.ts | 10 +++++----- packages/utils/src/promisify.ts | 11 +++-------- 3 files changed, 12 insertions(+), 15 deletions(-) (limited to 'packages/utils/src') diff --git a/packages/utils/src/address_utils.ts b/packages/utils/src/address_utils.ts index bdbcd3a89..f94985441 100644 --- a/packages/utils/src/address_utils.ts +++ b/packages/utils/src/address_utils.ts @@ -11,8 +11,10 @@ export const addressUtils = { for (let i = 0; i < 40; i++) { // The nth letter should be uppercase if the nth digit of casemap is 1 - if ((parseInt(addressHash[i], 16) > 7 && unprefixedAddress[i].toUpperCase() !== unprefixedAddress[i]) || - (parseInt(addressHash[i], 16) <= 7 && unprefixedAddress[i].toLowerCase() !== unprefixedAddress[i])) { + if ( + (parseInt(addressHash[i], 16) > 7 && unprefixedAddress[i].toUpperCase() !== unprefixedAddress[i]) || + (parseInt(addressHash[i], 16) <= 7 && unprefixedAddress[i].toLowerCase() !== unprefixedAddress[i]) + ) { return false; } } diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 403bc7236..ca3219f10 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,5 +1,5 @@ -export {promisify} from './promisify'; -export {addressUtils} from './address_utils'; -export {classUtils} from './class_utils'; -export {intervalUtils} from './interval_utils'; -export {bigNumberConfigs} from './bignumber_config'; +export { promisify } from './promisify'; +export { addressUtils } from './address_utils'; +export { classUtils } from './class_utils'; +export { intervalUtils } from './interval_utils'; +export { bigNumberConfigs } from './bignumber_config'; diff --git a/packages/utils/src/promisify.ts b/packages/utils/src/promisify.ts index c114cf32f..29d626b61 100644 --- a/packages/utils/src/promisify.ts +++ b/packages/utils/src/promisify.ts @@ -5,16 +5,11 @@ import * as _ from 'lodash'; * Promisify provides a default callback of the form (error, result) and rejects when `error` is not null. You can also * supply thisArg object as the second argument which will be passed to `apply`. */ -export function promisify( - originalFn: ( - ...args: any[], - // HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453 - ) => void, - thisArg?: any, -): (...callArgs: any[]) => Promise { +// HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453 +export function promisify(originalFn: (...args: any[]) => void, thisArg?: any): (...callArgs: any[]) => Promise { const promisifiedFunction = async (...callArgs: any[]): Promise => { return new Promise((resolve, reject) => { - const callback = (err: Error|null, data?: T) => { + const callback = (err: Error | null, data?: T) => { _.isNull(err) ? resolve(data) : reject(err); }; originalFn.apply(thisArg, [...callArgs, callback]); -- cgit v1.2.3 From 4dfa720f2dec2be0b0758633c087579a02e482bb Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 4 Jan 2018 12:38:19 +0100 Subject: Use configured version of bignumber in all packages --- packages/utils/src/bignumber_config.ts | 11 ----------- packages/utils/src/configured_bignumber.ts | 9 +++++++++ packages/utils/src/index.ts | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) delete mode 100644 packages/utils/src/bignumber_config.ts create mode 100644 packages/utils/src/configured_bignumber.ts (limited to 'packages/utils/src') diff --git a/packages/utils/src/bignumber_config.ts b/packages/utils/src/bignumber_config.ts deleted file mode 100644 index 2d5214e6f..000000000 --- a/packages/utils/src/bignumber_config.ts +++ /dev/null @@ -1,11 +0,0 @@ -import BigNumber from 'bignumber.js'; - -export const bigNumberConfigs = { - configure() { - // By default BigNumber's `toString` method converts to exponential notation if the value has - // more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number - BigNumber.config({ - EXPONENTIAL_AT: 1000, - }); - }, -}; diff --git a/packages/utils/src/configured_bignumber.ts b/packages/utils/src/configured_bignumber.ts new file mode 100644 index 000000000..e44c062c2 --- /dev/null +++ b/packages/utils/src/configured_bignumber.ts @@ -0,0 +1,9 @@ +import { BigNumber } from 'bignumber.js'; + +// By default BigNumber's `toString` method converts to exponential notation if the value has +// more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number +BigNumber.config({ + EXPONENTIAL_AT: 1000, +}); + +export { BigNumber }; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index ca3219f10..2768e49ab 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -2,4 +2,4 @@ export { promisify } from './promisify'; export { addressUtils } from './address_utils'; export { classUtils } from './class_utils'; export { intervalUtils } from './interval_utils'; -export { bigNumberConfigs } from './bignumber_config'; +export { BigNumber } from './configured_bignumber'; -- cgit v1.2.3 From 065570ebf57eb37b14ffd0b2fe131c3dcec4064a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 10 Jan 2018 13:50:26 +0100 Subject: Add an error handler parameter to intervals --- packages/utils/src/interval_utils.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'packages/utils/src') diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts index 62b79f2f5..ebecc7015 100644 --- a/packages/utils/src/interval_utils.ts +++ b/packages/utils/src/interval_utils.ts @@ -1,14 +1,18 @@ import * as _ from 'lodash'; export const intervalUtils = { - setAsyncExcludingInterval(fn: () => Promise, intervalMs: number) { + setAsyncExcludingInterval(fn: () => Promise, intervalMs: number, onError: (err: Error) => void) { let locked = false; const intervalId = setInterval(async () => { if (locked) { return; } else { locked = true; - await fn(); + try { + await fn(); + } catch (err) { + onError(err); + } locked = false; } }, intervalMs); @@ -17,4 +21,17 @@ export const intervalUtils = { clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void { clearInterval(intervalId); }, + setInterval(fn: () => void, intervalMs: number, onError: (err: Error) => void) { + const intervalId = setInterval(() => { + try { + fn(); + } catch (err) { + onError(err); + } + }, intervalMs); + return intervalId; + }, + clearInterval(intervalId: NodeJS.Timer): void { + clearInterval(intervalId); + }, }; -- cgit v1.2.3