WalletConnect-TRON
什么是 walletconnect-tron
walletconnect-tron
用来帮助 DApp 通过 WalletConnect 连接到波场链(TRON)。
开始
安装
你可以通过 npm/yarn/pnpm
安装@tronweb3/walletconnect-tron
。
npm i @tronweb3/walletconnect-tron
//or
yarn add @tronweb3/walletconnect-tron
创建 WalletConnect 钱包
请求参数
参数 | 描述 | 类型 |
---|---|---|
network | the Chain (Mainnet, Shasta, Nile) | string |
options | The options Info of the wallet | object |
例子
import { WalletConnectWallet, WalletConnectChainID } from '@tronweb3/walletconnect-tron';
const wallet = new WalletConnectWallet({
network: WalletConnectChainID.Mainnet,
options: {
relayUrl: 'wss://relay.walletconnect.com',
projectId: '....',
metadata: {
name: 'JustLend',
description: 'JustLend WalletConnect',
url: 'https://app.justlend.org/',
icons: ['https://app.justlend.org/mainLogo.svg']
}
}
});
连接钱包
用 wallet.connect()
方法去连接钱包。如果 DApp 以前连接过钱包,则会直接连接,否则会显示 WalletConnect 连接的二维码。
返回值
如果连接成功返回钱包地址。
例子
const { address } = await wallet.connect();
断开钱包连接
用 wallet.disconnect()
方法断开钱包连接
例子
try {
await wallet.disconnect();
} catch (error) {
console.log('disconnect:' + error);
}
签名交易
签名一个交易对象
请求参数
参数 | 描述 | 类型 |
---|---|---|
transation | Tron Transaction | object |
例子
try {
const signature = await wallet.signTransaction(transaction);
} catch (error) {
console.log('signTransaction:' + error);
}
签名字符串
签名一个字符串
请求参数
参数 | 描述 | 类型 |
---|---|---|
message | message | string |
例子
try {
const signature = await wallet.signMessage('hello world');
} catch (error) {
console.log('signTransaction:' + error);
}
查看连接状态
查看钱包的连接状态
返回值
如果钱包已经连接,返回钱包地址,否则返回为空。
例子
const { address } = await wallet.checkConnectStatus();
注意事项
因为连接是基于 relayUrl 服务的,DApp 开发者应该根据场景合理处理网络错误、连接错误和超时错误等。
WalletConnect 的错误信息和错误代码,请参考WalletConnect Errors。
License
MIT
Updated 6 months ago