import { Polymesh } from '@polymathnetwork/polymesh-sdk';
import { decodeAddress } from '@polkadot/util-crypto';
import { IdentityId } from '@polymathnetwork/polymesh-sdk/polkadot/types';

export const getDidFromAddress = async (
  userAddress: string,
  sdk: Polymesh
): Promise<IdentityId | undefined> => {
  const publicKey = decodeAddress(userAddress);
  const linkedKeyInfo = await sdk._polkadotApi.query.identity.keyToIdentityIds(
    publicKey
  );

  if (!linkedKeyInfo.isEmpty) {
    return linkedKeyInfo as IdentityId;
  }
};
