From 4323c6fd95a5a6c9e72e8f7669db42efff6d9dd7 Mon Sep 17 00:00:00 2001 From: Valentin Fernandez <33705477+valentinfernandez1@users.noreply.github.com> Date: Tue, 14 Jan 2025 17:00:38 -0300 Subject: [PATCH] Allow api.derive.accounts.identity() to obtain SubIdentities (#6066) * Allow api.derive.accounts.identity to obtain subidentities * Fix subs type --- packages/api-derive/src/accounts/identity.ts | 25 ++++++++++++++++++++ packages/api-derive/src/accounts/types.ts | 2 ++ 2 files changed, 27 insertions(+) diff --git a/packages/api-derive/src/accounts/identity.ts b/packages/api-derive/src/accounts/identity.ts index af8b5bd3f7c..2724d24e953 100644 --- a/packages/api-derive/src/accounts/identity.ts +++ b/packages/api-derive/src/accounts/identity.ts @@ -135,11 +135,36 @@ export function identity (instanceId: string, api: DeriveApi): (accountId?: Acco ), map(([identityOfOpt, superOf]) => extractIdentity(identityOfOpt, superOf) + ), + switchMap((identity) => + getSubIdentities(identity, api, accountId) ) ) ); } +// if an account has no parents it will extract its subidentities +// otherwise if the account is a subidentity, obtain all subidentities of its parent. +function getSubIdentities (identity: DeriveAccountRegistration, api: DeriveApi, accountId?: AccountId | Uint8Array | string): Observable { + const targetAccount = identity.parent || accountId; + + if (!targetAccount) { + // No valid accountId return the identity as-is + return of(identity); + } + + return api.query.identity.subsOf(targetAccount).pipe( + map((subsResponse) => { + const subs = subsResponse[1]; + + return { + ...identity, + subs + }; + }) + ); +} + export const hasIdentity = /*#__PURE__*/ firstMemo( (api: DeriveApi, accountId: AccountId | Uint8Array | string) => api.derive.accounts.hasIdentityMulti([accountId]) diff --git a/packages/api-derive/src/accounts/types.ts b/packages/api-derive/src/accounts/types.ts index 1400bb34e50..d3c27bc67a1 100644 --- a/packages/api-derive/src/accounts/types.ts +++ b/packages/api-derive/src/accounts/types.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { AccountId, AccountIndex, RegistrationJudgement } from '@polkadot/types/interfaces'; +import type { Vec } from '@polkadot/types-codec'; export type AccountIdAndIndex = [AccountId | undefined, AccountIndex | undefined]; @@ -20,6 +21,7 @@ export interface DeriveAccountRegistration { parent?: AccountId | undefined; pgp?: string | undefined; riot?: string | undefined; + subs?: Vec | undefined; twitter?: string | undefined; web?: string | undefined; judgements: RegistrationJudgement[];