Hex <-> Uint8Array utils (#69)
This commit is contained in:
@@ -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
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user