Compact support for decoding and decoding (#256)

This commit is contained in:
Jaco Greeff
2018-11-21 15:41:19 +01:00
committed by GitHub
parent e23cadc193
commit 477be907d8
15 changed files with 316 additions and 1 deletions
@@ -0,0 +1,13 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import compactAddLength from './addLength';
describe('compactAddLength', () => {
it('correctly adds the length prefix', () => {
expect(
compactAddLength(Uint8Array.from([12, 13]))
).toEqual(Uint8Array.from([2 << 2, 12, 13]));
});
});
+28
View File
@@ -0,0 +1,28 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BitLength } from './types';
import { u8aConcat } from '../u8a';
import { DEFAULT_BITLENGTH } from './defaults';
import compactToU8a from './toU8a';
/**
* @name compactAddLength
* @description Adds a length prefix to the input value
* @example
* <BR>
*
* ```javascript
* import { compactAddLength } from '@polkadot/util';
*
* console.log(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef])
* ```
*/
export default function compactAddLength (input: Uint8Array, bitLength: BitLength = DEFAULT_BITLENGTH): Uint8Array {
return u8aConcat(
compactToU8a(input.length, bitLength),
input
);
}
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BitLength } from './types';
export const DEFAULT_BITLENGTH: BitLength = 32;
+39
View File
@@ -0,0 +1,39 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import BN from 'bn.js';
import compactFromU8a from './fromU8a';
describe('compactFromU8a', () => {
it('decoded u8 value', () => {
expect(
compactFromU8a(new Uint8Array([0b11111100]))
).toEqual([1, new BN(63)]);
});
it('decodes from same u16 encoded value', () => {
expect(
compactFromU8a(new Uint8Array([0b11111101, 0b00000111]), 32)
).toEqual([2, new BN(511)]);
});
it('decodes from same u32 encoded value (short)', () => {
expect(
compactFromU8a(new Uint8Array([254, 255, 3, 0]), 32)
).toEqual([4, new BN(0xffff)]);
});
it('decodes from same u32 encoded value (full)', () => {
expect(
compactFromU8a(new Uint8Array([3, 249, 255, 255, 255]), 32)
).toEqual([5, new BN(0xfffffff9)]);
});
it('decodes from same u32 as u64 encoded value (full, default)', () => {
expect(
compactFromU8a(new Uint8Array([3, 249, 255, 255, 255]), 64)
).toEqual([9, new BN(0xfffffff9)]);
});
});
+41
View File
@@ -0,0 +1,41 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BitLength } from './types';
import BN from 'bn.js';
import { u8aToBn, u8aToU8a } from '../u8a';
import { DEFAULT_BITLENGTH } from './defaults';
/**
* @name compactFromU8a
* @description Retrievs the offset and encoded length from a compact-prefixed value
* @example
* <BR>
*
* ```javascript
* import { compactFromU8a } from '@polkadot/util';
*
* const [offset, length] = compactFromU8a(new Uint8Array([254, 255, 3, 0]), 32));
*
* console.log('value offset=', offset, 'length=', length); // 4, 0xffff
* ```
*/
export default function compactFromU8a (_input: Uint8Array | string, bitLength: BitLength = DEFAULT_BITLENGTH): [number, BN] {
const input = u8aToU8a(_input);
const flag = input[0] & 0b11;
if (flag === 0b00) {
return [1, new BN(input[0]).shrn(2)];
} else if (flag === 0b01) {
return [2, u8aToBn(input.slice(0, 2), true).shrn(2)];
} else if (flag === 0b10) {
return [4, u8aToBn(input.slice(0, 4), true).shrn(2)];
}
const offset = 1 + (bitLength / 8);
return [offset, u8aToBn(input.subarray(1, offset), true)];
}
+18
View File
@@ -0,0 +1,18 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import compactAddLength from './addLength';
import compactStripLength from './stripLength';
import compactFromU8a from './fromU8a';
import compactToU8a from './toU8a';
/**
* @summary Encoding and decoding of parity-codec compact numbers
*/
export {
compactAddLength,
compactFromU8a,
compactStripLength,
compactToU8a
};
@@ -0,0 +1,16 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import compactStripLength from './stripLength';
describe('compactStripLength', () => {
it('correctly removes the length prefix', () => {
expect(
compactStripLength(Uint8Array.from([2 << 2, 12, 13]))
).toEqual([
3,
Uint8Array.from([12, 13])
]);
});
});
+30
View File
@@ -0,0 +1,30 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BitLength } from './types';
import { DEFAULT_BITLENGTH } from './defaults';
import compactFromU8a from './fromU8a';
/**
* @name compactStripLength
* @description Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length
* @example
* <BR>
*
* ```javascript
* import { compactStripLength } from '@polkadot/util';
*
* console.log(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
* ```
*/
export default function compactStripLength (input: Uint8Array, bitLength: BitLength = DEFAULT_BITLENGTH): [number, Uint8Array] {
const [offset, length] = compactFromU8a(input, bitLength);
const total = offset + length.toNumber();
return [
total,
input.subarray(offset, total)
];
}
+57
View File
@@ -0,0 +1,57 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import BN from 'bn.js';
import compactToU8a from './toU8a';
describe('encode', () => {
it('encodes short u8', () => {
expect(
compactToU8a(18, 8)
).toEqual(
new Uint8Array([18 << 2])
);
});
it('encodes max u8 values', () => {
expect(
compactToU8a(new BN(63), 16)
).toEqual(
new Uint8Array([0b11111100])
);
});
it('encodes basic u16 value', () => {
expect(
compactToU8a(511, 32)
).toEqual(
new Uint8Array([0b11111101, 0b00000111])
);
});
it('encodes basic u16 (not at edge)', () => {
expect(
compactToU8a(111, 32)
).toEqual(
new Uint8Array([0xbd, 0x01])
);
});
it('encodes basic u32 values (short)', () => {
expect(
compactToU8a(0xffff, 32)
).toEqual(
new Uint8Array([254, 255, 3, 0])
);
});
it('encodes basic u32 values (full)', () => {
expect(
compactToU8a(0xfffffff9)
).toEqual(
new Uint8Array([3, 249, 255, 255, 255])
);
});
});
+46
View File
@@ -0,0 +1,46 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BitLength } from './types';
import BN from 'bn.js';
import { bnToBn, bnToU8a } from '../bn';
import { u8aConcat } from '../u8a';
import { DEFAULT_BITLENGTH } from './defaults';
const MAX_U8 = new BN(2).pow(new BN(8 - 2)).subn(1);
const MAX_U16 = new BN(2).pow(new BN(16 - 2)).subn(1);
const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
/**
* @name compactToU8a
* @description Encodes a number into a compact representation
* @example
* <BR>
*
* ```javascript
* import { compactToU8a } from '@polkadot/util';
*
* console.log(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
* ```
*/
export default function compactToU8a (_value: BN | number, bitLength: BitLength = DEFAULT_BITLENGTH): Uint8Array {
const value = bnToBn(_value);
if (value.lte(MAX_U8)) {
return new Uint8Array([value.toNumber() << 2]);
} else if (value.lte(MAX_U16)) {
return bnToU8a(value.shln(2).addn(0b01), 16, true);
} else if (value.lte(MAX_U32)) {
return bnToU8a(value.shln(2).addn(0b10), 32, true);
}
return u8aConcat(
new Uint8Array([
0b11
]),
bnToU8a(value, bitLength, true)
);
}
+5
View File
@@ -0,0 +1,5 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export type BitLength = 8 | 16 | 32 | 64 | 128 | 256;