Some non-functionality readability cleanups (#1295)

* Some non-functionality readability cleanups

* Small Address cleanups
This commit is contained in:
Jaco Greeff
2019-08-11 21:45:16 +02:00
committed by GitHub
parent ddc5f40a27
commit 07b89e759c
7 changed files with 79 additions and 74 deletions
+1 -4
View File
@@ -54,12 +54,9 @@ export function typeSplit (type: string): string[] {
case '{': sDepth++; break;
case '}': sDepth--; break;
// adjusttuple depth
// adjust tuple depth
case '(': tDepth++; break;
case ')': tDepth--; break;
// normal character
default: break;
}
}
@@ -57,24 +57,11 @@ export class MortalEra extends Tuple {
private static decodeMortalEra (value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue {
if (isHex(value)) {
return MortalEra.decodeMortalEra(hexToU8a(value));
return MortalEra.decodeMortalU8a(hexToU8a(value));
} else if (Array.isArray(value)) {
return MortalEra.decodeMortalEra(new Uint8Array(value));
return MortalEra.decodeMortalU8a(new Uint8Array(value));
} else if (isU8a(value)) {
if (value.length === 0) {
return [new U64(), new U64()];
}
const first = u8aToBn(value.subarray(0, 1)).toNumber();
const second = u8aToBn(value.subarray(1, 2)).toNumber();
const encoded: number = first + (second << 8);
const period = 2 << (encoded % (1 << 4));
const quantizeFactor = Math.max(period >> 12, 1);
const phase = (encoded >> 4) * quantizeFactor;
assert(period >= 4 && phase < period, 'Invalid data passed to Mortal era');
return [new U64(period), new U64(phase)];
return MortalEra.decodeMortalU8a(value);
} else if (isObject(value)) {
const { current, period } = value;
let calPeriod = Math.pow(2, Math.ceil(Math.log2(period)));
@@ -91,6 +78,23 @@ export class MortalEra extends Tuple {
throw new Error('Invalid data passed to Mortal era');
}
private static decodeMortalU8a (value: Uint8Array): MortalEraValue {
if (value.length === 0) {
return [new U64(), new U64()];
}
const first = u8aToBn(value.subarray(0, 1)).toNumber();
const second = u8aToBn(value.subarray(1, 2)).toNumber();
const encoded: number = first + (second << 8);
const period = 2 << (encoded % (1 << 4));
const quantizeFactor = Math.max(period >> 12, 1);
const phase = (encoded >> 4) * quantizeFactor;
assert(period >= 4 && phase < period, 'Invalid data passed to Mortal era');
return [new U64(period), new U64(phase)];
}
/**
* @description Encoded length for mortals occupy 2 bytes, different from the actual Tuple since it is encoded. This is a shortcut fro `toU8a().length`
*/
@@ -199,11 +203,9 @@ export default class ExtrinsicEra extends Enum implements IExtrinsicEra {
} else if (isHex(value)) {
return ExtrinsicEra.decodeExtrinsicEra(hexToU8a(value));
} else if (isU8a(value)) {
if (!value.length || value[0] === 0) {
return new Uint8Array([0]);
} else {
return new Uint8Array([1, value[0], value[1]]);
}
return (!value.length || value[0] === 0)
? new Uint8Array([0])
: new Uint8Array([1, value[0], value[1]]);
} else if (isObject(value)) {
// this is to de-serialize from JSON
if ((value as MortalEnumDef).MortalEra) {
+17 -17
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { hexToU8a, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import createType from '../../codec/createType';
@@ -37,22 +37,8 @@ export default class Address extends Base<AccountId | AccountIndex> {
return createType('AccountIndex', value);
} else if (value instanceof Address) {
return value.raw;
} else if (Array.isArray(value)) {
return Address.decodeAddress(u8aToU8a(value));
} else if (isU8a(value)) {
// This allows us to instantiate an address with a raw publicKey. Do this first before
// we checking the first byte, otherwise we may split an already-existent valid address
if (value.length === 32) {
return createType('AccountId', value);
} else if (value[0] === 0xff) {
return createType('AccountId', value.subarray(1));
}
const [offset, length] = AccountIndex.readLength(value);
return createType('AccountIndex', u8aToBn(value.subarray(offset, offset + length), true));
} else if (isHex(value)) {
return Address.decodeAddress(hexToU8a(value));
} else if (Array.isArray(value) || isHex(value) || isU8a(value)) {
return Address.decodeU8a(u8aToU8a(value));
}
const decoded = decodeAddress(value);
@@ -62,6 +48,20 @@ export default class Address extends Base<AccountId | AccountIndex> {
: createType('AccountIndex', u8aToBn(decoded, true));
}
private static decodeU8a (value: Uint8Array): AccountId | AccountIndex {
// This allows us to instantiate an address with a raw publicKey. Do this first before
// we checking the first byte, otherwise we may split an already-existent valid address
if (value.length === 32) {
return createType('AccountId', value);
} else if (value[0] === 0xff) {
return createType('AccountId', value.subarray(1));
}
const [offset, length] = AccountIndex.readLength(value);
return createType('AccountIndex', u8aToBn(value.subarray(offset, offset + length), true));
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
+1 -3
View File
@@ -64,9 +64,7 @@ export default class StorageKey extends Bytes {
};
} else if (!value || isString(value) || isU8a(value)) {
// let Bytes handle these inputs
return {
key: value
};
return { key: value };
} else if (isFunction(value)) {
return {
key: value(),