useIdentity

The SDK manages Identity with the useIdentity hook:

import { useIdentity } from 'zk3-sdk'

Identity generation and retrieval

const Component = () => {
    const { identity, signIdentityMessage } = useIdentity()
    
    const handleGenerateIdentity = async () => {
        await signIdentityMessage()
    }
    
    return (
    <div>
        <button onClick={handleGenerateIdentity}>
            Generate Identity
        </button>
        <h1>
            {identity ? identity.getCommitment().toString() : 'No Identity'}
        </h1>
    </div>
    )
}

The identity variable is null if signIdentityMessage() has not yet been called; it returns a Semaphore Identity object otherwise.

Once await signIdentityMessage() has been called, the identity data is stored and persisted automatically using the React Context API and the browser's local storage. You don't have to worry about stale states or regenerating the identity on browser refresh. As long as the user is using the same EOA, they will be able to regenerate the same identity.

Disconnecting Identities

const { disconnectIdentity } = useIdentity()

const handleDisconnectIdentity = () => {
    disconnectIdentity()
}

You can disconnect the currently stored identity with the synchronous disconnectIdentity() function. This wipes the context and the local storage. You will have to await signIdentityMessage() again to regenerate an Identity.

Retrieving the Identity linked EOA

When users change their connected EOA, the Identity doesn't change. It is still linked with the EOA used to sign the Identity message. This is necessary for the double signing of proofs (see Double Signed Proofs) with EOAs different than the Identity linked EOA.

const { identityLinkedEOA } = useIdentity()

Last updated