Add identity names to derive.accounts.info (#1673)
This commit is contained in:
@@ -3,9 +3,9 @@
|
|||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||||
import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/interfaces';
|
import { AccountId, AccountIndex, Address, Balance, Registration } from '@polkadot/types/interfaces';
|
||||||
import { ITuple } from '@polkadot/types/types';
|
import { ITuple } from '@polkadot/types/types';
|
||||||
import { DeriveAccountInfo } from '../types';
|
import { DeriveAccountInfo, DeriveAccountRegistration } from '../types';
|
||||||
|
|
||||||
import { Observable, combineLatest, of } from 'rxjs';
|
import { Observable, combineLatest, of } from 'rxjs';
|
||||||
import { map, switchMap } from 'rxjs/operators';
|
import { map, switchMap } from 'rxjs/operators';
|
||||||
@@ -16,7 +16,7 @@ import { memo } from '../util';
|
|||||||
|
|
||||||
function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<string | undefined> {
|
function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<string | undefined> {
|
||||||
return ((
|
return ((
|
||||||
accountId && api.query.nicks
|
accountId && api.query.nicks?.nameOf
|
||||||
? api.query.nicks.nameOf<Option<ITuple<[Bytes, Balance]>>>(accountId)
|
? api.query.nicks.nameOf<Option<ITuple<[Bytes, Balance]>>>(accountId)
|
||||||
: of(undefined)
|
: of(undefined)
|
||||||
) as Observable<Option<ITuple<[Bytes, Balance]>> | undefined>).pipe(
|
) as Observable<Option<ITuple<[Bytes, Balance]>> | undefined>).pipe(
|
||||||
@@ -28,6 +28,29 @@ function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<s
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function retrieveIdentity (api: ApiInterfaceRx, accountId?: AccountId): Observable<DeriveAccountRegistration> {
|
||||||
|
return ((
|
||||||
|
accountId && api.query.identity?.identityOf
|
||||||
|
? api.query.identity.identityOf<Option<Registration>>(accountId)
|
||||||
|
: of(undefined)
|
||||||
|
) as Observable<Option<Registration> | undefined>).pipe(
|
||||||
|
map((identityOfOpt): DeriveAccountRegistration => {
|
||||||
|
if (!identityOfOpt?.isSome) {
|
||||||
|
return { judgements: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { info, judgements } = identityOfOpt.unwrap();
|
||||||
|
|
||||||
|
return {
|
||||||
|
displayName: info.display.isRaw
|
||||||
|
? u8aToString(info.display.asRaw.toU8a(true))
|
||||||
|
: undefined,
|
||||||
|
judgements
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name info
|
* @name info
|
||||||
* @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname
|
* @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname
|
||||||
@@ -35,14 +58,15 @@ function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<s
|
|||||||
export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable<DeriveAccountInfo> {
|
export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable<DeriveAccountInfo> {
|
||||||
return memo((address?: AccountIndex | AccountId | Address | string | null): Observable<DeriveAccountInfo> =>
|
return memo((address?: AccountIndex | AccountId | Address | string | null): Observable<DeriveAccountInfo> =>
|
||||||
api.derive.accounts.idAndIndex(address).pipe(
|
api.derive.accounts.idAndIndex(address).pipe(
|
||||||
switchMap(([accountId, accountIndex]): Observable<[DeriveAccountInfo, string?]> =>
|
switchMap(([accountId, accountIndex]): Observable<[Partial<DeriveAccountInfo>, DeriveAccountRegistration, string?]> =>
|
||||||
combineLatest([
|
combineLatest([
|
||||||
of({ accountId, accountIndex }),
|
of({ accountId, accountIndex }),
|
||||||
|
retrieveIdentity(api, accountId),
|
||||||
retrieveNick(api, accountId)
|
retrieveNick(api, accountId)
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
map(([{ accountId, accountIndex }, nickname]): DeriveAccountInfo => ({
|
map(([{ accountId, accountIndex }, identity, nickname]): DeriveAccountInfo => ({
|
||||||
accountId, accountIndex, nickname
|
accountId, accountIndex, identity, nickname
|
||||||
}))
|
}))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,22 @@
|
|||||||
// This software may be modified and distributed under the terms
|
// This software may be modified and distributed under the terms
|
||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import { AccountId, AccountIndex, Balance, BalanceLock, BlockNumber, EraIndex, EraPoints, Exposure, Hash, Index, Keys, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RewardDestination, SessionIndex, SetIndex, StakingLedger, TreasuryProposal, ValidatorPrefs, Vote, Votes, VoteIndex } from '@polkadot/types/interfaces';
|
import { AccountId, AccountIndex, Balance, BalanceLock, BlockNumber, EraIndex, EraPoints, Exposure, Hash, Index, Keys, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RegistrationJudgement, RewardDestination, SessionIndex, SetIndex, StakingLedger, TreasuryProposal, ValidatorPrefs, Vote, Votes, VoteIndex } from '@polkadot/types/interfaces';
|
||||||
|
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import { u32, Vec } from '@polkadot/types';
|
import { u32, Vec } from '@polkadot/types';
|
||||||
|
|
||||||
export type AccountIndexes = Record<string, AccountIndex>;
|
export type AccountIndexes = Record<string, AccountIndex>;
|
||||||
|
|
||||||
|
export interface DeriveAccountRegistration {
|
||||||
|
displayName?: string;
|
||||||
|
judgements: RegistrationJudgement[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface DeriveAccountInfo {
|
export interface DeriveAccountInfo {
|
||||||
accountId?: AccountId;
|
accountId?: AccountId;
|
||||||
accountIndex?: AccountIndex;
|
accountIndex?: AccountIndex;
|
||||||
|
identity: DeriveAccountRegistration;
|
||||||
nickname?: string;
|
nickname?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { isString, isU8a, u8aToU8a } from '@polkadot/util';
|
|||||||
|
|
||||||
import Enum from '../codec/Enum';
|
import Enum from '../codec/Enum';
|
||||||
import { createType } from '../codec/create/createType';
|
import { createType } from '../codec/create/createType';
|
||||||
|
import Bytes from './Bytes';
|
||||||
|
|
||||||
function decodeDataU8a (registry: Registry, value: Uint8Array): [any, number | undefined] {
|
function decodeDataU8a (registry: Registry, value: Uint8Array): [any, number | undefined] {
|
||||||
if (!value.length) {
|
if (!value.length) {
|
||||||
@@ -61,6 +62,14 @@ export default class Data extends Enum {
|
|||||||
}, ...decodeData(registry, value));
|
}, ...decodeData(registry, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get asRaw (): Bytes {
|
||||||
|
return this.raw as Bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
get isRaw (): boolean {
|
||||||
|
return this.index === 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description The encoded length
|
* @description The encoded length
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user