How to convert public key to principal?

In TypeScript, how to convert a public key (Uint8Array) into a principal?

I tried Principal.fromUint8Array(pubkey) this produces a too long and just wrong principal.

To which key pair does that public key belong to? Where does that public key come from?

From NFID identity:

  function updateClientNfid(identity) {
    const pubkey = identity.getDelegation().delegations[0].delegation.pubkey; // TODO: correct?
    const isAuthenticated = true; // FIXME
    const principal = Principal.fromUint8Array(pubkey); // FIXME: wrong
    const agent = new HttpAgent({identity});
    if (getIsLocal()) {
      agent.fetchRootKey();
    }

    setAuth({agent, isAuthenticated, identity, principal, options: props.options});
  }

...
      const delegationIdentity: Identity = await nfid.getDelegation({
        targets: [], // FIXME: needed?
        derivationOrigin: `https://${process.env.CANISTER_ID_frontend!}.icp0.io`,
        maxTimeToLive: BigInt(8) * BigInt(3_600_000_000_000),
      });
      updateClientNfid(delegationIdentity);

I want to display the obtained principal to the user.

Since the delegationIdentity is of type Identity, you should be able to just do:

const principal = delegationIdentity.getPrincipal();

No, it is if type DelegationIdentify and does not have getPrincipal method :frowning:

According to line 69 of the source code, the getDelegation method returns an Identity :thinking:

Are you using another package than @nfid/embed?

Your probably looking for Principal.selfAuthenticating(pubKey).

1 Like