@bsv/overlay-express 0.8.2-rc1 → 0.8.2

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.
@@ -13,7 +13,9 @@ import {
13
13
  HTTPSOverlayBroadcastFacilitator,
14
14
  DEFAULT_TESTNET_SLAP_TRACKERS,
15
15
  DEFAULT_SLAP_TRACKERS,
16
- Utils
16
+ Utils,
17
+ Beef,
18
+ Transaction
17
19
  } from '@bsv/sdk'
18
20
  import Knex from 'knex'
19
21
  import { MongoClient, Db } from 'mongodb'
@@ -757,10 +759,59 @@ export default class OverlayExpress {
757
759
  this.app.post('/lookup', (req, res) => {
758
760
  ; (async () => {
759
761
  try {
760
- const result = await engine.lookup(req.body)
761
- return res.status(200).json(result)
762
+ // Check for aggregation header to determine response format
763
+ const aggregationHeader = req.headers['x-aggregation']
764
+ const shouldReturnBinary = aggregationHeader === 'yes'
765
+
766
+ // Validate request body structure
767
+ const lookupRequest = req.body as { service: string, query: unknown }
768
+ if (typeof lookupRequest.service !== 'string' || lookupRequest.query === undefined) {
769
+ return res.status(400).json({
770
+ status: 'error',
771
+ message: 'Invalid request: body must contain "service" (string) and "query" fields'
772
+ })
773
+ }
774
+
775
+ const result = await engine.lookup(lookupRequest)
776
+
777
+ if (!shouldReturnBinary) {
778
+ // Return JSON response (default behavior)
779
+ return res.status(200).json(result)
780
+ }
781
+
782
+ const beef = new Beef()
783
+ const outputs = result.outputs
784
+
785
+ // Serialize in the format expected by LookupResolver
786
+ const writer = new Utils.Writer()
787
+
788
+ // Write number of outpoints
789
+ writer.writeVarIntNum(outputs.length)
790
+
791
+ // Write each outpoint data
792
+ for (const output of outputs) {
793
+ const tx = Transaction.fromBEEF(output.beef)
794
+ // Write txid (32 bytes)
795
+ writer.write(tx.id())
796
+ // Write outputIndex
797
+ writer.writeVarIntNum(output.outputIndex)
798
+ // Write context length and data
799
+ if ((output.context != null) && output.context.length > 0) {
800
+ writer.writeVarIntNum(output.context.length)
801
+ writer.write(output.context)
802
+ } else {
803
+ writer.writeVarIntNum(0)
804
+ }
805
+ beef.mergeTransaction(tx)
806
+ }
807
+
808
+ // Write the beef data
809
+ writer.write(beef.toBinary())
810
+
811
+ res.setHeader('Content-Type', 'application/octet-stream')
812
+ return res.status(200).send(Buffer.from(writer.toArray()))
762
813
  } catch (error) {
763
- console.error(chalk.red('❌ Error in /lookup:'), error)
814
+ console.error(chalk.red('Error in /lookup:'), error)
764
815
  return res.status(400).json({
765
816
  status: 'error',
766
817
  message: error instanceof Error ? error.message : 'An unknown error occurred'