diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts index 3e4b4226da..6046b9a87e 100644 --- a/packages/api-derive/src/accounts/info.ts +++ b/packages/api-derive/src/accounts/info.ts @@ -3,9 +3,9 @@ // of the Apache-2.0 license. See the LICENSE file for details. 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 { DeriveAccountInfo } from '../types'; +import { DeriveAccountInfo, DeriveAccountRegistration } from '../types'; import { Observable, combineLatest, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; @@ -16,7 +16,7 @@ import { memo } from '../util'; function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable { return (( - accountId && api.query.nicks + accountId && api.query.nicks?.nameOf ? api.query.nicks.nameOf>>(accountId) : of(undefined) ) as Observable> | undefined>).pipe( @@ -28,6 +28,29 @@ function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable { + return (( + accountId && api.query.identity?.identityOf + ? api.query.identity.identityOf>(accountId) + : of(undefined) + ) as Observable | 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 * @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 Observable { return memo((address?: AccountIndex | AccountId | Address | string | null): Observable => api.derive.accounts.idAndIndex(address).pipe( - switchMap(([accountId, accountIndex]): Observable<[DeriveAccountInfo, string?]> => + switchMap(([accountId, accountIndex]): Observable<[Partial, DeriveAccountRegistration, string?]> => combineLatest([ of({ accountId, accountIndex }), + retrieveIdentity(api, accountId), retrieveNick(api, accountId) ]) ), - map(([{ accountId, accountIndex }, nickname]): DeriveAccountInfo => ({ - accountId, accountIndex, nickname + map(([{ accountId, accountIndex }, identity, nickname]): DeriveAccountInfo => ({ + accountId, accountIndex, identity, nickname })) )); } diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 9b8acd0d4e..df028b8de1 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -2,16 +2,22 @@ // This software may be modified and distributed under the terms // 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 { u32, Vec } from '@polkadot/types'; export type AccountIndexes = Record; +export interface DeriveAccountRegistration { + displayName?: string; + judgements: RegistrationJudgement[]; +} + export interface DeriveAccountInfo { accountId?: AccountId; accountIndex?: AccountIndex; + identity: DeriveAccountRegistration; nickname?: string; } diff --git a/packages/types/src/primitive/Data.ts b/packages/types/src/primitive/Data.ts index e16e5290f6..a0c3ce86af 100644 --- a/packages/types/src/primitive/Data.ts +++ b/packages/types/src/primitive/Data.ts @@ -8,6 +8,7 @@ import { isString, isU8a, u8aToU8a } from '@polkadot/util'; import Enum from '../codec/Enum'; import { createType } from '../codec/create/createType'; +import Bytes from './Bytes'; function decodeDataU8a (registry: Registry, value: Uint8Array): [any, number | undefined] { if (!value.length) { @@ -61,6 +62,14 @@ export default class Data extends Enum { }, ...decodeData(registry, value)); } + get asRaw (): Bytes { + return this.raw as Bytes; + } + + get isRaw (): boolean { + return this.index === 1; + } + /** * @description The encoded length */