@bsv/overlay-express 2.4.2 → 2.4.9

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 (135) hide show
  1. package/LICENSE.txt +57 -27
  2. package/README.md +141 -56
  3. package/dist/cjs/mod.d.ts +11 -0
  4. package/dist/cjs/mod.d.ts.map +1 -0
  5. package/dist/cjs/mod.js.map +1 -1
  6. package/dist/cjs/package.json +26 -13
  7. package/dist/cjs/src/ArcadeProvider.d.ts +37 -0
  8. package/dist/cjs/src/ArcadeProvider.d.ts.map +1 -0
  9. package/dist/cjs/src/ArcadeProvider.js.map +1 -1
  10. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts +36 -0
  11. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts.map +1 -0
  12. package/dist/cjs/src/BanAwareDiscoveryStorage.js +5 -4
  13. package/dist/cjs/src/BanAwareDiscoveryStorage.js.map +1 -1
  14. package/dist/cjs/src/BanAwareLookupWrapper.d.ts +31 -0
  15. package/dist/cjs/src/BanAwareLookupWrapper.d.ts.map +1 -0
  16. package/dist/cjs/src/BanAwareLookupWrapper.js +3 -2
  17. package/dist/cjs/src/BanAwareLookupWrapper.js.map +1 -1
  18. package/dist/cjs/src/BanAwareTopicManager.d.ts +28 -0
  19. package/dist/cjs/src/BanAwareTopicManager.d.ts.map +1 -0
  20. package/dist/cjs/src/BanAwareTopicManager.js +3 -2
  21. package/dist/cjs/src/BanAwareTopicManager.js.map +1 -1
  22. package/dist/cjs/src/BanService.d.ts +81 -0
  23. package/dist/cjs/src/BanService.d.ts.map +1 -0
  24. package/dist/cjs/src/BanService.js.map +1 -1
  25. package/dist/cjs/src/ChaintracksProvider.d.ts +29 -0
  26. package/dist/cjs/src/ChaintracksProvider.d.ts.map +1 -0
  27. package/dist/cjs/src/ChaintracksProvider.js +1 -1
  28. package/dist/cjs/src/ChaintracksProvider.js.map +1 -1
  29. package/dist/cjs/src/JanitorService.d.ts +128 -0
  30. package/dist/cjs/src/JanitorService.d.ts.map +1 -0
  31. package/dist/cjs/src/JanitorService.js +108 -37
  32. package/dist/cjs/src/JanitorService.js.map +1 -1
  33. package/dist/cjs/src/OverlayExpress.d.ts +466 -0
  34. package/dist/cjs/src/OverlayExpress.d.ts.map +1 -0
  35. package/dist/cjs/src/OverlayExpress.js +307 -148
  36. package/dist/cjs/src/OverlayExpress.js.map +1 -1
  37. package/dist/cjs/src/OverlayMonitor.d.ts +197 -0
  38. package/dist/cjs/src/OverlayMonitor.d.ts.map +1 -0
  39. package/dist/cjs/src/OverlayMonitor.js +6 -4
  40. package/dist/cjs/src/OverlayMonitor.js.map +1 -1
  41. package/dist/cjs/src/ProviderChainBroadcaster.d.ts +18 -0
  42. package/dist/cjs/src/ProviderChainBroadcaster.d.ts.map +1 -0
  43. package/dist/cjs/src/ProviderChainBroadcaster.js +7 -3
  44. package/dist/cjs/src/ProviderChainBroadcaster.js.map +1 -1
  45. package/dist/cjs/src/ReorgStream.d.ts +74 -0
  46. package/dist/cjs/src/ReorgStream.d.ts.map +1 -0
  47. package/dist/cjs/src/ReorgStream.js +14 -6
  48. package/dist/cjs/src/ReorgStream.js.map +1 -1
  49. package/dist/cjs/src/generalGuide.md.d.ts +3 -0
  50. package/dist/cjs/src/generalGuide.md.d.ts.map +1 -0
  51. package/dist/cjs/src/generalGuide.md.js.map +1 -1
  52. package/dist/cjs/src/makeUserInterface.d.ts +22 -0
  53. package/dist/cjs/src/makeUserInterface.d.ts.map +1 -0
  54. package/dist/cjs/src/makeUserInterface.js +4 -4
  55. package/dist/cjs/src/makeUserInterface.js.map +1 -1
  56. package/dist/cjs/src/security/edgePolicy.d.ts +57 -0
  57. package/dist/cjs/src/security/edgePolicy.d.ts.map +1 -0
  58. package/dist/cjs/src/security/edgePolicy.js +379 -0
  59. package/dist/cjs/src/security/edgePolicy.js.map +1 -0
  60. package/dist/esm/mod.js.map +1 -1
  61. package/dist/esm/src/ArcadeProvider.js.map +1 -1
  62. package/dist/esm/src/BanAwareDiscoveryStorage.js +5 -4
  63. package/dist/esm/src/BanAwareDiscoveryStorage.js.map +1 -1
  64. package/dist/esm/src/BanAwareLookupWrapper.js +3 -2
  65. package/dist/esm/src/BanAwareLookupWrapper.js.map +1 -1
  66. package/dist/esm/src/BanAwareTopicManager.js +3 -2
  67. package/dist/esm/src/BanAwareTopicManager.js.map +1 -1
  68. package/dist/esm/src/ChaintracksProvider.js +1 -1
  69. package/dist/esm/src/ChaintracksProvider.js.map +1 -1
  70. package/dist/esm/src/JanitorService.js +84 -8
  71. package/dist/esm/src/JanitorService.js.map +1 -1
  72. package/dist/esm/src/OverlayExpress.js +290 -134
  73. package/dist/esm/src/OverlayExpress.js.map +1 -1
  74. package/dist/esm/src/ProviderChainBroadcaster.js +7 -3
  75. package/dist/esm/src/ProviderChainBroadcaster.js.map +1 -1
  76. package/dist/esm/src/ReorgStream.js +14 -6
  77. package/dist/esm/src/ReorgStream.js.map +1 -1
  78. package/dist/esm/src/makeUserInterface.js +3 -4
  79. package/dist/esm/src/makeUserInterface.js.map +1 -1
  80. package/dist/esm/src/security/edgePolicy.js +377 -0
  81. package/dist/esm/src/security/edgePolicy.js.map +1 -0
  82. package/dist/types/mod.d.ts +1 -1
  83. package/dist/types/mod.d.ts.map +1 -1
  84. package/dist/types/src/ArcadeProvider.d.ts.map +1 -1
  85. package/dist/types/src/BanAwareDiscoveryStorage.d.ts.map +1 -1
  86. package/dist/types/src/BanAwareLookupWrapper.d.ts.map +1 -1
  87. package/dist/types/src/BanAwareTopicManager.d.ts.map +1 -1
  88. package/dist/types/src/BanService.d.ts.map +1 -1
  89. package/dist/types/src/ChaintracksProvider.d.ts.map +1 -1
  90. package/dist/types/src/JanitorService.d.ts +14 -1
  91. package/dist/types/src/JanitorService.d.ts.map +1 -1
  92. package/dist/types/src/OverlayExpress.d.ts +48 -2
  93. package/dist/types/src/OverlayExpress.d.ts.map +1 -1
  94. package/dist/types/src/OverlayMonitor.d.ts.map +1 -1
  95. package/dist/types/src/ProviderChainBroadcaster.d.ts.map +1 -1
  96. package/dist/types/src/ReorgStream.d.ts.map +1 -1
  97. package/dist/types/src/generalGuide.md.d.ts +1 -1
  98. package/dist/types/src/generalGuide.md.d.ts.map +1 -1
  99. package/dist/types/src/makeUserInterface.d.ts +1 -2
  100. package/dist/types/src/makeUserInterface.d.ts.map +1 -1
  101. package/dist/types/src/security/edgePolicy.d.ts +57 -0
  102. package/dist/types/src/security/edgePolicy.d.ts.map +1 -0
  103. package/package.json +47 -25
  104. package/dist/cjs/tsconfig.cjs.tsbuildinfo +0 -1
  105. package/dist/esm/tsconfig.esm.tsbuildinfo +0 -1
  106. package/dist/types/tsconfig.types.tsbuildinfo +0 -1
  107. package/mod.ts +0 -47
  108. package/src/ArcadeProvider.ts +0 -232
  109. package/src/BanAwareDiscoveryStorage.ts +0 -91
  110. package/src/BanAwareLookupWrapper.ts +0 -95
  111. package/src/BanAwareTopicManager.ts +0 -87
  112. package/src/BanService.ts +0 -154
  113. package/src/ChaintracksProvider.ts +0 -79
  114. package/src/JanitorService.ts +0 -423
  115. package/src/OverlayExpress.ts +0 -2479
  116. package/src/OverlayMonitor.ts +0 -817
  117. package/src/ProviderChainBroadcaster.ts +0 -108
  118. package/src/ReorgStream.ts +0 -221
  119. package/src/__tests__/BanAwareDiscoveryStorage.test.ts +0 -131
  120. package/src/__tests__/BanAwareLookupWrapper.test.ts +0 -255
  121. package/src/__tests__/BanAwareTopicManager.test.ts +0 -132
  122. package/src/__tests__/BanService.test.ts +0 -310
  123. package/src/__tests__/InMemoryMigrationSource.test.ts +0 -11
  124. package/src/__tests__/JanitorService.test.ts +0 -941
  125. package/src/__tests__/OverlayExpress.test.ts +0 -1300
  126. package/src/__tests__/OverlayMonitor.test.ts +0 -255
  127. package/src/__tests__/OverlayProviders.live.test.ts +0 -43
  128. package/src/__tests__/OverlayProviders.test.ts +0 -123
  129. package/src/__tests__/README.md +0 -105
  130. package/src/__tests__/ReorgStream.test.ts +0 -66
  131. package/src/__tests__/makeUserInterface.test.ts +0 -193
  132. package/src/__tests__/setup.ts +0 -36
  133. package/src/auth-express-middleware.d.ts +0 -16
  134. package/src/generalGuide.md.ts +0 -74
  135. package/src/makeUserInterface.ts +0 -1224
