From 55c491f8ea8bb0ba8cb1b856b3c42dd05c31565d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 11:19:14 +0200 Subject: Add generateSalt and tests for it --- package.json | 1 + src/ts/0x.js.ts | 11 +++++++++++ test/0x.js.ts | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/package.json b/package.json index 275a57a93..ba66272be 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "@types/mocha": "^2.2.41", "@types/node": "^7.0.22", "awesome-typescript-loader": "^3.1.3", + "bignumber.js": "^4.0.2", "chai": "^3.5.0", "mocha": "^3.4.1", "npm-run-all": "^4.0.2", diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index acbdd01e2..14f9f2061 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,13 @@ export class ZeroEx { return false; } } + /** Generates pseudo-random 256 bits salt */ + 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; + } } diff --git a/test/0x.js.ts b/test/0x.js.ts index 71126c7da..e4e3cc0a7 100644 --- a/test/0x.js.ts +++ b/test/0x.js.ts @@ -1,6 +1,7 @@ import {ZeroEx} from '../src/ts/0x.js'; import {expect} from 'chai'; import 'mocha'; +import * as BigNumber from 'bignumber.js'; describe('ZeroEx library', () => { describe('#isValidSignature', () => { @@ -73,4 +74,16 @@ describe('ZeroEx library', () => { expect(isValid).to.be.true; }); }); + describe('#generateSalt', () => { + it('generates different salts', () => { + const equal = ZeroEx.generatePseudoRandomSalt().eq(ZeroEx.generatePseudoRandomSalt()); + expect(equal).to.be.false; + }); + it('generates salt in range [0..2^256)', () => { + const salt = ZeroEx.generatePseudoRandomSalt(); + expect(salt.greaterThanOrEqualTo(0)).to.be.true; + const twoPow256 = new BigNumber(2).pow(256); + expect(salt.lessThan(twoPow256)).to.be.true; + }); + }); }); -- cgit v1.2.3 From 7c40fee2ad0f390f350c0226108218190fb9c98a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 11:24:44 +0200 Subject: Add lodash --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index ba66272be..f88442f37 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "dependencies": { "bignumber.js": "^4.0.2", "ethereumjs-util": "^5.1.1", + "lodash": "^4.17.4", "web3": "^0.19.0" } } -- cgit v1.2.3 From 8fe580ea526a048148df2b3545f903eae660ed58 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 11:38:01 +0200 Subject: More descriptive comment --- src/ts/0x.js.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index 14f9f2061..4b9680a18 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -36,7 +36,11 @@ export class ZeroEx { return false; } } - /** Generates pseudo-random 256 bits salt */ + /** + * 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 -- cgit v1.2.3