Skip to content

Commit

Permalink
fix up android history url
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Aug 7, 2024
1 parent 97952f6 commit 8f98de7
Showing 1 changed file with 66 additions and 76 deletions.
142 changes: 66 additions & 76 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,53 @@ class XMTPModule : Module() {
}
}

private fun clientOptions(
dbEncryptionKey: List<Int>?,
authParams: String,
hasCreateIdentityCallback: Boolean? = null,
hasEnableIdentityCallback: Boolean? = null,
hasPreAuthenticateToInboxCallback: Boolean? = null,
): ClientOptions {
if (hasCreateIdentityCallback == true)
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
val encryptionKeyBytes =
dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v ->
a.apply { set(i, v.toByte()) }
}

val historySyncUrl = authOptions.historySyncUrl
?: when (authOptions.environment) {
"production" -> "https://message-history.production.ephemera.network/"
"local" -> "http://0.0.0.0:5558"
else -> "https://message-history.dev.ephemera.network/"
}

return ClientOptions(
api = apiEnvironments(authOptions.environment, authOptions.appVersion),
preCreateIdentityCallback = preCreateIdentityCallback,
preEnableIdentityCallback = preEnableIdentityCallback,
preAuthenticateToInboxCallback = preAuthenticateToInboxCallback,
enableV3 = authOptions.enableV3,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
dbDirectory = authOptions.dbDirectory,
historySyncUrl = historySyncUrl
)
}

private var clients: MutableMap<String, Client> = mutableMapOf()
private var xmtpPush: XMTPPush? = null
private var signer: ReactNativeSigner? = null
Expand Down Expand Up @@ -235,36 +282,12 @@ class XMTPModule : Module() {
logV("auth")
val reactSigner = ReactNativeSigner(module = this@XMTPModule, address = address)
signer = reactSigner
val authOptions = AuthParamsWrapper.authParamsFromJson(authParams)

if (hasCreateIdentityCallback == true)
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 ->
a.apply { set(i, v.toByte()) }
}

val options = ClientOptions(
api = apiEnvironments(authOptions.environment, authOptions.appVersion),
preCreateIdentityCallback = preCreateIdentityCallback,
preEnableIdentityCallback = preEnableIdentityCallback,
preAuthenticateToInboxCallback = preAuthenticateToInboxCallback,
enableV3 = authOptions.enableV3,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
dbDirectory = authOptions.dbDirectory,
historySyncUrl = authOptions.historySyncUrl
val options = clientOptions(
dbEncryptionKey,
authParams,
hasCreateIdentityCallback,
hasEnableIdentityCallback,
hasAuthInboxCallback,
)
val client = Client().create(account = reactSigner, options = options)
clients[client.inboxId] = client
Expand All @@ -284,38 +307,12 @@ class XMTPModule : Module() {
withContext(Dispatchers.IO) {
logV("createRandom")
val privateKey = PrivateKeyBuilder()

if (hasCreateIdentityCallback == true)
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
val encryptionKeyBytes =
dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v ->
a.apply { set(i, v.toByte()) }
}

val options = ClientOptions(
api = apiEnvironments(authOptions.environment, authOptions.appVersion),
preCreateIdentityCallback = preCreateIdentityCallback,
preEnableIdentityCallback = preEnableIdentityCallback,
preAuthenticateToInboxCallback = preAuthenticateToInboxCallback,
enableV3 = authOptions.enableV3,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
dbDirectory = authOptions.dbDirectory,
historySyncUrl = authOptions.historySyncUrl

val options = clientOptions(
dbEncryptionKey,
authParams,
hasCreateIdentityCallback,
hasEnableIdentityCallback,
hasPreAuthenticateToInboxCallback,
)
val randomClient = Client().create(account = privateKey, options = options)

Expand All @@ -328,20 +325,10 @@ class XMTPModule : Module() {
AsyncFunction("createFromKeyBundle") Coroutine { keyBundle: String, dbEncryptionKey: List<Int>?, authParams: String ->
withContext(Dispatchers.IO) {
logV("createFromKeyBundle")
val authOptions = AuthParamsWrapper.authParamsFromJson(authParams)
try {
val context = if (authOptions.enableV3) context else null
val encryptionKeyBytes =
dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v ->
a.apply { set(i, v.toByte()) }
}
val options = ClientOptions(
api = apiEnvironments(authOptions.environment, authOptions.appVersion),
enableV3 = authOptions.enableV3,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
dbDirectory = authOptions.dbDirectory,
historySyncUrl = authOptions.historySyncUrl
val options = clientOptions(
dbEncryptionKey,
authParams
)
val bundle =
PrivateKeyOuterClass.PrivateKeyBundle.parseFrom(
Expand Down Expand Up @@ -921,7 +908,10 @@ class XMTPModule : Module() {
val client = clients[inboxId] ?: throw XMTPException("No client")
val createGroupParams =
CreateGroupParamsWrapper.createGroupParamsFromJson(groupOptionsJson)
val permissionPolicySet = PermissionPolicySetWrapper.createPermissionPolicySetFromJson(permissionPolicySetJson)
val permissionPolicySet =
PermissionPolicySetWrapper.createPermissionPolicySetFromJson(
permissionPolicySetJson
)
val group = client.conversations.newGroupCustomPermissions(
peerAddresses,
permissionPolicySet,
Expand Down

0 comments on commit 8f98de7

Please sign in to comment.