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

Support MetaMask #246

Merged
merged 3 commits into from
Jan 9, 2022
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
4 changes: 2 additions & 2 deletions NFTY.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@
CODE_SIGN_ENTITLEMENTS = NFTY/NFTY.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 16;
DEVELOPMENT_ASSET_PATHS = "NFTY/Preview\\ Content";
DEVELOPMENT_TEAM = C28QWE5379;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -1527,7 +1527,7 @@
CODE_SIGN_ENTITLEMENTS = NFTY/NFTY.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 16;
DEVELOPMENT_ASSET_PATHS = "NFTY/Preview\\ Content";
DEVELOPMENT_TEAM = C28QWE5379;
ENABLE_PREVIEWS = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<key>CreateAttributeScores.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
<integer>2</integer>
</dict>
<key>CreateRankingFiles.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>1</integer>
</dict>
<key>Demo (Playground) 1.xcscheme</key>
<dict>
Expand All @@ -38,7 +38,7 @@
<key>DownloadCollection.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>3</integer>
</dict>
<key>DownloadCryptoPunks.xcscheme_^#shared#^_</key>
<dict>
Expand Down
21 changes: 21 additions & 0 deletions NFTY/Assets.xcassets/RainbowWallet.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "RainbowWallet.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 35 additions & 9 deletions NFTY/ViewModel/UserWalletModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,43 @@ class UserWallet: ObservableObject {
}
}

private func getConnectionUrl(scheme: String,wcUrl:WCURL) throws -> String {

switch(scheme) {
case "metamask:":
// https://github.com/WalletConnect/WalletConnectSwift/issues/79#issuecomment-1007324661

let _encodeURL = wcUrl.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
let _end2 = _encodeURL.replacingOccurrences(of: "=", with: "%3D").replacingOccurrences(of: "&", with: "%26")

let metamaskLink = "https://metamask.app.link/wc?uri="
return "\(metamaskLink)\(_end2)"
case "rainbow:":
// https://github.com/WalletConnect/WalletConnectSwift/issues/79#issuecomment-1007324661

let _encodeURL = wcUrl.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
let _end2 = _encodeURL.replacingOccurrences(of: "=", with: "%3D").replacingOccurrences(of: "&", with: "%26")

let metamaskLink = "https://rnbwapp.com/wc?uri="
return "\(metamaskLink)\(_end2)"
default:
let uri = wcUrl.fullyPercentEncodedStr
var delimiter: String
if scheme.contains("http") {
delimiter = "/"
} else {
delimiter = "//"
}
let redirect = "www.nftygo.com".addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
return "\(scheme)\(delimiter)wc?uri=\(uri)&redirectUrl=\(redirect)"
}


}

func connectToWallet(scheme: String) throws -> Void {
let wcUrl = connect()
let uri = wcUrl.fullyPercentEncodedStr
var delimiter: String
if scheme.contains("http") {
delimiter = "/"
} else {
delimiter = "//"
}
let redirect = "www.nftygo.com".addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
let urlStr = "\(scheme)\(delimiter)wc?uri=\(uri)&redirectUrl=\(redirect)"
let urlStr = try! getConnectionUrl(scheme: scheme, wcUrl: wcUrl)
let url = URL(string: urlStr)!
// we need a delay so that WalletConnectClient can send handshake request
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000)) {
Expand Down
110 changes: 45 additions & 65 deletions NFTY/Views/ConnectWalletSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,82 +27,62 @@ struct UserWalletConnectorView : View {
.foregroundColor(.secondary)
}
case (.none,false),(.some,_):
HStack(spacing:30) {
HStack {
Spacer()

HStack {
VStack {
Text("Sign-in using")
.foregroundColor(.secondary)
.font(.subheadline)
.bold()

/*
Button(action:{
UIImpactFeedbackGenerator(style: .light)
.impactOccurred()

self.userWallet.removeWalletConnectSession()
self.isConnecting = true
let url = try! userWallet.connectToWallet(link:"metamask:")
// we need a delay so that WalletConnectClient can send handshake request
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000)) {
print("Launching=\(url)")
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}) {
VStack {
Image("Metamask")
.resizable()
.frame(width: 60,height:60)

Text("Sign-In with MetaMask")
.font(.caption)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.foregroundColor(Color.orange)
}
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.border(Color.orange)
.clipShape(RoundedRectangle(cornerRadius:20, style: .continuous))
.overlay(
RoundedRectangle(cornerRadius:20, style: .continuous)
.stroke(Color.orange, lineWidth: 1))
}

*/

Button(action:{
UIImpactFeedbackGenerator(style: .light)
.impactOccurred()
self.userWallet.removeWalletConnectSession()
self.isConnecting = true
try! userWallet.connectToWallet(scheme:"trust:")
}) {
VStack {
Image("TrustWallet")
.resizable()
.frame(width: 60,height:60)
HStack(spacing:20) {

let config = [
(title:"MetaMask",image:"Metamask",color:Color.orange,scheme:"metamask:"),
(title:"Trust Wallet",image:"TrustWallet",color:Color.blue,scheme:"trust:"),
(title:"Rainbow",image:"RainbowWallet",color:Color(red:200/255,green:230/255,blue:80/255),scheme:"rainbow:"),
];

ForEach(config.indices) { index in
let (title,image,color,scheme) = config[index];
Button(action:{
UIImpactFeedbackGenerator(style: .light)
.impactOccurred()
self.userWallet.removeWalletConnectSession()
self.isConnecting = true
try! userWallet.connectToWallet(scheme:scheme)
}) {
VStack {
Image(image)
.resizable()
.frame(width: 50,height:50)

Text(title)
.font(.caption)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.foregroundColor(color)
}
.padding()
.border(color)
.clipShape(RoundedRectangle(cornerRadius:20, style: .continuous))
.overlay(
RoundedRectangle(cornerRadius:20, style: .continuous)
.stroke(color, lineWidth: 3))
}

Text("Sign-In with Trust Wallet")
.font(.caption)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.foregroundColor(Color.blue)
}
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.border(Color.blue)
.clipShape(RoundedRectangle(cornerRadius:20, style: .continuous))
.overlay(
RoundedRectangle(cornerRadius:20, style: .continuous)
.stroke(Color.blue, lineWidth: 1))
}
}
Spacer()
}

switch(userWallet.signedIn) {
case false:
switch(userWallet.signedIn,userWallet.walletConnectSession?.walletInfo?.peerMeta.name) {
case (false,_),(_,.none):
EmptyView()
case true:
Text("Currently signed-in using Trust Wallet")
case (true,.some(let wallet)):
Text("Currently signed-in using \(wallet)")
.foregroundColor(.secondary)
.font(.caption)
.italic()
Expand Down