From 42cdb8607a476d859a7a9605bac945b4006a0ee5 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 11 Jan 2019 15:36:12 +0100 Subject: [PATCH 1/3] Construct Bytes via Array (no prefix) --- packages/types/src/Bytes.spec.ts | 6 ++++++ packages/types/src/Bytes.ts | 6 ++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/types/src/Bytes.spec.ts b/packages/types/src/Bytes.spec.ts index 4f44e00c01cc..651b7435729f 100644 --- a/packages/types/src/Bytes.spec.ts +++ b/packages/types/src/Bytes.spec.ts @@ -15,6 +15,12 @@ describe('Bytes', () => { ).toEqual(CODE); }); + it('decodes from Array', () => { + expect( + new Bytes([0x3a, 0x63, 0x6f, 0x64, 0x65]) + ).toEqual(CODE); + }); + it('creates via storagedata (no prefix)', () => { expect( new Bytes( diff --git a/packages/types/src/Bytes.ts b/packages/types/src/Bytes.ts index b79f8ceeeaa7..f6f77fa5cb4c 100644 --- a/packages/types/src/Bytes.ts +++ b/packages/types/src/Bytes.ts @@ -26,10 +26,10 @@ export default class Bytes extends U8a { // StorageData to cater for the _specific_ problematic case const StorageData = require('./StorageData').default; - if (isHex(value)) { + if (Array.isArray(value) || isString(value)) { // FIXME We manually add the length prefix for hex for now // https://github.com/paritytech/substrate/issues/889 - const u8a = hexToU8a(value); + const u8a = u8aToU8a(value); return Bytes.decodeBytes( Compact.addLengthPrefix(u8a) @@ -51,8 +51,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; From b92607ab766cde0231010ecb76558d1f7669fb65 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 11 Jan 2019 15:37:28 +0100 Subject: [PATCH 2/3] Remove unused imports --- packages/types/src/Bytes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/Bytes.ts b/packages/types/src/Bytes.ts index f6f77fa5cb4c..fb6509764fa6 100644 --- a/packages/types/src/Bytes.ts +++ b/packages/types/src/Bytes.ts @@ -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'; From af327c46cd4ce9f09893b92c9db277743a8bce79 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 11 Jan 2019 16:19:36 +0100 Subject: [PATCH 3/3] Allow extrinsics with/without prefix --- packages/types/src/Bytes.spec.ts | 2 +- packages/types/src/Bytes.ts | 2 -- packages/types/src/Extrinsic.spec.ts | 22 ++++++++++++++++++++++ packages/types/src/Extrinsic.ts | 17 +++++++++++------ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/types/src/Bytes.spec.ts b/packages/types/src/Bytes.spec.ts index 651b7435729f..d74bbc6a90c0 100644 --- a/packages/types/src/Bytes.spec.ts +++ b/packages/types/src/Bytes.spec.ts @@ -17,7 +17,7 @@ describe('Bytes', () => { it('decodes from Array', () => { expect( - new Bytes([0x3a, 0x63, 0x6f, 0x64, 0x65]) + new Bytes([0x3a, 0x63, 0x6f, 0x64, 0x65]).toU8a() ).toEqual(CODE); }); diff --git a/packages/types/src/Bytes.ts b/packages/types/src/Bytes.ts index fb6509764fa6..99c3bc1a3eee 100644 --- a/packages/types/src/Bytes.ts +++ b/packages/types/src/Bytes.ts @@ -27,8 +27,6 @@ export default class Bytes extends U8a { const StorageData = require('./StorageData').default; if (Array.isArray(value) || isString(value)) { - // FIXME We manually add the length prefix for hex for now - // https://github.com/paritytech/substrate/issues/889 const u8a = u8aToU8a(value); return Bytes.decodeBytes( diff --git a/packages/types/src/Extrinsic.spec.ts b/packages/types/src/Extrinsic.spec.ts index ada99676e97c..bb1557312adc 100644 --- a/packages/types/src/Extrinsic.spec.ts +++ b/packages/types/src/Extrinsic.spec.ts @@ -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'); + }); }); diff --git a/packages/types/src/Extrinsic.ts b/packages/types/src/Extrinsic.ts index 7e20672272cf..14c2f9426e10 100644 --- a/packages/types/src/Extrinsic.ts +++ b/packages/types/src/Extrinsic.ts @@ -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 | 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);