flutter-区块链,创建助记词,助记词创建多链ETH、BSC、TRX
基于区块链的多链操作应用相当广泛的今天,掌握好助记词生成的多链成为必要.
下面简单介绍一款flutter平台自动生成助记词,并根据助记词生成多链信息的插件lj_web3_tron_sdk,
支持:
1、助记词创建
2、助记词生成ETH、BSC、TRX的Token
3、生成ETH、BSC、TRX链
4、生成ETH、BSC、TRX链相关children
一、导入插件
lj_web3_tron_sdk: ^1.0.0
二、 具体功能
import 'package:lj_web3_tron_sdk/lj_web3_tron_sdk.dart';
import 'package:lj_web3_tron_sdk/src/models/chain_model.dart';
声明
// 钱包
Web3Model token = Web3Model();
// 代币
ChainModel chain = ChainModel();
创建助记词
Web3TronSDK.generateMnemonic();
创建多链- ETH
Web3TronSDK.getToken(token.mnemonic, type: ChainTypes.eth);
获取对应链的对象
await Web3TronSDK.getChainModel(ChainTypes.eth);
三、完整代码
import 'package:flutter/material.dart';
import 'package:lj_web3_tron_sdk/lj_web3_tron_sdk.dart';
// ignore: implementation_imports
import 'package:lj_web3_tron_sdk/src/models/chain_model.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: '钱包创建'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// 钱包
Web3Model token = Web3Model();
// 代币
ChainModel chain = ChainModel();
@override
void initState() {
super.initState();
token.mnemonic = Web3TronSDK.generateMnemonic();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
iconView(),
mnemonicView(),
nameView(),
pathView(),
privateKeyView(),
addressView(),
Expanded(child: _coinView()),
// const Spacer(),
_botomView()
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Widget _coinView(){
if (chain.icon.isEmpty) return Container();
return ListView.builder(itemBuilder: ((context, index) {
return ListTile(
leading: Image.asset(chain.symbols[index].icon, width: 30,height: 30,),
title: Text(chain.symbols[index].name),
subtitle: Text(chain.symbols[index].contractAddress),
);
}),
itemCount: chain.symbols.length,);
}
Widget iconView(){
if (chain.icon.isEmpty) return Container();
return Container(
padding: const EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(width: 80,child: Text("logo:"),),
Image.asset(chain.icon)
],),
);
}
Widget privateKeyView(){
if (token.privateKey.isEmpty) return Container();
return Container(
padding: const EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(width: 80,child: Text("私钥:"),),
Expanded(child: Text(token.privateKey, maxLines: 2,style: const TextStyle(fontWeight: FontWeight.w600),))
],),
);
}
Widget pathView(){
if (token.path.isEmpty) return Container();
return Container(
padding: const EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(width: 80,child: Text("path:"),),
Expanded(child: Text(token.path, maxLines: 2,style: const TextStyle(fontWeight: FontWeight.w600),))
],),
);
}
Widget addressView(){
if (token.address.isEmpty) return Container();
return Container(
padding: const EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(width: 80,child: Text("地址:"),),
Expanded(child: Text(token.address, maxLines: 2,style: const TextStyle(fontWeight: FontWeight.w600),))
],),
);
}
Widget nameView(){
if (token.name.isEmpty) return Container();
return Container(
padding: const EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(width: 80,child: Text("名称:"),),
Expanded(child: Text(token.name, maxLines: 2,style: const TextStyle(fontWeight: FontWeight.w600),))
],),
);
}
Widget mnemonicView(){
return Container(
padding: const EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(width: 80,child: Text("助记词:"),),
Expanded(child: Text(token.mnemonic, maxLines: 2,style: const TextStyle(fontWeight: FontWeight.w600),))
],),
);
}
Widget _botomView() {
return Container(
margin: const EdgeInsets.only(bottom: 50),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TextButton(
onPressed: () async {
token =
Web3TronSDK.getToken(token.mnemonic, type: ChainTypes.eth);
chain = await Web3TronSDK.getChainModel(ChainTypes.eth);
setState(() {});
},
child: Container(
width: 100,
height: 36,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.purple.withAlpha(200),
borderRadius: const BorderRadius.all(Radius.circular(8))),
child: const Text(
"ETH",
style: TextStyle(color: Colors.white),
),
)),
TextButton(
onPressed: () async {
token =
Web3TronSDK.getToken(token.mnemonic, type: ChainTypes.bsc);
chain = await Web3TronSDK.getChainModel(ChainTypes.bsc);
setState(() {});
},
child: Container(
width: 100,
height: 36,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.purple.withAlpha(200),
borderRadius: const BorderRadius.all(Radius.circular(8))),
child: const Text(
"BSC",
style: TextStyle(color: Colors.white),
),
)),
TextButton(
onPressed: () async {
token =
Web3TronSDK.getToken(token.mnemonic, type: ChainTypes.trx);
chain = await Web3TronSDK.getChainModel(ChainTypes.trx);
setState(() {});
},
child: Container(
width: 100,
height: 36,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.purple.withAlpha(200),
borderRadius: const BorderRadius.all(Radius.circular(8))),
child: const Text(
"TRX",
style: TextStyle(color: Colors.white),
),
)),
],
),
);
}
}
四、预览效果
ETH链
BSC链
TRX链
五、支持
目前仅支持ETH、BSC、TRX三条链, 后续会陆续更新其他链