-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/xmtp/xmtp-android into np/e…
…xpose-decrypted-encoded-messages
- Loading branch information
Showing
19 changed files
with
726 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
example/src/main/java/org/xmtp/android/example/ExampleApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.xmtp.android.example | ||
|
||
import android.app.Application | ||
import com.walletconnect.android.Core | ||
import com.walletconnect.android.CoreClient | ||
import com.walletconnect.android.relay.ConnectionType | ||
import com.walletconnect.wcmodal.client.Modal | ||
import com.walletconnect.wcmodal.client.WalletConnectModal | ||
import timber.log.Timber | ||
|
||
const val BASE_LOG_TAG = "WC2" | ||
class ExampleApp: Application() { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
val connectionType = ConnectionType.AUTOMATIC | ||
val relayUrl = "relay.walletconnect.com" | ||
val serverUrl = "wss://$relayUrl?projectId=${BuildConfig.PROJECT_ID}" | ||
val appMetaData = Core.Model.AppMetaData( | ||
name = "XMTP Example", | ||
description = "Example app using the xmtp-android SDK", | ||
url = "https://xmtp.org", | ||
icons = listOf("https://avatars.githubusercontent.com/u/82580170?s=48&v=4"), | ||
redirect = "xmtp-example-wc://request" | ||
) | ||
|
||
CoreClient.initialize( | ||
metaData = appMetaData, | ||
relayServerUrl = serverUrl, | ||
connectionType = connectionType, | ||
application = this, | ||
onError = { | ||
Timber.tag("$BASE_LOG_TAG CoreClient").d(it.toString()) | ||
} | ||
) | ||
|
||
WalletConnectModal.initialize( | ||
init = Modal.Params.Init(core = CoreClient), | ||
onSuccess = { | ||
Timber.tag("$BASE_LOG_TAG initialize").d("initialize successfully") | ||
}, | ||
onError = { error -> | ||
Timber.tag("$BASE_LOG_TAG initialize").d(error.toString()) | ||
} | ||
) | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
example/src/main/java/org/xmtp/android/example/account/WalletConnectAccount.kt
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
example/src/main/java/org/xmtp/android/example/account/WalletConnectV2Account.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.xmtp.android.example.account | ||
|
||
import android.net.Uri | ||
import com.walletconnect.wcmodal.client.Modal | ||
import kotlinx.coroutines.flow.first | ||
import org.web3j.crypto.Keys | ||
import org.xmtp.android.example.connect.getPersonalSignBody | ||
import org.xmtp.android.example.extension.requestMethod | ||
import org.xmtp.android.library.SigningKey | ||
import org.xmtp.android.library.messages.SignatureBuilder | ||
import org.xmtp.proto.message.contents.SignatureOuterClass | ||
|
||
|
||
data class WalletConnectV2Account( | ||
val session: Modal.Model.ApprovedSession, | ||
val chain: String, | ||
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 sign(String(data)) | ||
} | ||
|
||
override suspend fun sign(message: String): SignatureOuterClass.Signature? { | ||
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(message, account), | ||
chainId = "$parentChain:$chainId" | ||
) | ||
} | ||
runCatching { | ||
requestMethod(requestParams!!, sendSessionRequestDeepLink).first().getOrThrow() | ||
} | ||
.onSuccess { | ||
return SignatureBuilder.buildFromSignatureData(it) | ||
} | ||
.onFailure {} | ||
|
||
return null | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
example/src/main/java/org/xmtp/android/example/connect/ChainSelectionUi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.xmtp.android.example.connect | ||
|
||
|
||
data class ChainSelectionUi( | ||
val chainName: String, | ||
val chainNamespace: String, | ||
val chainReference: String, | ||
val icon: Int, | ||
val methods: List<String>, | ||
val events: List<String>, | ||
var isSelected: Boolean = false, | ||
) { | ||
val chainId = "${chainNamespace}:${chainReference}" | ||
} | ||
|
||
fun Chains.toChainUiState() = ChainSelectionUi(chainName, chainNamespace, chainReference, icon, methods, events) |
46 changes: 46 additions & 0 deletions
46
example/src/main/java/org/xmtp/android/example/connect/Chains.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.xmtp.android.example.connect | ||
|
||
import androidx.annotation.DrawableRes | ||
import org.xmtp.android.example.R | ||
|
||
|
||
fun getPersonalSignBody(message:String, account: String): String { | ||
val msg = message.encodeToByteArray() | ||
.joinToString(separator = "", prefix = "0x") { eachByte -> "%02x".format(eachByte) } | ||
return "[\"$msg\", \"$account\"]" | ||
} | ||
enum class Chains( | ||
val chainName: String, | ||
val chainNamespace: String, | ||
val chainReference: String, | ||
@DrawableRes val icon: Int, | ||
val methods: List<String>, | ||
val events: List<String>, | ||
) { | ||
ETHEREUM_MAIN( | ||
chainName = "Ethereum", | ||
chainNamespace = Info.Eth.chain, | ||
chainReference = "1", | ||
icon = R.drawable.ic_ethereum, | ||
methods = Info.Eth.defaultMethods, | ||
events = Info.Eth.defaultEvents, | ||
) | ||
} | ||
|
||
sealed class Info { | ||
abstract val chain: String | ||
abstract val defaultEvents: List<String> | ||
abstract val defaultMethods: List<String> | ||
|
||
object Eth : Info() { | ||
override val chain = "eip155" | ||
override val defaultEvents: List<String> = listOf("chainChanged", "accountsChanged") | ||
override val defaultMethods: List<String> = listOf( | ||
"eth_sendTransaction", | ||
"personal_sign", | ||
"eth_sign", | ||
"eth_signTypedData" | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.