diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-03 00:42:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-03 00:42:59 +0800 |
commit | a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0 (patch) | |
tree | 7f7e45f54cdd8fc2830621cbb4f89093bef8cbf4 /packages/order-utils/src/salt.ts | |
parent | f08738e13379ea86a33ad9be9f7579e637e69acb (diff) | |
parent | 3585326d7eb82ae730752d1956876b22f7638fa6 (diff) | |
download | dexon-sol-tools-a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0.tar dexon-sol-tools-a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0.tar.gz dexon-sol-tools-a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0.tar.bz2 dexon-sol-tools-a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0.tar.lz dexon-sol-tools-a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0.tar.xz dexon-sol-tools-a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0.tar.zst dexon-sol-tools-a054b485d01d5003d47e7959a4fe1e1c0a6dc6a0.zip |
Merge pull request #559 from 0xProject/feature/order-utils
Move order utils to @0xproject/order-utils
Diffstat (limited to 'packages/order-utils/src/salt.ts')
-rw-r--r-- | packages/order-utils/src/salt.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/packages/order-utils/src/salt.ts b/packages/order-utils/src/salt.ts new file mode 100644 index 000000000..90a4197c0 --- /dev/null +++ b/packages/order-utils/src/salt.ts @@ -0,0 +1,18 @@ +import { BigNumber } from '@0xproject/utils'; + +const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; + +/** + * Generates a pseudo-random 256-bit salt. + * The salt can be included in a 0x order, ensuring that the order generates a unique orderHash + * and will not collide with other outstanding orders that are identical in all other parameters. + * @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).round(); + return salt; +} |