关于 meshjs-cardano
meshjs-cardano 是面向使用 TypeScript 或 JavaScript 和 MeshJS 构建 Cardano dApp 的技能,涵盖钱包连接、交易构建、脚本交互与安全实践。
主要功能
- 01CIP-30 钱包连接模式
- 02ADA 与代币交易构建
- 03交易元数据支持
- 04Plutus 脚本交互
- 05UTxO 查询与 NFT 铸造指导
- 06钱包安全与网络检查
适用场景
- 01为 React dApp 连接 CIP-30 钱包
- 02构建 ADA 或原生代币交易
- 03与 Plutus 脚本交互
- 04查询地址 UTxO 或规划 NFT 铸造
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ flux-point-studios/ cardano-agent-skills --skill meshjs-cardano安装后在 Claude Code 中输入/meshjs-cardano确认已加载。
附带文件reference/meshjs-patterns.md
详细介绍
meshjs-cardano
适用场景
- 使用 MeshJS(TypeScript/JavaScript)构建 Cardano dApp
- 集成 CIP-30 钱包连接器
- 在浏览器或 Node.js 中构建交易
操作规则(必须遵守)
- 确认 MeshJS 版本和环境(Next.js、Vite、Node)
- 绝不索取助记词或私钥
- 妥善处理钱包断开连接
- 在主网前先在预生产网测试
设置
安装
npm install @meshsdk/core @meshsdk/react
# or
yarn add @meshsdk/core @meshsdk/react
Provider 设置
import { BlockfrostProvider } from '@meshsdk/core';
const provider = new BlockfrostProvider('<PROJECT_ID>');
// or for preprod:
const provider = new BlockfrostProvider('<PROJECT_ID>', 'preprod');
钱包连接(React)
import { CardanoWallet, useWallet } from '@meshsdk/react';
function App() {
const { connected, wallet } = useWallet();
return (
<div>
<CardanoWallet />
{connected && <p>Connected!</p>}
</div>
);
}
交易构建
简单的 ADA 转账
import { Transaction } from '@meshsdk/core';
const tx = new Transaction({ initiator: wallet })
.sendLovelace(
'addr_test1qz...',
'5000000' // 5 ADA
);
const unsignedTx = await tx.build();
const signedTx = await wallet.signTx(unsignedTx);
const txHash = await wallet.submitTx(signedTx);
转账原生代币
const tx = new Transaction({ initiator: wallet })
.sendAssets(
'addr_test1qz...',
[
{ unit: 'lovelace', quantity: '2000000' },
{ unit: '<policy_id><asset_name_hex>', quantity: '1' }
]
);
添加元数据
const tx = new Transaction({ initiator: wallet })
.sendLovelace('addr_test1qz...', '1000000')
.setMetadata(674, { msg: ['Hello from MeshJS'] });
脚本交互
在脚本地址锁定资金
import { Data } from '@meshsdk/core';
const datum: Data = {
alternative: 0,
fields: [wallet.getPaymentAddress()]
};
const tx = new Transaction({ initiator: wallet })
.sendLovelace(
{
address: scriptAddress,
datum: { inline: datum }
},
'5000000'
);
从脚本地址花费资金
const tx = new Transaction({ initiator: wallet })
.redeemValue({
value: scriptUtxo,
script: plutusScript,
datum: datum,
redeemer: { data: { alternative: 0, fields: [] } }
})
.sendLovelace(recipientAddress, '4500000')
.setCollateral([collateralUtxo]);
示例
示例:铸造 NFT
用户请求:“使用 MeshJS 铸造 NFT”
回复:
import { ForgeScript, Transaction } from '@meshsdk/core';
// Create minting script (time-locked)
const { pubKeyHash } = wallet.getPaymentAddress();
const forgingScript = ForgeScript.withOneSignature(pubKeyHash);
const assetMetadata = {
name: 'My NFT',
image: 'ipfs://...',
mediaType: 'image/png'
};
const tx = new Transaction({ initiator: wallet })
.mintAsset(
forgingScript,
{
assetName: 'MyNFT',
assetQuantity: '1',
metadata: assetMetadata,
label: '721',
recipient: wallet.getPaymentAddress()
}
);
const unsignedTx = await tx.build();
const signedTx = await wallet.signTx(unsignedTx);
const txHash = await wallet.submitTx(signedTx);
console.log('Minted:', txHash);
示例:查询 UTxO
import { BlockfrostProvider } from '@meshsdk/core';
const provider = new BlockfrostProvider('<PROJECT_ID>');
// Get UTxOs for address
const utxos = await provider.fetchAddressUTxOs(address);
// Get UTxOs at script address
const scriptUtxos = await provider.fetchAddressUTxOs(scriptAddress);
// Filter by asset
const nftUtxos = utxos.filter(utxo =>
utxo.output.amount.some(a => a.unit.includes(policyId))
);
常见模式
错误处理
try {
const txHash = await wallet.submitTx(signedTx);
console.log('Success:', txHash);
} catch (error) {
if (error.message.includes('UTxO')) {
console.log('UTxO issue - refresh and retry');
} else if (error.message.includes('collateral')) {
console.log('Need ADA-only UTxO for collateral');
}
}
钱包状态管理
const { connected, connecting, disconnect, wallet, name } = useWallet();
// Check connection before operations
if (!connected) {
throw new Error('Wallet not connected');
}
// Get network
const network = await wallet.getNetworkId();
// 0 = testnet, 1 = mainnet
安全 / 密钥处理
- 绝不索取助记词
- 验证 CIP-30 API 是否可用
- 妥善处理断开连接
- 签名前显示确认提示
参考资料
shared/PRINCIPLES.md- MeshJS 文档
- CIP-30 钱包 API
评论
支持 Markdown