diff --git a/docs/hex.md b/docs/hex.md index 4b01635f2..708ac6e06 100644 --- a/docs/hex.md +++ b/docs/hex.md @@ -3,6 +3,7 @@ Internal utilities to create and test for hex values - [hexAddPrefix](#hexaddprefix) Adds the `0x` prefix to string values. +- [hexFixLength](#hexfixlength) Pads or shifts a hex string to a specific bitLength - [hexFromBn](#hexfrombn) Creates a hex value from a BN.js bignumber object. - [hexFromBn](#hexfrombn) Creates a hex value from a Buffer object. - [hexFromNumber](#hexfromnumber) Creates a hex value from a number. @@ -29,6 +30,24 @@ import { hexAddPrefix } from '@polkadot/util'; console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12 ``` +## hexFixLength + +Pads or shifts a hex string to a specific bitLength + +```js +hexFixLength (value: string, bitLength: number = -1): string +``` + + +Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Input values with less bits are zero-padded, values with more bits are trimmed to the specified length. + +```js +import { hexFixLength } from '@polkadot/util'; + +console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012 +console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12 +``` + ## hexFromBn Creates a hex value from a BN.js bignumber object. [(alias of bnToHex)](bn.md#bntohex) diff --git a/src/hex/constants.js b/src/hex/constants.js new file mode 100644 index 000000000..ecfeb2fc2 --- /dev/null +++ b/src/hex/constants.js @@ -0,0 +1,8 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const ZEROS_256 = '0000000000000000000000000000000000000000000000000000000000000000'; + +module.exports = { + ZEROS_256 +}; diff --git a/src/hex/fixLength.js b/src/hex/fixLength.js index 683cfa278..0e8062808 100644 --- a/src/hex/fixLength.js +++ b/src/hex/fixLength.js @@ -5,9 +5,20 @@ const assert = require('../assert'); const isHex = require('../is/hex'); const hexAddPrefix = require('./addPrefix'); const hexStripPrefix = require('./stripPrefix'); +const { ZEROS_256 } = require('./constants'); -const ZEROS = '0000000000000000000000000000000000000000000000000000000000000000'; +/** + @name hexFixLength + @signature hexFixLength (value: string, bitLength: number = -1): string + @summary Pads or shifts a hex string to a specific bitLength + @description + Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Input values with less bits are zero-padded, values with more bits are trimmed to the specified length. + @example + import { hexFixLength } from '@polkadot/util'; + console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012 + console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12 +*/ module.exports = function hexFixLength (value: string, bitLength: number = -1): string { assert(isHex(value), `Expected hex input value, found '${value}' instead`); @@ -15,9 +26,20 @@ module.exports = function hexFixLength (value: string, bitLength: number = -1): return value; } - const byteLength = -1 * (bitLength / 8) * 2; + const strLength = bitLength / 4; + const hexLength = strLength + 2; + + if (value.length === hexLength) { + return value; + } + + if (value.length > hexLength) { + return hexAddPrefix( + hexStripPrefix(value).slice(-1 * strLength) + ); + } return hexAddPrefix( - `${ZEROS}${hexStripPrefix(value)}`.slice(byteLength) + `${ZEROS_256}${hexStripPrefix(value)}`.slice(-1 * strLength) ); };