Skip to content

Commit

Permalink
Merge pull request #684 from shocknet/state-beacon
Browse files Browse the repository at this point in the history
update state beacon
  • Loading branch information
shocknet-justin authored May 9, 2024
2 parents a1c9e18 + 760d367 commit 64ae5a0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const start = async () => {
const nostrSettings = LoadNosrtSettingsFromEnv()
const { Send } = nostrMiddleware(serverMethods, mainHandler, { ...nostrSettings, apps })
mainHandler.attachNostrSend(Send)
mainHandler.StartBeacons()
const Server = NewServer(serverMethods, serverOptions(mainHandler))
Server.Listen(mainSettings.servicePort)
}
Expand Down
33 changes: 30 additions & 3 deletions src/services/main/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { MainSettings } from './settings.js'
import PaymentManager from './paymentManager.js'
import { InboundOptionals, defaultInvoiceExpiry } from '../storage/paymentStorage.js'
import { ApplicationUser } from '../storage/entity/ApplicationUser.js'
import { getLogger } from '../helpers/logger.js'
import { PubLogger, getLogger } from '../helpers/logger.js'
import crypto from 'crypto'
import { Application } from '../storage/entity/Application.js'

const TOKEN_EXPIRY_TIME = 2 * 60 * 1000 // 2 minutes, in milliseconds

Expand All @@ -19,11 +20,18 @@ export default class {
settings: MainSettings
paymentManager: PaymentManager
nPubLinkingTokens = new Map<string, NsecLinkingData>();
linkingTokenInterval: NodeJS.Timeout
linkingTokenInterval: NodeJS.Timeout | null = null
serviceBeaconInterval: NodeJS.Timeout | null = null
log: PubLogger
constructor(storage: Storage, settings: MainSettings, paymentManager: PaymentManager) {
this.log = getLogger({ component: "ApplicationManager" })
this.storage = storage
this.settings = settings
this.paymentManager = paymentManager
this.StartLinkingTokenInterval()
}

StartLinkingTokenInterval() {
this.linkingTokenInterval = setInterval(() => {
const now = Date.now();
for (let [token, data] of this.nPubLinkingTokens) {
Expand All @@ -36,8 +44,27 @@ export default class {
}
}, 60 * 1000); // 1 minute
}

async StartAppsServiceBeacon(publishBeacon: (app: Application) => void) {
this.serviceBeaconInterval = setInterval(async () => {
try {
const apps = await this.storage.applicationStorage.GetApplications()
apps.forEach(app => {
publishBeacon(app)
})
} catch (e) {
this.log("error in beacon", (e as any).message)
}
}, 60 * 1000)
}

Stop() {
clearInterval(this.linkingTokenInterval)
if (this.linkingTokenInterval) {
clearInterval(this.linkingTokenInterval)
}
if (this.serviceBeaconInterval) {
clearInterval(this.serviceBeaconInterval)
}
}
SignAppToken(appId: string): string {
return jwt.sign({ appId }, this.settings.jwtSecret);
Expand Down
25 changes: 23 additions & 2 deletions src/services/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type UserOperationsSub = {
newIncomingTx: (operation: Types.UserOperation) => void
newOutgoingTx: (operation: Types.UserOperation) => void
}

const appTag = "Lightning.Pub"
export default class {
storage: Storage
lnd: LND
Expand All @@ -36,7 +36,6 @@ export default class {
paymentSubs: Record<string, ((op: Types.UserOperation) => void) | null> = {}
metricsManager: MetricsManager
nostrSend: NostrSend = () => { getLogger({})("nostr send not initialized yet") }

constructor(settings: MainSettings, storage: Storage) {
this.settings = settings
this.storage = storage
Expand All @@ -56,6 +55,12 @@ export default class {
this.paymentManager.Stop()
}

StartBeacons() {
this.applicationManager.StartAppsServiceBeacon(app => {
this.UpdateBeacon(app, { type: 'service', name: app.name })
})
}

attachNostrSend(f: NostrSend) {
this.nostrSend = f
}
Expand Down Expand Up @@ -206,6 +211,22 @@ export default class {
this.nostrSend(app.app_id, { type: 'content', content: JSON.stringify(message), pub: user.nostr_public_key })
}

async UpdateBeacon(app: Application, content: { type: 'service', name: string }) {
if (!app.nostr_public_key) {
getLogger({ appName: app.name })("cannot update beacon, public key not set")
return
}
const tags = [["d", appTag]]
const event: UnsignedEvent = {
content: JSON.stringify(content),
created_at: Math.floor(Date.now() / 1000),
kind: 30078,
pubkey: app.nostr_public_key,
tags,
}
this.nostrSend(app.app_id, { type: 'event', event })
}

async createZapReceipt(log: PubLogger, invoice: UserReceivingInvoice) {
const zapInfo = invoice.zap_info
if (!zapInfo || !invoice.linkedApplication || !invoice.linkedApplication.nostr_public_key) {
Expand Down

0 comments on commit 64ae5a0

Please sign in to comment.