Skip to content

Commit

Permalink
Merge pull request #372 from Planetable/walletconnect-v2-initial
Browse files Browse the repository at this point in the history
WalletConnectV2
  • Loading branch information
livid authored Jul 2, 2024
2 parents ed58121 + 00cda43 commit 7322e3d
Show file tree
Hide file tree
Showing 16 changed files with 951 additions and 99 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/insider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ jobs:

- name: Set insider SUFeedURL
run: /usr/libexec/PlistBuddy -c "Set :SUFeedURL https://opensource.planetable.xyz/planet-insider/appcast.xml" Planet/Info.plist

- name: Set WalletConnectV2 Project ID
run: /usr/libexec/PlistBuddy -c "Set :WALLETCONNECTV2_PROJECT_ID ${{ secrets.WALLETCONNECTV2_PROJECT_ID }}" Planet/Info.plist

- name: Set Etherscan API token
run: /usr/libexec/PlistBuddy -c "Set :ETHERSCAN_API_TOKEN ${{ secrets.ETHERSCAN_API_TOKEN }}" Planet/Info.plist

- name: Set insider icon
run: /usr/bin/sed -i '' 's/AppIcon/AppIcon-Insider/g' Planet/Release.xcconfig
Expand Down
25 changes: 24 additions & 1 deletion Planet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
2AA7974729496F730031E873 /* PFDashboardWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA7973B29496F730031E873 /* PFDashboardWindowController.swift */; };
2AA7974829496F730031E873 /* PlanetPublishedFolders+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA7973C29496F730031E873 /* PlanetPublishedFolders+Extension.swift */; };
2AA7974929496F730031E873 /* PFDashboardInspectorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA7973D29496F730031E873 /* PFDashboardInspectorViewController.swift */; };
2AAF510A2C293DE100C0B7AA /* HDWalletKit in Frameworks */ = {isa = PBXBuildFile; productRef = 2AAF51092C293DE100C0B7AA /* HDWalletKit */; };
2AB6A93B29703757007186A7 /* PlanetAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB6A93A29703757007186A7 /* PlanetAPI.swift */; };
2AB6A93E29703825007186A7 /* Swifter in Frameworks */ = {isa = PBXBuildFile; productRef = 2AB6A93D29703825007186A7 /* Swifter */; };
2AB6A94029707980007186A7 /* PlanetSettingsAPIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB6A93F29707980007186A7 /* PlanetSettingsAPIView.swift */; };
Expand Down Expand Up @@ -408,6 +409,8 @@
6A98E54C2BEDA2590008E3A4 /* PlanetSettingsGeneralView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AE44EAB28CD218200944786 /* PlanetSettingsGeneralView.swift */; };
6A98E54D2BEDA2910008E3A4 /* PlanetSettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AE44EAF28CD223C00944786 /* PlanetSettingsViewModel.swift */; };
6A9C211029792464005A815E /* AVKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A9C210F29792464005A815E /* AVKit.framework */; };
6A9C7BAF2C2B92100056BF22 /* WCTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A9C7BAE2C2B92100056BF22 /* WCTransaction.swift */; };
6A9C7BB02C2B92100056BF22 /* WCTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A9C7BAE2C2B92100056BF22 /* WCTransaction.swift */; };
6AA2C2022AFF538B00F6C633 /* MyArticleGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AA2C2012AFF538B00F6C633 /* MyArticleGridView.swift */; };
6AA2C2032AFF538B00F6C633 /* MyArticleGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AA2C2012AFF538B00F6C633 /* MyArticleGridView.swift */; };
6AABC6A7291A6216009FD13F /* WalletConnectV1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AABC6A6291A6216009FD13F /* WalletConnectV1.swift */; };
Expand Down Expand Up @@ -738,6 +741,7 @@
6A900192296C9F3800BC088E /* MyArticleSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyArticleSettingsView.swift; sourceTree = "<group>"; };
6A941CEC2AF5E7A300CA8261 /* PlanetStore+ServerInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PlanetStore+ServerInfo.swift"; sourceTree = "<group>"; };
6A9C210F29792464005A815E /* AVKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVKit.framework; path = System/Library/Frameworks/AVKit.framework; sourceTree = SDKROOT; };
6A9C7BAE2C2B92100056BF22 /* WCTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WCTransaction.swift; sourceTree = "<group>"; };
6AA2BC3227DC09AE00AC96B5 /* Planet v3.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Planet v3.xcdatamodel"; sourceTree = "<group>"; };
6AA2C2012AFF538B00F6C633 /* MyArticleGridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyArticleGridView.swift; sourceTree = "<group>"; };
6AABC6A6291A6216009FD13F /* WalletConnectV1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletConnectV1.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -869,6 +873,7 @@
6A7189692947FD0500279D77 /* Web3 in Frameworks */,
BBC831AB2836239300CE1F67 /* PlanetSiteTemplates in Frameworks */,
6AB6E50F291527B800F17328 /* WalletConnect in Frameworks */,
2AAF510A2C293DE100C0B7AA /* HDWalletKit in Frameworks */,
2A69F04727E1B53A00141DC0 /* FeedKit in Frameworks */,
2AC4996C27BB99DC00F1C2D1 /* Stencil in Frameworks */,
6A71896B2947FD0500279D77 /* Web3ContractABI in Frameworks */,
Expand Down Expand Up @@ -1457,6 +1462,7 @@
6ACF166429246BD800AE318B /* TipSelectView.swift */,
6ACF16682927267800AE318B /* WalletTransactionProgressView.swift */,
6AC33B922934159A001ABBCF /* EthereumTransaction.swift */,
6A9C7BAE2C2B92100056BF22 /* WCTransaction.swift */,
);
path = Wallet;
sourceTree = "<group>";
Expand Down Expand Up @@ -1653,6 +1659,7 @@
6AFCF7DB2B7353C6002299BA /* Collections */,
6AFCF7DD2B7353C6002299BA /* DequeModule */,
6AFCF7DF2B7353C6002299BA /* OrderedCollections */,
2AAF51092C293DE100C0B7AA /* HDWalletKit */,
);
productName = Planet;
productReference = 2AC4994527BB6E7500F1C2D1 /* Planet.app */;
Expand Down Expand Up @@ -1709,6 +1716,7 @@
6A2A4D2A2A86422700F4491A /* XCRemoteSwiftPackageReference "WrappingHStack" */,
2A27743F2A9514EE00A5DDC2 /* XCRemoteSwiftPackageReference "Zip" */,
6AFCF7DA2B7353C6002299BA /* XCRemoteSwiftPackageReference "swift-collections" */,
2AAF51082C293DE100C0B7AA /* XCRemoteSwiftPackageReference "HDWallet" */,
);
productRefGroup = 2AC4994627BB6E7500F1C2D1 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -1957,6 +1965,7 @@
2A95E6FD2A1B003D001288B8 /* MyPlanetEditView.swift in Sources */,
2A996AD62A1DA6C700BEF898 /* PlanetDownloadsViewModel.swift in Sources */,
2A466FD22A809E2C009FB646 /* IndicatorViews.swift in Sources */,
6A9C7BB02C2B92100056BF22 /* WCTransaction.swift in Sources */,
2A95E6772A19A3C4001288B8 /* URLUtils.swift in Sources */,
2ABD1CC42A1616DA0082D7EA /* AppDelegate.swift in Sources */,
2A8E085B2BF4B19B002D0C03 /* IPFSOpenWindow.swift in Sources */,
Expand Down Expand Up @@ -2046,6 +2055,7 @@
2AF4003927F2321F005DF1A9 /* WriterTextView.swift in Sources */,
6A6A1468282159DC009644B6 /* TemplateStore.swift in Sources */,
2AF9C5BC289263BC00327A8F /* Updater.swift in Sources */,
6A9C7BAF2C2B92100056BF22 /* WCTransaction.swift in Sources */,
6A2827D42BD8CD29006B4A84 /* QuickPostView.swift in Sources */,
6A18F2062A0F0FA700FC4050 /* RebuildProgressView.swift in Sources */,
2A2556BC293E1F53006462D8 /* TemplateBrowserInspectorView.swift in Sources */,
Expand Down Expand Up @@ -2571,6 +2581,14 @@
minimumVersion = 9.0.0;
};
};
2AAF51082C293DE100C0B7AA /* XCRemoteSwiftPackageReference "HDWallet" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/WalletConnect/HDWallet";
requirement = {
branch = develop;
kind = branch;
};
};
2AB6A93C29703825007186A7 /* XCRemoteSwiftPackageReference "swifter" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/httpswift/swifter";
Expand Down Expand Up @@ -2632,7 +2650,7 @@
repositoryURL = "https://github.com/WalletConnect/WalletConnectSwiftV2";
requirement = {
kind = exactVersion;
version = 1.8.6;
version = 1.9.8;
};
};
6ADF40C229151407007A24E8 /* XCRemoteSwiftPackageReference "Starscream" */ = {
Expand Down Expand Up @@ -2817,6 +2835,11 @@
package = 2AB6A93C29703825007186A7 /* XCRemoteSwiftPackageReference "swifter" */;
productName = Swifter;
};
2AAF51092C293DE100C0B7AA /* HDWalletKit */ = {
isa = XCSwiftPackageProductDependency;
package = 2AAF51082C293DE100C0B7AA /* XCRemoteSwiftPackageReference "HDWallet" */;
productName = HDWalletKit;
};
2AB6A93D29703825007186A7 /* Swifter */ = {
isa = XCSwiftPackageProductDependency;
package = 2AB6A93C29703825007186A7 /* XCRemoteSwiftPackageReference "swifter" */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "2d350f39146062b36abb00c558bb2f952938a1a96238113e0d424870f7d32ad3",
"originHash" : "04f5ec2c370ad854d1601a3ffab8475cd1d8774bd8d70858486dcfe0847a1e8e",
"pins" : [
{
"identity" : "bigint",
Expand Down Expand Up @@ -46,6 +46,15 @@
"version" : "9.1.2"
}
},
{
"identity" : "hdwallet",
"kind" : "remoteSourceControl",
"location" : "https://github.com/WalletConnect/HDWallet",
"state" : {
"branch" : "develop",
"revision" : "748a85b1dfe9a2fa592bd9266c5a926e4e1d3f44"
}
},
{
"identity" : "libcmark_gfm",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -231,8 +240,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/WalletConnect/WalletConnectSwiftV2",
"state" : {
"revision" : "b1b2486a82c5ff72e61cad89d7c0442d5e025562",
"version" : "1.8.6"
"revision" : "addf9a3688ef5e5d9d148ecbb30ca0fd3132b908",
"version" : "1.9.8"
}
},
{
Expand Down
20 changes: 14 additions & 6 deletions Planet/Helper/KeyboardShortcutHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,26 @@ class KeyboardShortcutHelper: ObservableObject {
}
}
else {
/* TODO: Remove this button for V1
Button {
WalletManager.shared.connectV1()
} label: {
Text("Connect Wallet")
}
}
*/

if PlanetStore.shared.walletConnectV2Ready {
Button {
WalletManager.shared.connectV2()
} label: {
Text("Connect Wallet V2")
if PlanetStore.shared.walletConnectV2Ready {
Button {
Task { @MainActor in
do {
try await WalletManager.shared.connectV2()
} catch {
debugPrint("failed to connect wallet v2: \(error)")
}
}
} label: {
Text("Connect Wallet V2")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Planet/Helper/ViewUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ extension Color {
)
}

func toHexString() -> String {
func toHexValue() -> String {
var components: (CGFloat, CGFloat, CGFloat, CGFloat) {
let c = NSColor(self).usingColorSpace(.deviceRGB)!

Expand Down
4 changes: 2 additions & 2 deletions Planet/IPFS/IPFSState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class IPFSState: ObservableObject {
}
}
self.refreshRateTimer = Timer.scheduledTimer(withTimeInterval: Self.refreshRate, repeats: true, block: { _ in
Task.detached(priority: .utility) {
Task.detached(priority: .userInitiated) {
await self.updateStatus()
}
})
self.refreshTrafficTimer = Timer.scheduledTimer(withTimeInterval: Self.refreshTrafficRate, repeats: true, block: { _ in
Task.detached(priority: .background) {
Task.detached(priority: .userInitiated) {
await self.updateTrafficStatus()
}
})
Expand Down
4 changes: 3 additions & 1 deletion Planet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@
</dict>
</array>
<key>WALLETCONNECTV2_ENABLED</key>
<false/>
<true/>
<key>WALLETCONNECTV2_PROJECT_ID</key>
<string>$(WALLETCONNECTV2_PROJECT_ID)</string>
<key>ETHERSCAN_API_TOKEN</key>
<string>$(ETHERSCAN_API_TOKEN)</string>
<key>NSSupportsSuddenTermination</key>
<false/>
</dict>
Expand Down
20 changes: 17 additions & 3 deletions Planet/Labs/Wallet/EthereumTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class EthereumTransaction: Codable, Identifiable {
to: String,
toENS: String? = nil,
amount: Int,
memo: String
memo: String,
created: Date? = nil
) {
self.id = id
self.chainID = chainID
Expand All @@ -60,7 +61,11 @@ class EthereumTransaction: Codable, Identifiable {
self.toENS = toENS
self.amount = amount
self.memo = memo
self.created = Date()
if let created = created {
self.created = created
} else {
self.created = Date()
}
}

static func from(path: URL) -> EthereumTransaction? {
Expand Down Expand Up @@ -96,14 +101,23 @@ class EthereumTransaction: Codable, Identifiable {
try JSONEncoder.shared.encode(self).write(to: txPath)
}

func exists() -> Bool {
let walletPath = Self.walletsInfoPath().appendingPathComponent(
from,
isDirectory: true
)
let txPath = walletPath.appendingPathComponent(id + ".json", isDirectory: false)
return FileManager.default.fileExists(atPath: txPath.path)
}

@ViewBuilder
func recipientView() -> some View {
if let ens = toENS {
Text(ens)
.font(.body)
}
else {
Text(to)
Text(to.shortWalletAddress())
.font(.footnote)
}
}
Expand Down
28 changes: 21 additions & 7 deletions Planet/Labs/Wallet/TipSelectView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ struct TipSelectView: View {
HStack {
Text("Please select the amount")
Spacer()
Picker(selection: $ethereumChainId, label: Text("")) {
ForEach(EthereumChainID.allCases, id: \.id) { value in
Text(
"\(EthereumChainID.names[value.rawValue] ?? "Unknown Chain ID \(value.rawValue)")"
)
.tag(value)
.frame(width: 120)
}
}
.pickerStyle(.menu)
.frame(width: 120)
/* TODO: Remove this V1 logic
if WalletManager.shared.canSwitchNetwork() {
Picker(selection: $ethereumChainId, label: Text("")) {
ForEach(EthereumChainID.allCases, id: \.id) { value in
Expand All @@ -70,6 +82,7 @@ struct TipSelectView: View {
.help("This transaction will be sent to \(name) network")
}
}
*/
}.padding(10)

GroupBox {
Expand Down Expand Up @@ -131,7 +144,8 @@ struct TipSelectView: View {
}

private func updateCurrentGasPrice() {
let web3 = Web3(rpcURL: "https://cloudflare-eth.com")
let chain = EthereumChainID(rawValue: ethereumChainId) ?? .mainnet
let web3 = Web3(rpcURL: chain.rpcURL)
web3.eth.gasPrice() { response in
if response.status.isSuccess, let gasPrice = response.result {
print("Gas price: \(gasPrice)")
Expand All @@ -154,11 +168,8 @@ struct TipSelectView: View {
}
let ethereumChainName = WalletManager.shared.currentNetworkName()
var walletAppString: String = ""
if let walletAppName = WalletManager.shared.walletConnect.session.walletInfo?.peerMeta.name {
walletAppString = walletAppName + " on"
} else {
walletAppString = "the wallet app on"
}
let walletAppName = WalletManager.shared.getWalletAppName()
walletAppString = walletAppName + " on"
let message: String
if let ens = ens {
message = "Sending \(tipAmountLabel) to **\(ens)** on \(ethereumChainName), please confirm from \(walletAppString) your phone"
Expand All @@ -170,7 +181,10 @@ struct TipSelectView: View {
PlanetStore.shared.walletTransactionProgressMessage = message
PlanetStore.shared.isShowingWalletTransactionProgress = true
}
WalletManager.shared.walletConnect.sendTransaction(receiver: receiver, amount: tipAmount, memo: memo, ens: ens)
// WalletManager.shared.walletConnect.sendTransaction(receiver: receiver, amount: tipAmount, memo: memo, ens: ens)
Task {
await WalletManager.shared.sendTransactionV2(receiver: receiver, amount: tipAmount, memo: memo, ens: ens, gas: currentGasPrice)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
PlanetStore.shared.isShowingWalletTransactionProgress = false
}
Expand Down
61 changes: 61 additions & 0 deletions Planet/Labs/Wallet/WCTransaction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// WCTransaction.swift
// Planet
//
// Created by Xin Liu on 6/25/24.
//

import Foundation

/// https://docs.walletconnect.org/json-rpc-api-methods/ethereum#parameters-4
public struct WCTransaction: Codable {
public var from: String
public var to: String?
public var data: String
public var gas: String?
public var gasPrice: String?
public var value: String?
public var nonce: String?
public var type: String?
public var accessList: [AccessListItem]?
public var chainId: String?
public var maxPriorityFeePerGas: String?
public var maxFeePerGas: String?

/// https://eips.ethereum.org/EIPS/eip-2930
public struct AccessListItem: Codable {
public var address: String
public var storageKeys: [String]

public init(address: String, storageKeys: [String]) {
self.address = address
self.storageKeys = storageKeys
}
}

public init(from: String,
to: String?,
data: String,
gas: String?,
gasPrice: String?,
value: String?,
nonce: String?,
type: String?,
accessList: [AccessListItem]?,
chainId: String?,
maxPriorityFeePerGas: String?,
maxFeePerGas: String?) {
self.from = from
self.to = to
self.data = data
self.gas = gas
self.gasPrice = gasPrice
self.value = value
self.nonce = nonce
self.type = type
self.accessList = accessList
self.chainId = chainId
self.maxPriorityFeePerGas = maxPriorityFeePerGas
self.maxFeePerGas = maxFeePerGas
}
}
Loading

0 comments on commit 7322e3d

Please sign in to comment.