FlutterBIP39助记词生成插件substrate_bip39的使用
FlutterBIP39助记词生成插件substrate_bip39的使用
简介
substrate_bip39
是一个用于从BIP39助记词派生秘密密钥的包,遵循 substrate-bip39 标准。以下是该插件的详细用法示例。
使用方法
1. 简单的BIP39助记词
import 'package:convert/convert.dart' show hex;
import 'package:substrate_bip39/substrate_bip39.dart';
void main() async {
final phrase = 'ozone drill grab fiber curtain grace pudding thank cruise elder eight picnic';
final seed = await SubstrateBip39.ed25519.seedFromUri(phrase);
print('Seed: ${hex.encode(seed)}');
assert(hex.encode(seed) == 'ae2383139b3aa529c5997ed82f62def14243fbf92bc9ef7badf72e0ff289ea82');
}
2. Substrate Junctions和密码
import 'package:convert/convert.dart' show hex;
import 'package:substrate_bip39/substrate_bip39.dart';
void main() async {
final phrase = 'vessel ladder alter error federal sibling chat ability sun glass valve picture//0///password';
// Ed25519
final ed25519Seed = await SubstrateBip39.ed25519.seedFromUri(phrase);
print('Ed25519 Seed: ${hex.encode(ed25519Seed)}');
assert(hex.encode(ed25519Seed) == 'd77ca3404e232ff317509d5da871495f9f5bade1821a84216d7070fc92c9bedb');
// Ecdsa
final ecdsaSeed = await SubstrateBip39.ecdsa.seedFromUri(phrase);
print('Ecdsa Seed: ${hex.encode(ecdsaSeed)}');
assert(hex.encode(ecdsaSeed) == 'cfacf223c22a3ce5758b07be3c16fffd47acfec5a0bb5e0d71f3c21f87e22333');
}
3. Substrate开发账户
import 'package:convert/convert.dart' show hex;
import 'package:substrate_bip39/substrate_bip39.dart';
void main() async {
final phrase = '//Alice';
final seed = await SubstrateBip39.ed25519.seedFromUri(phrase);
print('Seed: ${hex.encode(seed)}');
assert(hex.encode(seed) == 'abf8e5bdbe30c65656c0a3cbd181ff8a56294a69dfedd27982aace4a76909115');
}
4. 从熵生成512位种子
import 'package:convert/convert.dart' show hex;
import 'package:substrate_bip39/substrate_bip39.dart';
void main() async {
final entropy = hex.decode('59d1d7b63fbc9a343360a7dabc035f66d7504c549696d2b0b1ae56862911a821');
final seed = await SubstrateBip39.seedFromEntropy(entropy);
print('Seed: ${hex.encode(seed)}');
assert(hex.encode(seed) == '38b35a8cfbad9e9ca3ed4260be142b8237494aea83dee55d24f6228d4014b5736f40f51ebb8ddab08c31da0da394a0e0f68cb0e06402497e31908e942bf79d78');
}
5. 从熵生成256位种子
import 'package:convert/convert.dart' show hex;
import 'package:substrate_bip39/substrate_bip39.dart';
void main() async {
final entropy = hex.decode('59d1d7b63fbc9a343360a7dabc035f66d7504c549696d2b0b1ae56862911a821');
final seed = await SubstrateBip39.miniSecretFromEntropy(entropy);
print('Seed: ${hex.encode(seed)}');
assert(hex.encode(seed) == '38b35a8cfbad9e9ca3ed4260be142b8237494aea83dee55d24f6228d4014b573');
}
完整示例Demo
以下是一个完整的Flutter应用示例,展示了如何在Flutter项目中使用substrate_bip39
包:
import 'package:flutter/material.dart';
import 'package:convert/convert.dart';
import 'package:substrate_bip39/substrate_bip39.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'substrate_bip39 Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'substrate_bip39 Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _seed = '';
Future<void> _generateSeed() async {
final phrase = 'ozone drill grab fiber curtain grace pudding thank cruise elder eight picnic';
final seed = await SubstrateBip39.ed25519.seedFromUri(phrase);
setState(() {
_seed = hex.encode(seed);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Generated Seed:',
),
Text(
'$_seed',
style: Theme.of(context).textTheme.headline4,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _generateSeed,
child: Text('Generate Seed'),
),
],
),
),
);
}
}
这个示例应用程序包含了一个按钮,点击后会生成并显示一个基于给定助记词的种子。希望这些信息对你有所帮助!
更多关于FlutterBIP39助记词生成插件substrate_bip39的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于FlutterBIP39助记词生成插件substrate_bip39的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是如何在Flutter项目中使用substrate_bip39
插件来生成BIP39助记词的代码示例。substrate_bip39
是一个用于生成BIP39助记词的Flutter插件,基于BIP39(Bitcoin Improvement Proposals #39)标准。
步骤 1: 添加依赖
首先,你需要在pubspec.yaml
文件中添加substrate_bip39
依赖。
dependencies:
flutter:
sdk: flutter
substrate_bip39: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
步骤 2: 导入插件
在你的Dart文件中导入substrate_bip39
插件。
import 'package:substrate_bip39/substrate_bip39.dart';
步骤 3: 生成BIP39助记词
下面是一个完整的示例,展示了如何生成BIP39助记词。
import 'package:flutter/material.dart';
import 'package:substrate_bip39/substrate_bip39.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('BIP39 助记词生成'),
),
body: Center(
child: GenerateMnemonicWords(),
),
),
);
}
}
class GenerateMnemonicWords extends StatefulWidget {
@override
_GenerateMnemonicWordsState createState() => _GenerateMnemonicWordsState();
}
class _GenerateMnemonicWordsState extends State<GenerateMnemonicWords> {
String mnemonicWords = '';
@override
void initState() {
super.initState();
_generateMnemonicWords();
}
void _generateMnemonicWords() async {
try {
String result = await Bip39.generateMnemonic(strength: 256); // 你可以指定强度,比如128, 160, 192, 224, 256位
setState(() {
mnemonicWords = result;
});
} catch (e) {
print('生成助记词失败: $e');
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'生成的BIP39助记词:',
style: TextStyle(fontSize: 20),
),
Text(
mnemonicWords,
style: TextStyle(fontSize: 16),
maxLines: 10, // 根据需要调整显示的行数
overflow: TextOverflow.ellipsis,
),
],
);
}
}
注意事项
- 异步操作:
Bip39.generateMnemonic
是一个异步方法,因此需要使用async
和await
来处理。 - 错误处理:在实际应用中,最好添加更多的错误处理逻辑,以确保在生成助记词失败时能够妥善处理。
- UI优化:上述示例的UI非常基础,你可以根据需求进行更复杂的UI设计。
这个示例展示了如何在Flutter应用中使用substrate_bip39
插件生成BIP39助记词。希望这对你有帮助!