卡多诺与钱包核心连接
该寄存器旨在为Cardano分散化应用程序(dApps)和钱包(钱包)之间的无缝互动提供框架独立的帮助和包装功能CIP 8 (中文(简体) ) 财务报告和审定财务报表 CIP 30 (简体中文)) (中文(简体) ). 如果您正在寻找 React.js 组件,请继续 此寄存器。 。 。 .
玩得开心点
开始
国家防范机制一揽子计划
npm i @cardano-foundation/cardano-connect-with-wallet-core
import { Wallet } from '@cardano-foundation/cardano-connect-with-wallet-core';
浏览器导入
<script src="https://cardano-foundation.github.io/cardano-connect-with-wallet/bundle-latest/index.js"></script>
<script>
const wallet = CardanoConnectWithWalletCore.Wallet;
...
</script>
示例使用
这个 Wallet 类包含静态帮助函数来简化 dApp 开发. 请检查一下反应钩 使用卡尔德诺 以实例说明如何利用 Wallet 在基于模块的工程中。 从我们这里得到一个静态html、javascript、cs上下文中的用法 基于 html 的钱包按钮。 。 。 .
钱包模块
/*
Register a function to those events:
'enabled',
'connecting',
'enabledWallet',
'stakeAddress',
'usedAddresses',
'unusedAddresses',
'accountBalance',
'connected',
'lastConnectedWallet',
'meerkatAddress',
'installedWalletExtensions'
*/
Wallet.addEventListener: void;
Wallet.removeEventListener: void;
// Starts a background thread to listen for new wallets in the window.cardano object
Wallet.startInjectWalletListener(): void;
Wallet.stopInjectWalletListener(): void;
// Connect or disconnect a wallet by name e.g. 'yoroi', 'eternl', 'flint', etc.
Wallet.connectToWallet(walletName: string, networkType: NetworkType, retries?: number, retryIntervalInMs?: number): Promise<void>;
Wallet.connect(walletName: string, network: NetworkType, onConnect?: () => void | undefined, onError?: (code: Error) => void): Promise<void>;
Wallet.disconnect(): void;
Wallet.checkEnabled(network: NetworkType): Promise<void>;
Wallet.getInstalledWalletExtensions(supportedWallets?: Array<string>): Array<string>;
Wallet.getRewardAddresses(): Promise<any>;
// sign a message
Wallet.signMessage(message: string, onSignMessage?: (signature: string, key: string | undefined) => void, onSignError?: (error: Error) => void, limitNetwork?: NetworkType): Promise<void>;
移动钱包支持
核心包会导出帮助工具,用于移动钱包检测和深层连接.
import {
checkIsMobile,
mobileWallets,
nativeWallets,
generateCip158DeepLink,
} from '@cardano-foundation/cardano-connect-with-wallet-core';
// true when running on a mobile device (Android / iOS)
const isMobile = checkIsMobile();
// list of wallet names that have a native mobile app
// ['flint', 'eternl', 'vespr', 'begin', 'yoroi']
console.log(mobileWallets);
// per-wallet metadata including app store URLs and CIP-158 support flag
// {
// eternl: { playStoreUrl, appStoreUrl, walletName, hasCIP158Support: true },
// vespr: { playStoreUrl, appStoreUrl, walletName, hasCIP158Support: true },
// begin: { playStoreUrl, appStoreUrl, walletName, hasCIP158Support: false },
// yoroi: { playStoreUrl, appStoreUrl, walletName, hasCIP158Support: false },
// }
console.log(nativeWallets);
CIP-158 深层链接
CIP-158 (中文(简体) ) 定义 a web+cardano://browse/v1?uri=<encoded_url> 深链接打开钱包内应用浏览器内的当前dApp页面,让钱包的CIP-30 API直接访问页面.
// Generates the CIP-158 deep link for the given URL (defaults to window.location.href)
const deepLink = generateCip158DeepLink('https://my-dapp.io/swap');
// → 'web+cardano://browse/v1?uri=https%3A%2F%2Fmy-dapp.io%2Fswap'
window.location.href = deepLink;
钱包 hasCIP158Support 旗帜是 true (目前为 永远 财务报告和审定财务报表 维斯普) 由 React 组件自动提供此流后再返回应用商店重定向.
钱包供应设施
import {
estimateAvailableWallets,
UnavailableWalletVisibility,
getWalletIcon,
isWalletInstalled,
formatSupportedWallets,
} from '@cardano-foundation/cardano-connect-with-wallet-core';
// Returns the filtered list of wallets to display
const wallets = estimateAvailableWallets(
['Eternl', 'Nami', 'Yoroi'],
UnavailableWalletVisibility.SHOW_UNAVAILABLE_ON_MOBILE, // default
[], // alwaysVisibleWallets
installedExtensions,
);
// UnavailableWalletVisibility options:
// HIDE_UNAVAILABLE – show only installed wallets
// SHOW_UNAVAILABLE – show all wallets regardless of device
// SHOW_UNAVAILABLE_ON_MOBILE – show all on mobile, only installed on desktop
// Returns a base64 data-URI icon for the given wallet name
const icon = getWalletIcon('eternl');
// Returns true when window.cardano[walletName] exists
const installed = isWalletInstalled('eternl');
// Returns a human-readable comma-separated list e.g. "Eternl, Nami, Yoroi"
const label = formatSupportedWallets(['Eternl', 'Nami', 'Yoroi']);
错误分类
所有错误都由 Wallet 分类被打入并可以单独捕获:
import {
WalletConnectError,
WrongNetworkTypeError,
WalletNotCip30CompatibleError,
WalletNotInstalledError,
WalletExtensionNotFoundError,
ExtensionNotInjectedError,
EnablementFailedError,
} from '@cardano-foundation/cardano-connect-with-wallet-core';
try {
await Wallet.connectToWallet('eternl', NetworkType.MAINNET);
} catch (e) {
if (e instanceof WrongNetworkTypeError) {
// wallet is on a different network (e.g. testnet vs mainnet)
} else if (e instanceof WalletExtensionNotFoundError) {
// wallet extension / app not available in the browser
}
}
类型( E)
import {
NetworkType, // MAINNET | TESTNET
UnavailableWalletVisibility,
Extension, // { cip: number }
Cip95Api, // governance extension (CIP-95)
Cip103Api, // bulk transaction signing (CIP-103)
} from '@cardano-foundation/cardano-connect-with-wallet-core';
捐款
请看看我们 提供信息 熟悉我们的准则.