Flutter加密钱包管理插件tron_wallet的使用
Flutter加密钱包管理插件tron_wallet的使用
Tron Wallet插件是一个专为Flutter应用设计的Dart库,旨在简化Tron区块链功能的集成。它提供了全面的工具集,用于与Tron网络交互,使开发者能够管理Tron账户、执行TRX和TRC20代币交易、检索区块链数据并执行智能合约功能。该插件非常适合构建基于Tron的钱包、去中心化应用程序(DApps)或其他区块链相关的Flutter项目。
特性
账户管理
- 在指定的网络上创建Tron账户(例如Nile测试网、主网)。
- 验证Tron钱包地址。
- 获取详细的账户信息,包括余额和交易历史记录。
TRX交易
- 向其他Tron钱包地址发送TRX。
- 将TRX金额转换为Sun(Tron的最小单位)。
TRC20代币支持
- 在钱包之间转移TRC20代币。
- 与TRC20智能合约交互,包括参数编码和合约执行。
智能合约交互
- 使用自定义参数触发智能合约。
- 签名并广播智能合约交易。
区块链数据访问
- 获取Tron地址的余额。
- 使用交易哈希获取特定交易的详细信息。
- 获取超级代表(SR)列表并管理SR投票。
资源管理
- 冻结和解冻TRX以分配资源(例如带宽或能量)。
- 管理超级代表投票和提案。
错误处理
- 对Tron地址进行内置验证。
- 提供全面的错误处理和标准化的成功/错误消息格式。
自定义网络支持
- 轻松切换Tron网络,包括Nile测试网、主网和Shasta测试网。
- 动态获取网络配置详情。
入门指南
body: Tronwallet(childwidget: )
将应用或组件包装在Tronwallet
中,然后可以访问TronwalletAction
方法。
使用示例
创建新账户
/// 获取Tron链上的新账户,使用私钥。
await TronwalletAction.createAccount().then((value) {
print("Response $value");
// 成功: {"Response": *******}
// 错误: {"Error": *******}
});
获取账户余额
/// 获取指定地址和网络的Tron钱包余额。
await TronwalletAction.getbalance(
walletaddress: 'XXXXXXXXXXXXXXXXXXXX'
).then((value) {
// 成功: {"Response": *******}
// 错误: {"Error": *******}
});
获取账户信息
/// 获取指定钱包地址和网络的账户信息。
await TronwalletAction.getAccouninfo(
walletaddress: 'XXXXXXXXXXXXXXXXXXXXXXX'
).then((value) {
print("Response $value");
// 成功: {"Response": *******}
// 错误: {"Error": *******}
});
发送TRX
/// 向接收者的钱包地址发送TRX。
var resp = await TronwalletAction.SendTrx(
ReceiverAddress: '************************',
amountinSun: TronwalletAction.getSun(num.parse('x')),
privatekey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
resp:
// 成功: {"Response": *******}
// 错误: {"Error": *******}
发送TRC20代币
/// 向接收者的钱包地址发送TRC20代币。
var resp = await TronwalletAction.SendTRC20Token(
ReceiverAddress: 'xxxxxxxxxxxxxxxxxxxxxx',
ContractAddresss: 'xxxxxxxxxxxxxxxxxxxxx',
privatekey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
OwnerAddress: 'xxxxxxxxxxxxxxxxxxxxxxxx',
amountInContactType: TronwalletAction.getSun(num.parse('x'))
);
获取交易信息
/// 根据交易哈希获取交易信息。
var resp = await TronwalletAction.getTransactionInfo(
hash: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
投票给超级代表
/// 投票给网络上的超级代表。
var resp = await TronwalletAction.vote(
Owneraddress: 'xxxxxxxxxxxxxxxxxxxxxxxxx',
votes: {"****************": 0},
privatekey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
其他信息
其他方法还包括:
FreezeBalance
和UNFreezeBalance
(用于带宽和能量)。Apply SR
,Get SRList
,Vote for SR
。voteProposal
,DeleteProposal
。
完整示例代码
以下是完整的示例代码,展示了如何使用tron_wallet
插件进行各种操作:
import 'package:flutter/material.dart';
import 'package:tron_wallet/tron_wallet.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Tron wallet',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Tron wallet'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Tronwallet(
childwidget: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
'Tronwallet Example',
),
const SizedBox(
height: 30,
),
Clickbutton('生成账户', () async {
await TronwalletAction.createAccount().then((value) {
print("Response $value");
// 成功: {"Response": *******}
// 错误: {"Error": *******}
});
}),
Clickbutton('查询账户余额', () async {
await TronwalletAction.getbalance(
walletaddress: 'TPcdhwnZciir3gz3WYiNdeuYorLn9xQqeK'
).then((value) {
// 成功: {"Response": *******}
// 错误: {"Error": *******}
});
}),
Clickbutton('查询账户信息', () async {
await TronwalletAction.getAccouninfo(
walletaddress: 'TPcdhwnZciir3gz3WYiNdeuYorLn9xQqeK'
).then((value) {
print("Response $value");
// 成功: {"Response": *******}
// 错误: {"Error": *******}
});
}),
Clickbutton('发送TRX', () async {
var resp = await TronwalletAction.SendTrx(
ReceiverAddress: '************************',
amountinSun: TronwalletAction.getSun(num.parse('x')),
privatekey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
// 成功: {"Response": *******}
// 错误: {"Error": *******}
}),
Clickbutton('发送TRC20代币', () async {
var resp = await TronwalletAction.SendTRC20Token(
ReceiverAddress: 'xxxxxxxxxxxxxxxxxxxxxx',
ContractAddresss: 'xxxxxxxxxxxxxxxxxxxxx',
privatekey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
OwnerAddress: 'xxxxxxxxxxxxxxxxxxxxxxxx',
amountInContactType: TronwalletAction.getSun(num.parse('x'))
);
// 成功: {"Response": *******}
// 错误: {"Error": *******}
}),
Clickbutton('查询交易信息', () async {
var resp = await TronwalletAction.getTransactionInfo(
hash: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
// 成功: {"Response": *******}
// 错误: {"Error": *******}
}),
Clickbutton('投票', () async {
var resp = await TronwalletAction.vote(
Owneraddress: 'xxxxxxxxxxxxxxxxxxxxxxxxx',
votes: {"****************": 0},
privatekey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
// 成功: {"Response": *******}
// 错误: {"Error": *******}
}),
],
),
),
),
);
}
Widget Clickbutton(String title, void Function()? onPressed) {
return ElevatedButton(onPressed: onPressed, child: Text(title));
}
}
更多关于Flutter加密钱包管理插件tron_wallet的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter加密钱包管理插件tron_wallet的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中管理加密钱包,特别是与TRON区块链交互,可以使用tron_wallet
插件。这个插件提供了一系列功能来创建、导入、导出和管理TRON钱包,以及与TRON区块链进行交互。以下是如何使用tron_wallet
插件的基本指南。
1. 添加依赖
首先,你需要在pubspec.yaml
文件中添加tron_wallet
插件的依赖:
dependencies:
flutter:
sdk: flutter
tron_wallet: ^0.1.0 # 请使用最新版本
然后运行flutter pub get
来获取依赖。
2. 导入插件
在你的Dart文件中,导入tron_wallet
插件:
import 'package:tron_wallet/tron_wallet.dart';
3. 创建新钱包
你可以使用TronWallet
类来创建一个新的TRON钱包:
void createNewWallet() async {
TronWallet wallet = TronWallet.create();
print("Address: ${wallet.address}");
print("Private Key: ${wallet.privateKey}");
print("Mnemonic: ${wallet.mnemonic}");
}
4. 导入钱包
你可以通过私钥或助记词导入现有的钱包:
通过私钥导入钱包
void importWalletFromPrivateKey(String privateKey) async {
TronWallet wallet = TronWallet.fromPrivateKey(privateKey);
print("Address: ${wallet.address}");
}
通过助记词导入钱包
void importWalletFromMnemonic(String mnemonic) async {
TronWallet wallet = TronWallet.fromMnemonic(mnemonic);
print("Address: ${wallet.address}");
}
5. 获取钱包余额
你可以通过钱包地址获取TRX余额:
void getBalance(String address) async {
TronWallet wallet = TronWallet.fromAddress(address);
BigInt balance = await wallet.getBalance();
print("Balance: $balance TRX");
}
6. 发送交易
你可以使用钱包发送TRX或代币交易:
发送TRX
void sendTrx(String toAddress, BigInt amount) async {
TronWallet wallet = TronWallet.fromPrivateKey('your-private-key');
String txId = await wallet.sendTrx(toAddress, amount);
print("Transaction ID: $txId");
}
发送代币
void sendToken(String contractAddress, String toAddress, BigInt amount) async {
TronWallet wallet = TronWallet.fromPrivateKey('your-private-key');
String txId = await wallet.sendToken(contractAddress, toAddress, amount);
print("Transaction ID: $txId");
}
7. 获取交易历史
你可以获取指定地址的交易历史:
void getTransactionHistory(String address) async {
TronWallet wallet = TronWallet.fromAddress(address);
List<Transaction> transactions = await wallet.getTransactionHistory();
transactions.forEach((tx) {
print("Tx ID: ${tx.txId}, Amount: ${tx.amount}");
});
}
8. 获取代币余额
你可以获取指定地址的代币余额:
void getTokenBalance(String contractAddress, String address) async {
TronWallet wallet = TronWallet.fromAddress(address);
BigInt balance = await wallet.getTokenBalance(contractAddress);
print("Token Balance: $balance");
}