diff --git a/contracts/test-utils/CHANGELOG.json b/contracts/test-utils/CHANGELOG.json index 315c518d8d..cbbc7d4ca7 100644 --- a/contracts/test-utils/CHANGELOG.json +++ b/contracts/test-utils/CHANGELOG.json @@ -65,6 +65,10 @@ { "note": "`web3Wrapper` is created with `shouldAllowUnlimitedContractSize` if `UNLIMITED_CONTRACT_SIZE` environment variable is set.", "pr": 2075 + }, + { + "note": "Add `toHex()`, `hexLeftPad()`, `hexRightPad()`, and 'hexInvert()' hex utils", + "pr": "TODO" } ] }, diff --git a/contracts/test-utils/src/hex_utils.ts b/contracts/test-utils/src/hex_utils.ts index d1a70442c2..873584484a 100644 --- a/contracts/test-utils/src/hex_utils.ts +++ b/contracts/test-utils/src/hex_utils.ts @@ -1,15 +1,63 @@ +import { BigNumber } from '@0x/utils'; import * as crypto from 'crypto'; import * as ethUtil from 'ethereumjs-util'; +import { constants } from './constants'; + +const { WORD_LENGTH } = constants; + /** * Concatenate all arguments as a hex string. */ export function hexConcat(...args: Array): string { return ethUtil.bufferToHex(Buffer.concat(args.map(h => ethUtil.toBuffer(h)))); } + /** * Generate a random hex string. */ -export function hexRandom(size: number = 32): string { +export function hexRandom(size: number = WORD_LENGTH): string { return ethUtil.bufferToHex(crypto.randomBytes(size)); } + +/** + * Left-pad a hex number to a number of bytes. + */ +export function hexLeftPad(n: string | BigNumber | number, size: number = WORD_LENGTH): string { + return ethUtil.bufferToHex(ethUtil.setLengthLeft(toHex(n), size)); +} + +/** + * Right-pad a hex number to a number of bytes. + */ +export function hexRightPad(n: string | BigNumber | number, size: number = WORD_LENGTH): string { + return ethUtil.bufferToHex(ethUtil.setLengthRight(toHex(n), size)); +} + +/** + * Inverts a hex word. + */ +export function hexInvert(n: string | BigNumber | number, size: number = WORD_LENGTH): string { + const buf = ethUtil.setLengthLeft(toHex(n), size); + // tslint:disable-next-line: no-bitwise + return ethUtil.bufferToHex(Buffer.from(buf.map(b => ~b))); +} + +/** + * Convert a string, a number, or a BigNumber into a hex string. + */ +export function toHex(n: string | BigNumber | number): string { + let hex = '0x00'; + if (typeof(n) === 'number') { + hex = `0x${n.toString(16)}`; + } else if (BigNumber.isBigNumber(n)) { + hex = `0x${n.toString(16)}`; + } else { + if (/^0x/.test(n)) { + hex = n; + } else { + hex = `0x${parseInt(n, 10).toString(16)}`; + } + } + return hex; +} diff --git a/contracts/test-utils/src/index.ts b/contracts/test-utils/src/index.ts index 06fa5cec92..e3dea662f0 100644 --- a/contracts/test-utils/src/index.ts +++ b/contracts/test-utils/src/index.ts @@ -28,7 +28,7 @@ export { bytes32Values, testCombinatoriallyWithReferenceFunc, uint256Values } fr export { TransactionFactory } from './transaction_factory'; export { MutatorContractFunction, TransactionHelper } from './transaction_helper'; export { testWithReferenceFuncAsync } from './test_with_reference'; -export { hexConcat, hexRandom } from './hex_utils'; +export { hexConcat, hexLeftPad, hexInvert, hexRandom, hexRightPad, toHex } from './hex_utils'; export { BatchMatchOrder, ContractName,