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

fix: LNC typings #2847

Merged
merged 21 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e336e07
fix: add typings for lnc
reneaaron Nov 6, 2023
c251b36
fix: upgrade lnc to latest version
reneaaron Nov 6, 2023
d74904f
Merge branch 'master' into fix/lnc-typings
reneaaron Nov 10, 2023
11b2bce
Merge remote-tracking branch 'upstream/master' into fix/lnc-typings
pavanjoshi914 Dec 29, 2023
18ecb5c
Merge branch 'master' into fix/lnc-typings
reneaaron Mar 12, 2024
d0bd8a0
fix: upgrade package and adopt lnc connector to updated types
reneaaron Mar 12, 2024
0a29de8
fix: use enum
reneaaron Mar 13, 2024
4a57ceb
fix: formatting
reneaaron Mar 13, 2024
7b62c33
fix: add payment hash
reneaaron Mar 13, 2024
c461a71
fix: formatting
reneaaron Mar 13, 2024
31de2a9
Merge branch 'master' into fix/lnc-typings
reneaaron Mar 13, 2024
735104c
fix: use strings instead of constant because of compile errors in web…
reneaaron Mar 13, 2024
ddb8ba4
Merge branch 'fix/lnc-typings' of https://github.com/getAlby/lightnin…
reneaaron Mar 13, 2024
21591c0
fix: add docs for settleDate
reneaaron Mar 13, 2024
409e7c5
Merge branch 'master' into fix/lnc-typings
reneaaron Mar 13, 2024
761de13
fix: conversion of msat
reneaaron Mar 13, 2024
49eb9a4
Merge branch 'fix/lnc-typings' of https://github.com/getAlby/lightnin…
reneaaron Mar 13, 2024
8b0a0f1
fix: msat conversion part 2
reneaaron Mar 13, 2024
40166e2
Merge branch 'master' into fix/lnc-typings
reneaaron Mar 13, 2024
9c0da45
Merge branch 'master' into fix/lnc-typings
reneaaron Mar 13, 2024
fdb042e
Merge branch 'master' into fix/lnc-typings
pavanjoshi914 Mar 13, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@bitcoinerlab/secp256k1": "^1.0.5",
"@getalby/sdk": "^3.4.0",
"@headlessui/react": "^1.7.18",
"@lightninglabs/lnc-web": "^0.2.4-alpha",
"@lightninglabs/lnc-web": "^0.3.1-alpha",
"@noble/ciphers": "^0.5.1",
"@noble/curves": "^1.3.0",
"@noble/hashes": "^1.3.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface ConnectorTransaction {
preimage: string;
payment_hash?: string;
settled: boolean;
/**
* Settle date in UNIX milliseconds
*/
settleDate: number;
totalAmount: number;
type: "received" | "sent";
Expand Down
236 changes: 122 additions & 114 deletions src/extension/background-script/connectors/lnc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Invoice } from "@lightninglabs/lnc-core/dist/types/proto/lnd/lightning";
import LNC from "@lightninglabs/lnc-web";
import lightningPayReq from "bolt11";
import Base64 from "crypto-js/enc-base64";
Expand All @@ -8,16 +9,14 @@ import SHA256 from "crypto-js/sha256";
import snakeCase from "lodash.snakecase";
import { encryptData } from "~/common/lib/crypto";
import utils from "~/common/lib/utils";
import { Account } from "~/types";

