Flutter比特币交易插件btc_direct的使用
Flutter比特币交易插件btc_direct的使用
BTC Direct SDK
- 使用BTC Direct包可以简化代币购买流程:输入钱包详情,选择代币,选择支付方式,并轻松完成KYC验证。非常适合安全的代币交易。
特性
- 安全且易于使用的API进行加密货币的买卖
- 可以无缝集成到您的Flutter应用中
- 提供全面的文档和示例
要求
- iOS 13.0 或更高版本是必需的。
- 更新您的
ios/Podfile
:
source 'https://cdn.cocoapods.org/'
source 'https://github.com/SumSubstance/Specs.git'
# 启用MRTDReader(NFC)模块
ENV['IDENSIC_WITH_MRTDREADER'] = 'true'
# 启用VideoIdent模块
ENV['IDENSIC_WITH_VIDEOIDENT'] = 'true'
- 更新您的
ios/Runner/Info.plist
:
<key>NSCameraUsageDescription</key>
<string>让我们拍摄照片。</string>
<key>NSMicrophoneUsageDescription</key>
<string>录制视频的时间到了。</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>让我们选择一张照片。</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>请提供您的地理位置数据,以便证明您的当前位置。</string>
<key>NFCReaderUsageDescription</key>
<string>让我们扫描文件以获得更精确的识别。</string>
<key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
<array>
<string>A0000002471001</string>
<string>A0000002472001</string>
<string>00000000000000</string>
</array>
- 确保整个项目以及
Pods
项目下的flutter_idensic_mobile_sdk_plugin
目标禁用了位码(Bitcode)(Pods -> flutter_idensic_mobile_sdk_plugin -> Build Settings -> Enable Bitcode -> No)。详情请参见这里。
Android
- 将您的
android/app/build.gradle
文件中的minSdkVersion
改为 21(或更高)。 - 声明以下权限:
<uses-permission android:name="android.permission.INTERNET" />
安装
在您的 pubspec.yaml
文件中添加 btc_direct
作为依赖项。
使用
BTCDirect(
myAddressesList: [
{
"address": "sender_wallet_address",
"currency": "BTC",
"id": '1',
"name": "Sender's Wallet"
},
],
xApiKey: "your_api_key_here",
isSandBox: true,
);
隐私
iOS
要求
- 用途:应用程序功能(涵盖欺诈预防)
- 是否从该应用收集的设备ID与用户身份相关联? 是
- 您或您的第三方合作伙伴是否使用设备ID进行跟踪? 是
Android
要求
- 用户提供的任何数据仅用于通过BTC Direct促进交易,不会与任何第三方共享。详情请参见这里。
- 在发布到Play商店时,应披露设备标识符的使用情况:
数据类型 | 收集 | 共享 | 临时处理 | 必需或可选 | 用途 |
---|---|---|---|---|---|
设备或其他ID | 是 | 否 | 否 | 必需 | 欺诈预防 |
贡献
我们欢迎对BTC Direct SDK的贡献!请提交包含您建议更改的pull请求。
示例代码
import 'package:btc_direct/btc_direct.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
});
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
foregroundColor: Colors.white,
centerTitle: true,
title: const Text(
"BTC Direct",
style: TextStyle(
fontSize: 22, color: Colors.black, fontWeight: FontWeight.w500),
),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0, vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: ElevatedButton(
onPressed: () {
// 导航到Buy小部件。此小部件允许用户选择钱包并完成购买交易。
// 传递给此小部件的参数是:
// - myAddressesList: 用户可以选择来完成交易的地址列表。
// - xApiKey: BTCDirect API的API密钥。
// - isSandBox: 确定交易是在沙盒模式还是生产模式下进行的布尔值。
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BTCDirect(
myAddressesList: const [
{
"address": "sender_wallet_address",
"currency": "BTC",
"id": '1234567',
"name": "Sender's Wallet"
},
],
xApiKey: "your_api_key_here",
isSandBox: true,
),
),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 40),
elevation: 2.0,
textStyle: const TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold),
),
child: const Text(
'Buy now',
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
),
const SizedBox(
height: 10,
),
],
),
),
);
}
}
更多关于Flutter比特币交易插件btc_direct的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter比特币交易插件btc_direct的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用btc_direct
插件进行比特币交易的示例代码。请注意,这只是一个基本示例,实际应用中可能需要更多的错误处理和安全性措施。
首先,确保你已经在pubspec.yaml
文件中添加了btc_direct
依赖:
dependencies:
flutter:
sdk: flutter
btc_direct: ^latest_version # 替换为实际的最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,我们编写一个简单的Flutter应用来演示如何使用btc_direct
插件。这个示例将展示如何初始化插件、创建交易以及发送交易。
import 'package:flutter/material.dart';
import 'package:btc_direct/btc_direct.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late BtcDirect _btcDirect;
@override
void initState() {
super.initState();
// 初始化BtcDirect插件
_btcDirect = BtcDirect();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('BTC Direct Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () async {
// 创建交易示例(实际使用中需要替换为真实的接收地址和金额)
String receiverAddress = '1YourBitcoinAddressHere';
double amount = 0.001; // 比特币金额,这里为0.001 BTC
// 注意:这里的API密钥和钱包信息需要从btc_direct获取,并妥善保管
String apiKey = 'your_btc_direct_api_key';
String walletId = 'your_wallet_id';
try {
// 假设btc_direct插件提供了createTransaction方法(具体方法需参考插件文档)
var transaction = await _btcDirect.createTransaction(
apiKey: apiKey,
walletId: walletId,
receiverAddress: receiverAddress,
amount: amount,
);
// 输出交易信息
print('Transaction ID: ${transaction.transactionId}');
print('Transaction Fee: ${transaction.fee}');
print('Transaction Status: ${transaction.status}');
} catch (e) {
print('Error creating transaction: $e');
}
},
child: Text('Create Transaction'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
// 发送交易示例(假设需要发送之前创建的交易)
String transactionId = 'your_transaction_id'; // 替换为实际的交易ID
try {
// 假设btc_direct插件提供了sendTransaction方法(具体方法需参考插件文档)
var result = await _btcDirect.sendTransaction(
apiKey: apiKey,
transactionId: transactionId,
);
// 输出发送结果
print('Send Result: $result');
} catch (e) {
print('Error sending transaction: $e');
}
},
child: Text('Send Transaction'),
),
],
),
),
),
);
}
}
注意:
- 上述代码中的
createTransaction
和sendTransaction
方法是假设存在的,具体方法名称和参数需参考btc_direct
插件的实际文档。 apiKey
、walletId
、receiverAddress
和transactionId
等参数需要替换为实际的值。- 比特币交易涉及资金安全,务必在正式环境中进行充分的安全测试,并遵循最佳安全实践。
由于btc_direct
插件的具体实现细节和API可能有所不同,因此强烈建议查阅该插件的官方文档以获取准确的信息。