@cardano-foundation/cardano-connect-with-wallet

中文文档
0.2.22发布于 2026/05/16Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 3874

This package aims to provide useful hooks and React components to simplify the cardano dapp integration

原始使用文档
文档目录9 节
  1. 🚀 Getting Started
  2. 🎨 Components
  3. ConnectWalletButton
  4. ConnectWalletList
  5. 🪝 useCardano Hook
  6. 📱 Mobile Wallets
  7. 🔗 CIP-45: P2P Wallet Connection
  8. ℹ️ SSR with Next.js
  9. 💪 Contributing

Cardano Connect with Wallet - React

Tests Release conventionalcommits Discord

React hooks and components for connecting Cardano wallets to dApps. Supports desktop browser extensions and mobile native wallets with built-in CIP-8, CIP-30, CIP-45, and CIP-158 support.

🚀 Getting Started

npm i @cardano-foundation/cardano-connect-with-wallet

For live examples see the storybook playground.

🎨 Components

ConnectWalletButton

A single button that opens a dropdown listing available wallets. On mobile it handles deep links and app store redirects automatically.

import { ConnectWalletButton } from '@cardano-foundation/cardano-connect-with-wallet';

<ConnectWalletButton
  supportedWallets={['Eternl', 'Nami', 'Yoroi', 'Vespr', 'Begin']}
  onConnect={(walletName) => console.log('connected:', walletName)}
  onDisconnect={() => console.log('disconnected')}
/>

Props

PropTypeDefaultDescription
labelstring | ReactNode'Connect Wallet'Button label when no wallet is connected
supportedWalletsstring[]Eternl, Nami, Yoroi...Wallets shown in the dropdown
showUnavailableWalletsUnavailableWalletVisibilitySHOW_UNAVAILABLE_ON_MOBILEControls which wallets are listed
alwaysVisibleWalletsstring[][]Wallets always shown regardless of install state
primaryColorstring'#0538AF'Accent color for the button and menu
borderRadiusnumber15Border radius in pixels
showAccountBalancebooleanfalseShow ADA balance instead of stake address
limitNetworkNetworkType—Restrict to MAINNET or TESTNET
peerConnectEnabledbooleantrueShow the CIP-45 P2P QR option
cip158EnabledbooleantrueUse CIP-158 deep links for supported mobile wallets
dAppNamestring'Awesome DApp'dApp name shown in the CIP-45 QR dialog
dAppUrlstring—dApp URL used for CIP-45 peer identity
extensionsnumber[]—CIP extension numbers to request on connect (e.g. [95])
messagestring—If set, adds a "Sign message" action when connected
customActionsAction[][]Extra menu items shown when connected
hideActionMenubooleanfalseHide the action dropdown when connected
customCSSstring—Extra CSS applied to the outer wrapper
onConnect(walletName: string) => void—Called after a successful connection
onDisconnect() => void—Called after disconnect
onSignMessage(signature, key) => void—Called after a successful message signature
onConnectError(walletName, error, level) => voidalertCalled when connection fails

ConnectWalletList

A flat list of wallet buttons, useful when you want to embed the wallet picker inline rather than inside a dropdown.

import { ConnectWalletList } from '@cardano-foundation/cardano-connect-with-wallet';

<ConnectWalletList
  borderRadius={15}
  gap={12}
  primaryColor="#0538AF"
  onConnect={onConnectWallet}
  customCSS={`
    font-family: Helvetica Light, sans-serif;
    font-size: 0.875rem;
    font-weight: 700;
    width: 164px;
    & > span { padding: 5px 16px; }
  `}
/>

Accepts the same props as ConnectWalletButton minus label, showAccountBalance, hideActionMenu, message, customActions, beforeComponent, and afterComponent.

🪝 useCardano Hook

useCardano is the low-level hook used by both components. Use it when you need direct access to wallet state or want to build a fully custom UI.

import { useCardano } from '@cardano-foundation/cardano-connect-with-wallet';

const { 
  isEnabled,
  isConnected,
  isConnecting,
  enabledWallet,
  stakeAddress,
  usedAddresses,
  unusedAddresses,
  accountBalance,
  installedExtensions,
  connect,
  disconnect,
  signMessage,
} = useCardano({ limitNetwork: NetworkType.MAINNET });

// Connect by wallet name
await connect('eternl', onSuccess, onError, [{ cip: 95 }]);

// Disconnect
disconnect();

// Sign a message (CIP-8)
signMessage('Hello Cardano', (signature, key) => console.log(signature));

📱 Mobile Wallets

On mobile devices the library automatically adapts its behavior:

Wallets with CIP-158 support (Eternl, Vespr): tapping the wallet fires a web+cardano://browse/v1?uri=<encoded_url> deep link. If the wallet app is installed it opens its in-app browser with the CIP-30 API already injected. If the app is not installed the user is redirected to the App Store or Play Store after a 2.5 s timeout.

Other mobile wallets (Begin, Yoroi, Flint): tapping redirects directly to the appropriate app store if the wallet is not already injected.

To opt out of CIP-158 deep links set cip158Enabled={false} on either component.

🔗 CIP-45: P2P Wallet Connection

CIP-45 lets a mobile wallet connect to a desktop dApp by scanning a QR code. Both components show a "P2P Wallet" entry in the wallet list when peerConnectEnabled is true (the default). Clicking it opens a modal with a QR code the user scans with their mobile wallet.

<ConnectWalletButton
  peerConnectEnabled={true}
  dAppName="My dApp"
  dAppUrl="https://my-dapp.io"
  peerConnectSubtitle="Scan with a CIP-45 compatible wallet such as Eternl"
/>

ℹ️ SSR with Next.js

This library accesses window and localStorage which are not available during server-side rendering. Use dynamic imports with ssr: false for both components and the hook.

const ConnectWalletButton = dynamic(
  () =>
    import('@cardano-foundation/cardano-connect-with-wallet').then(
      (mod) => mod.ConnectWalletButton
    ),
  { ssr: false }
);

💪 Contributing

Please have a look at our contributing infos to become familiar with our guidelines. There is also a short description for our development setup as we use Storybook for testing, playing around and for supporting the development process.

版本与发布记录

@cardano-foundation/cardano-connect-with-wallet 共发布 59 个版本,首次发布于 2022/09/20;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.22latest2026/05/165 个月前
0.2.202026/05/066 个月前
0.2.192026/03/257 个月前
0.2.182026/03/257 个月前
0.2.172026/02/278 个月前
0.2.152025/09/051.2 年前

关于 @cardano-foundation/cardano-connect-with-wallet 的常见问题

@cardano-foundation/cardano-connect-with-wallet 是什么?

This package aims to provide useful hooks and React components to simplify the cardano dapp integration

如何安装 @cardano-foundation/cardano-connect-with-wallet?

在项目目录运行 npm install @cardano-foundation/cardano-connect-with-wallet,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.2.0)、react-dom(>=18.2.0)、styled-components(>=6.3.12)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cardano-foundation/cardano-connect-with-wallet 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cardano-foundation/cardano-connect-with-wallet 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cardano-foundation/cardano-connect-with-wallet 可以商用吗?

@cardano-foundation/cardano-connect-with-wallet 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cardano-foundation/cardano-connect-with-wallet 的下载量有多少?

根据 npm 官方下载统计,@cardano-foundation/cardano-connect-with-wallet 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,874 次,日均约 129 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cardano-foundation/cardano-connect-with-wallet 还在维护吗?

最新版本 0.2.22 发布于 2026/05/16(5 个月前);至今共发布 59 个版本,其中过去 12 个月发布了 5 个;首次发布于 2022/09/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。