Docs: TypeScript SDK

Integrate

TypeScript SDK

sdk/hipid.ts is a small, browser-first module on top of viem: create a passkey, derive the account, build, sponsor, sign and send operations, and manage session keys.

Setup

The SDK is one file, sdk/hipid.ts, with viem as its only dependency. The passkey functions call navigator.credentials, so they run in a browser on a page served from the passkey's rpId. Every other function works in any JavaScript runtime. Reads take a viem PublicClient on Elysium; bundler calls take the bundler URL: HIPID_BUNDLER (https://hipid.app/api/bundler) from a server, or /api/bundler on hipid.app itself.

import { createPublicClient, http } from "viem";
const client = createPublicClient({ transport: http("https://rpc.elysium.kinetiq.xyz") });
const BUNDLER = "/api/bundler";

First operation

import { createPasskey, accountAddress, buildOp, helloCall, sponsor,
         signUserOpWithPasskey, sendUserOperation, waitForReceipt } from "@/sdk/hipid";

const pk = await createPasskey({ rpName: "My app", userName: "alice" }); // Face ID / Touch ID
const sender = await accountAddress(client, pk);                         // known before deployment
const op = await buildOp(client, pk, helloCall());                       // includes factory + factoryData
const { userOperation, userOpHash } = await sponsor(BUNDLER, op);        // gas + paymaster approval
userOperation.signature = await signUserOpWithPasskey(pk, userOpHash);   // Face ID again
await sendUserOperation(BUNDLER, userOperation);
const receipt = await waitForReceipt(BUNDLER, userOpHash);
console.log(receipt.success, receipt.hipid.latencyMs);

Store the returned Passkey (credential id, public key, rpId): every later signature needs it, because the keyId comes from x and y and the credential id selects the passkey. The site keeps it in localStorage.

Who fills which field

fieldbuildOpsponsorsignsend
sender
nonce
factory, factoryData
callData
callGasLimit
verificationGasLimit
preVerificationGas
maxFeePerGas, maxPriorityFeePerGas
paymaster + gas limits
paymasterData (approval)
signature
How a UserOperation is assembled. Filled square: the step sets the field. Outline: the field passes through unchanged. factory and factoryData are set only while the account is not deployed.

buildOp sets what the account decides: sender, nonce, the deployment fields and the call. sponsor sets what the bundler and paymaster decide: gas limits, fees and the paymaster approval, and it returns the userOpHash of the result. The signature comes last, because the hash covers every other field.

Passkeys

createPasskey(opts)

{ rpName: string; userName: string; rpId?: string }, rpId defaults to location.hostname (the HIP-ID app passes hipid.app, also on www). Requests ES256 (algorithm -7), a resident key, required user verification and no attestation. Reads x and y from the 91-byte SPKI public key and returns { credentialId, x, y, rpId }, with the credential id in base64url.

signUserOpWithPasskey(pk, userOpHash)

Calls navigator.credentials.get with the userOpHash bytes as the challenge, the stored credential id and required user verification. Decodes the DER signature, flips s to the low half, finds challengeIndex and typeIndex in the returned clientDataJSON, and returns 0x00 ++ abi.encode(keyId, WebAuthnAuth). See WebAuthn and P-256 for what the account checks.

Account calls

accountAddress(client, pk, salt = 0n) reads getAddress(x, y, sha256(rpId), salt) from the factory. buildOp(client, pk, callData) derives the address with salt 0, checks whether code exists there, reads the nonce from the EntryPoint if so, and otherwise adds factory and factoryData for createAccount with nonce 0.

import { encodeFunctionData, erc20Abi, parseUnits } from "viem";
import { executeCall, executeBatchCall } from "@/sdk/hipid";

const transfer = encodeFunctionData({ abi: erc20Abi, functionName: "transfer", args: [to, parseUnits("5", 18)] });
const one = executeCall(token, 0n, transfer);
const two = executeBatchCall([
  { target: token, value: 0n, data: transfer },
  { target: other, value: 0n, data: otherCall },
]);

The paymaster sponsors only execute and executeBatch calls that do not target the paymaster.

Bundler calls

  • sponsor(url, op) returns { userOperation, userOpHash, validUntil, maxCostWei }. Sign and send before validUntil.
  • sendUserOperation(url, op) returns the userOpHash once the bundler has validated and queued it.
  • waitForReceipt(url, userOpHash, timeoutMs) polls eth_getUserOperationReceipt every 250 ms and hipid_status in between; it throws if the status becomes failed, and after the timeout throws without implying failure, because the operation may still be included.
  • bundlerRpc(url, method, params) calls any method. Errors throw BundlerError with the JSON-RPC code, or the HTTP status if the response was not 2xx (503 when the bundler is offline).

Check before you sign

