@bsv/sdk 1.8.3 → 1.8.5

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/sdk",
3
- "version": "1.8.3",
3
+ "version": "1.8.5",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -1,5 +1,6 @@
1
1
  import { Transaction } from '../transaction/index.js'
2
2
  import OverlayAdminTokenTemplate from './OverlayAdminTokenTemplate.js'
3
+ import * as Utils from '../primitives/utils.js'
3
4
 
4
5
  // Only bind window.fetch in the browser
5
6
  const defaultFetch = typeof window !== 'undefined' ? fetch.bind(window) : fetch
@@ -135,14 +136,47 @@ export class HTTPSOverlayLookupFacilitator implements OverlayLookupFacilitator {
135
136
  try {
136
137
  const fco: RequestInit = {
137
138
  method: 'POST',
138
- headers: { 'Content-Type': 'application/json' },
139
+ headers: {
140
+ 'Content-Type': 'application/json',
141
+ 'X-Aggregation': 'yes'
142
+ },
139
143
  body: JSON.stringify({ service: question.service, query: question.query }),
140
144
  signal: controller?.signal
141
145
  }
142
146
  const response: Response = await this.fetchClient(`${url}/lookup`, fco)
143
147
 
144
148
  if (!response.ok) throw new Error(`Failed to facilitate lookup (HTTP ${response.status})`)
145
- return await response.json()
149
+ if (response.headers.get('content-type') === 'application/json') {
150
+ return await response.json()
151
+ } else {
152
+ const payload = await response.arrayBuffer()
153
+ const r = new Utils.Reader([...new Uint8Array(payload)])
154
+ const nOutpoints = r.readVarIntNum()
155
+ const outpoints: Array<{ txid: string, outputIndex: number, context?: number[] }> = []
156
+ for (let i = 0; i < nOutpoints; i++) {
157
+ const txid = Utils.toHex(r.read(32))
158
+ const outputIndex = r.readVarIntNum()
159
+ const contextLength = r.readVarIntNum()
160
+ let context
161
+ if (contextLength > 0) {
162
+ context = r.read(contextLength)
163
+ }
164
+ outpoints.push({
165
+ txid,
166
+ outputIndex,
167
+ context
168
+ })
169
+ }
170
+ const beef = r.read()
171
+ return {
172
+ type: 'output-list',
173
+ outputs: outpoints.map(x => ({
174
+ outputIndex: x.outputIndex,
175
+ context: x.context,
176
+ beef: Transaction.fromBEEF(beef, x.txid).toBEEF()
177
+ }))
178
+ }
179
+ }
146
180
  } catch (e) {
147
181
  // Normalize timeouts to a consistent error message
148
182
  if ((e as any)?.name === 'AbortError') throw new Error('Request timed out')
@@ -176,7 +210,9 @@ export default class LookupResolver {
176
210
  this.networkPreset = config.networkPreset ?? 'mainnet'
177
211
  this.facilitator = config.facilitator ?? new HTTPSOverlayLookupFacilitator(undefined, this.networkPreset === 'local')
178
212
  this.slapTrackers = config.slapTrackers ?? (this.networkPreset === 'mainnet' ? DEFAULT_SLAP_TRACKERS : DEFAULT_TESTNET_SLAP_TRACKERS)
179
- this.hostOverrides = config.hostOverrides ?? {}
213
+ const hostOverrides = config.hostOverrides ?? {}
214
+ this.assertValidOverrideServices(hostOverrides)
215
+ this.hostOverrides = hostOverrides
180
216
  this.additionalHosts = config.additionalHosts ?? {}
181
217
 
182
218
  // cache tuning
@@ -375,4 +411,12 @@ export default class LookupResolver {
375
411
  const firstKey = m.keys().next().value
376
412
  if (firstKey !== undefined) m.delete(firstKey)
377
413
  }
414
+
415
+ private assertValidOverrideServices (overrides: Record<string, string[]>): void {
416
+ for (const service of Object.keys(overrides)) {
417
+ if (!service.startsWith('ls_')) {
418
+ throw new Error(`Host override service names must start with "ls_": ${service}`)
419
+ }
420
+ }
421
+ }
378
422
  }