WalletConnectSwift 1.7.0

WalletConnectSwift 1.7.0

Andrey ScherbovichDmitry Bespalov 维护。



  • 作者:
  • Andrey Scherbovich 和 Dmitry Bespalov

WalletConnectSwift

Swift SDK,实现了 WalletConnect 1.x.x 协议,用于原生 iOS Dapps 和钱包。

特性

  • 服务器(钱包端)

    • 创建、重新连接、断开连接和更新会话
    • 具有通过 JSON RPC 2.0 支持的 Codable 的灵活、可扩展的请求处理
  • 客户端(原生 dapp 端)

    • 创建、重新连接、断开连接和更新会话
    • WalletConnect SDK API 的默认实现
      • personal_sign
      • eth_sign
      • eth_signTypedData
      • eth_sendTransaction
      • eth_signTransaction
      • eth_sendRawTransaction
    • 使用通过 JSON RPC 2.0 支持的 Codable 发送自定义 RPC 请求

示例代码

示例代码在单独的仓库中:https://github.com/WalletConnect/WalletConnectSwift-Example

  • 钱包示例应用
    • 通过 QR 码读取器连接
    • 通过深度链接("wc" 模式)连接
    • 重启后重新连接
    • 请求处理程序示例
  • Dapp 示例应用
    • 通过 QR 码读取器连接
    • 通过深度链接("wc" 模式)连接
    • 重启后重新连接
    • 请求处理程序示例

钱包中使用

要开始连接,您需要创建并保持一个Server对象,并为它提供一个代理

let server = Server(delegate: self)

库为您处理特定的WalletConnect会话请求 - wc_sessionRequestwc_sessionUpdate

要注册重要的会话更新事件,实现shouldStartdidConnectdidDisconnectdidFailToConnect方法。

默认情况下,服务器无法处理其他任何请求 - 您需要提供自己的实现。

通过注册请求处理程序来实现这一点。您可以选择为每种请求方法注册一个处理程序,或者注册一个通用的请求处理程序。

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

按照注册顺序,处理程序被问及是否能处理每个请求。第一个返回true的处理程序将得到handle(request:)的调用。所有其他处理程序将被跳过。

在请求处理程序中,请在canHandle实现中检查传入请求的方法,并在handle(request:)实现中处理实际的请求。

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

您可以通过使用send方法通过服务器发送对请求的响应

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!))
}

有关更多详细信息,请参阅ExampleApps/ServerApp

在Dapp中使用

要开始连接,您需要创建并保留一个Client对象,并为它提供DappInfo和一个代理

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

代理将接收连接建立、失败或断开时的调用。

成功连接后,您可以在Client上调用各种API方法。

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
  }

您还可以发送一个自定义请求。由JSON RPC要求的请求ID由库内部生成和处理。

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

您可以转换接收到的响应结果到一个Decodable类型。

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

您还可以检查钱包是否以错误响应

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

有关更多详细信息,请参阅ExampleApps/ClientApp

运行示例应用

请打开ExampleApps/ExampleApps.xcodeproj

安装

必备条件

  • iOS 13.0 或 macOS 10.14
  • Swift 5

WalletConnectSwift 依赖项

  • CryptoSwift - 用于加密操作

Swift 包管理器

在您的 Package.swift

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

CocoaPods

在您的 Podfile

platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'WalletConnectSwift'
end

Carthage

在您的 Cartfile

github "WalletConnect/WalletConnectSwift"

运行 carthage update 构建框架并将 WalletConnectSwift.framework 拖到您的 Xcode 项目中。

鸣谢

我们感谢 Trust Wallet 团队在实现这个库方面给我们带来的灵感。

贡献者

许可

MIT 许可证(见 LICENSE 文件)。