Skip to content

Commit

Permalink
Update WC2 to 1.31.0 (#20)
Browse files Browse the repository at this point in the history
* Update WC2 to 1.31.0

* Add expiry

* Fix build
  • Loading branch information
ruixhuang authored Mar 29, 2024
1 parent c11ea42 commit c885b0f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ buildscript {

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.2.2' apply false
id 'com.android.application' version '8.3.1' apply false
id 'com.android.library' version '8.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.21' apply false
id 'com.google.dagger.hilt.android' version '2.41' apply false
id "com.diffplug.spotless" version "6.22.0" // apply false
Expand Down
2 changes: 1 addition & 1 deletion cartera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dependencies {
// https://github.com/WalletConnect/WalletConnectKotlinV2
//

implementation platform('com.walletconnect:android-bom:1.23.0')
implementation platform('com.walletconnect:android-bom:1.31.0')
implementation('com.walletconnect:android-core')
implementation 'com.walletconnect:sign'
//implementation 'com.walletconnect:push'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class WalletConnectV2Provider(

private var currentPairing: Core.Model.Pairing? = null

// expiry must be between current timestamp + MIN_INTERVAL and current timestamp + MAX_INTERVAL (MIN_INTERVAL: 300, MAX_INTERVAL: 604800)
private val requestExpiry: Long
get() = (System.currentTimeMillis() / 1000) + 400

private val dappDelegate = object : SignClient.DappDelegate {
override fun onSessionApproved(approvedSession: Sign.Model.ApprovedSession) {
// Triggered when Dapp receives the session approval from wallet
Expand Down Expand Up @@ -289,7 +293,6 @@ class WalletConnectV2Provider(

override fun disconnect() {
currentPairing?.let {
it
CoreClient.Pairing.disconnect(Core.Params.Disconnect(it.topic)) { error ->
Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString())
}
Expand All @@ -316,14 +319,15 @@ class WalletConnectV2Provider(
val namespace = currentSession?.namespace()
val chainId = currentSession?.chainId()
return if (sessionTopic != null && account != null && namespace != null && chainId != null) {
return Sign.Params.Request(
Sign.Params.Request(
sessionTopic = sessionTopic,
method = "personal_sign",
params = "[\"${message}\", \"${account}\"]",
chainId = "$namespace:$chainId",
expiry = requestExpiry,
)
} else {
return null
null
}
}

Expand All @@ -349,6 +353,7 @@ class WalletConnectV2Provider(
method = "eth_signTypedData",
params = "[\"${account}\", \"${message}\"]",
chainId = "$namespace:$chainId",
expiry = requestExpiry,
)
} else {
null
Expand All @@ -375,6 +380,7 @@ class WalletConnectV2Provider(
method = "eth_sendTransaction",
params = "[$message]",
chainId = "$namespace:$chainId",
expiry = requestExpiry,
)
} else {
null
Expand Down Expand Up @@ -414,18 +420,16 @@ class WalletConnectV2Provider(
val proposal = Sign.Model.Namespace.Proposal(chains, methods, events)
val requiredNamespaces: Map<String, Sign.Model.Namespace.Proposal> = mapOf(namespace to proposal) /*Required namespaces to setup a session*/
val optionalNamespaces: Map<String, Sign.Model.Namespace.Proposal> = emptyMap() /*Optional namespaces to setup a session*/
//
// val pairing: Core.Model.Pairing?
// val pairings = CoreClient.Pairing.getPairings()
// if (pairings.isNotEmpty()) {
// pairing = pairings.first()
// } else {
// pairing = CoreClient.Pairing.create() { error ->
// Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString())
// }!!
// }

val pairing = CoreClient.Pairing.create()

val pairing: Core.Model.Pairing?
val pairings = CoreClient.Pairing.getPairings()
if (pairings.isNotEmpty()) {
pairing = pairings.first()
} else {
pairing = CoreClient.Pairing.create() { error ->
Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString())
}
}

val expiry = (System.currentTimeMillis() / 1000) + TimeUnit.SECONDS.convert(7, TimeUnit.DAYS)
val properties: Map<String, String> = mapOf("sessionExpiry" to "$expiry")
Expand Down Expand Up @@ -473,14 +477,20 @@ class WalletConnectV2Provider(

val params = requestParams()
if (params != null) {
reallyMakeRequest(request, params) { result, error ->
completion(result, error)
reallyMakeRequest(request, params) { result, requestError ->
CoroutineScope(Dispatchers.Main).launch {
completion(result, requestError)
}
}
} else {
completion(null, WalletError(CarteraErrorCode.INVALID_SESSION))
CoroutineScope(Dispatchers.Main).launch {
completion(null, WalletError(CarteraErrorCode.INVALID_SESSION))
}
}
} else {
completion(null, WalletError(CarteraErrorCode.INVALID_SESSION))
CoroutineScope(Dispatchers.Main).launch {
completion(null, WalletError(CarteraErrorCode.INVALID_SESSION))
}
}
}
}
Expand All @@ -492,9 +502,9 @@ class WalletConnectV2Provider(
) {
SignClient.request(
request = requestParams,
onSuccess = { request: Sign.Model.SentRequest ->
onSuccess = { sendRequest: Sign.Model.SentRequest ->
Log.d(tag(this@WalletConnectV2Provider), "Wallet request made.")
operationCompletions[request.sessionTopic] = completion
operationCompletions[sendRequest.sessionTopic] = completion
},
onError = { error ->
Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString())
Expand Down Expand Up @@ -537,14 +547,14 @@ class WalletConnectV2Provider(
Log.d(tag(this@WalletConnectV2Provider), "Wallet is null")
return
}
if (pairing?.uri == null) {
if (pairing == null) {
Log.d(tag(this@WalletConnectV2Provider), "Pairing is null")
return
}
// val deeplinkPairingUri = it.replace("wc:", "wc://")
val url = WalletConnectUtils.createUrl(
wallet = request.wallet,
deeplink = pairing?.uri,
deeplink = pairing.uri,
type = WalletConnectionType.WalletConnectV2,
context = request.context,
)
Expand Down Expand Up @@ -606,7 +616,7 @@ private fun EthereumTransactionRequest.toJsonRequest(): String? {
val filtered = request.filterValues { it != null }

return try {
JSONObject(filtered as Map<*, *>?).toString()
JSONObject(filtered).toString()
} catch (e: JSONException) {
null
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ android.nonTransitiveRClass=true

LIBRARY_GROUP=dydxprotocol
LIBRARY_ARTIFACT_ID=cartera-android
LIBRARY_VERSION_NAME=0.1.13
LIBRARY_VERSION_NAME=0.1.14

android.enableR8.fullMode = false
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jun 02 11:57:06 PDT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit c885b0f

Please sign in to comment.