Add Blake2s & XXHash utility functions (#66)
* Initial split of util & util-crypto * Add initial XXHash version * Blake2s wit tests & documentation * Add sha512 (from NaCl) * Add randomBytes (from NaCl) * Flow fixes * Update documentation * Update latest @polkadot/dev
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// 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 hasPrefix = require('./hasPrefix');
|
||||
|
||||
/**
|
||||
@name hexAddPrefix
|
||||
@signature hexAddPrefix (value: ?string): string
|
||||
@summary Adds the `0x` prefix to string values.
|
||||
@description
|
||||
Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
|
||||
@example
|
||||
import { hexAddPrefix } from '@polkadot/util';
|
||||
|
||||
console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12
|
||||
*/
|
||||
module.exports = function hexAddPrefix (value: ?string): string {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (value && hasPrefix(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
const prefix = value && value.length % 2 === 1
|
||||
? '0'
|
||||
: '';
|
||||
|
||||
return `0x${prefix}${value || ''}`;
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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 { hexAddPrefix } = require('./index');
|
||||
|
||||
describe('hexAddPrefix', () => {
|
||||
it('does not add when prefix is available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0x0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds the prefix when it is not available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds extra 0 when length % 2 === 1', () => {
|
||||
expect(
|
||||
hexAddPrefix('123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('returns null as 0x', () => {
|
||||
expect(
|
||||
hexAddPrefix(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
// 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 ZEROS_256 = '0000000000000000000000000000000000000000000000000000000000000000';
|
||||
|
||||
module.exports = {
|
||||
ZEROS_256
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
// 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 hexAddPrefix = require('./addPrefix');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
const { ZEROS_256 } = require('./constants');
|
||||
|
||||
/**
|
||||
@name hexFixLength
|
||||
@signature hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string
|
||||
@summary 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). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`.
|
||||
@example
|
||||
import { hexFixLength } from '@polkadot/util';
|
||||
|
||||
console.log('fixed', hexFixLength('0x12', 16)) // => 0x12
|
||||
console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012
|
||||
console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12
|
||||
*/
|
||||
module.exports = function hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string {
|
||||
const strLength = bitLength / 4;
|
||||
const hexLength = strLength + 2;
|
||||
|
||||
if (bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength)) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value)
|
||||
);
|
||||
}
|
||||
|
||||
if (value.length > hexLength) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value).slice(-1 * strLength)
|
||||
);
|
||||
}
|
||||
|
||||
return hexAddPrefix(
|
||||
`${ZEROS_256}${hexStripPrefix(value)}`.slice(-1 * strLength)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
// 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 { hexFixLength } = require('./index');
|
||||
|
||||
describe('hexFixLength', () => {
|
||||
it('returns bitLength === -1 as-is', () => {
|
||||
expect(
|
||||
hexFixLength('0x12345678')
|
||||
).toEqual('0x12345678');
|
||||
});
|
||||
|
||||
it('returns bitLength === -1 as-is (adding missing 0)', () => {
|
||||
expect(
|
||||
hexFixLength('0x1234567')
|
||||
).toEqual('0x01234567');
|
||||
});
|
||||
|
||||
it('does not change when bitlength === length', () => {
|
||||
expect(
|
||||
hexFixLength('0x12345678', 32)
|
||||
).toEqual('0x12345678');
|
||||
});
|
||||
|
||||
it('trims values when bitLength > length', () => {
|
||||
expect(
|
||||
hexFixLength('0x12345678', 16)
|
||||
).toEqual('0x5678');
|
||||
});
|
||||
|
||||
it('returns as-is when bitLength < length', () => {
|
||||
expect(
|
||||
hexFixLength('0x1234', 32)
|
||||
).toEqual('0x1234');
|
||||
});
|
||||
|
||||
it('adds zeros when bitLength < length (withPadded)', () => {
|
||||
expect(
|
||||
hexFixLength('0x1234', 32, true)
|
||||
).toEqual('0x00001234');
|
||||
});
|
||||
});
|
||||
@@ -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 hexFromBn = require('../bn/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromBn
|
||||
@signature hexFromBn (value?: BN): string
|
||||
@summary Creates a hex value from a BN.js bignumber object.
|
||||
@alias bn/toHex
|
||||
*/
|
||||
module.exports = hexFromBn;
|
||||
@@ -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 { hexFromBn } = require('./index');
|
||||
|
||||
describe('hexFromBn', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromBn)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -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 bufferToHex = require('../buffer/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromBuffer
|
||||
@signature hexFromBuffer (value?: Buffer): string
|
||||
@summary Creates a hex value from a Buffer object.
|
||||
@alias buffer/toHex
|
||||
*/
|
||||
module.exports = bufferToHex;
|
||||
@@ -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 { hexFromBuffer } = require('./index');
|
||||
|
||||
describe('hexFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -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 hexFromNumber = require('../number/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromNumber
|
||||
@signature hexFromNumber (value?: number): string
|
||||
@summary Creates a hex value from a number.
|
||||
@alias number/toHex
|
||||
*/
|
||||
module.exports = hexFromNumber;
|
||||
@@ -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 { hexFromNumber } = require('./index');
|
||||
|
||||
describe('hexFromNumber', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromNumber)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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 isHex = require('../is/hex');
|
||||
|
||||
/**
|
||||
@name hexHasPrefix
|
||||
@signature hexHasPrefix (value: ?string): boolean
|
||||
@summary Tests for the existence of a `0x` prefix.
|
||||
@description
|
||||
Checks for a valid hex input value and if the start matched `0x`
|
||||
@example
|
||||
import { hexHasPrefix } from '@polkadot/util';
|
||||
|
||||
console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
*/
|
||||
module.exports = function hexHasPrefix (value: ?string): boolean {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
return !!(value && isHex(value) && value.substr(0, 2) === '0x');
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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 { hexHasPrefix } = require('./index');
|
||||
|
||||
describe('hexHasPrefix', () => {
|
||||
it('returns true when hex prefix is found', () => {
|
||||
expect(
|
||||
hexHasPrefix('0x123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false when no prefix attached', () => {
|
||||
expect(
|
||||
hexHasPrefix('123')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when null value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when non-string value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(false)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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 hexAddPrefix = require('./addPrefix');
|
||||
const hexFixLength = require('./fixLength');
|
||||
const hexFromBn = require('./fromBn');
|
||||
const hexFromBuffer = require('./fromBuffer');
|
||||
const hexFromNumber = require('./fromNumber');
|
||||
const hexHasPrefix = require('./hasPrefix');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
const hexToBn = require('./toBn');
|
||||
const hexToBuffer = require('./toBuffer');
|
||||
const hexToNumber = require('./toNumber');
|
||||
|
||||
/**
|
||||
@summary Internal utilities to create and test for hex values
|
||||
*/
|
||||
module.exports = {
|
||||
hexAddPrefix,
|
||||
hexFixLength,
|
||||
hexFromBn,
|
||||
hexFromBuffer,
|
||||
hexFromNumber,
|
||||
hexHasPrefix,
|
||||
hexStripPrefix,
|
||||
hexToBn,
|
||||
hexToBuffer,
|
||||
hexToNumber
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
// 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 hasPrefix = require('./hasPrefix');
|
||||
|
||||
/**
|
||||
@name hexStripPrefix
|
||||
@signature hexStripPrefix (value: ?string): string
|
||||
@summary Strips any leading `0x` prefix.
|
||||
@description
|
||||
Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
|
||||
@example
|
||||
import { hexStripPrefix } from '@polkadot/util';
|
||||
|
||||
console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
*/
|
||||
module.exports = function hexStripPrefix (value: ?string): string {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (value && hasPrefix(value)) {
|
||||
return value.substr(2);
|
||||
}
|
||||
|
||||
return value || '';
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 { hexStripPrefix } = require('./index');
|
||||
|
||||
describe('hexStripPrefix', () => {
|
||||
it('returns the value as-is when no prefix', () => {
|
||||
expect(
|
||||
hexStripPrefix('01ab')
|
||||
).toEqual('01ab');
|
||||
});
|
||||
|
||||
it('returns an empty string when null value supplied', () => {
|
||||
expect(
|
||||
hexStripPrefix(null)
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('strips the prefix from other strings', () => {
|
||||
expect(
|
||||
hexStripPrefix('0x1223')
|
||||
).toEqual('1223');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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 BN = require('bn.js');
|
||||
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
const ZERO_BN = new BN(0);
|
||||
|
||||
/**
|
||||
@name hexToBn
|
||||
@signature hexToBn (value?: string): BN
|
||||
@summary Creates a BN.js bignumber object from a hex string.
|
||||
@description
|
||||
`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { hexToBn } from '@polkadot/util';
|
||||
|
||||
hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
*/
|
||||
module.exports = function hexToBn (value?: string): BN {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return ZERO_BN;
|
||||
}
|
||||
|
||||
return new BN(hexStripPrefix(value), 16);
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
// 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 BN = require('bn.js');
|
||||
|
||||
const { hexToBn } = require('./index');
|
||||
|
||||
describe('hexToBn', () => {
|
||||
const value = '80';
|
||||
|
||||
it('converts prefixed hex values to BN', () => {
|
||||
expect(
|
||||
hexToBn(`0x${value}`).eq(
|
||||
new BN(value, 16)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('converts null values to BN(0)', () => {
|
||||
expect(
|
||||
hexToBn(null).eq(
|
||||
new BN(0)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// 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 hexToBuffer
|
||||
@signature hexToBuffer (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a hex string.
|
||||
@description
|
||||
`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { hexToBuffer } from '@polkadot/util';
|
||||
|
||||
hexToBuffer('0x123480001f'); // Buffer([0x12, 0x34, 0x80, 0x00, 0x1f])
|
||||
*/
|
||||
module.exports = function hexToBuffer (value?: string): Buffer {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return Buffer.from(hexStripPrefix(value), 'hex');
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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 { hexToBuffer } = require('./index');
|
||||
|
||||
describe('hexToBuffer', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
hexToBuffer(null)
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('returns a buffer with the correct values', () => {
|
||||
expect(
|
||||
Buffer.from([128, 0, 10]).equals(
|
||||
hexToBuffer('0x80000a')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -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.
|
||||
// @flow
|
||||
|
||||
const bufferToNumber = require('../buffer/toNumber');
|
||||
const hexToBuffer = require('../hex/toBuffer');
|
||||
|
||||
/**
|
||||
@name hexToNumber
|
||||
@signature hexToNumber (value?: string): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
@example
|
||||
import { hexToNumber } from '@polkadot/util';
|
||||
|
||||
hexToNumber('0x1234'); // => 0x1234
|
||||
*/
|
||||
module.exports = function hexToNumber (value?: string): number {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return bufferToNumber(
|
||||
hexToBuffer(value)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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 { hexToNumber } = require('./index');
|
||||
|
||||
describe('hexToNumber', () => {
|
||||
it('converts an empty to NaN', () => {
|
||||
expect(
|
||||
hexToNumber()
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts to a number from hex', () => {
|
||||
expect(
|
||||
hexToNumber('0x1234')
|
||||
).toEqual(0x1234);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user