• Stars
    star
    289
  • Rank 140,506 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WalletConnect Swift SDK

WalletConnectSwift

Swift SDK implementing WalletConnect 1.x.x protocol for native iOS Dapps and Wallets.

Features

  • Server (wallet side)

    • Create, reconnect, disconnect, and update session
    • Flexible, extendable request handling with Codable support via JSON RPC 2.0
  • Client (native dapp side)

    • Create, reconnect, disconnect, and update session
    • Default implementation of WalletConnect SDK API
      • personal_sign
      • eth_sign
      • eth_signTypedData
      • eth_sendTransaction
      • eth_signTransaction
      • eth_sendRawTransaction
    • Send custom RPC requests with Codable support via JSON RPC 2.0

Example Code

Example code is in a separate repository: https://github.com/WalletConnect/WalletConnectSwift-Example

  • Wallet Example App:
    • Connecting via QR code reader
    • Connecting via deep link ("wc" scheme)
    • Reconnecting after restart
    • Examples of request handlers
  • Dapp Example App:
    • Connecting via QR code reader
    • Connecting via deep link ("wc" scheme)
    • Reconnecting after restart
    • Examples of request handlers

Usage in a Wallet

To start connections, you need to create and retain a Server object to which you provide a delegate:

let server = Server(delegate: self)

The library handles WalletConnect-specific session requests for you - wc_sessionRequest and wc_sessionUpdate.

To register for the important session update events, implement the delegate methods shouldStart, didConnect, didDisconnect and didFailToConnect.

By default, the server cannot handle any other reqeusts - you need to provide your implementation.

You do this by registering request handlers. You have the flexibility to register one handler per request method, or a catch-all request handler.

server.register(handler: PersonalSignHandler(for: self, server: server, wallet: wallet))

Handlers are asked (in order of registration) whether they can handle each request. First handler that returns true from canHandle(request:) method will get the handle(request:) call. All other handlers will be skipped.

In the request handler, check the incoming request's method in canHandle implementation, and handle actual request in the handle(request:) implementation.

func canHandle(request: Request) -> Bool {
   return request.method == "eth_signTransaction"
}

You can send back response for the request through the server using send method:

func handle(request: Request) {
  // do you stuff here ...
  
  // error response - rejected by user
  server.send(.reject(request))

  // or send actual response - assuming the request.id exists, and MyCodableStruct type defined
  try server.send(Response(url: request.url, value: MyCodableStruct(value: "Something"), id: request.id!))
}

For more details, see the ExampleApps/ServerApp

Usage in a Dapp

To start connections, you need to create and keep alive a Client object to which you provide DappInfo and a delegate:

let client = Client(delegate: self, dAppInfo: dAppInfo)

The delegate then will receive calls when connection established, failed, or disconnected.

Upon successful connection, you can invoke various API methods on the Client.

try? client.personal_sign(url: session.url, message: "Hi there!", account: session.walletInfo!.accounts[0]) {
      [weak self] response in
      // handle the response from Wallet here
  }

You can also send a custom request. The request ID required by JSON RPC is generated and handled by the library internally.

try? client.send(Request(url: url, method: "eth_gasPrice")) { [weak self] response in
    // handle the response
}

You can convert the received response result to a Decodable type.

let nonceString = try response.result(as: String.self)

You can also check if the wallet responded with error:

if let error = response.error { // NSError
  // handle error
}

For more details, see the ExampleApps/ClientApp

Logs

Filter the type of logs being printed by changing the log level LogService.level = .info.

Running Example Apps

Please open ExampleApps/ExampleApps.xcodeproj

Installation

Prerequisites

  • iOS 13.0 or macOS 10.14
  • Swift 5

WalletConnectSwift dependencies

  • CryptoSwift - for cryptography operations

Swift Package Manager

In your Package.swift:

dependencies: [
    .package(url: "https://github.com/WalletConnect/WalletConnectSwift.git", .upToNextMinor(from: "1.2.0"))
]

CocoaPods

In your Podfile:

platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'WalletConnectSwift'
end

Carthage

In your Cartfile:

github "WalletConnect/WalletConnectSwift"

Run carthage update to build the framework and drag the WalletConnectSwift.framework in your Xcode project.

Acknowledgments

We'd like to thank Trust Wallet team for inspiration in implementing this library.

Contributors

License

MIT License (see the LICENSE file).

More Repositories

1

web3modal

A single Web3 provider solution for all Wallets
TypeScript
4,702
star
2

create-eth-app

Create Ethereum-powered apps with one command
JavaScript
2,691
star
3

walletconnect-monorepo

WalletConnect Monorepo
TypeScript
1,263
star
4

walletconnect-docs

WalletConnect Documentation
CSS
359
star
5

walletconnect-example-dapp

Example Dapp
TypeScript
340
star
6

web-examples

Wallet and dapp examples implementing WalletConnect v2
TypeScript
322
star
7

WalletConnectSwiftV2

WalletConnect Swift SDK v2
Swift
251
star
8

walletconnect-test-wallet

Test Wallet (Web)
TypeScript
170
star
9

web3modal-vanilla-js-example

An example application how to use Web3modal from vanilla JavaScript
JavaScript
169
star
10

WalletConnectKotlinV2

WalletConnect Kotlin SDK v2
Kotlin
168
star
11

kotlin-walletconnect-lib

library to use WalletConnect with Kotlin or Java
Kotlin
145
star
12

a2

An Asynchronous Apple Push Notification (apns2) Client for Rust
Rust
135
star
13

WalletConnectSharp

A C# implementation of the WalletConnect client
C#
134
star
14

