Skip to content

Commit

Permalink
Merge pull request #82 from MetaMask/reconnection-handling
Browse files Browse the repository at this point in the history
Re-connection handling and response decoding fix
  • Loading branch information
elefantel authored Sep 11, 2023
2 parents f0e5748 + 68bcbf8 commit a496a75
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
12 changes: 11 additions & 1 deletion Example/metamask-ios-sdk/SignView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import metamask_ios_sdk
struct SignView: View {
@ObservedObject var ethereum: Ethereum = MetaMaskSDK.shared.ethereum

@State var message = "{\"domain\":{\"chainId\":1,\"name\":\"Ether Mail\",\"verifyingContract\":\"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC\",\"version\":\"1\"},\"message\":{\"contents\":\"Hello, Linda!\",\"from\":{\"name\":\"Aliko\",\"wallets\":[\"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826\",\"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF\"]},\"to\":[{\"name\":\"Linda\",\"wallets\":[\"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB\",\"0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57\",\"0xB0B0b0b0b0b0B000000000000000000000000000\"]}]},\"primaryType\":\"Mail\",\"types\":{\"EIP712Domain\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"},{\"name\":\"chainId\",\"type\":\"uint256\"},{\"name\":\"verifyingContract\",\"type\":\"address\"}],\"Group\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"members\",\"type\":\"Person[]\"}],\"Mail\":[{\"name\":\"from\",\"type\":\"Person\"},{\"name\":\"to\",\"type\":\"Person[]\"},{\"name\":\"contents\",\"type\":\"string\"}],\"Person\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"wallets\",\"type\":\"address[]\"}]}}"
@State var message = ""

@State private var cancellables: Set<AnyCancellable> = []

Expand Down Expand Up @@ -57,6 +57,16 @@ struct SignView: View {
}
}
}
.onAppear {
updateMessage()
}
.onChange(of: ethereum.chainId) { _ in
updateMessage()
}
}

func updateMessage() {
message = "{\"domain\":{\"chainId\":\"\(ethereum.chainId)\",\"name\":\"Ether Mail\",\"verifyingContract\":\"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC\",\"version\":\"1\"},\"message\":{\"contents\":\"Hello, Linda!\",\"from\":{\"name\":\"Aliko\",\"wallets\":[\"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826\",\"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF\"]},\"to\":[{\"name\":\"Linda\",\"wallets\":[\"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB\",\"0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57\",\"0xB0B0b0b0b0b0B000000000000000000000000000\"]}]},\"primaryType\":\"Mail\",\"types\":{\"EIP712Domain\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"},{\"name\":\"chainId\",\"type\":\"uint256\"},{\"name\":\"verifyingContract\",\"type\":\"address\"}],\"Group\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"members\",\"type\":\"Person[]\"}],\"Mail\":[{\"name\":\"from\",\"type\":\"Person\"},{\"name\":\"to\",\"type\":\"Person[]\"},{\"name\":\"contents\",\"type\":\"string\"}],\"Person\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"wallets\",\"type\":\"address[]\"}]}}"
}

