Skip to content

Commit

Permalink
Merge pull request #43 from MetaMask/accounts-handling
Browse files Browse the repository at this point in the history
Accounts handling
  • Loading branch information
elefantel authored Oct 13, 2023
2 parents b71cb79 + c14469c commit 3145fa9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall

private fun handleMessage(message: String) {
val jsonString = keyExchange.decrypt(message)
Logger.log("CommunicationClient:: Got message: $jsonString")
val json = JSONObject(jsonString)

when (json.optString(MessageType.TYPE.value)) {
Expand All @@ -140,7 +139,6 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
val data = json.optString(MessageType.DATA.value)

if (data.isNotEmpty()) {
Logger.log("CommunicationClient:: Received data $json")
val dataJson = JSONObject(data)
val id = dataJson.optString(MessageType.ID.value)

Expand All @@ -150,7 +148,6 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
handleError(dataJson.optString(MessageType.ERROR.value), "")
sentOriginatorInfo = false // connection request rejected
} else {
Logger.log("CommunicationClient:: Received event $json")
handleEvent(dataJson)
}
} else {
Expand Down Expand Up @@ -184,15 +181,14 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
}

private fun handleResponse(id: String, data: JSONObject) {
Logger.log("CommunicationClient:: handleResponse $data")
val error = data.optString("error")

if (handleError(error, id)) {
return
}

val request = submittedRequests[id]?.request
Logger.log("CommunicationClient:: Response for request ${request?.method}")
Logger.log("CommunicationClient:: Response for rpc request ${request?.method}")
val isResultMethod = EthereumMethod.isResultMethod(request?.method ?: "")

if (!isResultMethod) {
Expand Down Expand Up @@ -314,7 +310,6 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
EthereumMethod.METAMASK_ACCOUNTS_CHANGED.value -> {
val accountsJson = event.optString("params")
val accounts: List<String> = Gson().fromJson(accountsJson, object : TypeToken<List<String>>() {}.type)

accounts.getOrNull(0)?.let { account ->
updateAccount(account)
}
Expand All @@ -334,13 +329,11 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
}

private fun updateAccount(account: String) {
Logger.log("CommunicationClient:: Received account changed event: $account")
val callback = ethereumEventCallbackRef.get()
callback?.updateAccount(account)
}

private fun updateChainId(chainId: String) {
Logger.log("CommunicationClient:: Received chain changed event: $chainId")
val callback = ethereumEventCallbackRef.get()
callback?.updateChainId(chainId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Ethereum @Inject constructor (private val repository: ApplicationRepositor
private var sessionDuration: Long = DEFAULT_SESSION_DURATION

override fun updateAccount(account: String) {
Logger.log("Ethereum:: Selected account changed")
Logger.log("Ethereum:: Selected account changed: $account")
_ethereumState.postValue(
_ethereumState.value?.copy(
selectedAddress = account
Expand Down Expand Up @@ -114,30 +114,9 @@ class Ethereum @Inject constructor (private val repository: ApplicationRepositor
private fun requestAccounts(callback: ((Any?) -> Unit)? = null) {
Logger.log("Ethereum:: Requesting ethereum accounts")

val providerStateRequest = EthereumRequest(
UUID.randomUUID().toString(),
EthereumMethod.GET_METAMASK_PROVIDER_STATE.value,
""
)
sendRequest(providerStateRequest) { result ->
if (result is RequestError) {
Logger.error("Ethereum:: Provider Connection request failed ${result.message}")
communicationClient?.trackEvent(Event.SDK_CONNECTION_FAILED, null)

if (result.code == ErrorType.USER_REJECTED_REQUEST.code) {
Logger.error("Ethereum:: Provider Connection request rejected")
communicationClient?.trackEvent(Event.SDK_CONNECTION_REJECTED, null)
}
} else {
communicationClient?.trackEvent(Event.SDK_CONNECTION_AUTHORIZED, null)
updateSessionDuration()
}
}

val accountsRequest = EthereumRequest(
UUID.randomUUID().toString(),
EthereumMethod.ETH_REQUEST_ACCOUNTS.value,
""
EthereumMethod.ETH_REQUEST_ACCOUNTS.value
)
sendRequest(accountsRequest) { result ->
if (result is RequestError) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.metamask.androidsdk

object SDKInfo {
const val VERSION = "0.1.0"
const val VERSION = "0.2.0"
const val PLATFORM = "android"
}

0 comments on commit 3145fa9

Please sign in to comment.