@@ -1,255 +0,0 @@
1
- import { describe, it, expect, jest, beforeEach } from '@jest/globals'
2
- import { BanAwareLookupWrapper } from '../BanAwareLookupWrapper.js'
3
- import { BanService } from '../BanService.js'
4
- import { LookupService, OutputAdmittedByTopic } from '@bsv/overlay'
5
- import { PushDrop, Utils, Script } from '@bsv/sdk'
6
-
7
- jest.mock('@bsv/sdk', () => ({
8
- PushDrop: {
9
- decode: jest.fn()
10
- },
11
- Utils: {
12
- toUTF8: jest.fn()
13
- },
14
- Script: jest.fn()
15
- }))
16
-
17
- // After jest.mock hoisting, these imports reference the mocked versions
18
- const mockPushDropDecode = PushDrop.decode as unknown as jest.Mock<any>
19
- const mockToUTF8 = Utils.toUTF8 as unknown as jest.Mock<any>
20
-
21
- describe('BanAwareLookupWrapper', () => {
22
- let wrapper: BanAwareLookupWrapper
23
- let mockWrapped: jest.Mocked<LookupService>
24
- let mockBanService: jest.Mocked<BanService>
25
- let mockLogger: any
26
-
27
- beforeEach(() => {
28
- jest.clearAllMocks()
29
-
30
- mockWrapped = {
31
- admissionMode: 'locking-script',
32
- spendNotificationMode: 'locking-script',
33
- outputAdmittedByTopic: jest.fn<any>().mockResolvedValue(undefined),
34
- outputSpent: jest.fn<any>().mockResolvedValue(undefined),
35
- outputNoLongerRetainedInHistory: jest.fn<any>().mockResolvedValue(undefined),
36
- outputEvicted: jest.fn<any>().mockResolvedValue(undefined),
37
- lookup: jest.fn<any>().mockResolvedValue({ type: 'output-list', outputs: [] }),
38
- getDocumentation: jest.fn<any>().mockResolvedValue('docs'),
39
- getMetaData: jest.fn<any>().mockResolvedValue({ name: 'test', shortDescription: 'test' })
40
- } as any
41
-
42
- mockBanService = {
43
- isOutpointBanned: jest.fn<any>().mockResolvedValue(false),
44
- isDomainBanned: jest.fn<any>().mockResolvedValue(false),
45
- banDomain: jest.fn<any>().mockResolvedValue(undefined),
46
- banOutpoint: jest.fn<any>().mockResolvedValue(undefined)
47
- } as any
48
-
49
- mockLogger = {
50
- log: jest.fn(),
51
- error: jest.fn(),
52
- warn: jest.fn()
53
- }
54
-
55
- wrapper = new BanAwareLookupWrapper(mockWrapped, mockBanService, 'SHIP', mockLogger)
56
- })
57
-
58
- describe('constructor', () => {
59
- it('should copy admissionMode and spendNotificationMode from wrapped service', () => {
60
- expect(wrapper.admissionMode).toBe('locking-script')
61
- expect(wrapper.spendNotificationMode).toBe('locking-script')
62
- })
63
- })
64
-
65
- describe('outputAdmittedByTopic', () => {
66
- it('should block a banned outpoint', async () => {
67
- mockBanService.isOutpointBanned.mockResolvedValue(true)
68
-
69
- const payload: OutputAdmittedByTopic = {
70
- mode: 'locking-script',
71
- txid: 'banned-txid',
72
- outputIndex: 0,
73
- topic: 'tm_ship',
74
- satoshis: 1,
75
- lockingScript: new Script()
76
- }
77
-
78
- await wrapper.outputAdmittedByTopic(payload)
79
-
80
- expect(mockBanService.isOutpointBanned).toHaveBeenCalledWith('banned-txid', 0)
81
- expect(mockWrapped.outputAdmittedByTopic).not.toHaveBeenCalled()
82
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('[BAN]'))
83
- })
84
-
85
- it('should block a banned domain parsed from PushDrop fields', async () => {
86
- mockBanService.isOutpointBanned.mockResolvedValue(false)
87
- mockBanService.isDomainBanned.mockResolvedValue(true)
88
-
89
- mockPushDropDecode.mockReturnValue({
90
- fields: [Buffer.from('field0'), Buffer.from('field1'), Buffer.from('https://banned.com')]
91
- })
92
- mockToUTF8.mockReturnValue('https://banned.com')
93
-
94
- const payload: OutputAdmittedByTopic = {
95
- mode: 'locking-script',
96
- txid: 'some-txid',
97
- outputIndex: 1,
98
- topic: 'tm_ship',
99
- satoshis: 1,
100
- lockingScript: new Script()
101
- }
102
-
103
- await wrapper.outputAdmittedByTopic(payload)
104
-
105
- expect(mockBanService.isDomainBanned).toHaveBeenCalledWith('https://banned.com')
106
- expect(mockWrapped.outputAdmittedByTopic).not.toHaveBeenCalled()
107
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('banned domain'))
108
- })
109
-
110
- it('should delegate to wrapped service when not banned', async () => {
111
- mockBanService.isOutpointBanned.mockResolvedValue(false)
112
- mockBanService.isDomainBanned.mockResolvedValue(false)
113
-
114
- mockPushDropDecode.mockReturnValue({
115
- fields: [Buffer.from('f0'), Buffer.from('f1'), Buffer.from('https://good.com')]
116
- })
117
- mockToUTF8.mockReturnValue('https://good.com')
118
-
119
- const payload: OutputAdmittedByTopic = {
120
- mode: 'locking-script',
121
- txid: 'good-txid',
122
- outputIndex: 0,
123
- topic: 'tm_ship',
124
- satoshis: 1,
125
- lockingScript: new Script()
126
- }
127
-
128
- await wrapper.outputAdmittedByTopic(payload)
129
-
130
- expect(mockWrapped.outputAdmittedByTopic).toHaveBeenCalledWith(payload)
131
- })
132
-
133
- it('should delegate to wrapped service when PushDrop decode fails', async () => {
134
- mockBanService.isOutpointBanned.mockResolvedValue(false)
135
-
136
- mockPushDropDecode.mockImplementation(() => { throw new Error('Invalid script') })
137
-
138
- const payload: OutputAdmittedByTopic = {
139
- mode: 'locking-script',
140
- txid: 'weird-txid',
141
- outputIndex: 0,
142
- topic: 'tm_ship',
143
- satoshis: 1,
144
- lockingScript: new Script()
145
- }
146
-
147
- await wrapper.outputAdmittedByTopic(payload)
148
-
149
- expect(mockWrapped.outputAdmittedByTopic).toHaveBeenCalledWith(payload)
150
- })
151
-
152
- it('should delegate to wrapped service when PushDrop has fewer than 3 fields', async () => {
153
- mockBanService.isOutpointBanned.mockResolvedValue(false)
154
-
155
- mockPushDropDecode.mockReturnValue({ fields: [Buffer.from('f0'), Buffer.from('f1')] })
156
-
157
- const payload: OutputAdmittedByTopic = {
158
- mode: 'locking-script',
159
- txid: 'short-txid',
160
- outputIndex: 0,
161
- topic: 'tm_ship',
162
- satoshis: 1,
163
- lockingScript: new Script()
164
- }
165
-
166
- await wrapper.outputAdmittedByTopic(payload)
167
-
168
- expect(mockBanService.isDomainBanned).not.toHaveBeenCalled()
169
- expect(mockWrapped.outputAdmittedByTopic).toHaveBeenCalledWith(payload)
170
- })
171
-
172
- it('should delegate directly when mode is not locking-script', async () => {
173
- const payload = {
174
- mode: 'previous-output',
175
- txid: 'any-txid',
176
- outputIndex: 0,
177
- topic: 'tm_ship'
178
- } as any
179
-
180
- await wrapper.outputAdmittedByTopic(payload)
181
-
182
- expect(mockBanService.isOutpointBanned).not.toHaveBeenCalled()
183
- expect(mockWrapped.outputAdmittedByTopic).toHaveBeenCalledWith(payload)
184
- })
185
- })
186
-
187
- describe('outputSpent', () => {
188
- it('should delegate to wrapped service', async () => {
189
- const payload = { txid: 'spent-txid', outputIndex: 0, topic: 'tm_ship' } as any
190
-
191
- await wrapper.outputSpent(payload)
192
-
193
- expect(mockWrapped.outputSpent).toHaveBeenCalledWith(payload)
194
- })
195
-
196
- it('should handle wrapped service without outputSpent', async () => {
197
- const wrappedWithout = { ...mockWrapped, outputSpent: undefined } as any
198
- const w = new BanAwareLookupWrapper(wrappedWithout, mockBanService, 'SHIP', mockLogger)
199
-
200
- // Should not throw
201
- await w.outputSpent({ txid: 't', outputIndex: 0, topic: 'x' } as any)
202
- })
203
- })
204
-
205
- describe('outputNoLongerRetainedInHistory', () => {
206
- it('should delegate to wrapped service', async () => {
207
- await wrapper.outputNoLongerRetainedInHistory('txid', 0, 'topic')
208
-
209
- expect(mockWrapped.outputNoLongerRetainedInHistory).toHaveBeenCalledWith('txid', 0, 'topic')
210
- })
211
-
212
- it('should handle wrapped service without the method', async () => {
213
- const wrappedWithout = { ...mockWrapped, outputNoLongerRetainedInHistory: undefined } as any
214
- const w = new BanAwareLookupWrapper(wrappedWithout, mockBanService, 'SLAP', mockLogger)
215
-
216
- await w.outputNoLongerRetainedInHistory('txid', 0, 'topic')
217
- })
218
- })
219
-
220
- describe('outputEvicted', () => {
221
- it('should delegate to wrapped service', async () => {
222
- await wrapper.outputEvicted('txid', 0)
223
-
224
- expect(mockWrapped.outputEvicted).toHaveBeenCalledWith('txid', 0)
225
- })
226
- })
227
-
228
- describe('lookup', () => {
229
- it('should delegate to wrapped service', async () => {
230
- const question = { service: 'ls_ship', query: {} }
231
-
232
- await wrapper.lookup(question as any)
233
-
234
- expect(mockWrapped.lookup).toHaveBeenCalledWith(question)
235
- })
236
- })
237
-
238
- describe('getDocumentation', () => {
239
- it('should delegate to wrapped service', async () => {
240
- const result = await wrapper.getDocumentation()
241
-
242
- expect(result).toBe('docs')
243
- expect(mockWrapped.getDocumentation).toHaveBeenCalled()
244
- })
245
- })
246
-
247
- describe('getMetaData', () => {
248
- it('should delegate to wrapped service', async () => {
249
- const result = await wrapper.getMetaData()
250
-
251
- expect(result).toEqual({ name: 'test', shortDescription: 'test' })
252
- expect(mockWrapped.getMetaData).toHaveBeenCalled()
253
- })
254
- })
255
- })
@@ -1,132 +0,0 @@
1
- import { describe, it, expect, jest, beforeEach } from '@jest/globals'
2
- import { TopicManager } from '@bsv/overlay'
3
- import { PushDrop, Transaction, Utils } from '@bsv/sdk'
4
- import { BanAwareTopicManager } from '../BanAwareTopicManager.js'
5
- import { BanService } from '../BanService.js'
6
-
7
- jest.mock('@bsv/sdk', () => ({
8
- PushDrop: {
9
- decode: jest.fn()
10
- },
11
- Transaction: {
12
- fromBEEF: jest.fn()
13
- },
14
- Utils: {
15
- toUTF8: jest.fn()
16
- }
17
- }))
18
-
19
- const mockPushDropDecode = PushDrop.decode as unknown as jest.Mock<any>
20
- const mockTransactionFromBEEF = Transaction.fromBEEF as unknown as jest.Mock<any>
21
- const mockToUTF8 = Utils.toUTF8 as unknown as jest.Mock<any>
22
-
23
- describe('BanAwareTopicManager', () => {
24
- let wrapper: BanAwareTopicManager
25
- let mockWrapped: jest.Mocked<TopicManager>
26
- let mockBanService: jest.Mocked<BanService>
27
- let mockLogger: any
28
-
29
- beforeEach(() => {
30
- jest.clearAllMocks()
31
-
32
- mockWrapped = {
33
- identifyAdmissibleOutputs: jest.fn<any>().mockResolvedValue({
34
- outputsToAdmit: [0, 1],
35
- coinsToRetain: [0],
36
- coinsRemoved: [1]
37
- }),
38
- identifyNeededInputs: jest.fn<any>().mockResolvedValue([{ txid: 'needed-txid', outputIndex: 0 }]),
39
- getDocumentation: jest.fn<any>().mockResolvedValue('docs'),
40
- getMetaData: jest.fn<any>().mockResolvedValue({ name: 'test', shortDescription: 'test' })
41
- } as any
42
-
43
- mockBanService = {
44
- isOutpointBanned: jest.fn<any>().mockResolvedValue(false),
45
- isDomainBanned: jest.fn<any>().mockResolvedValue(false)
46
- } as any
47
-
48
- mockLogger = {
49
- log: jest.fn()
50
- }
51
-
52
- mockTransactionFromBEEF.mockReturnValue({
53
- id: jest.fn().mockReturnValue('ad-txid'),
54
- outputs: [
55
- { lockingScript: 'script-0' },
56
- { lockingScript: 'script-1' }
57
- ]
58
- })
59
- mockPushDropDecode.mockReturnValue({
60
- fields: [Buffer.from('SHIP'), Buffer.from('topic'), Buffer.from('https://good.example')]
61
- })
62
- mockToUTF8.mockImplementation((value: Buffer) => value.toString('utf8'))
63
-
64
- wrapper = new BanAwareTopicManager(mockWrapped, mockBanService, 'SHIP', mockLogger)
65
- })
66
-
67
- it('should remove banned outpoints from admittance instructions', async () => {
68
- mockBanService.isOutpointBanned.mockResolvedValueOnce(true).mockResolvedValue(false)
69
-
70
- const result = await wrapper.identifyAdmissibleOutputs([1, 2, 3], [0], [42], 'current-tx')
71
-
72
- expect(result.outputsToAdmit).toEqual([1])
73
- expect(mockWrapped.identifyAdmissibleOutputs).toHaveBeenCalledWith([1, 2, 3], [0], [42], 'current-tx')
74
- expect(mockBanService.isOutpointBanned).toHaveBeenCalledWith('ad-txid', 0)
75
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('banned outpoint'))
76
- })
77
-
78
- it('should remove outputs whose advertised domain is banned', async () => {
79
- mockBanService.isDomainBanned.mockResolvedValueOnce(true).mockResolvedValue(false)
80
-
81
- const result = await wrapper.identifyAdmissibleOutputs([1], [])
82
-
83
- expect(result.outputsToAdmit).toEqual([1])
84
- expect(mockBanService.isDomainBanned).toHaveBeenCalledWith('https://good.example')
85
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('banned domain'))
86
- })
87
-
88
- it('should preserve all outputs when nothing is banned', async () => {
89
- const result = await wrapper.identifyAdmissibleOutputs([1], [])
90
-
91
- expect(result.outputsToAdmit).toEqual([0, 1])
92
- })
93
-
94
- it('should preserve wrapped instructions when BEEF parsing fails', async () => {
95
- mockTransactionFromBEEF.mockImplementation(() => { throw new Error('bad beef') })
96
-
97
- const result = await wrapper.identifyAdmissibleOutputs([1], [])
98
-
99
- expect(result.outputsToAdmit).toEqual([0, 1])
100
- expect(mockBanService.isOutpointBanned).not.toHaveBeenCalled()
101
- })
102
-
103
- it('should preserve wrapped instructions for non-standard outputs', async () => {
104
- mockPushDropDecode.mockImplementation(() => { throw new Error('bad script') })
105
-
106
- const result = await wrapper.identifyAdmissibleOutputs([1], [])
107
-
108
- expect(result.outputsToAdmit).toEqual([0, 1])
109
- expect(mockBanService.isDomainBanned).not.toHaveBeenCalled()
110
- })
111
-
112
- it('should delegate identifyNeededInputs', async () => {
113
- await expect(wrapper.identifyNeededInputs([1], [2])).resolves.toEqual([{ txid: 'needed-txid', outputIndex: 0 }])
114
- expect(mockWrapped.identifyNeededInputs).toHaveBeenCalledWith([1], [2])
115
- })
116
-
117
- it('should return no needed inputs when wrapped manager does not implement it', async () => {
118
- const manager = new BanAwareTopicManager(
119
- { ...mockWrapped, identifyNeededInputs: undefined },
120
- mockBanService,
121
- 'SLAP',
122
- mockLogger
123
- )
124
-
125
- await expect(manager.identifyNeededInputs([1])).resolves.toEqual([])
126
- })
127
-
128
- it('should delegate documentation and metadata', async () => {
129
- await expect(wrapper.getDocumentation()).resolves.toBe('docs')
130
- await expect(wrapper.getMetaData()).resolves.toEqual({ name: 'test', shortDescription: 'test' })
131
- })
132
- })
@@ -1,310 +0,0 @@
1
- import { describe, it, expect, jest, beforeEach } from '@jest/globals'
2
- import { BanService } from '../BanService.js'
3
- import { Db } from 'mongodb'
4
-
5
- describe('BanService', () => {
6
- let banService: BanService
7
- let mockCollection: any
8
- let mockDb: Db
9
-
10
- beforeEach(() => {
11
- jest.clearAllMocks()
12
-
13
- mockCollection = {
14
- createIndex: jest.fn<any>().mockResolvedValue(undefined),
15
- updateOne: jest.fn<any>().mockResolvedValue({}),
16
- deleteOne: jest.fn<any>().mockResolvedValue({}),
17
- findOne: jest.fn<any>().mockResolvedValue(null),
18
- find: jest.fn<any>().mockReturnValue({
19
- sort: jest.fn<any>().mockReturnValue({
20
- toArray: jest.fn<any>().mockResolvedValue([])
21
- })
22
- }),
23
- countDocuments: jest.fn<any>().mockResolvedValue(0)
24
- }
25
-
26
- mockDb = {
27
- collection: jest.fn().mockReturnValue(mockCollection)
28
- } as unknown as Db
29
-
30
- banService = new BanService(mockDb)
31
- })
32
-
33
- describe('constructor', () => {
34
- it('should initialize with the bannedRecords collection', () => {
35
- expect(mockDb.collection).toHaveBeenCalledWith('bannedRecords')
36
- })
37
- })
38
-
39
- describe('ensureIndexes', () => {
40
- it('should create unique compound index on type+value and index on bannedAt', async () => {
41
- await banService.ensureIndexes()
42
-
43
- expect(mockCollection.createIndex).toHaveBeenCalledTimes(2)
44
- expect(mockCollection.createIndex).toHaveBeenCalledWith(
45
- { type: 1, value: 1 },
46
- { unique: true }
47
- )
48
- expect(mockCollection.createIndex).toHaveBeenCalledWith({ bannedAt: -1 })
49
- })
50
- })
51
-
52
- describe('banDomain', () => {
53
- it('should upsert a domain ban with default reason', async () => {
54
- await banService.banDomain('https://bad-host.com')
55
-
56
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
57
- { type: 'domain', value: 'https://bad-host.com' },
58
- {
59
- $set: expect.objectContaining({
60
- type: 'domain',
61
- value: 'https://bad-host.com',
62
- reason: 'Manually banned',
63
- bannedBy: undefined
64
- })
65
- },
66
- { upsert: true }
67
- )
68
- })
69
-
70
- it('should use custom reason and bannedBy', async () => {
71
- await banService.banDomain('https://spam.com', 'Spam host', 'admin-key-123')
72
-
73
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
74
- { type: 'domain', value: 'https://spam.com' },
75
- {
76
- $set: expect.objectContaining({
77
- reason: 'Spam host',
78
- bannedBy: 'admin-key-123'
79
- })
80
- },
81
- { upsert: true }
82
- )
83
- })
84
-
85
- it('should reject non-string domain (NoSQL injection prevention)', async () => {
86
- await expect(banService.banDomain({ $ne: '' } as any)).rejects.toThrow('Invalid input: expected a string value')
87
- })
88
- })
89
-
90
- describe('unbanDomain', () => {
91
- it('should delete a domain ban', async () => {
92
- await banService.unbanDomain('https://unbanned.com')
93
-
94
- expect(mockCollection.deleteOne).toHaveBeenCalledWith({
95
- type: 'domain',
96
- value: 'https://unbanned.com'
97
- })
98
- })
99
-
100
- it('should reject non-string domain', async () => {
101
- await expect(banService.unbanDomain({ $ne: '' } as any)).rejects.toThrow('Invalid input: expected a string value')
102
- })
103
- })
104
-
105
- describe('isDomainBanned', () => {
106
- it('should return true when domain is banned', async () => {
107
- mockCollection.findOne.mockResolvedValue({ type: 'domain', value: 'https://banned.com' })
108
-
109
- const result = await banService.isDomainBanned('https://banned.com')
110
-
111
- expect(result).toBe(true)
112
- expect(mockCollection.findOne).toHaveBeenCalledWith({
113
- type: 'domain',
114
- value: 'https://banned.com'
115
- })
116
- })
117
-
118
- it('should return false when domain is not banned', async () => {
119
- mockCollection.findOne.mockResolvedValue(null)
120
-
121
- const result = await banService.isDomainBanned('https://good-host.com')
122
-
123
- expect(result).toBe(false)
124
- })
125
-
126
- it('should reject non-string domain', async () => {
127
- await expect(banService.isDomainBanned(123 as any)).rejects.toThrow('Invalid input: expected a string value')
128
- })
129
- })
130
-
131
- describe('banOutpoint', () => {
132
- it('should upsert an outpoint ban with formatted value', async () => {
133
- await banService.banOutpoint('abc123', 0)
134
-
135
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
136
- { type: 'outpoint', value: 'abc123.0' },
137
- {
138
- $set: expect.objectContaining({
139
- type: 'outpoint',
140
- value: 'abc123.0',
141
- domain: undefined,
142
- reason: 'Manually banned'
143
- })
144
- },
145
- { upsert: true }
146
- )
147
- })
148
-
149
- it('should include domain, reason, and bannedBy when provided', async () => {
150
- await banService.banOutpoint('txid456', 2, 'Stale token', 'https://host.com', 'admin-key')
151
-
152
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
153
- { type: 'outpoint', value: 'txid456.2' },
154
- {
155
- $set: expect.objectContaining({
156
- value: 'txid456.2',
157
- domain: 'https://host.com',
158
- reason: 'Stale token',
159
- bannedBy: 'admin-key'
160
- })
161
- },
162
- { upsert: true }
163
- )
164
- })
165
-
166
- it('should reject non-string txid', async () => {
167
- await expect(banService.banOutpoint({ $ne: '' } as any, 0)).rejects.toThrow('Invalid input: expected a string value')
168
- })
169
- })
170
-
171
- describe('unbanOutpoint', () => {
172
- it('should delete an outpoint ban', async () => {
173
- await banService.unbanOutpoint('txid789', 1)
174
-
175
- expect(mockCollection.deleteOne).toHaveBeenCalledWith({
176
- type: 'outpoint',
177
- value: 'txid789.1'
178
- })
179
- })
180
-
181
- it('should reject non-string txid', async () => {
182
- await expect(banService.unbanOutpoint(42 as any, 0)).rejects.toThrow('Invalid input: expected a string value')
183
- })
184
- })
185
-
186
- describe('isOutpointBanned', () => {
187
- it('should return true when outpoint is banned', async () => {
188
- mockCollection.findOne.mockResolvedValue({ type: 'outpoint', value: 'abc.0' })
189
-
190
- const result = await banService.isOutpointBanned('abc', 0)
191
-
192
- expect(result).toBe(true)
193
- expect(mockCollection.findOne).toHaveBeenCalledWith({
194
- type: 'outpoint',
195
- value: 'abc.0'
196
- })
197
- })
198
-
199
- it('should return false when outpoint is not banned', async () => {
200
- mockCollection.findOne.mockResolvedValue(null)
201
-
202
- const result = await banService.isOutpointBanned('xyz', 5)
203
-
204
- expect(result).toBe(false)
205
- })
206
-
207
- it('should reject non-string txid', async () => {
208
- await expect(banService.isOutpointBanned(null as any, 0)).rejects.toThrow('Invalid input: expected a string value')
209
- })
210
- })
211
-
212
- describe('listBans', () => {
213
- it('should list all bans when no type filter is provided', async () => {
214
- const mockBans = [
215
- { type: 'domain', value: 'https://a.com', bannedAt: new Date() },
216
- { type: 'outpoint', value: 'tx.0', bannedAt: new Date() }
217
- ]
218
- mockCollection.find.mockReturnValue({
219
- sort: jest.fn<any>().mockReturnValue({
220
- toArray: jest.fn<any>().mockResolvedValue(mockBans)
221
- })
222
- })
223
-
224
- const result = await banService.listBans()
225
-
226
- expect(result).toEqual(mockBans)
227
- expect(mockCollection.find).toHaveBeenCalledWith({})
228
- })
229
-
230
- it('should filter by type when provided', async () => {
231
- mockCollection.find.mockReturnValue({
232
- sort: jest.fn<any>().mockReturnValue({
233
- toArray: jest.fn<any>().mockResolvedValue([])
234
- })
235
- })
236
-
237
- await banService.listBans('domain')
238
-
239
- expect(mockCollection.find).toHaveBeenCalledWith({ type: 'domain' })
240
- })
241
-
242
- it('should filter by outpoint type', async () => {
243
- mockCollection.find.mockReturnValue({
244
- sort: jest.fn<any>().mockReturnValue({
245
- toArray: jest.fn<any>().mockResolvedValue([])
246
- })
247
- })
248
-
249
- await banService.listBans('outpoint')
250
-
251
- expect(mockCollection.find).toHaveBeenCalledWith({ type: 'outpoint' })
252
- })
253
- })
254
-
255
- describe('removeBan', () => {
256
- it('should delete a ban by type and value', async () => {
257
- await banService.removeBan('domain', 'https://remove-me.com')
258
-
259
- expect(mockCollection.deleteOne).toHaveBeenCalledWith({
260
- type: 'domain',
261
- value: 'https://remove-me.com'
262
- })
263
- })
264
-
265
- it('should delete an outpoint ban', async () => {
266
- await banService.removeBan('outpoint', 'txid.0')
267
-
268
- expect(mockCollection.deleteOne).toHaveBeenCalledWith({
269
- type: 'outpoint',
270
- value: 'txid.0'
271
- })
272
- })
273
-
274
- it('should reject non-string type (NoSQL injection prevention)', async () => {
275
- await expect(banService.removeBan({ $ne: '' } as any, 'value')).rejects.toThrow('Invalid input: expected a string value')
276
- })
277
-
278
- it('should reject non-string value (NoSQL injection prevention)', async () => {
279
- await expect(banService.removeBan('domain', { $ne: '' } as any)).rejects.toThrow('Invalid input: expected a string value')
280
- })
281
- })
282
-
283
- describe('getStats', () => {
284
- it('should return ban counts', async () => {
285
- mockCollection.countDocuments
286
- .mockResolvedValueOnce(5) // domain count
287
- .mockResolvedValueOnce(3) // outpoint count
288
-
289
- const stats = await banService.getStats()
290
-
291
- expect(stats).toEqual({
292
- domainBans: 5,
293
- outpointBans: 3,
294
- totalBans: 8
295
- })
296
- expect(mockCollection.countDocuments).toHaveBeenCalledWith({ type: 'domain' })
297
- expect(mockCollection.countDocuments).toHaveBeenCalledWith({ type: 'outpoint' })
298
- })
299
-
300
- it('should return zero counts when no bans exist', async () => {
301
- const stats = await banService.getStats()
302
-
303
- expect(stats).toEqual({
304
- domainBans: 0,
305
- outpointBans: 0,
306
- totalBans: 0
307
- })
308
- })
309
- })
310
- })