init examples

EVM

import { useWalletClient } from 'wagmi';
const { data: walletClient } = useWalletClient();
IDENTITY_CLIENT.onSignMessage(async (data: { message: string }) => {
  return await signMessageAsync({ message: data.message });
});
IDENTITY_CLIENT.onSendTransaction(async (data: TransactionData) => {
  return walletClient.sendTransaction({
    account: data.accountAddress,
    to: data.to,
    data: data.data,
    value: data.value ? parseEther(data.value) : parseEther("0"),
  });
});
IDENTITY_CLIENT.onSdkReady((data) => {
  // this is the polygon did
  setDID(data.did);
});
IDENTITY_CLIENT.onVerification((isVerified) => {
  setVerified(isVerified);
});
await IDENTITY_CLIENT.init({
  accessToken: accessToken,
  signature: signature,
  signingMessage: signingMessage,
});

Aptos

export const signWithAptos = async (
  message: string,
  wallet: Aptos | undefined,
) => {
  if (!wallet) {
    throw new Error("signWithAptos called before wallet was connected");
  }
  const aptosMessage = {
    address: true,
    application: true,
    chainId: true,
    message,
    nonce: Date.now().toLocaleString(),
  };

  const { signature } = await wallet.signMessage(aptosMessage);

  return signature;
};

if (!accessToken || !signature || !signingMessage) throw new Error("Missing kycAuth data");

IDENTITY_CLIENT.onSignMessage(async (data: { message: string }) => {
  return await signWithAptos(data.message, wallet);
});

IDENTITY_CLIENT.onSdkReady((data) => {
  IDENTITY_CLIENT.startVerification();
  // this is the polygon did
  setDID(data.did);
});

await IDENTITY_CLIENT.init({
  accessToken: accessToken,
  signature: signature,
  signingMessage: signingMessage,
});

Cardano

export const signWithCardano = async (
  message: string,
  wallet: WalletApi | undefined,
) => {
  if (!wallet) {
    throw new Error("signWithCardano called before wallet was connected");
  }
  const usedAddresses = await wallet.getUsedAddresses();

  const userAddress = usedAddresses[0];
  const formattedAddress =
    userAddress && (await formatCardanoAddress(userAddress));

  if (!formattedAddress) {
    throw new Error("No user connected in wallet");
  }
  const hexMessage = Buffer.from(message).toString("hex");
  const { signature } = await wallet.signData(userAddress, hexMessage);
  return CardanoSignature.parse(signature);
};

if (!accessToken || !signature || !signingMessage)
        throw new Error("Missing kycAuth data");

IDENTITY_CLIENT.onSignMessage(async (data: { message: string }) => {
  return await signWithCardano(data.message, wallet);
});

IDENTITY_CLIENT.onSdkReady((data) => {
  setDID(data.did);
});

await IDENTITY_CLIENT.init({
  accessToken: accessToken,
  signature: signature,
  signingMessage: signingMessage,
});

Cosmos

export const signWithCosmos = async (
  message: string,
  wallet: Keplr | undefined,
) => {
  // Ensure that Keplr is installed and available
  if (!wallet) {
    throw new Error("Wallet not connected");
  }
  const offlineSigner = wallet.getOfflineSigner(COSMOS_CHAIN_ID);

  const accounts = await offlineSigner.getAccounts();
  const cosmosAddress = accounts[0]?.address;
  if (!cosmosAddress) {
    throw new Error("Wallet not connected - cosmosAddress missing");
  }

  // Sign the message using Keplr
  const signed = await wallet.signArbitrary(
    COSMOS_CHAIN_ID,
    cosmosAddress,
    message, // Make sure SIGN_MSG is defined somewhere in your code
  );
  // Decode base64 signature to bytes
  const signatureBytes = Uint8Array.from(atob(signed.signature), (c) =>
    c.charCodeAt(0),
  );

  // Convert bytes to hexadecimal string
  const signatureHex = Array.prototype.map
    .call(signatureBytes, (x: number) => ("00" + x.toString(16)).slice(-2))
    .join("");
  // Return the signature
  return CosmosSignature.parse(signatureHex);
};

IDENTITY_CLIENT.onSignMessage(async (data: { message: string }) => {
  return await signWithCosmos(data.message, wallet);
});

