use pks[]

This commit is contained in:
Felicio Mununga 2023-04-26 15:19:08 +02:00
parent 524245b82c
commit 5d4d7e4b6e
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
1 changed files with 75 additions and 48 deletions

View File

@ -99,7 +99,9 @@ class RequestClient {
/** Uncompressed */ /** Uncompressed */
publicKey: string publicKey: string
): Promise<CommunityInfo | undefined> => { ): Promise<CommunityInfo | undefined> => {
const communityDescription = await this.fetchCommunityDescription(publicKey) const communityDescription = await (
await this.fetchCommunityDescription([publicKey])
).get(publicKey)
if (!communityDescription) { if (!communityDescription) {
return return
@ -113,7 +115,9 @@ class RequestClient {
publicKey: string, publicKey: string,
uuid: string uuid: string
): Promise<ChannelInfo | undefined> => { ): Promise<ChannelInfo | undefined> => {
const communityDescription = await this.fetchCommunityDescription(publicKey) const communityDescription = await (
await this.fetchCommunityDescription([publicKey])
).get(publicKey)
if (!communityDescription) { if (!communityDescription) {
return return
@ -139,57 +143,80 @@ class RequestClient {
return mapUser(contactCodeAdvertisement, publicKey) return mapUser(contactCodeAdvertisement, publicKey)
} }
private fetchCommunityDescription = async ( public fetchCommunityDescription = async (
/** Uncompressed */ /** Compressed */
publicKey: string publicKeys: string[]
): Promise<CommunityDescription | undefined> => { ): Promise<Map<string, CommunityDescription>> => {
const contentTopic = idToContentTopic(publicKey) const decoderPromises = publicKeys.map(async publicKey => {
const symmetricKey = await generateKeyFromPassword(publicKey) const contentTopic = idToContentTopic(publicKey)
const symmetricKey = await generateKeyFromPassword(publicKey)
let communityDescription: CommunityDescription | undefined = undefined return new SymDecoder(contentTopic, symmetricKey)
await this.waku.store.queryOrderedCallback( })
[new SymDecoder(contentTopic, symmetricKey)],
wakuMessage => {
// handle
const message = this.handleWakuMessage(wakuMessage)
if (!message) { const decoders = await Promise.all(decoderPromises)
return
}
if ( const communityDescription: Map<string, CommunityDescription> = new Map()
message.type !== ApplicationMetadataMessage_Type.COMMUNITY_DESCRIPTION await this.waku.store.queryOrderedCallback(decoders, wakuMessage => {
) { // handle
return const message = this.handleWakuMessage(wakuMessage)
}
// decode if (!message) {
const decodedCommunityDescription = CommunityDescription.fromBinary( return
message.payload
)
// validate
if (
!isClockValid(
BigInt(decodedCommunityDescription.clock),
message.timestamp
)
) {
return
}
if (publicKey !== `0x${compressPublicKey(message.signerPublicKey)}`) {
return
}
if (!communityDescription) {
communityDescription = decodedCommunityDescription
}
// stop
return true
} }
)
if (
message.type !== ApplicationMetadataMessage_Type.COMMUNITY_DESCRIPTION
) {
return
}
// decode
const decodedCommunityDescription = CommunityDescription.fromBinary(
message.payload
)
// validate
if (
!isClockValid(
BigInt(decodedCommunityDescription.clock),
message.timestamp
)
) {
return
}
console.log(decodedCommunityDescription)
const decodedCommunityPublicKey = `0x${compressPublicKey(
message.signerPublicKey
)}`
if (!publicKeys.includes(decodedCommunityPublicKey)) {
return
}
if (!communityDescription.has(decodedCommunityPublicKey)) {
// todo?: ensure mapping back to original pk format if deserialized in this fn
communityDescription.set(
decodedCommunityPublicKey,
decodedCommunityDescription
)
}
// skip
// todo?: skip decoder if community is found; impossible?
// stop
// todo?: stop when last community; if decoders/pks served in order
if (publicKeys.some(publicKey => !communityDescription.has(publicKey))) {
return
}
const stop = true
return stop
})
return communityDescription return communityDescription
} }