Construct Bytes via Array<number> (no prefix) (#573)

* Construct Bytes via Array<number> (no prefix)

* Remove unused imports

* Allow extrinsics with/without prefix
This commit is contained in:
Jaco Greeff
2019-01-11 16:59:34 +01:00
committed by GitHub
parent 532dbe637a
commit a2f176dca7
4 changed files with 42 additions and 13 deletions
+6
View File
@@ -15,6 +15,12 @@ describe('Bytes', () => {
).toEqual(CODE);
});
it('decodes from Array<number>', () => {
expect(
new Bytes([0x3a, 0x63, 0x6f, 0x64, 0x65]).toU8a()
).toEqual(CODE);
});
it('creates via storagedata (no prefix)', () => {
expect(
new Bytes(
+3 -7
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util';
import { isString, isU8a, u8aToU8a } from '@polkadot/util';
import { AnyU8a } from './types';
import Compact from './codec/Compact';
@@ -26,10 +26,8 @@ export default class Bytes extends U8a {
// StorageData to cater for the _specific_ problematic case
const StorageData = require('./StorageData').default;
if (isHex(value)) {
// FIXME We manually add the length prefix for hex for now
// https://github.com/paritytech/substrate/issues/889
const u8a = hexToU8a(value);
if (Array.isArray(value) || isString(value)) {
const u8a = u8aToU8a(value);
return Bytes.decodeBytes(
Compact.addLengthPrefix(u8a)
@@ -51,8 +49,6 @@ export default class Bytes extends U8a {
const [offset, length] = Compact.decodeU8a(value);
return value.subarray(offset, offset + length.toNumber());
} else if (Array.isArray(value) || isString(value)) {
return Bytes.decodeBytes(u8aToU8a(value));
}
return value;
+22
View File
@@ -3,7 +3,9 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import extrinsics from '@polkadot/extrinsics/static';
import { hexToU8a, u8aToHex } from '@polkadot/util';
import Compact from './codec/Compact';
import Extrinsic from './Extrinsic';
import Method from './Method';
@@ -31,4 +33,24 @@ describe('Extrinsic', () => {
expect(extrinsic.callIndex).toEqual(new Uint8Array([2, 0]));
expect(extrinsic.args[0].toString()).toEqual('5CPaGq4KcmntaboAxg5nyqGXdyzaBV2hj6PvhNA3syigiRg8');
});
it('decodes an actual transaction with indexes (new format)', () => {
const extrinsic = new Extrinsic(
u8aToHex(
Compact.addLengthPrefix(
hexToU8a(
'0x8110f8e1ebdd3cdef7423d24fe68f3863945ea21c190907d7f3394ddf153f633c77b894af36f7a36e7ec767f4593bfd8d084b66d805605905054327a08ff6041870804270e0200ff0e6422725af0e9aede3bef6eba77bc87afadf60b9735057ae93801f4c472ad7b00407a10f35a00000000000000000000'
)
)
)
);
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([2, 0]));
expect(extrinsic.args[0].toString()).toEqual('5CPaGq4KcmntaboAxg5nyqGXdyzaBV2hj6PvhNA3syigiRg8');
});
});
+11 -6
View File
@@ -5,7 +5,7 @@
import { KeyringPair } from '@polkadot/keyring/types';
import { AnyNumber, AnyU8a, Codec } from './types';
import { hexToU8a, isHex, isU8a, u8aToHex } from '@polkadot/util';
import { isHex, isU8a, u8aToHex, u8aToU8a } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Compact from './codec/Compact';
@@ -42,15 +42,20 @@ export default class Extrinsic extends Struct {
}
static decodeExtrinsic (value: ExtrinsicValue | AnyU8a | Method): ExtrinsicValue | Array<number> | Uint8Array {
if (isHex(value)) {
// FIXME We manually add the length prefix for hex for now
// https://github.com/paritytech/substrate/issues/889
if (Array.isArray(value) || isHex(value)) {
// Instead of the block below, it should simply be:
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
const u8a = hexToU8a(value);
const u8a = u8aToU8a(value);
// HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388
// extrinsics didn't have the length, cater for both approaches
const [offset, length] = Compact.decodeU8a(u8a);
const withPrefix = u8a.length === (offset + length.toNumber());
return Extrinsic.decodeExtrinsic(
Compact.addLengthPrefix(u8a)
withPrefix
? u8a
: Compact.addLengthPrefix(u8a)
);
} else if (isU8a(value)) {
const [offset, length] = Compact.decodeU8a(value);