import { mergeTransactions } from "~/common/utils/helpers";
import { Account } from "~/types";
import state from "../state";
import Connector, {
CheckPaymentArgs,
CheckPaymentResponse,
ConnectorTransaction,
ConnectPeerResponse,
flattenRequestMethods,
ConnectorTransaction,
GetBalanceResponse,
GetInfoResponse,
GetTransactionsResponse,
Expand All @@ -28,6 +27,7 @@ import Connector, {
SendPaymentResponse,
SignMessageArgs,
SignMessageResponse,
flattenRequestMethods,
} from "./connector.interface";

interface Config {
Expand Down Expand Up @@ -160,7 +160,7 @@ class LncCredentialStore {
class Lnc implements Connector {
account: Account;
config: Config;
lnc: FixMe;
lnc: LNC;

constructor(account: Account, config: Config) {
this.account = account;
Expand Down Expand Up @@ -188,7 +188,6 @@ class Lnc implements Connector {
try {
console.info("LNC disconnect");
await this.lnc.disconnect();
delete this.lnc;
} catch (error) {
console.error("Unload LNC failed", error);
}
Expand Down Expand Up @@ -224,47 +223,50 @@ class Lnc implements Connector {
});
}

getInfo(): Promise<GetInfoResponse> {
async getInfo(): Promise<GetInfoResponse> {
this.checkConnection();
return this.lnc.lnd.lightning.GetInfo().then((data: FixMe) => {
return {
data: {
alias: data.alias,
pubkey: data.identityPubkey,
color: data.color,
},
};
});

const data = await this.lnc.lnd.lightning.getInfo();

return {
data: {
alias: data.alias,
pubkey: data.identityPubkey,
color: data.color,
},
};
}

getBalance(): Promise<GetBalanceResponse> {
async getBalance(): Promise<GetBalanceResponse> {
this.checkConnection();
return this.lnc.lnd.lightning.ChannelBalance().then((data: FixMe) => {
return {
data: {
balance: data.balance,
},
};
});

const data = await this.lnc.lnd.lightning.channelBalance();

return {
data: {
balance: parseInt(data.localBalance?.sat ?? ""),
},
};
}

private async getInvoices(): Promise<ConnectorTransaction[]> {
this.checkConnection();
const data = await this.lnc.lnd.lightning.ListInvoices({ reversed: true });
const data = await this.lnc.lnd.lightning.listInvoices({ reversed: true });

const invoices: ConnectorTransaction[] = data.invoices
.map((invoice: FixMe, index: number): ConnectorTransaction => {
const custom_records =
invoice.htlcs[0] && invoice.htlcs[0].customRecords;
.map((invoice: Invoice, index: number): ConnectorTransaction => {
let custom_records;

// TODO: Fill custom records from HTLC

return {
custom_records,
id: `${invoice.paymentRequest}-${index}`,
memo: invoice.memo,
preimage: invoice.rPreimage,
settled: invoice.settled,
preimage: invoice.rPreimage.toString(),
settled: invoice.state === "SETTLED",
settleDate: parseInt(invoice.settleDate) * 1000,
totalAmount: invoice.value,
totalAmount: parseInt(invoice.value),
type: "received",
};
})
Expand All @@ -290,31 +292,33 @@ class Lnc implements Connector {
}

private async getPayments(): Promise<ConnectorTransaction[]> {
const outgoingInvoicesResponse = await this.lnc.lnd.lightning.ListPayments({
const outgoingInvoicesResponse = await this.lnc.lnd.lightning.listPayments({
reversed: true,
max_payments: 100,
include_incomplete: false,
maxPayments: "100",
includeIncomplete: false,
});

const outgoingInvoices: ConnectorTransaction[] =
outgoingInvoicesResponse.payments.map(
(payment: FixMe, index: number): ConnectorTransaction => {
(payment, index): ConnectorTransaction => {
let memo = "Sent";
if (payment.payment_request) {
if (payment.paymentRequest) {
memo = (
lightningPayReq
.decode(payment.payment_request)
.decode(payment.paymentRequest)
.tags.find((tag) => tag.tagName === "description")?.data ||
"Sent"
).toString();
}

return {
id: `${payment.payment_request}-${index}`,
id: `${payment.paymentRequest}-${index}`,
memo: memo,
preimage: payment.payment_preimage,
preimage: payment.paymentPreimage,
payment_hash: payment.paymentHash,
settled: true,
settleDate: parseInt(payment.creation_date) * 1000,
totalAmount: payment.value_sat,
settleDate: parseInt(payment.creationTimeNs) / 1_000_000,
totalAmount: parseInt(payment.valueSat),
type: "sent",
};
}
Expand All @@ -332,41 +336,43 @@ class Lnc implements Connector {

async checkPayment(args: CheckPaymentArgs): Promise<CheckPaymentResponse> {
this.checkConnection();
return this.lnc.lnd.lightning
.LookupInvoice({ r_hash_str: args.paymentHash })
.then((data: FixMe) => {
return {
data: {
paid: data.settled,
},
};
});

const data = await this.lnc.lnd.lightning.lookupInvoice({
rHash: args.paymentHash,
});
return {
data: {
paid: data.state === "SETTLED",
},
};
}

sendPayment(args: SendPaymentArgs): Promise<SendPaymentResponse> {
async sendPayment(args: SendPaymentArgs): Promise<SendPaymentResponse> {
this.checkConnection();
return this.lnc.lnd.lightning
.SendPaymentSync({
payment_request: args.paymentRequest,
})
.then((data: FixMe) => {
if (data.paymentError) {
throw new Error(data.paymentError);
}
return {
data: {
preimage: utils.base64ToHex(data.paymentPreimage),
paymentHash: utils.base64ToHex(data.paymentHash),
route: {
total_amt: data.paymentRoute.totalAmt,
total_fees: data.paymentRoute.totalFees,
},
},
};
});

const data = await this.lnc.lnd.lightning.sendPaymentSync({
paymentRequest: args.paymentRequest,
});

if (data.paymentError) {
throw new Error(data.paymentError);
}

return {
data: {
preimage: utils.base64ToHex(data.paymentPreimage.toString()),
paymentHash: utils.base64ToHex(data.paymentHash.toString()),
route: {
total_amt: parseInt(data.paymentRoute?.totalAmtMsat ?? "0") / 1000,
total_fees: parseInt(data.paymentRoute?.totalFeesMsat ?? "0") / 1000,
},
},
};
}

async keysend(args: KeysendArgs): Promise<SendPaymentResponse> {
this.checkConnection();

//See: https://gist.github.com/dellagustin/c3793308b75b6b0faf134e64db7dc915
const dest_pubkey_hex = args.pubkey;
const dest_pubkey_base64 = Hex.parse(dest_pubkey_hex).toString(Base64);
Expand All @@ -382,59 +388,61 @@ class Lnc implements Connector {
args.customRecords[key]
).toString(Base64);
}

//mandatory record for keysend
records_base64[5482373484] = preimage_base64;

return this.lnc.lnd.lightning
.SendPaymentSync({
dest: dest_pubkey_base64,
amt: args.amount,
payment_hash: hash,
dest_custom_records: records_base64,
})
.then((data: FixMe) => {
if (data.paymentError) {
throw new Error(data.paymentError);
}
return {
data: {
preimage: utils.base64ToHex(data.paymentPreimage),
paymentHash: utils.base64ToHex(data.paymentHash),
route: {
total_amt: data.paymentRoute.totalAmt,
total_fees: data.paymentRoute.totalFees,
},
},
};
});
const data = await this.lnc.lnd.lightning.sendPaymentSync({
dest: dest_pubkey_base64,
amt: args.amount.toString(),
paymentHash: hash,
destCustomRecords: records_base64,
});

if (data.paymentError) {
throw new Error(data.paymentError);
}

return {
data: {
preimage: utils.base64ToHex(data.paymentPreimage.toString()),
paymentHash: utils.base64ToHex(data.paymentHash.toString()),
route: {
total_amt: parseInt(data.paymentRoute?.totalAmtMsat ?? "0") / 1000,
total_fees: parseInt(data.paymentRoute?.totalFeesMsat ?? "0") / 1000,
},
},
};
}

signMessage(args: SignMessageArgs): Promise<SignMessageResponse> {
async signMessage(args: SignMessageArgs): Promise<SignMessageResponse> {
this.checkConnection();
return this.lnc.lnd.lightning
.SignMessage({ msg: Base64.stringify(UTF8.parse(args.message)) })
.then((data: FixMe) => {
return {
data: {
message: args.message,
signature: data.signature,
},
};
});

const data = await this.lnc.lnd.lightning.signMessage({
msg: Base64.stringify(UTF8.parse(args.message)),
});

return {
data: {
message: args.message,
signature: data.signature,
},
};
}

makeInvoice(args: MakeInvoiceArgs): Promise<MakeInvoiceResponse> {
async makeInvoice(args: MakeInvoiceArgs): Promise<MakeInvoiceResponse> {
this.checkConnection();
return this.lnc.lnd.lightning
.AddInvoice({ memo: args.memo, value: args.amount })
.then((data: FixMe) => {
return {
data: {
paymentRequest: data.paymentRequest,
rHash: utils.base64ToHex(data.rHash),
},
};
});
const data = await this.lnc.lnd.lightning.addInvoice({
memo: args.memo,
value: args.amount.toString(),
});

return {
data: {
paymentRequest: data.paymentRequest,
rHash: utils.base64ToHex(data.rHash.toString()),
},
};
}

private checkConnection() {
Expand Down
Loading
Loading