Code Monkey home page Code Monkey logo

walletconnectswift's Introduction

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).

walletconnectswift's People

Contributors

brunobar79 avatar cassiuspacheco avatar darthmike avatar hboon avatar lvykk avatar oscahie avatar robin-on-git avatar sche avatar shmakovigor avatar valeriyvan avatar zulkis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

walletconnectswift's Issues

Open Metamask while transaction

I am connecting Metamask wallet from my app using WalletConnectSwift and get the wallet address successfully. (For connecting, the User is navigated to Metamask for connect)
But now I am trying to send_ethTransaction from my app, I want to redirect the Metamask app for approval same as connection flow.
How can I do it?

Example for eth_sendTransaction

Hello,
Is it possible for someone to give me an example of handling "eth_signedTransaction", such that Dapp sees that transaction is sent.

Trying to understand the architecture

I'm wondering why is there so much code in the example part of the proyect, shouldn't ideally be way more code in the lib itself? If you compare it with for example web3.js where you need like only 1 line of code to call an authentication.

Crash into Server.failedToConnect

Hi,

I have a few crashes into the Server class cuz you use force unwrap for weak var delegate

public private(set) weak var delegate: ServerDelegate!

Please make it optional. It allows avoiding a crash when delegate already destroeyed

Question - how to use the client dapp lib to connect to a different wallet

Hey there! Sorry for throwing a noob question in here, but are there any examples of using the client library to connect to a different mobile wallet, like Rainbow wallet?

I can't seem to figure out how to configure the lib to work with servers other than the mock wallet supplied by this lib. I'm happy to submit a little PR to add a code snippet to the README as well once this question gets answered!

Thank you for any help

Example Client App not connecting

Hitting "Connect to WalletConnect" opens Metamask, but the connect modal is never shown.
If I specify Rainbow as the target wallet, the same things happens, no modal.

Version 12.4 (12D4e)
Device: iOS 14.4.1

Possible related: #49

Send RPC url to client

Is it possible to send the RPC URL to the client, some clients are not working properly without an RPC URL coming from the provider.
For instance, when I connect to Uniswap I can see in the logs of uniswap that the RPC endpoint is wrong thus it shows unsupported wallet.

Edit: It might be issue from uniswap side as well, so please correct me if I am wrong
Thanks

Connecting to Wallet

Using this SDK

After I call open URL it redirects to the MetaMask then does nothing. Is there a missing step to have it connect and return to the parent app?

Issue compiling the example program in Xcode 13.1

I found an issue compiling the example program in Xcode 13.1.

The issue can be solved by adding the following to the dependencies section in the Package.swift file:

    .package(url: "https://github.com/Boilertalk/secp256k1.swift.git", from: "0.1.0"),
    .package(url: "[email protected]:apple/swift-nio-zlib-support.git", from: "1.0.0"),

I would have fixed it myself but don't think I have the ability to do a PR on the repo.

Connecting to MetaMask

Hello,

I have followed the documentation. I am trying to connect to MetaMask with the latest Example Client App from this Repo. However once MetaMask opens nothing else happens.

Is there known good code I can see so I can connect correctly...?

Thanks.

Trust Wallet

Please kindly help me with details of information about why my Trust Wallet can't get connected. Thanks

Deeplinking not working

Using the ClientApp in ExampleApps to connect is not working correctly with the two wallets I have installed on device (Metamask & Rainbow)

If I force it show the QRCode, screenshot it, and then open Rainbow, the flow works. But the deeplinking just opens Metamask, but nothing happens within Metamask.

WC: trying to reconnect session by url

I call the func self.server.disconnect(from: session), but it still logs these:

WC: didDisconnect url: https://bridge.walletconnect.org
WC: trying to reconnect session by url: https://bridge.walletconnect.org
WC: didConnect url: https://bridge.walletconnect.org
WC: ==> {"payload":"","topic":"66396782-A690-4888-9AD9-F74A70403E7D","type":"sub"}
WC: didDisconnect url: https://bridge.walletconnect.org
WC: trying to reconnect session by url: https://bridge.walletconnect.org
WC: didConnect url: https://bridge.walletconnect.org
WC: ==> {"payload":"","topic":"66396782-A690-4888-9AD9-F74A70403E7D","type":"sub"}

Example App - Client fails to connect to a wallet

Hey there,

I'm using your Client app that deploys a "wc://wc?uri=..." deep link. If there is no Server app on my iPhone, a MetaMask Wallet mobile app is evoked (I assume they just subscribed to the "wc://" schema).

But the Client app fails to connect to MetaMask Wallet app consistently. How can I fix this?

In fact, my app needs the user to cryptographically sign a message, so I need to connect to the MM wallet app and receive a signed message back.

Thanks!

Opening Trust Wallet from wallet connect showing 'Something went wrong'

Was trying to use wallet connect to get account data from trust wallet, however the request sent from wallet connect doesn't seem to work, trust wallet opens a screen with Something went wrong.

Below is the URL thats being created and used to open Trust wallet:
wc:46278999-4477-46FF-88D1-947FADDC979B@1?bridge=https%3A%2F%2Fbridge.walletconnect.org&key=127820ed39c533dcce56d33b6820cdceabe7692ed00094be492191199f027d39

Error Screenshot url : https://ibb.co/qy8cwzg
Any help would be highly appreciated.

Transaction Notifications

Hello everyone!
During the testing, I got the case when transaction requests weren't delivered to a device. Who knows anything about that? Is it the typical case or not? Moreover, sometimes the balance of assets is incorrect in the dapps.

SwiftUI with UIKit issue

Hi, my name is Taha. I'm coding with SwiftUI. I choose to use WalletConnect library but looks like it was written in UIKit. How can i use this library in SwiftUI?

Problem when connect with Trust Wallet

Hello.
I try to connect to Trust Wallet using connect function, i run example and got this alert from Trust Wallet

IMG_0036

Connect via QR code seem to be working. How to fix this problem or maybe Trust Wallet is not accept connect via this function.

Add support for EIP-1559 transactions

Currently the SDK only supports legacy transactions. It needs to support EIP-2718 typed transaction envelope to support transactions for EIP-1559

Connection failure on iOS 14.5

It works fine in 14.4, but on newly released 14.5, connection fails. Delegate's client(_ client: Client, didFailToConnect url: WCURL) gets called. It happens in both simulator and device.

Connecting to MetaMask rarely works

Using bridgeURL - https://safe-walletconnect.gnosis.io and metamask deep link https://metamask.app.link/wc?uri=wc yet MetaMask mobile rarely opens the prompt to allow permission to connect. It seems to be flaky and works less than it doesn't. Whats going on

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.