一个包,通过提供 FlutterWebln 接口来帮助您与 WebLN 提供程序交互,用于创建支持比特币闪电网络的 Web 应用程序。
特点
有以下方法:
- 启用提供程序(
FlutterWebln.enable) - 获取有关用户比特币闪电网络节点的信息(
FlutterWebln.getInfo) - 发送付款(
FlutterWebln.sendPayment) - 创建发票以接收付款(
FlutterWebln.makeInvoice) - 请求用户发送 keysend 付款(
FlutterWebln.keysend) - 请求签名任意消息(
FlutterWebln.signMessage) - 验证签名与原始消息(
FlutterWebln.verifyMessage)
入门
要使用任何 FlutterWebln 方法,您首先需要安装 WebLN 提供程序。
检测 WebLN 支持
在开始使用 FlutterWebln 之前,您需要通过检查变量 FlutterWebln.webln 是否已定义来检查浏览器支持。
void checkWebln() {
try {
final weblnValue = weblnDecode(FlutterWebln.webln);
if (weblnValue.isEmpty) {
isWallet = false;
} else {
isWallet = true;
}
print('[+] webln value is $weblnValue');
} catch (e) {
print("[!] Error in checkWebln method is $e");
}
}
weblnValue.isEmpty 表示未安装 WebLN 提供程序,用户无法使用任何 FlutterWebln 方法。
启用 WebLN
要开始与 FlutterWebln 方法交互,您首先需要 **启用** 提供程序,如下所示:
await FlutterWebln.enable()
这将提示用户授权使用浏览器的 WebLN 功能。之后,您可以随意调用任何其他 FlutterWebln 方法。
获取信息
通过 FlutterWebln.getInfo,用户可以获取有关已连接节点的信息,如下所示:
try {
await FlutterWebln.enable();
await FlutterWebln.getInfo().then(allowInterop((response) {
print('[+] GetInfoResponse: ${weblnDecode(response)}');
}));
} catch (error) {
print('[!] Error in getInfo method is $error');
}
响应
Result: [+] GetInfoResponse: {node: {alias: ? getalby.com}}
创建发票
通过 FlutterWebln.makeInvoice,用户创建发票供 Web 应用程序使用,如下所示:
final invoice = FlutterWebln.requestInvoiceArgs(
amount: 100,
defaultMemo: 'Hello World',
);
try {
await FlutterWebln.makeInvoice(requestInvoiceArgs: invoice)
.then(allowInterop((result) {
print('[+] RequestInvoiceResponse: ${weblnDecode(result)}');
}));
} catch (error) {
print('[!] Error in makeInvoice method is $error');
}
响应
[+] RequestInvoiceResponse: {paymentRequest: lnbc1u1p3jdmdzpp5aehslp0ts0wszr62lwl82qwzcgl6jtqudvg000ez75jx307vpcdqdqjfpjkcmr0yptk7unvvscqzpgxqyz5vqsp5npje0n0mct745acshv8dl5pz5kyznz2z6du45fwpyxgwvxvepdts9qyyssqdynz62hf8xh76pn4qfpswzcz4ezt6k9kj9mccf6lzwzkutm04rwjhzynctgphyk6xc0g2ftn7unjxvmszutzr07xq52h5qeja5mk3sqpqwwx7y, rHash: ee6f0f85eb83dd010f4afbbe7501c2c23fa92c1c6b10f7bf22f52468bfcc0e1a}
发送付款
通过 FlutterWebln.sendPayment,用户发送发票的付款。用户需要提供一个 BOLT-11 发票。
try {
await FlutterWebln.sendPayment(invoice: invoiceController.text)
.then(allowInterop((result) {
print('[+] SendPaymentResponse: ${weblnDecode(result)}');
}));
} catch (error) {
print('[!] Error in sendPayment method is $error');
}
响应
[+] SendPaymentResponse: {preimage: 6662313533626135643134626265623164343134363734626261336263306630, paymentHash: ee6f0f85eb83dd010f4afbbe7501c2c23fa92c1c6b10f7bf22f52468bfcc0e1a, route: {total_amt: 100, total_fees: 0}}
Keysend
使用 FlutterWebln.keysend,它请求用户发送 keysend 付款。此付款仅需要目标公钥和金额。
try {
await FlutterWebln.keysend(keysendArgs: keysendArgs)
.then(allowInterop((result) {
print('[+] KeysendPaymentResponse: ${weblnDecode(result)}');
}));
} catch (error) {
print('[!] Error in keysend method is $error');
}
响应
[+] KeysendPaymentResponse: {preimage: 3965373033306134316666323562396262333332393463653136383634636265, paymentHash: 20594ee7899bee252917bc44ec744309d0593adf0e79469bb067afb67b632ffc, route: {total_amt: 20, total_fees: 0}}
签名消息
使用 `FlutterWebln.signMessage`,它会请求用户签名任意字符串消息。
try {
await FlutterWebln.signMessage(message: 'Hello World!')
.then(allowInterop((result) {
print('[+] SignMessageResponse: ${weblnDecode(result)}');
}));
} catch (error) {
print('[!] Error in signMessage method is $error');
}
签名消息可以通过服务器端使用 LND RPC 方法进行验证,也可以通过客户端使用 FlutterWebln.verifyMessage 进行验证。
验证消息
使用 FlutterWebln.verifyMessage,用户客户端会验证 **签名** 与原始 **消息**,并告知用户是否有效。
try {
await FlutterWebln.verifyMessage(
signature: signatureController.text,
message: messageController.text,
).then(allowInterop((result) {
print('[+] VerifyMessageResponse: ${weblnDecode(result)}');
}));
} catch (error) {
print('[!] Error in verifyMessage method is $error');
}
示例项目
上述 flutter_webln_integration 项目可以在 这里 找到。

