助记词转换
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import 'package:chat/middleware/auth_middleware.dart';
|
||||
import 'package:chat/views/auth/index_page.dart';
|
||||
import 'package:chat/views/auth/create/index_page.dart';
|
||||
import 'package:chat/views/auth/create/verify_page.dart';
|
||||
import 'package:chat/views/auth/import/index_page.dart';
|
||||
import 'package:chat/views/auth/index/index_page.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
abstract class AuthRoutes {
|
||||
/// 身份验证页面
|
||||
static const String index = '/auth';
|
||||
static const String create = '/auth/create';
|
||||
static const String createVerify = '/auth/create/verify';
|
||||
static const String import = '/auth/import';
|
||||
|
||||
static GetPage router = GetPage(
|
||||
name: AuthRoutes.index,
|
||||
@@ -12,5 +18,21 @@ abstract class AuthRoutes {
|
||||
EnsureNotAuthMiddleware(),
|
||||
],
|
||||
page: () => const AuthPage(),
|
||||
children: [
|
||||
GetPage(
|
||||
name: '/create',
|
||||
page: () => const AuthCreatePage(),
|
||||
children: [
|
||||
GetPage(
|
||||
name: '/verify',
|
||||
page: () => const AuthCreateVerifyPage(),
|
||||
),
|
||||
],
|
||||
),
|
||||
GetPage(
|
||||
name: '/import',
|
||||
page: () => const AuthImportPage(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
47
lib/utils/hd_wallet.dart
Normal file
47
lib/utils/hd_wallet.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:bip32/bip32.dart' as bip32;
|
||||
import 'package:bip39_multi_language/bip39.dart' as bip39;
|
||||
import 'package:chat/utils/ui_tools.dart';
|
||||
import 'package:fast_base58/fast_base58.dart';
|
||||
import 'package:hash/hash.dart';
|
||||
|
||||
class HDWallet {
|
||||
/// 将助记词转换成BTY地址
|
||||
static String? mnemonicToAddress(String mnemonic) {
|
||||
var li = mnemonic.split(' ');
|
||||
li.removeWhere((e) => e.isEmpty);
|
||||
mnemonic = li.join(' ');
|
||||
|
||||
if (!bip39.validateMnemonic(
|
||||
mnemonic,
|
||||
language: _languageDetect('mnemonic'),
|
||||
)) {
|
||||
UiTools.toast('不符合标准的助记词');
|
||||
return null;
|
||||
}
|
||||
|
||||
var seed = bip39.mnemonicToSeed(mnemonic);
|
||||
var root = bip32.BIP32.fromSeed(seed);
|
||||
var child = root.derivePath("m/44'/13107'/0'/0/0");
|
||||
var step3 = SHA256().update(child.publicKey).digest();
|
||||
List<int> step4 = List.from(RIPEMD160().update(step3).digest());
|
||||
step4.insert(0, 0x00);
|
||||
List<int> step5 = step4;
|
||||
Uint8List step6 = SHA256().update(step5).digest();
|
||||
Uint8List step7 = SHA256().update(step6).digest();
|
||||
step5.addAll(step7.sublist(0, 4));
|
||||
Uint8List step8 = Uint8List.fromList(step5);
|
||||
|
||||
return Base58Encode(step8);
|
||||
}
|
||||
|
||||
/// 判断是否是英文助记词
|
||||
static String _languageDetect(String str) {
|
||||
if (str.startsWith(RegExp(r'[a-z]'))) {
|
||||
return 'english';
|
||||
} else {
|
||||
return 'chinese';
|
||||
}
|
||||
}
|
||||
}
|
||||
15
lib/utils/ui_tools.dart
Normal file
15
lib/utils/ui_tools.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
import 'package:chat/configs/app_colors.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
|
||||
class UiTools {
|
||||
/// 普通的消息提醒
|
||||
static void toast(String message) {
|
||||
Fluttertoast.showToast(
|
||||
msg: message,
|
||||
gravity: ToastGravity.CENTER,
|
||||
toastLength: Toast.LENGTH_SHORT,
|
||||
backgroundColor: AppColors.active.withAlpha(0x90),
|
||||
fontSize: 14,
|
||||
);
|
||||
}
|
||||
}
|
||||
172
lib/views/auth/create/index_page.dart
Normal file
172
lib/views/auth/create/index_page.dart
Normal file
@@ -0,0 +1,172 @@
|
||||
import 'package:bip39_multi_language/bip39.dart' as bip39;
|
||||
import 'package:chat/configs/app_colors.dart';
|
||||
import 'package:chat/routes/auth_routes.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class AuthCreatePage extends StatefulWidget {
|
||||
const AuthCreatePage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AuthCreatePage> createState() => _AuthCreatePageState();
|
||||
}
|
||||
|
||||
class _AuthCreatePageState extends State<AuthCreatePage>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final TabController _tabController;
|
||||
|
||||
List<String> _englishMnemonic = [];
|
||||
List<String> _chineseMnemonic = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(
|
||||
length: 2,
|
||||
vsync: this,
|
||||
);
|
||||
_generateMnemonic();
|
||||
}
|
||||
|
||||
void _generateMnemonic() {
|
||||
String _englishBip39Str = bip39.generateMnemonic(language: 'english');
|
||||
String _chineseBip39Str = bip39.generateMnemonic(language: 'chinese');
|
||||
|
||||
setState(() {
|
||||
_englishMnemonic = _englishBip39Str.split(' ');
|
||||
_chineseMnemonic = _chineseBip39Str.split(' ');
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('备份助记词'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {},
|
||||
child: const Text('跳过备份'),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
TabBar(
|
||||
controller: _tabController,
|
||||
isScrollable: true,
|
||||
indicatorColor: AppColors.primary,
|
||||
indicatorWeight: 5,
|
||||
indicatorSize: TabBarIndicatorSize.label,
|
||||
tabs: const [
|
||||
Tab(
|
||||
text: 'English',
|
||||
),
|
||||
Tab(
|
||||
text: '中文',
|
||||
),
|
||||
],
|
||||
),
|
||||
const Padding(
|
||||
padding: EdgeInsets.only(right: 8.0),
|
||||
child: Text(
|
||||
'请务必抄下助记词,确定之后将进行校验',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
_moArea(_englishMnemonic),
|
||||
_moArea(_chineseMnemonic),
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
height: Get.height * 0.382,
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: const Text(
|
||||
'提示:请勿截图!如果有人获取您的助记词将直接获取您的资产,请抄写助记词并存放在安全的地方,我们会在下一屏幕进行校验,',
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
_generateMnemonic();
|
||||
},
|
||||
child: const Text('更换助记词'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
var language = _tabController.index;
|
||||
Get.toNamed(
|
||||
AuthRoutes.createVerify,
|
||||
arguments: {
|
||||
'language': language == 0 ? 'english' : 'chinese',
|
||||
'mnemonic':
|
||||
language == 0 ? _englishMnemonic : _chineseMnemonic
|
||||
},
|
||||
);
|
||||
},
|
||||
child: const Text('开始备份'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _moArea(List<String> mnemonic) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: GridView.builder(
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
childAspectRatio: 4.5 / 1,
|
||||
crossAxisSpacing: 8,
|
||||
mainAxisSpacing: 8,
|
||||
),
|
||||
itemCount: mnemonic.length,
|
||||
itemBuilder: (_, i) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: AppColors.border,
|
||||
width: 1,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'${i + 1}. ${mnemonic[i]}',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
65
lib/views/auth/create/verify_page.dart
Normal file
65
lib/views/auth/create/verify_page.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:chat/configs/app_colors.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class AuthCreateVerifyPage extends StatefulWidget {
|
||||
const AuthCreateVerifyPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_AuthCreateVerifyPageState createState() => _AuthCreateVerifyPageState();
|
||||
}
|
||||
|
||||
class _AuthCreateVerifyPageState extends State<AuthCreateVerifyPage> {
|
||||
late final List<String> _mnemonicList;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
Get.arguments['language'];
|
||||
var m = Get.arguments['mnemonic'] as List<String>;
|
||||
m.shuffle();
|
||||
_mnemonicList = m;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('校验助记词'),
|
||||
),
|
||||
body: _moArea(_mnemonicList),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _moArea(List<String> mnemonic) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: GridView.builder(
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
childAspectRatio: 4.5 / 1,
|
||||
crossAxisSpacing: 8,
|
||||
mainAxisSpacing: 8,
|
||||
),
|
||||
itemCount: mnemonic.length,
|
||||
itemBuilder: (_, i) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: AppColors.border,
|
||||
width: 1,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'${i + 1}. ${mnemonic[i]}',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
58
lib/views/auth/import/index_page.dart
Normal file
58
lib/views/auth/import/index_page.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
import 'package:chat/configs/app_colors.dart';
|
||||
import 'package:chat/utils/hd_wallet.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AuthImportPage extends StatefulWidget {
|
||||
const AuthImportPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AuthImportPage> createState() => _AuthImportPageState();
|
||||
}
|
||||
|
||||
class _AuthImportPageState extends State<AuthImportPage> {
|
||||
final TextEditingController _editingController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_editingController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('导入账户'),
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: _editingController,
|
||||
maxLines: 4,
|
||||
decoration: const InputDecoration(
|
||||
hintText: '请输入您的助记词',
|
||||
border: OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: AppColors.border,
|
||||
width: 0.4,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Text('支持导入所有遵循BIP标准生成的助记词'),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
HDWallet.mnemonicToAddress(_editingController.text);
|
||||
},
|
||||
child: const Text('开始导入'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
37
lib/views/auth/index/index_page.dart
Normal file
37
lib/views/auth/index/index_page.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:chat/routes/auth_routes.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class AuthPage extends StatefulWidget {
|
||||
const AuthPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AuthPage> createState() => _AuthPageState();
|
||||
}
|
||||
|
||||
class _AuthPageState extends State<AuthPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('ZH-CHAT'),
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Get.toNamed(AuthRoutes.create);
|
||||
},
|
||||
child: const Text('创建账户'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Get.toNamed(AuthRoutes.import);
|
||||
},
|
||||
child: const Text('导入账户'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AuthPage extends StatefulWidget {
|
||||
const AuthPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AuthPage> createState() => _AuthPageState();
|
||||
}
|
||||
|
||||
class _AuthPageState extends State<AuthPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container();
|
||||
}
|
||||
}
|
||||
@@ -15,12 +15,24 @@ class TransitPage extends StatefulWidget {
|
||||
|
||||
/// 这里的加载图片,应该是可以请求网络图片的,但是要考虑网络图片的加载周期,还有网络环境因素等
|
||||
class _TransitPageState extends State<TransitPage> {
|
||||
final int _leftTime = 5;
|
||||
int _leftTime = 5;
|
||||
late Timer _timer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_leftTime--;
|
||||
});
|
||||
}
|
||||
if (_leftTime <= 0) {
|
||||
timer.cancel();
|
||||
_jumpToRootPage();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _jumpToRootPage() {
|
||||
|
||||
Reference in New Issue
Block a user