mirror of
https://github.com/pezkuwichain/pezkuwi-wallet-android.git
synced 2026-11-20 01:26:11 +00:00
Polkadot-js requests reach the wallet: bridge calls the registered interface (#35)
The WebView registers the polkadot-js interface as Pezkuwi_PolkadotJs, but the bridge script still called Nova_PolkadotJs. That call threw inside the page's message listener, so enable, account and signing requests never reached the app and every Substrate dApp waited forever with nothing on screen. Measured on a phone running 1.3.0: the request logged nothing on the native side. Both bridges now build their interface name from one function, and a test pins the bundled bridge script to the name the injector registers.
This commit is contained in:
1 parent
295a0df17d
commit
5c13c84eca
5 files changed
+28
-7
No files matched your search
+2
-1
@@ -7,6 +7,7 @@ import io.novafoundation.nova.feature_dapp_impl.web3.states.ExtensionsStore
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.Web3Injector
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.WebViewScriptInjector
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.WebViewWeb3JavaScriptInterface
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.jsInterfaceFullName
|
||||
|
||||
private const val JS_INTERFACE_NAME = "Metamask"
|
||||
|
||||
@@ -45,7 +46,7 @@ class MetamaskInjector(
|
||||
window.ethereum = new pezkuwi.Provider($providerConfigJson);
|
||||
window.web3 = new pezkuwi.Web3(window.ethereum);
|
||||
pezkuwi.postMessage = (jsonString) => {
|
||||
Pezkuwi_Metamask.onNewMessage(JSON.stringify(jsonString))
|
||||
${jsInterfaceFullName(JS_INTERFACE_NAME)}.onNewMessage(JSON.stringify(jsonString))
|
||||
};
|
||||
""".trimIndent()
|
||||
|
||||
|
||||
+2
-2
@@ -8,7 +8,7 @@ import io.novafoundation.nova.feature_dapp_impl.web3.webview.WebViewScriptInject
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.WebViewWeb3JavaScriptInterface
|
||||
|
||||
// should be in tact with javascript_interface_bridge.js
|
||||
private const val JS_INTERFACE_NAME = "PolkadotJs"
|
||||
internal const val POLKADOT_JS_INTERFACE_NAME = "PolkadotJs"
|
||||
|
||||
class PolkadotJsInjector(
|
||||
private val jsInterface: WebViewWeb3JavaScriptInterface,
|
||||
@@ -16,7 +16,7 @@ class PolkadotJsInjector(
|
||||
) : Web3Injector {
|
||||
|
||||
override fun initialInject(into: WebView) {
|
||||
webViewScriptInjector.injectJsInterface(into, jsInterface, JS_INTERFACE_NAME)
|
||||
webViewScriptInjector.injectJsInterface(into, jsInterface, POLKADOT_JS_INTERFACE_NAME)
|
||||
}
|
||||
|
||||
override fun injectForPage(into: WebView, extensionStore: ExtensionsStore) {
|
||||
|
||||
+4
-3
@@ -6,6 +6,9 @@ import io.novafoundation.nova.common.resources.ResourceManager
|
||||
|
||||
private const val JAVASCRIPT_INTERFACE_PREFIX = "Pezkuwi"
|
||||
|
||||
// The name a page's scripts call the native interface by; bundled bridge scripts must use this exact name.
|
||||
internal fun jsInterfaceFullName(interfaceName: String) = "${JAVASCRIPT_INTERFACE_PREFIX}_$interfaceName"
|
||||
|
||||
class WebViewScriptInjector(
|
||||
private val resourceManager: ResourceManager
|
||||
) {
|
||||
@@ -21,9 +24,7 @@ class WebViewScriptInjector(
|
||||
jsInterface: WebViewWeb3JavaScriptInterface,
|
||||
interfaceName: String
|
||||
) {
|
||||
val fullName = "${JAVASCRIPT_INTERFACE_PREFIX}_$interfaceName"
|
||||
|
||||
into.addJavascriptInterface(jsInterface, fullName)
|
||||
into.addJavascriptInterface(jsInterface, jsInterfaceFullName(interfaceName))
|
||||
}
|
||||
|
||||
fun injectScript(
|
||||
|
||||
@@ -9,6 +9,6 @@ window.addEventListener("message", ({ data, source }) => {
|
||||
|
||||
if (data.origin === "dapp-request") {
|
||||
// should be in tact with PolkadotJsExtension.kt
|
||||
Nova_PolkadotJs.onNewMessage(dataJson)
|
||||
Pezkuwi_PolkadotJs.onNewMessage(dataJson)
|
||||
}
|
||||
});
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package io.novafoundation.nova.feature_dapp_impl.web3.polkadotJs
|
||||
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.jsInterfaceFullName
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
class PolkadotJsBridgeTest {
|
||||
|
||||
// A bridge calling a name the WebView never registered throws inside the page's message listener,
|
||||
// so every polkadot-js request (enable, accounts, signing) is dropped without a word.
|
||||
@Test
|
||||
fun `bridge script calls the interface the injector registers`() {
|
||||
val bridge = File("src/main/res/raw/javascript_interface_bridge.js").readText()
|
||||
|
||||
val call = "${jsInterfaceFullName(POLKADOT_JS_INTERFACE_NAME)}.onNewMessage("
|
||||
assertTrue("bridge must call $call", bridge.contains(call))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user