Merge branch 'master' of https://github.com/shocknet/Lightning.Pub
This commit is contained in:
commit
4bc05953e1
6 changed files with 40 additions and 12 deletions
|
|
@ -17,7 +17,7 @@ const start = async () => {
|
||||||
log("manual process ended")
|
log("manual process ended")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const { apps, mainHandler, liquidityProviderInfo, wizard } = keepOn
|
const { apps, mainHandler, liquidityProviderInfo, wizard, adminManager } = keepOn
|
||||||
const serverMethods = GetServerMethods(mainHandler)
|
const serverMethods = GetServerMethods(mainHandler)
|
||||||
const nostrSettings = LoadNosrtSettingsFromEnv()
|
const nostrSettings = LoadNosrtSettingsFromEnv()
|
||||||
log("initializing nostr middleware")
|
log("initializing nostr middleware")
|
||||||
|
|
@ -28,9 +28,11 @@ const start = async () => {
|
||||||
log("starting server")
|
log("starting server")
|
||||||
mainHandler.attachNostrSend(Send)
|
mainHandler.attachNostrSend(Send)
|
||||||
mainHandler.StartBeacons()
|
mainHandler.StartBeacons()
|
||||||
|
const appNprofile = encodeNprofile({ pubkey: liquidityProviderInfo.publicKey, relays: nostrSettings.relays })
|
||||||
if (wizard) {
|
if (wizard) {
|
||||||
wizard.AddConnectInfo(encodeNprofile({ pubkey: liquidityProviderInfo.publicKey, relays: nostrSettings.relays }), nostrSettings.relays)
|
wizard.AddConnectInfo(appNprofile, nostrSettings.relays)
|
||||||
}
|
}
|
||||||
|
adminManager.setAppNprofile(appNprofile)
|
||||||
const Server = NewServer(serverMethods, serverOptions(mainHandler))
|
const Server = NewServer(serverMethods, serverOptions(mainHandler))
|
||||||
Server.Listen(mainSettings.servicePort)
|
Server.Listen(mainSettings.servicePort)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -236,7 +236,6 @@ export default class {
|
||||||
|
|
||||||
async NewAddress(addressType: Types.AddressType, { useProvider, from }: TxActionOptions): Promise<NewAddressResponse> {
|
async NewAddress(addressType: Types.AddressType, { useProvider, from }: TxActionOptions): Promise<NewAddressResponse> {
|
||||||
|
|
||||||
await this.Health()
|
|
||||||
let lndAddressType: AddressType
|
let lndAddressType: AddressType
|
||||||
switch (addressType) {
|
switch (addressType) {
|
||||||
case Types.AddressType.NESTED_PUBKEY_HASH:
|
case Types.AddressType.NESTED_PUBKEY_HASH:
|
||||||
|
|
@ -254,6 +253,7 @@ export default class {
|
||||||
if (useProvider) {
|
if (useProvider) {
|
||||||
throw new Error("provider payments not support chain payments yet")
|
throw new Error("provider payments not support chain payments yet")
|
||||||
}
|
}
|
||||||
|
await this.Health()
|
||||||
try {
|
try {
|
||||||
const res = await this.lightning.newAddress({ account: "", type: lndAddressType }, DeadLineMetadata())
|
const res = await this.lightning.newAddress({ account: "", type: lndAddressType }, DeadLineMetadata())
|
||||||
this.utils.stateBundler.AddTxPoint('addedAddress', 1, { from, used: 'lnd' })
|
this.utils.stateBundler.AddTxPoint('addedAddress', 1, { from, used: 'lnd' })
|
||||||
|
|
@ -265,12 +265,12 @@ export default class {
|
||||||
}
|
}
|
||||||
|
|
||||||
async NewInvoice(value: number, memo: string, expiry: number, { useProvider, from }: TxActionOptions): Promise<Invoice> {
|
async NewInvoice(value: number, memo: string, expiry: number, { useProvider, from }: TxActionOptions): Promise<Invoice> {
|
||||||
await this.Health()
|
|
||||||
if (useProvider) {
|
if (useProvider) {
|
||||||
const invoice = await this.liquidProvider.AddInvoice(value, memo, from)
|
const invoice = await this.liquidProvider.AddInvoice(value, memo, from)
|
||||||
const providerDst = this.liquidProvider.GetProviderDestination()
|
const providerDst = this.liquidProvider.GetProviderDestination()
|
||||||
return { payRequest: invoice, providerDst }
|
return { payRequest: invoice, providerDst }
|
||||||
}
|
}
|
||||||
|
await this.Health()
|
||||||
try {
|
try {
|
||||||
const res = await this.lightning.addInvoice(AddInvoiceReq(value, expiry, true, memo), DeadLineMetadata())
|
const res = await this.lightning.addInvoice(AddInvoiceReq(value, expiry, true, memo), DeadLineMetadata())
|
||||||
this.utils.stateBundler.AddTxPoint('addedInvoice', value, { from, used: 'lnd' })
|
this.utils.stateBundler.AddTxPoint('addedInvoice', value, { from, used: 'lnd' })
|
||||||
|
|
@ -304,12 +304,12 @@ export default class {
|
||||||
this.log("outgoing ops locked, rejecting payment request")
|
this.log("outgoing ops locked, rejecting payment request")
|
||||||
throw new Error("lnd node is currently out of sync")
|
throw new Error("lnd node is currently out of sync")
|
||||||
}
|
}
|
||||||
await this.Health()
|
|
||||||
if (useProvider) {
|
if (useProvider) {
|
||||||
const res = await this.liquidProvider.PayInvoice(invoice, decodedAmount, from)
|
const res = await this.liquidProvider.PayInvoice(invoice, decodedAmount, from)
|
||||||
const providerDst = this.liquidProvider.GetProviderDestination()
|
const providerDst = this.liquidProvider.GetProviderDestination()
|
||||||
return { feeSat: res.network_fee + res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage, providerDst }
|
return { feeSat: res.network_fee + res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage, providerDst }
|
||||||
}
|
}
|
||||||
|
await this.Health()
|
||||||
try {
|
try {
|
||||||
const abortController = new AbortController()
|
const abortController = new AbortController()
|
||||||
const req = PayInvoiceReq(invoice, amount, feeLimit)
|
const req = PayInvoiceReq(invoice, amount, feeLimit)
|
||||||
|
|
@ -320,6 +320,7 @@ export default class {
|
||||||
rej(error)
|
rej(error)
|
||||||
})
|
})
|
||||||
stream.responses.onMessage(payment => {
|
stream.responses.onMessage(payment => {
|
||||||
|
console.log("payment", payment)
|
||||||
switch (payment.status) {
|
switch (payment.status) {
|
||||||
case Payment_PaymentStatus.FAILED:
|
case Payment_PaymentStatus.FAILED:
|
||||||
this.log("invoice payment failed", payment.failureReason)
|
this.log("invoice payment failed", payment.failureReason)
|
||||||
|
|
@ -358,8 +359,8 @@ export default class {
|
||||||
if (useProvider) {
|
if (useProvider) {
|
||||||
throw new Error("provider payments not support chain payments yet")
|
throw new Error("provider payments not support chain payments yet")
|
||||||
}
|
}
|
||||||
|
await this.Health()
|
||||||
try {
|
try {
|
||||||
await this.Health()
|
|
||||||
const res = await this.lightning.sendCoins(SendCoinsReq(address, amount, satPerVByte, label), DeadLineMetadata())
|
const res = await this.lightning.sendCoins(SendCoinsReq(address, amount, satPerVByte, label), DeadLineMetadata())
|
||||||
this.utils.stateBundler.AddTxPoint('paidAnAddress', amount, { from, used: 'lnd', timeDiscount: true })
|
this.utils.stateBundler.AddTxPoint('paidAnAddress', amount, { from, used: 'lnd', timeDiscount: true })
|
||||||
return res.response
|
return res.response
|
||||||
|
|
@ -370,7 +371,6 @@ export default class {
|
||||||
}
|
}
|
||||||
|
|
||||||
async GetTransactions(startHeight: number): Promise<TransactionDetails> {
|
async GetTransactions(startHeight: number): Promise<TransactionDetails> {
|
||||||
await this.Health()
|
|
||||||
const res = await this.lightning.getTransactions({ startHeight, endHeight: 0, account: "" }, DeadLineMetadata())
|
const res = await this.lightning.getTransactions({ startHeight, endHeight: 0, account: "" }, DeadLineMetadata())
|
||||||
return res.response
|
return res.response
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,29 @@ export class AdminManager {
|
||||||
dataDir: string
|
dataDir: string
|
||||||
adminNpubPath: string
|
adminNpubPath: string
|
||||||
adminEnrollTokenPath: string
|
adminEnrollTokenPath: string
|
||||||
|
adminConnectPath: string
|
||||||
|
appNprofilePath: string
|
||||||
interval: NodeJS.Timer
|
interval: NodeJS.Timer
|
||||||
|
appNprofile: string
|
||||||
constructor(mainSettings: MainSettings, storage: Storage) {
|
constructor(mainSettings: MainSettings, storage: Storage) {
|
||||||
this.storage = storage
|
this.storage = storage
|
||||||
this.dataDir = mainSettings.storageSettings.dataDir
|
this.dataDir = mainSettings.storageSettings.dataDir
|
||||||
this.adminNpubPath = getDataPath(this.dataDir, 'admin.npub')
|
this.adminNpubPath = getDataPath(this.dataDir, 'admin.npub')
|
||||||
this.adminEnrollTokenPath = getDataPath(this.dataDir, '.admin_enroll')
|
this.adminEnrollTokenPath = getDataPath(this.dataDir, '.admin_enroll')
|
||||||
|
this.adminConnectPath = getDataPath(this.dataDir, '.admin_connect')
|
||||||
|
this.appNprofilePath = getDataPath(this.dataDir, 'app.nprofile')
|
||||||
this.start()
|
this.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setAppNprofile = (nprofile: string) => {
|
||||||
|
this.appNprofile = nprofile
|
||||||
|
const enrollToken = this.ReadAdminEnrollToken()
|
||||||
|
fs.writeFileSync(this.appNprofilePath, this.appNprofile)
|
||||||
|
if (enrollToken) {
|
||||||
|
const connectString = `${this.appNprofile}:${enrollToken}`
|
||||||
|
fs.writeFileSync(this.adminConnectPath, connectString)
|
||||||
|
}
|
||||||
|
}
|
||||||
Stop = () => {
|
Stop = () => {
|
||||||
clearInterval(this.interval)
|
clearInterval(this.interval)
|
||||||
}
|
}
|
||||||
|
|
@ -25,6 +40,8 @@ export class AdminManager {
|
||||||
GenerateAdminEnrollToken = async () => {
|
GenerateAdminEnrollToken = async () => {
|
||||||
const token = crypto.randomBytes(32).toString('hex')
|
const token = crypto.randomBytes(32).toString('hex')
|
||||||
fs.writeFileSync(this.adminEnrollTokenPath, token)
|
fs.writeFileSync(this.adminEnrollTokenPath, token)
|
||||||
|
const connectString = `${this.appNprofile}:${token}`
|
||||||
|
fs.writeFileSync(this.adminConnectPath, connectString)
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -92,6 +109,7 @@ export class AdminManager {
|
||||||
}
|
}
|
||||||
fs.writeFileSync(this.adminNpubPath, npub)
|
fs.writeFileSync(this.adminNpubPath, npub)
|
||||||
fs.unlinkSync(this.adminEnrollTokenPath)
|
fs.unlinkSync(this.adminEnrollTokenPath)
|
||||||
|
fs.unlinkSync(this.adminConnectPath)
|
||||||
this.adminNpub = npub
|
this.adminNpub = npub
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -89,15 +89,19 @@ export class LiquidityProvider {
|
||||||
this.ready = true
|
this.ready = true
|
||||||
this.queue.forEach(q => q('ready'))
|
this.queue.forEach(q => q('ready'))
|
||||||
this.log("subbing to user operations")
|
this.log("subbing to user operations")
|
||||||
this.client.GetLiveUserOperations(res => {
|
this.client.GetLiveUserOperations(async res => {
|
||||||
if (res.status === 'ERROR') {
|
if (res.status === 'ERROR') {
|
||||||
this.log("error getting user operations", res.reason)
|
this.log("error getting user operations", res.reason)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//this.log("got user operation", res.operation)
|
//this.log("got user operation", res.operation)
|
||||||
if (res.operation.type === Types.UserOperationType.INCOMING_INVOICE) {
|
if (res.operation.type === Types.UserOperationType.INCOMING_INVOICE) {
|
||||||
this.incrementProviderBalance(res.operation.amount)
|
try {
|
||||||
this.invoicePaidCb(res.operation.identifier, res.operation.amount, 'provider')
|
await this.invoicePaidCb(res.operation.identifier, res.operation.amount, 'provider')
|
||||||
|
this.incrementProviderBalance(res.operation.amount)
|
||||||
|
} catch (err: any) {
|
||||||
|
this.log("error processing incoming invoice", err.message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -105,7 +109,9 @@ export class LiquidityProvider {
|
||||||
GetUserState = async () => {
|
GetUserState = async () => {
|
||||||
const res = await Promise.race([this.client.GetUserInfo(), new Promise<Types.ResultError>(res => setTimeout(() => res({ status: 'ERROR', reason: 'timeout' }), 10 * 1000))])
|
const res = await Promise.race([this.client.GetUserInfo(), new Promise<Types.ResultError>(res => setTimeout(() => res({ status: 'ERROR', reason: 'timeout' }), 10 * 1000))])
|
||||||
if (res.status === 'ERROR') {
|
if (res.status === 'ERROR') {
|
||||||
this.log("error getting user info", res.reason)
|
if (res.reason !== 'timeout') {
|
||||||
|
this.log("error getting user info", res.reason)
|
||||||
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
this.utils.stateBundler.AddBalancePoint('providerBalance', res.balance)
|
this.utils.stateBundler.AddBalancePoint('providerBalance', res.balance)
|
||||||
|
|
|
||||||
|
|
@ -240,8 +240,9 @@ export class Unlocker {
|
||||||
try {
|
try {
|
||||||
const { encryptedData } = this.EncryptBackup(Buffer.from(msg.multiChanBackup.multiChanBackup))
|
const { encryptedData } = this.EncryptBackup(Buffer.from(msg.multiChanBackup.multiChanBackup))
|
||||||
await this.storage.liquidityStorage.SaveNodeBackup(pub, JSON.stringify(encryptedData))
|
await this.storage.liquidityStorage.SaveNodeBackup(pub, JSON.stringify(encryptedData))
|
||||||
|
this.log("new channel backup saved correctly")
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
this.log("failed to save backup", err.message)
|
this.log("new channel backup was not saved")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,7 @@ export class Wizard {
|
||||||
|
|
||||||
AddConnectInfo = (nprofile: string, relays: string[]) => {
|
AddConnectInfo = (nprofile: string, relays: string[]) => {
|
||||||
this.nprofile = nprofile
|
this.nprofile = nprofile
|
||||||
|
this.relays = relays
|
||||||
this.awaitingNprofile.forEach(q => q.res(nprofile))
|
this.awaitingNprofile.forEach(q => q.res(nprofile))
|
||||||
this.awaitingNprofile = []
|
this.awaitingNprofile = []
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue