Skip to content

Commit

Permalink
Allow api.derive.accounts.identity() to obtain SubIdentities (#6066)
Browse files Browse the repository at this point in the history
* Allow api.derive.accounts.identity to obtain subidentities

* Fix subs type
  • Loading branch information
valentinfernandez1 authored Jan 14, 2025
1 parent eab2f76 commit 4323c6f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/api-derive/src/accounts/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeriveAccountRegistration> {
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])
Expand Down
2 changes: 2 additions & 0 deletions packages/api-derive/src/accounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -20,6 +21,7 @@ export interface DeriveAccountRegistration {
parent?: AccountId | undefined;
pgp?: string | undefined;
riot?: string | undefined;
subs?: Vec<AccountId> | undefined;
twitter?: string | undefined;
web?: string | undefined;
judgements: RegistrationJudgement[];
Expand Down

0 comments on commit 4323c6f

Please sign in to comment.