Skip to content

Commit

Permalink
Adds pre auth to inbox callback
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Jul 31, 2024
1 parent ffc79f1 commit eb0d094
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 38 deletions.
22 changes: 11 additions & 11 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.14.13"
// implementation "org.xmtp:android:0.14.13"
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.facebook.react:react-native:0.71.3'
implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1"
// xmtp-android local testing setup below (comment org.xmtp:android above)
// implementation files('<PATH TO XMTP-ANDROID>/xmtp-android/library/build/outputs/aar/library-debug.aar')
// implementation 'com.google.crypto.tink:tink-android:1.8.0'
// implementation 'io.grpc:grpc-kotlin-stub:1.4.1'
// implementation 'io.grpc:grpc-okhttp:1.62.2'
// implementation 'io.grpc:grpc-protobuf-lite:1.62.2'
// implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0'
// implementation 'org.web3j:crypto:5.0.0'
// implementation "net.java.dev.jna:jna:5.14.0@aar"
// api 'com.google.protobuf:protobuf-kotlin-lite:3.22.3'
// api 'org.xmtp:proto-kotlin:3.62.1'
implementation files('/Users/cameronvoell/XMTP/xmtp-android/library/build/outputs/aar/library-debug.aar')
implementation 'com.google.crypto.tink:tink-android:1.8.0'
implementation 'io.grpc:grpc-kotlin-stub:1.4.1'
implementation 'io.grpc:grpc-okhttp:1.62.2'
implementation 'io.grpc:grpc-protobuf-lite:1.62.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0'
implementation 'org.web3j:crypto:5.0.0'
implementation "net.java.dev.jna:jna:5.14.0@aar"
api 'com.google.protobuf:protobuf-kotlin-lite:3.22.3'
api 'org.xmtp:proto-kotlin:3.62.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class XMTPModule : Module() {
private val subscriptions: MutableMap<String, Job> = mutableMapOf()
private var preEnableIdentityCallbackDeferred: CompletableDeferred<Unit>? = null
private var preCreateIdentityCallbackDeferred: CompletableDeferred<Unit>? = null
private var preAuthenticateToInboxCallbackDeferred: CompletableDeferred<Unit>? = null


