@novastera-oss/nitro-metamask 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  package com.margelo.nitro.nitrometamask
2
2
 
3
3
  import com.margelo.nitro.core.Promise
4
- import com.margelo.nitro.core.NitroRuntime
5
4
  import io.metamask.androidsdk.Ethereum
6
5
  import io.metamask.androidsdk.Result
7
6
  import io.metamask.androidsdk.DappMetadata
@@ -14,10 +13,10 @@ import kotlin.coroutines.resume
14
13
  class HybridMetamaskConnector : HybridMetamaskConnectorSpec() {
15
14
  // Initialize Ethereum SDK with Context, DappMetadata, and SDKOptions
16
15
  // Based on: https://github.com/MetaMask/metamask-android-sdk
17
- // Using NitroRuntime for proper Nitro context access (survives reloads, no static leaks)
16
+ // Using MetamaskContextHolder for Context access (Nitro doesn't provide Context APIs)
17
+ // This pattern matches how other Nitro modules handle Context (VisionCamera, MMKV, etc.)
18
18
  private val ethereum: Ethereum by lazy {
19
- val context = NitroRuntime.applicationContext
20
- ?: throw IllegalStateException("Nitro application context not available")
19
+ val context = MetamaskContextHolder.get()
21
20
 
22
21
  val dappMetadata = DappMetadata(
23
22
  name = "Nitro MetaMask Connector",
@@ -56,12 +55,14 @@ class HybridMetamaskConnector : HybridMetamaskConnectorSpec() {
56
55
  ?: throw IllegalStateException("MetaMask SDK returned no chainId after connection")
57
56
 
58
57
  // Parse chainId from hex string (e.g., "0x1") or decimal string to number
58
+ // Nitro requires chainId to be Double (number in TS maps to Double in Kotlin)
59
59
  val chainId = try {
60
- if (chainIdString.startsWith("0x") || chainIdString.startsWith("0X")) {
60
+ val chainIdInt = if (chainIdString.startsWith("0x") || chainIdString.startsWith("0X")) {
61
61
  chainIdString.substring(2).toLong(16).toInt()
62
62
  } else {
63
63
  chainIdString.toLong().toInt()
64
64
  }
65
+ chainIdInt.toDouble()
65
66
  } catch (e: NumberFormatException) {
66
67
  throw IllegalStateException("Invalid chainId format: $chainIdString")
67
68
  }
@@ -0,0 +1,36 @@
1
+ package com.margelo.nitro.nitrometamask
2
+
3
+ import android.content.Context
4
+
5
+ /**
6
+ * Context holder for MetaMask SDK initialization.
7
+ *
8
+ * Nitro does not provide Android Context access, so we must manage it ourselves.
9
+ * This pattern is used by all Nitro modules that need Context (VisionCamera, MMKV, etc.)
10
+ *
11
+ * The context is initialized from NitroMetamaskPackage when React Native loads the module.
12
+ */
13
+ object MetamaskContextHolder {
14
+ @Volatile
15
+ private var appContext: Context? = null
16
+
17
+ /**
18
+ * Initialize the context holder with the React Native application context.
19
+ * This should be called once from NitroMetamaskPackage.getModule()
20
+ */
21
+ fun initialize(context: Context) {
22
+ appContext = context.applicationContext
23
+ }
24
+
25
+ /**
26
+ * Get the application context.
27
+ * Throws if not initialized - this ensures we fail fast if the package wasn't loaded correctly.
28
+ */
29
+ fun get(): Context {
30
+ return appContext
31
+ ?: throw IllegalStateException(
32
+ "MetamaskContextHolder not initialized. " +
33
+ "Make sure NitroMetamaskPackage is properly registered in your React Native app."
34
+ )
35
+ }
36
+ }
@@ -7,7 +7,10 @@ import com.facebook.react.BaseReactPackage
7
7
 
8
8
  class NitroMetamaskPackage : BaseReactPackage() {
9
9
  override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
10
- // No need to store ReactApplicationContext - HybridMetamaskConnector uses NitroRuntime
10
+ // Initialize MetamaskContextHolder with React Native application context
11
+ // This is the ONLY way to get Context in Nitro modules - Nitro doesn't provide Context APIs
12
+ // The context is stored in our own holder and accessed by HybridMetamaskConnector
13
+ MetamaskContextHolder.initialize(reactContext)
11
14
  return null
12
15
  }
13
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@novastera-oss/nitro-metamask",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "nitro-metamask",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",