From 35c37aa66c915b0ac258576a6381cc9c05156c72 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 26 Aug 2020 12:47:15 +0200 Subject: [PATCH] Allow AccountId null in constructor --- packages/types/src/generic/AccountId.spec.ts | 6 ++++++ packages/types/src/generic/AccountId.ts | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/types/src/generic/AccountId.spec.ts b/packages/types/src/generic/AccountId.spec.ts index c87107f4b8..164efab471 100644 --- a/packages/types/src/generic/AccountId.spec.ts +++ b/packages/types/src/generic/AccountId.spec.ts @@ -26,6 +26,12 @@ describe('AccountId', (): void => { it('equals the empty address', (): void => { expect(id.eq('5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM')).toBe(true); }); + + it('allows decoding from null', (): void => { + expect( + registry.createType('AccountId', null).eq('5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM') + ).toBe(true); + }); }); describe('decoding', (): void => { diff --git a/packages/types/src/generic/AccountId.ts b/packages/types/src/generic/AccountId.ts index 6ccaea8496..29baf91dab 100644 --- a/packages/types/src/generic/AccountId.ts +++ b/packages/types/src/generic/AccountId.ts @@ -10,8 +10,10 @@ import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; import U8aFixed from '../codec/U8aFixed'; /** @internal */ -function decodeAccountId (value: AnyU8a | AnyString): Uint8Array { - if (isU8a(value) || Array.isArray(value)) { +function decodeAccountId (value?: AnyU8a | AnyString): Uint8Array { + if (!value) { + return new Uint8Array(); + } else if (isU8a(value) || Array.isArray(value)) { return u8aToU8a(value); } else if (isHex(value)) { return hexToU8a(value.toString()); @@ -30,7 +32,7 @@ function decodeAccountId (value: AnyU8a | AnyString): Uint8Array { * just a Uint8Array wrapper with a fixed length. */ export default class AccountId extends U8aFixed { - constructor (registry: Registry, value: AnyU8a = new Uint8Array()) { + constructor (registry: Registry, value?: AnyU8a) { const decoded = decodeAccountId(value); // Part of stream containing >= 32 bytes or a all empty (defaults)