diff --git a/lerna.json b/lerna.json index c7ee0702f..818610ec7 100644 --- a/lerna.json +++ b/lerna.json @@ -10,5 +10,5 @@ "packages": [ "packages/*" ], - "version": "0.21.3" + "version": "0.22.0" } diff --git a/packages/util-crypto/package.json b/packages/util-crypto/package.json index d1b98dfae..b3ce4ac0b 100644 --- a/packages/util-crypto/package.json +++ b/packages/util-crypto/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-crypto", - "version": "0.21.3", + "version": "0.22.0", "description": "A collection of useful crypto utilities for @polkadot", "main": "index.js", "keywords": [ @@ -30,7 +30,7 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.21.3", + "@polkadot/util": "^0.22.0", "blakejs": "^1.1.0", "js-sha3": "^0.7.0", "tweetnacl": "^1.0.0", diff --git a/packages/util-keyring/package.json b/packages/util-keyring/package.json index b2f299c7e..edcee65e2 100644 --- a/packages/util-keyring/package.json +++ b/packages/util-keyring/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-keyring", - "version": "0.21.3", + "version": "0.22.0", "description": "Keyring management", "main": "index.js", "engines": { @@ -32,8 +32,8 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.21.3", - "@polkadot/util-crypto": "^0.21.3", + "@polkadot/util": "^0.22.0", + "@polkadot/util-crypto": "^0.22.0", "bs58": "^4.0.1" } } diff --git a/packages/util-rlp/package.json b/packages/util-rlp/package.json index 65514c238..c534479d4 100644 --- a/packages/util-rlp/package.json +++ b/packages/util-rlp/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-rlp", - "version": "0.21.3", + "version": "0.22.0", "description": "RLP encoding and decoding", "main": "index.js", "keywords": [ @@ -30,7 +30,7 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.21.3" + "@polkadot/util": "^0.22.0" }, "devDependencies": { "ethereumjs-testing": "^1.0.4" diff --git a/packages/util-triehash/package.json b/packages/util-triehash/package.json index 7b190c3a1..8829254f8 100644 --- a/packages/util-triehash/package.json +++ b/packages/util-triehash/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-triehash", - "version": "0.21.3", + "version": "0.22.0", "description": "Creation of triehash values", "main": "index.js", "keywords": [ @@ -30,8 +30,8 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.21.3", - "@polkadot/util-crypto": "^0.21.3", - "@polkadot/util-rlp": "^0.21.3" + "@polkadot/util": "^0.22.0", + "@polkadot/util-crypto": "^0.22.0", + "@polkadot/util-rlp": "^0.22.0" } } diff --git a/packages/util/package.json b/packages/util/package.json index f11ff16b9..c650d1598 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util", - "version": "0.21.3", + "version": "0.22.0", "description": "A collection of useful utilities for @polkadot", "main": "index.js", "keywords": [ diff --git a/packages/util/src/hex/hasPrefix.js b/packages/util/src/hex/hasPrefix.js index 305036417..901d655f7 100644 --- a/packages/util/src/hex/hasPrefix.js +++ b/packages/util/src/hex/hasPrefix.js @@ -18,5 +18,5 @@ const isHex = require('../is/hex'); */ module.exports = function hexHasPrefix (value: ?string): boolean { // flowlint-next-line sketchy-null-string:off - return !!(value && isHex(value) && value.substr(0, 2) === '0x'); + return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x'); }; diff --git a/packages/util/src/is/hex.js b/packages/util/src/is/hex.js index 46c90e685..5800eb45a 100644 --- a/packages/util/src/is/hex.js +++ b/packages/util/src/is/hex.js @@ -19,14 +19,14 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/; isHex('0x1234'); // => true isHex('0x1234', 8); // => false */ -module.exports = function isHex (value: mixed, bitLength: number = -1): boolean { - // $FlowFixMe for the regex we have a string +module.exports = function isHex (_value: mixed, bitLength: number = -1, ignoreLength: boolean = false): boolean { + // flowlint-next-line unclear-type:off + const value = ((_value: any): string); const isValidHex = value === '0x' || (isString(value) && HEX_REGEX.test(value)); if (isValidHex && bitLength !== -1) { - // $FlowFixMe type is a string as checked above - return value.length === (2 + bitLength / 4); + return value.length === (2 + Math.ceil(bitLength / 4)); } - return isValidHex; + return isValidHex && (ignoreLength || (value.length % 2 === 0)); }; diff --git a/packages/util/src/is/hex.spec.js b/packages/util/src/is/hex.spec.js index 03c7fbd93..af29669a3 100644 --- a/packages/util/src/is/hex.spec.js +++ b/packages/util/src/is/hex.spec.js @@ -5,7 +5,7 @@ const { isHex } = require('./index'); describe('isHex', () => { - const test = '123abcd45'; + const test = '1234abcd'; it('returns true on 0x hex values', () => { expect( @@ -19,6 +19,12 @@ describe('isHex', () => { ).toEqual(true); }); + it('returns false on hex values (non % 2)', () => { + expect( + isHex(`0x${test}0`) + ).toEqual(false); + }); + it('returns true on uppercase values', () => { expect( isHex(`0x${test.toUpperCase()}`) diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index e8a0b1683..ffa24adf6 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -13,7 +13,6 @@ const u8aFromString = require('./fromString'); const u8aToBn = require('./toBn'); const u8aToBuffer = require('./toBuffer'); const u8aToHex = require('./toHex'); -const u8aToHexShort = require('./toHexShort'); const u8aToString = require('./toString'); const u8aToU8a = require('./toU8a'); const u8aToUtf8 = require('./toUtf8'); @@ -32,7 +31,6 @@ module.exports = { u8aToBn, u8aToBuffer, u8aToHex, - u8aToHexShort, u8aToString, u8aToU8a, u8aToUtf8 diff --git a/packages/util/src/u8a/toHex.js b/packages/util/src/u8a/toHex.js index 7b32a7ddc..4468d682f 100644 --- a/packages/util/src/u8a/toHex.js +++ b/packages/util/src/u8a/toHex.js @@ -14,12 +14,24 @@ u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f */ -module.exports = function u8aToHex (value?: Uint8Array): string { +module.exports = function u8aToHex (value?: Uint8Array, bitLength: number = -1, isPrefixed: boolean = true): string { + const prefix = isPrefixed + ? '0x' + : ''; + if (!value || !value.length) { - return '0x'; + return prefix; + } + + const byteLength = Math.ceil(bitLength / 8); + + if (byteLength > 0 && value.length > byteLength) { + const halfLength = Math.ceil(byteLength / 2); + + return `${u8aToHex(value.subarray(0, halfLength), -1, isPrefixed)}…${u8aToHex(value.subarray(value.length - halfLength), -1, false)}`; } return value.reduce((result, item) => { - return result + ('0' + item.toString(16)).slice(-2); - }, '0x'); + return result + `0${item.toString(16)}`.slice(-2); + }, prefix); }; diff --git a/packages/util/src/u8a/toHex.spec.js b/packages/util/src/u8a/toHex.spec.js index 82ed913c1..7a6abdbee 100644 --- a/packages/util/src/u8a/toHex.spec.js +++ b/packages/util/src/u8a/toHex.spec.js @@ -11,6 +11,12 @@ describe('u8aToHex', () => { ).toEqual('0x'); }); + it('returns empty as "" (unprefixed)', () => { + expect( + u8aToHex('', -1, false) + ).toEqual(''); + }); + it('returns the hex value for the array', () => { expect( u8aToHex( @@ -19,6 +25,15 @@ describe('u8aToHex', () => { ).toEqual('0x80000a'); }); + it('returns the hex value for the array (unprefixed)', () => { + expect( + u8aToHex( + new Uint8Array([128, 0, 10]), + -1, false + ) + ).toEqual('80000a'); + }); + it('handles starting zeros correctly', () => { expect( u8aToHex( @@ -26,4 +41,21 @@ describe('u8aToHex', () => { ) ).toEqual('0x0001000000000000'); }); + + it('returns the hex value where allowed < max', () => { + expect( + u8aToHex( + new Uint8Array([128, 0, 10, 11], 64) + ) + ).toEqual('0x80000a0b'); + }); + + it('returns the trimmed hex value where allowed >= max', () => { + expect( + u8aToHex( + new Uint8Array([128, 0, 10, 11, 12, 13]), + 32 + ) + ).toEqual('0x8000…0c0d'); + }); }); diff --git a/packages/util/src/u8a/toHexShort.js b/packages/util/src/u8a/toHexShort.js deleted file mode 100644 index bbca4df48..000000000 --- a/packages/util/src/u8a/toHexShort.js +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2017-2018 Jaco Greeff -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. -// @flow - -const u8aToHex = require('./toHex'); - -/** - @name u8aToHexShort - @signature u8aToHexShort (value?: UInt8Array, max: number = 16): string - @summary Creates a hex string from a Uint8Array object with max characters. - @description - `UInt8Array` input values return the actual hex string shortened to the specified characters. `null` or `undefined` values returns an `0x` string. - @example - import { u8aToHexShort } from '@polkadot/util'; - - u8aToHexShort(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf]), 4); // 0x68...0f -*/ -module.exports = function u8aToHexShort (value?: Uint8Array, _max: number = 16): string { - if (!value || !value.length) { - return '0x'; - } - - const max = _max / 2; - - if (value.length <= max) { - return u8aToHex(value); - } - - return `${u8aToHex(value.subarray(0, max))}…`; -}; diff --git a/packages/util/src/u8a/toHexShort.spec.js b/packages/util/src/u8a/toHexShort.spec.js deleted file mode 100644 index 6ae60d7a8..000000000 --- a/packages/util/src/u8a/toHexShort.spec.js +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017-2018 Jaco Greeff -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -const { u8aToHexShort } = require('./index'); - -describe('u8aToHexShort', () => { - const input = new Uint8Array([1, 2, 3, 4, 5, 0, 6, 7, 8, 9]); - - it('returns empty as 0x', () => { - expect( - u8aToHexShort() - ).toEqual('0x'); - }); - - it('returns the hex value for the array (max >= length)', () => { - expect( - u8aToHexShort(input, 20) - ).toEqual('0x01020304050006070809'); - }); - - it('returns the hex value for the array (max (default) < length)', () => { - expect( - u8aToHexShort(input) - ).toEqual('0x0102030405000607…'); - }); - - it('returns the hex value for the array (max < length)', () => { - expect( - u8aToHexShort(input, 10) - ).toEqual('0x0102030405…'); - }); -});