@bsv/overlay-express 2.2.0 → 2.3.0

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.
@@ -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'
@@ -189,6 +189,9 @@ export default class OverlayExpress {
189
189
  // ARC API Key
190
190
  arcApiKey: string | undefined = undefined
191
191
 
192
+ // Optional ARC callback token for /arc-ingest notifications
193
+ arcCallbackToken: string | undefined = undefined
194
+
192
195
  // Verbose request logging
193
196
  verboseRequestLogging: boolean = false
194
197
 
@@ -372,6 +375,15 @@ export default class OverlayExpress {
372
375
  this.logger.log(chalk.blue('ARC API key has been configured.'))
373
376
  }
374
377
 
378
+ /**
379
+ * Configures the ARC callback token expected by /arc-ingest.
380
+ * @param token - The token ARC should present when posting callback notifications.
381
+ */
382
+ configureArcCallbackToken (token: string): void {
383
+ this.arcCallbackToken = token
384
+ this.logger.log(chalk.blue('ARC callback token has been configured.'))
385
+ }
386
+
375
387
  /**
376
388
  * Enables or disables GASP synchronization (high-level setting).
377
389
  * This is a broad toggle that can be overridden or customized through syncConfiguration.
@@ -393,7 +405,7 @@ export default class OverlayExpress {
393
405
 
394
406
  /**
395
407
  * Configure Knex (SQL) database connection.
396
- * @param config - Knex configuration object, or MySQL connection string (e.g. mysql://overlayAdmin:overlay123@mysql:3306/overlay).
408
+ * @param config - Knex configuration object, or a MySQL connection string loaded from configuration.
397
409
  */
398
410
  async configureKnex (config: Knex.Knex.Config | string): Promise<void> {
399
411
  if (typeof config === 'string') {
@@ -509,7 +521,6 @@ export default class OverlayExpress {
509
521
  const knex = this.ensureKnex()
510
522
 
511
523
  if (autoConfigureShipSlap) {
512
- // Auto-configure SHIP and SLAP services
513
524
  this.configureTopicManager('tm_ship', new DiscoveryServices.SHIPTopicManager())
514
525
  this.configureTopicManager('tm_slap', new DiscoveryServices.SLAPTopicManager())
515
526
  this.configureLookupServiceWithMongo('ls_ship', (db) => new DiscoveryServices.SHIPLookupService(
@@ -520,137 +531,113 @@ export default class OverlayExpress {
520
531
  ))
521
532
  }
522
533
 
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
- }
545
-
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
- }
534
+ this.wrapBanAwareServices()
557
535
 
558
- // Build the actual Storage
536
+ const syncConfig = this.buildSyncConfig()
559
537
  const storage = new KnexStorage(knex)
560
- // Include the KnexStorage migrations
561
538
  this.migrationsToRun = [...KnexStorageMigrations.default, ...this.migrationsToRun]
562
539
 
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
- }
540
+ const broadcaster = this.buildBroadcaster()
541
+ const advertiser = await this.buildAdvertiser()
591
542
 
592
- // Construct the Engine with any advanced config overrides. Fallback to defaults.
593
543
  this.engine = new Engine(
594
544
  this.managers,
595
545
  this.services,
596
546
  storage,
597
- // chainTracker
598
- typeof this.engineConfig.chainTracker !== 'undefined'
599
- ? this.engineConfig.chainTracker
600
- : this.chainTracker,
601
- // hostingURL
547
+ this.engineConfig.chainTracker ?? this.chainTracker,
602
548
  `https://${this.advertisableFQDN}`,
603
- // shipTrackers
604
549
  this.network === 'test'
605
550
  ? (this.engineConfig.shipTrackers ?? DEFAULT_TESTNET_SLAP_TRACKERS)
606
551
  : 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
552
+ this.resolveSlapTrackers(),
614
553
  broadcaster ?? this.engineConfig.broadcaster,
615
- // advertiser
616
554
  advertiser,
617
- // syncConfiguration
618
555
  syncConfig,
619
- // logTime
620
556
  this.engineConfig.logTime ?? false,
621
- // logPrefix
622
557
  this.engineConfig.logPrefix ?? '[OVERLAY_ENGINE] ',
623
- // throwOnBroadcastFailure
624
558
  this.engineConfig.throwOnBroadcastFailure ?? false,
625
- // overlayBroadcastFacilitator
626
559
  this.engineConfig.overlayBroadcastFacilitator ?? new HTTPSOverlayBroadcastFacilitator(),
627
- // logger
628
560
  this.logger,
629
- // suppressDefaultSyncAdvertisements
630
561
  this.engineConfig.suppressDefaultSyncAdvertisements ?? true
631
562
  )
632
563
 
633
- // Create the server wallet for BSV mutual authentication.
634
- // This uses the same private key as the WalletAdvertiser.
564
+ this.initServerWallet()
565
+ this.logger.log(chalk.green('Engine has been configured.'))
566
+ }
567
+
568
+ /** Wrap SHIP/SLAP with ban-aware filter if BanService is configured. */
569
+ private wrapBanAwareServices (): void {
570
+ if (this.banService === undefined) return
571
+ for (const key of ['ls_ship', 'ls_slap'] as const) {
572
+ if (this.services[key] !== undefined) {
573
+ const label = key === 'ls_ship' ? 'SHIP' : 'SLAP'
574
+ this.services[key] = new BanAwareLookupWrapper(this.services[key], this.banService, label, this.logger)
575
+ this.logger.log(chalk.blue(`${label} lookup service wrapped with ban-aware filter.`))
576
+ }
577
+ }
578
+ }
579
+
580
+ /** Build the sync config based on enableGASPSync and engineConfig. */
581
+ private buildSyncConfig (): Record<string, string[] | 'SHIP' | false> {
582
+ if (this.enableGASPSync) {
583
+ return this.engineConfig.syncConfiguration ?? {}
584
+ }
585
+ const syncConfig: Record<string, string[] | 'SHIP' | false> = {}
586
+ for (const name of Object.keys(this.managers)) {
587
+ syncConfig[name] = false
588
+ }
589
+ return syncConfig
590
+ }
591
+
592
+ /** Build the ARC broadcaster if an API key is configured. */
593
+ private buildBroadcaster (): Broadcaster | undefined {
594
+ if (typeof this.arcApiKey !== 'string') return undefined
595
+ const arcUrl = this.network === 'test' ? 'https://arc-test.taal.com' : 'https://arc.taal.com'
596
+ return new ARC(arcUrl, {
597
+ apiKey: this.arcApiKey,
598
+ callbackUrl: `https://${this.advertisableFQDN}/arc-ingest`,
599
+ callbackToken: this.arcCallbackToken
600
+ })
601
+ }
602
+
603
+ /** Resolve the SLAP trackers from config or network defaults. */
604
+ private resolveSlapTrackers (): string[] | undefined {
605
+ if (Array.isArray(this.engineConfig.slapTrackers)) return this.engineConfig.slapTrackers
606
+ return this.network === 'test' ? DEFAULT_TESTNET_SLAP_TRACKERS : DEFAULT_SLAP_TRACKERS
607
+ }
608
+
609
+ /** Build the WalletAdvertiser (or use user-provided one). */
610
+ private async buildAdvertiser (): Promise<Advertiser | undefined> {
611
+ if (this.engineConfig.advertiser !== undefined) return this.engineConfig.advertiser
612
+ const storageBase = this.network === 'test'
613
+ ? 'https://staging-storage.babbage.systems'
614
+ : 'https://storage.babbage.systems'
615
+ try {
616
+ return new DiscoveryServices.WalletAdvertiser(
617
+ this.network,
618
+ this.privateKey,
619
+ storageBase,
620
+ `https://${this.advertisableFQDN}`
621
+ )
622
+ } catch (e) {
623
+ this.logger.log(`Advertiser not initialized for FQDN ${this.advertisableFQDN} - SHIP and SLAP will be disabled. Reason: ${e}`)
624
+ return undefined
625
+ }
626
+ }
627
+
628
+ /** Initialize the server wallet for BSV mutual authentication. */
629
+ private initServerWallet (): void {
635
630
  try {
636
631
  const keyDeriver = new KeyDeriver(new PrivateKey(this.privateKey, 'hex'))
637
632
  const storageManager = new WalletStorageManager(keyDeriver.identityKey)
638
633
  const signer = new WalletSigner(this.network, keyDeriver, storageManager)
639
634
  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
-
635
+ this.serverWallet = new Wallet(signer, services)
636
+ this.adminIdentityKey ??= keyDeriver.identityKey
648
637
  this.logger.log(chalk.blue('Server wallet initialized for BSV mutual authentication.'))
649
638
  } catch (e) {
650
- this.logger.log(chalk.yellow('Server wallet could not be initialized. BSV auth will not be available.'))
639
+ this.logger.log(chalk.yellow(`Server wallet could not be initialized. BSV auth will not be available. Reason: ${e}`))
651
640
  }
652
-
653
- this.logger.log(chalk.green('Engine has been configured.'))
654
641
  }
655
642
 
656
643
  /**
@@ -658,8 +645,8 @@ export default class OverlayExpress {
658
645
  * @throws Error if Knex is not configured
659
646
  */
660
647
  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!')
648
+ if (this.knex === undefined) {
649
+ throw new TypeError('You must configure your SQL database with the .configureKnex() method first!')
663
650
  }
664
651
  return this.knex
665
652
  }
