Skip to content

Commit

Permalink
Merge pull request #368 from Outblock/develop
Browse files Browse the repository at this point in the history
fix: in-app-browser wc signature issue
  • Loading branch information
jaymengxy authored Oct 17, 2024
2 parents 236c977 + 705caf9 commit 2426c3e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,48 @@ fun Sign.Model.SessionProposal.approveSession() {
val namespaces = mutableMapOf<String, Sign.Model.Namespace.Session>()
val targetChain = if (isMainnet()) "eip155:747" else "eip155:545"
namespaces.putAll(requiredNamespaces.map { item ->
val caip2Namespace = item.key
val proposalNamespace = item.value
val chains = if (caip2Namespace.lowercase() == ETHEREUM_NETWORK) {
proposalNamespace.chains?.filter { it in supportedChain }.orEmpty()
} else {
proposalNamespace.chains.orEmpty()
}
val accounts = if (caip2Namespace.lowercase() == ETHEREUM_NETWORK) {
if (chains.contains(targetChain)) {
listOf("$targetChain:${getWalletAddress(caip2Namespace)}")
} else {
emptyList()
}
} else {
chains.map { "$it:${getWalletAddress(caip2Namespace)}" }
}
caip2Namespace to Sign.Model.Namespace.Session(
chains = chains,
accounts = accounts,
methods = proposalNamespace.methods.filter { it in WalletConnectMethod.getSupportedMethod() },
events = proposalNamespace.events
)
pair(item, targetChain)
}.toMap())
namespaces.putAll(optionalNamespaces.map { item ->
val caip2Namespace = item.key
val proposalNamespace = item.value
val chains = if (caip2Namespace.lowercase() == ETHEREUM_NETWORK) {
proposalNamespace.chains?.filter { it in supportedChain }.orEmpty()
} else {
proposalNamespace.chains.orEmpty()
}
val accounts = if (caip2Namespace.lowercase() == ETHEREUM_NETWORK) {
if (chains.contains(targetChain)) {
listOf("$targetChain:${getWalletAddress(caip2Namespace)}")
} else {
emptyList()
}
} else {
chains.map { "$it:${getWalletAddress(caip2Namespace)}" }
}
caip2Namespace to Sign.Model.Namespace.Session(
chains = chains,
accounts = accounts,
methods = proposalNamespace.methods.filter { it in WalletConnectMethod.getSupportedMethod() },
events = proposalNamespace.events
)
pair(item, targetChain)
}.toMap())
logd(TAG, "approveSession: $namespaces")
SignClient.approveSession(Sign.Params.Approve(proposerPublicKey, namespaces)) { error -> loge(error.throwable) }
}

private fun pair(
item: Map.Entry<String, Sign.Model.Namespace.Proposal>,
targetChain: String
): Pair<String, Sign.Model.Namespace.Session> {
val caip2Namespace = item.key
val proposalNamespace = item.value
val chains = if (caip2Namespace.lowercase() == ETHEREUM_NETWORK) {
proposalNamespace.chains?.filter { it in supportedChain }.orEmpty()
} else {
proposalNamespace.chains.orEmpty()
}
val accounts = if (caip2Namespace.lowercase() == ETHEREUM_NETWORK) {
if (chains.contains(targetChain)) {
listOf("$targetChain:${getWalletAddress(caip2Namespace)}")
} else {
emptyList()
}
} else {
chains.map { "$it:${getWalletAddress(caip2Namespace)}" }
}
val methods = if (caip2Namespace.lowercase() == ETHEREUM_NETWORK) {
WalletConnectMethod.getSupportedEVMMethod()
} else {
WalletConnectMethod.getSupportedFlowMethod()
}
return caip2Namespace to Sign.Model.Namespace.Session(
chains = chains,
accounts = accounts,
methods = methods,
events = proposalNamespace.events
)
}

