Hex <-> Uint8Array utils (#69)

This commit is contained in:
Jaco Greeff
2018-01-30 12:36:01 +01:00
committed by GitHub
parent 1ce557fa4a
commit 19e365e2c8
10 changed files with 138 additions and 3 deletions
+3 -1
View File
@@ -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
};
+8
View File
@@ -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);
});
});
+34
View File
@@ -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;
};
+29
View File
@@ -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])
);
});
});
+1 -1
View File
@@ -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
*/
+14
View File
@@ -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;
+15
View File
@@ -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);
});
});
+2
View File
@@ -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