Skip to content

Commit

Permalink
linter clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 12, 2024
1 parent 7a7c08a commit 49b6b67
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 1 addition & 46 deletions example/src/LaunchScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array> {
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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions example/src/StreamScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client<any> | null>(null)

useEffect(() => {
Client.createRandom().then((client) => {
const dbEncryptionKey = getDbEncryptionKey('dev')
Client.createRandom({
env: 'dev',
dbEncryptionKey,
}).then((client) => {
setClient(client)
})
}, [])
Expand Down
45 changes: 45 additions & 0 deletions example/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion example/src/tests/clientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 55 additions & 6 deletions example/src/tests/conversationTests.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
})
Expand Down
8 changes: 3 additions & 5 deletions example/src/tests/createdAtTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ test('group createdAt matches streamAll', async () => {

// Start streaming groups
const allGroups: Conversation<any>[] = []
const cancelStream = await alix.conversations.stream(
async (group: Conversation<any>) => {
allGroups.push(group)
}
)
await alix.conversations.stream(async (group: Conversation<any>) => {
allGroups.push(group)
})

await delayToPropogate()

Expand Down
9 changes: 9 additions & 0 deletions example/src/tests/dmTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
4 changes: 2 additions & 2 deletions example/src/tests/groupPerformanceTests.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)

Expand Down
11 changes: 4 additions & 7 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,9 @@ test('can stream groups', async () => {

// Start streaming groups
const groups: Conversation<any>[] = []
await alixClient.conversations.stream(
async (group: Conversation<any>) => {
groups.push(group)
},
'groups'
)
await alixClient.conversations.stream(async (group: Conversation<any>) => {
groups.push(group)
}, 'groups')

// caro creates a group with alix, so stream callback is fired
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 49b6b67

Please sign in to comment.