Skip to content
This repository was archived by the owner on May 20, 2021. It is now read-only.

add walletconnect support okexchain #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions Example/WalletConnect/WCSessionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class WCSessionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let string = "wc:fcfecccf-4930-46b9-9f42-5648579c1658@1?bridge=https%3A%2F%2Fbridge.walletconnect.org&key=4941e24abe9cce7822c17ebeadcd2f25a96b6e6904b9e4ec0942446ad5de8a18"

// let string = "wc:fcfecccf-4930-46b9-9f42-5648579c1658@1?bridge=https%3A%2F%2Fbridge.walletconnect.org&key=4941e24abe9cce7822c17ebeadcd2f25a96b6e6904b9e4ec0942446ad5de8a18"
let string = "wc:6087eb23-a8ac-4270-9f48-99cf86e56dd0@1?bridge=https%3A%2F%2Fbridge.walletconnect.org&key=228a5cfa700aa907a32c97568163d7a95d911fc56fe2ffcbe93b4199a2bcbc3a"
defaultAddress = CoinType.ethereum.deriveAddress(privateKey: privateKey)
uriField.text = string
addressField.text = defaultAddress
Expand Down Expand Up @@ -127,6 +127,19 @@ class WCSessionViewController: UIViewController {
}))
self?.show(alert, sender: nil)
}

interactor.okt.onTransaction = { [weak self] (id, event, transaction) in
let data = try! JSONEncoder().encode(transaction)
let message = String(data: data, encoding: .utf8)
let alert = UIAlertController(title: event.rawValue, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Reject", style: .destructive, handler: { _ in
self?.interactor?.rejectRequest(id: id, message: "I don't have okt").cauterize()
}))
alert.addAction(UIAlertAction(title: "Approve", style: .default, handler: { (_) in
self?.interactor?.approveRequest(id: id, result: "This is the signed data").cauterize()
}))
self?.show(alert, sender: nil)
}
}

func approve(accounts: [String], chainId: Int) {
Expand Down
22 changes: 22 additions & 0 deletions WalletConnect/Models/OKExChain/WCOKExChainTransaction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright © 2017-2019 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

import Foundation

public struct WCOKExChainTransaction: Codable {
public let from: String?
public let to: String?
public let value: String?
public let gasLimit: String?
public let gasPrice: String?
public let accountNumber: String?
public let sequenceNumber: String?
public let symbol: String?
public let memo: String?
public let decimalNum: String?
public let contractAddress: String?
public let data: String?
}
4 changes: 4 additions & 0 deletions WalletConnect/Models/WCEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public enum WCEvent: String {
case bnbTransactionConfirm = "bnb_tx_confirmation"
case trustSignTransacation = "trust_signTransaction"
case getAccounts = "get_accounts"

case oktSignTransaction = "okt_signTransaction"
case oktSendTransaction = "okt_sendTransaction"
}

extension WCEvent {

static let eth = Set<WCEvent>([.ethSign, .ethPersonalSign, .ethSignTypeData, .ethSignTransaction, .ethSendTransaction])
static let bnb = Set<WCEvent>([.bnbSign, .bnbTransactionConfirm])
static let trust = Set<WCEvent>([.trustSignTransacation, .getAccounts])
static let okt = Set<WCEvent>([.oktSignTransaction, .oktSendTransaction])

func decode<T: Codable>(_ data: Data) throws -> JSONRPCRequest<T> {
return try JSONDecoder().decode(JSONRPCRequest<T>.self, from: data)
Expand Down
24 changes: 24 additions & 0 deletions WalletConnect/SubInteractor/WCOKExChainInteractor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © 2017-2019 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

import Foundation

public typealias OktTransactionClosure = (_ id: Int64, _ event: WCEvent, _ transaction: WCOKExChainTransaction) -> Void

public struct WCOKExChainInteractor {
public var onTransaction: OktTransactionClosure?

func handleEvent(_ event: WCEvent, topic: String, decrypted: Data) throws {
switch event {
case .oktSignTransaction, .oktSendTransaction:
let request: JSONRPCRequest<[WCOKExChainTransaction]> = try event.decode(decrypted)
guard !request.params.isEmpty else { throw WCError.badJSONRPCRequest }
onTransaction?(request.id, event, request.params[0])
default:
break
}
}
}
4 changes: 4 additions & 0 deletions WalletConnect/WCInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ open class WCInteractor {
public var eth: WCEthereumInteractor
public var bnb: WCBinanceInteractor
public var trust: WCTrustInteractor
public var okt: WCOKExChainInteractor

// incoming event handlers
public var onSessionRequest: SessionRequestClosure?
Expand Down Expand Up @@ -64,6 +65,7 @@ open class WCInteractor {
self.eth = WCEthereumInteractor()
self.bnb = WCBinanceInteractor()
self.trust = WCTrustInteractor()
self.okt = WCOKExChainInteractor()

socket.onConnect = { [weak self] in self?.onConnect() }
socket.onDisconnect = { [weak self] error in self?.onDisconnect(error: error) }
Expand Down Expand Up @@ -211,6 +213,8 @@ extension WCInteractor {
try bnb.handleEvent(event, topic: topic, decrypted: decrypted)
} else if WCEvent.trust.contains(event) {
try trust.handleEvent(event, topic: topic, decrypted: decrypted)
}else if WCEvent.okt.contains(event) {
try okt.handleEvent(event, topic: topic, decrypted: decrypted)
}
}
}
Expand Down