Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example app to use WalletConnect v2 #166

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions XMTPiOSExample/XMTPiOSExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
A6AE5191297B625F006FDD0F /* XMTP in Frameworks */ = {isa = PBXBuildFile; productRef = A6AE5190297B625F006FDD0F /* XMTP */; };
A6AE5192297B6270006FDD0F /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = A65F0703297B5D4E00C3C76E /* Persistence.swift */; };
A6AE5194297B62C8006FDD0F /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = A6AE5193297B62C8006FDD0F /* KeychainAccess */; };
A6C0F37B2AC1E321008C6AA7 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = A6C0F37A2AC1E321008C6AA7 /* Starscream */; };
A6C0F37E2AC1E34F008C6AA7 /* WalletConnect in Frameworks */ = {isa = PBXBuildFile; productRef = A6C0F37D2AC1E34F008C6AA7 /* WalletConnect */; };
A6C0F3802AC1E34F008C6AA7 /* WalletConnectModal in Frameworks */ = {isa = PBXBuildFile; productRef = A6C0F37F2AC1E34F008C6AA7 /* WalletConnectModal */; };
A6C0F3822AC1E34F008C6AA7 /* Web3Wallet in Frameworks */ = {isa = PBXBuildFile; productRef = A6C0F3812AC1E34F008C6AA7 /* Web3Wallet */; };
A6C0F3842AC1E4B5008C6AA7 /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6C0F3832AC1E4B5008C6AA7 /* LoginView.swift */; };
A6C0F3862AC1E549008C6AA7 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6C0F3852AC1E549008C6AA7 /* Data.swift */; };
A6D192D0293A7B97006B49F2 /* ConversationListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6D192CF293A7B97006B49F2 /* ConversationListView.swift */; };
A6E774162B154CF000F01DFF /* XMTP in Frameworks */ = {isa = PBXBuildFile; productRef = A6E774152B154CF000F01DFF /* XMTP */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -84,6 +90,8 @@
A6AE5180297B61AE006FDD0F /* NotificationService.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = NotificationService.appex; sourceTree = BUILT_PRODUCTS_DIR; };
A6AE518B297B61C8006FDD0F /* NotificationService.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NotificationService.entitlements; sourceTree = "<group>"; };
A6AE518C297B6210006FDD0F /* NotificationService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationService.swift; sourceTree = "<group>"; };
A6C0F3832AC1E4B5008C6AA7 /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
A6C0F3852AC1E549008C6AA7 /* Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
A6D192CF293A7B97006B49F2 /* ConversationListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationListView.swift; sourceTree = "<group>"; };
A6E774192B154D1E00F01DFF /* xmtp-ios */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "xmtp-ios"; path = ..; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand All @@ -95,8 +103,12 @@
files = (
A6E774162B154CF000F01DFF /* XMTP in Frameworks */,
A65F0707297B5E7600C3C76E /* WalletConnectSwift in Frameworks */,
A6C0F3802AC1E34F008C6AA7 /* WalletConnectModal in Frameworks */,
6AEE396E29F330CD0027B657 /* secp256k1 in Frameworks */,
A6C0F37E2AC1E34F008C6AA7 /* WalletConnect in Frameworks */,
A6C0F3822AC1E34F008C6AA7 /* Web3Wallet in Frameworks */,
A69F33C6292DC992005A5556 /* XMTP in Frameworks */,
A6C0F37B2AC1E321008C6AA7 /* Starscream in Frameworks */,
A65F070A297B5E8600C3C76E /* KeychainAccess in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -136,6 +148,7 @@
A6281991292DC825004B9117 /* XMTPiOSExample */ = {
isa = PBXGroup;
children = (
A6C0F3872AC1E54F008C6AA7 /* Extensions */,
A65F0703297B5D4E00C3C76E /* Persistence.swift */,
A687810529679B5B00042FAB /* Account */,
A69F33C8292DD54C005A5556 /* Views */,
Expand Down Expand Up @@ -186,6 +199,7 @@
A683860B293EA862006336FF /* MessageListView.swift */,
A6557A302941166E00CC4C7B /* MessageCellView.swift */,
A6557A3229411F4F00CC4C7B /* NewConversationView.swift */,
A6C0F3832AC1E4B5008C6AA7 /* LoginView.swift */,
);
path = Views;
sourceTree = "<group>";
Expand All @@ -199,6 +213,14 @@
path = NotificationService;
sourceTree = "<group>";
};
A6C0F3872AC1E54F008C6AA7 /* Extensions */ = {
isa = PBXGroup;
children = (
A6C0F3852AC1E549008C6AA7 /* Data.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -222,6 +244,10 @@
A65F0706297B5E7600C3C76E /* WalletConnectSwift */,
A65F0709297B5E8600C3C76E /* KeychainAccess */,
6AEE396D29F330CD0027B657 /* secp256k1 */,
A6C0F37A2AC1E321008C6AA7 /* Starscream */,
A6C0F37D2AC1E34F008C6AA7 /* WalletConnect */,
A6C0F37F2AC1E34F008C6AA7 /* WalletConnectModal */,
A6C0F3812AC1E34F008C6AA7 /* Web3Wallet */,
A6E774152B154CF000F01DFF /* XMTP */,
);
productName = XMTPiOSExample;
Expand Down Expand Up @@ -280,6 +306,8 @@
A65F0705297B5E7500C3C76E /* XCRemoteSwiftPackageReference "WalletConnectSwift" */,
A65F0708297B5E8600C3C76E /* XCRemoteSwiftPackageReference "KeychainAccess" */,
6AEE396C29F330CD0027B657 /* XCRemoteSwiftPackageReference "secp256k1.swift" */,
A6C0F3792AC1E321008C6AA7 /* XCRemoteSwiftPackageReference "Starscream" */,
A6C0F37C2AC1E34F008C6AA7 /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */,
);
productRefGroup = A6281990292DC825004B9117 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -316,6 +344,7 @@
buildActionMask = 2147483647;
files = (
A6557A3329411F4F00CC4C7B /* NewConversationView.swift in Sources */,
A6C0F3862AC1E549008C6AA7 /* Data.swift in Sources */,
A65F0704297B5D4E00C3C76E /* Persistence.swift in Sources */,
A687810729679BC700042FAB /* Account.swift in Sources */,
A6557A312941166E00CC4C7B /* MessageCellView.swift in Sources */,
Expand All @@ -325,6 +354,7 @@
A687810C29679BFC00042FAB /* WalletConnection.swift in Sources */,
A69F33CA292DD557005A5556 /* LoggedInView.swift in Sources */,
A69F33CC292DD568005A5556 /* QRCodeSheetView.swift in Sources */,
A6C0F3842AC1E4B5008C6AA7 /* LoginView.swift in Sources */,
A6281993292DC825004B9117 /* XMTPiOSExampleApp.swift in Sources */,
A67CCEC129355B4B00131F5C /* AccountManager.swift in Sources */,
A687810E29679C0D00042FAB /* WalletConnectionMethod.swift in Sources */,
Expand Down Expand Up @@ -658,6 +688,22 @@
kind = branch;
};
};
A6C0F3792AC1E321008C6AA7 /* XCRemoteSwiftPackageReference "Starscream" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/daltoniam/Starscream";
requirement = {
kind = exactVersion;
version = 3.1.2;
};
};
A6C0F37C2AC1E34F008C6AA7 /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/WalletConnect/WalletConnectSwiftV2";
requirement = {
branch = main;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand Down Expand Up @@ -689,6 +735,26 @@
package = A65F0708297B5E8600C3C76E /* XCRemoteSwiftPackageReference "KeychainAccess" */;
productName = KeychainAccess;
};
A6C0F37A2AC1E321008C6AA7 /* Starscream */ = {
isa = XCSwiftPackageProductDependency;
package = A6C0F3792AC1E321008C6AA7 /* XCRemoteSwiftPackageReference "Starscream" */;
productName = Starscream;
};
A6C0F37D2AC1E34F008C6AA7 /* WalletConnect */ = {
isa = XCSwiftPackageProductDependency;
package = A6C0F37C2AC1E34F008C6AA7 /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */;
productName = WalletConnect;
};
A6C0F37F2AC1E34F008C6AA7 /* WalletConnectModal */ = {
isa = XCSwiftPackageProductDependency;
package = A6C0F37C2AC1E34F008C6AA7 /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */;
productName = WalletConnectModal;
};
A6C0F3812AC1E34F008C6AA7 /* Web3Wallet */ = {
isa = XCSwiftPackageProductDependency;
package = A6C0F37C2AC1E34F008C6AA7 /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */;
productName = Web3Wallet;
};
A6E774152B154CF000F01DFF /* XMTP */ = {
isa = XCSwiftPackageProductDependency;
productName = XMTP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
"revision" : "ecb18d8ce4d88277cc4fb103973352d91e18c535"
}
},
{
"identity" : "qrcode",
"kind" : "remoteSourceControl",
"location" : "https://github.com/WalletConnect/QRCode",
"state" : {
"revision" : "263f280d2c8144adfb0b6676109846cfc8dd552b",
"version" : "14.3.1"
}
},
{
"identity" : "secp256k1.swift",
"kind" : "remoteSourceControl",
Expand All @@ -63,6 +72,15 @@
"version" : "0.10.0"
}
},
{
"identity" : "starscream",
"kind" : "remoteSourceControl",
"location" : "https://github.com/daltoniam/Starscream",
"state" : {
"revision" : "a063fda2b8145a231953c20e7a646be254365396",
"version" : "3.1.2"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -171,6 +189,24 @@
"version" : "1.25.1"
}
},
{
"identity" : "swift-qrcode-generator",
"kind" : "remoteSourceControl",
"location" : "https://github.com/dagronf/swift-qrcode-generator",
"state" : {
"revision" : "5ca09b6a2ad190f94aa3d6ddef45b187f8c0343b",
"version" : "1.0.3"
}
},
{
"identity" : "swiftimagereadwrite",
"kind" : "remoteSourceControl",
"location" : "https://github.com/dagronf/SwiftImageReadWrite",
"state" : {
"revision" : "5596407d1cf61b953b8e658fa8636a471df3c509",
"version" : "1.1.6"
}
},
{
"identity" : "walletconnectswift",
"kind" : "remoteSourceControl",
Expand All @@ -180,6 +216,15 @@
"revision" : "9e4dfba34fb35336fd5da551285d7986ff536cb8"
}
},
{
"identity" : "walletconnectswiftv2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/WalletConnect/WalletConnectSwiftV2",
"state" : {
"branch" : "main",
"revision" : "13446a81e678e8eddc6ab506e85c522df0163f1f"
}
},
{
"identity" : "web3.swift",
"kind" : "remoteSourceControl",
Expand Down
67 changes: 16 additions & 51 deletions XMTPiOSExample/XMTPiOSExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,29 @@ struct ContentView: View {

@State private var isShowingQRCode = false
@State private var qrCodeImage: UIImage?
@State private var isConnectingWallet = false

@State private var client: Client?

var body: some View {
VStack {
switch status {
case .unknown:
Button("Connect Wallet", action: connectWallet)
Button("Generate Wallet", action: generateWallet)
Button("Connect Wallet") { isConnectingWallet = true }
.sheet(isPresented: $isConnectingWallet) {
LoginView(onConnected: { client in
do {
let keysData = try client.privateKeyBundle.serializedData()
Persistence().saveKeys(keysData)
self.status = .connected(client)
} catch {
print("Error setting up client: \(error)")
}

print("Got a client: \(client)")
})
}
Button("Generate Wallet") { generateWallet() }
case .connecting:
ProgressView("Connecting…")
case let .connected(client):
Expand All @@ -52,55 +66,6 @@ struct ContentView: View {
}
}

func connectWallet() {
status = .connecting

do {
switch try accountManager.account.preferredConnectionMethod() {
case let .qrCode(image):
qrCodeImage = image

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
self.isShowingQRCode = true
}
case let .redirect(url):
UIApplication.shared.open(url)
case let .manual(url):
print("Open \(url) in mobile safari")
}

Task {
do {
try await accountManager.account.connect()

for _ in 0 ... 30 {
if accountManager.account.isConnected {
let client = try await Client.create(account: accountManager.account)

let keysData = try client.privateKeyBundle.serializedData()
Persistence().saveKeys(keysData)

self.status = .connected(client)
self.isShowingQRCode = false
return
}

try await Task.sleep(for: .seconds(1))
}

self.status = .error("Timed out waiting to connect (30 seconds)")
} catch {
await MainActor.run {
self.status = .error("Error connecting: \(error)")
self.isShowingQRCode = false
}
}
}
} catch {
status = .error("No acceptable connection methods found \(error)")
}
}

func generateWallet() {
Task {
do {
Expand Down
42 changes: 42 additions & 0 deletions XMTPiOSExample/XMTPiOSExample/Extensions/Data.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Data.swift
// XMTPChat
//
// Created by Pat Nakajima on 9/20/23.
//

import Foundation

extension Data {
static func value(of nibble: UInt8) -> UInt8? {
guard let letter = String(bytes: [nibble], encoding: .ascii) else { return nil }
return UInt8(letter, radix: 16)
}

public init(hexString hex: String) {
var data = Data()
let string = hex.hasPrefix("0x") ? String(hex.dropFirst(2)) : hex

// Convert the string to bytes for better performance
guard
let stringData = string.data(using: .ascii, allowLossyConversion: true)
else {
self = data
return
}

let stringBytes = Array(stringData)
for idx in stride(from: 0, to: stringBytes.count, by: 2) {
guard let high = Data.value(of: stringBytes[idx]) else {
data.removeAll()
break
}
if idx < stringBytes.count - 1, let low = Data.value(of: stringBytes[idx + 1]) {
data.append((high << 4) | low)
} else {
data.append(high)
}
}
self = data
}
}
Loading