From 19e365e2c8740530761d763da192bdd90d818532 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 30 Jan 2018 12:36:01 +0100 Subject: [PATCH] Hex <-> Uint8Array utils (#69) --- packages/util/docs/hex.md | 18 ++++++++++++++ packages/util/docs/u8a.md | 15 +++++++++++- packages/util/src/hex/index.js | 4 ++- packages/util/src/hex/toBuffer.spec.js | 8 ++++++ packages/util/src/hex/toU8a.js | 34 ++++++++++++++++++++++++++ packages/util/src/hex/toU8a.spec.js | 29 ++++++++++++++++++++++ packages/util/src/u8a/fromBuffer.js | 2 +- packages/util/src/u8a/fromHex.js | 14 +++++++++++ packages/util/src/u8a/fromHex.spec.js | 15 ++++++++++++ packages/util/src/u8a/index.js | 2 ++ 10 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 packages/util/src/hex/toU8a.js create mode 100644 packages/util/src/hex/toU8a.spec.js create mode 100644 packages/util/src/u8a/fromHex.js create mode 100644 packages/util/src/u8a/fromHex.spec.js diff --git a/packages/util/docs/hex.md b/packages/util/docs/hex.md index e06f0950e..19d1e19ad 100644 --- a/packages/util/docs/hex.md +++ b/packages/util/docs/hex.md @@ -12,6 +12,7 @@ Internal utilities to create and test for hex values - [hexToBn](#hextobn) Creates a BN.js bignumber object from a hex string. - [hexToBuffer](#hextobuffer) Creates a Buffer object from a hex string. - [hexToNumber](#hextonumber) Creates a Number value from a Buffer object. +- [hexToU8a](#hextou8a) Creates a Buffer object from a hex string. ## hexAddPrefix @@ -168,4 +169,21 @@ hexToNumber (value?: string): number import { hexToNumber } from '@polkadot/util'; hexToNumber('0x1234'); // => 0x1234 +``` + +## hexToU8a + +Creates a Buffer object from a hex string. + +```js +hexToU8a (value?: string): Uint8Array +``` + + +`null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error. + +```js +import { hexToU8a } from '@polkadot/util'; + +hexToU8a('0x123480001f'); // Uint8Array([0x12, 0x34, 0x80, 0x00, 0x1f]) ``` \ No newline at end of file diff --git a/packages/util/docs/u8a.md b/packages/util/docs/u8a.md index f1e2df16d..66ce43cbe 100644 --- a/packages/util/docs/u8a.md +++ b/packages/util/docs/u8a.md @@ -3,6 +3,7 @@ Utility methods to convert to and from `Uint8Array` objects - [u8aFromBuffer](#u8afrombuffer) Creates a Uint8Array value from a Buffer object. +- [u8aFromHex](#u8afromhex) Creates a Uint8Array value from a hex string. - [u8aFromUtf8](#u8afromutf8) Creates a Uint8Array object from a utf-8 string. - [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string. - [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object. @@ -12,7 +13,19 @@ Utility methods to convert to and from `Uint8Array` objects Creates a Uint8Array value from a Buffer object. [(alias of bufferToU8a)](buffer.md#buffertou8a) ```js -u8aFromBuffer (value?: Buffer): number +u8aFromBuffer (value?: Buffer): Uint8Array +``` + + + + + +## u8aFromHex + +Creates a Uint8Array value from a hex string. [(alias of hexToU8a)](hex.md#hextou8a) + +```js +u8aFromHex (value?: string): Uint8Array ``` diff --git a/packages/util/src/hex/index.js b/packages/util/src/hex/index.js index 6d861aad3..0d3d85d65 100644 --- a/packages/util/src/hex/index.js +++ b/packages/util/src/hex/index.js @@ -13,6 +13,7 @@ const hexStripPrefix = require('./stripPrefix'); const hexToBn = require('./toBn'); const hexToBuffer = require('./toBuffer'); const hexToNumber = require('./toNumber'); +const hexToU8a = require('./toU8a'); /** @summary Internal utilities to create and test for hex values @@ -27,5 +28,6 @@ module.exports = { hexStripPrefix, hexToBn, hexToBuffer, - hexToNumber + hexToNumber, + hexToU8a }; diff --git a/packages/util/src/hex/toBuffer.spec.js b/packages/util/src/hex/toBuffer.spec.js index b4ef17875..1a32ceb9c 100644 --- a/packages/util/src/hex/toBuffer.spec.js +++ b/packages/util/src/hex/toBuffer.spec.js @@ -18,4 +18,12 @@ describe('hexToBuffer', () => { ) ).toEqual(true); }); + + it('returns a buffer with the correct values (no prefix)', () => { + expect( + Buffer.from([128, 0, 10]).equals( + hexToBuffer('80000a') + ) + ).toEqual(true); + }); }); diff --git a/packages/util/src/hex/toU8a.js b/packages/util/src/hex/toU8a.js new file mode 100644 index 000000000..ea205f3b7 --- /dev/null +++ b/packages/util/src/hex/toU8a.js @@ -0,0 +1,34 @@ +// 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 hexStripPrefix = require('./stripPrefix'); + +/** + @name hexToU8a + @signature hexToU8a (value?: string): Uint8Array + @summary Creates a Buffer object from a hex string. + @description + `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error. + @example + import { hexToU8a } from '@polkadot/util'; + + hexToU8a('0x123480001f'); // Uint8Array([0x12, 0x34, 0x80, 0x00, 0x1f]) +*/ +module.exports = function hexToU8a (_value?: string): Uint8Array { + // flowlint-next-line sketchy-null-string:off + if (!_value) { + return new Uint8Array([]); + } + + const value = hexStripPrefix(_value); + const bufLength = Math.ceil(value.length / 2); + const result = new Uint8Array(bufLength); + + for (let index = 0; index < bufLength; index++) { + result[index] = parseInt(value.substr(index * 2, 2), 16); + } + + return result; +}; diff --git a/packages/util/src/hex/toU8a.spec.js b/packages/util/src/hex/toU8a.spec.js new file mode 100644 index 000000000..e8b554f50 --- /dev/null +++ b/packages/util/src/hex/toU8a.spec.js @@ -0,0 +1,29 @@ +// 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 { hexToU8a } = require('./index'); + +describe('hexToU8a', () => { + it('returns an empty Uint8Array when null provided', () => { + expect( + hexToU8a(null) + ).toHaveLength(0); + }); + + it('returns a Uint8Array with the correct values', () => { + expect( + hexToU8a('0x80000a') + ).toEqual( + new Uint8Array([128, 0, 10]) + ); + }); + + it('returns a Uint8Array with the correct values (no prefix)', () => { + expect( + hexToU8a('80000a') + ).toEqual( + new Uint8Array([128, 0, 10]) + ); + }); +}); diff --git a/packages/util/src/u8a/fromBuffer.js b/packages/util/src/u8a/fromBuffer.js index d68a356fe..4d98d1973 100644 --- a/packages/util/src/u8a/fromBuffer.js +++ b/packages/util/src/u8a/fromBuffer.js @@ -7,7 +7,7 @@ const u8aFromBuffer = require('../buffer/toU8a'); /** @name u8aFromBuffer - @signature u8aFromBuffer (value?: Buffer): number + @signature u8aFromBuffer (value?: Buffer): Uint8Array @summary Creates a Uint8Array value from a Buffer object. @alias buffer/toU8a */ diff --git a/packages/util/src/u8a/fromHex.js b/packages/util/src/u8a/fromHex.js new file mode 100644 index 000000000..8e354e411 --- /dev/null +++ b/packages/util/src/u8a/fromHex.js @@ -0,0 +1,14 @@ +// 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 u8aFromHex = require('../hex/toU8a'); + +/** + @name u8aFromHex + @signature u8aFromHex (value?: string): Uint8Array + @summary Creates a Uint8Array value from a hex string. + @alias hex/toU8a +*/ +module.exports = u8aFromHex; diff --git a/packages/util/src/u8a/fromHex.spec.js b/packages/util/src/u8a/fromHex.spec.js new file mode 100644 index 000000000..a777c8669 --- /dev/null +++ b/packages/util/src/u8a/fromHex.spec.js @@ -0,0 +1,15 @@ +// 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 isFunction = require('../is/function'); + +const { u8aFromHex } = require('./index'); + +describe('u8aFromHex', () => { + it('exists as a function', () => { + expect( + isFunction(u8aFromHex) + ).toEqual(true); + }); +}); diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index fd13111be..39d80dea1 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -4,6 +4,7 @@ // @flow const u8aFromBuffer = require('./fromBuffer'); +const u8aFromHex = require('./fromHex'); const u8aFromUtf8 = require('./fromUtf8'); const u8aToBuffer = require('./toBuffer'); const u8aToUtf8 = require('./toUtf8'); @@ -13,6 +14,7 @@ const u8aToUtf8 = require('./toUtf8'); */ module.exports = { u8aFromBuffer, + u8aFromHex, u8aFromUtf8, u8aToBuffer, u8aToUtf8