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:
@@ -12,7 +12,7 @@ Create Keccak256 values as hex, string, Buffer & Uint8Array output
|
||||
Creates a keccak Buffer from the input.
|
||||
|
||||
```js
|
||||
keccakAsBuffer (value: Buffer | string): Buffer
|
||||
keccakAsBuffer (value: Buffer | Uint8Array | string): Buffer
|
||||
```
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ console.log('asBuffer', keccakAsBuffer('123')) // => Buffer
|
||||
Creates a keccak hex string from the input.
|
||||
|
||||
```js
|
||||
keccakAsHex (value: Buffer | string): string
|
||||
keccakAsHex (value: Buffer | Uint8Array | string): string
|
||||
```
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ console.log('asHex', keccakAsHex('123')) // => 0x...
|
||||
Creates a keccak string from the input.
|
||||
|
||||
```js
|
||||
keccakAsString (value: Buffer | string): string
|
||||
keccakAsString (value: Buffer | Uint8Array | string): string
|
||||
```
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ console.log('asString', keccakAsString('123')) // => string
|
||||
Creates a keccak Uint8Array from the input.
|
||||
|
||||
```js
|
||||
keccakAsU8a (value: Buffer | string): Uint8Array
|
||||
keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.26.0",
|
||||
"blakejs": "^1.1.0",
|
||||
"keccak": "^1.3.0",
|
||||
"keccak": "jacogr/keccak#f25ae4b299038bb2f0b72f150b99b5048ffb9e48",
|
||||
"tweetnacl": "^1.0.0",
|
||||
"xxhashjs": "^0.2.2"
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ const createKeccak = require('keccak');
|
||||
|
||||
/**
|
||||
@name keccakAsBuffer
|
||||
@signature keccakAsBuffer (value: Buffer | string): Buffer
|
||||
@signature keccakAsBuffer (value: Buffer | Uint8Array | string): Buffer
|
||||
@summary Creates a keccak Buffer from the input.
|
||||
@description
|
||||
From either a `string` or a `Buffer` input, create the keccak and return the result as a `Buffer`.
|
||||
@@ -16,6 +16,8 @@ const createKeccak = require('keccak');
|
||||
|
||||
console.log('asBuffer', keccakAsBuffer('123')) // => Buffer
|
||||
*/
|
||||
module.exports = function keccakAsBuffer (value: Buffer | string): Buffer {
|
||||
return createKeccak('keccak256').update(value).digest();
|
||||
module.exports = function keccakAsBuffer (value: Buffer | Uint8Array | string): Buffer {
|
||||
// HACK: For the time being, don't change the definition (although fork of library is used)
|
||||
// flowlint-next-line unclear-type:off
|
||||
return createKeccak('keccak256').update(((value: any): Buffer)).digest();
|
||||
};
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const u8aFromString = require('@polkadot/util/u8a/fromString');
|
||||
|
||||
const { keccakAsBuffer } = require('./index');
|
||||
|
||||
const value = 'test value';
|
||||
@@ -15,4 +17,12 @@ describe('keccakAsBuffer', () => {
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns an hex representation (Uint8Array input)', () => {
|
||||
expect(
|
||||
keccakAsBuffer(u8aFromString(value)).equals(
|
||||
Buffer.from(result, 'hex')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ const keccakAsBuffer = require('./asBuffer');
|
||||
|
||||
/**
|
||||
@name keccakAsHex
|
||||
@signature keccakAsHex (value: Buffer | string): string
|
||||
@signature keccakAsHex (value: Buffer | Uint8Array | string): string
|
||||
@summary Creates a keccak hex string from the input.
|
||||
@description
|
||||
From either a `string` or a `Buffer` input, create the keccak and return the result as a `0x` prefixed hex string.
|
||||
@@ -18,7 +18,7 @@ const keccakAsBuffer = require('./asBuffer');
|
||||
|
||||
console.log('asHex', keccakAsHex('123')) // => 0x...
|
||||
*/
|
||||
module.exports = function keccakAsHex (value: Buffer | string): string {
|
||||
module.exports = function keccakAsHex (value: Buffer | Uint8Array | string): string {
|
||||
return bufferToHex(
|
||||
keccakAsBuffer(value)
|
||||
);
|
||||
|
||||
@@ -7,7 +7,7 @@ const keccakAsBuffer = require('./asBuffer');
|
||||
|
||||
/**
|
||||
@name keccakAsString
|
||||
@signature keccakAsString (value: Buffer | string): string
|
||||
@signature keccakAsString (value: Buffer | Uint8Array | string): string
|
||||
@summary Creates a keccak string from the input.
|
||||
@description
|
||||
From either a `string` or a `Buffer` input, create the keccak and return the result as a non-prefixed string.
|
||||
@@ -16,6 +16,6 @@ const keccakAsBuffer = require('./asBuffer');
|
||||
|
||||
console.log('asString', keccakAsString('123')) // => string
|
||||
*/
|
||||
module.exports = function keccakAsString (value: Buffer | string): string {
|
||||
module.exports = function keccakAsString (value: Buffer | Uint8Array | string): string {
|
||||
return keccakAsBuffer(value).toString('hex');
|
||||
};
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
// @flow
|
||||
|
||||
const bufferToU8a = require('@polkadot/util/buffer/toU8a');
|
||||
const createKeccak = require('keccak');
|
||||
const keccakAsBuffer = require('./asBuffer');
|
||||
|
||||
/**
|
||||
@name keccakAsU8a
|
||||
@signature keccakAsU8a (value: Buffer | string): Uint8Array
|
||||
@signature keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array
|
||||
@summary Creates a keccak Uint8Array from the input.
|
||||
@description
|
||||
From either a `string` or a `Buffer` input, create the keccak and return the result as a `Uint8Array`.
|
||||
@@ -17,8 +17,8 @@ const createKeccak = require('keccak');
|
||||
|
||||
keccakAsU8a('123') // => Uint8Array
|
||||
*/
|
||||
module.exports = function keccakAsU8a (value: Buffer | string): Uint8Array {
|
||||
module.exports = function keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array {
|
||||
return bufferToU8a(
|
||||
createKeccak('keccak256').update(value).digest()
|
||||
keccakAsBuffer(value)
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
ISC License (ISC)
|
||||
|
||||
Copyright 2017-2018 Jaco Greeff <jacogr@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
@@ -0,0 +1,17 @@
|
||||
[](https://polkadot.js.org)
|
||||

|
||||
[](https://github.com/Flet/semistandard)
|
||||
[](https://www.npmjs.com/package/@polkadot/util-rlp)
|
||||
[](https://travis-ci.org/polkadot-js/util)
|
||||
[](https://codeclimate.com/github/polkadot-js/util/maintainability)
|
||||
[](https://coveralls.io/github/polkadot-js/util?branch=master)
|
||||
[](https://david-dm.org/polkadot-js/util?path=packages/util-rlp)
|
||||
[](https://david-dm.org/polkadot-js/util?path=packages/util-rlp#info=devDependencies)
|
||||
|
||||
# @polkadot/util-rlp
|
||||
|
||||
Encoding and decoding for [Ethereum RLP structures](https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP). This is port of the original [EthereumJS RLP](https://github.com/ethereumjs/rlp) to cater for `Uint8Array` outputs (instead of `Buffer`) in addition to typings.
|
||||
|
||||
## Available Utilities
|
||||
|
||||
For a list of currently exposed methods, see the [library documentation](docs/README.md).
|
||||
@@ -0,0 +1,40 @@
|
||||
# Available interfaces
|
||||
|
||||
Utility methods to encode and decode [Ethereum RLP](https://github.com/ethereum/wiki/wiki/RLP). Adapted from the [EthereumJS implementation](https://github.com/ethereumjs/rlp/blob/0ce09db81fc303fcee593f7cc094ba44015f9b92/index.js)
|
||||
|
||||
|
||||
# Available methods
|
||||
|
||||
## decode
|
||||
|
||||
Decodes the input RLP.
|
||||
|
||||
```js
|
||||
decoder (input?: Uint8Array): Uint8Array | Array<*>
|
||||
```
|
||||
|
||||
|
||||
From an input, decod the RLP and return the result as a `Uint8Array` or `Array`.
|
||||
|
||||
```js
|
||||
import { decode } from '@polkadot/util-rlp';
|
||||
|
||||
encode(new Uint8Array([0x83, 0x64, 0x6f, 0x67])) // => 'dog' as Uint8Array
|
||||
```
|
||||
|
||||
## encode
|
||||
|
||||
Encodes the input value into RLP.
|
||||
|
||||
```js
|
||||
encode (input: any): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
From an input, calculate the RLP and return the result as a `Uint8Array`.
|
||||
|
||||
```js
|
||||
import { encode } from '@polkadot/util-rlp';
|
||||
|
||||
encode('dog') // => [0x83, 0x64, 0x6f, 0x67]
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@polkadot/util-rlp",
|
||||
"version": "0.14.1",
|
||||
"description": "RLP encoding and decoding",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"Polkadot"
|
||||
],
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=6.4"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/polkadot-js/util.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/polkadot-js/util/issues"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/util/tree/master/packages/util-triehash#readme",
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-babel && polkadot-dev-build-docs",
|
||||
"check": "eslint src && flow check",
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/util": "^0.14.1",
|
||||
"babel-runtime": "^6.26.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ethereumjs-testing": "^1.0.4"
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
// @flow
|
||||
|
||||
const decoder = require('./decoder');
|
||||
|
||||
/**
|
||||
@name decode
|
||||
@signature decoder (input?: Uint8Array): Uint8Array | Array<*>
|
||||
@summary Decodes the input RLP.
|
||||
@description
|
||||
From an input, decod the RLP and return the result as a `Uint8Array` or `Array`.
|
||||
@example
|
||||
import { decode } from '@polkadot/util-rlp';
|
||||
|
||||
encode(new Uint8Array([0x83, 0x64, 0x6f, 0x67])) // => 'dog' as Uint8Array
|
||||
*/
|
||||
module.exports = decoder;
|
||||
@@ -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.
|
||||
|
||||
const hexToU8a = require('@polkadot/util/hex/toU8a');
|
||||
const u8aToHex = require('@polkadot/util/u8a/toHex');
|
||||
|
||||
const { decode, encode } = require('./index');
|
||||
|
||||
const rlptests = require('../test/getTests')('RLPTests/rlptest.json');
|
||||
|
||||
describe('decode', () => {
|
||||
it('returns empty list for undefined inputs', () => {
|
||||
expect(
|
||||
decode()
|
||||
).toEqual(
|
||||
new Uint8Array([])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns empty list for empty inputs', () => {
|
||||
expect(
|
||||
decode([])
|
||||
).toEqual(
|
||||
new Uint8Array([])
|
||||
);
|
||||
});
|
||||
|
||||
rlptests.forEach(({ name, output }) => {
|
||||
it(`passes official ${name}`, () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
encode(
|
||||
decode(
|
||||
hexToU8a(output)
|
||||
)
|
||||
)
|
||||
)
|
||||
).toEqual(output);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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
|
||||
|
||||
import type { DecodeOutput } from './types';
|
||||
|
||||
const decodeListLong = require('./listLong');
|
||||
const decodeListShort = require('./listShort');
|
||||
const decodeNumber = require('./number');
|
||||
const decodeSingle = require('./single');
|
||||
const decodeString = require('./string');
|
||||
|
||||
function decode (input: Uint8Array): DecodeOutput {
|
||||
const firstByte = input[0];
|
||||
|
||||
// a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding
|
||||
if (firstByte <= 0x7f) {
|
||||
return decodeSingle(decode, input);
|
||||
}
|
||||
|
||||
// string is 0-55 bytes long. A single byte with value 0x80 plus the length of the string followed by the string
|
||||
// The range of the first byte is [0x80, 0xb7]
|
||||
if (firstByte <= 0xb7) {
|
||||
return decodeString(decode, input);
|
||||
}
|
||||
|
||||
// a number
|
||||
if (firstByte <= 0xbf) {
|
||||
return decodeNumber(decode, input);
|
||||
}
|
||||
|
||||
// a list between 0-55 bytes long
|
||||
if (firstByte <= 0xf7) {
|
||||
return decodeListShort(decode, input);
|
||||
}
|
||||
|
||||
// a list over 55 bytes long
|
||||
return decodeListLong(decode, input);
|
||||
}
|
||||
|
||||
module.exports = decode;
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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 assert = require('@polkadot/util/assert');
|
||||
|
||||
const internalDecode = require('./decode');
|
||||
|
||||
module.exports = function decoder (input?: Uint8Array): Uint8Array | Array<*> {
|
||||
if (!input || input.length === 0) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
const { decoded, remainder } = internalDecode(input);
|
||||
|
||||
assert(remainder.length === 0, 'invalid remainder');
|
||||
|
||||
return decoded;
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
|
||||
const safeParseInt = require('./safeParseInt');
|
||||
|
||||
module.exports = function decodeListLong (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const llength = input[0] - 0xf6;
|
||||
const length = safeParseInt(input.slice(1, llength));
|
||||
const totalLength = llength + length;
|
||||
const decoded = [];
|
||||
|
||||
assert(totalLength <= input.length, 'invalid rlp: total length is larger than the data');
|
||||
|
||||
let innerRemainder = input.slice(llength, totalLength);
|
||||
|
||||
assert(innerRemainder.length > 0, 'invalid rlp, List has a invalid length');
|
||||
|
||||
while (innerRemainder.length) {
|
||||
const d = decode(innerRemainder);
|
||||
|
||||
decoded.push(d.decoded);
|
||||
innerRemainder = d.remainder;
|
||||
}
|
||||
|
||||
return {
|
||||
decoded,
|
||||
remainder: input.slice(totalLength)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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 { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
module.exports = function decodeListShort (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const length = input[0] - 0xbf;
|
||||
let innerRemainder = input.slice(1, length);
|
||||
const decoded = [];
|
||||
|
||||
while (innerRemainder.length) {
|
||||
const d = decode(innerRemainder);
|
||||
|
||||
decoded.push(d.decoded);
|
||||
innerRemainder = d.remainder;
|
||||
}
|
||||
|
||||
return {
|
||||
decoded,
|
||||
remainder: input.slice(length)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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 { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
|
||||
const safeParseInt = require('./safeParseInt');
|
||||
|
||||
module.exports = function decodeNumber (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const llength = input[0] - 0xb6;
|
||||
const length = safeParseInt(input.slice(1, llength));
|
||||
const decoded = input.slice(llength, length + llength);
|
||||
|
||||
assert(decoded.length === length, 'invalid RLP');
|
||||
|
||||
return {
|
||||
decoded,
|
||||
remainder: input.slice(length + llength)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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 assert = require('@polkadot/util/assert');
|
||||
|
||||
module.exports = function safeParseInt (input: Uint8Array): number {
|
||||
assert(input[0] > 0, 'invalid RLP: extra zeros');
|
||||
|
||||
return input.reduce((result, value) => (result * 256) + value, 0);
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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 safeParseInt = require('./safeParseInt');
|
||||
|
||||
describe('safeParseInt', () => {
|
||||
it('does not allow 0 in first position', () => {
|
||||
expect(
|
||||
() => safeParseInt(
|
||||
new Uint8Array([0])
|
||||
)
|
||||
).toThrow(/invalid RLP: extra zeros/);
|
||||
});
|
||||
|
||||
it('converts a value to the correct number', () => {
|
||||
expect(
|
||||
safeParseInt(
|
||||
new Uint8Array([0x12, 0x34, 0x56])
|
||||
)
|
||||
).toEqual(0x123456);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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 { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
module.exports = function decodeSingle (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
return {
|
||||
decoded: input.slice(0, 1),
|
||||
remainder: input.slice(1)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
// 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 { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
|
||||
module.exports = function decodeString (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const firstByte = input[0];
|
||||
const length = firstByte - 0x7f;
|
||||
let decoded;
|
||||
|
||||
// set 0x80 null to 0
|
||||
if (firstByte === 0x80) {
|
||||
decoded = new Uint8Array([]);
|
||||
} else {
|
||||
decoded = input.slice(1, length);
|
||||
}
|
||||
|
||||
assert(!(length === 2 && decoded[0] < 0x80), 'invalid rlp encoding: byte must be less 0x80');
|
||||
|
||||
return {
|
||||
decoded,
|
||||
remainder: input.slice(length)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
|
||||
export type DecodeOutput = {
|
||||
decoded: Uint8Array | Array<*>,
|
||||
remainder: Uint8Array
|
||||
}
|
||||
|
||||
export type DecodeFunc = (input: Uint8Array) => DecodeOutput;
|
||||
@@ -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.
|
||||
// @flow
|
||||
|
||||
const encoder = require('./encoder');
|
||||
|
||||
/**
|
||||
@name encode
|
||||
@signature encode (input: any): Uint8Array
|
||||
@summary Encodes the input value into RLP.
|
||||
@description
|
||||
From an input, calculate the RLP and return the result as a `Uint8Array`.
|
||||
@example
|
||||
import { encode } from '@polkadot/util-rlp';
|
||||
|
||||
encode('dog') // => [0x83, 0x64, 0x6f, 0x67]
|
||||
*/
|
||||
module.exports = encoder;
|
||||
@@ -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 u8aToHex = require('@polkadot/util/u8a/toHex');
|
||||
|
||||
const { encode } = require('./index');
|
||||
|
||||
const rlptests = require('../test/getTests')('RLPTests/rlptest.json');
|
||||
|
||||
describe('encode', () => {
|
||||
rlptests.forEach(({ name, input, output }) => {
|
||||
it(`passes official ${name}`, () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
encode(input)
|
||||
)
|
||||
).toEqual(output);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// 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 { EncodeFunc } from './types';
|
||||
|
||||
const u8aConcat = require('@polkadot/util/u8a/concat');
|
||||
|
||||
const encodeLength = require('./length');
|
||||
|
||||
// flowlint-next-line unclear-type:off
|
||||
module.exports = function encodeArray (encoder: EncodeFunc, input: any): Uint8Array {
|
||||
const output = [];
|
||||
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
output.push(
|
||||
encoder(input[i])
|
||||
);
|
||||
}
|
||||
|
||||
const encoded = u8aConcat(output);
|
||||
|
||||
return u8aConcat([
|
||||
encodeLength(encoded.length, 192),
|
||||
encoded
|
||||
]);
|
||||
};
|
||||
@@ -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.
|
||||
// @flow
|
||||
|
||||
const encodeArray = require('./array');
|
||||
const toU8a = require('./toU8a');
|
||||
const encodeU8a = require('./u8a');
|
||||
|
||||
// flowlint-next-line unclear-type:off
|
||||
function encoder (input: any): Uint8Array {
|
||||
if (input instanceof Array) {
|
||||
return encodeArray(encoder, input);
|
||||
}
|
||||
|
||||
return encodeU8a(encoder, toU8a(input));
|
||||
}
|
||||
|
||||
module.exports = encoder;
|
||||
@@ -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.
|
||||
// @flow
|
||||
|
||||
const numberToHex = require('@polkadot/util/number/toHex');
|
||||
const hexToU8a = require('@polkadot/util/hex/toU8a');
|
||||
|
||||
module.exports = function encodeLength (length: number, offset: number): Uint8Array {
|
||||
if (length < 56) {
|
||||
return new Uint8Array([length + offset]);
|
||||
}
|
||||
|
||||
const hexLength = numberToHex(length).slice(2);
|
||||
|
||||
return hexToU8a(
|
||||
numberToHex(offset + 55 + hexLength.length / 2) + hexLength
|
||||
);
|
||||
};
|
||||
@@ -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 encodeLength = require('./length');
|
||||
|
||||
describe('encodeLength', () => {
|
||||
it('returns offset + length where <= 55', () => {
|
||||
expect(
|
||||
encodeLength(5, 6)
|
||||
).toEqual(
|
||||
new Uint8Array([11])
|
||||
);
|
||||
});
|
||||
|
||||
it('encodes > 55 length properly (short)', () => {
|
||||
expect(
|
||||
encodeLength(56, 6)
|
||||
).toEqual(
|
||||
new Uint8Array([62, 56])
|
||||
);
|
||||
});
|
||||
|
||||
it('encodes > 55 length properly (long)', () => {
|
||||
expect(
|
||||
encodeLength(512, 6)
|
||||
).toEqual(
|
||||
new Uint8Array([63, 2, 0])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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 bnToU8a = require('@polkadot/util/bn/toU8a');
|
||||
const bufferToU8a = require('@polkadot/util/buffer/toU8a');
|
||||
const hexHasPrefix = require('@polkadot/util/hex/hasPrefix');
|
||||
const hexToU8a = require('@polkadot/util/hex/toU8a');
|
||||
const isInstanceOf = require('@polkadot/util/is/instanceOf');
|
||||
const isBn = require('@polkadot/util/is/bn');
|
||||
const isBuffer = require('@polkadot/util/is/buffer');
|
||||
const isNull = require('@polkadot/util/is/null');
|
||||
const isNumber = require('@polkadot/util/is/number');
|
||||
const isString = require('@polkadot/util/is/string');
|
||||
const isUndefined = require('@polkadot/util/is/undefined');
|
||||
const numberToU8a = require('@polkadot/util/number/toU8a');
|
||||
const u8aFromUtf8 = require('@polkadot/util/u8a/fromUtf8');
|
||||
|
||||
// flowlint-next-line unclear-type:off
|
||||
module.exports = function toU8a (value: any): Uint8Array {
|
||||
if (isNull(value) || isUndefined(value)) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
if (isBuffer(value)) {
|
||||
return bufferToU8a(value);
|
||||
}
|
||||
|
||||
if (isInstanceOf(value, Uint8Array)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (isNumber(value)) {
|
||||
return numberToU8a(value);
|
||||
}
|
||||
|
||||
if (isString(value)) {
|
||||
if (hexHasPrefix(value)) {
|
||||
return hexToU8a(value);
|
||||
}
|
||||
|
||||
return u8aFromUtf8(value);
|
||||
}
|
||||
|
||||
if (isBn(value)) {
|
||||
return bnToU8a(value);
|
||||
}
|
||||
|
||||
throw new Error('invalid type');
|
||||
};
|
||||
@@ -0,0 +1,79 @@
|
||||
// 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 toU8a = require('./toU8a');
|
||||
|
||||
describe('toU8a', () => {
|
||||
it('returns a Uint8Array input as-is', () => {
|
||||
expect(
|
||||
toU8a(new Uint8Array([1, 2, 3]))
|
||||
).toEqual(
|
||||
new Uint8Array([1, 2, 3])
|
||||
);
|
||||
});
|
||||
|
||||
it('converts Buffers (uses as Uint8Array)', () => {
|
||||
expect(
|
||||
toU8a(Buffer.from([0x12, 0x34, 0x56]))
|
||||
).toEqual(
|
||||
new Uint8Array([0x12, 0x34, 0x56])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns undefined as empty', () => {
|
||||
expect(
|
||||
toU8a()
|
||||
).toEqual(
|
||||
new Uint8Array([])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns null as empty', () => {
|
||||
expect(
|
||||
toU8a(null)
|
||||
).toEqual(
|
||||
new Uint8Array([])
|
||||
);
|
||||
});
|
||||
|
||||
it('converts hex strings', () => {
|
||||
expect(
|
||||
toU8a('0x123456')
|
||||
).toEqual(
|
||||
new Uint8Array([0x12, 0x34, 0x56])
|
||||
);
|
||||
});
|
||||
|
||||
it('converts UTF-8 strings', () => {
|
||||
expect(
|
||||
toU8a('Привет, мир!')
|
||||
).toEqual(
|
||||
new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33])
|
||||
);
|
||||
});
|
||||
|
||||
it('converts numbers', () => {
|
||||
expect(
|
||||
toU8a(0x123456)
|
||||
).toEqual(
|
||||
new Uint8Array([0x12, 0x34, 0x56])
|
||||
);
|
||||
});
|
||||
|
||||
it('converts BN', () => {
|
||||
expect(
|
||||
toU8a(new BN(0x123456))
|
||||
).toEqual(
|
||||
new Uint8Array([0x12, 0x34, 0x56])
|
||||
);
|
||||
});
|
||||
|
||||
it('throws on unknown type', () => {
|
||||
expect(
|
||||
() => toU8a(true)
|
||||
).toThrow(/invalid type/);
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,5 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@summary Internal utilities used along with hash generation
|
||||
*/
|
||||
module.exports = {};
|
||||
// flowlint-next-line unclear-type:off
|
||||
export type EncodeFunc = (input: any) => Uint8Array;
|
||||
@@ -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.
|
||||
// @flow
|
||||
|
||||
import type { EncodeFunc } from './types';
|
||||
|
||||
const u8aConcat = require('@polkadot/util/u8a/concat');
|
||||
|
||||
const encodeLength = require('./length');
|
||||
|
||||
module.exports = function encodeU8a (encoder: EncodeFunc, input: Uint8Array): Uint8Array {
|
||||
if (input.length === 1 && input[0] < 128) {
|
||||
return input;
|
||||
}
|
||||
|
||||
return u8aConcat([
|
||||
encodeLength(input.length, 128),
|
||||
input
|
||||
]);
|
||||
};
|
||||
@@ -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.
|
||||
// @flow
|
||||
|
||||
const decode = require('./decode');
|
||||
const encode = require('./encode');
|
||||
|
||||
/**
|
||||
@summary Utility methods to encode and decode [Ethereum RLP](https://github.com/ethereum/wiki/wiki/RLP). Adapted from the [EthereumJS implementation](https://github.com/ethereumjs/rlp/blob/0ce09db81fc303fcee593f7cc094ba44015f9b92/index.js)
|
||||
*/
|
||||
module.exports = {
|
||||
decode,
|
||||
encode
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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 { getSingleFile } = require('ethereumjs-testing');
|
||||
|
||||
const bnToU8a = require('@polkadot/util/bn/toU8a');
|
||||
|
||||
module.exports = function getTests (file) {
|
||||
const tests = getSingleFile(file);
|
||||
|
||||
return Object.keys(tests).map((name) => ({
|
||||
name,
|
||||
input: tests[name].in[0] !== '#'
|
||||
? tests[name].in
|
||||
: bnToU8a(new BN(tests[name].in.slice(1))),
|
||||
output: `0x${tests[name].out.toLowerCase()}`
|
||||
}));
|
||||
};
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
Utility methods to create [Ethereum Trie hashes](https://github.com/ethereum/wiki/wiki/Patricia-Tree)
|
||||
|
||||
- [util](util.md) Internal utilities used along with hash generation
|
||||
|
||||
# Available methods
|
||||
|
||||
@@ -32,7 +31,7 @@ v: u8aFromString('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
|
||||
Creates a trie hash from the supplied values.
|
||||
|
||||
```js
|
||||
trieRootOrdered (pvalues: Array<Uint8Array>): Uint8Array
|
||||
trieRootOrdered (values: Array<Uint8Array>): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
# util
|
||||
|
||||
Internal utilities used along with hash generation
|
||||
|
||||
- [asNibbles](#asnibbles) Converts the input to Nibbles.
|
||||
|
||||
## asNibbles
|
||||
|
||||
Converts the input to Nibbles.
|
||||
|
||||
```js
|
||||
asNibbles (pairs: Trie$Pairs): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
From an `Uint8Array` input, calculate and return a list of nibbles that makes up the input.
|
||||
|
||||
```js
|
||||
import { asNibbles } from '@polkadot/util-triehash/util';
|
||||
|
||||
asNibbles(new Uint8Array([0x41, 0x20]) // => Uint8Array([4, 1, 2, 0])
|
||||
```
|
||||
@@ -31,7 +31,7 @@
|
||||
"dependencies": {
|
||||
"@polkadot/util": "^0.14.1",
|
||||
"@polkadot/util-crypto": "^0.14.1",
|
||||
"babel-runtime": "^6.26.0",
|
||||
"rlp": "jacogr/rlp"
|
||||
"@polkadot/util-rlp": "^0.14.1",
|
||||
"babel-runtime": "^6.26.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferToU8a = require('@polkadot/util/buffer/toU8a');
|
||||
const rlp = require('rlp');
|
||||
const rlpEncode = require('@polkadot/util-rlp/encode');
|
||||
|
||||
const asNibbles = require('./util/asNibbles');
|
||||
const genRoot = require('./util/genRoot');
|
||||
@@ -11,7 +10,7 @@ const pairsUniq = require('./util/pairsUniq');
|
||||
|
||||
/**
|
||||
@name trieRootOrdered
|
||||
@signature trieRootOrdered (pvalues: Array<Uint8Array>): Uint8Array
|
||||
@signature trieRootOrdered (values: Array<Uint8Array>): Uint8Array
|
||||
@summary Creates a trie hash from the supplied values.
|
||||
@description
|
||||
From an `Array<Uint8Array>` input, calculate the ordered triehash and return the result as a `Uint8Array`.
|
||||
@@ -29,10 +28,7 @@ module.exports = function trieRootOrdered (values: Array<Uint8Array>): Uint8Arra
|
||||
pairsUniq(
|
||||
values.map((v, index) => ({
|
||||
k: asNibbles(
|
||||
bufferToU8a(
|
||||
// $FlowFixMe rlp type definition is lacking
|
||||
rlp.encode(index)
|
||||
)
|
||||
rlpEncode(index)
|
||||
),
|
||||
v
|
||||
}))
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import type { Trie$Pairs } from '../types';
|
||||
|
||||
const keccakAsU8a = require('@polkadot/util-crypto/keccak/asU8a');
|
||||
const rlp = require('rlp');
|
||||
const rlpEncode = require('@polkadot/util-rlp/encode');
|
||||
|
||||
const encodeHexPrefix = require('./encodeHexPrefix');
|
||||
const sharedPrefixLength = require('./sharedPrefixLength');
|
||||
@@ -14,8 +14,7 @@ const sharedPrefixLength = require('./sharedPrefixLength');
|
||||
// flowlint-next-line unclear-type:off
|
||||
function encodeAux (pairs: Trie$Pairs, preLength: number): any {
|
||||
const encoded = encode(pairs, preLength);
|
||||
// flowlint-next-line unclear-type:off
|
||||
const rlped = rlp.encode((encoded: any));
|
||||
const rlped = rlpEncode(encoded);
|
||||
|
||||
if (rlped.length <= 31) {
|
||||
return encoded;
|
||||
|
||||
@@ -6,14 +6,13 @@
|
||||
import type { Trie$Pairs } from '../types';
|
||||
|
||||
const keccakAsU8a = require('@polkadot/util-crypto/keccak/asU8a');
|
||||
const rlp = require('rlp');
|
||||
const rlpEncode = require('@polkadot/util-rlp/encode');
|
||||
|
||||
const encode = require('./encode');
|
||||
|
||||
module.exports = function genRoot (pairs: Trie$Pairs): Uint8Array {
|
||||
return keccakAsU8a(
|
||||
rlp.encode(
|
||||
// $FlowFixMe rlp definition is not 100%
|
||||
rlpEncode(
|
||||
encode(pairs, 0)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -4,6 +4,7 @@ Utility methods to convert to and from `BN` objects
|
||||
|
||||
- [bnFromHex](#bnfromhex) Creates a BN.js bignumber object from a hex string.
|
||||
- [bnToHex](#bntohex) Creates a hex value from a BN.js bignumber object.
|
||||
- [bnToU8a](#bntou8a) Creates a Uint8Array object from a BN.
|
||||
|
||||
## bnFromHex
|
||||
|
||||
@@ -33,4 +34,21 @@ import BN from 'bn.js';
|
||||
import { bnToHex } from '@polkadot/util';
|
||||
|
||||
bnToHex(new BN(0x123456)); // => '0x123456'
|
||||
```
|
||||
|
||||
## bnToU8a
|
||||
|
||||
Creates a Uint8Array object from a BN.
|
||||
|
||||
```js
|
||||
bnToU8a (value?: BN): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`.
|
||||
|
||||
```js
|
||||
import { bnToU8a } from '@polkadot/util';
|
||||
|
||||
bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
|
||||
```
|
||||
@@ -6,6 +6,7 @@ Utility methods to convert to and from `number` values
|
||||
- [numberFromHex](#numberfromhex) Creates a Number value from a Buffer object.
|
||||
- [numberToBuffer](#numbertobuffer) Creates a Buffer object from a number.
|
||||
- [numberToHex](#numbertohex) Creates a hex value from a number.
|
||||
- [numberToU8a](#numbertou8a) Creates a Uint8Array object from a number.
|
||||
|
||||
## numberFromBuffer
|
||||
|
||||
@@ -63,4 +64,21 @@ numberToHex (value?: number): string
|
||||
import { numberToHex } from '@polkadot/util';
|
||||
|
||||
const hex = numberToHex(0x1234); // => '0x1234'
|
||||
```
|
||||
|
||||
## numberToU8a
|
||||
|
||||
Creates a Uint8Array object from a number.
|
||||
|
||||
```js
|
||||
numberToBuffer (value?: number): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`.
|
||||
|
||||
```js
|
||||
import { numberToU8a } from '@polkadot/util';
|
||||
|
||||
numberToU8a(0x1234); // => [0x12, 0x34]
|
||||
```
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
Utility methods to convert to and from `Uint8Array` objects
|
||||
|
||||
- [u8aConcat](#u8aconcat) Creates a concatenated Uint8Array from the inputs.
|
||||
- [u8aFromBn](#u8afrombn) Creates a Uint8Array value from a BN object.
|
||||
- [u8aFromBuffer](#u8afrombuffer) Creates a Uint8Array value from a Buffer object.
|
||||
- [u8aFromHex](#u8afromhex) Creates a Uint8Array value from a hex string.
|
||||
- [u8aFromString](#u8afromstring) Creates a Uint8Array object from a string.
|
||||
@@ -10,6 +12,38 @@ Utility methods to convert to and from `Uint8Array` objects
|
||||
- [u8aToHex](#u8atohex) Creates a hex string from a Uint8Array object.
|
||||
- [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object.
|
||||
|
||||
## u8aConcat
|
||||
|
||||
Creates a concatenated Uint8Array from the inputs.
|
||||
|
||||
```js
|
||||
u8aConcat (values: Array<Uint8Array>): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
Concatenates the input arrays into a single `UInt8Array`.
|
||||
|
||||
```js
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
u8aConcat([
|
||||
new Uint8Array([1, 2, 3]),
|
||||
new Uint8Array([4, 5, 6])
|
||||
]); // [1, 2, 3, 4, 5, 6]
|
||||
```
|
||||
|
||||
## u8aFromBn
|
||||
|
||||
Creates a Uint8Array value from a BN object. [(alias of bnToU8a)](bn.md#bntou8a)
|
||||
|
||||
```js
|
||||
u8aFromBn (value?: BN): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## u8aFromBuffer
|
||||
|
||||
Creates a Uint8Array value from a Buffer object. [(alias of bufferToU8a)](buffer.md#buffertou8a)
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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());
|
||||
};
|
||||
@@ -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([]));
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
};
|
||||
@@ -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]));
|
||||
});
|
||||
});
|
||||
@@ -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