Skip to content

Commit

Permalink
add preEventCallback to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
kele-leanes committed Dec 5, 2023
1 parent 330661e commit 53a3137
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ import java.util.TimeZone

typealias PublishResponse = org.xmtp.proto.message.api.v1.MessageApiOuterClass.PublishResponse
typealias QueryResponse = org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryResponse
typealias PreEventCallback = suspend () -> Unit

data class ClientOptions(val api: Api = Api()) {
data class ClientOptions(val api: Api = Api(), val preCreateIdentityCallback: PreEventCallback? = null, val preEnableIdentityCallback: PreEventCallback? = null ) {
data class Api(
val env: XMTPEnvironment = XMTPEnvironment.DEV,
val isSecure: Boolean = true,
Expand Down Expand Up @@ -152,13 +153,13 @@ class Client() {
val clientOptions = options ?: ClientOptions()
val apiClient =
GRPCApiClient(environment = clientOptions.api.env, secure = clientOptions.api.isSecure)
return create(account = account, apiClient = apiClient)
return create(account = account, apiClient = apiClient, options = options)
}

fun create(account: SigningKey, apiClient: ApiClient): Client {
fun create(account: SigningKey, apiClient: ApiClient, options: ClientOptions? = null): Client {
return runBlocking {
try {
val privateKeyBundleV1 = loadOrCreateKeys(account, apiClient)
val privateKeyBundleV1 = loadOrCreateKeys(account, apiClient, options)
val client = Client(account.address, privateKeyBundleV1, apiClient)
client.ensureUserContactPublished()
client
Expand All @@ -182,14 +183,15 @@ class Client() {
private suspend fun loadOrCreateKeys(
account: SigningKey,
apiClient: ApiClient,
options: ClientOptions? = null
): PrivateKeyBundleV1 {
val keys = loadPrivateKeys(account, apiClient)
val keys = loadPrivateKeys(account, apiClient, options)
return if (keys != null) {
keys
} else {
val v1Keys = PrivateKeyBundleV1.newBuilder().build().generate(account)
val v1Keys = PrivateKeyBundleV1.newBuilder().build().generate(account, options)
val keyBundle = PrivateKeyBundleBuilder.buildFromV1Key(v1Keys)
val encryptedKeys = keyBundle.encrypted(account)
val encryptedKeys = keyBundle.encrypted(account, options?.preEnableIdentityCallback)
authSave(apiClient, keyBundle.v1, encryptedKeys)
v1Keys
}
Expand All @@ -198,11 +200,12 @@ class Client() {
private suspend fun loadPrivateKeys(
account: SigningKey,
apiClient: ApiClient,
options: ClientOptions? = null
): PrivateKeyBundleV1? {
val encryptedBundles = authCheck(apiClient, account.address)
for (encryptedBundle in encryptedBundles) {
try {
val bundle = encryptedBundle.decrypted(account)
val bundle = encryptedBundle.decrypted(account, options?.preEnableIdentityCallback)
return bundle.v1
} catch (e: Throwable) {
print("Error decoding encrypted private key bundle: $e")
Expand Down

0 comments on commit 53a3137

Please sign in to comment.