Where is "AccountIdentifier"?

@NathanosDev, I am trying to convert a principal into an AccountIdentifier in javascript but I cannot find it:

import { AccountIdentifier } from "@dfinity/icp-ledger";   //<--- Error

let me_address = AccountIdentifier.fromPrincipal({
    principal: me,   // me:Principal
  }).toHex();

This gives me an error in the import:

Cannot find module '@dfinity/icp-ledger' or its corresponding type declarations.ts(2307)

How Can I convert the principal into an account (in string format)?

You need to import it from here

import { AccountIdentifier } from '@dfinity/nns';
1 Like

Also

    return AccountIdentifier.fromPrincipal({
      principal: Principal.fromText(<some_principal>),
    })
1 Like

Actually, I think it’s neither @dfinity/nns or @dfinity/icp-ledger if you are looking for the JavaScript class that can be initialized with AccountIdentifier.fromSomething.

It’s in @dfinity/ledger-icp (link).

e.g. for an account without subaccount:

import { AccountIdentifier } from '@dfinity/ledger-icp';
import type { Principal } from '@dfinity/principal';

export const getAccountIdentifier = (principal: Principal): AccountIdentifier =>
	AccountIdentifier.fromPrincipal({ principal, subAccount: undefined });
2 Likes