Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/types/src/Bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 3 additions & 7 deletions packages/types/src/Bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)
Expand All @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions packages/types/src/Extrinsic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
});
});
17 changes: 11 additions & 6 deletions packages/types/src/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down