Additional MultiAddress testing paths (#3205)
* Additional MultiAddress testing paths * Update packages/types/src/generic/MultiAddress.ts
This commit is contained in:
@@ -20,6 +20,20 @@ describe('MultiAddress', (): void => {
|
||||
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 => {
|
||||
const a = registry.createType('MultiAddress', '0x67f89207abe6e1b093befd84a48f033137659292');
|
||||
|
||||
|
||||
@@ -3,23 +3,25 @@
|
||||
|
||||
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 { Enum } from '../codec/Enum';
|
||||
import { GenericAccountId } from './AccountId';
|
||||
import { GenericAccountIndex } from './AccountIndex';
|
||||
|
||||
function decodeMultiU8a (value?: unknown): unknown {
|
||||
if (isU8a(value) && value.length <= 32) {
|
||||
if (value.length === 32) {
|
||||
return { id: value };
|
||||
} else if (value.length === 20) {
|
||||
return { Address20: value };
|
||||
}
|
||||
function decodeString (registry: Registry, value: string): unknown {
|
||||
const u8a = decodeAddress(value.toString());
|
||||
|
||||
if (u8a.length === 32) {
|
||||
return { Id: u8a };
|
||||
} 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 {
|
||||
@@ -27,13 +29,13 @@ function decodeMultiAny (registry: Registry, value?: unknown): unknown {
|
||||
return value;
|
||||
} else if (value instanceof GenericAccountId) {
|
||||
return { Id: value };
|
||||
} else if (value instanceof GenericAccountIndex || isNumber(value) || isBn(value)) {
|
||||
return { Index: registry.createType('Compact<AccountIndex>', value) };
|
||||
} else if (value instanceof GenericAccountIndex || isBn(value) || isNumber(value)) {
|
||||
return { Index: isNumber(value) ? value : value.toNumber() };
|
||||
} else if (isString(value)) {
|
||||
return decodeMultiU8a(decodeAddress(value.toString()));
|
||||
return decodeString(registry, value.toString());
|
||||
}
|
||||
|
||||
return decodeMultiU8a(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
export class GenericMultiAddress extends Enum {
|
||||
|
||||
Reference in New Issue
Block a user