Flutter比特币BIP32密钥派生插件bitcoin_bip32_ng的使用
Flutter比特币BIP32密钥派生插件bitcoin_bip32_ng的使用
BIP32
bitcoin_bip32_ng
是一个实现了 BIP32 规范 的库,用于生成分层确定性比特币地址。没有定义 超结构钱包。
示例
你可以通过两种方式使用此库:一种是通过序列化的公钥或私钥,另一种是通过十六进制编码的种子。
更多复杂的用法可以查看测试文件。
使用种子
Chain chain = Chain.seed(hex.decode("000102030405060708090a0b0c0d0e0f"));
ExtendedPrivateKey key = chain.forPath("m/0/100");
print(key);
// => xprv9ww7sMFLzJN5LhdyGB9zfhm9MAVZ8P97iTWQtVeAg2rA9MPZfJUESWe6NaSu44zz44QBjWtwH9HNfJ4vFiUwfrTCvf7AGrgYpXe17bfh2Je
key
对象有一个名为 key
的字段,该字段包含一个 BigInt
。这就是实际的密钥。
导入HD私钥
Chain chain = Chain.import("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi");
ExtendedPrivateKey childKey = chain.forPath("m/0/100");
导入HD公钥
Chain chain = Chain.import("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8");
ExtendedPublicKey childKey = chain.forPath("M/0/100");
key
对象有一个名为 q
的字段,该字段包含一个 ECPoint
。这就是实际的密钥。
请注意,尝试从公钥生成私钥将会抛出异常。
异常
在衍生子键时可能会有极小的概率发生错误。请在代码中捕获相应的异常。
这些异常包括:
KeyZero
KeyBiggerThanOrder
KeyInfinite
安装
将它添加到你的 pubspec.yaml
文件中:
dependencies:
bitcoin_bip32_ng: ^0.1.0
示例代码
以下是一个完整的示例代码:
import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:bitcoin_bip32_ng/bitcoin_bip32_ng.dart';
void main() {
// 创建一个链对象,使用种子生成密钥
var chain = Chain.seed(hex.decode("000102030405060708090a0b0c0d0e0f"));
// 派生子密钥
var key = chain.forPath('m/0/100');
// 打印派生的密钥
print(key);
}
更多关于Flutter比特币BIP32密钥派生插件bitcoin_bip32_ng的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter比特币BIP32密钥派生插件bitcoin_bip32_ng的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用bitcoin_bip32_ng
插件进行BIP32密钥派生的代码示例。这个插件允许你从主种子(master seed)生成BIP32层次结构的密钥。
首先,确保你已经在pubspec.yaml
文件中添加了bitcoin_bip32_ng
依赖:
dependencies:
flutter:
sdk: flutter
bitcoin_bip32_ng: ^最新版本号 # 请替换为当前最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤使用bitcoin_bip32_ng
插件:
- 导入包:
import 'package:bitcoin_bip32_ng/bitcoin_bip32.dart';
import 'package:pointycastle/export.dart';
- 生成主密钥(从种子):
void main() async {
// 假设你有一个种子(通常是从助记词生成的)
Uint8List seed = Uint8List.fromList(hex.decode('000102030405060708090a0b0c0d0e0f'));
// 使用BIP32类生成主密钥
BIP32 masterNode = BIP32.fromSeed(seed);
// 打印主密钥的扩展私钥和公钥
print('Master Extended Private Key: ${masterNode.toBase58()}');
print('Master Extended Public Key: ${masterNode.neutralized().toBase58()}');
}
- 派生子密钥:
void deriveChildKeys() {
// 假设你已经有了主密钥节点
BIP32 masterNode = // ... (从上面的代码获取)
// 派生第一个子密钥(m/0')
BIP32 childNode = masterNode.derivePath("m/0'");
// 打印子密钥的扩展私钥和公钥
print('Child Extended Private Key: ${childNode.toBase58()}');
print('Child Extended Public Key: ${childNode.neutralized().toBase58()}');
// 你可以继续派生更深的层次,例如 m/0'/0
BIP32 grandChildNode = childNode.derivePath("0");
print('Grandchild Extended Private Key: ${grandChildNode.toBase58()}');
print('Grandchild Extended Public Key: ${grandChildNode.neutralized().toBase58()}');
}
- 在Flutter UI中使用:
你可以将这些功能封装在按钮点击事件或其他UI交互中。以下是一个简单的例子,展示如何在Flutter UI中触发密钥派生:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String masterPrivateKey = '';
String masterPublicKey = '';
String childPrivateKey = '';
String childPublicKey = '';
@override
void initState() {
super.initState();
_generateKeys();
}
void _generateKeys() async {
Uint8List seed = Uint8List.fromList(hex.decode('000102030405060708090a0b0c0d0e0f'));
BIP32 masterNode = BIP32.fromSeed(seed);
setState(() {
masterPrivateKey = masterNode.toBase58();
masterPublicKey = masterNode.neutralized().toBase58();
BIP32 childNode = masterNode.derivePath("m/0'");
childPrivateKey = childNode.toBase58();
childPublicKey = childNode.neutralized().toBase58();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('BIP32 Key Derivation'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Master Extended Private Key: $masterPrivateKey'),
SizedBox(height: 16),
Text('Master Extended Public Key: $masterPublicKey'),
SizedBox(height: 16),
Text('Child Extended Private Key: $childPrivateKey'),
SizedBox(height: 16),
Text('Child Extended Public Key: $childPublicKey'),
],
),
),
),
);
}
}
以上代码展示了如何在Flutter应用中集成bitcoin_bip32_ng
插件,从种子生成BIP32主密钥,并派生子密钥。你可以根据具体需求进一步扩展这些功能。