11import { BTMSStorageManager } from './BTMSStorageManager.js'
2- import { AdmissionMode , LookupFormula , LookupQuestion , LookupService , OutputAdmittedByTopic , OutputSpent , SpendNotificationMode } from '@bsv/overlay'
2+ import {
3+ AdmissionMode ,
4+ LookupFormula ,
5+ LookupQuestion ,
6+ LookupService ,
7+ OutputAdmittedByTopic ,
8+ OutputSpent ,
9+ SpendNotificationMode
10+ } from '@bsv/overlay'
311import { LockingScript , PushDrop , Transaction , Utils } from '@bsv/sdk'
412import { Db } from 'mongodb'
513import { btmsProtocol , BTMSLookupResult , BTMSQuery , BTMSRecord } from './types.js'
@@ -16,9 +24,9 @@ class BTMSLookupService implements LookupService {
1624 private static readonly TOPIC = 'tm_btms'
1725 private static readonly SERVICE_ID = 'ls_btms'
1826
19- constructor ( public storageManager : BTMSStorageManager ) { }
27+ constructor ( public storageManager : BTMSStorageManager ) { }
2028
21- private isLikelySignatureField ( field : number [ ] ) : boolean {
29+ private isLikelySignatureField ( field : number [ ] ) : boolean {
2230 if ( field . length < 40 ) {
2331 return false
2432 }
@@ -42,7 +50,11 @@ class BTMSLookupService implements LookupService {
4250 return printable / Math . max ( asText . length , 1 ) < 0.8
4351 }
4452
45- private decodeAdmittedToken ( lockingScript : LockingScript , txid : string , outputIndex : number ) : {
53+ private decodeAdmittedToken (
54+ lockingScript : LockingScript ,
55+ txid : string ,
56+ outputIndex : number
57+ ) : {
4658 assetId : string
4759 amount : number
4860 metadata ?: string
@@ -72,9 +84,7 @@ class BTMSLookupService implements LookupService {
7284 metadata = Utils . toUTF8 ( decoded . fields [ btmsProtocol . metadata ] )
7385 }
7486
75- const assetId = assetIdField === 'ISSUE'
76- ? `${ txid } .${ outputIndex } `
77- : assetIdField
87+ const assetId = assetIdField === 'ISSUE' ? `${ txid } .${ outputIndex } ` : assetIdField
7888
7989 return {
8090 assetId,
@@ -84,7 +94,7 @@ class BTMSLookupService implements LookupService {
8494 }
8595 }
8696
87- async outputAdmittedByTopic ( payload : OutputAdmittedByTopic ) : Promise < void > {
97+ async outputAdmittedByTopic ( payload : OutputAdmittedByTopic ) : Promise < void > {
8898 if ( payload . mode !== 'locking-script' ) {
8999 throw new Error ( 'Invalid payload mode' )
90100 }
@@ -95,35 +105,32 @@ class BTMSLookupService implements LookupService {
95105 }
96106
97107 try {
98- const { assetId, amount, metadata, ownerKey } = this . decodeAdmittedToken ( lockingScript , txid , outputIndex )
99-
100- await this . storageManager . storeRecord (
108+ const { assetId, amount, metadata, ownerKey } = this . decodeAdmittedToken (
109+ lockingScript ,
101110 txid ,
102- outputIndex ,
103- assetId ,
104- amount ,
105- ownerKey ,
106- metadata
111+ outputIndex
107112 )
113+
114+ await this . storageManager . storeRecord ( txid , outputIndex , assetId , amount , ownerKey , metadata )
108115 } catch ( error ) {
109116 console . error ( 'Error processing BTMS output:' , error )
110117 throw error
111118 }
112119 }
113120
114- async outputSpent ( payload : OutputSpent ) : Promise < void > {
121+ async outputSpent ( payload : OutputSpent ) : Promise < void > {
115122 if ( payload . mode !== 'none' ) throw new Error ( 'Invalid payload mode' )
116123 const { topic, txid, outputIndex } = payload
117124 if ( topic !== BTMSLookupService . TOPIC ) return
118125
119126 await this . storageManager . deleteRecord ( txid , outputIndex )
120127 }
121128
122- async outputEvicted ( txid : string , outputIndex : number ) : Promise < void > {
129+ async outputEvicted ( txid : string , outputIndex : number ) : Promise < void > {
123130 await this . storageManager . deleteRecord ( txid , outputIndex )
124131 }
125132
126- async lookup ( question : LookupQuestion ) : Promise < LookupFormula > {
133+ async lookup ( question : LookupQuestion ) : Promise < LookupFormula > {
127134 if ( question . query === undefined || question . query === null ) {
128135 throw new Error ( 'A valid query must be provided' )
129136 }
@@ -149,11 +156,7 @@ class BTMSLookupService implements LookupService {
149156 query . sortOrder
150157 )
151158 } else {
152- results = await this . storageManager . findAllRecords (
153- query . limit ,
154- query . skip ,
155- query . sortOrder
156- )
159+ results = await this . storageManager . findAllRecords ( query . limit , query . skip , query . sortOrder )
157160 }
158161
159162 const lookupResults : BTMSLookupResult [ ] = [ ]
@@ -163,9 +166,9 @@ class BTMSLookupService implements LookupService {
163166 txid : result . txid ,
164167 outputIndex : result . outputIndex ,
165168 history : query . history
166- ? async ( beef : number [ ] , outputIndex : number , currentDepth : number ) => {
167- return await this . historySelector ( beef , outputIndex , result . assetId )
168- }
169+ ? async ( beef : number [ ] , outputIndex : number , _currentDepth : number ) => {
170+ return await this . historySelector ( beef , outputIndex , result . assetId )
171+ }
169172 : undefined
170173 } )
171174 }
@@ -176,7 +179,11 @@ class BTMSLookupService implements LookupService {
176179 /**
177180 * History selector for determining which outputs to include in chain tracking
178181 */
179- private async historySelector ( beef : number [ ] , outputIndex : number , assetId ?: string ) : Promise < boolean > {
182+ private async historySelector (
183+ beef : number [ ] ,
184+ outputIndex : number ,
185+ assetId ?: string
186+ ) : Promise < boolean > {
180187 try {
181188 const tx = Transaction . fromBEEF ( beef )
182189 const output = tx . outputs [ outputIndex ]
@@ -192,17 +199,17 @@ class BTMSLookupService implements LookupService {
192199 }
193200
194201 return true
195- } catch ( _e ) {
202+ } catch {
196203 // Malformed BEEF or script — output is not a valid BTMS token; exclude from history
197204 return false
198205 }
199206 }
200207
201- async getDocumentation ( ) : Promise < string > {
208+ async getDocumentation ( ) : Promise < string > {
202209 return docs
203210 }
204211
205- async getMetaData ( ) : Promise < {
212+ async getMetaData ( ) : Promise < {
206213 name : string
207214 shortDescription : string
208215 iconURL ?: string
@@ -217,7 +224,7 @@ class BTMSLookupService implements LookupService {
217224}
218225
219226// Factory function
220- function createBTMSLookupService ( db : Db ) : BTMSLookupService {
227+ function createBTMSLookupService ( db : Db ) : BTMSLookupService {
221228 return new BTMSLookupService ( new BTMSStorageManager ( db ) )
222229}
223230export default createBTMSLookupService
0 commit comments