From 5d4d7e4b6e7baa6913ce079a41b4595f65f05145 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Wed, 26 Apr 2023 15:19:08 +0200 Subject: [PATCH] use pks[] --- .../src/request-client/request-client.ts | 123 +++++++++++------- 1 file changed, 75 insertions(+), 48 deletions(-) diff --git a/packages/status-js/src/request-client/request-client.ts b/packages/status-js/src/request-client/request-client.ts index 40d4263d..c1992cb5 100644 --- a/packages/status-js/src/request-client/request-client.ts +++ b/packages/status-js/src/request-client/request-client.ts @@ -99,7 +99,9 @@ class RequestClient { /** Uncompressed */ publicKey: string ): Promise => { - const communityDescription = await this.fetchCommunityDescription(publicKey) + const communityDescription = await ( + await this.fetchCommunityDescription([publicKey]) + ).get(publicKey) if (!communityDescription) { return @@ -113,7 +115,9 @@ class RequestClient { publicKey: string, uuid: string ): Promise => { - const communityDescription = await this.fetchCommunityDescription(publicKey) + const communityDescription = await ( + await this.fetchCommunityDescription([publicKey]) + ).get(publicKey) if (!communityDescription) { return @@ -139,57 +143,80 @@ class RequestClient { return mapUser(contactCodeAdvertisement, publicKey) } - private fetchCommunityDescription = async ( - /** Uncompressed */ - publicKey: string - ): Promise => { - const contentTopic = idToContentTopic(publicKey) - const symmetricKey = await generateKeyFromPassword(publicKey) + public fetchCommunityDescription = async ( + /** Compressed */ + publicKeys: string[] + ): Promise> => { + const decoderPromises = publicKeys.map(async publicKey => { + const contentTopic = idToContentTopic(publicKey) + const symmetricKey = await generateKeyFromPassword(publicKey) - let communityDescription: CommunityDescription | undefined = undefined - await this.waku.store.queryOrderedCallback( - [new SymDecoder(contentTopic, symmetricKey)], - wakuMessage => { - // handle - const message = this.handleWakuMessage(wakuMessage) + return new SymDecoder(contentTopic, symmetricKey) + }) - if (!message) { - return - } + const decoders = await Promise.all(decoderPromises) - if ( - message.type !== ApplicationMetadataMessage_Type.COMMUNITY_DESCRIPTION - ) { - return - } + const communityDescription: Map = new Map() + await this.waku.store.queryOrderedCallback(decoders, wakuMessage => { + // handle + const message = this.handleWakuMessage(wakuMessage) - // decode - const decodedCommunityDescription = CommunityDescription.fromBinary( - 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) { + return } - ) + + 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 }