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:
Jaco Greeff
2018-02-08 14:50:54 +01:00
committed by GitHub
parent a18e7f8474
commit e832aebb07
64 changed files with 1073 additions and 188 deletions
+3 -1
View File
@@ -5,11 +5,13 @@
const bnFromHex = require('./fromHex');
const bnToHex = require('./toHex');
const bnToU8a = require('./toU8a');
/**
@summary Utility methods to convert to and from `BN` objects
*/
module.exports = {
bnFromHex,
bnToHex
bnToHex,
bnToU8a
};
+26
View File
@@ -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
import type BN from 'bn.js';
/**
@name bnToU8a
@signature bnToU8a (value?: BN): Uint8Array
@summary Creates a Uint8Array object from a BN.
@description
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`.
@example
import { bnToU8a } from '@polkadot/util';
bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
*/
module.exports = function bnToU8a (value?: BN): Uint8Array {
if (!value) {
return new Uint8Array([]);
}
// $FlowFixMe toArray doesn't need params
return new Uint8Array(value.toArray());
};
+21
View File
@@ -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 BN = require('bn.js');
const { bnToU8a } = require('./index');
describe('bnToU8a', () => {
it('converts BN values to a prefixed hex representation', () => {
expect(
bnToU8a(new BN(0x123456))
).toEqual(new Uint8Array([0x12, 0x34, 0x56]));
});
it('converts null values to 0x00', () => {
expect(
bnToU8a(null)
).toEqual(new Uint8Array([]));
});
});
+1 -7
View File
@@ -19,11 +19,5 @@ module.exports = function bufferToU8a (buffer?: Buffer): Uint8Array {
return new Uint8Array([]);
}
const array = new Uint8Array(buffer.length);
for (let index = 0; index < buffer.length; ++index) {
array[index] = buffer[index];
}
return array;
return new Uint8Array(buffer);
};
+3 -1
View File
@@ -7,6 +7,7 @@ const numberFromBuffer = require('./fromBuffer');
const numberFromHex = require('./fromHex');
const numberToBuffer = require('./toBuffer');
const numberToHex = require('./toHex');
const numberToU8a = require('./toU8a');
/**
@summary Utility methods to convert to and from `number` values
@@ -15,5 +16,6 @@ module.exports = {
numberFromBuffer,
numberFromHex,
numberToBuffer,
numberToHex
numberToHex,
numberToU8a
};
+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.
// @flow
const hexToU8a = require('../hex/toU8a');
const numberToHex = require('./toHex');
/**
@name numberToU8a
@signature numberToBuffer (value?: number): Uint8Array
@summary Creates a Uint8Array object from a number.
@description
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`.
@example
import { numberToU8a } from '@polkadot/util';
numberToU8a(0x1234); // => [0x12, 0x34]
*/
module.exports = function numberToU8a (value?: number): Uint8Array {
// flowlint-next-line sketchy-null-number:off
if (!value || isNaN(value)) {
return new Uint8Array([]);
}
return hexToU8a(
numberToHex(value)
);
};
+37
View File
@@ -0,0 +1,37 @@
// 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 { numberToU8a } = require('./index');
describe('numberToU8a', () => {
it('converts 0 to empty', () => {
expect(
numberToU8a(0)
).toEqual(new Uint8Array([]));
});
it('converts undefined to empty', () => {
expect(
numberToU8a()
).toEqual(new Uint8Array([]));
});
it('converts null to empty', () => {
expect(
numberToU8a(null)
).toEqual(new Uint8Array([]));
});
it('converts NaN to empty', () => {
expect(
numberToU8a(NaN)
).toEqual(new Uint8Array([]));
});
it('converts values to the buffer', () => {
expect(
numberToU8a(0x3456)
).toEqual(new Uint8Array([0x34, 0x56]));
});
});
+30
View File
@@ -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));
};
+19
View File
@@ -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])
);
});
});
+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 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;
+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 { u8aFromBn } = require('./index');
describe('u8aFromBn', () => {
it('exists as a function', () => {
expect(
isFunction(u8aFromBn)
).toEqual(true);
});
});
+4
View File
@@ -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,