Fix variable-length MultiAddress decoding (only detect 32 & 20) (#3204)

* Fix variable-length MultiAddress decoding (only detect 32 & 20)

* Decoding tests

* Fix Call test

* patch-only

* Update CHANGELOG.md
This commit is contained in:
Jaco Greeff
2021-02-23 09:13:47 +01:00
committed by GitHub
parent 0ac085756a
commit b49c2314e7
16 changed files with 112 additions and 74 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/types",
"version": "3.10.2-0",
"version": "3.10.2",
"description": "Implementation of the Parity codec",
"main": "index.js",
"sideEffects": [
@@ -16,7 +16,7 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/types#readme",
"dependencies": {
"@babel/runtime": "^7.12.18",
"@polkadot/metadata": "3.10.2-0",
"@polkadot/metadata": "3.10.2",
"@polkadot/util": "^5.7.1",
"@polkadot/util-crypto": "^5.7.1",
"@polkadot/x-rxjs": "^5.7.1",
+1 -1
View File
@@ -28,7 +28,7 @@ describe('Call', (): void => {
expect(
new Call(registry, '0x0601').toU8a()
).toEqual(
new Uint8Array([6, 1, 1, 110, 7, 17, 0, 0, 0])
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
});
});
@@ -0,0 +1,29 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '../create';
describe('MultiAddress', (): void => {
const registry = new TypeRegistry();
it('correctly decodes a stream with Address20', (): void => {
const a = registry.createType('MultiAddress', '0x0467f89207abe6e1b093befd84a48f03313765929207009e292608');
expect(a.index).toEqual(4);
expect(a.toString()).toEqual('0x67f89207abe6e1b093befd84a48f033137659292');
});
it('correctly decodes an AccountId', (): void => {
const a = registry.createType('MultiAddress', '0x0102030405060708010203040506070801020304050607080102030405060708');
expect(a.index).toEqual(0);
expect(a.toString()).toEqual('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF');
});
it('correctly decodes an Address20', (): void => {
const a = registry.createType('MultiAddress', '0x67f89207abe6e1b093befd84a48f033137659292');
expect(a.index).toEqual(4);
expect(a.toString()).toEqual('0x67f89207abe6e1b093befd84a48f033137659292');
});
});
+3 -5
View File
@@ -10,14 +10,12 @@ import { Enum } from '../codec/Enum';
import { GenericAccountId } from './AccountId';
import { GenericAccountIndex } from './AccountIndex';
function decodeMultiU8a (registry: Registry, value?: unknown): unknown {
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 };
} else {
return decodeMultiAny(registry, registry.createType('AccountIndex', value));
}
}
@@ -32,10 +30,10 @@ function decodeMultiAny (registry: Registry, value?: unknown): unknown {
} else if (value instanceof GenericAccountIndex || isNumber(value) || isBn(value)) {
return { Index: registry.createType('Compact<AccountIndex>', value) };
} else if (isString(value)) {
return decodeMultiU8a(registry, decodeAddress(value.toString()));
return decodeMultiU8a(decodeAddress(value.toString()));
}
return decodeMultiU8a(registry, value);
return decodeMultiU8a(value);
}
export class GenericMultiAddress extends Enum {