Skip to content

Commit

Permalink
SHIPBroadcaster.ts fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen-Thomson committed Feb 7, 2025
1 parent 139fe5d commit 02b7d42
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/messages/EncryptedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
}
}
2 changes: 1 addition & 1 deletion src/messages/SignedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ export const verify = (
const signingKey = signer.deriveChild(recipient, invoiceNumber)
const verified = signingKey.verify(message, signature)
return verified
}
}
48 changes: 24 additions & 24 deletions src/overlay-tools/SHIPBroadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
>
}

Expand All @@ -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<STEAK> {
async send (url: string, taggedBEEF: TaggedBEEF): Promise<STEAK> {
if (!url.startsWith('https:')) {
throw new Error(
'HTTPS facilitator can only use URLs that start with "https:"'
Expand Down Expand Up @@ -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[]
>

/**
Expand All @@ -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.')
}
Expand All @@ -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 =
Expand All @@ -162,7 +162,7 @@ export default class SHIPCast implements Broadcaster {
* @param {Transaction} tx - The transaction to be sent.
* @returns {Promise<BroadcastResponse | BroadcastFailure>} A promise that resolves to either a success or failure response.
*/
async broadcast(
async broadcast (
tx: Transaction
): Promise<BroadcastResponse | BroadcastFailure> {
let beef: number[]
Expand All @@ -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 }
Expand Down Expand Up @@ -334,7 +334,7 @@ export default class SHIPCast implements Broadcaster {
}
}

private checkAcknowledgmentFromAllHosts(
private checkAcknowledgmentFromAllHosts (
hostAcknowledgments: Record<string, Set<string>>,
requiredTopics: string[],
require: 'all' | 'any'
Expand Down Expand Up @@ -362,7 +362,7 @@ export default class SHIPCast implements Broadcaster {
return true
}

private checkAcknowledgmentFromAnyHost(
private checkAcknowledgmentFromAnyHost (
hostAcknowledgments: Record<string, Set<string>>,
requiredTopics: string[],
require: 'all' | 'any'
Expand Down Expand Up @@ -395,13 +395,13 @@ export default class SHIPCast implements Broadcaster {
}
}

private checkAcknowledgmentFromSpecificHosts(
private checkAcknowledgmentFromSpecificHosts (
hostAcknowledgments: Record<string, Set<string>>,
requirements: Record<string, 'all' | 'any' | string[]>
): 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
}
Expand Down Expand Up @@ -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<Record<string, Set<string>>> {
private async findInterestedHosts (): Promise<Record<string, Set<string>>> {
// 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)
Expand Down Expand Up @@ -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)
Expand All @@ -488,4 +488,4 @@ export default class SHIPCast implements Broadcaster {
}
return results
}
}
}
2 changes: 1 addition & 1 deletion src/overlay-tools/__tests/SHIPBroadcaster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,4 +1300,4 @@ describe('SHIPCast', () => {
})
})
})
})
})

0 comments on commit 02b7d42

Please sign in to comment.