diff --git a/datasource.js b/datasource.js index 8b250b72..e599e1ca 100644 --- a/datasource.js +++ b/datasource.js @@ -17,6 +17,7 @@ import { TrackedProvider } from "./build/src/services/storage/entity/TrackedProv import { InviteToken } from "./build/src/services/storage/entity/InviteToken.js" import { DebitAccess } from "./build/src/services/storage/entity/DebitAccess.js" import { UserOffer } from "./build/src/services/storage/entity/UserOffer.js" +import { ManagementGrant } from "./build/src/services/storage/entity/ManagementGrant.js" import { Initial1703170309875 } from './build/src/services/storage/migrations/1703170309875-initial.js' import { LspOrder1718387847693 } from './build/src/services/storage/migrations/1718387847693-lsp_order.js' @@ -35,7 +36,7 @@ export default new DataSource({ // logging: true, migrations: [Initial1703170309875, LspOrder1718387847693, LiquidityProvider1719335699480, LndNodeInfo1720187506189, CreateInviteTokenTable1721751414878, PaymentIndex1721760297610, DebitAccess1726496225078, DebitAccessFixes1726685229264, DebitToPub1727105758354, UserCbUrl1727112281043, UserOffer1733502626042], entities: [User, UserReceivingInvoice, UserReceivingAddress, AddressReceivingTransaction, UserInvoicePayment, UserTransactionPayment, - UserBasicAuth, UserEphemeralKey, Product, UserToUserPayment, Application, ApplicationUser, UserToUserPayment, LspOrder, LndNodeInfo, TrackedProvider, InviteToken, DebitAccess, UserOffer], + UserBasicAuth, UserEphemeralKey, Product, UserToUserPayment, Application, ApplicationUser, UserToUserPayment, LspOrder, LndNodeInfo, TrackedProvider, InviteToken, DebitAccess, UserOffer, ManagementGrant], // synchronize: true, }) -//npx typeorm migration:generate ./src/services/storage/migrations/ops_time -d ./datasource.js \ No newline at end of file +//npx typeorm migration:generate ./src/services/storage/migrations/management_grant -d ./datasource.js \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 8d642916..14264ba5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "@protobuf-ts/grpc-transport": "^2.9.4", "@protobuf-ts/plugin": "^2.5.0", "@protobuf-ts/runtime": "^2.5.0", - "@shocknet/clink-sdk": "^1.1.2", + "@shocknet/clink-sdk": "^1.1.4", "@stablelib/xchacha20": "^1.0.1", "@types/express": "^4.17.21", "@types/node": "^17.0.31", @@ -591,9 +591,9 @@ } }, "node_modules/@shocknet/clink-sdk": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/@shocknet/clink-sdk/-/clink-sdk-1.1.2.tgz", - "integrity": "sha512-nICsXlLZRIs6E+wy3PfQccIidmQ/D7uSHfHfqrJzNJFOUH2+XGDkApB9TQU1eTrNgD/BHxm9tSZkEmG0it7I3w==", + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@shocknet/clink-sdk/-/clink-sdk-1.1.4.tgz", + "integrity": "sha512-b0YVsisIkTxOAwxrb1a9DGDxwWkHm7kJ2BpqOzkEbtJ6flkJxo2ggmRH3fxsVIiJOeVWwgSPATab68JU8DSLOA==", "license": "ISC", "dependencies": { "@noble/hashes": "^1.8.0", diff --git a/package.json b/package.json index 63ef6c1e..9894c250 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@protobuf-ts/grpc-transport": "^2.9.4", "@protobuf-ts/plugin": "^2.5.0", "@protobuf-ts/runtime": "^2.5.0", - "@shocknet/clink-sdk": "^1.1.2", + "@shocknet/clink-sdk": "^1.1.4", "@stablelib/xchacha20": "^1.0.1", "@types/express": "^4.17.21", "@types/node": "^17.0.31", diff --git a/src/nostrMiddleware.ts b/src/nostrMiddleware.ts index 3c6d4434..cac2662c 100644 --- a/src/nostrMiddleware.ts +++ b/src/nostrMiddleware.ts @@ -4,8 +4,7 @@ import { NostrEvent, NostrSend, NostrSettings } from "./services/nostr/handler.j import * as Types from '../proto/autogenerated/ts/types.js' import NewNostrTransport, { NostrRequest } from '../proto/autogenerated/ts/nostr_transport.js'; import { ERROR, getLogger } from "./services/helpers/logger.js"; -import { NdebitData, NofferData } from "@shocknet/clink-sdk"; -import { NmanageRequest } from "./services/main/managementManager.js"; +import { NdebitData, NofferData, NmanageRequest } from "@shocknet/clink-sdk"; export default (serverMethods: Types.ServerMethods, mainHandler: Main, nostrSettings: NostrSettings, onClientEvent: (e: { requestId: string }, fromPub: string) => void): { Stop: () => void, Send: NostrSend, Ping: () => Promise } => { const log = getLogger({}) diff --git a/src/services/main/index.ts b/src/services/main/index.ts index 22f6c31a..5c839d08 100644 --- a/src/services/main/index.ts +++ b/src/services/main/index.ts @@ -77,8 +77,8 @@ export default class { this.applicationManager = new ApplicationManager(this.storage, this.settings, this.paymentManager) this.appUserManager = new AppUserManager(this.storage, this.settings, this.applicationManager) this.debitManager = new DebitManager(this.storage, this.lnd, this.applicationManager) - this.offerManager = new OfferManager(this.storage, this.lnd, this.applicationManager, this.productManager, this.liquidityManager) - this.managementManager = new ManagementManager(this.storage, this.offerManager) + this.offerManager = new OfferManager(this.storage, this.settings, this.lnd, this.applicationManager, this.productManager, this.liquidityManager) + this.managementManager = new ManagementManager(this.storage, this.settings) //this.webRTC = new webRTC(this.storage, this.utils) } diff --git a/src/services/storage/db/db.ts b/src/services/storage/db/db.ts index 12e8b396..197b635d 100644 --- a/src/services/storage/db/db.ts +++ b/src/services/storage/db/db.ts @@ -24,6 +24,7 @@ import { InviteToken } from "../entity/InviteToken.js" import { DebitAccess } from "../entity/DebitAccess.js" import { RootOperation } from "../entity/RootOperation.js" import { UserOffer } from "../entity/UserOffer.js" +import { ManagementGrant } from "../entity/ManagementGrant.js" export type DbSettings = { @@ -65,7 +66,8 @@ export const MainDbEntities = { 'InviteToken': InviteToken, 'DebitAccess': DebitAccess, 'UserOffer': UserOffer, - 'Product': Product + 'Product': Product, + 'ManagementGrant': ManagementGrant } export type MainDbNames = keyof typeof MainDbEntities export const MainDbEntitiesNames = Object.keys(MainDbEntities) diff --git a/src/services/storage/entity/ManagementGrant.ts b/src/services/storage/entity/ManagementGrant.ts index 978cc236..46a962cd 100644 --- a/src/services/storage/entity/ManagementGrant.ts +++ b/src/services/storage/entity/ManagementGrant.ts @@ -1,7 +1,7 @@ import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, ManyToOne, JoinColumn, UpdateDateColumn } from "typeorm"; import { User } from "./User"; -@Entity("management_grants") +@Entity() export class ManagementGrant { @PrimaryGeneratedColumn() serial_id: number diff --git a/src/services/storage/migrations/1749933500426-AddOfferManagingAppPubKey.ts b/src/services/storage/migrations/1749933500426-AddOfferManagingAppPubKey.ts deleted file mode 100644 index 2585e67f..00000000 --- a/src/services/storage/migrations/1749933500426-AddOfferManagingAppPubKey.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { MigrationInterface, QueryRunner } from "typeorm" - -export class AddOfferManagingAppPubKey1749933500426 implements MigrationInterface { - name = 'AddOfferManagingAppPubKey1749933500426' - - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE "user_offer" ADD "managing_app_pubkey" character varying`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE "user_offer" DROP COLUMN "managing_app_pubkey"`); - } - -} diff --git a/src/services/storage/migrations/1749934345873-CreateManagementGrant.ts b/src/services/storage/migrations/1749934345873-CreateManagementGrant.ts deleted file mode 100644 index a14838a1..00000000 --- a/src/services/storage/migrations/1749934345873-CreateManagementGrant.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm" - -export class CreateManagementGrant1749934345873 implements MigrationInterface { - name = 'CreateManagementGrant1749934345873' - - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.createTable(new Table({ - name: "management_grants", - columns: [ - { - name: "id", - type: "varchar", - isPrimary: true, - isGenerated: true, - generationStrategy: "uuid" - }, - { - name: "user_id", - type: "varchar" - }, - { - name: "app_pubkey", - type: "varchar" - }, - { - name: "created_at", - type: "timestamp", - default: "CURRENT_TIMESTAMP" - }, - { - name: "expires_at", - type: "timestamp", - isNullable: true - } - ] - })); - - await queryRunner.createForeignKey("management_grants", new TableForeignKey({ - columnNames: ["user_id"], - referencedColumnNames: ["user_id"], - referencedTableName: "user", - onDelete: "CASCADE" - })); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.dropTable("management_grants"); - } - -} diff --git a/src/services/storage/migrations/1751307732346-management_grant.ts b/src/services/storage/migrations/1751307732346-management_grant.ts new file mode 100644 index 00000000..170fba69 --- /dev/null +++ b/src/services/storage/migrations/1751307732346-management_grant.ts @@ -0,0 +1,22 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class ManagementGrant1751307732346 implements MigrationInterface { + name = 'ManagementGrant1751307732346' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "management_grant" ("serial_id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "app_user_id" varchar NOT NULL, "app_pubkey" varchar NOT NULL, "expires_at_unix" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`); + await queryRunner.query(`CREATE TABLE "temporary_user_offer" ("serial_id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "app_user_id" varchar NOT NULL, "offer_id" varchar NOT NULL, "label" varchar NOT NULL, "price_sats" integer NOT NULL DEFAULT (0), "callback_url" varchar NOT NULL DEFAULT (''), "expected_data" text, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), "management_pubkey" varchar NOT NULL DEFAULT (''), CONSTRAINT "UQ_478f72095abd8a516d3a309a5c5" UNIQUE ("offer_id"))`); + await queryRunner.query(`INSERT INTO "temporary_user_offer"("serial_id", "app_user_id", "offer_id", "label", "price_sats", "callback_url", "expected_data", "created_at", "updated_at") SELECT "serial_id", "app_user_id", "offer_id", "label", "price_sats", "callback_url", "expected_data", "created_at", "updated_at" FROM "user_offer"`); + await queryRunner.query(`DROP TABLE "user_offer"`); + await queryRunner.query(`ALTER TABLE "temporary_user_offer" RENAME TO "user_offer"`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "user_offer" RENAME TO "temporary_user_offer"`); + await queryRunner.query(`CREATE TABLE "user_offer" ("serial_id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "app_user_id" varchar NOT NULL, "offer_id" varchar NOT NULL, "label" varchar NOT NULL, "price_sats" integer NOT NULL DEFAULT (0), "callback_url" varchar NOT NULL DEFAULT (''), "expected_data" text, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_478f72095abd8a516d3a309a5c5" UNIQUE ("offer_id"))`); + await queryRunner.query(`INSERT INTO "user_offer"("serial_id", "app_user_id", "offer_id", "label", "price_sats", "callback_url", "expected_data", "created_at", "updated_at") SELECT "serial_id", "app_user_id", "offer_id", "label", "price_sats", "callback_url", "expected_data", "created_at", "updated_at" FROM "temporary_user_offer"`); + await queryRunner.query(`DROP TABLE "temporary_user_offer"`); + await queryRunner.query(`DROP TABLE "management_grant"`); + } + +} diff --git a/src/services/storage/migrations/runner.ts b/src/services/storage/migrations/runner.ts index d2ee0082..be0eedfe 100644 --- a/src/services/storage/migrations/runner.ts +++ b/src/services/storage/migrations/runner.ts @@ -16,7 +16,8 @@ import { UserCbUrl1727112281043 } from './1727112281043-user_cb_url.js' import { RootOps1732566440447 } from './1732566440447-root_ops.js' import { UserOffer1733502626042 } from './1733502626042-user_offer.js' import { RootOpsTime1745428134124 } from './1745428134124-root_ops_time.js' -export const allMigrations = [Initial1703170309875, LspOrder1718387847693, LiquidityProvider1719335699480, LndNodeInfo1720187506189, TrackedProvider1720814323679, CreateInviteTokenTable1721751414878, PaymentIndex1721760297610, DebitAccess1726496225078, DebitAccessFixes1726685229264, DebitToPub1727105758354, UserCbUrl1727112281043, UserOffer1733502626042] +import { ManagementGrant1751307732346 } from './1751307732346-management_grant.js' +export const allMigrations = [Initial1703170309875, LspOrder1718387847693, LiquidityProvider1719335699480, LndNodeInfo1720187506189, TrackedProvider1720814323679, CreateInviteTokenTable1721751414878, PaymentIndex1721760297610, DebitAccess1726496225078, DebitAccessFixes1726685229264, DebitToPub1727105758354, UserCbUrl1727112281043, UserOffer1733502626042, ManagementGrant1751307732346] export const allMetricsMigrations = [LndMetrics1703170330183, ChannelRouting1709316653538, HtlcCount1724266887195, BalanceEvents1724860966825, RootOps1732566440447, RootOpsTime1745428134124] /* export const TypeOrmMigrationRunner = async (log: PubLogger, storageManager: Storage, settings: DbSettings, arg: string | undefined): Promise => { await connectAndMigrate(log, storageManager, allMigrations, allMetricsMigrations)