@bsv/overlay-express 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/overlay-express",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Overlay Express",
6
6
  "main": "dist/cjs/mod.js",
@@ -25,21 +25,10 @@
25
25
  "require": "./dist/cjs/src/*.js"
26
26
  }
27
27
  },
28
- "scripts": {
29
- "test": "jest",
30
- "test:watch": "jest --watch",
31
- "test:coverage": "jest --coverage",
32
- "lint": "ts-standard --fix src/**/*.ts",
33
- "lint:ci": "ts-standard src/**/*.ts",
34
- "build": "tsc -b && tsconfig-to-dual-package tsconfig.cjs.json",
35
- "dev": "tsc -b -w",
36
- "prepublish": "npm run build",
37
- "doc": "ts2md --inputFilename=mod.ts --outputFilename=API.md --filenameSubstring=API --firstHeadingLevel=1",
38
- "demo": "rm -r dist && npm run build && npx tsx demo-ui.ts"
39
- },
40
28
  "repository": {
41
29
  "type": "git",
42
- "url": "git+https://github.com/bsv-blockchain/overlay-express.git"
30
+ "url": "git+https://github.com/bsv-blockchain/ts-stack.git",
31
+ "directory": "packages/overlays/overlay-express"
43
32
  },
44
33
  "keywords": [
45
34
  "BSV",
@@ -51,30 +40,44 @@
51
40
  "author": "BSV Association",
52
41
  "license": "SEE LICENSE IN LICENSE.txt",
53
42
  "bugs": {
54
- "url": "https://github.com/bsv-blockchain/overlay-express/issues"
43
+ "url": "https://github.com/bsv-blockchain/ts-stack/issues"
55
44
  },
56
- "homepage": "https://github.com/bsv-blockchain/overlay-express#readme",
45
+ "homepage": "https://github.com/bsv-blockchain/ts-stack/tree/main/packages/overlays/overlay-express#readme",
57
46
  "devDependencies": {
47
+ "@jest/globals": "^30.3.0",
48
+ "@types/body-parser": "^1.19.5",
58
49
  "@types/express": "^5.0.0",
59
- "@types/jest": "^29.5.12",
60
- "jest": "^29.7.0",
61
- "ts-jest": "^29.1.1",
50
+ "@types/jest": "^30.0.0",
51
+ "jest": "^30.3.0",
52
+ "ts-jest": "^29.4.9",
62
53
  "ts-standard": "^12.0.2",
63
54
  "ts2md": "^0.2.0",
64
55
  "tsconfig-to-dual-package": "^1.2.0",
65
56
  "typescript": "^5.2.2"
66
57
  },
67
58
  "dependencies": {
68
- "@bsv/auth-express-middleware": "^2.0.2",
59
+ "@bsv/auth-express-middleware": "^2.0.5",
69
60
  "@bsv/overlay": "^2.0.2",
70
- "@bsv/overlay-discovery-services": "^2.0.2",
71
- "@bsv/sdk": "^2.0.4",
72
- "@bsv/wallet-toolbox-client": "^2.0.20",
61
+ "@bsv/overlay-discovery-services": "^2.0.3",
62
+ "@bsv/sdk": "^2.1.2",
63
+ "@bsv/wallet-toolbox-client": "^2.1.28",
73
64
  "body-parser": "^2.2.2",
74
65
  "chalk": "^5.6.2",
75
66
  "express": "^5.2.1",
76
67
  "knex": "^3.1.0",
77
68
  "mongodb": "^7.1.0",
78
69
  "uuid": "^13.0.0"
70
+ },
71
+ "scripts": {
72
+ "test": "jest",
73
+ "test:watch": "jest --watch",
74
+ "test:coverage": "jest --coverage",
75
+ "lint": "ts-standard --fix src/**/*.ts",
76
+ "lint:ci": "ts-standard src/**/*.ts",
77
+ "build": "tsc -b && tsconfig-to-dual-package tsconfig.cjs.json",
78
+ "dev": "tsc -b -w",
79
+ "prepublish": "npm run build",
80
+ "doc": "ts2md --inputFilename=mod.ts --outputFilename=API.md --filenameSubstring=API --firstHeadingLevel=1",
81
+ "demo": "rm -r dist && npm run build && npx tsx demo-ui.ts"
79
82
  }
80
- }
83
+ }
package/src/BanService.ts CHANGED
@@ -40,7 +40,7 @@ export class BanService {
40
40
  */
41
41
  private sanitize (value: unknown): string {
42
42
  if (typeof value !== 'string') {
43
- throw new Error('Invalid input: expected a string value')
43
+ throw new TypeError('Invalid input: expected a string value')
44
44
  }
45
45
  return value
46
46
  }
@@ -99,7 +99,7 @@ export class BanService {
99
99
  $set: {
100
100
  type: 'outpoint',
101
101
  value,
102
- domain: domain !== undefined ? this.sanitize(domain) : undefined,
102
+ domain: domain != null ? this.sanitize(domain) : undefined,
103
103
  reason: reason ?? 'Manually banned',
104
104
  bannedAt: new Date(),
105
105
  bannedBy
@@ -25,7 +25,7 @@ import { MongoClient, Db } from 'mongodb'
25
25
  import makeUserInterface, { type UIConfig } from './makeUserInterface.js'
26
26
  import * as DiscoveryServices from '@bsv/overlay-discovery-services'
27
27
  import chalk from 'chalk'
28
- import util from 'util'
28
+ import util from 'node:util'
29
29
  import { v4 as uuidv4 } from 'uuid'
30
30
  import { JanitorService, type JanitorReport } from './JanitorService.js'
31
31
  import { BanService } from './BanService.js'
@@ -393,7 +393,7 @@ export default class OverlayExpress {
393
393
 
394
394
  /**
395
395
  * Configure Knex (SQL) database connection.
396
- * @param config - Knex configuration object, or MySQL connection string (e.g. mysql://overlayAdmin:overlay123@mysql:3306/overlay).
396
+ * @param config - Knex configuration object, or a MySQL connection string loaded from configuration.
397
397
  */
398
398
  async configureKnex (config: Knex.Knex.Config | string): Promise<void> {
399
399
  if (typeof config === 'string') {
@@ -509,7 +509,6 @@ export default class OverlayExpress {
509
509
  const knex = this.ensureKnex()
510
510
 
511
511
  if (autoConfigureShipSlap) {
512
- // Auto-configure SHIP and SLAP services
513
512
  this.configureTopicManager('tm_ship', new DiscoveryServices.SHIPTopicManager())
514
513
  this.configureTopicManager('tm_slap', new DiscoveryServices.SLAPTopicManager())
515
514
  this.configureLookupServiceWithMongo('ls_ship', (db) => new DiscoveryServices.SHIPLookupService(
@@ -520,137 +519,109 @@ export default class OverlayExpress {
520
519
  ))
521
520
  }
522
521
 
523
- // Wrap SHIP/SLAP lookup services with ban-aware wrappers if BanService is available.
524
- // This prevents GASP from re-syncing tokens whose domains or outpoints have been banned.
525
- if (this.banService !== undefined) {
526
- if (this.services.ls_ship !== undefined) {
527
- this.services.ls_ship = new BanAwareLookupWrapper(
528
- this.services.ls_ship,
529
- this.banService,
530
- 'SHIP',
531
- this.logger
532
- )
533
- this.logger.log(chalk.blue('SHIP lookup service wrapped with ban-aware filter.'))
534
- }
535
- if (this.services.ls_slap !== undefined) {
536
- this.services.ls_slap = new BanAwareLookupWrapper(
537
- this.services.ls_slap,
538
- this.banService,
539
- 'SLAP',
540
- this.logger
541
- )
542
- this.logger.log(chalk.blue('SLAP lookup service wrapped with ban-aware filter.'))
543
- }
544
- }
522
+ this.wrapBanAwareServices()
545
523
 
546
- // Construct a default sync configuration, in case the user doesn't want GASP at all:
547
- let syncConfig: Record<string, string[] | 'SHIP' | false> = {}
548
- if (!this.enableGASPSync) {
549
- // For each manager, disable sync
550
- for (const managerName of Object.keys(this.managers)) {
551
- syncConfig[managerName] = false
552
- }
553
- } else {
554
- // If the user provided a syncConfiguration, use that. Otherwise default to an empty object.
555
- syncConfig = this.engineConfig.syncConfiguration ?? {}
556
- }
557
-
558
- // Build the actual Storage
524
+ const syncConfig = this.buildSyncConfig()
559
525
  const storage = new KnexStorage(knex)
560
- // Include the KnexStorage migrations
561
526
  this.migrationsToRun = [...KnexStorageMigrations.default, ...this.migrationsToRun]
562
527
 
563
- // Prepare broadcaster if arcApiKey is set
564
- let broadcaster: Broadcaster | undefined
565
- if (typeof this.arcApiKey === 'string') {
566
- broadcaster = new ARC(
567
- // We hard-code some ARC URLs for now, but we should make this configurable later.
568
- this.network === 'test' ? 'https://arc-test.taal.com' : 'https://arc.taal.com',
569
- {
570
- apiKey: this.arcApiKey
571
- })
572
- }
573
-
574
- // Prepare advertiser if not set by the user
575
- let advertiser: Advertiser | undefined = this.engineConfig.advertiser
576
- if (typeof advertiser === 'undefined') {
577
- try {
578
- advertiser = new DiscoveryServices.WalletAdvertiser(
579
- this.network,
580
- this.privateKey,
581
- this.network === 'test' // For now, we hard-code some storage servers. In the future, this needs to be configurable.
582
- ? 'https://staging-storage.babbage.systems'
583
- : 'https://storage.babbage.systems',
584
- // Until multiple protocols (like https+bsvauth+smf) are fully supported, HTTPS is the one to always use.
585
- `https://${this.advertisableFQDN}`
586
- )
587
- } catch (e) {
588
- this.logger.log(`Advertiser not initialized for FQDN ${this.advertisableFQDN} - SHIP and SLAP will be disabled.`)
589
- }
590
- }
528
+ const broadcaster = this.buildBroadcaster()
529
+ const advertiser = await this.buildAdvertiser()
591
530
 
592
- // Construct the Engine with any advanced config overrides. Fallback to defaults.
593
531
  this.engine = new Engine(
594
532
  this.managers,
595
533
  this.services,
596
534
  storage,
597
- // chainTracker
598
- typeof this.engineConfig.chainTracker !== 'undefined'
599
- ? this.engineConfig.chainTracker
600
- : this.chainTracker,
601
- // hostingURL
535
+ this.engineConfig.chainTracker ?? this.chainTracker,
602
536
  `https://${this.advertisableFQDN}`,
603
- // shipTrackers
604
537
  this.network === 'test'
605
538
  ? (this.engineConfig.shipTrackers ?? DEFAULT_TESTNET_SLAP_TRACKERS)
606
539
  : this.engineConfig.shipTrackers,
607
- // slapTrackers
608
- Array.isArray(this.engineConfig.slapTrackers)
609
- ? this.engineConfig.slapTrackers
610
- : this.network === 'test'
611
- ? DEFAULT_TESTNET_SLAP_TRACKERS
612
- : DEFAULT_SLAP_TRACKERS,
613
- // broadcaster
540
+ this.resolveSlapTrackers(),
614
541
  broadcaster ?? this.engineConfig.broadcaster,
615
- // advertiser
616
542
  advertiser,
617
- // syncConfiguration
618
543
  syncConfig,
619
- // logTime
620
544
  this.engineConfig.logTime ?? false,
621
- // logPrefix
622
545
  this.engineConfig.logPrefix ?? '[OVERLAY_ENGINE] ',
623
- // throwOnBroadcastFailure
624
546
  this.engineConfig.throwOnBroadcastFailure ?? false,
625
- // overlayBroadcastFacilitator
626
547
  this.engineConfig.overlayBroadcastFacilitator ?? new HTTPSOverlayBroadcastFacilitator(),
627
- // logger
628
548
  this.logger,
629
- // suppressDefaultSyncAdvertisements
630
549
  this.engineConfig.suppressDefaultSyncAdvertisements ?? true
631
550
  )
632
551
 
633
- // Create the server wallet for BSV mutual authentication.
634
- // This uses the same private key as the WalletAdvertiser.
552
+ this.initServerWallet()
553
+ this.logger.log(chalk.green('Engine has been configured.'))
554
+ }
555
+
556
+ /** Wrap SHIP/SLAP with ban-aware filter if BanService is configured. */
557
+ private wrapBanAwareServices (): void {
558
+ if (this.banService === undefined) return
559
+ for (const key of ['ls_ship', 'ls_slap'] as const) {
560
+ if (this.services[key] !== undefined) {
561
+ const label = key === 'ls_ship' ? 'SHIP' : 'SLAP'
562
+ this.services[key] = new BanAwareLookupWrapper(this.services[key], this.banService, label, this.logger)
563
+ this.logger.log(chalk.blue(`${label} lookup service wrapped with ban-aware filter.`))
564
+ }
565
+ }
566
+ }
567
+
568
+ /** Build the sync config based on enableGASPSync and engineConfig. */
569
+ private buildSyncConfig (): Record<string, string[] | 'SHIP' | false> {
570
+ if (this.enableGASPSync) {
571
+ return this.engineConfig.syncConfiguration ?? {}
572
+ }
573
+ const syncConfig: Record<string, string[] | 'SHIP' | false> = {}
574
+ for (const name of Object.keys(this.managers)) {
575
+ syncConfig[name] = false
576
+ }
577
+ return syncConfig
578
+ }
579
+
580
+ /** Build the ARC broadcaster if an API key is configured. */
581
+ private buildBroadcaster (): Broadcaster | undefined {
582
+ if (typeof this.arcApiKey !== 'string') return undefined
583
+ const arcUrl = this.network === 'test' ? 'https://arc-test.taal.com' : 'https://arc.taal.com'
584
+ return new ARC(arcUrl, { apiKey: this.arcApiKey })
585
+ }
586
+
587
+ /** Resolve the SLAP trackers from config or network defaults. */
588
+ private resolveSlapTrackers (): string[] | undefined {
589
+ if (Array.isArray(this.engineConfig.slapTrackers)) return this.engineConfig.slapTrackers
590
+ return this.network === 'test' ? DEFAULT_TESTNET_SLAP_TRACKERS : DEFAULT_SLAP_TRACKERS
591
+ }
592
+
593
+ /** Build the WalletAdvertiser (or use user-provided one). */
594
+ private async buildAdvertiser (): Promise<Advertiser | undefined> {
595
+ if (this.engineConfig.advertiser !== undefined) return this.engineConfig.advertiser
596
+ const storageBase = this.network === 'test'
597
+ ? 'https://staging-storage.babbage.systems'
598
+ : 'https://storage.babbage.systems'
599
+ try {
600
+ return new DiscoveryServices.WalletAdvertiser(
601
+ this.network,
602
+ this.privateKey,
603
+ storageBase,
604
+ `https://${this.advertisableFQDN}`
605
+ )
606
+ } catch (e) {
607
+ this.logger.log(`Advertiser not initialized for FQDN ${this.advertisableFQDN} - SHIP and SLAP will be disabled. Reason: ${e}`)
608
+ return undefined
609
+ }
610
+ }
611
+
612
+ /** Initialize the server wallet for BSV mutual authentication. */
613
+ private initServerWallet (): void {
635
614
  try {
636
615
  const keyDeriver = new KeyDeriver(new PrivateKey(this.privateKey, 'hex'))
637
616
  const storageManager = new WalletStorageManager(keyDeriver.identityKey)
638
617
  const signer = new WalletSigner(this.network, keyDeriver, storageManager)
639
618
  const services = new Services(this.network)
640
- const wallet = new Wallet(signer, services)
641
- this.serverWallet = wallet
642
-
643
- // Auto-set the admin identity key from the server's own key if not configured
644
- if (typeof this.adminIdentityKey === 'undefined') {
645
- this.adminIdentityKey = keyDeriver.identityKey
646
- }
647
-
619
+ this.serverWallet = new Wallet(signer, services)
620
+ this.adminIdentityKey ??= keyDeriver.identityKey
648
621
  this.logger.log(chalk.blue('Server wallet initialized for BSV mutual authentication.'))
649
622
  } catch (e) {
650
- this.logger.log(chalk.yellow('Server wallet could not be initialized. BSV auth will not be available.'))
623
+ this.logger.log(chalk.yellow(`Server wallet could not be initialized. BSV auth will not be available. Reason: ${e}`))
651
624
  }
652
-
653
- this.logger.log(chalk.green('Engine has been configured.'))
654
625
  }
655
626
 
656
627
  /**
@@ -658,8 +629,8 @@ export default class OverlayExpress {
658
629
  * @throws Error if Knex is not configured
659
630
  */
660
631
  private ensureKnex (): Knex.Knex {
661
- if (typeof this.knex === 'undefined') {
662
- throw new Error('You must configure your SQL database with the .configureKnex() method first!')
632
+ if (this.knex === undefined) {
633
+ throw new TypeError('You must configure your SQL database with the .configureKnex() method first!')
663
634
  }
664
635
  return this.knex
665
636
  }
@@ -669,8 +640,8 @@ export default class OverlayExpress {
669
640
  * @throws Error if MongoDB is not configured
670
641
  */
671
642
  private ensureMongo (): Db {
672
- if (typeof this.mongoDb === 'undefined') {
673
- throw new Error('You must configure your MongoDB connection with the .configureMongo() method first!')
643
+ if (this.mongoDb === undefined) {
644
+ throw new TypeError('You must configure your MongoDB connection with the .configureMongo() method first!')
674
645
  }
675
646
  return this.mongoDb
676
647
  }
@@ -680,8 +651,8 @@ export default class OverlayExpress {
680
651
  * @throws Error if the Engine is not configured
681
652
  */
682
653
  private ensureEngine (): Engine {
683
- if (typeof this.engine === 'undefined') {
684
- throw new Error('You must configure your Overlay Services engine with the .configureEngine() method first!')
654
+ if (this.engine === undefined) {
655
+ throw new TypeError('You must configure your Overlay Services engine with the .configureEngine() method first!')
685
656
  }
686
657
  return this.engine
687
658
  }
@@ -701,6 +672,71 @@ export default class OverlayExpress {
701
672
  })
702
673
  }
703
674
 
675
+ /** Ban a domain and remove all its SHIP/SLAP records from MongoDB. */
676
+ private async handleBanDomain (res: express.Response, value: string, reason?: string): Promise<express.Response> {
677
+ await this.banService!.banDomain(value, reason)
678
+ const db = this.ensureMongo()
679
+ const [shipDeleted, slapDeleted] = await Promise.all([
680
+ db.collection('shipRecords').deleteMany({ domain: value }),
681
+ db.collection('slapRecords').deleteMany({ domain: value })
682
+ ])
683
+ return res.status(200).json({
684
+ status: 'success',
685
+ message: `Domain "${value}" banned. Removed ${shipDeleted.deletedCount} SHIP and ${slapDeleted.deletedCount} SLAP records.`
686
+ })
687
+ }
688
+
689
+ /** Parse outpoint string, ban it, and evict it from all lookup services. */
690
+ private async handleBanOutpoint (res: express.Response, engine: Engine, value: string, reason?: string): Promise<express.Response> {
691
+ const dotIndex = value.lastIndexOf('.')
692
+ if (dotIndex === -1) {
693
+ return res.status(400).json({ status: 'error', message: 'Outpoint format must be "txid.outputIndex"' })
694
+ }
695
+ const txid = value.substring(0, dotIndex)
696
+ const outputIndex = Number.parseInt(value.substring(dotIndex + 1))
697
+ if (Number.isNaN(outputIndex)) {
698
+ return res.status(400).json({ status: 'error', message: 'Invalid outputIndex in outpoint' })
699
+ }
700
+ await this.banService!.banOutpoint(txid, outputIndex, reason)
701
+ await this.evictFromServices(engine, txid, outputIndex)
702
+ return res.status(200).json({
703
+ status: 'success',
704
+ message: `Outpoint "${value}" banned and evicted from lookup services.`
705
+ })
706
+ }
707
+
708
+ /** Evict an output from a specific service or all services (silent per-service errors). */
709
+ private async evictFromServices (engine: Engine, txid: string, outputIndex: number, service?: string): Promise<void> {
710
+ if (typeof service === 'string') {
711
+ const svc = engine.lookupServices[service]
712
+ if (svc !== undefined) await svc.outputEvicted(txid, outputIndex)
713
+ return
714
+ }
715
+ for (const svc of Object.values(engine.lookupServices)) {
716
+ try { await svc.outputEvicted(txid, outputIndex) } catch { /* best-effort */ }
717
+ }
718
+ }
719
+
720
+ /** Look up the domain of an outpoint from SHIP or SLAP records. */
721
+ private async lookupDomainForOutpoint (txid: string, outputIndex: number): Promise<string | undefined> {
722
+ const db = this.ensureMongo()
723
+ const [shipRecord, slapRecord] = await Promise.all([
724
+ db.collection('shipRecords').findOne({ txid, outputIndex }),
725
+ db.collection('slapRecords').findOne({ txid, outputIndex })
726
+ ])
727
+ return (shipRecord?.domain ?? slapRecord?.domain) as string | undefined
728
+ }
729
+
730
+ /** Ban a domain and delete all SHIP/SLAP records for it. */
731
+ private async banDomainAndRemoveRecords (domain: string, reason: string): Promise<void> {
732
+ await this.banService!.banDomain(domain, reason)
733
+ const db = this.ensureMongo()
734
+ await Promise.all([
735
+ db.collection('shipRecords').deleteMany({ domain }),
736
+ db.collection('slapRecords').deleteMany({ domain })
737
+ ])
738
+ }
739
+
704
740
  private async runHealthCheck (
705
741
  definition: Required<Pick<HealthCheckDefinition, 'name' | 'scope' | 'critical'>> & { handler: HealthCheckHandler }
706
742
  ): Promise<HealthCheckResult> {
@@ -757,8 +793,8 @@ export default class OverlayExpress {
757
793
  scope: 'ready',
758
794
  critical: true,
759
795
  handler: async () => {
760
- if (typeof this.engine === 'undefined') {
761
- throw new Error('Overlay engine is not configured')
796
+ if (this.engine === undefined) {
797
+ throw new TypeError('Overlay engine is not configured')
762
798
  }
763
799
 
764
800
  return {
@@ -775,8 +811,8 @@ export default class OverlayExpress {
775
811
  scope: 'ready',
776
812
  critical: true,
777
813
  handler: async () => {
778
- if (typeof this.knex === 'undefined') {
779
- throw new Error('Knex is not configured')
814
+ if (this.knex === undefined) {
815
+ throw new TypeError('Knex is not configured')
780
816
  }
781
817
 
782
818
  await this.knex.raw('select 1 as ok')
@@ -793,8 +829,8 @@ export default class OverlayExpress {
793
829
  scope: 'ready',
794
830
  critical: true,
795
831
  handler: async () => {
796
- if (typeof this.mongoDb === 'undefined') {
797
- throw new Error('MongoDB is not configured')
832
+ if (this.mongoDb === undefined) {
833
+ throw new TypeError('MongoDB is not configured')
798
834
  }
799
835
 
800
836
  await this.mongoDb.command({ ping: 1 })
@@ -852,7 +888,7 @@ export default class OverlayExpress {
852
888
  port: this.port,
853
889
  network: this.network,
854
890
  startedAt: this.startTime?.toISOString(),
855
- uptimeMs: typeof this.startTime === 'undefined' ? 0 : Date.now() - this.startTime.getTime(),
891
+ uptimeMs: this.startTime === undefined ? 0 : Date.now() - this.startTime.getTime(),
856
892
  topicManagerCount: Object.keys(this.managers).length,
857
893
  lookupServiceCount: Object.keys(this.services).length
858
894
  },
@@ -1102,7 +1138,7 @@ export default class OverlayExpress {
1102
1138
  const topicsHeader = req.headers['x-topics']
1103
1139
  const includesOffChain = req.headers['x-includes-off-chain-values'] === 'true'
1104
1140
  if (typeof topicsHeader !== 'string') {
1105
- throw new Error('Missing x-topics header')
1141
+ throw new TypeError('Missing x-topics header')
1106
1142
  }
1107
1143
  const topics = JSON.parse(topicsHeader)
1108
1144
  let offChainValues: number[] | undefined
@@ -1374,7 +1410,7 @@ export default class OverlayExpress {
1374
1410
  data: {
1375
1411
  nodeName: this.name,
1376
1412
  network: this.network,
1377
- uptime: this.startTime !== undefined ? Date.now() - this.startTime.getTime() : 0,
1413
+ uptime: this.startTime != null ? Date.now() - this.startTime.getTime() : 0,
1378
1414
  startedAt: this.startTime?.toISOString(),
1379
1415
  shipRecordCount: shipCount,
1380
1416
  slapRecordCount: slapCount,
@@ -1407,9 +1443,9 @@ export default class OverlayExpress {
1407
1443
  const collection = db.collection('shipRecords')
1408
1444
 
1409
1445
  const search = typeof req.query.search === 'string' ? req.query.search : undefined
1410
- const rawPage = parseInt(req.query.page as string, 10)
1446
+ const rawPage = Number.parseInt(req.query.page as string, 10)
1411
1447
  const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage)
1412
- const rawLimit = parseInt(req.query.limit as string, 10)
1448
+ const rawLimit = Number.parseInt(req.query.limit as string, 10)
1413
1449
  const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit))
1414
1450
  const skip = (page - 1) * limit
1415
1451
 
@@ -1453,9 +1489,9 @@ export default class OverlayExpress {
1453
1489
  const collection = db.collection('slapRecords')
1454
1490
 
1455
1491
  const search = typeof req.query.search === 'string' ? req.query.search : undefined
1456
- const rawPage = parseInt(req.query.page as string, 10)
1492
+ const rawPage = Number.parseInt(req.query.page as string, 10)
1457
1493
  const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage)
1458
- const rawLimit = parseInt(req.query.limit as string, 10)
1494
+ const rawLimit = Number.parseInt(req.query.limit as string, 10)
1459
1495
  const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit))
1460
1496
  const skip = (page - 1) * limit
1461
1497
 
@@ -1532,48 +1568,9 @@ export default class OverlayExpress {
1532
1568
  }
1533
1569
 
1534
1570
  if (type === 'domain') {
1535
- await this.banService.banDomain(value, reason)
1536
-
1537
- // Also remove any existing records for this domain from SHIP and SLAP
1538
- const db = this.ensureMongo()
1539
- const [shipDeleted, slapDeleted] = await Promise.all([
1540
- db.collection('shipRecords').deleteMany({ domain: value }),
1541
- db.collection('slapRecords').deleteMany({ domain: value })
1542
- ])
1543
-
1544
- return res.status(200).json({
1545
- status: 'success',
1546
- message: `Domain "${value}" banned. Removed ${shipDeleted.deletedCount} SHIP and ${slapDeleted.deletedCount} SLAP records.`
1547
- })
1548
- } else {
1549
- // Parse outpoint: "txid.outputIndex"
1550
- const dotIndex = value.lastIndexOf('.')
1551
- if (dotIndex === -1) {
1552
- return res.status(400).json({ status: 'error', message: 'Outpoint format must be "txid.outputIndex"' })
1553
- }
1554
- const txid = value.substring(0, dotIndex)
1555
- const outputIndex = parseInt(value.substring(dotIndex + 1))
1556
- if (isNaN(outputIndex)) {
1557
- return res.status(400).json({ status: 'error', message: 'Invalid outputIndex in outpoint' })
1558
- }
1559
-
1560
- await this.banService.banOutpoint(txid, outputIndex, reason)
1561
-
1562
- // Also evict from lookup services
1563
- const services = Object.values(engine.lookupServices)
1564
- for (const service of services) {
1565
- try {
1566
- await service.outputEvicted(txid, outputIndex)
1567
- } catch {
1568
- continue
1569
- }
1570
- }
1571
-
1572
- return res.status(200).json({
1573
- status: 'success',
1574
- message: `Outpoint "${value}" banned and evicted from lookup services.`
1575
- })
1571
+ return await this.handleBanDomain(res, value, reason)
1576
1572
  }
1573
+ return await this.handleBanOutpoint(res, engine, value, reason)
1577
1574
  } catch (error) {
1578
1575
  return res.status(400).json({
1579
1576
  status: 'error',
@@ -1650,52 +1647,29 @@ export default class OverlayExpress {
1650
1647
  return res.status(400).json({ status: 'error', message: 'txid (string) and outputIndex (number) are required' })
1651
1648
  }
1652
1649
 
1653
- // Get the domain before removing (for ban option)
1650
+ // Look up domain before eviction if needed for banning
1654
1651
  let removedDomain: string | undefined
1655
1652
  if (shouldBanDomain === true || ban === true) {
1656
- const db = this.ensureMongo()
1657
- const shipRecord = await db.collection('shipRecords').findOne({ txid, outputIndex })
1658
- const slapRecord = await db.collection('slapRecords').findOne({ txid, outputIndex })
1659
- removedDomain = (shipRecord?.domain ?? slapRecord?.domain) as string | undefined
1653
+ removedDomain = await this.lookupDomainForOutpoint(txid, outputIndex)
1660
1654
  }
1661
1655
 
1662
- // Evict from specified service or all services
1663
- if (typeof service === 'string') {
1664
- const svc = engine.lookupServices[service]
1665
- if (svc !== undefined) {
1666
- await svc.outputEvicted(txid, outputIndex)
1667
- }
1668
- } else {
1669
- const services = Object.values(engine.lookupServices)
1670
- for (const svc of services) {
1671
- try {
1672
- await svc.outputEvicted(txid, outputIndex)
1673
- } catch {
1674
- continue
1675
- }
1676
- }
1677
- }
1656
+ await this.evictFromServices(engine, txid, outputIndex, service)
1678
1657
 
1679
- // Ban the outpoint if requested
1680
1658
  if (ban === true && this.banService !== undefined) {
1681
1659
  await this.banService.banOutpoint(txid, outputIndex, 'Manually removed by admin', removedDomain)
1682
1660
  }
1683
1661
 
1684
- // Ban the domain if requested
1685
1662
  if (shouldBanDomain === true && typeof removedDomain === 'string' && this.banService !== undefined) {
1686
- await this.banService.banDomain(removedDomain, 'Domain banned by admin via token removal')
1687
-
1688
- // Remove all records for this domain
1689
- const banDb = this.ensureMongo()
1690
- await Promise.all([
1691
- banDb.collection('shipRecords').deleteMany({ domain: removedDomain }),
1692
- banDb.collection('slapRecords').deleteMany({ domain: removedDomain })
1693
- ])
1663
+ await this.banDomainAndRemoveRecords(removedDomain, 'Domain banned by admin via token removal')
1694
1664
  }
1695
1665
 
1666
+ const banMsg = ban === true ? ' Outpoint banned.' : ''
1667
+ const domainMsg = shouldBanDomain === true && typeof removedDomain === 'string'
1668
+ ? ` Domain "${removedDomain}" banned.`
1669
+ : ''
1696
1670
  return res.status(200).json({
1697
1671
  status: 'success',
1698
- message: `Token ${txid}.${outputIndex} removed.${ban === true ? ' Outpoint banned.' : ''}${shouldBanDomain === true && typeof removedDomain === 'string' ? ` Domain "${removedDomain}" banned.` : ''}`
1672
+ message: `Token ${txid}.${outputIndex} removed.${banMsg}${domainMsg}`
1699
1673
  })
1700
1674
  } catch (error) {
1701
1675
  return res.status(400).json({
@@ -1765,9 +1739,9 @@ export default class OverlayExpress {
1765
1739
  await service.outputEvicted(req.body.txid, req.body.outputIndex)
1766
1740
  } else {
1767
1741
  const services = Object.values(engine.lookupServices)
1768
- for (let i = 0; i < services.length; i++) {
1742
+ for (const service of services) {
1769
1743
  try {
1770
- await services[i].outputEvicted(req.body.txid, req.body.outputIndex)
1744
+ await service.outputEvicted(req.body.txid, req.body.outputIndex)
1771
1745
  } catch {
1772
1746
  continue
1773
1747
  }