Skip to content

Commit

Permalink
Merge pull request #1444 from WalletConnect/link_mode/remove_callback…
Browse files Browse the repository at this point in the history
…_on_session_atuthenticate

Link mode/remove authenticate callback
  • Loading branch information
jakubuid authored Jul 15, 2024
2 parents 26d731c + 82415bd commit c216c44
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ object Web3Modal {
fun authenticate(
authenticate: Modal.Params.Authenticate,
walletAppLink: String? = null,
onSuccess: (String?) -> Unit,
onSuccess: (String) -> Unit,
onError: (Modal.Model.Error) -> Unit,
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal class Web3ModalEngine(
name: String, method: String,
authenticate: Modal.Params.Authenticate,
walletAppLink: String? = null,
onSuccess: (String?) -> Unit,
onSuccess: (String) -> Unit,
onError: (Throwable) -> Unit
) {
connectionEventRepository.saveEvent(name, method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ internal class ConnectViewModel : ViewModel(), Navigator by NavigatorImpl(), Par
name, method,
walletAppLink = linkMode,
authParams = if (Web3Modal.selectedChain != null) Web3Modal.authPayloadParams!!.copy(chains = listOf(Web3Modal.selectedChain!!.id)) else Web3Modal.authPayloadParams!!,
onSuccess = { if (!it.isNullOrBlank()) onSuccess(it) },
onSuccess = { onSuccess(it) },
onError = {
sendEventUseCase.send(Props(EventType.TRACK, EventType.Track.CONNECT_ERROR, Properties(message = it.message ?: "Relay error while connecting")))
showError(it.localizedMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal interface ParingController {
name: String, method: String,
authParams: Modal.Model.AuthPayloadParams,
walletAppLink: String? = null,
onSuccess: (String?) -> Unit,
onSuccess: (String) -> Unit,
onError: (Throwable) -> Unit
)

Expand Down Expand Up @@ -66,7 +66,7 @@ internal class PairingControllerImpl : ParingController {
method: String,
authParams: Modal.Model.AuthPayloadParams,
walletAppLink: String?,
onSuccess: (String?) -> Unit,
onSuccess: (String) -> Unit,
onError: (Throwable) -> Unit
) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun dappClientAuthenticateLinkMode(onPairing: (String) -> Unit) {
"https://web3modal-laboratory-git-chore-kotlin-assetlinks-walletconnect1.vercel.app/wallet",
onSuccess = { pairingUrl ->
Timber.d("DappClient: on sent authenticate success: $pairingUrl")
onPairing(pairingUrl ?: "")
onPairing(pairingUrl)
},
onError = ::globalOnError
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ interface SignInterface {
onError: (Sign.Model.Error) -> Unit,
)

fun authenticate(authenticate: Sign.Params.Authenticate, walletAppLink: String?, onSuccess: (String?) -> Unit, onError: (Sign.Model.Error) -> Unit)

@Deprecated(
"The onSuccess callback has been replaced with a new callback that returns optional Pairing URL",
replaceWith = ReplaceWith("fun authenticate(authenticate: Sign.Params.Authenticate, val walletAppLink: String?, onSuccess: (String?) -> Unit, onError: (Sign.Model.Error) -> Unit)")
)
fun authenticate(authenticate: Sign.Params.Authenticate, onSuccess: (String) -> Unit, onError: (Sign.Model.Error) -> Unit)

fun authenticate(authenticate: Sign.Params.Authenticate, walletAppLink: String? = null, onSuccess: (String) -> Unit, onError: (Sign.Model.Error) -> Unit)
fun dispatchEnvelope(urlWithEnvelope: String, onError: (Sign.Model.Error) -> Unit)

@Deprecated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.walletconnect.sign.di.signJsonRpcModule
import com.walletconnect.sign.di.storageModule
import com.walletconnect.sign.engine.domain.SignEngine
import com.walletconnect.sign.engine.model.EngineDO
import com.walletconnect.util.Empty
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -137,7 +136,7 @@ class SignProtocol(private val koinApp: KoinApplication = wcKoinApp) : SignInter
override fun authenticate(
authenticate: Sign.Params.Authenticate,
walletAppLink: String?,
onSuccess: (String?) -> Unit,
onSuccess: (String) -> Unit,
onError: (Sign.Model.Error) -> Unit,
) {
checkEngineInitialization()
Expand Down Expand Up @@ -499,31 +498,6 @@ class SignProtocol(private val koinApp: KoinApplication = wcKoinApp) : SignInter
}
}

@Deprecated(
"The onSuccess callback has been replaced with a new callback that returns optional Pairing URL",
replaceWith = ReplaceWith("fun authenticate(authenticate: Sign.Params.Authenticate, val walletAppLink: String?, onSuccess: (String?) -> Unit, onError: (Sign.Model.Error) -> Unit)")
)
@Throws(IllegalStateException::class)
override fun authenticate(
authenticate: Sign.Params.Authenticate,
onSuccess: (String) -> Unit,
onError: (Sign.Model.Error) -> Unit,
) {
checkEngineInitialization()
scope.launch {
try {
signEngine.authenticate(authenticate.toAuthenticate(),
authenticate.methods, authenticate.pairingTopic,
if (authenticate.expiry == null) null else Expiry(authenticate.expiry),
null,
onSuccess = { url -> onSuccess(url ?: String.Empty) },
onFailure = { throwable -> onError(Sign.Model.Error(throwable)) })
} catch (error: Exception) {
onError(Sign.Model.Error(error))
}
}
}

@Deprecated(
"The onSuccess callback has been replaced with a new callback that returns Sign.Model.SentRequest",
replaceWith = ReplaceWith("this.request(request, onSuccessWithSentRequest, onError)", "com.walletconnect.sign.client")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal class SessionAuthenticateUseCase(
pairingTopic: String?,
expiry: Expiry?,
walletAppLink: String?,
onSuccess: (String?) -> Unit,
onSuccess: (String) -> Unit,
onFailure: (Throwable) -> Unit
) {
if (authenticate.chains.isEmpty()) {
Expand All @@ -75,7 +75,6 @@ internal class SessionAuthenticateUseCase(
}

val requestExpiry = expiry ?: Expiry(currentTimeInSeconds + oneHourInSeconds)
val pairing = getPairingForSessionAuthenticate(pairingTopic)
val optionalNamespaces = getNamespacesFromReCaps(authenticate.chains, if (methods.isNullOrEmpty()) listOf("personal_sign") else methods).toMapOfEngineNamespacesOptional()
val externalReCapsJson: String = getExternalReCapsJson(authenticate)
val signReCapsJson = getSignReCapsJson(methods, authenticate)
Expand All @@ -99,14 +98,14 @@ internal class SessionAuthenticateUseCase(
val authRequest: SignRpc.SessionAuthenticate = SignRpc.SessionAuthenticate(params = authParams)
crypto.setKey(requesterPublicKey, responseTopic.getParticipantTag())

if (!walletAppLink.isNullOrEmpty() && selfAppMetaData.redirect?.linkMode == true && linkModeStorageRepository.isEnabled(walletAppLink)) {
if (isLinkModeEnabled(walletAppLink)) {
try {
linkModeJsonRpcInteractor.triggerRequest(authRequest, appLink = walletAppLink, topic = Topic(generateUUID()), envelopeType = EnvelopeType.TWO)
onSuccess(null)
linkModeJsonRpcInteractor.triggerRequest(authRequest, appLink = walletAppLink!!, topic = Topic(generateUUID()), envelopeType = EnvelopeType.TWO)
} catch (e: Error) {
onFailure(e)
}
} else {
val pairing = getPairingForSessionAuthenticate(pairingTopic)
logger.log("Session authenticate subscribing on topic: $responseTopic")
jsonRpcInteractor.subscribe(
responseTopic,
Expand Down Expand Up @@ -140,6 +139,8 @@ internal class SessionAuthenticateUseCase(
}
}

private suspend fun isLinkModeEnabled(walletAppLink: String?) = !walletAppLink.isNullOrEmpty() && selfAppMetaData.redirect?.linkMode == true && linkModeStorageRepository.isEnabled(walletAppLink)

private fun getSignReCapsJson(methods: List<String>?, authenticate: EngineDO.Authenticate) =
if (!methods.isNullOrEmpty()) {
val namespace = SignValidator.getNamespaceKeyFromChainId(authenticate.chains.first())
Expand Down Expand Up @@ -228,7 +229,7 @@ internal interface SessionAuthenticateUseCaseInterface {
pairingTopic: String?,
expiry: Expiry?,
walletAppLink: String? = null,
onSuccess: (String?) -> Unit,
onSuccess: (String) -> Unit,
onFailure: (Throwable) -> Unit
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ internal class SessionRequestUseCase(
if (session.peerAppLink.isNullOrEmpty()) return@supervisorScope onFailure(IllegalStateException("App link is missing"))
try {
linkModeJsonRpcInteractor.triggerRequest(sessionPayload, Topic(request.topic), session.peerAppLink)
onSuccess(sessionPayload.id)
} catch (e: Exception) {
onFailure(e)
}
Expand Down
2 changes: 1 addition & 1 deletion sample/dapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ dependencies {
releaseImplementation(platform("com.walletconnect:android-bom:$BOM_VERSION"))
releaseImplementation("com.walletconnect:android-core")
releaseImplementation("com.walletconnect:walletconnect-modal")
releaseImplementation("com.walletconnect:android-core")
releaseImplementation("com.walletconnect:sign")
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,35 @@ class Web3WalletActivity : AppCompatActivity() {

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
handleAppLink(intent)

when {
intent?.dataString?.startsWith("kotlin-web3wallet:/wc") == true -> {
val uri = intent.dataString?.replace("kotlin-web3wallet:/wc", "kotlin-web3wallet://wc")
intent.setData(uri?.toUri())
if (intent?.dataString?.contains("wc_ev") == true) {
Web3Wallet.dispatchEnvelope(intent.dataString ?: "") {
lifecycleScope.launch(Dispatchers.Main) {
Toast.makeText(this@Web3WalletActivity, "Error dispatching envelope: ${it.throwable.message}", Toast.LENGTH_SHORT).show()
}
}
} else {
when {
intent?.dataString?.startsWith("kotlin-web3wallet:/wc") == true -> {
val uri = intent.dataString?.replace("kotlin-web3wallet:/wc", "kotlin-web3wallet://wc")
intent.setData(uri?.toUri())
}

intent?.dataString?.startsWith("wc:") == true -> {
val uri = "kotlin-web3wallet://wc?uri=" + URLEncoder.encode(intent.dataString, "UTF-8")
intent.setData(uri.toUri())
intent?.dataString?.startsWith("wc:") == true -> {
val uri = "kotlin-web3wallet://wc?uri=" + URLEncoder.encode(intent.dataString, "UTF-8")
intent.setData(uri.toUri())
}
}
}

if (intent?.dataString?.startsWith("kotlin-web3wallet://request") == true) {
web3walletViewModel.showRequestLoader(true)
}
if (intent?.dataString?.startsWith("kotlin-web3wallet://request") == true) {
web3walletViewModel.showRequestLoader(true)
}

if (intent?.dataString?.startsWith("kotlin-web3wallet://request") == false
&& intent.dataString?.contains("requestId") == false
) {
navController.handleDeepLink(intent)
if (intent?.dataString?.startsWith("kotlin-web3wallet://request") == false
&& intent.dataString?.contains("requestId") == false
) {
navController.handleDeepLink(intent)
}
}
}

Expand Down

0 comments on commit c216c44

Please sign in to comment.