// Copyright 2017-2023 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; import type { AccountId, AccountIndex } from '@polkadot/types/interfaces'; import type { DeriveApi } from '../types.js'; import { map, of } from 'rxjs'; import { memo } from '../util/index.js'; /** * @name indexToId * @param {( AccountIndex | string )} accountIndex - An accounts index in different formats. * @returns Returns the corresponding AccountId. * @example *
* * ```javascript * api.derive.accounts.indexToId('F7Hs', (accountId) => { * console.log(`The AccountId of F7Hs is ${accountId}`); * }); * ``` */ export function indexToId (instanceId: string, api: DeriveApi): (accountIndex: AccountIndex | string) => Observable { return memo(instanceId, (accountIndex: AccountIndex | string): Observable => api.query.indices ? api.query.indices.accounts(accountIndex).pipe( map((optResult): AccountId | undefined => optResult.unwrapOr([])[0] ) ) : of(undefined) ); }