Fix decoding with eras (#293)

* Fix decoding with eras

* Typo
This commit is contained in:
Jaco Greeff
2018-10-15 11:48:14 +02:00
committed by GitHub
parent d449189b57
commit 9644daf5eb
8 changed files with 50 additions and 16 deletions
+1 -3
View File
@@ -60,13 +60,11 @@ export default class AccountIndex extends U8a {
static writeLength (input: Uint8Array): Uint8Array {
switch (input.length) {
case 1: return new Uint8Array([]);
case 2: return new Uint8Array([0xfc]);
case 4: return new Uint8Array([0xfd]);
case 8: return new Uint8Array([0xfe]);
default: return new Uint8Array([]);
}
throw new Error(`Invalid account index length, ${input.length}`);
}
fromJSON (input: any): AccountIndex {
+5
View File
@@ -5,6 +5,7 @@
import isHex from '@polkadot/util/is/hex';
import isU8a from '@polkadot/util/is/u8a';
import u8aConcat from '@polkadot/util/u8a/concat';
import u8aToHex from '@polkadot/util/u8a/toHex';
import u8aToU8a from '@polkadot/util/u8a/toU8a';
import AccountId from './AccountId';
@@ -73,6 +74,10 @@ export default class Address extends Base<AccountId | AccountIndex> {
return this;
}
toHex (): string {
return u8aToHex(this.toU8a());
}
toJSON (): any {
return this.raw.toJSON();
}
+14
View File
@@ -19,4 +19,18 @@ describe('Extrinsic', () => {
expect(extrinsic.callIndex).toEqual(new Uint8Array([2, 0]));
expect(extrinsic.data).toEqual(new Uint8Array([234, 81, 183, 91, 0, 0, 0, 0]));
});
it('decodes an actual transaction with indexes', () => {
const extrinsic = new Extrinsic('0x8110f8e1ebdd3cdef7423d24fe68f3863945ea21c190907d7f3394ddf153f633c77b894af36f7a36e7ec767f4593bfd8d084b66d805605905054327a08ff604187080100000000000000270e0100ff0e6422725af0e9aede3bef6eba77bc87afadf60b9735057ae93801f4c472ad7b00407a10f35a00000000000000000000');
console.error('signature', extrinsic.args);
expect(extrinsic.isSigned).toEqual(true);
expect(extrinsic.signature.signer.toU8a()).toEqual(new Uint8Array([0x10]));
expect(extrinsic.signature.signature.toHex()).toEqual('0xf8e1ebdd3cdef7423d24fe68f3863945ea21c190907d7f3394ddf153f633c77b894af36f7a36e7ec767f4593bfd8d084b66d805605905054327a08ff60418708');
expect(extrinsic.signature.nonce.toNumber()).toEqual(1);
expect(extrinsic.signature.era.toU8a()).toEqual(new Uint8Array([0x27, 0x0e]));
expect(extrinsic.callIndex).toEqual(new Uint8Array([1, 0]));
expect(extrinsic.method.args[0].toHex()).toEqual('0xff0e6422725af0e9aede3bef6eba77bc87afadf60b9735057ae93801f4c472ad7b');
});
});
+3
View File
@@ -53,6 +53,7 @@ export default class Extrinsic extends Struct {
// Instead of the block below, it should simply be:
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
const u8a = hexToU8a(value as string);
return Extrinsic.decodeExtrinsic(
u8aConcat(
Compact.encodeU8a(u8a.length, DEFAULT_LENGTH_BITS),
@@ -61,8 +62,10 @@ export default class Extrinsic extends Struct {
);
} else if (isU8a(value)) {
const [offset, length] = Compact.decodeU8a(value, DEFAULT_LENGTH_BITS);
return value.subarray(offset, offset + length.toNumber());
}
return value as any;
}
+20 -4
View File
@@ -4,13 +4,29 @@
import { AnyU8a } from './types';
import toU8a from '@polkadot/util/u8a/toU8a';
import U8a from './codec/U8a';
import U8aFixed from './codec/U8aFixed';
// Description of a transaction era
// FIXME should be a enum?
export default class ExtrinsicEra extends U8aFixed {
constructor (value?: AnyU8a) {
// FIXME this actually indicates an Immortal (i.e. 1 byte)
super(value, 8);
const decoded = ExtrinsicEra.decodeExtrinsicEra(value);
super(decoded, (decoded.length * 8) as any);
}
static decodeExtrinsicEra (value?: AnyU8a): Uint8Array {
if (value instanceof U8a) {
return value.raw;
} else if (value) {
const u8a = toU8a(value);
// If we have a zero byte, it is immortal (1 byte in length), otherwise we have
// the era details following as another byte
return u8a.subarray(0, (u8a[0] === 0) ? 1 : 2);
}
return new Uint8Array([0]);
}
}
+1 -1
View File
@@ -33,7 +33,7 @@ const BIT_VERSION = 0b0000001;
// 1/3/5/9/33 bytes: The signing account identity, in Address format
// 64 bytes: The Ed25519 signature of the Signing Payload
// 8 bytes: The Transaction Index of the signing account
// 2 bytes: The Transaction Era
// 1/2 bytes: The Transaction Era
export default class ExtrinsicSignature extends Struct {
constructor (value?: ExtrinsicSignatureValue) {
super({
+5 -7
View File
@@ -32,21 +32,21 @@ export default class UInt extends Base<BN> {
constructor (value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isHexJson: boolean = true) {
super(
UInt.decodeUInt(value)
UInt.decodeUInt(value, bitLength)
);
this._bitLength = bitLength;
this._isHexJson = isHexJson;
}
static decodeUInt (value: AnyNumber): BN {
static decodeUInt (value: AnyNumber, bitLength: UIntBitLength): BN {
if (value instanceof UInt) {
return value.raw;
} else if (isHex(value)) {
return hexToBn(value as string);
} else if (isU8a(value)) {
// NOTE When passing u8a in (typically from decoded data), it is always u8a
return u8aToBn(value, true);
return u8aToBn(value.subarray(0, bitLength / 8), true);
} else if (isString(value)) {
return new BN(value, 10);
}
@@ -59,15 +59,13 @@ export default class UInt extends Base<BN> {
}
fromJSON (input: any): UInt {
this.raw = UInt.decodeUInt(input);
this.raw = UInt.decodeUInt(input, this._bitLength);
return this;
}
fromU8a (input: Uint8Array): UInt {
this.raw = u8aToBn(input.subarray(0, this.byteLength()), true);
return this;
return this.fromJSON(input);
}
toHex (): string {