Fix MultiAddress decoding from raw publicKey (#3215)

* Fix Multiddress decoding from raw publicKey

* Full decoding on empty

* Update
This commit is contained in:
Jaco Greeff
2021-02-25 12:14:00 +01:00
committed by GitHub
parent b50eab11c0
commit 7603f9e6c1
3 changed files with 14 additions and 7 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ describe('Call', (): void => {
it('handles creation from a hex value properly', (): void => {
expect(
new Call(registry, '0x0601').toU8a()
new Call(registry, '0x06010000000000000000000000000000000000000000000000000000000000000000').toU8a()
).toEqual(
new Uint8Array([6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
); // balances.setBalance
@@ -6,6 +6,13 @@ import { TypeRegistry } from '../create';
describe('MultiAddress', (): void => {
const registry = new TypeRegistry();
it('decodes from an empty value', (): void => {
const a = registry.createType('MultiAddress');
expect(a.index).toEqual(0);
expect(a.toHex()).toEqual('0x000000000000000000000000000000000000000000000000000000000000000000');
});
it('correctly decodes a stream with Address20', (): void => {
const a = registry.createType('MultiAddress', '0x0467f89207abe6e1b093befd84a48f03313765929207009e292608');
+6 -6
View File
@@ -3,17 +3,15 @@
import type { Registry } from '../types';
import { isBn, isNumber, isString } from '@polkadot/util';
import { isBn, isNumber, isString, isU8a } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import { Enum } from '../codec/Enum';
import { GenericAccountId } from './AccountId';
import { GenericAccountIndex } from './AccountIndex';
function decodeString (registry: Registry, value: string): unknown {
const u8a = decodeAddress(value.toString());
if (u8a.length === 32) {
function decodeU8a (registry: Registry, u8a: Uint8Array): unknown {
if ([0, 32].includes(u8a.length)) {
return { Id: u8a };
} else if (u8a.length === 20) {
return { Address20: u8a };
@@ -32,7 +30,9 @@ function decodeMultiAny (registry: Registry, value?: unknown): unknown {
} else if (value instanceof GenericAccountIndex || isBn(value) || isNumber(value)) {
return { Index: isNumber(value) ? value : value.toNumber() };
} else if (isString(value)) {
return decodeString(registry, value.toString());
return decodeU8a(registry, decodeAddress(value.toString()));
} else if (isU8a(value)) {
return decodeU8a(registry, value);
}
return value;