From 02b7d42b68f6bec8eaa404615062e8b014055484 Mon Sep 17 00:00:00 2001 From: Stephen-Thomson Date: Thu, 6 Feb 2025 16:49:33 -0800 Subject: [PATCH] SHIPBroadcaster.ts fixed --- src/messages/EncryptedMessage.ts | 2 +- src/messages/SignedMessage.ts | 2 +- src/overlay-tools/SHIPBroadcaster.ts | 48 +++++++++---------- .../__tests/SHIPBroadcaster.test.ts | 2 +- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/messages/EncryptedMessage.ts b/src/messages/EncryptedMessage.ts index fd89102f..ddbfbe42 100644 --- a/src/messages/EncryptedMessage.ts +++ b/src/messages/EncryptedMessage.ts @@ -72,4 +72,4 @@ export const decrypt = (message: number[], recipient: PrivateKey): number[] => { const sharedSecret = signingPriv.deriveSharedSecret(recipientPub) const symmetricKey = new SymmetricKey(sharedSecret.encode(true).slice(1)) return symmetricKey.decrypt(encrypted) as number[] -} \ No newline at end of file +} diff --git a/src/messages/SignedMessage.ts b/src/messages/SignedMessage.ts index a26d685a..59c0c2b4 100644 --- a/src/messages/SignedMessage.ts +++ b/src/messages/SignedMessage.ts @@ -90,4 +90,4 @@ export const verify = ( const signingKey = signer.deriveChild(recipient, invoiceNumber) const verified = signingKey.verify(message, signature) return verified -} \ No newline at end of file +} diff --git a/src/overlay-tools/SHIPBroadcaster.ts b/src/overlay-tools/SHIPBroadcaster.ts index 0bed36b3..991e2821 100644 --- a/src/overlay-tools/SHIPBroadcaster.ts +++ b/src/overlay-tools/SHIPBroadcaster.ts @@ -61,8 +61,8 @@ export interface SHIPBroadcasterConfig { requireAcknowledgmentFromAnyHostForTopics?: 'all' | 'any' | string[] /** Determines a mapping whose keys are specific hosts and whose values are the topics (all, any, or a specific list) that must be present within the STEAK received by the given hosts, in order for the broadcast to be considered a success. */ requireAcknowledgmentFromSpecificHostsForTopics?: Record< - string, - 'all' | 'any' | string[] + string, + 'all' | 'any' | string[] > } @@ -74,14 +74,14 @@ export interface OverlayBroadcastFacilitator { const MAX_SHIP_QUERY_TIMEOUT = 1000 export class HTTPSOverlayBroadcastFacilitator - implements OverlayBroadcastFacilitator { +implements OverlayBroadcastFacilitator { httpClient: typeof fetch - constructor(httpClient = fetch) { + constructor (httpClient = fetch) { this.httpClient = httpClient } - async send(url: string, taggedBEEF: TaggedBEEF): Promise { + async send (url: string, taggedBEEF: TaggedBEEF): Promise { if (!url.startsWith('https:')) { throw new Error( 'HTTPS facilitator can only use URLs that start with "https:"' @@ -111,18 +111,18 @@ export default class SHIPCast implements Broadcaster { private readonly facilitator: OverlayBroadcastFacilitator private readonly resolver: LookupResolver private readonly requireAcknowledgmentFromAllHostsForTopics: - | 'all' - | 'any' - | string[] + | 'all' + | 'any' + | string[] private readonly requireAcknowledgmentFromAnyHostForTopics: - | 'all' - | 'any' - | string[] + | 'all' + | 'any' + | string[] private readonly requireAcknowledgmentFromSpecificHostsForTopics: Record< - string, - 'all' | 'any' | string[] + string, + 'all' | 'any' | string[] > /** @@ -131,7 +131,7 @@ export default class SHIPCast implements Broadcaster { * @param {string[]} topics - The list of SHIP topic names where transactions are to be sent. * @param {SHIPBroadcasterConfig} config - Configuration options for the SHIP broadcaster. */ - constructor(topics: string[], config?: SHIPBroadcasterConfig) { + constructor (topics: string[], config?: SHIPBroadcasterConfig) { if (topics.length === 0) { throw new Error('At least one topic is required for broadcast.') } @@ -145,7 +145,7 @@ export default class SHIPCast implements Broadcaster { requireAcknowledgmentFromAllHostsForTopics, requireAcknowledgmentFromAnyHostForTopics, requireAcknowledgmentFromSpecificHostsForTopics - } = config ?? ({} as SHIPBroadcasterConfig) + } = config ?? defaultConfig this.facilitator = facilitator ?? new HTTPSOverlayBroadcastFacilitator() this.resolver = resolver ?? new LookupResolver() this.requireAcknowledgmentFromAllHostsForTopics = @@ -162,7 +162,7 @@ export default class SHIPCast implements Broadcaster { * @param {Transaction} tx - The transaction to be sent. * @returns {Promise} A promise that resolves to either a success or failure response. */ - async broadcast( + async broadcast ( tx: Transaction ): Promise { let beef: number[] @@ -188,7 +188,7 @@ export default class SHIPCast implements Broadcaster { beef, topics: [...topics] }) - if (!steak || Object.keys(steak).length === 0) { + if (steak == null || Object.keys(steak).length === 0) { throw new Error('Steak has no topics.') } return { host, success: true, steak } @@ -334,7 +334,7 @@ export default class SHIPCast implements Broadcaster { } } - private checkAcknowledgmentFromAllHosts( + private checkAcknowledgmentFromAllHosts ( hostAcknowledgments: Record>, requiredTopics: string[], require: 'all' | 'any' @@ -362,7 +362,7 @@ export default class SHIPCast implements Broadcaster { return true } - private checkAcknowledgmentFromAnyHost( + private checkAcknowledgmentFromAnyHost ( hostAcknowledgments: Record>, requiredTopics: string[], require: 'all' | 'any' @@ -395,13 +395,13 @@ export default class SHIPCast implements Broadcaster { } } - private checkAcknowledgmentFromSpecificHosts( + private checkAcknowledgmentFromSpecificHosts ( hostAcknowledgments: Record>, requirements: Record ): boolean { for (const [host, requiredTopicsOrAllAny] of Object.entries(requirements)) { const acknowledgedTopics = hostAcknowledgments[host] - if (!acknowledgedTopics) { + if (acknowledgedTopics == null) { // Host did not respond successfully return false } @@ -447,7 +447,7 @@ export default class SHIPCast implements Broadcaster { * * @returns A mapping of URLs for hosts interested in this transaction. Keys are URLs, values are which of our topics the specific host cares about. */ - private async findInterestedHosts(): Promise>> { + private async findInterestedHosts (): Promise>> { // TODO: cache the list of interested hosts to avoid spamming SHIP trackers. // TODO: Monetize the operation of the SHIP tracker system. // TODO: Cache ship/slap lookup with expiry (every 5min) @@ -478,7 +478,7 @@ export default class SHIPCast implements Broadcaster { // This should make us think a LOT less highly of this SHIP tracker if it ever happens... continue } - if (!results[parsed.domain]) { + if (results[parsed.domain] === undefined) { results[parsed.domain] = new Set() } results[parsed.domain].add(parsed.topicOrService) @@ -488,4 +488,4 @@ export default class SHIPCast implements Broadcaster { } return results } -} \ No newline at end of file +} diff --git a/src/overlay-tools/__tests/SHIPBroadcaster.test.ts b/src/overlay-tools/__tests/SHIPBroadcaster.test.ts index 58ef1b05..de923ada 100644 --- a/src/overlay-tools/__tests/SHIPBroadcaster.test.ts +++ b/src/overlay-tools/__tests/SHIPBroadcaster.test.ts @@ -1300,4 +1300,4 @@ describe('SHIPCast', () => { }) }) }) -}) \ No newline at end of file +})