Skip to content

Commit

Permalink
local imports
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Dec 23, 2024
1 parent 4c95367 commit e02ac37
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:ndk/domain_layer/usecases/nwc/nwc_connection.dart';
import 'package:ndk/domain_layer/usecases/zaps/Invoice_response.dart';
import 'package:ndk/domain_layer/usecases/zaps/zap_receipt.dart';
import 'package:ndk/domain_layer/usecases/zaps/zap_request.dart';

import '../../../shared/logger/logger.dart';
import '../../entities/filter.dart';
import '../../entities/request_response.dart';
import '../../repositories/event_signer.dart';
import '../lnurl/lnurl.dart';
import '../nwc/nwc.dart';
import '../nwc/nwc_connection.dart';
import '../nwc/responses/pay_invoice_response.dart';
import '../requests/requests.dart';
import 'Invoice_response.dart';
import 'zap_receipt.dart';
import 'zap_request.dart';

/// Zaps
class Zaps {
Expand All @@ -29,13 +29,12 @@ class Zaps {
_nwc = nwc;

/// creates an invoice with an optional zap request encoded if signer, pubKey & relays are non empty
Future<InvoiceResponse?> fecthInvoice({
required String lud16Link,
required int amountSats,
ZapRequest? zapRequest,
String? comment,
http.Client? client
}) async {
Future<InvoiceResponse?> fecthInvoice(
{required String lud16Link,
required int amountSats,
ZapRequest? zapRequest,
String? comment,
http.Client? client}) async {
var lnurlResponse = await Lnurl.getLnurlResponse(lud16Link, client: client);
if (lnurlResponse == null) {
return null;
Expand All @@ -62,7 +61,9 @@ class Zaps {
}

// ZAP ?
if (lnurlResponse.doesAllowsNostr && zapRequest!=null && zapRequest.sig.isNotEmpty) {
if (lnurlResponse.doesAllowsNostr &&
zapRequest != null &&
zapRequest.sig.isNotEmpty) {
Logger.log.d(jsonEncode(zapRequest));
var eventStr = Uri.encodeQueryComponent(jsonEncode(zapRequest));
callback += "&nostr=$eventStr";
Expand All @@ -75,10 +76,12 @@ class Zaps {
try {
var response = await (client ?? http.Client()).get(uri);
final decodedResponse =
jsonDecode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>;
jsonDecode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>;
String invoice = decodedResponse["pr"];
return InvoiceResponse(invoice: invoice, amountSats: amountSats, nostrPubkey: lnurlResponse.nostrPubkey);

return InvoiceResponse(
invoice: invoice,
amountSats: amountSats,
nostrPubkey: lnurlResponse.nostrPubkey);
} catch (e) {
Logger.log.d(e);

Check warning on line 86 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L86

Added line #L86 was not covered by tests
}
Expand All @@ -94,7 +97,7 @@ class Zaps {
required Iterable<String> relays,
String? pollOption,
}) async {
if (amountSats<0) {
if (amountSats < 0) {
throw ArgumentError("amount cannot be < 0");
}
final amount = amountSats * 1000;
Expand All @@ -111,7 +114,7 @@ class Zaps {
tags.add(["poll_option", pollOption]);

Check warning on line 114 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L114

Added line #L114 was not covered by tests
}
var event = ZapRequest(
pubKey: signer.getPublicKey(), tags: tags, content: comment??'');
pubKey: signer.getPublicKey(), tags: tags, content: comment ?? '');
await signer.sign(event);
return event;
}
Expand Down Expand Up @@ -156,39 +159,47 @@ class Zaps {
return ZapResponse(error: "couldn't get invoice from $lnurl");

Check warning on line 159 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L159

Added line #L159 was not covered by tests
}
try {
PayInvoiceResponse payResponse =
await _nwc.payInvoice(nwcConnection, invoice: invoice.invoice, timeout: Duration(seconds: 10));
PayInvoiceResponse payResponse = await _nwc.payInvoice(nwcConnection,
invoice: invoice.invoice, timeout: Duration(seconds: 10));
if (payResponse.preimage.isNotEmpty && payResponse.errorCode == null) {
ZapResponse zapResponse = ZapResponse(
payInvoiceResponse: payResponse);
if (zapRequest != null && fetchZapReceipt && invoice.nostrPubkey!=null && invoice.nostrPubkey!.isNotEmpty) {
ZapResponse zapResponse = ZapResponse(payInvoiceResponse: payResponse);

Check warning on line 165 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L162-L165

Added lines #L162 - L165 were not covered by tests
if (zapRequest != null &&
fetchZapReceipt &&
invoice.nostrPubkey != null &&
invoice.nostrPubkey!.isNotEmpty) {

Check warning on line 169 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L168-L169

Added lines #L168 - L169 were not covered by tests
// if it's a zap, try to find the zap receipt
zapResponse.receiptResponse = _requests.subscription(filters: [

Check warning on line 171 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L171

Added line #L171 was not covered by tests
eventId != null
? Filter(kinds: [ZapReceipt.KIND], eTags: [eventId], pTags: [pubKey!])
? Filter(
kinds: [ZapReceipt.KIND],
eTags: [eventId],
pTags: [pubKey!])
: Filter(kinds: [ZapReceipt.KIND], pTags: [pubKey!])

Check warning on line 177 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L173-L177

Added lines #L173 - L177 were not covered by tests
]);
// TODO make timeout waiting for receipt parameterizable somehow
final timeout = Timer(Duration(seconds: 30), () {
_requests.closeSubscription(
zapResponse.zapReceiptResponse!.requestId);
Logger.log.w("timed out waiting for zap receipt for invoice $invoice");
_requests
.closeSubscription(zapResponse.zapReceiptResponse!.requestId);
Logger.log
.w("timed out waiting for zap receipt for invoice $invoice");

Check warning on line 184 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L180-L184

Added lines #L180 - L184 were not covered by tests
});

zapResponse.zapReceiptResponse!.stream.listen((event) {
String? bolt11 = event.getFirstTag("bolt11");
String? preimage = event.getFirstTag("preimage");
if (bolt11!=null && bolt11 == invoice || preimage!=null && preimage==payResponse.preimage) {
if (bolt11 != null && bolt11 == invoice ||
preimage != null && preimage == payResponse.preimage) {
ZapReceipt receipt = ZapReceipt.fromEvent(event);
Logger.log.d("Zap Receipt: $receipt");
if (receipt.isValid(nostrPubKey: invoice.nostrPubkey!, recipientLnurl: lnurl)) {
if (receipt.isValid(
nostrPubKey: invoice.nostrPubkey!, recipientLnurl: lnurl)) {
zapResponse.emitReceipt(receipt);

Check warning on line 196 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L187-L196

Added lines #L187 - L196 were not covered by tests
} else {
Logger.log.w("Zap Receipt invalid: $receipt");

Check warning on line 198 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L198

Added line #L198 was not covered by tests
}
timeout.cancel();
_requests.closeSubscription(
zapResponse.zapReceiptResponse!.requestId);
_requests
.closeSubscription(zapResponse.zapReceiptResponse!.requestId);

Check warning on line 202 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L200-L202

Added lines #L200 - L202 were not covered by tests
}
});
} else {
Expand All @@ -205,7 +216,8 @@ class Zaps {
/// fetch all zap receipts matching given pubKey and optional event id, in sats
Stream<ZapReceipt> fetchZappedReceipts(

Check warning on line 217 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L217

Added line #L217 was not covered by tests
{required String pubKey, String? eventId, Duration? timeout}) {
NdkResponse? response = _requests.query(timeout: timeout??Duration(seconds:10), filters: [
NdkResponse? response =
_requests.query(timeout: timeout ?? Duration(seconds: 10), filters: [

Check warning on line 220 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L220

Added line #L220 was not covered by tests
eventId != null
? Filter(kinds: [ZapReceipt.KIND], eTags: [eventId], pTags: [pubKey])
: Filter(kinds: [ZapReceipt.KIND], pTags: [pubKey])

Check warning on line 223 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L222-L223

Added lines #L222 - L223 were not covered by tests
Expand Down

0 comments on commit e02ac37

Please sign in to comment.