@0x/contracts-test-utils
: Add toHex()
, hexInvert()
, hexLeftPad()
, and hexRightPad()
hex utils.
This commit is contained in:
parent
7b5e3dab17
commit
0999805b3a
@ -65,6 +65,10 @@
|
|||||||
{
|
{
|
||||||
"note": "`web3Wrapper` is created with `shouldAllowUnlimitedContractSize` if `UNLIMITED_CONTRACT_SIZE` environment variable is set.",
|
"note": "`web3Wrapper` is created with `shouldAllowUnlimitedContractSize` if `UNLIMITED_CONTRACT_SIZE` environment variable is set.",
|
||||||
"pr": 2075
|
"pr": 2075
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"note": "Add `toHex()`, `hexLeftPad()`, `hexRightPad()`, and 'hexInvert()' hex utils",
|
||||||
|
"pr": "TODO"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -1,15 +1,63 @@
|
|||||||
|
import { BigNumber } from '@0x/utils';
|
||||||
import * as crypto from 'crypto';
|
import * as crypto from 'crypto';
|
||||||
import * as ethUtil from 'ethereumjs-util';
|
import * as ethUtil from 'ethereumjs-util';
|
||||||
|
|
||||||
|
import { constants } from './constants';
|
||||||
|
|
||||||
|
const { WORD_LENGTH } = constants;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Concatenate all arguments as a hex string.
|
* Concatenate all arguments as a hex string.
|
||||||
*/
|
*/
|
||||||
export function hexConcat(...args: Array<string | number | Buffer>): string {
|
export function hexConcat(...args: Array<string | number | Buffer>): string {
|
||||||
return ethUtil.bufferToHex(Buffer.concat(args.map(h => ethUtil.toBuffer(h))));
|
return ethUtil.bufferToHex(Buffer.concat(args.map(h => ethUtil.toBuffer(h))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random hex string.
|
* 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));
|
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;
|
||||||
|
}
|
||||||
|
@ -28,7 +28,7 @@ export { bytes32Values, testCombinatoriallyWithReferenceFunc, uint256Values } fr
|
|||||||
export { TransactionFactory } from './transaction_factory';
|
export { TransactionFactory } from './transaction_factory';
|
||||||
export { MutatorContractFunction, TransactionHelper } from './transaction_helper';
|
export { MutatorContractFunction, TransactionHelper } from './transaction_helper';
|
||||||
export { testWithReferenceFuncAsync } from './test_with_reference';
|
export { testWithReferenceFuncAsync } from './test_with_reference';
|
||||||
export { hexConcat, hexRandom } from './hex_utils';
|
export { hexConcat, hexLeftPad, hexInvert, hexRandom, hexRightPad, toHex } from './hex_utils';
|
||||||
export {
|
export {
|
||||||
BatchMatchOrder,
|
BatchMatchOrder,
|
||||||
ContractName,
|
ContractName,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user