update
This commit is contained in:
parent
e108c3642e
commit
c5fd454089
2 changed files with 83 additions and 37 deletions
|
|
@ -5,36 +5,69 @@ import fs from 'fs'
|
|||
import { LightningClient } from '../../../proto/lnd/rpc.client'
|
||||
import { InvoicesClient } from '../../../proto/lnd/invoices.client'
|
||||
import { RouterClient } from '../../../proto/lnd/router.client'
|
||||
import * as Types from '../../../proto/autogenerated/ts/types'
|
||||
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) {
|
||||
import { GetInfoResponse } from '../../../proto/lnd/rpc'
|
||||
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(
|
||||
}
|
||||
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(
|
||||
);
|
||||
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 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 {
|
||||
getInfo: async (): Promise<GetInfoResponse> => (await lightning.getInfo({}, DefaultMetadata())).response
|
||||
return new Promise(res => {
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,25 @@
|
|||
import Storage from '../storage'
|
||||
import * as Types from '../../../proto/autogenerated/ts/types'
|
||||
import lnd from '../lnd'
|
||||
const methods: Types.ServerMethods = {
|
||||
EncryptionExchange: async (ctx: Types.EncryptionExchange_Context, req: Types.EncryptionExchangeRequest): Promise<void> => { },
|
||||
Health: async (ctx: Types.Health_Context): Promise<void> => { },
|
||||
LndGetInfo: async (ctx: Types.LndGetInfo_Context): Promise<Types.LndGetInfoResponse> => {
|
||||
const info = await lnd.getInfo()
|
||||
return { alias: info.alias }
|
||||
import LND from '../lnd'
|
||||
export default class {
|
||||
storage: Storage
|
||||
lnd: LND
|
||||
constructor(storageHandler: Storage, lndHandler: LND) {
|
||||
this.storage = storageHandler
|
||||
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 "" }
|
||||
async GenerateInvoice(rootToken: string, amountSats: number): Promise<string> { return "" }
|
||||
async GenerateAddress(rootToken: string): Promise<string> { return "" }
|
||||
// payment received sub
|
||||
}
|
||||
export default methods
|
||||
Loading…
Add table
Add a link
Reference in a new issue