WalletConnectUnity

An extension of WalletConnectSharp that brings WalletConnect to Unity 🎮
C#
118
star
15

walletconnect-registry

App Registry for WalletConnect Protocol
JavaScript
107
star
16

walletconnect-website

WalletConnect Website
TypeScript
101
star
17

WalletConnectFlutterV2

WalletConnect v2 client made in Dart for Flutter.
Dart
97
star
18

react-native-examples

React Native examples implementing WalletConnect v2
TypeScript
72
star
19

py-walletconnect-bridge

WalletConnect Bridge Python Implementation
Python
64
star
20

walletconnect-utils

Javascript Utilities for WalletConnect
TypeScript
62
star
21

web3modal-examples

JavaScript
57
star
22

awesome-walletconnect

A curated list of awesome WalletConnect resources, libraries, tools and more
55
star
23

node-walletconnect-bridge

[Deprecated] Moved to walletconnect-monorepo
Shell
48
star
24

walletconnect-demo-app

Demo React-Native App to Test WalletConnect integration
TypeScript
45
star
25

web3inbox

TypeScript
42
star
26

web3modal-swift

Swift
40
star
27

walletconnect-assets

WalletConnect Brand Guidelines
37
star
28

EIP6963

A web application showcasing the implementation and usage of EIP-6963 for Multi Injected Provider Discovery
TypeScript
34
star
29

web3modal-checkout

Payment checkout with Web3Modal
TypeScript
33
star
30

Web3ModalFlutter

The Web3Modal for WalletConnect built using Flutter.
Dart
28
star
31

WalletConnectSwift-Example

Example code for WalletConnectSwift library
Swift
27
star
32

walletconnect-specs

WalletConnect Specifications
CSS
27
star
33

web3modal-react-native

Web3Modal React-Native SDK
TypeScript
27
star
34

blockchain-api

WalletConnect's Blockchain API
Rust
26
star
35

modal-react-native

Simplest and most minimal way to connect your users with WalletConnect
TypeScript
25
star
36

create-wc-dapp

create-wc-dapp is an npx starter template that allows you to easily set up a WalletConnect integrated dApp.
TypeScript
23
star
37

relay

This repository contains the minimal relay server for WalletConnect v2.0
TypeScript
22
star
38

echo-server

WalletConnect Push Server Implementation
Rust
22
star
39

cosmos-wallet

Configurable Wallet for Cosmos SDK Chains
TypeScript
21
star
40

gm-hackers

Get started in minutes with WalletConnect Notify API.
TypeScript
19
star
41

WalletConnectRust

WalletConnect Rust SDK
Rust
18
star
42

WalletConnectModalFlutter

The WalletConnectModal for WalletConnect built using Flutter.
Dart
18
star
43

firebase-walletconnect-push

WalletConnect Push Notification Firebase Webhook
TypeScript
15
star
44

solib

Solana friendly API
TypeScript
14
star
45

cast-server

Rust
10
star
46

auth-client-js

TypeScript
10
star
47

.github

10
star
48

swift-walletconnect-lib

[Deprecated] Please refer to wallet-connect-swift repo
Swift
10
star
49

cloud-auth-api

PoC of the GoTrue replacement
HCL
9
star
50

node-walletconnect-push

[Deprecated] Moved to walletconnect-monorepo
TypeScript
8
star
51

WCIPs

WalletConnect Improvement Proposals
7
star
52

keys-server

Microservice to manage identity keys and invite keys for Chat SDK
Rust
7
star
53

association

WalletConnect Association (Verein) in Zug, Switzerland
6
star
54

CloudSIWE

Files that support Cloud's Ethereum login
HCL
6
star
55

modal

WalletConnectModal repository
TypeScript
6
star
56

py-walletconnect-push

Reference Push Server implementation in Python3 for WalletConnect
Python
6
star
57

solana-wallet

TypeScript
5
star
58

Community

A place to store public documents related to our community
5
star
59

hyper-alpn

An ALPN connector for Hyper
Rust
5
star
60

KotlinCodingChallenge

Kotlin
5
star
61

chat-client-js

TypeScript
4
star
62

www-web3modal

Website for Web3Modal
TypeScript
4
star
63

notify-server

notify.walletconnect.com
Rust
4
star
64

actions

WalletConnect GitHub Actions
4
star
65

gm-dapp

dapp publishing example of WalletConnect Push
TypeScript
3
star
66

rs-common

Rust
3
star
67

modal-swift

Swift
3
star
68

gilgamesh

Rust
3
star
69

web3inbox-widget

TypeScript
2
star
70

notify-client-js

TypeScript
2
star
71

rust-http-starter

Starter project for a Rust HTTP server with Axum backed by a Postgres database
HCL
2
star
72

terraform-null-label

HCL
2
star
73

supabase-prometheus-exporter

Shell
2
star
74

terraform-aws-dns

Terraform module to create a public Route53 zone for a fqdn and generate a wildcard certificate for it.
HCL
2
star
75

verify-server

You either get in or you don't
Smarty
2
star
76

walletconnect-legacy

Monorepo for WalletConnect (v1.0) legacy JS packages
TypeScript
2
star
77

gorgon

Analytics exporter
Rust
1
star
78

self-host-relay

1
star
79

cerberus

Rust
1
star
80

session-keys-demo-contracts

Solidity
1
star
81

se-sdk

TypeScript
1
star
82

push-webhook-test-server

Test Server to allow us integ test our push offering
HCL
1
star
83

erc6492

Universal Etheruem signature verification with ERC-6492
Rust
1
star
84

session-keys-demo

TypeScript
1
star