Skip to content

Commit

Permalink
Merge pull request #151 from MetaMask/handle-complex-requests
Browse files Browse the repository at this point in the history
fix: handle complex request types
  • Loading branch information
elefantel authored May 24, 2024
2 parents 5bff8f8 + 64067ab commit 94b22f6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Example/metamask-ios-sdk/ConnectView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ struct ConnectView: View {
showProgressView = false

switch result {
case .success(_):
status = "Online"
case let .failure(error):
errorMessage = error.localizedDescription
showError = true
default:
break
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Alternatively, you can add the URL directly in your project's package file:
dependencies: [
.package(
url: "https://github.com/MetaMask/metamask-ios-sdk",
from: "0.6.3"
from: "0.6.4"
)
]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,36 @@ extension String {
return unescapedString
}
}

func json(from value: Any) -> String? {
// Recursive function to decode nested JSON strings
func decodeNestedJson(_ value: Any) -> Any {
if let arrayValue = value as? [Any] {
// If it's an array, recursively decode each element
return arrayValue.map { decodeNestedJson($0) }
} else if let dictValue = value as? [String: Any] {
// If it's a dictionary, recursively decode each value
var decodedDict = [String: Any]()
for (key, value) in dictValue {
decodedDict[key] = decodeNestedJson(value)
}
return decodedDict
} else {
// If it's neither a string, array, nor dictionary, return the value as is
return value
}
}

// Decode any nested JSON strings recursively in the input dictionary
let decodedJsonObject = decodeNestedJson(value)

// Step 3: Convert the cleaned dictionary back to a JSON string
guard let cleanedJsonData = try? JSONSerialization.data(withJSONObject: decodedJsonObject, options: []),
let cleanedJsonString = String(data: cleanedJsonData, encoding: .utf8) else {
Logging.error("Failed to serialize cleaned JSON dictionary")
return nil
}

return cleanedJsonString
}

12 changes: 3 additions & 9 deletions Sources/metamask-ios-sdk/Classes/Ethereum/Ethereum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ public class Ethereum {
"params": connectWithParams
]

let connectWithData = try JSONSerialization.data(withJSONObject: connectWithDict)

let connectWithJson = String(data: connectWithData, encoding: .utf8)?.trimEscapingChars() ?? ""
let connectWithJson = json(from: connectWithDict) ?? ""

commClient.connect(with: connectWithJson)
} catch {
Expand Down Expand Up @@ -409,10 +407,7 @@ public class Ethereum {
"params": params
]

let requestData = try JSONSerialization.data(withJSONObject: requestDict)


let requestJson = String(data: requestData, encoding: .utf8)?.trimEscapingChars() ?? ""
let requestJson = json(from: requestDict) ?? ""

commClient.sendMessage(requestJson, encrypt: true)
} catch {
Expand Down Expand Up @@ -441,8 +436,7 @@ public class Ethereum {
"params": params
]

let jsonData = try JSONSerialization.data(withJSONObject: requestDict)
let requestJson = String(data: jsonData, encoding: .utf8)?.trimEscapingChars() ?? ""
let requestJson = json(from: requestDict) ?? ""

commClient.sendMessage(requestJson, encrypt: true)
} catch {
Expand Down
2 changes: 1 addition & 1 deletion metamask-ios-sdk.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'metamask-ios-sdk'
s.version = '0.6.3'
s.version = '0.6.4'
s.summary = 'Enable users to easily connect with their MetaMask Mobile wallet.'
s.swift_version = '5.5'

Expand Down

0 comments on commit 94b22f6

Please sign in to comment.