fun Sign.Model.SessionProposal.network(): String? {
val chains = requiredNamespaces[nameTag()]?.chains
val reference = chains?.firstOrNull { it.contains(nameTag()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,41 @@ private suspend fun WCRequest.respondAuthz() {
private fun WCRequest.respondPreAuthz() {
val walletAddress = WalletManager.wallet()?.walletAddress() ?: return
val payerAddress = if (AppConfig.isFreeGas()) AppConfig.payer().address else walletAddress
val cryptoProvider = CryptoProviderManager.getCurrentCryptoProvider() ?: return
val keyId = FlowAddress(walletAddress).currentKeyId(cryptoProvider.getPublicKey())

val response = PollingResponse(
status = ResponseStatus.APPROVED,
data = PollingData(
fType = "PreAuthzResponse",
fVsn = "1.0.0",
proposer = Service(
identity = Identity(address = walletAddress, keyId = 0),
fType = "Service",
fVsn = "1.0.0",
type = "authz",
uid = "https://frw-link.lilico.app/wc",
identity = Identity(address = walletAddress, keyId = keyId),
method = "WC/RPC",
endpoint = WalletConnectMethod.AUTHZ.value,
),
payer = listOf(
Service(
identity = Identity(address = payerAddress, keyId = 0),
fType = "Service",
fVsn = "1.0.0",
type = "authz",
uid = "https://frw-link.lilico.app/wc",
identity = Identity(address = payerAddress, keyId = AppConfig.payer().keyId),
method = "WC/RPC",
endpoint = WalletConnectMethod.SIGN_PAYER.value,
)
),
authorization = listOf(
Service(
identity = Identity(address = walletAddress, keyId = 0),
fType = "Service",
fVsn = "1.0.0",
type = "authz",
uid = "https://frw-link.lilico.app/wc",
identity = Identity(address = walletAddress, keyId = keyId),
method = "WC/RPC",
endpoint = WalletConnectMethod.SIGN_PROPOSER.value,
)
Expand Down Expand Up @@ -347,6 +364,8 @@ private suspend fun WCRequest.respondSignPayer() {
val response = PollingResponse(
status = ResponseStatus.APPROVED,
data = PollingData(
fType = "CompositeSignature",
fVsn = "1.0.0",
address = sigs.address,
keyId = sigs.keyId,
signature = sigs.sig,
Expand All @@ -365,6 +384,7 @@ private suspend fun WCRequest.respondSignProposer() {
val signable = params.toSignables(gson())
val address = WalletManager.wallet()?.walletAddress() ?: return
val cryptoProvider = CryptoProviderManager.getCurrentCryptoProvider() ?: return
val keyId = FlowAddress(address).currentKeyId(cryptoProvider.getPublicKey())

val data = FclDialogModel(
title = metaData?.name,
Expand All @@ -388,8 +408,10 @@ private suspend fun WCRequest.respondSignProposer() {
val response = PollingResponse(
status = ResponseStatus.APPROVED,
data = PollingData(
fType = "CompositeSignature",
fVsn = "1.0.0",
address = address,
keyId = 0,
keyId = keyId,
signature = cryptoProvider.signData(signable?.message!!.hexToBytes())
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,22 @@ enum class WalletConnectMethod(val value: String) {
companion object {

@JvmStatic
fun getSupportedMethod(): Set<String> {
fun getAllMethod(): Set<String> {
return entries.map { it.value }.toSet()
}

@JvmStatic
fun getSupportedFlowMethod(): List<String> {
return listOf(AUTHN.value, AUTHZ.value, PRE_AUTHZ.value, SIGN_PAYER.value,
SIGN_PROPOSER.value, USER_SIGNATURE.value, ACCOUNT_PROOF.value)
}

@JvmStatic
fun getSupportedEVMMethod(): List<String> {
return listOf(EVM_SIGN_MESSAGE.value, EVM_SEND_TRANSACTION.value, EVM_SIGN_TYPED_DATA.value,
EVM_SIGN_TYPED_DATA_V3.value,
EVM_SIGN_TYPED_DATA_V4.value)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import com.flowfoundation.wallet.utils.extensions.dp2px
import com.flowfoundation.wallet.utils.logd
import com.flowfoundation.wallet.utils.safeRun
import com.flowfoundation.wallet.utils.uiScope
import com.flowfoundation.wallet.widgets.webview.*
import com.flowfoundation.wallet.widgets.webview.JS_FCL_EXTENSIONS
import com.flowfoundation.wallet.widgets.webview.JS_LISTEN_FLOW_WALLET_TRANSACTION
import com.flowfoundation.wallet.widgets.webview.JS_LISTEN_WINDOW_FCL_MESSAGE
import com.flowfoundation.wallet.widgets.webview.JS_QUERY_WINDOW_COLOR
import com.flowfoundation.wallet.widgets.webview.JsInterface
import com.flowfoundation.wallet.widgets.webview.evm.EvmInterface
import com.flowfoundation.wallet.widgets.webview.executeJs

@SuppressLint("SetJavaScriptEnabled")
class LilicoWebView : WebView {
Expand All @@ -39,6 +44,7 @@ class LilicoWebView : WebView {
webViewClient = WebViewClient()
webChromeClient = WebChromeClient()
domStorageEnabled = true
javaScriptCanOpenWindowsAutomatically = true
}
setWebContentsDebuggingEnabled(BuildConfig.DEBUG)

Expand Down Expand Up @@ -128,6 +134,8 @@ class LilicoWebView : WebView {
WalletConnect.get().pair(getWalletConnectUri(it).toString())
}
return true
} else if (it.toString() == "about:blank#blocked") {
return true
}
}
return super.shouldOverrideUrlLoading(view, request)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/dialog_fcl_sign_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@
android:paddingHorizontal="18dp"
android:paddingTop="5dp"
android:paddingBottom="13dp"
android:visibility="gone"
tools:visibility="visible">
android:visibility="visible">

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ android.injected.testOnly=false
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
vCode=156
vName=r2.5.5
vCode=158
vName=r2.5.6

0 comments on commit 2426c3e

Please sign in to comment.