This commit is contained in:
hatim boufnichel 2022-11-04 21:22:13 +01:00
parent e108c3642e
commit c5fd454089
2 changed files with 83 additions and 37 deletions

View file

@ -5,36 +5,69 @@ import fs from 'fs'
import { LightningClient } from '../../../proto/lnd/rpc.client' import { LightningClient } from '../../../proto/lnd/rpc.client'
import { InvoicesClient } from '../../../proto/lnd/invoices.client' import { InvoicesClient } from '../../../proto/lnd/invoices.client'
import { RouterClient } from '../../../proto/lnd/router.client' import { RouterClient } from '../../../proto/lnd/router.client'
import * as Types from '../../../proto/autogenerated/ts/types' import { GetInfoResponse } from '../../../proto/lnd/rpc'
import { GetInfoRequest, GetInfoResponse } from '../../../proto/lnd/rpc'
const lndAddr = process.env.LND_ADDRESS;
const lndCertPath = process.env.LND_CERT_PATH;
const lndMacaroonPath = process.env.LND_MACAROON_PATH;
if (!lndAddr || !lndCertPath || !lndMacaroonPath) {
throw new Error(`Something missing from ADDR/TLS/MACAROON`);
}
const lndCert = fs.readFileSync(lndCertPath);
const macaroon = fs.readFileSync(lndMacaroonPath).toString('hex');
const sslCreds = credentials.createSsl(lndCert);
const macaroonCreds = credentials.createFromMetadataGenerator(
function (args: any, callback: any) {
let metadata = new Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
},
);
const creds = credentials.combineChannelCredentials(
sslCreds,
macaroonCreds,
);
const transport = new GrpcTransport({ host: lndAddr, channelCredentials: creds })
const lightning = new LightningClient(transport)
const invoices = new InvoicesClient(transport)
const router = new RouterClient(transport)
const DefaultMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline }) const DefaultMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline })
export default class {
lightning: LightningClient
invoices: InvoicesClient
router: RouterClient
constructor() {
const lndAddr = process.env.LND_ADDRESS;
const lndCertPath = process.env.LND_CERT_PATH;
const lndMacaroonPath = process.env.LND_MACAROON_PATH;
if (!lndAddr || !lndCertPath || !lndMacaroonPath) {
throw new Error(`Something missing from ADDR/TLS/MACAROON`);
}
const lndCert = fs.readFileSync(lndCertPath);
const macaroon = fs.readFileSync(lndMacaroonPath).toString('hex');
const sslCreds = credentials.createSsl(lndCert);
const macaroonCreds = credentials.createFromMetadataGenerator(
function (args: any, callback: any) {
let metadata = new Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
},
);
const creds = credentials.combineChannelCredentials(
sslCreds,
macaroonCreds,
);
const transport = new GrpcTransport({ host: lndAddr, channelCredentials: creds })
this.lightning = new LightningClient(transport)
this.invoices = new InvoicesClient(transport)
this.router = new RouterClient(transport)
}
async GetInfo(): Promise<GetInfoResponse> {
const res = await this.lightning.getInfo({}, DefaultMetadata())
return res.response
}
async OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats) {
const stream = this.lightning.openChannel({
nodePubkey: Buffer.from(destination, 'hex'),
closeAddress: closeAddress,
localFundingAmount: fundingAmount,
pushSats: pushSats,
sa: satPerByte
})
export default { return new Promise(res => {
getInfo: async (): Promise<GetInfoResponse> => (await lightning.getInfo({}, DefaultMetadata())).response stream.on('data', response => {
if (response) {
res(true)
}
})
stream.on('error', err => {
if (err) {
console.error("err")
console.error(err)
this.statusAll(true)
// move to next client after the refresh
.then(() => this.nextPreferredClient())
res(false)
}
})
})
}
} }

View file

@ -1,12 +1,25 @@
import Storage from '../storage'
import * as Types from '../../../proto/autogenerated/ts/types' import * as Types from '../../../proto/autogenerated/ts/types'
import lnd from '../lnd' import LND from '../lnd'
const methods: Types.ServerMethods = { export default class {
EncryptionExchange: async (ctx: Types.EncryptionExchange_Context, req: Types.EncryptionExchangeRequest): Promise<void> => { }, storage: Storage
Health: async (ctx: Types.Health_Context): Promise<void> => { }, lnd: LND
LndGetInfo: async (ctx: Types.LndGetInfo_Context): Promise<Types.LndGetInfoResponse> => { constructor(storageHandler: Storage, lndHandler: LND) {
const info = await lnd.getInfo() this.storage = storageHandler
return { alias: info.alias } this.lnd = lndHandler
}
async AddUser(req: Types.AddUserRequest): Promise<Types.AddUserResponse> {
const newUser = await this.storage.AddUser(req.name, req.callback_url, req.secret)
return {
user_id: newUser.user_id,
auth_token: "TMP"
}
}
async OpenChannel(req: Types.OpenChannelRequest): Promise<Types.OpenChannelResponse> {
} }
} async PayInvoice(rootToken: string, invoice: string): Promise<string> { return "" }
export default methods async GenerateInvoice(rootToken: string, amountSats: number): Promise<string> { return "" }
async GenerateAddress(rootToken: string): Promise<string> { return "" }
// payment received sub
}