Skip to content

Commit

Permalink
feat: fix up the example app
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 23, 2024
1 parent 14b3870 commit 5ddf0f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.xmtp.android.example.account
import android.net.Uri
import com.walletconnect.wcmodal.client.Modal
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.web3j.crypto.Keys
import org.xmtp.android.example.connect.getPersonalSignBody
import org.xmtp.android.example.extension.requestMethod
Expand All @@ -14,20 +15,38 @@ import org.xmtp.proto.message.contents.SignatureOuterClass
data class WalletConnectV2Account(
val session: Modal.Model.ApprovedSession,
val chain: String,
private val sendSessionRequestDeepLink: (Uri) -> Unit
private val sendSessionRequestDeepLink: (Uri) -> Unit,
) :
SigningKey {
override val address: String
get() = Keys.toChecksumAddress(
session.namespaces.getValue(chain).accounts[0].substringAfterLast(
":"
)
)

override suspend fun sign(data: ByteArray): SignatureOuterClass.Signature? {
return signLegacy(String(data))
}

override fun sign(text: String): ByteArray {
val (parentChain, chainId, account) = session.namespaces.getValue(chain).accounts[0].split(":")
val requestParams = session.namespaces.getValue(chain).methods.find { method ->
method == "personal_sign"
}?.let { method ->
Modal.Params.Request(
sessionTopic = session.topic,
method = method,
params = getPersonalSignBody(text, account),
chainId = "$parentChain:$chainId"
)
}

runCatching {
runBlocking {
requestMethod(requestParams!!, sendSessionRequestDeepLink).first().getOrThrow()
}
}.onSuccess {
return it
}.onFailure {}

return byteArrayOf()
}

override suspend fun signLegacy(message: String): SignatureOuterClass.Signature? {
val (parentChain, chainId, account) = session.namespaces.getValue(chain).accounts[0].split(":")
val requestParams = session.namespaces.getValue(chain).methods.find { method ->
Expand All @@ -50,4 +69,12 @@ data class WalletConnectV2Account(

return null
}

override fun getAddress(): String {
return Keys.toChecksumAddress(
session.namespaces.getValue(chain).accounts[0].substringAfterLast(
":"
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ConnectWalletViewModel : ViewModel() {
val wallet = PrivateKeyBuilder()
val client = Client().create(wallet, ClientManager.CLIENT_OPTIONS)
_uiState.value = ConnectUiState.Success(
wallet.address,
wallet.getAddress(),
PrivateKeyBundleV1Builder.encodeData(client.privateKeyBundleV1)
)
} catch (e: XMTPException) {
Expand All @@ -110,7 +110,7 @@ class ConnectWalletViewModel : ViewModel() {
}
val client = Client().create(wallet, ClientManager.CLIENT_OPTIONS)
_uiState.value = ConnectUiState.Success(
wallet.address,
wallet.getAddress(),
PrivateKeyBundleV1Builder.encodeData(client.privateKeyBundleV1)
)
} catch (e: Exception) {
Expand Down

0 comments on commit 5ddf0f5

Please sign in to comment.