Introduce @polkadot/util-rlp (#76)
* Add toU8a conversions * Add u8a utilities * @polkadot/rlp with encoding * Decoding test runthrough * Update util locations * Convert util-triehash to use util-rlp * Allow keccak from Uint8Array * Pass Uint8Array to keccak input * Update documentation * Skip some doc generation * Update @polkadot/dev * Update doc typo * Update allocations * Retrieve tests from ethereumjs-testing * Simplify test retrieval * Exclude etereumjs-testing from flow
This commit is contained in:
@@ -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
|
||||
|
||||
/**
|
||||
@name u8aConcat
|
||||
@signature u8aConcat (values: Array<Uint8Array>): Uint8Array
|
||||
@summary Creates a concatenated Uint8Array from the inputs.
|
||||
@description
|
||||
Concatenates the input arrays into a single `UInt8Array`.
|
||||
@example
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
u8aConcat([
|
||||
new Uint8Array([1, 2, 3]),
|
||||
new Uint8Array([4, 5, 6])
|
||||
]); // [1, 2, 3, 4, 5, 6]
|
||||
*/
|
||||
module.exports = function u8aConcat (list: Array<Uint8Array>): Uint8Array {
|
||||
const length = list.reduce((total, item) => total + item.length, 0);
|
||||
let offset = 0;
|
||||
|
||||
return list.reduce((result, item) => {
|
||||
result.set(item, offset);
|
||||
offset += item.length;
|
||||
|
||||
return result;
|
||||
}, new Uint8Array(length));
|
||||
};
|
||||
@@ -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 { u8aConcat } = require('./index');
|
||||
|
||||
describe('u8aConcat', () => {
|
||||
it('concatenates arrays', () => {
|
||||
expect(
|
||||
u8aConcat([
|
||||
new Uint8Array([1, 2, 3, 4]),
|
||||
new Uint8Array([5, 6]),
|
||||
new Uint8Array([7, 8, 9])
|
||||
])
|
||||
).toEqual(
|
||||
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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 u8aFromBn = require('../bn/toU8a');
|
||||
|
||||
/**
|
||||
@name u8aFromBn
|
||||
@signature u8aFromBn (value?: BN): Uint8Array
|
||||
@summary Creates a Uint8Array value from a BN object.
|
||||
@alias bn/toU8a
|
||||
*/
|
||||
module.exports = u8aFromBn;
|
||||
@@ -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 { u8aFromBn } = require('./index');
|
||||
|
||||
describe('u8aFromBn', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(u8aFromBn)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,8 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aConcat = require('./concat');
|
||||
const u8aFromBn = require('./fromBn');
|
||||
const u8aFromBuffer = require('./fromBuffer');
|
||||
const u8aFromHex = require('./fromHex');
|
||||
const u8aFromUtf8 = require('./fromUtf8');
|
||||
@@ -15,6 +17,8 @@ const u8aToUtf8 = require('./toUtf8');
|
||||
@summary Utility methods to convert to and from `Uint8Array` objects
|
||||
*/
|
||||
module.exports = {
|
||||
u8aConcat,
|
||||
u8aFromBn,
|
||||
u8aFromBuffer,
|
||||
u8aFromHex,
|
||||
u8aFromString,
|
||||
|
||||
Reference in New Issue
Block a user