override fun definition() = ModuleDefinition {
Expand All @@ -165,6 +166,7 @@ class XMTPModule : Module() {
"authed",
"preCreateIdentityCallback",
"preEnableIdentityCallback",
"preAuthenticateToInboxCallback",
// Conversations
"conversation",
"group",
Expand Down Expand Up @@ -227,7 +229,7 @@ class XMTPModule : Module() {
//
// Auth functions
//
AsyncFunction("auth") Coroutine { address: String, hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, dbEncryptionKey: List<Int>?, authParams: String ->
AsyncFunction("auth") Coroutine { address: String, hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, hasAuthInboxCallback: Boolean?, dbEncryptionKey: List<Int>?, authParams: String ->
withContext(Dispatchers.IO) {

logV("auth")
Expand All @@ -239,10 +241,14 @@ class XMTPModule : Module() {
preCreateIdentityCallbackDeferred = CompletableDeferred()
if (hasEnableIdentityCallback == true)
preEnableIdentityCallbackDeferred = CompletableDeferred()
if (hasAuthInboxCallback == true)
preAuthenticateToInboxCallbackDeferred = CompletableDeferred()
val preCreateIdentityCallback: PreEventCallback? =
preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true }
val preEnableIdentityCallback: PreEventCallback? =
preEnableIdentityCallback.takeIf { hasEnableIdentityCallback == true }
val preAuthenticateToInboxCallback: PreEventCallback? =
preAuthenticateToInboxCallback.takeIf { hasAuthInboxCallback == true }
val context = if (authOptions.enableV3) context else null
val encryptionKeyBytes =
dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v ->
Expand All @@ -253,6 +259,7 @@ class XMTPModule : Module() {
api = apiEnvironments(authOptions.environment, authOptions.appVersion),
preCreateIdentityCallback = preCreateIdentityCallback,
preEnableIdentityCallback = preEnableIdentityCallback,
preAuthenticateToInboxCallback = preAuthenticateToInboxCallback,
enableV3 = authOptions.enableV3,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
Expand All @@ -273,7 +280,7 @@ class XMTPModule : Module() {
}

// Generate a random wallet and set the client to that
AsyncFunction("createRandom") Coroutine { hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, dbEncryptionKey: List<Int>?, authParams: String ->
AsyncFunction("createRandom") Coroutine { hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, hasPreAuthenticateToInboxCallback: Boolean?, dbEncryptionKey: List<Int>?, authParams: String ->
withContext(Dispatchers.IO) {
logV("createRandom")
val privateKey = PrivateKeyBuilder()
Expand All @@ -282,10 +289,14 @@ class XMTPModule : Module() {
preCreateIdentityCallbackDeferred = CompletableDeferred()
if (hasEnableIdentityCallback == true)
preEnableIdentityCallbackDeferred = CompletableDeferred()
if (hasPreAuthenticateToInboxCallback == true)
preAuthenticateToInboxCallbackDeferred = CompletableDeferred()
val preCreateIdentityCallback: PreEventCallback? =
preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true }
val preEnableIdentityCallback: PreEventCallback? =
preEnableIdentityCallback.takeIf { hasEnableIdentityCallback == true }
val preAuthenticateToInboxCallback: PreEventCallback? =
preAuthenticateToInboxCallback.takeIf { hasPreAuthenticateToInboxCallback == true }

val authOptions = AuthParamsWrapper.authParamsFromJson(authParams)
val context = if (authOptions.enableV3) context else null
Expand All @@ -298,6 +309,7 @@ class XMTPModule : Module() {
api = apiEnvironments(authOptions.environment, authOptions.appVersion),
preCreateIdentityCallback = preCreateIdentityCallback,
preEnableIdentityCallback = preEnableIdentityCallback,
preAuthenticateToInboxCallback = preAuthenticateToInboxCallback,
enableV3 = authOptions.enableV3,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
Expand Down Expand Up @@ -1524,6 +1536,11 @@ class XMTPModule : Module() {
preEnableIdentityCallbackDeferred?.complete(Unit)
}

Function("preAuthenticateToInboxCallbackCompleted") {
logV("preAuthenticateToInboxCallbackCompleted")
preAuthenticateToInboxCallbackDeferred?.complete(Unit)
}

AsyncFunction("allowGroups") Coroutine { inboxId: String, groupIds: List<String> ->
withContext(Dispatchers.IO) {
logV("allowGroups")
Expand Down Expand Up @@ -1844,6 +1861,12 @@ class XMTPModule : Module() {
preCreateIdentityCallbackDeferred?.await()
preCreateIdentityCallbackDeferred = null
}

private val preAuthenticateToInboxCallback: suspend () -> Unit = {
sendEvent("preAuthenticateToInboxCallback")
preAuthenticateToInboxCallbackDeferred?.await()
preAuthenticateToInboxCallbackDeferred = null
}
}


7 changes: 7 additions & 0 deletions example/src/LaunchScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export default function LaunchScreen(
console.log('Pre Enable Identity Callback')
}

const preAuthenticateToInboxCallback = async () => {
console.log('Pre Authenticate To Inbox Callback')
}

const networkOptions = [
{ key: 0, label: 'dev' },
{ key: 1, label: 'local' },
Expand Down Expand Up @@ -222,8 +226,10 @@ export default function LaunchScreen(
XMTP.Client.create(signer, {
env: selectedNetwork,
appVersion,
codecs: supportedCodecs,
preCreateIdentityCallback,
preEnableIdentityCallback,
preAuthenticateToInboxCallback,
enableV3: enableGroups === 'true',
dbEncryptionKey,
})
Expand Down Expand Up @@ -258,6 +264,7 @@ export default function LaunchScreen(
codecs: supportedCodecs,
preCreateIdentityCallback,
preEnableIdentityCallback,
preAuthenticateToInboxCallback,
enableV3: enableGroups === 'true',
dbEncryptionKey,
})
Expand Down
42 changes: 42 additions & 0 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,48 @@ test('can make a MLS V3 client', async () => {
return true
})


Check warning on line 44 in example/src/tests/groupTests.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
test('calls preAuthenticateToInboxCallback when supplied', async () => {
let isCallbackCalled = 0
let isPreAuthCalled = false
const preAuthenticateToInboxCallback = () => {
isCallbackCalled++;

Check warning on line 49 in example/src/tests/groupTests.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
isPreAuthCalled = true
}
const preEnableIdentityCallback = () => {
isCallbackCalled++
}
const preCreateIdentityCallback = () => {
isCallbackCalled++
}
const keyBytes = new Uint8Array([
233, 120, 198, 96, 154, 65, 132, 17, 132, 96, 250, 40, 103, 35, 125, 64,
166, 83, 208, 224, 254, 44, 205, 227, 175, 49, 234, 129, 74, 252, 135,

Check warning on line 60 in example/src/tests/groupTests.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎···`
145,
])

Check warning on line 63 in example/src/tests/groupTests.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎··`

await Client.createRandom({
env: 'local',
enableV3: true,
preEnableIdentityCallback,
preCreateIdentityCallback,
preAuthenticateToInboxCallback,
dbEncryptionKey: keyBytes,
})

assert(
isCallbackCalled === 3,
`callback should be called 3 times but was ${isCallbackCalled}`
)

if (!isPreAuthCalled) {
throw new Error('preAuthenticateToInboxCallback not called')
}

return true
})

test('can delete a local database', async () => {
let [client, anotherClient] = await createClients(2)

Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export async function auth(
appVersion?: string | undefined,
hasCreateIdentityCallback?: boolean | undefined,
hasEnableIdentityCallback?: boolean | undefined,
hasPreAuthenticateToInboxCallback?: boolean | undefined,
enableV3?: boolean | undefined,
dbEncryptionKey?: Uint8Array | undefined,
dbDirectory?: string | undefined,
Expand All @@ -96,6 +97,7 @@ export async function auth(
address,
hasCreateIdentityCallback,
hasEnableIdentityCallback,
hasPreAuthenticateToInboxCallback,
encryptionKey,
JSON.stringify(authParams)
)
Expand All @@ -110,6 +112,7 @@ export async function createRandom(
appVersion?: string | undefined,
hasCreateIdentityCallback?: boolean | undefined,
hasEnableIdentityCallback?: boolean | undefined,
hasPreAuthenticateToInboxCallback?: boolean | undefined,
enableV3?: boolean | undefined,
dbEncryptionKey?: Uint8Array | undefined,
dbDirectory?: string | undefined,
Expand All @@ -129,6 +132,7 @@ export async function createRandom(
return await XMTPModule.createRandom(
hasCreateIdentityCallback,
hasEnableIdentityCallback,
hasPreAuthenticateToInboxCallback,
encryptionKey,
JSON.stringify(authParams)
)
Expand Down Expand Up @@ -863,6 +867,10 @@ export function preCreateIdentityCallbackCompleted() {
XMTPModule.preCreateIdentityCallbackCompleted()
}

export function preAuthenticateToInboxCallbackCompleted() {
XMTPModule.preAuthenticateToInboxCallbackCompleted()
}

export async function isGroupActive(
inboxId: string,
id: string
Expand Down
Loading

0 comments on commit eb0d094

Please sign in to comment.