From 71703e0ce140d68234ab46430a61850a66899654 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sat, 26 Aug 2023 11:12:02 +0300 Subject: [PATCH] Adjust Option toHex conversion --- packages/types-codec/src/base/Option.spec.ts | 62 ++++++++++++++++---- packages/types-codec/src/base/Option.ts | 4 +- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/packages/types-codec/src/base/Option.spec.ts b/packages/types-codec/src/base/Option.spec.ts index 880431c4a731..a7d0b05717c5 100644 --- a/packages/types-codec/src/base/Option.spec.ts +++ b/packages/types-codec/src/base/Option.spec.ts @@ -93,17 +93,6 @@ describe('Option', (): void => { ).toEqual('hello'); }); - it('converts correctly from hex with toHex (Bytes)', (): void => { - // Option for a parachain head, however, this is effectively an - // Option> (hence the length, since it is from storage) - const HEX = '0x210100000000000000000000000000000000000000000000000000000000000000000000000000000000011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce'; - - // watch the hex prefix and length - expect( - new Option(registry, Bytes, HEX).toHex().substring(6) - ).toEqual(HEX.substring(2)); - }); - it('converts correctly from hex with toNumber (U64)', (): void => { const HEX = '0x12345678'; @@ -124,8 +113,6 @@ describe('Option', (): void => { testDecode('string (without)', undefined, ''); testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo'); testDecode('Uint8Array (without)', Uint8Array.from([0]), ''); - - testEncode('toHex', '0x0c666f6f'); testEncode('toString', 'foo'); testEncode('toU8a', Uint8Array.from([1, 12, 102, 111, 111])); @@ -177,6 +164,55 @@ describe('Option', (): void => { ).toEqual('abcde'); }); + describe('toHex()', (): void => { + it('converts Option correctly', (): void => { + expect( + new Option(registry, U32, 0x1234).toHex() + ).toEqual('0x00001234'); + }); + + it('converts Option> correctly', (): void => { + expect( + new Option(registry, Option.with(U32), 0x1234).toHex() + ).toEqual('0x00001234'); + }); + + it('constructs from hex Option> correctly', (): void => { + expect( + new Option(registry, Option.with(U32), '0x00001234').toHex() + ).toEqual('0x00001234'); + }); + + it('converts Option correctly', (): void => { + expect( + new Option(registry, Bytes, 'abcde').toHex() + ).toEqual('0x6162636465'); + }); + + it('converts Option> correctly', (): void => { + expect( + new Option(registry, Option.with(Bytes), 'abcde').toHex() + ).toEqual('0x6162636465'); + }); + + it('constructs from hex Option> correctly', (): void => { + expect( + new Option(registry, Option.with(Bytes), '0x6162636465').toHex() + ).toEqual('0x6162636465'); + }); + + it('converts correctly from hex with toHex (Bytes)', (): void => { + // Option for a parachain head, however, this is effectively an + // Option> (hence the length, since it is from storage) + const HEX = '0x210100000000000000000000000000000000000000000000000000000000000000000000000000000000011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce'; + + // watch the hex prefix and length + expect( + new Option(registry, Bytes, HEX).toHex() + ).toEqual(HEX); + }); + }); + describe('utils', (): void => { const test = new Option(registry, Text, '1234'); diff --git a/packages/types-codec/src/base/Option.ts b/packages/types-codec/src/base/Option.ts index 86d273a4be12..b6a22e64c912 100644 --- a/packages/types-codec/src/base/Option.ts +++ b/packages/types-codec/src/base/Option.ts @@ -4,7 +4,7 @@ import type { HexString } from '@polkadot/util/types'; import type { AnyJson, Codec, CodecClass, DefinitionSetter, Inspect, IOption, IU8a, Registry } from '../types/index.js'; -import { identity, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util'; +import { identity, isCodec, isNull, isU8a, isUndefined } from '@polkadot/util'; import { typeToConstructor } from '../utils/index.js'; import { Null } from './Null.js'; @@ -177,7 +177,7 @@ export class Option implements IOption { // the isSome value is correct, however the `isNone` may be problematic return this.isNone ? '0x' - : u8aToHex(this.toU8a().subarray(1)); + : this.#raw.toHex(); } /**