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 4366ddc commit fa60673
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 16 deletions.
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
Expand Up @@ -58,9 +58,9 @@ private extension ImportPresenter {
result = result + Array(namespace.accounts)
}

print(accounts)

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

}.store(in: &disposeBag)
}
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 fa60673

Please sign in to comment.