diff --git a/packages/types/src/create/createType.spec.ts b/packages/types/src/create/createType.spec.ts index 8e5be72e85..3757fd65b6 100644 --- a/packages/types/src/create/createType.spec.ts +++ b/packages/types/src/create/createType.spec.ts @@ -122,6 +122,26 @@ describe('createType', (): void => { ).toEqual(new Uint8Array([0x00, 0x12, 0x00, 0x23, 0x00, 0x45, 0x00, 0x67])); }); + describe('isPedantic', (): void => { + it('correctly decodes Bytes', (): void => { + expect( + createTypeUnsafe(registry, 'Bytes', ['0x12345678'], { isPedantic: true }) + ).toBeDefined(); + }); + + it('correctly decodes Bytes (prefixed)', (): void => { + expect( + createTypeUnsafe(registry, 'Bytes', [new Uint8Array([4 << 2, 1, 2, 3, 4])], { isPedantic: true }) + ).toBeDefined(); + }); + + it('correctly decodes Text', (): void => { + expect( + createTypeUnsafe(registry, 'Text', ['0x2070726f7669646572'], { isPedantic: true }) + ).toBeDefined(); + }); + }); + describe('instanceof', (): void => { it('instanceof should work (primitive type)', (): void => { const value = registry.createType('Balance', 1234); diff --git a/packages/types/src/create/createType.ts b/packages/types/src/create/createType.ts index 0eb0b8c328..633b41f79c 100644 --- a/packages/types/src/create/createType.ts +++ b/packages/types/src/create/createType.ts @@ -1,6 +1,7 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { Bytes } from '../primitive/Bytes'; import type { Codec, Constructor, InterfaceTypes, Registry } from '../types'; import type { FromReg } from './types'; @@ -19,8 +20,15 @@ interface CreateOptions { function checkInstance (value: Uint8Array, created: FromReg): void { const u8a = created.toU8a(); const rawType = created.toRawType(); - const isEqual = rawType === 'Bytes' - ? value.length === u8a.length + + // for length-prefixed byte types, just check the length, else the full encoding + const isEqual = ['Bytes', 'Text'].includes(rawType) + ? [ + // input was via hex, we don't have a prefix + (created as Bytes).length, + // input was raw bytes, we do have a prefix + u8a.length + ].includes(value.length) : u8aEq(value, u8a); assert(isEqual, () => `${rawType}:: Decoded input doesn't match input, received ${u8aToHex(value, 512)} (${value.length} bytes), created ${u8aToHex(u8a, 512)} (${u8a.length} bytes)`);