Skip to main content

Template: Game App

The game-app template shows how a game can write on-chain on every move without a wallet popup per move. The player signs in with Google or email, grants one scoped Privy session signer, and then taps. Each tap is a real transaction submitted by the app's backend on the player's behalf. The player pays gas.

No sponsorship: the user pays gas in PEPU.

What it demonstrates​

PillarCovered
Google to walletYes
Session keysYes: a Privy session signer on a plain embedded wallet. The scope is enforced off-chain by Privy's policy engine
Sponsored transactionsNo. The player's wallet pays gas, a fraction of a cent per tap

The flow: sign in, grant a session (one consent screen, one signature), play (the backend submits tap() with no further prompts), revoke at any time or let it expire.

The scope, built by buildSessionPolicy from the one definition in lib/scope.ts:

ConstraintValue
Contractonly the deployed TapScore address
Functiononly tap(), matched by selector
Valueexactly 0x0
Expirydenied after SESSION_DURATION_MINUTES

Trust model, stated in the app and the README: Privy's backend policy engine enforces the scope. There is no on-chain validator to audit. For the on-chain model see sponsored-session-app.

Scaffold​

npx create-pepu-app@latest my-app --template game-app

SDK functions it uses​

JobSDK function (subpath)Where
Providers and active-wallet pinPepuPrivyWagmiProvider (/privy/wagmi)app/providers.tsx
Wallet provisioningcreateEnsureWalletRoute (/privy/server), useEnsureWallet, useEmbeddedWallet (/privy/react)app/api/wallet/ensure, components/
Scoped policy and consent databuildSessionPolicy, describeSessionScope, SESSION_TRUST_NOTICE, PRIVY_POLICY_NAME_MAX_LENGTH (/session)lib/scope.ts, components/session-consent-screen.tsx
Grant, status and revoke routescreateSessionRoutes (/session/server)app/api/session/*
One gameplay transaction: Privy signs, the SDK broadcastscreateSessionActionRoute (/session/server)app/api/game/tap
Client session flowuseSessionGrant, useSessionStatus, useSessionRevoke, useSessionAct (/session/react)components/
Authorization key generationgeneratePrivyAuthorizationKey (/doctor)scripts/generate-authorization-key.mjs
Explorer linksexplorerLinks (core)components

Privy's server-side eth_sendTransaction does not support chain 97741. The SDK works around it by asking Privy to sign only (eth_signTransaction) and broadcasting the signed transaction to PEPU's RPC itself, with a 20% gas-limit buffer. See Sign with Privy, Broadcast Yourself.

Environment and Privy settings​

VariableRequiredNotes
NEXT_PUBLIC_PRIVY_APP_IDYesPublic.
PRIVY_APP_SECRETYesServer only. Authenticates the backend that submits taps and provisions wallets.
NEXT_PUBLIC_PRIVY_KEY_QUORUM_IDYesId of the key quorum Privy returns after you register the public key. An identifier, not a secret.
PRIVY_AUTHORIZATION_KEYYesThe private half, base64 PKCS8. A real secret: anyone holding it can request transactions as any wallet that granted a signer to the quorum, bounded by the policy. Never commit it.
NEXT_PUBLIC_GAME_CONTRACT_ADDRESSYesYour deployed TapScore. The policy names this contract explicitly.
SESSION_DURATION_MINUTESNoDefault 15, clamped to 1440. Baked into the policy expiry, so Privy enforces it.
PEPU_RPC_URLNoServer only. RPC the backend broadcasts to. Without it the SDK uses the public default.

Privy dashboard checklist (the same list sits in the template's .env.example). Doctor requirements login-methods, allowed-origins, embedded-wallets, authorization-key-quorum:

  • Env: NEXT_PUBLIC_PRIVY_APP_ID (public) and PRIVY_APP_SECRET (secret, server only), both under Settings, Basics.
  • Allowed origins: the exact origin with the port, http://localhost:3000, plus your production domain.
  • Login methods: email, Google and external wallet, matching loginMethods in app/providers.tsx (enable what you list).
  • Embedded wallets: enable for Ethereum, create on login. WalletGate calls /api/wallet/ensure after login and creates the wallet server-side with the app secret if the dashboard setting is off, so both together is fine. TEE (user-controlled) mode is what the live tests used.
  • Smart Wallets: not needed. This template uses a session signer on a plain embedded wallet.
  • Coinbase Wallet is pinned to eoaOnly in code because it does not support chain 97741.
  • Authorization key and key quorum (Wallet infrastructure, Authorization keys): run npm run generate-authorization-key, register the public key, put it in a key quorum, and set NEXT_PUBLIC_PRIVY_KEY_QUORUM_ID (public id) and PRIVY_AUTHORIZATION_KEY (the private half, secret). They must come from the same pair. Policy and rule names are limited to 49 characters.

See Authorization Keys and Key Quorums and Dashboard Settings, Field by Field.

Run and check​

cd my-app
npm install
npm run generate-authorization-key # register the public half in Privy, then fill .env.local
# deploy contracts/foundry or contracts/hardhat (TapScore) and set NEXT_PUBLIC_GAME_CONTRACT_ADDRESS
npm run doctor
npm run dev # http://localhost:3000
npm test # the scope tests

TapScore is also deployed on mainnet at 0x261eA5971B93237a56990206A46Bc80318e4d078 (used by the sponsored templates). Deploy your own for your own game.

Project structure​

app/api/config/ booleans only, never values
app/api/wallet/ensure/ createEnsureWalletRoute()
app/api/session/* createSessionRoutes(): grant, status, revoke
app/api/game/tap/ createSessionActionRoute(): one gameplay tx
components/ session-consent-screen (the most important UI), session-status-card, game-board
lib/scope.ts the ONE session scope and the game ABI
lib/env.ts client-safe NEXT_PUBLIC_* values
contracts/ TapScore for Foundry and Hardhat

Limitations​

  • Sessions are Privy session signers scoped off-chain. The user pays gas.

Customizing it​

  • To add a gameplay function, add its ABI fragment to lib/scope.ts and add a condition for it. Widening the policy widens what a compromised key can do.
  • Keep gameplay functions non-payable: that is what makes the zero-value condition meaningful.
  • Keep sessions short. The 15 minute default is deliberate.
  • Never add a code path that grants an unscoped signer. An empty policyIds makes a signer effectively unscoped over the whole wallet, and buildSessionPolicy refuses to build one.
  • Policy and rule names must be at most 49 characters (Privy's live validator); buildSessionPolicy enforces it.
  • Keep the off-chain notice in the README and the consent screen. Do not soften it.

Common failures​

SymptomCauseFix
Grant fails with a name errorA custom policy name is 50 characters or moreKeep names at 49 or fewer.
A tap returns 409 or is deniedThe session expired, was revoked, or the call is outside the scopeGrant a new session; check lib/scope.ts against the call.
"Unsupported chain with id 97741"Something is calling Privy's server-side eth_sendTransactionUse createSessionActionRoute, which signs and broadcasts itself.
Taps fail to broadcastPublic RPC rate limitSet PEPU_RPC_URL to an authenticated endpoint.
Login hangs or returns 403Wrong port or originUse http://localhost:3000 and allow it in Privy.