aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--packages/order-utils/src/salt.ts10
-rw-r--r--packages/utils/src/address_utils.ts11
-rw-r--r--packages/utils/src/index.ts1
-rw-r--r--packages/utils/src/random.ts16
4 files changed, 29 insertions, 9 deletions
diff --git a/packages/order-utils/src/salt.ts b/packages/order-utils/src/salt.ts
index 95df66c99..a7cc4aea0 100644
--- a/packages/order-utils/src/salt.ts
+++ b/packages/order-utils/src/salt.ts
@@ -1,6 +1,4 @@
-import { BigNumber } from '@0x/utils';
-
-const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
+import { BigNumber, generatePseudoRandom256BitNumber } from '@0x/utils';
/**
* Generates a pseudo-random 256-bit salt.
@@ -9,10 +7,6 @@ const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
* @return A pseudo-random 256-bit number that can be used as a salt.
*/
export function generatePseudoRandomSalt(): BigNumber {
- // BigNumber.random returns a pseudo-random number between 0 & 1 with a passed in number of decimal places.
- // Source: https://mikemcl.github.io/bignumber.js/#random
- const randomNumber = BigNumber.random(MAX_DIGITS_IN_UNSIGNED_256_INT);
- const factor = new BigNumber(10).pow(MAX_DIGITS_IN_UNSIGNED_256_INT - 1);
- const salt = randomNumber.times(factor).integerValue();
+ const salt = generatePseudoRandom256BitNumber();
return salt;
}
diff --git a/packages/utils/src/address_utils.ts b/packages/utils/src/address_utils.ts
index 1fc960408..361e35cd8 100644
--- a/packages/utils/src/address_utils.ts
+++ b/packages/utils/src/address_utils.ts
@@ -1,7 +1,9 @@
-import { addHexPrefix, stripHexPrefix } from 'ethereumjs-util';
+import { addHexPrefix, sha3, stripHexPrefix } from 'ethereumjs-util';
import * as jsSHA3 from 'js-sha3';
import * as _ from 'lodash';
+import { generatePseudoRandom256BitNumber } from './random';
+
const BASIC_ADDRESS_REGEX = /^(0x)?[0-9a-f]{40}$/i;
const SAME_CASE_ADDRESS_REGEX = /^(0x)?([0-9a-f]{40}|[0-9A-F]{40})$/;
const ADDRESS_LENGTH = 40;
@@ -43,4 +45,11 @@ export const addressUtils = {
padZeros(address: string): string {
return addHexPrefix(_.padStart(stripHexPrefix(address), ADDRESS_LENGTH, '0'));
},
+ generatePseudoRandomAddress(): string {
+ const randomBigNum = generatePseudoRandom256BitNumber();
+ const randomBuff = sha3(randomBigNum.toString());
+ const addressLengthInBytes = 20;
+ const randomAddress = `0x${randomBuff.slice(0, addressLengthInBytes).toString('hex')}`;
+ return randomAddress;
+ },
};
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index 467129d2b..f9c2693fe 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -12,3 +12,4 @@ export { fetchAsync } from './fetch_async';
export { signTypedDataUtils } from './sign_typed_data_utils';
export import AbiEncoder = require('./abi_encoder');
export * from './types';
+export { generatePseudoRandom256BitNumber } from './random';
diff --git a/packages/utils/src/random.ts b/packages/utils/src/random.ts
new file mode 100644
index 000000000..69243bab8
--- /dev/null
+++ b/packages/utils/src/random.ts
@@ -0,0 +1,16 @@
+import { BigNumber } from './configured_bignumber';
+
+const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
+
+/**
+ * Generates a pseudo-random 256-bit number.
+ * @return A pseudo-random 256-bit number.
+ */
+export function generatePseudoRandom256BitNumber(): BigNumber {
+ // BigNumber.random returns a pseudo-random number between 0 & 1 with a passed in number of decimal places.
+ // Source: https://mikemcl.github.io/bignumber.js/#random
+ const randomNumber = BigNumber.random(MAX_DIGITS_IN_UNSIGNED_256_INT);
+ const factor = new BigNumber(10).pow(MAX_DIGITS_IN_UNSIGNED_256_INT - 1);
+ const randomNumberScaledTo256Bits = randomNumber.times(factor).integerValue();
+ return randomNumberScaledTo256Bits;
+}