Add initial MultiAddress impl. (#2776)

* Add intitial MultiAddress impl.

* Allow invalid ss58 to be handled upstream
This commit is contained in:
Jaco Greeff
2020-10-24 09:52:36 +02:00
committed by GitHub
parent 7ca78c5679
commit 1ae37ed2d3
3 changed files with 46 additions and 0 deletions
@@ -0,0 +1,44 @@
// Copyright 2017-2020 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Registry } from '../types';
import { isString, u8aConcat } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import Enum from '../codec/Enum';
export default class MultiAddress extends Enum {
constructor (registry: Registry, value?: unknown) {
super(registry, {
Id: 'AccountId',
Index: 'Compact<AccountIndex>',
Raw: 'Bytes',
// eslint-disable-next-line sort-keys
Address32: 'H256',
// eslint-disable-next-line sort-keys
Address20: 'H160'
}, MultiAddress._decodeMultiAddress(value as string));
}
private static _decodeMultiAddress (value?: unknown): unknown {
if (isString(value)) {
try {
const u8a = decodeAddress(value.toString());
return u8aConcat(new Uint8Array(u8a.length <= 8 ? 1 : 0), u8a);
} catch (error) {
// ignore, not a valid ss58 address
}
}
return value;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.value.toString();
}
}
+1
View File
@@ -10,4 +10,5 @@ export { default as GenericCall } from './Call';
export { default as GenericConsensusEngineId } from './ConsensusEngineId';
export { default as GenericEvent } from './Event';
export { default as GenericLookupSource } from './LookupSource';
export { default as GenericMultiAddress } from './MultiAddress';
export { default as GenericVote } from './Vote';
@@ -81,6 +81,7 @@ export default {
LookupSource: 'GenericLookupSource',
LookupTarget: 'AccountId',
ModuleId: 'LockIdentifier',
MultiAddress: 'GenericMultiAddress',
Moment: 'u64',
OpaqueCall: 'Bytes',
Origin: 'DoNotConstruct<Origin>',