Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to specify timeout of fetching URL #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/dart_lnurl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library dart_lnurl;

import 'dart:async';
import 'dart:convert';

import 'package:bech32/bech32.dart';
Expand All @@ -19,8 +20,11 @@ export 'src/bech32.dart';
/// * `LNURLAuthParams`
/// * `LNURLPayParams`
///
/// If [timeout] is provided, then [TimeoutException] is being thrown
/// when request does not complete within specified timeout.
///
/// Throws [ArgumentError] if the provided input is not a valid lnurl.
Future<LNURLParseResult> getParams(String encodedUrl) async {
Future<LNURLParseResult> getParams(String encodedUrl, {Duration? timeout}) async {
/// Try to parse the input as a lnUrl. Will throw an error if it fails.
final lnUrl = findLnUrl(encodedUrl);

Expand All @@ -30,7 +34,8 @@ Future<LNURLParseResult> getParams(String encodedUrl) async {

try {
/// Call the lnurl to get a response
final res = await http.get(decodedUri);
final call = http.get(decodedUri);
final res = await (timeout == null ? call : call.timeout(timeout));

/// If there's an error then throw it
if (res.statusCode >= 300) {
Expand Down Expand Up @@ -85,6 +90,8 @@ Future<LNURLParseResult> getParams(String encodedUrl) async {

throw Exception('Unknown tag: ${parsedJson['tag']}');
}
} on TimeoutException {
rethrow;
} catch (e) {
return LNURLParseResult(
error: LNURLErrorResponse.fromJson({
Expand Down
61 changes: 40 additions & 21 deletions test/dart_lnurl_test.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import 'dart:async';

import 'package:dart_lnurl/dart_lnurl.dart';
import 'package:dart_lnurl/src/lnurl.dart';
import 'package:dart_lnurl/src/types.dart';
import 'package:flutter_test/flutter_test.dart';

import 'util.dart';

void main() {
test('should match lnurl without lightning:', () {
final lnurl =
'lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq';
expect(findLnUrl(lnurl),
'lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq');
});
void main() async {
group('Tests for "findLnUrl"', () {
test('should match lnurl without lightning:', () {
final lnurl =
'lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq';
expect(findLnUrl(lnurl),
'lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq');
});

test('should match lnurl with lightning:', () {
final lnurl =
'lightning:lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq';
expect(findLnUrl(lnurl),
'lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq');
});
test('should match lnurl with lightning:', () {
final lnurl =
'lightning:lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq';
expect(findLnUrl(lnurl),
'lnurl1dp68gurn8ghj7mrww4exctt5dahkccn00qhxget8wfjk2um0veax2un09e3k7mf0w5lhz0t9xcekzv34vgcx2vfkvcurxwphvgcrwefjvgcnqwrpxqmkxven89skgvp3vs6nwvpjvy6njdfsx5ekgephvcurxdf5xcerwvecvyunsf32lqq');
});

test('should fail matching lnurl on invalid string', () {
expect(() => findLnUrl('invalid string'), throwsArgumentError);
test('should fail matching lnurl on invalid string', () {
expect(() => findLnUrl('invalid string'), throwsArgumentError);
});
});

test('should decipher preimage', () {
Expand All @@ -39,15 +42,31 @@ void main() {
'cipherText': data.cipherText,
'iv': data.iv,
'tag': 'aes',
'description': '', //test fails without it
'url': '', //test fails without it
'message': '', //test fails without it
}),
);
expect(decrypted, plainText);
});

test('should decode lnurl-pay', () async {
final url =
'lightning:LNURL1DP68GURN8GHJ7MRWW4EXCTNZD9NHXATW9EU8J730D3H82UNV94CXZ7FLWDJHXUMFDAHR6C3NXGCNGCEE8YEX2CFHXCCRYCNXXUENVEFHXQCR2EF5XS6R2D35XUERSEFC8YCKGDF5XAJNGCTX8PJRYVP4XDJNVD3CX3NRVEFCX4SSWRA86F';
final res = await getParams(url);
expect(res.payParams, isNotNull);
group('Tests for "getParams"', () {
test('should decode lnurl-pay', () async {
final url =
'lightning:LNURL1DP68GURN8GHJ7MRWW4EXCTNZD9NHXATW9EU8J730D3H82UNV94CXZ7FLWDJHXUMFDAHR6C3NXGCNGCEE8YEX2CFHXCCRYCNXXUENVEFHXQCR2EF5XS6R2D35XUERSEFC8YCKGDF5XAJNGCTX8PJRYVP4XDJNVD3CX3NRVEFCX4SSWRA86F';
final res = await getParams(url);
expect(res.payParams, isNotNull);
}, skip: true); //domain https://lnurl.bigsun.xyz/lnurl-pay?session=b3214c992ea7602bf736e7005e444564728e891d547e4af8d2053e6684f6e85a can not be found

test('should interrupt after timeout', () async {
final notExistingUrl = 'lnurl1dp68gurn8ghj7etcv9khqmr99e3k7mf0d3h82unvq257ls';
res() => getParams(notExistingUrl, timeout: Duration(milliseconds: 10));

expect(res, throwsA(predicate((e) =>
e is TimeoutException
&& e.message == 'Future not completed'
&& e.toString() == 'TimeoutException after 0:00:00.010000: Future not completed'
)));
});
});
}