Wallet integration engine
@blueprint/wallet — the reusable wallet layer every generated app uses: connect, network switching, balances, contract reads, user-authorized writes, transaction tracking and signed-message sign-in.
Principles#
- Never asks for a seed phrase or private key. There is no API that accepts one.
- Every write goes through the wallet's own confirmation popup. The runtime never sends a transaction on its own.
- Production Preview refuses all signatures and transactions.
- Anything produced by the simulated provider is flagged
simulated: true. - No privileged credentials in frontend code — only
PUBLIC_*environment values reach the bundle.
@blueprint/wallet is part of Blueprint App Runtime v1. Builds resolve it to Blueprint's own runtime files; exported projects get a copy in src/blueprint/, so they have no dependency on Blueprint.Runtime wallet modes#
Blueprint injects a runtime config (globalThis.__BP__) before the app bundle. The wallet mode follows the preview mode:
| Mode | Provider | Behaviour |
|---|---|---|
simulated | Built-in fake EIP-1193 provider | Blockchain Simulation Mode. Nothing touches a chain. |
parent-readonly | Proxied through the Blueprint workspace | Production Preview: real address and real reads; signing and sending are refused. |
injected | window.ethereum or an EIP-6963 announced wallet | Hosted and exported apps. Writes always open the wallet. |
none | — | Interactive Mode: wallet features report unavailable. |
Exported projects fall back to PUBLIC_BLUEPRINT_NETWORK (Vite envPrefix: "PUBLIC_") when no config is injected; the default network is Elysium (chain 99901).
React hooks#
Generated apps import everything from @blueprint/wallet. The React layer re-renders whenever the account, chain, simulation state or a tracked transaction changes.
| Hook | Returns |
|---|---|
useWallet() | status, address, chainId, isConnected, isSimulated, isReadOnly, network, walletNetwork, isCorrectNetwork, session, error, unavailableReason, plus connect, disconnect, switchNetwork, signIn, signOut. |
useNativeBalance(address?) | { data, loading, error, refetch } with { value, decimals, symbol, formatted }. |
useTokenBalance(token, { owner?, standard? }) | { data, loading, error, refetch } with { value, decimals, symbol, formatted, simulated }. standard is erc20 (default) or erc721. |
useContractRead({ address, abi, functionName, args, enabled? }) | { data, loading, error, refetch }. |
useWrite() | { write, status, hash, error, receipt, reset, isSimulated } — status is idle → awaiting-wallet → pending → confirmed | failed. |
useTransactions() | Tracked transactions: hash, label, chain id, status, simulated. |
useSignIn() | { session, signIn(statement?), signOut, busy, error }. |
Components#
| Component | What it does |
|---|---|
<ConnectButton /> | Connect / account menu with network badge; shows a Simulated tag in simulation. |
<TokenGate token minBalance standard fallback> | Renders children only for wallets holding at least minBalance (human units) of token; handles connect, loading, read errors and an unset token address. |
<TxConfirmModal open title details action onConfirmed> | Confirmation dialog around a write: shows details, opens the wallet, tracks the receipt. |
<NetworkBadge />, <SimulatedTag /> | Status labels for the app network and simulated activity. |
Components read CSS variables (--bp-primary, --bp-primary-fg, --bp-surface, --bp-surface-2, --bp-text, --bp-muted, --bp-border, --bp-radius) so they follow the app's theme in src/styles.css.
Core functions#
The hooks are built on framework-free functions that are exported too:
| Function | Returns | Notes |
|---|---|---|
connect() | Promise<Address> | eth_requestAccounts; rejections become WalletError("REJECTED"). |
disconnect() | Promise<void> | Revokes permissions where the wallet supports it, always clears local state. |
switchNetwork(target?) | Promise<void> | Network key or chain id; adds the chain to the wallet if missing. Defaults to the app network. |
getAppNetwork() | RuntimeNetwork | Name, chain id, RPC, native symbol, explorer (or null). |
getNativeBalance(address?) / getTokenBalance(token, owner?, opts) | balance or null | Default to the connected address. |
readContract({ address, abi, functionName, args }) | Promise<T> | Read-only call through the app network's public RPC (or the simulator). |
writeContract({ address, abi, functionName, args, value?, label? }) | Promise<Hex> | User-authorized write via the wallet popup; switches network first; tracked automatically. |
waitForTransaction(hash) | Promise<Receipt> | Polls for the receipt; marks the tracked tx confirmed or failed. |
signIn({ statement? }) / signOut() | WalletSession | EIP-4361-style message verified in the browser (EOA, then ERC-1271 via RPC). Proves wallet control to the UI — it is not a server session. |
publicEnv("PUBLIC_…") | string | undefined | Reads a PUBLIC_ environment value — the only kind present in a build. |
RUNTIME_NETWORKS, explorerTxUrl, explorerAddressUrl, shortAddress | helpers | Explorer helpers return null where no explorer is verified. |
Errors
Failures throw WalletError with a code:
| WalletError code | Meaning |
|---|---|
NO_WALLET | No provider in this environment (no extension, or Interactive Mode). |
NOT_CONNECTED | A write was attempted before connecting. |
READ_ONLY | Production Preview refused a signature, transaction or network switch. |
REJECTED | The user rejected the request in the wallet (EIP-1193 code 4001). |
WRONG_NETWORK | The requested network is unknown. |
FAILED | Anything else, including signatures that fail verification. |
Examples#
import { ConnectButton, TokenGate, publicEnv } from "@blueprint/wallet";
const TOKEN = publicEnv("PUBLIC_TOKEN_ADDRESS") as `0x${string}` | undefined;
export function Members() {
return (
<main className="mx-auto max-w-3xl p-6">
<ConnectButton />
<TokenGate token={TOKEN} minBalance="100">
<section>{/* exclusive member content */}</section>
</TokenGate>
</main>
);
}import { useWrite } from "@blueprint/wallet";
import { parseAbi } from "viem";
const abi = parseAbi(["function vote(uint256 proposalId, bool support)"]);
const GOVERNOR = "0x…"; // from your deployment record
export function VoteButton({ id }: { id: bigint }) {
const { write, status, error } = useWrite();
return (
<button
disabled={status === "awaiting-wallet" || status === "pending"}
onClick={() => write({ address: GOVERNOR, abi, functionName: "vote", args: [id, true], label: "Vote yes" })}
>
{status === "pending" ? "Confirming…" : "Vote yes"}
{error && <span role="alert">{error}</span>}
</button>
);
}Asking the builder for wallet features#
These prompts map directly onto the engine:
- “Add wallet authentication” →
<ConnectButton />plususeSignIn()for a signed-message session. - “Show the connected user's token balance” →
useTokenBalance()with formatting and a Simulated tag in simulation. - “Restrict this page to holders of a specified token” →
<TokenGate>reading the token address from aPUBLIC_variable. - “Add a transaction confirmation modal” →
<TxConfirmModal>around the write, showing label, network and tracked status.
