@novastera-oss/nitro-metamask 0.2.1 → 0.2.2

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,47 +1,65 @@
1
1
  package com.margelo.nitro.nitrometamask
2
2
 
3
- import io.metamask.androidsdk.ConnectOptions
4
- import io.metamask.androidsdk.EthereumClient
5
- import io.metamask.androidsdk.Request
6
- import io.metamask.androidsdk.RequestResult
3
+ import io.metamask.androidsdk.Ethereum
4
+ import io.metamask.androidsdk.EthereumRequest
5
+ import io.metamask.androidsdk.EthereumMethod
6
+ import io.metamask.androidsdk.Result
7
7
 
8
8
  class HybridMetamaskConnector : HybridMetamaskConnectorSpec() {
9
- private val client by lazy { EthereumClient.getInstance() }
9
+ private val ethereum by lazy {
10
+ Ethereum.getInstance()
11
+ }
10
12
 
11
13
  override suspend fun connect(): ConnectResult {
12
- val response = client.connect(ConnectOptions())
13
- val address = response.accounts.firstOrNull()
14
- ?: throw IllegalStateException("MetaMask SDK returned no accounts")
15
-
16
- return ConnectResult(
17
- address = address,
18
- chainId = response.chainId.toString()
19
- )
14
+ val result = ethereum.connect()
15
+ return when (result) {
16
+ is Result.Success.Item -> {
17
+ // result.value contains the connection result
18
+ // Based on SDK docs, this should contain address and chainId
19
+ val connectionResult = result.value
20
+ // Extract address and chainId from the result
21
+ // The SDK returns account info in result.value
22
+ val address = ethereum.selectedAddress
23
+ ?: throw IllegalStateException("MetaMask SDK returned no address")
24
+ val chainId = ethereum.chainId
25
+ ?: throw IllegalStateException("MetaMask SDK returned no chainId")
26
+
27
+ ConnectResult(
28
+ address = address,
29
+ chainId = chainId.toString()
30
+ )
31
+ }
32
+ is Result.Error -> {
33
+ throw Exception(result.error.message ?: "Failed to connect to MetaMask")
34
+ }
35
+ else -> {
36
+ throw IllegalStateException("Unexpected result type from MetaMask connect")
37
+ }
38
+ }
20
39
  }
21
40
 
22
41
  override suspend fun signMessage(message: String): String {
23
- val address = client.getSelectedAddress()
42
+ val address = ethereum.selectedAddress
24
43
  ?: throw IllegalStateException("No connected account. Call connect() first.")
25
44
 
26
- // Convert message to hex-encoded format for personal_sign
27
- // personal_sign expects: personal_sign(messageHex, address)
28
- // where messageHex is "0x" + hex-encoded UTF-8 bytes
29
- val messageBytes = message.toByteArray(Charsets.UTF_8)
30
- val messageHex = "0x" + messageBytes.joinToString("") { "%02x".format(it) }
31
-
32
- val request = Request(
33
- method = "personal_sign",
34
- params = listOf(messageHex, address)
45
+ // Based on SDK documentation, personal_sign params are: [address, message]
46
+ // The SDK handles message encoding internally
47
+ val request = EthereumRequest(
48
+ method = EthereumMethod.PERSONAL_SIGN.value,
49
+ params = listOf(address, message)
35
50
  )
36
51
 
37
- val result = client.sendRequest(request)
52
+ val result = ethereum.sendRequest(request)
38
53
  return when (result) {
39
- is RequestResult.Success -> {
40
- result.data as? String ?: throw IllegalStateException("Invalid signature response")
54
+ is Result.Success.Item -> {
55
+ result.value as? String ?: throw IllegalStateException("Invalid signature response")
41
56
  }
42
- is RequestResult.Error -> {
57
+ is Result.Error -> {
43
58
  throw Exception(result.error.message ?: "Failed to sign message")
44
59
  }
60
+ else -> {
61
+ throw IllegalStateException("Unexpected result type from MetaMask signMessage")
62
+ }
45
63
  }
46
64
  }
47
65
  }
@@ -5,16 +5,18 @@ class HybridMetamaskConnector: HybridMetamaskConnectorSpec {
5
5
  private let sdk = MetaMaskSDK.shared
6
6
 
7
7
  func connect() async throws -> ConnectResult {
8
- try await withCheckedThrowingContinuation { continuation in
9
- sdk.connect { response in
10
- switch response {
11
- case .success(let account):
12
- let result = ConnectResult(address: account.address, chainId: "\(account.chainId)")
13
- continuation.resume(returning: result)
14
- case .failure(let error):
15
- continuation.resume(throwing: error)
16
- }
8
+ // Based on MetaMask iOS SDK docs: let connectResult = await metamaskSDK.connect()
9
+ let connectResult = await sdk.connect()
10
+
11
+ switch connectResult {
12
+ case .success(let value):
13
+ // After successful connection, get account info from SDK
14
+ guard let account = sdk.account else {
15
+ throw NSError(domain: "MetamaskConnector", code: -1, userInfo: [NSLocalizedDescriptionKey: "MetaMask SDK returned no account"])
17
16
  }
17
+ return ConnectResult(address: account.address, chainId: "\(account.chainId)")
18
+ case .failure(let error):
19
+ throw error
18
20
  }
19
21
  }
20
22
 
@@ -24,16 +26,10 @@ class HybridMetamaskConnector: HybridMetamaskConnectorSpec {
24
26
  throw NSError(domain: "MetamaskConnector", code: -1, userInfo: [NSLocalizedDescriptionKey: "No connected account. Call connect() first."])
25
27
  }
26
28
 
27
- // Convert message to hex-encoded format for personal_sign
28
- // personal_sign expects: personal_sign(messageHex, address)
29
- // where messageHex is "0x" + hex-encoded UTF-8 bytes
30
- // Reference: https://github.com/MetaMask/metamask-ios-sdk#5-connect-with-request
31
- guard let messageData = message.data(using: .utf8) else {
32
- throw NSError(domain: "MetamaskConnector", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to encode message"])
33
- }
34
-
35
- let messageHex = "0x" + messageData.map { String(format: "%02x", $0) }.joined()
36
- let params: [String] = [messageHex, account.address]
29
+ // Based on MetaMask iOS SDK docs, personal_sign params are: [account, message]
30
+ // The SDK handles message encoding internally
31
+ // Reference: https://github.com/MetaMask/metamask-ios-sdk
32
+ let params: [String] = [account.address, message]
37
33
 
38
34
  // Create EthereumRequest for personal_sign JSON-RPC method
39
35
  let request = EthereumRequest(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@novastera-oss/nitro-metamask",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "nitro-metamask",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",