How to convert principal id to account id in js?

I know from Canlista that @noble/hashes has a way of converting principal id to account id, so presumably the plugin wallet works by generating a corresponding principal id for each account id.
By verifying the transaction history of the converted account id, I think my inference is correct.
However, I have now only found how to do this conversion with rust on the forums, but not how to do it with js.
I’ve tried looking for the ic-js nns library, and @dfinity/principal.
By the way, why not do this conversion directly into @dfinity/principal?

I assuming that with “account id” you mean an account identifier used for the ICP ledger transactions?

If yes, checkount the @dfinity/ledger-icp library class AccountIdentifier :point_right: https://github.com/dfinity/ic-js/tree/main/packages/ledger-icp#factory-accountidentifier

4 Likes

Thanks for the reply Spider-Man, you are always so quick to solve our problems. :grinning:
With @dfinity/ledger-icp I did solve the problem, but I would also like to point out that the following two pieces of code were previously mentioned in the readme of the nns library for ic-js:
import { AccountIdentifier, LedgerCanister } from "@dfinity/nns";

const accountIdentifier = AccountIdentifier.fromHex(
    "efa01544f509c56dd85449edf2381244a48fad1ede5183836229c00ab00d52df",
  ).

The AccountIdentifier in this now seems to have been moved to the @dfinity/ledger-icp library, which caused a degree of misunderstanding on my part.

2 Likes

Cool! Thanks for the input, probably forgot to update the README when we migrated the code to the new library (related announcement). I’ll update the README next week.

2 Likes

Done https://github.com/dfinity/ic-js/pull/458.
Note: The new README will appears on npmjs next time we publish the lib.

1 Like

cool, efficient work :wink:

1 Like