Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/types/src/generic/MultiAddress.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions packages/types/src/generic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
1 change: 1 addition & 0 deletions packages/types/src/interfaces/runtime/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default {
LookupSource: 'GenericLookupSource',
LookupTarget: 'AccountId',
ModuleId: 'LockIdentifier',
MultiAddress: 'GenericMultiAddress',
Moment: 'u64',
OpaqueCall: 'Bytes',
Origin: 'DoNotConstruct<Origin>',
Expand Down