Additional MultiAddress testing paths (#3205)

* Additional MultiAddress testing paths

* Update packages/types/src/generic/MultiAddress.ts
This commit is contained in:
Jaco Greeff
2021-02-23 15:25:48 +01:00
committed by GitHub
parent e45ff6d026
commit dcccab705c
2 changed files with 29 additions and 13 deletions
@@ -20,6 +20,20 @@ describe('MultiAddress', (): void => {
expect(a.toString()).toEqual('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'); expect(a.toString()).toEqual('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF');
}); });
it('correctly decodes an AccountIndex', (): void => {
const a = registry.createType('MultiAddress', '25GUyv');
expect(a.index).toEqual(1);
expect(a.toString()).toEqual('25GUyv');
});
it('correctly decodes an AccountIndex (AccountIndex input)', (): void => {
const a = registry.createType('MultiAddress', registry.createType('AccountIndex', '25GUyv'));
expect(a.index).toEqual(1);
expect(a.toString()).toEqual('25GUyv');
});
it('correctly decodes an Address20', (): void => { it('correctly decodes an Address20', (): void => {
const a = registry.createType('MultiAddress', '0x67f89207abe6e1b093befd84a48f033137659292'); const a = registry.createType('MultiAddress', '0x67f89207abe6e1b093befd84a48f033137659292');
+15 -13
View File
@@ -3,23 +3,25 @@
import type { Registry } from '../types'; import type { Registry } from '../types';
import { isBn, isNumber, isString, isU8a } from '@polkadot/util'; import { isBn, isNumber, isString } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto'; import { decodeAddress } from '@polkadot/util-crypto';
import { Enum } from '../codec/Enum'; import { Enum } from '../codec/Enum';
import { GenericAccountId } from './AccountId'; import { GenericAccountId } from './AccountId';
import { GenericAccountIndex } from './AccountIndex'; import { GenericAccountIndex } from './AccountIndex';
function decodeMultiU8a (value?: unknown): unknown { function decodeString (registry: Registry, value: string): unknown {
if (isU8a(value) && value.length <= 32) { const u8a = decodeAddress(value.toString());
if (value.length === 32) {
return { id: value }; if (u8a.length === 32) {
} else if (value.length === 20) { return { Id: u8a };
return { Address20: value }; } else if (u8a.length === 20) {
} return { Address20: u8a };
} else if (u8a.length <= 8) {
return { Index: registry.createType('AccountIndex', u8a).toNumber() };
} }
return value; return u8a;
} }
function decodeMultiAny (registry: Registry, value?: unknown): unknown { function decodeMultiAny (registry: Registry, value?: unknown): unknown {
@@ -27,13 +29,13 @@ function decodeMultiAny (registry: Registry, value?: unknown): unknown {
return value; return value;
} else if (value instanceof GenericAccountId) { } else if (value instanceof GenericAccountId) {
return { Id: value }; return { Id: value };
} else if (value instanceof GenericAccountIndex || isNumber(value) || isBn(value)) { } else if (value instanceof GenericAccountIndex || isBn(value) || isNumber(value)) {
return { Index: registry.createType('Compact<AccountIndex>', value) }; return { Index: isNumber(value) ? value : value.toNumber() };
} else if (isString(value)) { } else if (isString(value)) {
return decodeMultiU8a(decodeAddress(value.toString())); return decodeString(registry, value.toString());
} }
return decodeMultiU8a(value); return value;
} }
export class GenericMultiAddress extends Enum { export class GenericMultiAddress extends Enum {