Skip to content

Commit

Permalink
fix: bolt12 invoice pay status (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Nov 14, 2024
1 parent 428d814 commit 8dca13c
Show file tree
Hide file tree
Showing 18 changed files with 1,023 additions and 807 deletions.
4 changes: 2 additions & 2 deletions lib/VersionCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class VersionCheck {
maximal: '0.2.0',
},
[MpayClient.serviceName]: {
minimal: '0.1.0',
maximal: '0.1.2',
minimal: '0.1.3',
maximal: '0.1.3',
},
[LndClient.serviceName]: {
minimal: '0.17.0',
Expand Down
15 changes: 6 additions & 9 deletions lib/lightning/cln/ClnClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ class ClnClient
public checkPayStatus = async (
invoice: string,
): Promise<PaymentResponse | undefined> => {
const decoded = await this.decodeInvoice(invoice);

const listPayReq = new noderpc.ListpaysRequest();
listPayReq.setBolt11(invoice);

Expand All @@ -706,12 +708,7 @@ class ClnClient
(sum, attempt) =>
sum + BigInt(attempt.getAmountSentMsat()?.getMsat() || 0),
0n,
) -
completedAttempts.reduce(
(sum, attempt) =>
sum + BigInt(attempt.getAmountMsat()?.getMsat() || 0),
0n,
);
) - BigInt(decoded.valueMsat);

return {
feeMsat: Number(fee),
Expand Down Expand Up @@ -750,15 +747,15 @@ class ClnClient
noderpc.ListpeerchannelsResponse
>('listPeerChannels', new noderpc.ListpeerchannelsRequest(), false);

const paymentHash = (await this.decodeInvoice(invoice)).paymentHash;

const hasPendingHtlc = channels
.getChannelsList()
.some((channel) =>
channel
.getHtlcsList()
.some((htlc) =>
paymentHash.equals(Buffer.from(htlc.getPaymentHash_asU8())),
decoded.paymentHash.equals(
Buffer.from(htlc.getPaymentHash_asU8()),
),
),
);

Expand Down
2 changes: 1 addition & 1 deletion lib/lightning/cln/MpayClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Mpay extends BaseClient {

public payStatus = (invoice: string) => {
const req = new mpayrpc.PayStatusRequest();
req.setBolt11(invoice);
req.setInvoice(invoice);

return this.unaryNodeCall<
mpayrpc.PayStatusRequest,
Expand Down
23 changes: 16 additions & 7 deletions lib/proto/mpay/mpay_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 76 additions & 10 deletions lib/proto/mpay/mpay_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions test/integration/lightning/cln/ClnClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getHexString, getUnixTime } from '../../../../lib/Utils';
import { ClientStatus } from '../../../../lib/consts/Enums';
import Errors from '../../../../lib/lightning/Errors';
import { InvoiceFeature } from '../../../../lib/lightning/LightningClient';
import * as noderpc from '../../../../lib/proto/cln/node_pb';
import * as primitivesrpc from '../../../../lib/proto/cln/primitives_pb';
import Sidecar from '../../../../lib/sidecar/Sidecar';
import { wait } from '../../../Utils';
import {
Expand Down Expand Up @@ -192,6 +194,35 @@ describe('ClnClient', () => {
clnClient['mpay']!.setClientStatus(ClientStatus.Connected);
});

test('should not throw when getting pay status of BOLT12 invoices', async () => {
const offerReq = new noderpc.OfferRequest();
offerReq.setAmount('any');

const offer: noderpc.OfferResponse.AsObject = await clnClient[
'unaryNodeCall'
]('offer', offerReq, true);

const invoiceReq = new noderpc.FetchinvoiceRequest();
invoiceReq.setOffer(offer.bolt12);

const amount = new primitivesrpc.Amount();
amount.setMsat(1_000);
invoiceReq.setAmountMsat(amount);

const invoice: noderpc.FetchinvoiceResponse.AsObject = await clnClient[
'unaryNodeCall'
]('fetchInvoice', invoiceReq, true);

await expect(
clnClient.checkPayStatus(invoice.invoice),
).resolves.toBeUndefined();

await clnClient.sendPayment(invoice.invoice);
await expect(
clnClient.checkPayStatus(invoice.invoice),
).resolves.not.toBeUndefined();
});

test('should emit events for invoice acceptance and settlement', async () => {
const preimage = randomBytes(32);
const preimageHash = crypto.sha256(preimage);
Expand Down
6 changes: 3 additions & 3 deletions test/unit/grpc/GrpcServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('GrpcServer', () => {
expect(preRestartServerPem).toEqual(
fs.readFileSync(path.join(certsDir, 'server.pem')),
);
});
}, 10_000);

test('should not allow clients with invalid certificates', async () => {
const port = await getPort();
Expand Down Expand Up @@ -215,7 +215,7 @@ describe('GrpcServer', () => {

client.close();
await server.close();
});
}, 10_000);

test('should throw when trying to bind to impossible port', async () => {
const server = new GrpcServer(
Expand Down Expand Up @@ -256,5 +256,5 @@ describe('GrpcServer', () => {
resolve();
});
});
});
}, 10_000);
});
2 changes: 1 addition & 1 deletion test/unit/notifications/CommandHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe('CommandHandler', () => {
const id = 'notFound';
sendMessage(`swapinfo ${id}`);

await wait(10);
await wait(50);

expect(mockSendMessage).toHaveBeenCalledTimes(5);
expect(mockSendMessage).toHaveBeenCalledWith(`${errorMessage}${id}`);
Expand Down
2 changes: 1 addition & 1 deletion tools/plugins/mpay/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def register_options(pl: Plugin) -> None:

# pyln.client added full dynamic option support in 24.05
ver = __version__.split(".")
if int(ver[0]) > 24 or (ver[0] == "24" and int(ver[1]) >= 5):
if int(ver[0].replace("v", "")) > 24 or (ver[0] == "24" and int(ver[1]) >= 5):
pl.add_option(
OptionKeys.OverridePay,
OptionDefaults.DefaultOverridePay,
Expand Down
2 changes: 1 addition & 1 deletion tools/plugins/mpay/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class Network(StrEnum):


PLUGIN_NAME = "mpay"
VERSION = "0.1.2"
VERSION = "0.1.3"

GRPC_HOST_REGTEST = "0.0.0.0" # noqa: S104
6 changes: 4 additions & 2 deletions tools/plugins/mpay/protos/mpay.proto
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ message ResetPathMemoryResponse {
}

message PayStatusRequest {
optional string bolt11 = 1;
optional string invoice = 1;
}

message PayStatusResponse {
Expand Down Expand Up @@ -157,7 +157,9 @@ message PayStatusResponse {
optional Failure failure = 7;
}

string bolt11 = 1;
optional string bolt11 = 1;
optional string bolt12 = 5;

uint64 amount_msat = 2;
string destination = 3;
repeated Attempt attempts = 4;
Expand Down
Loading

0 comments on commit 8dca13c

Please sign in to comment.