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

Account integration #1378

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,48 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectModal"
BuildableName = "WalletConnectModal"
BlueprintName = "WalletConnectModal"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectNotify"
BuildableName = "WalletConnectNotify"
BlueprintName = "WalletConnectNotify"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "YttriumWrapper"
BuildableName = "YttriumWrapper"
BlueprintName = "YttriumWrapper"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down Expand Up @@ -606,6 +648,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "EventsTests"
BuildableName = "EventsTests"
BlueprintName = "EventsTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
1 change: 1 addition & 0 deletions Example/DApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import WalletConnectSign
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import WalletConnectSign
struct AccountDetails {
let chain: String
let methods: [String]
let account: String
let address: String
var account: String {
"\(chain):\(address)"
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class SessionAccountPresenter: ObservableObject {
}

func copyUri() {
UIPasteboard.general.string = sessionAccount.account
UIPasteboard.general.string = sessionAccount.address
}
}

Expand Down Expand Up @@ -128,17 +128,17 @@ extension SessionAccountPresenter.Errors: LocalizedError {
// MARK: - Transaction Stub
private enum Stub {
struct Transaction: Codable {
let from, to, data, gas: String
let from, to, data, gasLimit: String
let gasPrice, value, nonce: String
}

static let tx = [Transaction(from: "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
to: "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
data: "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
gas: "0x76c0",
gasPrice: "0x9184e72a000",
value: "0x9184e72a",
nonce: "0x117")]
data: "0x",
gasLimit: "0x5208",
gasPrice: "0x013e3d2ed4",
value: "0x00",
nonce: "0x09")]
static let eth_signTypedData = """
{
"types": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct SessionAccountView: View {
ScrollView {
VStack(spacing: 12) {
networkView(title: String(presenter.sessionAccount.chain.split(separator: ":").first ?? ""))
accountView(address: presenter.sessionAccount.account)
accountView(address: presenter.sessionAccount.address)
methodsView(methods: presenter.sessionAccount.methods)

Spacer()
Expand Down
9 changes: 8 additions & 1 deletion Example/DApp/Modules/Sign/SignPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ extension SignPresenter {
}
.store(in: &subscriptions)

Sign.instance.sessionSettlePublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] _ in
self.getSession()
}
.store(in: &subscriptions)

Sign.instance.authResponsePublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] response in
Expand Down Expand Up @@ -227,7 +234,7 @@ extension SignPresenter {
AccountDetails(
chain: account.blockchainIdentifier,
methods: Array(namespace.methods),
account: account.address
address: account.address
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion Example/DApp/Modules/Sign/SignView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct SignView: View {
.padding(12)
} else {
VStack {
ForEach(presenter.accountsDetails, id: \.chain) { account in
ForEach(presenter.accountsDetails, id: \.account) { account in
Button {
presenter.presentSessionAccount(sessionAccount: account)
} label: {
Expand Down
11 changes: 11 additions & 0 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
61DAC4AB2C65897800BAF3F8 /* YttriumWrapper in Frameworks */ = {isa = PBXBuildFile; productRef = 61DAC4AA2C65897800BAF3F8 /* YttriumWrapper */; };
767DC83528997F8E00080FA9 /* EthSendTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767DC83428997F8E00080FA9 /* EthSendTransaction.swift */; };
84310D05298BC980000C15B6 /* MainInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84310D04298BC980000C15B6 /* MainInteractor.swift */; };
8439CB89293F658E00F2F2E2 /* PushMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8439CB88293F658E00F2F2E2 /* PushMessage.swift */; };
Expand All @@ -27,6 +28,7 @@
84733CD32C1C2A4B001B2850 /* AlertPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84AEC2502B4D42C100E27A5B /* AlertPresenter.swift */; };
84733CD42C1C2C24001B2850 /* ProfilingService.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50B6A372B06697B00162B01 /* ProfilingService.swift */; };
84733CD52C1C2CEB001B2850 /* InputConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = C56EE25D293F56D6004840D1 /* InputConfig.swift */; };
84733CDA2C258BDB001B2850 /* AccountMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84733CD92C258BDB001B2850 /* AccountMock.swift */; };
847BD1D62989492500076C90 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BD1D12989492500076C90 /* MainViewController.swift */; };
847BD1D82989492500076C90 /* MainModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BD1D32989492500076C90 /* MainModule.swift */; };
847BD1D92989492500076C90 /* MainPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BD1D42989492500076C90 /* MainPresenter.swift */; };
Expand Down Expand Up @@ -408,6 +410,7 @@
846E35A32C0065B600E63DF4 /* ConfigPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigPresenter.swift; sourceTree = "<group>"; };
846E35A52C0065C100E63DF4 /* ConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigView.swift; sourceTree = "<group>"; };
846E35A72C006C5600E63DF4 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
84733CD92C258BDB001B2850 /* AccountMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountMock.swift; sourceTree = "<group>"; };
847BD1D12989492500076C90 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
847BD1D32989492500076C90 /* MainModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainModule.swift; sourceTree = "<group>"; };
847BD1D42989492500076C90 /* MainPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainPresenter.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -763,6 +766,7 @@
A59D25EE2AB3672700D7EA3A /* AsyncButton in Frameworks */,
C5133A78294125CC00A8314C /* Web3 in Frameworks */,
C5B2F7052970573D000DBA0E /* SolanaSwift in Frameworks */,
61DAC4AB2C65897800BAF3F8 /* YttriumWrapper in Frameworks */,
8487A9462A836C3F0003D5AF /* Sentry in Frameworks */,
C55D349929630D440004314A /* Web3Wallet in Frameworks */,
C56EE255293F569A004840D1 /* Starscream in Frameworks */,
Expand Down Expand Up @@ -1438,6 +1442,7 @@
isa = PBXGroup;
children = (
A5D610CC2AB3592F00C20083 /* ListingsSertice */,
84733CD92C258BDB001B2850 /* AccountMock.swift */,
);
path = BusinessLayer;
sourceTree = "<group>";
Expand Down Expand Up @@ -2068,6 +2073,7 @@
C54C248F2AEB1B5600DA4BF6 /* WalletConnectRouter */,
84AEC2532B4D43CD00E27A5B /* SwiftMessages */,
84F391F92BA87CEB00FDC20A /* Web3ModalUI */,
61DAC4AA2C65897800BAF3F8 /* YttriumWrapper */,
);
productName = ChatWallet;
productReference = C56EE21B293F55ED004840D1 /* WalletApp.app */;
Expand Down Expand Up @@ -2495,6 +2501,7 @@
C5B2F6F729705293000DBA0E /* SessionRequestRouter.swift in Sources */,
C56EE24F293F566D004840D1 /* WalletView.swift in Sources */,
C55D34B22965FB750004314A /* SessionProposalView.swift in Sources */,
84733CDA2C258BDB001B2850 /* AccountMock.swift in Sources */,
C56EE248293F566D004840D1 /* ScanQR.swift in Sources */,
847BD1EB298A87AB00076C90 /* SubscriptionsViewModel.swift in Sources */,
C55D349B2965BC2F0004314A /* TagsView.swift in Sources */,
Expand Down Expand Up @@ -3324,6 +3331,10 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
61DAC4AA2C65897800BAF3F8 /* YttriumWrapper */ = {
isa = XCSwiftPackageProductDependency;
productName = YttriumWrapper;
};
844749FC29B9E6B2005F520B /* WalletConnectNetworking */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectNetworking;
Expand Down
68 changes: 66 additions & 2 deletions Example/Shared/Signer/Signer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import Commons
import WalletConnectSign
import YttriumWrapper

final class Signer {
enum Errors: Error {
Expand All @@ -9,7 +10,39 @@ final class Signer {

private init() {}

static func sign(request: Request, importAccount: ImportAccount) throws -> AnyCodable {
static func sign(request: Request, importAccount: ImportAccount) async throws -> AnyCodable {
if try await didRequestSmartAccount(request) {
return try await signWithSmartAccount(request: request)
} else {
return try signWithEOA(request: request, importAccount: importAccount)
}
}


private static func didRequestSmartAccount(_ request: Request) async throws -> Bool {
// Attempt to decode params for transaction requests encapsulated in an array of dictionaries
if let paramsArray = try? request.params.get([AnyCodable].self),
let firstParam = paramsArray.first?.value as? [String: Any],
let account = firstParam["from"] as? String {
let smartAccountAddress = try await SmartAccount.instance.getAddress()
return account.lowercased() == smartAccountAddress.lowercased()
}

// Attempt to decode params for signing message requests
if let paramsArray = try? request.params.get([AnyCodable].self) {
if request.method == "personal_sign" || request.method == "eth_signTypedData" {
// Typically, the account address is the second parameter for personal_sign and eth_signTypedData
if paramsArray.count > 1,
let account = paramsArray[1].value as? String {
let smartAccountAddress = try await SmartAccount.instance.getAddress()
return account.lowercased() == smartAccountAddress.lowercased()
}
}
}

return false
}
private static func signWithEOA(request: Request, importAccount: ImportAccount) throws -> AnyCodable {
let signer = ETHSigner(importAccount: importAccount)

switch request.method {
Expand All @@ -24,11 +57,41 @@ final class Signer {

case "solana_signTransaction":
return SOLSigner.signTransaction(request.params)

default:
throw Signer.Errors.notImplemented
}
}


private static func signWithSmartAccount(request: Request) async throws -> AnyCodable {
switch request.method {
case "personal_sign":
let params = try request.params.get([String].self)
let message = params[0]
let signed = try SmartAccount.mockInstance.signMessage(message)
return AnyCodable(signed)

case "eth_signTypedData":
let params = try request.params.get([String].self)
let message = params[0]
let signed = try SmartAccount.mockInstance.signMessage(message)
return AnyCodable(signed)

case "eth_sendTransaction":
let params = try request.params.get([YttriumWrapper.Transaction].self)
let transaction = params[0]
let result = try await SmartAccount.mockInstance.sendTransaction(transaction)
return AnyCodable(result)

// case "wallet_sendCalls":
// let transactions =
// return try await SmartAccount.mockInstance.sendBatchTransaction(<#T##batch: [Transaction]##[Transaction]#>)

default:
throw Signer.Errors.notImplemented
}
}
}

extension Signer.Errors: LocalizedError {
Expand All @@ -38,3 +101,4 @@ extension Signer.Errors: LocalizedError {
}
}
}

1 change: 1 addition & 0 deletions Example/WalletApp/ApplicationLayer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
SmartAccount.configure(entryPoint: "", chainId: 11155111)
return true
}

Expand Down
Loading
Loading