diff --git a/src/auth.ts b/src/auth.ts index a0fa62f1..a8595844 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,11 +1,14 @@ import { ServerOptions } from "../proto/autogenerated/ts/express_server"; import { AdminContext } from "../proto/autogenerated/ts/types"; import Main from './services/main' +import { getLogger } from './services/helpers/logger' const serverOptions = (mainHandler: Main): ServerOptions => { + const log = getLogger({}) return { + logger: { log, error: err => log("ERROR", err) }, AdminAuthGuard: adminAuth, - AppAuthGuard: async (authHeader) => { return { app_id: mainHandler.applicationManager.DecodeAppToken(authHeader) } }, - UserAuthGuard: async (authHeader) => { return { user_id: mainHandler.userManager.DecodeUserToken(authHeader) } }, + AppAuthGuard: async (authHeader) => { return { app_id: mainHandler.applicationManager.DecodeAppToken(stripBearer(authHeader)) } }, + UserAuthGuard: async (authHeader) => { return { user_id: mainHandler.userManager.DecodeUserToken(stripBearer(authHeader)) } }, GuestAuthGuard: async (_) => ({}), encryptCallback: async (_, b) => b, decryptCallback: async (_, b) => b, @@ -13,6 +16,16 @@ const serverOptions = (mainHandler: Main): ServerOptions => { } } +const stripBearer = (header?: string) => { + if (!header) { + return "" + } + if (header.startsWith("Bearer ")) { + return header.substring("Bearer ".length) + } + return header +} + const adminAuth = async (header: string | undefined): Promise => { const AdminToken = process.env.ADMIN_TOKEN if (!AdminToken) { diff --git a/src/services/helpers/logger.ts b/src/services/helpers/logger.ts new file mode 100644 index 00000000..754489a1 --- /dev/null +++ b/src/services/helpers/logger.ts @@ -0,0 +1,43 @@ +import fs from 'fs' +type LoggerParams = { appName?: string, userId?: string } +export type PubLogger = (...message: (string | number | object)[]) => void +type Writer = (message: string) => void +try { + fs.mkdirSync("logs") +} catch { } +const z = (n: number) => n < 10 ? `0${n}` : `${n}` +const openWriter = (fileName: string): Writer => { + const logStream = fs.createWriteStream(`logs/${fileName}`, { flags: 'a' }); + return (message) => { + logStream.write(message + "\n") + } +} +const rootWriter = openWriter("ROOT.log") +export const getLogger = (params: LoggerParams): PubLogger => { + const writers: Writer[] = [] + if (params.appName) { + writers.push(openWriter(`apps/${params.appName}.log`)) + } + if (params.userId) { + writers.push(openWriter(`users/${params.userId}.log`)) + } + if (writers.length === 0) { + writers.push(rootWriter) + } + + return (...message) => { + const now = new Date() + const timestamp = `${now.getFullYear()}-${z(now.getMonth())}-${z(now.getDate())} ${z(now.getHours())}:${z(now.getMinutes())}:${z(now.getSeconds())}` + const toLog = [timestamp] + if (params.appName) { + toLog.push(params.appName) + } + if (params.userId) { + toLog.push(params.userId) + } + const parsed = message.map(m => typeof m === 'object' ? JSON.stringify(m) : m) + const final = `${toLog.join(" ")} >> ${parsed.join(" ")}` + console.log(final) + writers.forEach(w => w(final)) + } +} diff --git a/src/services/lnd/lnd.ts b/src/services/lnd/lnd.ts index 7087e3ab..58941ee8 100644 --- a/src/services/lnd/lnd.ts +++ b/src/services/lnd/lnd.ts @@ -13,6 +13,7 @@ import { AddInvoiceReq } from './addInvoiceReq.js'; import { PayInvoiceReq } from './payInvoiceReq.js'; import { SendCoinsReq } from './sendCoinsReq.js'; import { LndSettings, AddressPaidCb, InvoicePaidCb, NodeInfo, Invoice, DecodedInvoice, PaidInvoice } from './settings.js'; +import { getLogger } from '../helpers/logger.js'; const DeadLineMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline }) export default class { @@ -26,6 +27,7 @@ export default class { abortController = new AbortController() addressPaidCb: AddressPaidCb invoicePaidCb: InvoicePaidCb + log = getLogger({}) constructor(settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb) { this.settings = settings this.addressPaidCb = addressPaidCb @@ -84,19 +86,21 @@ export default class { startHeight: this.latestKnownBlockHeigh, }, { abort: this.abortController.signal }) stream.responses.onMessage(tx => { + if (tx.blockHeight > this.latestKnownBlockHeigh) { this.latestKnownBlockHeigh = tx.blockHeight } if (tx.numConfirmations > 0) { tx.outputDetails.forEach(output => { if (output.isOurAddress) { + this.log("received chan TX", Number(output.amount), "sats") this.addressPaidCb({ hash: tx.txHash, index: Number(output.outputIndex) }, output.address, Number(output.amount)) } }) } }) stream.responses.onError(error => { - // TODO... + this.log("Error with invoice stream") }) } @@ -107,12 +111,13 @@ export default class { }, { abort: this.abortController.signal }) stream.responses.onMessage(invoice => { if (invoice.state === Invoice_InvoiceState.SETTLED) { + this.log("An invoice was paid for", Number(invoice.amtPaidSat), "sats") this.latestKnownSettleIndex = Number(invoice.settleIndex) this.invoicePaidCb(invoice.paymentRequest, Number(invoice.amtPaidSat)) } }) stream.responses.onError(error => { - // TODO... + this.log("Error with invoice stream") }) } async NewAddress(addressType: Types.AddressType): Promise { @@ -170,9 +175,11 @@ export default class { console.log(payment) switch (payment.status) { case Payment_PaymentStatus.FAILED: + this.log("invoice payment failed", payment.failureReason) rej(PaymentFailureReason[payment.failureReason]) return case Payment_PaymentStatus.SUCCEEDED: + this.log("invoice payment succeded", Number(payment.valueSat)) res({ feeSat: Number(payment.feeSat), valueSat: Number(payment.valueSat), paymentPreimage: payment.paymentPreimage }) } }) @@ -193,6 +200,7 @@ export default class { async PayAddress(address: string, amount: number, satPerVByte: number, label = ""): Promise { this.checkReady() const res = await this.lightning.sendCoins(SendCoinsReq(address, amount, satPerVByte, label), DeadLineMetadata()) + this.log("sent chain TX for", amount, "sats") return res.response } diff --git a/src/services/main/applicationManager.ts b/src/services/main/applicationManager.ts index aa3922b0..8af7fe4b 100644 --- a/src/services/main/applicationManager.ts +++ b/src/services/main/applicationManager.ts @@ -6,6 +6,7 @@ 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' export default class { storage: Storage settings: MainSettings @@ -35,7 +36,8 @@ export default class { } async SetMockAppUserBalance(appId: string, req: Types.SetMockAppUserBalanceRequest) { - const user = await this.storage.applicationStorage.GetOrCreateApplicationUser(appId, req.user_identifier, 0) + const app = await this.storage.applicationStorage.GetApplication(appId) + const { user } = await this.storage.applicationStorage.GetOrCreateApplicationUser(app, req.user_identifier, 0) await this.paymentManager.SetMockUserBalance(user.user.user_id, req.amount) } @@ -47,6 +49,8 @@ export default class { async AddApp(req: Types.AuthAppRequest): Promise { const app = await this.storage.applicationStorage.AddApplication(req.name) + getLogger({ appName: app.name })("app created") + return { app: { id: app.app_id, @@ -79,11 +83,16 @@ export default class { } async AddAppUser(appId: string, req: Types.AddAppUserRequest): Promise { + const app = await this.storage.applicationStorage.GetApplication(appId) + const log = getLogger({ appName: app.name }) let u: ApplicationUser if (req.fail_if_exists) { - u = await this.storage.applicationStorage.AddApplicationUser(appId, req.identifier, req.balance) + u = await this.storage.applicationStorage.AddApplicationUser(app, req.identifier, req.balance) + log(u.identifier, u.user.user_id, "user created") } else { - u = await this.storage.applicationStorage.GetOrCreateApplicationUser(appId, req.identifier, req.balance) + const { user, created } = await this.storage.applicationStorage.GetOrCreateApplicationUser(app, req.identifier, req.balance) + u = user + if (created) log(u.identifier, u.user.user_id, "user created") } return { identifier: u.identifier, @@ -97,26 +106,29 @@ export default class { async AddAppInvoice(appId: string, req: Types.AddAppInvoiceRequest): Promise { const app = await this.storage.applicationStorage.GetApplication(appId) - const payer = await this.storage.applicationStorage.GetOrCreateApplicationUser(appId, req.payer_identifier, 0) + const { user: payer } = await this.storage.applicationStorage.GetOrCreateApplicationUser(app, req.payer_identifier, 0) const opts: InboundOptionals = { callbackUrl: req.http_callback_url, expiry: defaultInvoiceExpiry, expectedPayer: payer.user, linkedApplication: app } - return this.paymentManager.NewInvoice(app.owner.user_id, req.invoice_req, opts) + const invoice = await this.paymentManager.NewInvoice(app.owner.user_id, req.invoice_req, opts) + getLogger({ appName: app.name })("app invoice created to be paid by", payer.identifier) + return invoice } async AddAppUserInvoice(appId: string, req: Types.AddAppUserInvoiceRequest): Promise { const app = await this.storage.applicationStorage.GetApplication(appId) - const receiver = await this.storage.applicationStorage.GetApplicationUser(appId, req.receiver_identifier) - const payer = await this.storage.applicationStorage.GetOrCreateApplicationUser(appId, req.payer_identifier, 0) + const receiver = await this.storage.applicationStorage.GetApplicationUser(app, req.receiver_identifier) + const { user: payer } = await this.storage.applicationStorage.GetOrCreateApplicationUser(app, req.payer_identifier, 0) const opts: InboundOptionals = { callbackUrl: req.http_callback_url, expiry: defaultInvoiceExpiry, expectedPayer: payer.user, linkedApplication: app } const appUserInvoice = await this.paymentManager.NewInvoice(receiver.user.user_id, req.invoice_req, opts) + getLogger({ appName: app.name })(receiver.identifier, "invoice created to be paid by", payer.identifier) return { invoice: appUserInvoice.invoice } } async GetAppUser(appId: string, req: Types.GetAppUserRequest): Promise { - const user = await this.storage.applicationStorage.GetApplicationUser(appId, req.user_identifier) + const app = await this.storage.applicationStorage.GetApplication(appId) + const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier) const max = this.paymentManager.GetMaxPayableInvoice(user.user.balance_sats, true) - console.log(max, user.user.balance_sats) return { max_withdrawable: max, identifier: req.user_identifier, info: { userId: user.user.user_id, balance: user.user.balance_sats @@ -126,25 +138,29 @@ export default class { async PayAppUserInvoice(appId: string, req: Types.PayAppUserInvoiceRequest): Promise { const app = await this.storage.applicationStorage.GetApplication(appId) - const appUser = await this.storage.applicationStorage.GetApplicationUser(appId, req.user_identifier) - return this.paymentManager.PayInvoice(appUser.user.user_id, req, app) + const appUser = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier) + const paid = await this.paymentManager.PayInvoice(appUser.user.user_id, req, app) + getLogger({ appName: app.name })(appUser.identifier, "invoice paid", paid.amount_paid, "sats") + return paid } async SendAppUserToAppUserPayment(appId: string, req: Types.SendAppUserToAppUserPaymentRequest): Promise { - const fromUser = await this.storage.applicationStorage.GetApplicationUser(appId, req.from_user_identifier) - const toUser = await this.storage.applicationStorage.GetOrCreateApplicationUser(appId, req.to_user_identifier, 0) const app = await this.storage.applicationStorage.GetApplication(appId) + const fromUser = await this.storage.applicationStorage.GetApplicationUser(app, req.from_user_identifier) + const { user: toUser } = await this.storage.applicationStorage.GetOrCreateApplicationUser(app, req.to_user_identifier, 0) await this.paymentManager.SendUserToUserPayment(fromUser.user.user_id, toUser.user.user_id, req.amount, app) + getLogger({ appName: app.name })(toUser.identifier, "received internal payment by", fromUser.identifier, "of", req.amount, "sats") } async SendAppUserToAppPayment(appId: string, req: Types.SendAppUserToAppPaymentRequest): Promise { - const fromUser = await this.storage.applicationStorage.GetApplicationUser(appId, req.from_user_identifier) const app = await this.storage.applicationStorage.GetApplication(appId) + const fromUser = await this.storage.applicationStorage.GetApplicationUser(app, req.from_user_identifier) await this.paymentManager.SendUserToUserPayment(fromUser.user.user_id, app.owner.user_id, req.amount, app) + getLogger({ appName: app.name })("app received internal payment by", fromUser.identifier, "of", req.amount, "sats") } async GetAppUserLNURLInfo(appId: string, req: Types.GetAppUserLNURLInfoRequest): Promise { - const user = await this.storage.applicationStorage.GetApplicationUser(appId, req.user_identifier) const app = await this.storage.applicationStorage.GetApplication(appId) + const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier) return this.paymentManager.GetLnurlPayInfoFromUser(user.user.user_id, app, req.base_url_override) } } \ No newline at end of file diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index 1b82feb5..8ffed649 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -308,6 +308,7 @@ export default class { const fromUser = await this.storage.userStorage.GetUser(fromUserId, tx) const toUser = await this.storage.userStorage.GetUser(toUserId, tx) if (fromUser.balance_sats < amount) { + console.log({balance:fromUser.balance_sats, amount}) throw new Error("not enough balance to send user to user payment") } if (!linkedApplication) { diff --git a/src/services/serverMethods/index.ts b/src/services/serverMethods/index.ts index cd6797dc..e598d248 100644 --- a/src/services/serverMethods/index.ts +++ b/src/services/serverMethods/index.ts @@ -138,7 +138,6 @@ export default (mainHandler: Main): Types.ServerMethods => { payer_identifier_CustomCheck: id => id !== '', }) if (err != null) throw new Error(err.message) - console.log(req) return mainHandler.applicationManager.AddAppInvoice(ctx.app_id, req) }, AddAppUserInvoice: async (ctx, req) => { diff --git a/src/services/storage/applicationStorage.ts b/src/services/storage/applicationStorage.ts index b5f9d0c1..67f8372b 100644 --- a/src/services/storage/applicationStorage.ts +++ b/src/services/storage/applicationStorage.ts @@ -46,38 +46,38 @@ export default class { return found } - async AddApplicationUser(appId: string, userIdentifier: string, balance: number) { + async AddApplicationUser(application: Application, userIdentifier: string, balance: number) { return this.DB.transaction(async tx => { const user = await this.userStorage.AddUser(balance, tx) const repo = tx.getRepository(ApplicationUser) const appUser = repo.create({ user: user, - application: await this.GetApplication(appId), + application, identifier: userIdentifier, }) return repo.save(appUser) }) } - GetApplicationUserIfExists(appId: string, userIdentifier: string, entityManager = this.DB): Promise { - return entityManager.getRepository(ApplicationUser).findOne({ where: { identifier: userIdentifier, application: { app_id: appId } } }) + GetApplicationUserIfExists(application: Application, userIdentifier: string, entityManager = this.DB): Promise { + return entityManager.getRepository(ApplicationUser).findOne({ where: { identifier: userIdentifier, application: application } }) } - async GetOrCreateApplicationUser(appId: string, userIdentifier: string, balance: number, entityManager = this.DB): Promise { - const found = await this.GetApplicationUserIfExists(appId, userIdentifier, entityManager) - if (found) { - return found + async GetOrCreateApplicationUser(application: Application, userIdentifier: string, balance: number, entityManager = this.DB): Promise<{ user: ApplicationUser, created: boolean }> { + const user = await this.GetApplicationUserIfExists(application, userIdentifier, entityManager) + if (user) { + return { user, created: false } } - return this.AddApplicationUser(appId, userIdentifier, balance) + return { user: await this.AddApplicationUser(application, userIdentifier, balance), created: true } } - async GetApplicationUser(appId: string, userIdentifier: string, entityManager = this.DB): Promise { - const found = await this.GetApplicationUserIfExists(appId, userIdentifier, entityManager) + async GetApplicationUser(application: Application, userIdentifier: string, entityManager = this.DB): Promise { + const found = await this.GetApplicationUserIfExists(application, userIdentifier, entityManager) if (!found) { throw new Error(`application user not found`) } - if (found.application.app_id !== appId) { + if (found.application.app_id !== application.app_id) { throw new Error("requested user does not belong to requestor application") } return found diff --git a/src/services/storage/userStorage.ts b/src/services/storage/userStorage.ts index a011df82..b98371c0 100644 --- a/src/services/storage/userStorage.ts +++ b/src/services/storage/userStorage.ts @@ -3,6 +3,7 @@ import { DataSource, EntityManager } from "typeorm" import { User } from './entity/User.js'; import { UserBasicAuth } from './entity/UserBasicAuth.js'; import { UserNostrAuth } from './entity/UserNostrAuth.js'; +import { getLogger } from '../helpers/logger.js'; export default class { DB: DataSource | EntityManager constructor(DB: DataSource | EntityManager) { @@ -84,12 +85,14 @@ export default class { } } async IncrementUserBalance(userId: string, increment: number, entityManager = this.DB) { + const user = await this.GetUser(userId, entityManager) const res = await entityManager.getRepository(User).increment({ user_id: userId, }, "balance_sats", increment) if (!res.affected) { throw new Error("unaffected balance increment for " + userId) // TODO: fix logs doxing } + getLogger({ userId: userId })("incremented balance from", user.balance_sats, "sats, by", increment, "sats") } async DecrementUserBalance(userId: string, decrement: number, entityManager = this.DB) { const user = await this.GetUser(userId, entityManager) @@ -102,6 +105,7 @@ export default class { if (!res.affected) { throw new Error("unaffected balance decrement for " + userId) // TODO: fix logs doxing } + getLogger({ userId: userId })("decremented balance from", user.balance_sats, "sats, by", decrement, "sats") } async UpdateUser(userId: string, update: Partial, entityManager = this.DB) {