@novastera-oss/nitro-metamask 0.2.0 → 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.
package/NitroMetamask.podspec
CHANGED
package/android/build.gradle
CHANGED
|
@@ -126,6 +126,7 @@ android {
|
|
|
126
126
|
repositories {
|
|
127
127
|
mavenCentral()
|
|
128
128
|
google()
|
|
129
|
+
maven { url "https://jitpack.io" }
|
|
129
130
|
}
|
|
130
131
|
|
|
131
132
|
|
|
@@ -139,6 +140,8 @@ dependencies {
|
|
|
139
140
|
implementation project(":react-native-nitro-modules")
|
|
140
141
|
|
|
141
142
|
// MetaMask Android SDK
|
|
142
|
-
|
|
143
|
+
// See: https://github.com/MetaMask/metamask-android-sdk
|
|
144
|
+
// Available on Maven Central: https://mvnrepository.com/artifact/io.metamask.androidsdk/metamask-android-sdk
|
|
145
|
+
implementation "io.metamask.androidsdk:metamask-android-sdk:0.6.6"
|
|
143
146
|
}
|
|
144
147
|
|
|
@@ -1,47 +1,65 @@
|
|
|
1
1
|
package com.margelo.nitro.nitrometamask
|
|
2
2
|
|
|
3
|
-
import io.metamask.androidsdk.
|
|
4
|
-
import io.metamask.androidsdk.
|
|
5
|
-
import io.metamask.androidsdk.
|
|
6
|
-
import io.metamask.androidsdk.
|
|
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
|
|
9
|
+
private val ethereum by lazy {
|
|
10
|
+
Ethereum.getInstance()
|
|
11
|
+
}
|
|
10
12
|
|
|
11
13
|
override suspend fun connect(): ConnectResult {
|
|
12
|
-
val
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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 =
|
|
42
|
+
val address = ethereum.selectedAddress
|
|
24
43
|
?: throw IllegalStateException("No connected account. Call connect() first.")
|
|
25
44
|
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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 =
|
|
52
|
+
val result = ethereum.sendRequest(request)
|
|
38
53
|
return when (result) {
|
|
39
|
-
is
|
|
40
|
-
result.
|
|
54
|
+
is Result.Success.Item -> {
|
|
55
|
+
result.value as? String ?: throw IllegalStateException("Invalid signature response")
|
|
41
56
|
}
|
|
42
|
-
is
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
|
|
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(
|