@bsv/sdk 2.1.2 → 2.1.3

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.
Files changed (39) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/auth/Peer.js +21 -18
  3. package/dist/cjs/src/auth/Peer.js.map +1 -1
  4. package/dist/cjs/src/auth/SessionManager.js.map +1 -1
  5. package/dist/cjs/src/auth/clients/AuthFetch.js +4 -1
  6. package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -1
  7. package/dist/cjs/src/overlay-tools/LookupResolver.js +99 -28
  8. package/dist/cjs/src/overlay-tools/LookupResolver.js.map +1 -1
  9. package/dist/cjs/src/transaction/MerklePath.js +1 -1
  10. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  11. package/dist/esm/src/auth/Peer.js +28 -18
  12. package/dist/esm/src/auth/Peer.js.map +1 -1
  13. package/dist/esm/src/auth/SessionManager.js.map +1 -1
  14. package/dist/esm/src/auth/clients/AuthFetch.js +4 -1
  15. package/dist/esm/src/auth/clients/AuthFetch.js.map +1 -1
  16. package/dist/esm/src/overlay-tools/LookupResolver.js +99 -28
  17. package/dist/esm/src/overlay-tools/LookupResolver.js.map +1 -1
  18. package/dist/esm/src/transaction/MerklePath.js +1 -1
  19. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  20. package/dist/types/src/auth/Peer.d.ts +3 -3
  21. package/dist/types/src/auth/Peer.d.ts.map +1 -1
  22. package/dist/types/src/auth/SessionManager.d.ts +21 -0
  23. package/dist/types/src/auth/SessionManager.d.ts.map +1 -1
  24. package/dist/types/src/auth/clients/AuthFetch.d.ts +2 -2
  25. package/dist/types/src/auth/clients/AuthFetch.d.ts.map +1 -1
  26. package/dist/types/src/overlay-tools/LookupResolver.d.ts +1 -0
  27. package/dist/types/src/overlay-tools/LookupResolver.d.ts.map +1 -1
  28. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  29. package/dist/umd/bundle.js +4 -4
  30. package/package.json +1 -1
  31. package/src/auth/Peer.ts +30 -20
  32. package/src/auth/SessionManager.ts +22 -0
  33. package/src/auth/__tests/Peer.test.ts +47 -1
  34. package/src/auth/clients/AuthFetch.ts +6 -3
  35. package/src/overlay-tools/LookupResolver.ts +102 -25
  36. package/src/overlay-tools/__tests/LookupResolver.additional.test.ts +90 -0
  37. package/src/script/__tests/Spend.test.ts +45 -4
  38. package/src/transaction/MerklePath.ts +1 -1
  39. package/src/transaction/__tests/Transaction.test.ts +17 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