IDENTITY_CLIENT.onSdkReady((data) => {
  setDID(data.did);
});

await IDENTITY_CLIENT.init({
  accessToken: accessToken,
  signature: signature,
  signingMessage: signingMessage,
});

Polkadot

import type { InjectedExtension } from "@polkadot/extension-inject/types";
export const signWithPolkadot = async (
  message: string,
  accountAddress: PolkadotAddress,
  injector: InjectedExtension | undefined,
) => {
  if (!injector) {
    throw new Error("signWithPolkadot called before wallet was injected");
  }
  const messageU8a = new TextEncoder().encode(message);

  // Use the injector to sign the message
  const signature =
    injector.signer.signRaw &&
    (await injector.signer.signRaw({
      address: accountAddress,
      data: messageU8a.toString(),
      type: "bytes",
    }));

  return signature;
};

IDENTITY_CLIENT.onSignMessage(async (data: { message: string }) => {
  if (!wallet?.address) {
    return "";
  }
  return await signWithPolkadot(
    data.message,
    wallet?.address,
    wallet?.injector,
  );
});

IDENTITY_CLIENT.onSdkReady((data) => {
  // this is the polygon did
  setDID(data.did);
});

await IDENTITY_CLIENT.init({
  accessToken: accessToken,
  signature: signature,
  signingMessage: signingMessage,
});

Starknet

export const signWithStarknet = async (
  message: string,
  account: AccountInterface | undefined,
) => {
  if (!account?.signer) {
    throw new Error("signWithStarknet called before wallet was connected");
  }
  // Message must be hashed in order to be signed by starknet
  const messageHash = hash
    .starknetKeccak(message)
    .toString(16)
    .substring(0, 31);
  const starknetMessage = {
    types: {
      Message: [{ name: "hash", type: "felt" }],
      StarkNetDomain: [{ name: "name", type: "felt" }],
    },
    primaryType: "Message",
    domain: {
      name: "Nexera ID Auth Message",
    },
    message: {
      hash: messageHash,
    },
  };

IDENTITY_CLIENT.onSignMessage(async (data: { message: string }) => {
  return await signWithStarknet(
    data.message,
    wallet?.account as AccountInterface,
  );
});

IDENTITY_CLIENT.onSdkReady((data) => {
  IDENTITY_CLIENT.startVerification();
  setDID(data.did);
});

await IDENTITY_CLIENT.init({
  accessToken: accessToken,
  signature: signature,
  signingMessage: signingMessage,
});

Tezos

import { BeaconWallet } from "@taquito/beacon-wallet";
import { stringToBytes } from "@taquito/utils";
import { SigningType } from "@airgap/beacon-types";
export const signWithTezos = async (
  message: string,
  wallet: BeaconWallet | undefined,
) => {
  if (!wallet) {
    throw new Error("signWithTezos called before wallet was connected");
  }
  const activeAccount = await wallet.client.getActiveAccount();
  const pubKey = activeAccount?.publicKey;

  const formattedInput: string = [
    "Tezos Signed Message:",
    (window as Window).location.host,
    new Date().toISOString(),
    message,
  ].join(" ");
  const bytes = stringToBytes(formattedInput);
  const bytesLength = (bytes.length / 2).toString(16);
  const addPadding = `00000000${bytesLength}`;
  const paddedBytesLength = addPadding.slice(addPadding.length - 8);
  const payloadBytes = "05" + "01" + paddedBytesLength + bytes;
  
  // 05 prefix is needed for MICHELINE signing
  const { signature } = await wallet.client.requestSignPayload({
    signingType: SigningType.MICHELINE,
    payload: payloadBytes,
  });

  const fullSignature = signature + pubKey;
  return TezosSignature.parse(fullSignature);
};


if (!accessToken || !signature || !signingMessage)
  throw new Error("Missing kycAuth data");
  IDENTITY_CLIENT.onSignMessage(async (data: { message: string }) => {
  return await signWithTezos(data.message, wallet);
});

IDENTITY_CLIENT.onSdkReady((data) => {
  // this is the polygon did
  setDID(data.did);
});

await IDENTITY_CLIENT.init({
  accessToken: accessToken,
  signature: signature,
  signingMessage: signingMessage,
});