The passkey prompt shows the site, not the operation, so the client is the only place that can notice if an operation was changed on the way. sponsor checks the bundler's answer before returning it, and throws BundlerError if anything is off:

  • sender, nonce, factory, factoryData and callData come back exactly as you sent them;
  • the paymaster is the HIP-ID paymaster;
  • the userOpHash is recomputed locally with viem's getUserOperationHash (EntryPoint v0.7, chain 99901) and must match.

The bundler only adds gas limits, fees and the paymaster approval. If you talk to a bundler without the SDK, run the same check yourself:checkSponsored(askedOp, sponsoredResult) is exported.

Session keys

import { generatePrivateKey, privateKeyToAddress } from "viem/accounts";
import { toFunctionSelector } from "viem";
import { grantSessionCall, signUserOpWithSessionKey, NO_AMOUNT_ARG, HELLO, buildOp, helloCall,
         sponsor, signUserOpWithPasskey, sendUserOperation } from "@/sdk/hipid";

// 1. Grant, signed once by the passkey
const sessionKey = generatePrivateKey();             // keep it in memory, never on a server
const sessionAddress = privateKeyToAddress(sessionKey);
const now = Math.floor(Date.now() / 1000);
const grant = grantSessionCall(sender, sessionAddress, now + 3600, 600, 20, [
  { target: HELLO, selector: toFunctionSelector("sayHello()"), amountArg: NO_AMOUNT_ARG, maxValue: 0n, maxAmount: 0n },
]);
const s1 = await sponsor(BUNDLER, await buildOp(client, pk, grant));
s1.userOperation.signature = await signUserOpWithPasskey(pk, s1.userOpHash);
await sendUserOperation(BUNDLER, s1.userOperation);

// 2. Use, no prompt: up to 20 ops per 10 minutes, for one hour, only HipHello.sayHello
const s2 = await sponsor(BUNDLER, await buildOp(client, pk, helloCall()));
s2.userOperation.signature = await signUserOpWithSessionKey(sessionKey, sessionAddress, s2.userOpHash);
await sendUserOperation(BUNDLER, s2.userOperation);

Scope is { target, selector, amountArg, maxValue, maxAmount } with bigint caps. The rules are on Session keys. revokeSessionCall(account, key) builds the revoke; sign it with the passkey.

Passkeys and recovery

addPasskeyCall(account, pk) adds another passkey (for example from a second device) using its x, y and rpId. setGuardianCall(account, guardian) sets the address allowed to propose a recovery, and the zero address clears it. cancelRecoveryCall(account) cancels a pending recovery. All three go through execute on the account itself and must be signed by a passkey; a session key signature is refused.

Removing a passkey

The contract has removePasskey(keyId), which refuses to remove the last one. The SDK has no helper for it yet; encode it with viem and wrap it in executeCall(account, 0n, data).

Export reference

ExportWhat
createPasskey(opts)Creates a P-256 passkey; returns Passkey { credentialId, x, y, rpId }
signUserOpWithPasskey(pk, userOpHash)Face ID or Touch ID signs; returns the 0x00 account signature
signUserOpWithSessionKey(privateKey, sessionAddress, userOpHash)Session key signs; returns the 0x01 account signature
accountAddress(client, pk, salt = 0n)Counterfactual account address from the factory
buildOp(client, pk, callData)UserOperation for pk's account, with factory fields if not deployed
getNonce(client, sender)EntryPoint nonce, key 0
executeCall(target, value, data)execute calldata
executeBatchCall(calls)executeBatch calldata
helloCall()execute calldata for HipHello.sayHello
grantSessionCall(account, key, validUntil, period, maxUsesPerPeriod, scopes)Passkey-only: grant a session key
revokeSessionCall(account, key)Passkey-only: revoke a session key
addPasskeyCall(account, pk)Passkey-only: add another passkey
setGuardianCall(account, guardian)Passkey-only: set or clear (zero address) the guardian
cancelRecoveryCall(account)Passkey-only: cancel a pending recovery
sponsor(url, op)pm_sponsorUserOperation
sendUserOperation(url, op)eth_sendUserOperation; returns the userOpHash
waitForReceipt(url, userOpHash, timeoutMs = 120000)Polls the receipt every 250 ms; throws on a failed status
bundlerRpc(url, method, params)Any bundler method; throws BundlerError
rpIdHash(rpId), keyId(x, y)sha256(rpId) and keccak256(abi.encode(x, y))
derToRs(der), spkiToXY(spki)DER signature to low-s (r, s); SPKI public key to (x, y)
ENTRY_POINT, FACTORY, PAYMASTER, HELLODeployed addresses
NO_AMOUNT_ARG255: a scope with no amount cap
factoryAbicreateAccount and getAddress ABI
Passkey, UserOperation, Scope, UserOpReceipt, BundlerErrorTypes and the error class