From 191abe4e08ea572257a761d8090b195123118db0 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 5 Dec 2019 12:54:52 +0100 Subject: [PATCH] Cleanup Text from U8a (wrapper class) (#1642) * Cleanup Text from U8a (wrapper class) * U8a test --- packages/types/src/primitive/Text.spec.ts | 2 +- packages/types/src/primitive/Text.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/types/src/primitive/Text.spec.ts b/packages/types/src/primitive/Text.spec.ts index b4fcdb1d22..06fb673c53 100644 --- a/packages/types/src/primitive/Text.spec.ts +++ b/packages/types/src/primitive/Text.spec.ts @@ -19,7 +19,7 @@ describe('Text', (): void => { testDecode('string', 'foo', 'foo'); testDecode('Text', new Text(registry, 'foo'), 'foo'); testDecode('Uint8Array', Uint8Array.from([12, 102, 111, 111]), 'foo'); - testDecode('U8a', new U8a(registry, Uint8Array.from([12, 102, 111, 111])), 'foo'); + testDecode('U8a', new U8a(registry, Uint8Array.from([102, 111, 111])), 'foo'); // no length testDecode('object with `toString()`', { toString (): string { return 'foo'; } }, 'foo'); testDecode('hex input value', new Text(registry, '0x12345678'), '0x12345678', 'toHex'); }); diff --git a/packages/types/src/primitive/Text.ts b/packages/types/src/primitive/Text.ts index bd077845d9..f0c394371a 100644 --- a/packages/types/src/primitive/Text.ts +++ b/packages/types/src/primitive/Text.ts @@ -9,6 +9,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto'; import { createType } from '../codec/create'; import Compact from '../codec/Compact'; +import U8a from '../codec/U8a'; /** * @name Text @@ -37,6 +38,12 @@ export default class Text extends String implements Codec { return ''; } + // for U8a, the internal buffer does not have an internal length + // (the same applies in e.g. Bytes, where length is added at encoding-time) + if (value instanceof U8a) { + return u8aToString(value); + } + const [offset, length] = Compact.decodeU8a(value); const total = offset + length.toNumber();