Skip to content

Commit d8fbe16

Browse files
fix: support pay offer invoice and bug fixing when it is necessary
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent fcc38b6 commit d8fbe16

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

lib/src/kraken.dart

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:collection';
23
import 'dart:convert';
34
import 'package:clightning_rpc/clightning_rpc.dart';
45
import 'package:cln_common/cln_common.dart';
@@ -16,20 +17,25 @@ class KrakenPlugin extends Plugin {
1617
Plugin plugin, Map<String, Object> request) async {
1718
log(level: "info", message: "This is the kraken doctor output.");
1819
var params = DoctorRequest.fromJson(request);
20+
Map<String, dynamic> response = {};
1921
try {
2022
// TODO make sure that this will trow an exception
21-
Map<String, dynamic> listPays = await rpc!
22-
.call(method: "paystatus", params: params.toPaysStatusRequest());
23+
Map<String, dynamic>? listPays;
24+
if (request.containsKey("bolt11")) {
25+
listPays = await rpc!
26+
.call(method: "paystatus", params: params.toPaysStatusRequest());
27+
response["listpays"] = listPays;
28+
}
2329
Map<String, dynamic> listFunds = await rpc!
2430
.call(method: "listfunds", params: params.toListFundsRequest());
2531

26-
/// Make a call to listfunds like the prev one, and take only the channels
27-
/// The raw reason is the error message that core lightning return
28-
return Future.value({
29-
"listpays": listPays,
32+
response.addAll({
3033
"channels": listFunds["channels"],
31-
"raw_reason": request["raw_reason"] ?? "unknown"
34+
"raw_reason": request["raw_reason"] ?? "unknown",
35+
"raw_error_code": request["raw_error_code"] ?? "unknown",
36+
"raw_data": request["raw_data"] ?? "unknown"
3237
});
38+
return Future.value(response.cast<String, Object>());
3339
} on LNClientException catch (ex, stacktrace) {
3440
plugin.log(level: "broken", message: "error received: ${ex.message}");
3541
plugin.log(level: "broken", message: stacktrace.toString());
@@ -39,9 +45,13 @@ class KrakenPlugin extends Plugin {
3945

4046
Future<String> handleBolt12(Plugin plugin,
4147
{required FetchInvoiceRequest offer}) async {
42-
var fetchInvoice = await rpc!
43-
.call<FetchInvoiceRequest, FetchInvoiceResponse>(
44-
method: "fetchinvoice", params: offer);
48+
var fetchInvoice = await rpc!.call<FetchInvoiceRequest,
49+
FetchInvoiceResponse>(
50+
method: "fetchinvoice",
51+
params: offer,
52+
onDecode: (json) =>
53+
FetchInvoiceResponse.fromJson(json as HashMap<String, dynamic>));
54+
log(level: "info", message: "fetch invoice: ${fetchInvoice.toJSON()}");
4555
return fetchInvoice.invoice;
4656
}
4757

@@ -60,16 +70,22 @@ class KrakenPlugin extends Plugin {
6070
String invoice = request["invoice"]! as String;
6171
// The human-readable prefix for BOLT 12 offers is lno.
6272
if (invoice.startsWith("lno")) {
73+
request["bolt11"] = invoice;
6374
// Modify request with the correct bolt 12!
6475
// FIXME: we should remove the msatoshi amount from the request? or the pay command
6576
// us smart enough to handle it?
77+
var amountMsat = request["amount_msat"] == null
78+
? null
79+
: int.parse(request["amount_msat"].toString());
80+
6681
request["bolt11"] = await handleBolt12(plugin,
6782
offer: FetchInvoiceRequest(
68-
offer: invoice, msamsatoshi: request["msatoshi"] as String?));
83+
offer: invoice,
84+
msatoshi: request["msatoshi"]?.toString(),
85+
amountMsat: amountMsat));
6986
} else {
7087
request["bolt11"] = invoice;
7188
}
72-
// TODO: rpc should return an exception if any error occurs, and if we have the error returned run the doctor command
7389
PayResponse result = await rpc!.call<PayRequest, PayResponse>(
7490
method: "pay",
7591
params: PayRequest.fromJson(request),
@@ -78,7 +94,14 @@ class KrakenPlugin extends Plugin {
7894
return result.toJSON() as Map<String, Object>;
7995
} on LNClientException catch (ex, stacktrace) {
8096
request["raw_reason"] = ex.message;
81-
plugin.log(level: "broken", message: "error received: ${ex.message}");
97+
request["raw_error_code"] = ex.code;
98+
if (ex.data != null) {
99+
request["raw_data"] = ex.data!;
100+
}
101+
plugin.log(
102+
level: "broken",
103+
message:
104+
"error received: msg=${ex.message} code: ${ex.code} data: ${ex.data}");
82105
plugin.log(level: "broken", message: stacktrace.toString());
83106
return krakenDoctor(plugin, request);
84107
} catch (ex, stacktrace) {
@@ -112,7 +135,7 @@ class KrakenPlugin extends Plugin {
112135
var rpcName = configuration["rpc-file"]!.toString();
113136
rpc = RPCClient().connect("$rpcPath/$rpcName") as RPCClient;
114137
log(level: "info", message: "kraken payment configured");
115-
138+
log(level: "info", message: "Configuration payload $configuration");
116139
return response;
117140
}
118141
}

lib/src/model/model.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ class PayResponse extends Serializable {
8484
Map<String, dynamic> toJSON() => _$PayResponseToJson(this);
8585
}
8686

87-
@JsonSerializable()
87+
@JsonSerializable(includeIfNull: false)
8888
class FetchInvoiceRequest extends Serializable {
8989
String offer;
90-
String? msamsatoshi;
90+
String? msatoshi;
91+
@JsonKey(name: "amount_msat")
92+
int? amountMsat;
9193

92-
FetchInvoiceRequest({required this.offer, this.msamsatoshi});
94+
FetchInvoiceRequest({required this.offer, this.msatoshi, this.amountMsat});
9395

9496
factory FetchInvoiceRequest.fromJson(Map<String, dynamic> json) =>
9597
_$FetchInvoiceRequestFromJson(json);

lib/src/model/model.g.dart

Lines changed: 17 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ environment:
88

99
dependencies:
1010
clightning_rpc: ^0.0.2-beta.4
11-
cln_common: ^0.0.1-beta.1
11+
cln_common: ^0.0.1-beta.2
1212
cln_plugin_api: ^0.0.1-beta.2
1313
json_annotation: ^4.5.0
1414

0 commit comments

Comments
 (0)