func signInput() {
Expand Down
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/socketio/socket.io-client-swift",
"state" : {
"revision" : "af5ce97b755d964235348d96f6db5cbdcbe334a5",
"version" : "16.0.1"
"revision" : "175da8b5156f6b132436f0676cc84c2f6a766b6e",
"version" : "16.1.0"
}
},
{
"identity" : "starscream",
"kind" : "remoteSourceControl",
"location" : "https://github.com/daltoniam/Starscream",
"state" : {
"revision" : "df8d82047f6654d8e4b655d1b1525c64e1059d21",
"version" : "4.0.4"
"revision" : "ac6c0fc9da221873e01bd1a0d4818498a71eef33",
"version" : "4.0.6"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/socketio/socket.io-client-swift", .upToNextMajor(from: "16.0.1"))
.package(url: "https://github.com/socketio/socket.io-client-swift", .upToNextMajor(from: "16.1.0"))
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To add MetaMask iOS SDK as an SPM package to your project, in Xcode select: `Fil
dependencies: [
.package(
url: "https://github.com/MetaMask/metamask-ios-sdk",
from: "0.2.0"
from: "0.2.1"
)
]
```
Expand Down Expand Up @@ -196,8 +196,4 @@ To run the example project, clone this repository, change directory to `metamask
You will need to have MetaMask Mobile wallet installed on your target i.e physical device or simulator, so you can either have it installed from the [App Store](https://apps.apple.com/us/app/metamask-blockchain-wallet/id1438144202), or clone and compile MetaMask Mobile wallet from [source](https://github.com/MetaMask/metamask-mobile) and build to your target device.

## Requirements
### iOS
This SDK has an iOS minimum version requirement of 14.0. You need your app to have an iOS minimum deployment of no less than 14.0. We have not tested the SDK on beta versions of iOS 17 yet, it is possible that compatibility issues may be experienced therefore we advise against running it on iOS 17 for best results.

### Xcode
This SDK has been tested on up to version Xcode 14.3. Some compatibility issues may exist with beta versions of Xcode 15, therefore we advise against using Xcode 15 for best results.
This SDK has an iOS minimum version requirement of 14.0. You need your app to have an iOS minimum deployment of no less than 14.0.
54 changes: 36 additions & 18 deletions Sources/metamask-ios-sdk/Classes/Communication/SocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ class SocketClient: CommunicationClient {
handleDisconnection()
}

func resetClient() {
isConnected = false
self.keyExchange.reset()
tearDownConnection?()
}

func connect() {
guard !channel.isConnected else { return }

Expand Down Expand Up @@ -137,7 +131,6 @@ class SocketClient: CommunicationClient {

func clearSession() {
store.deleteData(for: SESSION_KEY)
resetClient()
setupClient()
}

Expand Down Expand Up @@ -229,10 +222,8 @@ private extension SocketClient {
let message = data.first as? [String: Any]
else { return }

if
let message = message["message"] as? [String: Any],
message["type"] as? String == KeyExchangeType.start.rawValue {
self.keyExchange.reset()
if !self.isValidMessage(message: message) {
return
}

if !self.keyExchange.keysExchanged {
Expand All @@ -244,6 +235,35 @@ private extension SocketClient {
}
}
}

func isValidMessage(message: [String: Any]) -> Bool {
if
let message = message["message"] as? [String: Any],
let type = message["type"] as? String {
if type == "ping" {
return false
}

if type.contains("key_handshake") {
return true
} else if !keyExchange.keysExchanged {
return false
}
}

return true
}

func isKeyExchangeMessage(_ message: [String: Any]) -> Bool {
if
let msg = message["message"] as? [String: Any],
let type = msg["type"] as? String,
type.contains("key_handshake") {
return true
}

return false
}

// MARK: Socket disconnected event

Expand All @@ -262,7 +282,6 @@ private extension SocketClient {
)

if !self.connectionPaused {
self.resetClient()
self.connectionPaused = true
}
}
Expand All @@ -286,6 +305,11 @@ private extension SocketClient {
}

func handleMessage(_ msg: [String: Any]) {
if isKeyExchangeMessage(msg) {
handleReceiveKeyExchange(msg)
return
}

guard let message = Message<String>.message(from: msg) else {
Logging.error("Could not parse message \(msg)")
return
Expand Down Expand Up @@ -376,8 +400,6 @@ extension SocketClient {

do {
let encryptedMessage: String = try self.keyExchange.encryptMessage(message)
// debug code
let data = try! JSONEncoder().encode(message)
let message: Message = .init(
id: self.channelId,
message: encryptedMessage
Expand All @@ -396,8 +418,6 @@ extension SocketClient {

do {
let encryptedMessage: String = try self.keyExchange.encryptMessage(message)
// debug code
let data = try! JSONEncoder().encode(message)
let message: Message = .init(
id: self.channelId,
message: encryptedMessage
Expand All @@ -411,7 +431,6 @@ extension SocketClient {
} else {
do {
let encryptedMessage: String = try self.keyExchange.encryptMessage(message)
let data = try! JSONEncoder().encode(message)
let message: Message = .init(
id: channelId,
message: encryptedMessage
Expand All @@ -423,7 +442,6 @@ extension SocketClient {
}
}
} else {
let data = try! JSONEncoder().encode(message)
let message = Message(
id: channelId,
message: message
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.2.0'
s.version = '0.2.1'
s.summary = 'Enable users to easily connect with their MetaMask Mobile wallet.'
s.swift_version = '5.0'

Expand Down

0 comments on commit a496a75

Please sign in to comment.