Flutter以太坊名称服务插件ens_dart的使用
Flutter以太坊名称服务插件ens_dart的使用
简介
ENS Dart
是一个封装了以太坊名称服务(ENS)的 Dart 库。ENS 类似于域名服务(DNS),允许用户和开发者使用人类友好的名称代替复杂的十六进制地址、内容哈希等。
- 与
web3dart
包进行互操作 - 从 ETH 地址获取 ENS 名称
- 从 ENS 名称获取地址
- 获取内容哈希
- 获取文本记录
- 调用 ENS 智能合约上的函数并监听合约事件
使用方法
获取ENS名称和地址
ENS Dart
提供了一个接口,可以用来从名称查找地址,反之亦然。
Future<void> main() async {
env.load('../.env');
final infuraKey = env.env['INFURA_KEY'];
final rpcUrl = 'https://mainnet.infura.io/v3/$infuraKey';
final wsUrl = 'wss://mainnet.infura.io/ws/v3/$infuraKey';
final client = Web3Client(rpcUrl, Client(), socketConnector: () {
return IOWebSocketChannel.connect(wsUrl).cast<String>();
});
/// ENS客户端
final ens = Ens(client: client);
/// 从地址获取名称
final name = await ens
.withAddress('0x1a5cdcFBA600e0c669795e0B65c344D5A37a4d5A')
.getName();
/// 从名称获取地址
final addr = await ens.withName('brantly.eth').getAddress();
/// 获取文本记录
final textRecord = await ens.getTextRecord();
print('name: $name'); // 输出: addr: 0x324f9307b6d26881822c7c8692eeac39791a9756
print('addr: $addr'); // 输出: name: sea.eth
print(
'textRecord: $textRecord'); /* 输出:
EnsTextRecord {
email: me@brantly.xyz,
url: http://brantly.xyz/,
avatar: eip155:1/erc721:0xb7F7F6C52F2e2fdb1963Eab30438024864c313F6/2430,
description: "If anyone would come after me, let him deny himself and take up his cross daily and follow me. For whoever would save his life will lose it, but whoever loses his life for my sake will save it. For what does it profit a man if he gains the whole world and loses or forfeits himself?" - Jesus, Luke 9.23-25,
notice: Not for sale,
keywords: ,
com.discord: brantly.eth#9803,
com.github: brantlymillegan,
com.reddit: brantlymillegan,
com.twitter: brantlymillegan,
org.telegram: brantlymillegan,
eth.ens.delegate: https://discuss.ens.domains/t/ens-dao-delegate-applications/815/697?u=brantlymillegan
}
*/
}
完整示例
以下是一个完整的示例代码,展示了如何使用 ens_dart
插件来获取 ENS 名称和地址,并打印相关信息。
import 'package:ens_dart/ens_dart.dart';
import 'package:dotenv/dotenv.dart' as env;
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import 'package:web_socket_channel/io.dart';
Future<void> main() async {
env.load('../.env');
final infuraKey = env.env['INFURA_KEY'];
final rpcUrl = 'https://mainnet.infura.io/v3/$infuraKey';
final wsUrl = 'wss://mainnet.infura.io/ws/v3/$infuraKey';
final client = Web3Client(rpcUrl, Client(), socketConnector: () {
return IOWebSocketChannel.connect(wsUrl).cast<String>();
});
final ens = Ens(client: client);
// 从地址获取名称
final name = await ens
.withAddress('0x1a5cdcFBA600e0c669795e0B65c344D5A37a4d5A')
.getName();
// 从名称获取地址
final addr = await ens.withName('brantly.eth').getAddress();
// 获取文本记录
final textRecord = await ens.getTextRecord();
print('name: $name'); // 输出: addr: 0x324f9307b6d26881822c7c8692eeac39791a9756
print('addr: $addr'); // 输出: name: sea.eth
print(
'textRecord: $textRecord'); /* 输出:
EnsTextRecord {
email: me@brantly.xyz,
url: http://brantly.xyz/,
avatar: eip155:1/erc721:0xb7F7F6C52F2e2fdb1963Eab30438024864c313F6/2430,
description: "If anyone would come after me, let him deny himself and take up his cross daily and follow me. For whoever would save his life will lose it, but whoever loses his life for my sake will save it. For what does it profit a man if he gains the whole world and loses or forfeits himself?" - Jesus, Luke 9.23-25,
notice: Not for sale,
keywords: ,
com.discord: brantly.eth#9803,
com.github: brantlymillegan,
com.reddit: brantlymillegan,
com.twitter: brantlymillegan,
org.telegram: brantlymillegan,
eth.ens.delegate: https://discuss.ens.domains/t/ens-dao-delegate-applications/815/697?u=brantlymillegan
}
*/
}
更多关于Flutter以太坊名称服务插件ens_dart的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter以太坊名称服务插件ens_dart的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter项目中使用ens_dart
插件来与以太坊名称服务(ENS)进行交互的代码案例。这个案例展示了如何查询ENS域名对应的以太坊地址。
首先,确保你已经在你的pubspec.yaml
文件中添加了ens_dart
依赖:
dependencies:
flutter:
sdk: flutter
ens_dart: ^最新版本号 # 请替换为最新的版本号
然后,运行flutter pub get
来安装依赖。
接下来,是一个简单的Flutter应用示例,展示如何使用ens_dart
插件:
import 'package:flutter/material.dart';
import 'package:ens_dart/ens_dart.dart';
import 'package:web3dart/web3dart.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String ensAddress = '';
String resolvedAddress = '';
bool isLoading = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('ENS Dart Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
decoration: InputDecoration(labelText: 'ENS Domain'),
onChanged: (value) {
setState(() {
ensAddress = value;
});
},
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () async {
setState(() {
isLoading = true;
});
// 使用Infura的节点作为示例,你需要替换为你的以太坊节点URL
final client = Web3Client(
'https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
// 创建一个ENS实例
final ens = ENS(client);
try {
final address = await ens.address(ensAddress);
setState(() {
resolvedAddress = address.hex;
isLoading = false;
});
} catch (e) {
setState(() {
resolvedAddress = 'Error: ${e.toString()}';
isLoading = false;
});
}
},
child: Text('Resolve Address'),
),
SizedBox(height: 16),
if (isLoading) CircularProgressIndicator(),
Text('Resolved Address: $resolvedAddress'),
],
),
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个文本输入框用于输入ENS域名,以及一个按钮用于解析该域名对应的以太坊地址。
注意事项:
-
Infura节点:在代码中,我们使用了Infura的以太坊节点。你需要替换
YOUR_PROJECT_ID
为你的实际Infura项目ID。或者,你可以使用你自己的以太坊节点。 -
错误处理:代码中包含基本的错误处理,如果解析域名失败,会显示错误信息。
-
依赖版本:确保你使用的是
ens_dart
和web3dart
的最新版本,因为API可能会随着版本更新而变化。 -
网络连接:由于这个示例使用网络请求来与以太坊节点通信,确保你的设备可以访问互联网。
-
ENS域名:输入有效的ENS域名进行测试,例如
alice.eth
。
这个示例展示了如何使用ens_dart
插件在Flutter应用中与以太坊名称服务进行交互。根据你的需求,你可以进一步扩展这个示例,添加更多功能或进行更复杂的交互。