diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-05-25 17:48:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-25 17:48:21 +0800 |
commit | 334d2f175fd9dcb3f9e336250d21c7916a2671ae (patch) | |
tree | 52cfb1e8923864978a50e9fe304f952218c8a43f /src/ts/0x.js.ts | |
parent | d23dbf53fd3689613f28d576d03ccfdce94b6646 (diff) | |
parent | 8fe580ea526a048148df2b3545f903eae660ed58 (diff) | |
download | dexon-sol-tools-334d2f175fd9dcb3f9e336250d21c7916a2671ae.tar dexon-sol-tools-334d2f175fd9dcb3f9e336250d21c7916a2671ae.tar.gz dexon-sol-tools-334d2f175fd9dcb3f9e336250d21c7916a2671ae.tar.bz2 dexon-sol-tools-334d2f175fd9dcb3f9e336250d21c7916a2671ae.tar.lz dexon-sol-tools-334d2f175fd9dcb3f9e336250d21c7916a2671ae.tar.xz dexon-sol-tools-334d2f175fd9dcb3f9e336250d21c7916a2671ae.tar.zst dexon-sol-tools-334d2f175fd9dcb3f9e336250d21c7916a2671ae.zip |
Merge pull request #9 from 0xProject/generateSalt
Add generateSalt and tests for it
Diffstat (limited to 'src/ts/0x.js.ts')
-rw-r--r-- | src/ts/0x.js.ts | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index acbdd01e2..4b9680a18 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -11,6 +11,8 @@ export interface ECSignature { s: string; } +const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; + export class ZeroEx { /** * Verifies that the elliptic curve signature `signature` was generated @@ -34,4 +36,17 @@ export class ZeroEx { return false; } } + /** + * Generates pseudo-random 256 bit salt. + * The salt is used to ensure that the 0x order generated has a unique orderHash that does + * not collide with any other outstanding orders. + */ + public static generatePseudoRandomSalt(): BigNumber.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; + } } |