Skip to content

Commit

Permalink
Web3Modal import
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Jun 8, 2023
1 parent 0af3bb6 commit 28f6812
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"state": {
"branch": null,
"revision": "eee9ad754926c40a0f7e73f152357d37b119b7fa",
"version": "1.7.1"
"revision": "19b3c3ceed117c5cc883517c4e658548315ba70b",
"version": "1.6.0"
}
},
{
Expand All @@ -33,8 +33,8 @@
"repositoryURL": "https://github.com/mxcl/PromiseKit.git",
"state": {
"branch": null,
"revision": "8a98e31a47854d3180882c8068cc4d9381bf382d",
"version": "6.22.1"
"revision": "43772616c46a44a9977e41924ae01d0e55f2f9ca",
"version": "6.18.1"
}
},
{
Expand Down Expand Up @@ -105,8 +105,8 @@
"repositoryURL": "https://github.com/bigearsenal/task-retrying-swift.git",
"state": {
"branch": null,
"revision": "1249b3524378423c848cef39fb220041e00a08ec",
"version": "1.0.4"
"revision": "645eaaf207a6f39ab4b469558d916ae23df199b5",
"version": "1.0.3"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Example/IntegrationTests/History/HistoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ final class HistoryTests: XCTestCase {

try await relayClient2.subscribe(topic: topic)
try await relayClient1.publish(topic: topic, payload: payload, tag: tag, prompt: false, ttl: 3000)
await fulfillment(of: [exp], timeout: InputConfig.defaultTimeout)

wait(for: [exp], timeout: InputConfig.defaultTimeout)

sleep(5) // History server has a queue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ private extension ImportAccount {
return name
case .custom(let privateKey):
return privateKey
case .web3Modal(let account):
return account.absoluteString
}
}
}
75 changes: 68 additions & 7 deletions Example/Showcase/Classes/DomainLayer/Chat/ChatService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import Combine
import WalletConnectChat
import WalletConnectRelay
import WalletConnectSign

typealias Stream<T> = AnyPublisher<T, Never>

Expand Down Expand Up @@ -81,7 +82,7 @@ final class ChatService {
try await client.reject(inviteId: invite.id)
}

func goPublic(account: Account, privateKey: String) async throws {
func goPublic(account: Account) async throws {
try await client.goPublic(account: account)
}

Expand All @@ -91,17 +92,15 @@ final class ChatService {
try await client.invite(invite: invite)
}

func register(account: Account, privateKey: String) async throws {
func register(account: Account, importAccount: ImportAccount) async throws {
_ = try await client.register(account: account) { message in
let signature = self.onSign(message: message, privateKey: privateKey)
return SigningResult.signed(signature)
return await self.onSign(message: message, importAccount: importAccount)
}
}

func unregister(account: Account, privateKey: String) async throws {
func unregister(account: Account, importAccount: ImportAccount) async throws {
try await client.unregister(account: account) { message in
let signature = self.onSign(message: message, privateKey: privateKey)
return SigningResult.signed(signature)
return await self.onSign(message: message, importAccount: importAccount)
}
}

Expand All @@ -116,9 +115,71 @@ final class ChatService {

private extension ChatService {

func onSign(message: String, importAccount: ImportAccount) async -> SigningResult {
switch importAccount {
case .swift, .kotlin, .js, .custom:
return .signed(onSign(message: message, privateKey: importAccount.privateKey))
case .web3Modal(let account):
return await onWeb3ModalSign(message: message, account: account)
}
}

func onSign(message: String, privateKey: String) -> CacaoSignature {
let privateKey = Data(hex: privateKey)
let signer = MessageSignerFactory(signerFactory: DefaultSignerFactory()).create()
return try! signer.sign(message: message, privateKey: privateKey, type: .eip191)
}

func onWeb3ModalSign(message: String, account: Account) async -> SigningResult {
guard let session = Sign.instance.getSessions().last else { return .rejected }

do {
let request = makeRequest(session: session, message: message, account: account)
try await Sign.instance.request(params: request)

let signature: CacaoSignature = try await withCheckedThrowingContinuation { continuation in
var cancellable: AnyCancellable?
cancellable = Sign.instance.sessionResponsePublisher
.sink { response in
defer { cancellable?.cancel() }
switch response.result {
case .response(let value):
do {
let string = try value.get(String.self)
let signature = CacaoSignature(t: .eip191, s: string.deleting0x())
continuation.resume(returning: signature)
} catch {
continuation.resume(throwing: error)
}
case .error(let error):
continuation.resume(throwing: error)
}
}
}

return .signed(signature)
} catch {
return .rejected
}
}

func makeRequest(session: WalletConnectSign.Session, message: String, account: Account) -> Request {
return Request(
topic: session.topic,
method: "personal_sign",
params: AnyCodable(["0x" + message.data(using: .utf8)!.toHexString(), account.address]),
chainId: Blockchain("eip155:1")!
)
}
}

fileprivate extension String {

func deleting0x() -> String {
var string = self
if starts(with: "0x") {
string.removeFirst(2)
}
return string
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum ImportAccount {
case kotlin
case js
case custom(privateKey: String)
case web3Modal(account: Account)

init?(input: String) {
switch input.lowercased() {
Expand Down Expand Up @@ -34,7 +35,7 @@ enum ImportAccount {
return "kotlin.eth"
case .js:
return "js.eth"
case .custom:
case .custom, .web3Modal:
return account.address
}
}
Expand All @@ -50,6 +51,8 @@ enum ImportAccount {
case .custom(let privateKey):
let address = try! EthereumPrivateKey(hexPrivateKey: "0x" + privateKey, ctx: nil).address.hex(eip55: true)
return Account("eip155:1:\(address)")!
case .web3Modal(let account):
return account
}
}

Expand All @@ -63,6 +66,8 @@ enum ImportAccount {
return "8df6b8206eebcd3da89b750f1cf9bba887630c3c5eade83f44c06fa4f7cc5f65"
case .custom(let privateKey):
return privateKey
case .web3Modal:
fatalError("Private key not available")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class ChatListInteractor {
func logout() async throws {
guard let importAccount = accountStorage.importAccount else { return }
try await chatService.goPrivate(account: importAccount.account)
try await chatService.unregister(account: importAccount.account, privateKey: importAccount.privateKey)
try await chatService.unregister(account: importAccount.account, importAccount: importAccount)
accountStorage.importAccount = nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ final class ImportInteractor {
}

func register(importAccount: ImportAccount) async throws {
try await chatService.register(account: importAccount.account, privateKey: importAccount.privateKey)
try await chatService.register(account: importAccount.account, importAccount: importAccount)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UIKit
import Combine
import WalletConnectSign

final class ImportPresenter: ObservableObject {

Expand Down Expand Up @@ -52,7 +53,16 @@ extension ImportPresenter: SceneViewModel {
private extension ImportPresenter {

func setupInitialState() {
Sign.instance.sessionSettlePublisher.sink { session in
let accounts = session.namespaces.values.reduce(into: []) { result, namespace in
result = result + Array(namespace.accounts)
}

Task(priority: .userInitiated) {
try await self.importAccount(.web3Modal(account: accounts.first!))
}

}.store(in: &disposeBag)
}

@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class WelcomeInteractor {

func goPublic() async throws {
guard let importAccount = importAccount else { return }
try await chatService.goPublic(account: importAccount.account, privateKey: importAccount.privateKey)
try await chatService.goPublic(account: importAccount.account)
}
}

Expand Down

0 comments on commit 28f6812

Please sign in to comment.