@@ -669,8 +656,8 @@ export default class OverlayExpress {
669
656
  * @throws Error if MongoDB is not configured
670
657
  */
671
658
  private ensureMongo (): Db {
672
- if (typeof this.mongoDb === 'undefined') {
673
- throw new Error('You must configure your MongoDB connection with the .configureMongo() method first!')
659
+ if (this.mongoDb === undefined) {
660
+ throw new TypeError('You must configure your MongoDB connection with the .configureMongo() method first!')
674
661
  }
675
662
  return this.mongoDb
676
663
  }
@@ -680,8 +667,8 @@ export default class OverlayExpress {
680
667
  * @throws Error if the Engine is not configured
681
668
  */
682
669
  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!')
670
+ if (this.engine === undefined) {
671
+ throw new TypeError('You must configure your Overlay Services engine with the .configureEngine() method first!')
685
672
  }
686
673
  return this.engine
687
674
  }
@@ -701,6 +688,71 @@ export default class OverlayExpress {
701
688
  })
702
689
  }
703
690
 
691
+ /** Ban a domain and remove all its SHIP/SLAP records from MongoDB. */
692
+ private async handleBanDomain (res: express.Response, value: string, reason?: string): Promise<express.Response> {
693
+ await this.banService!.banDomain(value, reason)
694
+ const db = this.ensureMongo()
695
+ const [shipDeleted, slapDeleted] = await Promise.all([
696
+ db.collection('shipRecords').deleteMany({ domain: value }),
697
+ db.collection('slapRecords').deleteMany({ domain: value })
698
+ ])
699
+ return res.status(200).json({
700
+ status: 'success',
701
+ message: `Domain "${value}" banned. Removed ${shipDeleted.deletedCount} SHIP and ${slapDeleted.deletedCount} SLAP records.`
702
+ })
703
+ }
704
+
705
+ /** Parse outpoint string, ban it, and evict it from all lookup services. */
706
+ private async handleBanOutpoint (res: express.Response, engine: Engine, value: string, reason?: string): Promise<express.Response> {
707
+ const dotIndex = value.lastIndexOf('.')
708
+ if (dotIndex === -1) {
709
+ return res.status(400).json({ status: 'error', message: 'Outpoint format must be "txid.outputIndex"' })
710
+ }
711
+ const txid = value.substring(0, dotIndex)
712
+ const outputIndex = Number.parseInt(value.substring(dotIndex + 1))
713
+ if (Number.isNaN(outputIndex)) {
714
+ return res.status(400).json({ status: 'error', message: 'Invalid outputIndex in outpoint' })
715
+ }
716
+ await this.banService!.banOutpoint(txid, outputIndex, reason)
717
+ await this.evictFromServices(engine, txid, outputIndex)
718
+ return res.status(200).json({
719
+ status: 'success',
720
+ message: `Outpoint "${value}" banned and evicted from lookup services.`
721
+ })
722
+ }
723
+
724
+ /** Evict an output from a specific service or all services (silent per-service errors). */
725
+ private async evictFromServices (engine: Engine, txid: string, outputIndex: number, service?: string): Promise<void> {
726
+ if (typeof service === 'string') {
727
+ const svc = engine.lookupServices[service]
728
+ if (svc !== undefined) await svc.outputEvicted(txid, outputIndex)
729
+ return
730
+ }
731
+ for (const svc of Object.values(engine.lookupServices)) {
732
+ try { await svc.outputEvicted(txid, outputIndex) } catch { /* best-effort */ }
733
+ }
734
+ }
735
+
736
+ /** Look up the domain of an outpoint from SHIP or SLAP records. */
737
+ private async lookupDomainForOutpoint (txid: string, outputIndex: number): Promise<string | undefined> {
738
+ const db = this.ensureMongo()
739
+ const [shipRecord, slapRecord] = await Promise.all([
740
+ db.collection('shipRecords').findOne({ txid, outputIndex }),
741
+ db.collection('slapRecords').findOne({ txid, outputIndex })
742
+ ])
743
+ return (shipRecord?.domain ?? slapRecord?.domain) as string | undefined
744
+ }
745
+
746
+ /** Ban a domain and delete all SHIP/SLAP records for it. */
747
+ private async banDomainAndRemoveRecords (domain: string, reason: string): Promise<void> {
748
+ await this.banService!.banDomain(domain, reason)
749
+ const db = this.ensureMongo()
750
+ await Promise.all([
751
+ db.collection('shipRecords').deleteMany({ domain }),
752
+ db.collection('slapRecords').deleteMany({ domain })
753
+ ])
754
+ }
755
+
704
756
  private async runHealthCheck (
705
757
  definition: Required<Pick<HealthCheckDefinition, 'name' | 'scope' | 'critical'>> & { handler: HealthCheckHandler }
706
758
  ): Promise<HealthCheckResult> {
@@ -757,8 +809,8 @@ export default class OverlayExpress {
757
809
  scope: 'ready',
758
810
  critical: true,
759
811
  handler: async () => {
760
- if (typeof this.engine === 'undefined') {
761
- throw new Error('Overlay engine is not configured')
812
+ if (this.engine === undefined) {
813
+ throw new TypeError('Overlay engine is not configured')
762
814
  }
763
815
 
764
816
  return {
@@ -775,8 +827,8 @@ export default class OverlayExpress {
775
827
  scope: 'ready',
776
828
  critical: true,
777
829
  handler: async () => {
778
- if (typeof this.knex === 'undefined') {
779
- throw new Error('Knex is not configured')
830
+ if (this.knex === undefined) {
831
+ throw new TypeError('Knex is not configured')
780
832
  }
781
833
 
782
834
  await this.knex.raw('select 1 as ok')
@@ -793,8 +845,8 @@ export default class OverlayExpress {
793
845
  scope: 'ready',
794
846
  critical: true,
795
847
  handler: async () => {
796
- if (typeof this.mongoDb === 'undefined') {
797
- throw new Error('MongoDB is not configured')
848
+ if (this.mongoDb === undefined) {
849
+ throw new TypeError('MongoDB is not configured')
798
850
  }
799
851
 
800
852
  await this.mongoDb.command({ ping: 1 })
@@ -852,7 +904,7 @@ export default class OverlayExpress {
852
904
  port: this.port,
853
905
  network: this.network,
854
906
  startedAt: this.startTime?.toISOString(),
855
- uptimeMs: typeof this.startTime === 'undefined' ? 0 : Date.now() - this.startTime.getTime(),
907
+ uptimeMs: this.startTime === undefined ? 0 : Date.now() - this.startTime.getTime(),
856
908
  topicManagerCount: Object.keys(this.managers).length,
857
909
  lookupServiceCount: Object.keys(this.services).length
858
910
  },
@@ -1102,7 +1154,7 @@ export default class OverlayExpress {
1102
1154
  const topicsHeader = req.headers['x-topics']
1103
1155
  const includesOffChain = req.headers['x-includes-off-chain-values'] === 'true'
1104
1156
  if (typeof topicsHeader !== 'string') {
1105
- throw new Error('Missing x-topics header')
1157
+ throw new TypeError('Missing x-topics header')
1106
1158
  }
1107
1159
  const topics = JSON.parse(topicsHeader)
1108
1160
  let offChainValues: number[] | undefined
@@ -1217,6 +1269,18 @@ export default class OverlayExpress {
1217
1269
  this.app.post('/arc-ingest', (req, res) => {
1218
1270
  ; (async () => {
1219
1271
  try {
1272
+ if (typeof this.arcCallbackToken === 'string' && this.arcCallbackToken.length > 0) {
1273
+ const authorization = req.headers.authorization
1274
+ const headerToken = Array.isArray(authorization) ? authorization[0] : authorization
1275
+ const xCallbackToken = req.headers['x-callback-token']
1276
+ const callbackToken = Array.isArray(xCallbackToken) ? xCallbackToken[0] : xCallbackToken
1277
+ const bearerToken = typeof headerToken === 'string' && headerToken.startsWith('Bearer ')
1278
+ ? headerToken.slice('Bearer '.length)
1279
+ : headerToken
1280
+ if (bearerToken !== this.arcCallbackToken && callbackToken !== this.arcCallbackToken) {
1281
+ return res.status(401).json({ status: 'error', message: 'Unauthorized callback' })
1282
+ }
1283
+ }
1220
1284
  const { txid, merklePath: merklePathHex, blockHeight } = req.body
1221
1285
  const merklePath = MerklePath.fromHex(merklePathHex)
1222
1286
  await engine.handleNewMerkleProof(txid, merklePath, blockHeight)
@@ -1315,7 +1379,7 @@ export default class OverlayExpress {
1315
1379
  */
1316
1380
  const checkAdminAuth = (req: express.Request, res: express.Response, next: express.NextFunction): void => {
1317
1381
  // Method 1: BSV mutual authentication (identity key match)
1318
- const authReq = req as AuthRequest
1382
+ const authReq = req as unknown as AuthRequest
1319
1383
  if (
1320
1384
  typeof this.adminIdentityKey === 'string' &&
1321
1385
  authReq.auth !== undefined &&
@@ -1374,7 +1438,7 @@ export default class OverlayExpress {
1374
1438
  data: {
1375
1439
  nodeName: this.name,
1376
1440
  network: this.network,
1377
- uptime: this.startTime !== undefined ? Date.now() - this.startTime.getTime() : 0,
1441
+ uptime: this.startTime != null ? Date.now() - this.startTime.getTime() : 0,
1378
1442
  startedAt: this.startTime?.toISOString(),
1379
1443
  shipRecordCount: shipCount,
1380
1444
  slapRecordCount: slapCount,
@@ -1407,9 +1471,9 @@ export default class OverlayExpress {
1407
1471
  const collection = db.collection('shipRecords')
1408
1472
 
1409
1473
  const search = typeof req.query.search === 'string' ? req.query.search : undefined
1410
- const rawPage = parseInt(req.query.page as string, 10)
1474
+ const rawPage = Number.parseInt(req.query.page as string, 10)
1411
1475
  const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage)
1412
- const rawLimit = parseInt(req.query.limit as string, 10)
1476
+ const rawLimit = Number.parseInt(req.query.limit as string, 10)
1413
1477
  const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit))
1414
1478
  const skip = (page - 1) * limit
1415
1479
 
@@ -1453,9 +1517,9 @@ export default class OverlayExpress {
1453
1517
  const collection = db.collection('slapRecords')
1454
1518
 
1455
1519
  const search = typeof req.query.search === 'string' ? req.query.search : undefined
1456
- const rawPage = parseInt(req.query.page as string, 10)
1520
+ const rawPage = Number.parseInt(req.query.page as string, 10)
1457
1521
  const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage)
1458
- const rawLimit = parseInt(req.query.limit as string, 10)
1522
+ const rawLimit = Number.parseInt(req.query.limit as string, 10)
1459
1523
  const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit))
1460
1524
  const skip = (page - 1) * limit
1461
1525
 
@@ -1532,48 +1596,9 @@ export default class OverlayExpress {
1532
1596
  }
1533
1597
 
1534
1598
  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
- })
1599
+ return await this.handleBanDomain(res, value, reason)
1576
1600
  }
1601
+ return await this.handleBanOutpoint(res, engine, value, reason)
1577
1602
  } catch (error) {
1578
1603
  return res.status(400).json({
1579
1604
  status: 'error',
@@ -1650,52 +1675,29 @@ export default class OverlayExpress {
1650
1675
  return res.status(400).json({ status: 'error', message: 'txid (string) and outputIndex (number) are required' })
1651
1676
  }
1652
1677
 
1653
- // Get the domain before removing (for ban option)
1678
+ // Look up domain before eviction if needed for banning
1654
1679
  let removedDomain: string | undefined
1655
1680
  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
1681
+ removedDomain = await this.lookupDomainForOutpoint(txid, outputIndex)
1660
1682
  }
1661
1683
 
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
- }
1684
+ await this.evictFromServices(engine, txid, outputIndex, service)
1678
1685
 
1679
- // Ban the outpoint if requested
1680
1686
  if (ban === true && this.banService !== undefined) {
1681
1687
  await this.banService.banOutpoint(txid, outputIndex, 'Manually removed by admin', removedDomain)
1682
1688
  }
1683
1689
 
1684
- // Ban the domain if requested
1685
1690
  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
- ])
1691
+ await this.banDomainAndRemoveRecords(removedDomain, 'Domain banned by admin via token removal')
1694
1692
  }
1695
1693
 
1694
+ const banMsg = ban === true ? ' Outpoint banned.' : ''
1695
+ const domainMsg = shouldBanDomain === true && typeof removedDomain === 'string'
1696
+ ? ` Domain "${removedDomain}" banned.`
1697
+ : ''
1696
1698
  return res.status(200).json({
1697
1699
  status: 'success',
1698
- message: `Token ${txid}.${outputIndex} removed.${ban === true ? ' Outpoint banned.' : ''}${shouldBanDomain === true && typeof removedDomain === 'string' ? ` Domain "${removedDomain}" banned.` : ''}`
1700
+ message: `Token ${txid}.${outputIndex} removed.${banMsg}${domainMsg}`
1699
1701
  })
1700
1702
  } catch (error) {
1701
1703
  return res.status(400).json({
@@ -1765,9 +1767,9 @@ export default class OverlayExpress {
1765
1767
  await service.outputEvicted(req.body.txid, req.body.outputIndex)
1766
1768
  } else {
1767
1769
  const services = Object.values(engine.lookupServices)
1768
- for (let i = 0; i < services.length; i++) {
1770
+ for (const service of services) {
1769
1771
  try {
1770
- await services[i].outputEvicted(req.body.txid, req.body.outputIndex)
1772
+ await service.outputEvicted(req.body.txid, req.body.outputIndex)
1771
1773
  } catch {
1772
1774
  continue
1773
1775
  }