package/src/auth/Peer.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SessionManager } from './SessionManager.js'
1
+ import { SessionManager, AsyncSessionManager } from './SessionManager.js'
2
2
  import {
3
3
  createNonce,
4
4
  verifyNonce,
@@ -28,6 +28,13 @@ const BufferCtor =
28
28
  * This version supports multiple concurrent sessions per peer identityKey.
29
29
  */
30
30
  export class Peer {
31
+ // Declared as the synchronous {@link SessionManager} for back-compat with
32
+ // pre-existing consumers that read `peer.sessionManager.getSession(...)`
33
+ // and friends as synchronous calls. The constructor also accepts an
34
+ // {@link AsyncSessionManager}; in that case the runtime value's methods
35
+ // return Promises. Peer always awaits internal calls so both work, but
36
+ // external code that reaches in directly should match the implementation
37
+ // it injected. See `AsyncSessionManager` for the opt-in async contract.
31
38
  public sessionManager: SessionManager
32
39
  private readonly transport: Transport
33
40
  private readonly wallet: WalletInterface
@@ -97,7 +104,7 @@ export class Peer {
97
104
  * @param {WalletInterface} wallet - The wallet instance used for cryptographic operations.
98
105
  * @param {Transport} transport - The transport mechanism used for sending and receiving messages.
99
106
  * @param {RequestedCertificateSet} [certificatesToRequest] - Optional set of certificates to request from a peer during the initial handshake.
100
- * @param {SessionManager} [sessionManager] - Optional SessionManager to be used for managing peer sessions.
107
+ * @param {SessionManager | AsyncSessionManager} [sessionManager] - Optional session store. Pass an {@link AsyncSessionManager} for shared/durable storage in load-balanced deployments; otherwise the default in-process {@link SessionManager} is used.
101
108
  * @param {boolean} [autoPersistLastSession] - Whether to auto-persist the session with the last-interacted-with peer. Defaults to true.
102
109
  * @param {OriginatorDomainNameStringUnder250Bytes} [originator] - Optional originator domain name.
103
110
  */
@@ -105,7 +112,7 @@ export class Peer {
105
112
  wallet: WalletInterface,
106
113
  transport: Transport,
107
114
  certificatesToRequest?: RequestedCertificateSet,
108
- sessionManager?: SessionManager,
115
+ sessionManager?: SessionManager | AsyncSessionManager,
109
116
  autoPersistLastSession?: boolean,
110
117
  originator?: OriginatorDomainNameStringUnder250Bytes
111
118
  ) {
@@ -117,8 +124,11 @@ export class Peer {
117
124
  types: {}
118
125
  }
119
126
  this.ready = this.transport.onData(this.handleIncomingMessage.bind(this)) // NOSONAR(typescript:S7059): listener must register synchronously — see ready field comment
127
+ // Cast keeps the public field typed as the synchronous `SessionManager`
128
+ // for back-compat. When an `AsyncSessionManager` is injected, the actual
129
+ // runtime methods return Promises — Peer awaits them internally below.
120
130
  this.sessionManager =
121
- sessionManager ?? new SessionManager()
131
+ (sessionManager ?? new SessionManager()) as SessionManager
122
132
  if (autoPersistLastSession === false) {
123
133
  this.autoPersistLastSession = false
124
134
  } else {
@@ -178,7 +188,7 @@ export class Peer {
178
188
  }
179
189
 
180
190
  peerSession.lastUpdate = Date.now()
181
- this.sessionManager.updateSession(peerSession)
191
+ await this.sessionManager.updateSession(peerSession)
182
192
 
183
193
  try {
184
194
  await this.transport.send(generalMessage)
@@ -233,7 +243,7 @@ export class Peer {
233
243
 
234
244
  // Update last-used timestamp
235
245
  peerSession.lastUpdate = Date.now()
236
- this.sessionManager.updateSession(peerSession)
246
+ await this.sessionManager.updateSession(peerSession)
237
247
 
238
248
  try {
239
249
  await this.transport.send(certRequestMessage)
@@ -262,7 +272,7 @@ export class Peer {
262
272
 
263
273
  let peerSession: PeerSession | undefined
264
274
  if (typeof identityKey === 'string') {
265
- peerSession = this.sessionManager.getSession(identityKey)
275
+ peerSession = await this.sessionManager.getSession(identityKey)
266
276
  }
267
277
 
268
278
  // If that session doesn't exist or isn't authenticated, initiate handshake
@@ -270,7 +280,7 @@ export class Peer {
270
280
  // This will create a brand-new session
271
281
  const sessionNonce = await this.initiateHandshake(identityKey)
272
282
  // Now retrieve it by the sessionNonce
273
- peerSession = this.sessionManager.getSession(sessionNonce)
283
+ peerSession = await this.sessionManager.getSession(sessionNonce)
274
284
  if (peerSession?.isAuthenticated !== true) {
275
285
  throw new Error('Unable to establish mutual authentication with peer!')
276
286
  }
@@ -367,7 +377,7 @@ export class Peer {
367
377
  const certificatesRequired =
368
378
  this.certificatesToRequest.certifiers.length > 0
369
379
 
370
- this.sessionManager.addSession({
380
+ await this.sessionManager.addSession({
371
381
  isAuthenticated: false,
372
382
  sessionNonce,
373
383
  peerIdentityKey: identityKey,
@@ -511,7 +521,7 @@ export class Peer {
511
521
  Array.isArray(this.certificatesToRequest?.certifiers) &&
512
522
  this.certificatesToRequest.certifiers.length > 0
513
523
 
514
- this.sessionManager.addSession({
524
+ await this.sessionManager.addSession({
515
525
  isAuthenticated: true,
516
526
  sessionNonce,
517
527
  peerNonce: message.initialNonce,
@@ -588,7 +598,7 @@ export class Peer {
588
598
  )
589
599
  }
590
600
 
591
- const peerSession = this.sessionManager.getSession(message.yourNonce as string)
601
+ const peerSession = await this.sessionManager.getSession(message.yourNonce as string)
592
602
  if (peerSession == null) {
593
603
  throw new Error(`Peer session not found for peer: ${message.identityKey}`)
594
604
  }
@@ -624,7 +634,7 @@ export class Peer {
624
634
  peerSession.certificatesValidated = !peerSession.certificatesRequired
625
635
 
626
636
  peerSession.lastUpdate = Date.now()
627
- this.sessionManager.updateSession(peerSession)
637
+ await this.sessionManager.updateSession(peerSession)
628
638
 
629
639
  // --- Validate certificates if provided ---
630
640
  if (
@@ -641,7 +651,7 @@ export class Peer {
641
651
 
642
652
  peerSession.certificatesValidated = true
643
653
  peerSession.lastUpdate = Date.now()
644
- this.sessionManager.updateSession(peerSession)
654
+ await this.sessionManager.updateSession(peerSession)
645
655
 
646
656
  // Resolve any promises waiting for certificate validation
647
657
  if (peerSession.sessionNonce != null) {
@@ -711,7 +721,7 @@ export class Peer {
711
721
  `Unable to verify nonce for certificate request message from: ${message.identityKey}`
712
722
  )
713
723
  }
714
- const peerSession = this.sessionManager.getSession(message.yourNonce as string)
724
+ const peerSession = await this.sessionManager.getSession(message.yourNonce as string)
715
725
  if (peerSession == null) {
716
726
  throw new Error(`Session not found for nonce: ${message.yourNonce as string}`)
717
727
  }
@@ -731,7 +741,7 @@ export class Peer {
731
741
 
732
742
  // Update usage
733
743
  peerSession.lastUpdate = Date.now()
734
- this.sessionManager.updateSession(peerSession)
744
+ await this.sessionManager.updateSession(peerSession)
735
745
 
736
746
  if (
737
747
  (message.requestedCertificates != null) &&
@@ -789,7 +799,7 @@ export class Peer {
789
799
 
790
800
  // Update usage
791
801
  peerSession.lastUpdate = Date.now()
792
- this.sessionManager.updateSession(peerSession)
802
+ await this.sessionManager.updateSession(peerSession)
793
803
 
794
804
  try {
795
805
  await this.transport.send(certificateResponse)
@@ -813,7 +823,7 @@ export class Peer {
813
823
  )
814
824
  }
815
825
 
816
- const peerSession = this.sessionManager.getSession(message.yourNonce as string)
826
+ const peerSession = await this.sessionManager.getSession(message.yourNonce as string)
817
827
  if (peerSession == null) {
818
828
  throw new Error(`Session not found for nonce: ${message.yourNonce as string}`)
819
829
  }
@@ -843,7 +853,7 @@ export class Peer {
843
853
 
844
854
  peerSession.certificatesValidated = true
845
855
  peerSession.lastUpdate = Date.now()
846
- this.sessionManager.updateSession(peerSession)
856
+ await this.sessionManager.updateSession(peerSession)
847
857
 
848
858
  // Resolve any promises waiting for certificate validation
849
859
  if (peerSession.sessionNonce != null) {
@@ -878,7 +888,7 @@ export class Peer {
878
888
  )
879
889
  }
880
890
 
881
- const peerSession = this.sessionManager.getSession(message.yourNonce as string)
891
+ const peerSession = await this.sessionManager.getSession(message.yourNonce as string)
882
892
  if (peerSession == null) {
883
893
  throw new Error(`Session not found for nonce: ${message.yourNonce as string}`)
884
894
  }
@@ -942,7 +952,7 @@ export class Peer {
942
952
 
943
953
  // Mark last usage
944
954
  peerSession.lastUpdate = Date.now()
945
- this.sessionManager.updateSession(peerSession)
955
+ await this.sessionManager.updateSession(peerSession)
946
956
 
947
957
  // Update lastInteractedWithPeer
948
958
  this.lastInteractedWithPeer = message.identityKey
@@ -1,5 +1,27 @@
1
1
  import { PeerSession } from './types.js'
2
2
 
3
+ /**
4
+ * Opt-in async session-manager contract for horizontally scaled deployments.
5
+ *
6
+ * The default in-process {@link SessionManager} stores BRC-103 nonce/session
7
+ * state in memory and is synchronous. Multi-instance HTTP servers that need
8
+ * every instance to resolve the same handshake state — e.g. behind a load
9
+ * balancer without sticky routing — can implement `AsyncSessionManager`
10
+ * against a shared store such as Redis or SQL and pass it to {@link Peer}
11
+ * or `createAuthMiddleware` instead.
12
+ *
13
+ * {@link Peer} accepts `SessionManager | AsyncSessionManager` and awaits every
14
+ * call internally, so sync stores incur no extra latency while async stores
15
+ * work transparently.
16
+ */
17
+ export interface AsyncSessionManager {
18
+ addSession: (session: PeerSession) => Promise<void>
19
+ updateSession: (session: PeerSession) => Promise<void>
20
+ getSession: (identifier: string) => Promise<PeerSession | undefined>
21
+ removeSession: (session: PeerSession) => Promise<void>
22
+ hasSession: (identifier: string) => Promise<boolean>
23
+ }
24
+
3
25
  /**
4
26
  * Manages sessions for peers, allowing multiple concurrent sessions
5
27
  * per identity key. Primary lookup is always by `sessionNonce`.
@@ -1,5 +1,5 @@
1
1
  import { Peer } from '../../auth/Peer.js'
2
- import { AuthMessage, Transport } from '../../auth/types.js'
2
+ import { AuthMessage, PeerSession, Transport } from '../../auth/types.js'
3
3
  import { jest } from '@jest/globals'
4
4
  import { WalletInterface } from '../../wallet/Wallet.interfaces.js'
5
5
  import { Utils, PrivateKey } from '../../primitives/index.js'
@@ -8,6 +8,7 @@ import { MasterCertificate } from '../../auth/certificates/MasterCertificate.js'
8
8
  import { getVerifiableCertificates } from '../../auth/utils/getVerifiableCertificates.js'
9
9
  import { CompletedProtoWallet } from '../certificates/__tests/CompletedProtoWallet.js'
10
10
  import { SimplifiedFetchTransport } from '../../auth/transports/SimplifiedFetchTransport.js'
11
+ import { SessionManager, AsyncSessionManager } from '../../auth/SessionManager.js'
11
12
 
12
13
  const certifierPrivKey = new PrivateKey(21)
13
14
  const alicePrivKey = new PrivateKey(22)
@@ -51,6 +52,35 @@ class LocalTransport implements Transport {
51
52
  }
52
53
  }
53
54
 
55
+ class AsyncSessionStore implements AsyncSessionManager {
56
+ private readonly sessions = new SessionManager()
57
+
58
+ async addSession(session: PeerSession): Promise<void> {
59
+ await Promise.resolve()
60
+ this.sessions.addSession(session)
61
+ }
62
+
63
+ async updateSession(session: PeerSession): Promise<void> {
64
+ await Promise.resolve()
65
+ this.sessions.updateSession(session)
66
+ }
67
+
68
+ async getSession(identifier: string): Promise<PeerSession | undefined> {
69
+ await Promise.resolve()
70
+ return this.sessions.getSession(identifier)
71
+ }
72
+
73
+ async removeSession(session: PeerSession): Promise<void> {
74
+ await Promise.resolve()
75
+ this.sessions.removeSession(session)
76
+ }
77
+
78
+ async hasSession(identifier: string): Promise<boolean> {
79
+ await Promise.resolve()
80
+ return this.sessions.hasSession(identifier)
81
+ }
82
+ }
83
+
54
84
  function waitForNextGeneralMessage(
55
85
  peer: Peer,
56
86
  handler?: (senderPublicKey: string, payload: number[]) => void
@@ -290,6 +320,22 @@ describe('Peer class mutual authentication and certificate exchange', () => {
290
320
  expect(certificatesReceivedByBob).toEqual([])
291
321
  }, 15000)
292
322
 
323
+ it('supports asynchronous shared session managers', async () => {
324
+ alice = new Peer(walletA, transportA, undefined, new AsyncSessionStore())
325
+ bob = new Peer(walletB, transportB, undefined, new AsyncSessionStore())
326
+
327
+ const bobReceivedGeneralMessage = waitForNextGeneralMessage(bob)
328
+ const aliceReceivedGeneralMessage = waitForNextGeneralMessage(alice)
329
+
330
+ bob.listenForGeneralMessages((senderPublicKey) => {
331
+ void bob.toPeer(Utils.toArray('Hello Alice!'), senderPublicKey)
332
+ })
333
+
334
+ await alice.toPeer(Utils.toArray('Hello Bob!'))
335
+ await bobReceivedGeneralMessage
336
+ await aliceReceivedGeneralMessage
337
+ }, 15000)
338
+
293
339
  it('Alice talks to Bob across two devices, Bob can respond across both sessions', async () => {
294
340
  const transportA1 = new LocalTransport()
295
341
  const transportA2 = new LocalTransport()
@@ -7,7 +7,7 @@ import { OriginatorDomainNameStringUnder250Bytes, WalletInterface } from '../../
7
7
  import { createNonce } from '../utils/createNonce.js'
8
8
  import { Peer } from '../Peer.js'
9
9
  import { SimplifiedFetchTransport } from '../transports/SimplifiedFetchTransport.js'
10
- import { SessionManager } from '../SessionManager.js'
10
+ import { SessionManager, AsyncSessionManager } from '../SessionManager.js'
11
11
  import { RequestedCertificateSet } from '../types.js'
12
12
  import { VerifiableCertificate } from '../certificates/VerifiableCertificate.js'
13
13
  import { Writer } from '../../primitives/utils.js'
@@ -79,10 +79,13 @@ export class AuthFetch {
79
79
  * @param wallet - The wallet instance for signing and authentication.
80
80
  * @param requestedCertificates - Optional set of certificates to request from peers.
81
81
  */
82
- constructor(wallet: WalletInterface, requestedCertificates?: RequestedCertificateSet, sessionManager?: SessionManager, originator?: OriginatorDomainNameStringUnder250Bytes) {
82
+ constructor(wallet: WalletInterface, requestedCertificates?: RequestedCertificateSet, sessionManager?: SessionManager | AsyncSessionManager, originator?: OriginatorDomainNameStringUnder250Bytes) {
83
83
  this.wallet = wallet
84
84
  this.requestedCertificates = requestedCertificates
85
- this.sessionManager = sessionManager ?? new SessionManager()
85
+ // See `Peer.sessionManager`: field stays typed as the synchronous
86
+ // `SessionManager` for back-compat; if an `AsyncSessionManager` is
87
+ // injected, the underlying Peer awaits all calls internally.
88
+ this.sessionManager = (sessionManager ?? new SessionManager()) as SessionManager
86
89
  this.originator = originator
87
90
  }
88
91
 
@@ -105,6 +105,73 @@ export const DEFAULT_TESTNET_SLAP_TRACKERS: string[] = [
105
105
 
106
106
  const MAX_TRACKER_WAIT_TIME = 5000
107
107
 
108
+ /** A wall-clock deadline that rejects after `timeoutMs`, optionally aborting a controller. */
109
+ interface Deadline {
110
+ /** Rejects with `Error('Request timed out')` once the timer fires. */
111
+ promise: Promise<never>
112
+ /** Clears the underlying timer. Safe to call after the timer has already fired. */
113
+ cancel: () => void
114
+ /** Returns true once the timer has fired. */
115
+ didTimeOut: () => boolean
116
+ }
117
+
118
+ function createDeadline (timeoutMs: number, controller?: AbortController): Deadline {
119
+ let expired = false
120
+ let timer: ReturnType<typeof setTimeout> | null = null
121
+ const promise = new Promise<never>((_, reject) => {
122
+ timer = setTimeout(() => {
123
+ expired = true
124
+ try { controller?.abort() } catch { /* noop */ }
125
+ reject(new Error('Request timed out'))
126
+ }, timeoutMs)
127
+ })
128
+ return {
129
+ promise,
130
+ cancel: () => {
131
+ if (timer !== null) clearTimeout(timer)
132
+ },
133
+ didTimeOut: () => expired
134
+ }
135
+ }
136
+
137
+ function normalizeLookupError (err: unknown, timedOut: boolean): Error {
138
+ if (timedOut) return new Error('Request timed out')
139
+ if ((err as { name?: string })?.name === 'AbortError') return new Error('Request timed out')
140
+ if (err instanceof Error) return err
141
+ return new Error(stringifyErrorValue(err))
142
+ }
143
+
144
+ /**
145
+ * Coerce a non-Error thrown value to a human-readable string without falling
146
+ * back to the default `'[object Object]'` for plain objects.
147
+ */
148
+ function stringifyErrorValue (value: unknown): string {
149
+ if (value === null) return 'null'
150
+ if (value === undefined) return 'undefined'
151
+ if (typeof value === 'string') return value
152
+ if (typeof value === 'number') return value.toString()
153
+ if (typeof value === 'boolean') return value ? 'true' : 'false'
154
+ if (typeof value === 'bigint') return value.toString()
155
+ const message = (value as { message?: unknown }).message
156
+ if (typeof message === 'string' && message.length > 0) return message
157
+ try {
158
+ return JSON.stringify(value) ?? 'Unknown error'
159
+ } catch {
160
+ return 'Unknown error'
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Returns true when the given Content-Type header value represents
166
+ * `application/octet-stream`, ignoring case and any media-type parameters
167
+ * (e.g. `; charset=utf-8`).
168
+ */
169
+ function isOctetStream (contentType: string | null): boolean {
170
+ if (typeof contentType !== 'string') return false
171
+ const baseType = contentType.split(';', 1)[0].trim().toLowerCase()
172
+ return baseType === 'application/octet-stream'
173
+ }
174
+
108
175
  /** Internal cache options. Kept optional to preserve drop-in compatibility. */
109
176
  interface CacheOptions {
110
177
  /** How long (ms) a hosts entry is considered fresh. Default 5 minutes. */
@@ -181,36 +248,46 @@ export class HTTPSOverlayLookupFacilitator implements OverlayLookupFacilitator {
181
248
  }
182
249
 
183
250
  const controller = typeof AbortController === 'undefined' ? undefined : new AbortController()
184
- const timer = setTimeout(() => {
185
- try { controller?.abort() } catch { /* noop */ }
186
- }, timeout)
251
+ const deadline = createDeadline(timeout, controller)
187
252
 
188
- try {
189
- const fco: RequestInit = {
190
- method: 'POST',
191
- headers: {
192
- 'Content-Type': 'application/json',
193
- 'X-Aggregation': 'yes'
194
- },
195
- body: JSON.stringify({ service: question.service, query: question.query }),
196
- signal: controller?.signal
197
- }
198
- const response: Response = await this.fetchClient(`${url}/lookup`, fco)
253
+ // Hard wall-clock deadline: in some environments (e.g. browser/Electron CORS
254
+ // failures) the underlying fetch can stall without ever settling, and the
255
+ // AbortController signal alone is insufficient to make the returned promise
256
+ // resolve or reject. Race the fetch against a setTimeout-backed reject so
257
+ // the consumer-facing promise always settles within `timeout` ms.
258
+ const fetchPromise = this.performLookupRequest(url, question, controller?.signal)
259
+ // Swallow background rejection if the deadline wins first.
260
+ fetchPromise.catch(() => { /* noop */ })
199
261
 
200
- if (!response.ok) throw new Error(`Failed to facilitate lookup (HTTP ${response.status})`)
201
- if (response.headers.get('content-type') === 'application/octet-stream') {
202
- return await this.parseOctetStreamLookup(response)
203
- }
204
- return await response.json()
262
+ try {
263
+ return await Promise.race([fetchPromise, deadline.promise])
205
264
  } catch (e) {
206
- // Normalize timeouts to a consistent error message
207
- if ((e as { name?: string })?.name === 'AbortError') {
208
- throw new Error('Request timed out')
209
- }
210
- throw e
265
+ throw normalizeLookupError(e, deadline.didTimeOut())
211
266
  } finally {
212
- clearTimeout(timer)
267
+ deadline.cancel()
268
+ }
269
+ }
270
+
271
+ private async performLookupRequest (
272
+ url: string,
273
+ question: LookupQuestion,
274
+ signal: AbortSignal | undefined
275
+ ): Promise<LookupAnswer> {
276
+ const fco: RequestInit = {
277
+ method: 'POST',
278
+ headers: {
279
+ 'Content-Type': 'application/json',
280
+ 'X-Aggregation': 'yes'
281
+ },
282
+ body: JSON.stringify({ service: question.service, query: question.query }),
283
+ signal
284
+ }
285
+ const response: Response = await this.fetchClient(`${url}/lookup`, fco)
286
+ if (!response.ok) throw new Error(`Failed to facilitate lookup (HTTP ${response.status})`)
287
+ if (isOctetStream(response.headers.get('content-type'))) {
288
+ return await this.parseOctetStreamLookup(response)
213
289
  }
290
+ return await response.json()
214
291
  }
215
292
 
216
293
  /** Parse the aggregated octet-stream lookup response into an output-list LookupAnswer. */
@@ -472,6 +472,22 @@ describe('LookupResolver – additional coverage', () => {
472
472
  ).rejects.toThrow('Request timed out')
473
473
  })
474
474
 
475
+ it('rejects within the timeout even when fetch never settles', async () => {
476
+ // Simulate the CORS-blocked / hung-preflight case where the fetch promise
477
+ // does not honor the AbortController signal and never settles.
478
+ const neverFetch = jest.fn().mockImplementation(
479
+ () => new Promise(() => { /* never resolves */ })
480
+ )
481
+ const facilitator = new HTTPSOverlayLookupFacilitator(neverFetch, true)
482
+ const start = Date.now()
483
+ await expect(
484
+ facilitator.lookup('http://host', { service: 'ls_test', query: {} }, 50)
485
+ ).rejects.toThrow('Request timed out')
486
+ const elapsed = Date.now() - start
487
+ // Allow generous slack but must complete well before any global jest timeout.
488
+ expect(elapsed).toBeLessThan(2000)
489
+ })
490
+
475
491
  it('parses octet-stream responses', async () => {
476
492
  // Build a minimal octet-stream payload: 1 outpoint, then BEEF bytes
477
493
  const tx = new Transaction(
@@ -506,6 +522,36 @@ describe('LookupResolver – additional coverage', () => {
506
522
  expect(result.outputs[0].outputIndex).toBe(0)
507
523
  })
508
524
 
525
+ it('parses octet-stream responses when header carries parameters or differing case', async () => {
526
+ const tx = new Transaction(
527
+ 1,
528
+ [],
529
+ [{ lockingScript: LockingScript.fromHex('88'), satoshis: 1 }],
530
+ 0
531
+ )
532
+ const beef = tx.toBEEF()
533
+ const txid = Buffer.from(tx.id('hex'), 'hex')
534
+ const payload = Buffer.concat([
535
+ Buffer.from([0x01]), txid, Buffer.from([0x00]), Buffer.from([0x00]), Buffer.from(beef)
536
+ ])
537
+
538
+ for (const header of [
539
+ 'application/octet-stream; charset=utf-8',
540
+ 'Application/Octet-Stream',
541
+ ' application/octet-stream '
542
+ ]) {
543
+ const mockFetch = jest.fn().mockResolvedValue({
544
+ ok: true,
545
+ headers: { get: () => header },
546
+ arrayBuffer: async () => payload.buffer.slice(payload.byteOffset, payload.byteOffset + payload.byteLength)
547
+ })
548
+ const facilitator = new HTTPSOverlayLookupFacilitator(mockFetch, true)
549
+ const result = await facilitator.lookup('https://host', { service: 'ls_test', query: {} })
550
+ expect(result.type).toBe('output-list')
551
+ expect(result.outputs).toHaveLength(1)
552
+ }
553
+ })
554
+
509
555
  it('parses octet-stream responses with context bytes', async () => {
510
556
  const tx = new Transaction(
511
557
  1,
@@ -547,6 +593,50 @@ describe('LookupResolver – additional coverage', () => {
547
593
  ).rejects.toThrow('DNS failure')
548
594
  })
549
595
 
596
+ it('normalises string thrown values from fetch', async () => {
597
+ const mockFetch = jest.fn().mockRejectedValue('boom')
598
+ const facilitator = new HTTPSOverlayLookupFacilitator(mockFetch, true)
599
+ await expect(
600
+ facilitator.lookup('https://host', { service: 'ls_test', query: {} })
601
+ ).rejects.toThrow('boom')
602
+ })
603
+
604
+ it('normalises object-with-message thrown values from fetch', async () => {
605
+ const mockFetch = jest.fn().mockRejectedValue({ message: 'object boom' })
606
+ const facilitator = new HTTPSOverlayLookupFacilitator(mockFetch, true)
607
+ await expect(
608
+ facilitator.lookup('https://host', { service: 'ls_test', query: {} })
609
+ ).rejects.toThrow('object boom')
610
+ })
611
+
612
+ it('normalises plain-object thrown values via JSON', async () => {
613
+ const mockFetch = jest.fn().mockRejectedValue({ code: 42 })
614
+ const facilitator = new HTTPSOverlayLookupFacilitator(mockFetch, true)
615
+ await expect(
616
+ facilitator.lookup('https://host', { service: 'ls_test', query: {} })
617
+ ).rejects.toThrow('{"code":42}')
618
+ })
619
+
620
+ it('normalises number/boolean/null thrown values from fetch', async () => {
621
+ for (const value of [123, true, null]) {
622
+ const mockFetch = jest.fn().mockRejectedValue(value)
623
+ const facilitator = new HTTPSOverlayLookupFacilitator(mockFetch, true)
624
+ await expect(
625
+ facilitator.lookup('https://host', { service: 'ls_test', query: {} })
626
+ ).rejects.toThrow(String(value))
627
+ }
628
+ })
629
+
630
+ it('normalises circular thrown values without crashing', async () => {
631
+ const circular: { self?: unknown } = {}
632
+ circular.self = circular
633
+ const mockFetch = jest.fn().mockRejectedValue(circular)
634
+ const facilitator = new HTTPSOverlayLookupFacilitator(mockFetch, true)
635
+ await expect(
636
+ facilitator.lookup('https://host', { service: 'ls_test', query: {} })
637
+ ).rejects.toThrow('Unknown error')
638
+ })
639
+
550
640
  it('sends correct request body to /lookup endpoint', async () => {
551
641
  const mockFetch = jest.fn().mockResolvedValue({
552
642
  ok: true,
@@ -512,7 +512,7 @@ describe('Spend', () => {
512
512
  })
513
513
  })
514
514
 
515
- it('Successfully validates a spend where sequence is set to undefined', async () => {
515
+ it('Rejects spending an immature coinbase transaction', async () => {
516
516
  const sourceTransaction = new Transaction(
517
517
  1,
518
518
  [{
@@ -531,8 +531,49 @@ describe('Spend', () => {
531
531
  )
532
532
  const txid = sourceTransaction.id('hex')
533
533
  sourceTransaction.merklePath = MerklePath.fromCoinbaseTxidAndHeight(txid, 0)
534
- const chain = new MockChain({ blockheaders: [] })
535
- chain.addBlock(txid)
534
+ const chain = new MockChain({ blockheaders: [txid] })
535
+
536
+ const spendTx = new Transaction(
537
+ 1,
538
+ [
539
+ {
540
+ unlockingScript: Script.fromASM('OP_TRUE'),
541
+ sourceTransaction,
542
+ sourceOutputIndex: 0
543
+ }
544
+ ],
545
+ [{
546
+ lockingScript: Script.fromASM('OP_NOP'),
547
+ satoshis: 1
548
+ }],
549
+ 0
550
+ )
551
+
552
+ await expect(spendTx.verify(chain)).rejects.toThrow(
553
+ `Invalid merkle path for transaction ${txid}`
554
+ )
555
+ })
556
+
557
+ it('Successfully validates a mature coinbase spend where sequence is set to undefined', async () => {
558
+ const sourceTransaction = new Transaction(
559
+ 1,
560
+ [{
561
+ sourceTXID: '0000000000000000000000000000000000000000000000000000000000000000',
562
+ sourceOutputIndex: 0,
563
+ unlockingScript: Script.fromASM('OP_TRUE'),
564
+ sequence: 0xffffffff
565
+ }],
566
+ [
567
+ {
568
+ lockingScript: Script.fromASM('OP_NOP'),
569
+ satoshis: 2
570
+ }
571
+ ],
572
+ 0
573
+ )
574
+ const txid = sourceTransaction.id('hex')
575
+ sourceTransaction.merklePath = MerklePath.fromCoinbaseTxidAndHeight(txid, 0)
576
+ const chain = new MockChain({ blockheaders: [txid, ...new Array(100).fill('')] })
536
577
 
537
578
  const spendTx = new Transaction(
538
579
  1,
@@ -551,7 +592,7 @@ describe('Spend', () => {
551
592
  )
552
593
 
553
594
  const valid = await spendTx.verify(chain)
554
-
595
+
555
596
  expect(valid).toBe(true)
556
597
 
557
598
  const b = spendTx.toBinary()
@@ -375,7 +375,7 @@ export default class MerklePath {
375
375
  if (this.indexOf(txid) === 0) {
376
376
  // Coinbase transaction outputs can only be spent once they're 100 blocks deep.
377
377
  const height = await chainTracker.currentHeight()
378
- if (this.blockHeight + 100 < height) {
378
+ if (this.blockHeight + 100 > height) {
379
379
  return false
380
380
  }
381
381
  }