From 49b6b674a7c0ca283504c700373d5270c520d64f Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 12 Nov 2024 07:39:36 -0800 Subject: [PATCH] linter clean up --- .../project.pbxproj | 2 + example/src/LaunchScreen.tsx | 47 +------------- example/src/StreamScreen.tsx | 8 ++- example/src/hooks.tsx | 45 ++++++++++++++ example/src/tests/clientTests.ts | 2 +- example/src/tests/conversationTests.ts | 61 +++++++++++++++++-- example/src/tests/createdAtTests.ts | 8 +-- example/src/tests/dmTests.ts | 9 +++ example/src/tests/groupPerformanceTests.ts | 4 +- example/src/tests/groupTests.ts | 11 ++-- 10 files changed, 128 insertions(+), 69 deletions(-) diff --git a/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj b/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj index 4bfad6909..0d2d68566 100644 --- a/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj +++ b/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj @@ -314,10 +314,12 @@ inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-xmtpreactnativesdkexample/Pods-xmtpreactnativesdkexample-frameworks.sh", "${PODS_XCFRAMEWORKS_BUILD_DIR}/OpenSSL-Universal/OpenSSL.framework/OpenSSL", + "${PODS_XCFRAMEWORKS_BUILD_DIR}/hermes-engine/Pre-built/hermes.framework/hermes", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/OpenSSL.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/hermes.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; diff --git a/example/src/LaunchScreen.tsx b/example/src/LaunchScreen.tsx index 6038d2d1f..4e7df0776 100644 --- a/example/src/LaunchScreen.tsx +++ b/example/src/LaunchScreen.tsx @@ -10,50 +10,10 @@ import { useXmtp } from 'xmtp-react-native-sdk' import { NavigationParamList } from './Navigation' import { TestCategory } from './TestScreen' import { supportedCodecs } from './contentTypes/contentTypes' -import { useSavedAddress } from './hooks' +import { getDbEncryptionKey, useSavedAddress } from './hooks' const appVersion = 'XMTP_RN_EX/0.0.1' -async function getDbEncryptionKey( - network: string, - clear: boolean = false -): Promise { - const key = `xmtp-${network}` - const result = await EncryptedStorage.getItem(key) - if ((result && clear === true) || !result) { - if (result) { - await EncryptedStorage.removeItem(key) - } - - const randomBytes = crypto.getRandomValues(new Uint8Array(32)) - const randomBytesString = uint8ArrayToHexString(randomBytes) - await EncryptedStorage.setItem(key, randomBytesString) - return randomBytes - } else { - return hexStringToUint8Array(result) - } -} - -function uint8ArrayToHexString(byteArray: Uint8Array): string { - return Array.from(byteArray, function (byte) { - return ('0' + (byte & 0xff).toString(16)).slice(-2) - }).join('') -} - -function hexStringToUint8Array(hexString: string): Uint8Array { - // Ensure the hex string has an even number of characters for proper parsing - if (hexString.length % 2 !== 0) { - console.error('The hex string must have an even number of characters') - return new Uint8Array() - } - // Split the hex string into an array of byte-sized (2 characters) hex strings - const byteStrings = hexString.match(/.{1,2}/g) || [] - // Convert each byte-sized hex string into a numeric byte value - const byteArray = byteStrings.map((byteStr) => parseInt(byteStr, 16)) - // Create a new Uint8Array from the array of numeric byte values - return new Uint8Array(byteArray) -} - /// Prompt the user to run the tests, generate a wallet, or connect a wallet. export default function LaunchScreen( this: any, @@ -99,11 +59,6 @@ export default function LaunchScreen( { key: 2, label: 'production' }, ] - const groupOptions = [ - { key: 0, label: 'true' }, - { key: 1, label: 'false' }, - ] - const testOptions = Object.entries(TestCategory).map( ([key, value], index) => ({ key: index, diff --git a/example/src/StreamScreen.tsx b/example/src/StreamScreen.tsx index b3caeee88..3aa1154cf 100644 --- a/example/src/StreamScreen.tsx +++ b/example/src/StreamScreen.tsx @@ -2,15 +2,19 @@ import React, { useCallback, useEffect, useState } from 'react' import { Button, FlatList, Text, View, Switch, Pressable } from 'react-native' +import { getDbEncryptionKey } from './hooks' import { Client, Conversation, DecodedMessage } from '../../src' let events: StreamEvent[] = [] const useClient = () => { const [client, setClient] = useState | null>(null) - useEffect(() => { - Client.createRandom().then((client) => { + const dbEncryptionKey = getDbEncryptionKey('dev') + Client.createRandom({ + env: 'dev', + dbEncryptionKey, + }).then((client) => { setClient(client) }) }, []) diff --git a/example/src/hooks.tsx b/example/src/hooks.tsx index c092f7f6c..44576694d 100644 --- a/example/src/hooks.tsx +++ b/example/src/hooks.tsx @@ -550,3 +550,48 @@ export function useSavedAddress(): { }, } } + +export function getDbEncryptionKey( + network: string, + clear: boolean = false +): Uint8Array { + const key = `xmtp-${network}` + // eslint-disable-next-line no-unused-expressions + ;async () => { + const result = await EncryptedStorage.getItem(key) + if ((result && clear === true) || !result) { + if (result) { + await EncryptedStorage.removeItem(key) + } + + const randomBytes = crypto.getRandomValues(new Uint8Array(32)) + const randomBytesString = uint8ArrayToHexString(randomBytes) + await EncryptedStorage.setItem(key, randomBytesString) + return randomBytes + } else { + return hexStringToUint8Array(result) + } + } + const randomBytes = crypto.getRandomValues(new Uint8Array(32)) + return randomBytes +} + +function uint8ArrayToHexString(byteArray: Uint8Array): string { + return Array.from(byteArray, function (byte) { + return ('0' + (byte & 0xff).toString(16)).slice(-2) + }).join('') +} + +function hexStringToUint8Array(hexString: string): Uint8Array { + // Ensure the hex string has an even number of characters for proper parsing + if (hexString.length % 2 !== 0) { + console.error('The hex string must have an even number of characters') + return new Uint8Array() + } + // Split the hex string into an array of byte-sized (2 characters) hex strings + const byteStrings = hexString.match(/.{1,2}/g) || [] + // Convert each byte-sized hex string into a numeric byte value + const byteArray = byteStrings.map((byteStr) => parseInt(byteStr, 16)) + // Create a new Uint8Array from the array of numeric byte values + return new Uint8Array(byteArray) +} diff --git a/example/src/tests/clientTests.ts b/example/src/tests/clientTests.ts index f00a9259c..f1ea53e8f 100644 --- a/example/src/tests/clientTests.ts +++ b/example/src/tests/clientTests.ts @@ -56,7 +56,7 @@ test('can revoke all other installations', async () => { dbEncryptionKey: keyBytes, }) - const alix2Build = await Client.build(alix2.address, { + await Client.build(alix2.address, { env: 'local', appVersion: 'Testing/0.0.0', dbEncryptionKey: keyBytes, diff --git a/example/src/tests/conversationTests.ts b/example/src/tests/conversationTests.ts index 17246e62c..ebba986d1 100644 --- a/example/src/tests/conversationTests.ts +++ b/example/src/tests/conversationTests.ts @@ -1,5 +1,9 @@ import { Test, assert, createClients, delayToPropogate } from './test-utils' -import { Conversation, ConversationId, ConversationVersion } from '../../../src/index' +import { + Conversation, + ConversationId, + ConversationVersion, +} from '../../../src/index' export const conversationTests: Test[] = [] let counter = 1 @@ -172,6 +176,39 @@ test('can list groups', async () => { return true }) +test('can list conversation messages', async () => { + const [alixClient, boClient, caroClient] = await createClients(3) + + const boGroup = await boClient.conversations.newGroup([alixClient.address]) + const boDm = await boClient.conversations.findOrCreateDm(caroClient.address) + const boGroupConversation = await boClient.conversations.findConversation( + boGroup.id + ) + const boDmConversation = await boClient.conversations.findConversation( + boDm.id + ) + + await boGroupConversation?.send('hello') + await boGroupConversation?.send('hello') + await boDmConversation?.send('hello') + await boDmConversation?.send('hello') + + const boGroupMessages = await boGroupConversation?.messages() + const boDmMessages = await boDmConversation?.messages() + + assert( + boGroupMessages?.length === 3, + `bo conversation lengths should be 4 but was ${boGroupMessages?.length}` + ) + + assert( + boDmMessages?.length === 3, + `alix conversation lengths should be 3 but was ${boDmMessages?.length}` + ) + + return true +}) + test('can stream both conversations and messages at same time', async () => { const [alix, bo] = await createClients(2) @@ -209,22 +246,34 @@ test('can stream conversation messages', async () => { const alixGroup = await alixClient.conversations.newGroup([boClient.address]) const alixDm = await alixClient.conversations.findOrCreateDm(boClient.address) - const alixConversation = await alixClient.conversations.findConversation( + const alixGroupConversation = await alixClient.conversations.findConversation( alixGroup.id ) + const alixDmConversation = await alixClient.conversations.findConversation( + alixDm.id + ) let dmMessageCallbacks = 0 let conversationMessageCallbacks = 0 - await alixConversation?.streamMessages(async () => { + await alixGroupConversation?.streamMessages(async () => { conversationMessageCallbacks++ }) - await alixDm.streamMessages(async () => { + await alixDmConversation?.streamMessages(async () => { dmMessageCallbacks++ }) - await alixConversation?.send({ text: `first message` }) - await alixDm.send({ text: `first message` }) + await alixGroupConversation?.send({ text: `first message` }) + await alixDmConversation?.send({ text: `first message` }) + + assert( + conversationMessageCallbacks === 1, + 'conversation stream should have received 1 conversation' + ) + assert( + dmMessageCallbacks === 1, + 'message stream should have received 1 message' + ) return true }) diff --git a/example/src/tests/createdAtTests.ts b/example/src/tests/createdAtTests.ts index a4d0b4f08..5d038a02c 100644 --- a/example/src/tests/createdAtTests.ts +++ b/example/src/tests/createdAtTests.ts @@ -172,11 +172,9 @@ test('group createdAt matches streamAll', async () => { // Start streaming groups const allGroups: Conversation[] = [] - const cancelStream = await alix.conversations.stream( - async (group: Conversation) => { - allGroups.push(group) - } - ) + await alix.conversations.stream(async (group: Conversation) => { + allGroups.push(group) + }) await delayToPropogate() diff --git a/example/src/tests/dmTests.ts b/example/src/tests/dmTests.ts index 476794315..0c10ca24a 100644 --- a/example/src/tests/dmTests.ts +++ b/example/src/tests/dmTests.ts @@ -115,6 +115,15 @@ test('can stream dm messages', async () => { await alixConversation?.send({ text: `first message` }) await alixDm.send({ text: `first message` }) + assert( + conversationMessageCallbacks === 1, + 'conversation stream should have received 1 conversation' + ) + assert( + dmMessageCallbacks === 1, + 'message stream should have received 1 message' + ) + return true }) diff --git a/example/src/tests/groupPerformanceTests.ts b/example/src/tests/groupPerformanceTests.ts index 92a2fceac..a18d86df8 100644 --- a/example/src/tests/groupPerformanceTests.ts +++ b/example/src/tests/groupPerformanceTests.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-extra-non-null-assertion */ -import { Client, Conversation, Dm, Group } from 'xmtp-react-native-sdk' +import { Client, Dm, Group } from 'xmtp-react-native-sdk' import { Test, assert, createClients } from './test-utils' @@ -60,7 +60,7 @@ async function beforeAll( groupSize: number = 1, messages: number = 1, peersSize: number = 1, - includeDms: boolean = false, + includeDms: boolean = false ) { ;[alixClient] = await createClients(1) diff --git a/example/src/tests/groupTests.ts b/example/src/tests/groupTests.ts index dab24fd71..267a96f73 100644 --- a/example/src/tests/groupTests.ts +++ b/example/src/tests/groupTests.ts @@ -609,12 +609,9 @@ test('can stream groups', async () => { // Start streaming groups const groups: Conversation[] = [] - await alixClient.conversations.stream( - async (group: Conversation) => { - groups.push(group) - }, - 'groups' - ) + await alixClient.conversations.stream(async (group: Conversation) => { + groups.push(group) + }, 'groups') // caro creates a group with alix, so stream callback is fired // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -805,7 +802,7 @@ test('can stream groups and messages', async () => { }) test('canMessage', async () => { - const [bo, alix, caro] = await createClients(3) + const [alix, caro] = await createClients(3) const canMessageV3 = await caro.canMessage([ caro.address,