From 9a495fd9027c94354971e56e47c203b60b44e53d Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Mon, 18 Mar 2024 21:47:54 +0900 Subject: [PATCH] handle updates for encrypted communities (#537) * a * t * t * f * c * c * r --- .changeset/brave-paws-promise.md | 5 ++ .changeset/fluffy-maps-wait.md | 5 ++ .../src/client/community/is-encrypted.ts | 13 +++++ .../src/request-client/request-client.ts | 49 +++++++++++++++++-- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 .changeset/brave-paws-promise.md create mode 100644 .changeset/fluffy-maps-wait.md create mode 100644 packages/status-js/src/client/community/is-encrypted.ts diff --git a/.changeset/brave-paws-promise.md b/.changeset/brave-paws-promise.md new file mode 100644 index 00000000..46a86cf3 --- /dev/null +++ b/.changeset/brave-paws-promise.md @@ -0,0 +1,5 @@ +--- +'@status-im/components': patch +--- + +export `Tooltip` diff --git a/.changeset/fluffy-maps-wait.md b/.changeset/fluffy-maps-wait.md new file mode 100644 index 00000000..4652b294 --- /dev/null +++ b/.changeset/fluffy-maps-wait.md @@ -0,0 +1,5 @@ +--- +'@status-im/js': minor +--- + +handle updates for encrypted communities diff --git a/packages/status-js/src/client/community/is-encrypted.ts b/packages/status-js/src/client/community/is-encrypted.ts new file mode 100644 index 00000000..5062939e --- /dev/null +++ b/packages/status-js/src/client/community/is-encrypted.ts @@ -0,0 +1,13 @@ +import { + type CommunityDescription, + CommunityTokenPermission_Type, +} from '../../protos/communities_pb' + +export function isEncrypted( + tokenPermissions: CommunityDescription['tokenPermissions'] +): boolean { + return Object.values(tokenPermissions).some( + permission => + permission.type === CommunityTokenPermission_Type.BECOME_MEMBER + ) +} diff --git a/packages/status-js/src/request-client/request-client.ts b/packages/status-js/src/request-client/request-client.ts index 022111d5..90bd6202 100644 --- a/packages/status-js/src/request-client/request-client.ts +++ b/packages/status-js/src/request-client/request-client.ts @@ -4,12 +4,17 @@ import { createDecoder } from '@waku/message-encryption/symmetric' import { createLightNode, waitForRemotePeer } from '@waku/sdk' import { bytesToHex } from 'ethereum-cryptography/utils' +import { isEncrypted } from '../client/community/is-encrypted' import { peers } from '../consts/peers' +// import { EthereumClient } from '../ethereum-client/ethereum-client' import { ApplicationMetadataMessage, ApplicationMetadataMessage_Type, } from '../protos/application-metadata-message_pb' -import { CommunityDescription } from '../protos/communities_pb' +import { + CommunityDescription, + // CommunityTokenPermission_Type, +} from '../protos/communities_pb' import { ProtocolMessage } from '../protos/protocol-message_pb' import { ContactCodeAdvertisement } from '../protos/push-notifications_pb' import { compressPublicKey } from '../utils/compress-public-key' @@ -17,6 +22,7 @@ import { generateKeyFromPassword } from '../utils/generate-key-from-password' import { idToContentTopic } from '../utils/id-to-content-topic' import { isClockValid } from '../utils/is-clock-valid' import { payloadToId } from '../utils/payload-to-id' +// import { publicKeyToETHAddress } from '../utils/public-key-to-eth-address' import { recoverPublicKey } from '../utils/recover-public-key' import { mapChannel } from './map-channel' import { mapCommunity } from './map-community' @@ -189,7 +195,41 @@ class RequestClient { return } - if (publicKey !== `0x${compressPublicKey(message.signerPublicKey)}`) { + const signerPublicKey = `0x${compressPublicKey( + message.signerPublicKey + )}` + + // isSignatureValid + if (isEncrypted(decodedCommunityDescription.tokenPermissions)) { + // const permission = Object.values( + // decodedCommunityDescription.tokenPermissions + // ).find( + // permission => + // permission.type === + // CommunityTokenPermission_Type.BECOME_TOKEN_OWNER + // ) + // if (!permission) { + // return + // } + // const criteria = permission.tokenCriteria[0] + // const contracts = criteria?.contractAddresses + // const chainId = Object.keys(contracts)[0] + // if (!chainId) { + // return + // } + // // get client config based on chainId + // // get client + // const client = new EthereumClient( + // `https://mainnet.infura.io/v3/${process.env.KEY}` + // ) + // // call status contract for chainId + // const address = publicKeyToETHAddress(publicKey) + // // call contracts from previous call with address + // const ownerPublicKey = '0x0' + // if (ownerPublicKey !== signerPublicKey) { + // return + // } + } else if (publicKey !== signerPublicKey) { return } @@ -300,7 +340,10 @@ class RequestClient { let decodedProtocol try { decodedProtocol = ProtocolMessage.fromBinary(messageToDecode) - if (decodedProtocol) { + + if (decodedProtocol.encryptedMessage.none) { + messageToDecode = decodedProtocol.encryptedMessage.none.payload + } else if (decodedProtocol) { messageToDecode = decodedProtocol.publicMessage } } catch {