shmakovigor / swift-walletconnect-lib

Library to use WalletConnect with Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WalletConnect

Library to use WalletConnect with Swift.

Requirements

  • iOS 10.0+
  • Xcode 10.1+
  • Swift 4.2+

Installation

WalletConnect can be added to your project using CocoaPods by adding the following line to your Podfile:

pod 'WalletConnect', '~> 0.0.1'

Usage

Parse scanned QR code:

let scannedCode = "..."
let parser = WCCodeParser()
let result = parser.parse(string: scannedCode)
        
if case let .success(session) = result {
    // Handle session
} else {
    // Bad QR
}

Approve session for account with device push token to receive notifications with requests:

let deviceToken = "..."
let pushData = WCPushNotificationData(deviceToken: deviceToken, webhookUrl: "https://foo.io/walletconnect/push")
let interactor = WCInteractor(session: session, pushData: pushData)
interactor.approveSession(accounts: [account]) { result in          
    if case let .success(response) = result {
        // Session approved
    } else {
        // Something went wrong
    }
}

Reject session:

let interactor = WCInteractor(session: session)
interactor.rejectSession(accounts: [account]) { result in          
    if case let .success(response) = result {
        // Session rejected
    } else {
        // Something went wrong
    }
}

Call requests handling

Register your app in the Apple Push Notification Service:

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
    // Enable or disable features based on authorization.
}

Configure push handling:

import UserNotifications
UNUserNotificationCenter.current().delegate = self

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
       
    let userInfo = response.notification.request.content.userInfo
    if let push = WCPushContent.fromUserInfo(userInfo) {
        handlePush(push: push)
    }
}

Handle push:

func handlePush(push: WCPushContent) {
 
    guard let session = self.currentSession, push.sessionId == session.sessionId else { return }
    
    let interactor = WCInteractor(session: session)
    interactor.fetchCallRequest(callId: push.callId) { response in
        
        guard case let .success(callRequest) = response else { return }
        
        switch callRequest {
        case let .sendTransaction(transaction):
            // Approve and sign or reject 
        case let .signMessage(accountAddress, message):
            // Approve and sign or reject
        }
    }
}

Approve call request:

self.signTransaction(transaction) { hash in

    guard let hash = hash else { return }
    
    let interactor = WCInteractor(session: session)
    interactor.approveCallRequest(callId: push.callId, result: hash) { response in 
        // Handle response
    }
}

Reject call request:

let interactor = WCInteractor(session: session)
interactor.rejectCallRequest(callId: push.callId) { response in 
    // Handle response
}

About

Library to use WalletConnect with Swift

License:MIT License


Languages

Language:Swift 95.5%Language:Ruby 4.5%