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 { 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 DefaultMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline })
|
||||||
const lndAddr = process.env.LND_ADDRESS;
|
export default class {
|
||||||
const lndCertPath = process.env.LND_CERT_PATH;
|
lightning: LightningClient
|
||||||
const lndMacaroonPath = process.env.LND_MACAROON_PATH;
|
invoices: InvoicesClient
|
||||||
if (!lndAddr || !lndCertPath || !lndMacaroonPath) {
|
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`);
|
throw new Error(`Something missing from ADDR/TLS/MACAROON`);
|
||||||
}
|
}
|
||||||
const lndCert = fs.readFileSync(lndCertPath);
|
const lndCert = fs.readFileSync(lndCertPath);
|
||||||
const macaroon = fs.readFileSync(lndMacaroonPath).toString('hex');
|
const macaroon = fs.readFileSync(lndMacaroonPath).toString('hex');
|
||||||
const sslCreds = credentials.createSsl(lndCert);
|
const sslCreds = credentials.createSsl(lndCert);
|
||||||
const macaroonCreds = credentials.createFromMetadataGenerator(
|
const macaroonCreds = credentials.createFromMetadataGenerator(
|
||||||
function (args: any, callback: any) {
|
function (args: any, callback: any) {
|
||||||
let metadata = new Metadata();
|
let metadata = new Metadata();
|
||||||
metadata.add('macaroon', macaroon);
|
metadata.add('macaroon', macaroon);
|
||||||
callback(null, metadata);
|
callback(null, metadata);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const creds = credentials.combineChannelCredentials(
|
const creds = credentials.combineChannelCredentials(
|
||||||
sslCreds,
|
sslCreds,
|
||||||
macaroonCreds,
|
macaroonCreds,
|
||||||
);
|
);
|
||||||
const transport = new GrpcTransport({ host: lndAddr, channelCredentials: creds })
|
const transport = new GrpcTransport({ host: lndAddr, channelCredentials: creds })
|
||||||
const lightning = new LightningClient(transport)
|
this.lightning = new LightningClient(transport)
|
||||||
const invoices = new InvoicesClient(transport)
|
this.invoices = new InvoicesClient(transport)
|
||||||
const router = new RouterClient(transport)
|
this.router = new RouterClient(transport)
|
||||||
const DefaultMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline })
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 "" }
|
||||||
|
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