HooksuseIdentityKit

useIdentityKit

@nfid/identitykit exports only one react hook, which is needed for getting the agent for the currently connected user, its wallet address (as a valid ICRC1 Account), balance, disconnect function and other:

Import

import { useIdentityKit } from "@nfid/identitykit/react"

Usage

const {
  agent,
  isInitializing,
  user,
  isUserConnecting,
  icpBalance,
  signer,
  identity,
  delegationType,
  accounts,
  connect,
  disconnect,
  fetchIcpBalance,
} = useIdentityKit()

Return values

isInitializing

boolean

Boolean representing wether identitykit initialized or not (Connect wallet will be disabled until initialization finishes).

agent

SignerAgent<Signer<Transport>> | null

@nfid/identitykit agent which implements @dfinity/agent Agent, it comes with predefined user and should be used if canister call requires approve. Will be null until successfull connect.

user

{ principal: Principal, subaccount?: SubAccount} | undefined

Currently connected user principal and subaccount. Will be undefined until successfull connect.

isUserConnecting

boolean

Boolean representing wether user is connecting or not (signer selected, but user still not connected).

icpBalance

number | undefined

Balance of connected user, will be undefined until successfull connect

signer

Signer<Transport> | undefined

Selected signer, undefined until selection

identity

Identity | PartialIdentity | undefined

If IdentityKitProvider.authType is set to IdentityKitAuthType.DELEGATION value will contain @dfinity identity after successfull connect, will be undefined if other authType is selected or user not connected

delegationType

IdentityKitDelegationType.ACCOUNT | IdentityKitDelegationType.RELYING_PARTY

Equals IdentityKitDelegationType.ACCOUNT if delegation has targets, IdentityKitDelegationType.RELYING_PARTY if delegation doesn’t have targets, and undefined if user is not connected or authType is set to IdentityKitAuthType.ACCOUNTS

accounts

{ principal: Principal, subaccount?: SubAccount}[] | undefined

Will contain connected accounts in case IdentityKitProvider.authType is set to IdentityKitAuthType.ACCOUNTS and user is connected, otherwise will be undefined

connect

() => void

Function to trigger manual connect (opening of signers modal).

⚠️

Function will throw Error("Identitykit is not initialized yet") until identitykit initializes, Error("User is already connecting") if signer is selected, but user is still connecting or Error("Identitykit is already connected") for connected user. So make sure you disable your connect button in these cases.

disconnect

() => Promise<void>

Function to trigger manual disconnect

fetchIcpBalance

() => Promise<void> | undefined

Function to manually fetch ICP balance of connected user, will be undefined if user is not connected. Note function does not return balance, instead updates icpBalance from useIdentityKit()