TrustSDK
入门
TrustSDK 可以让您签署以太坊交易和消息,因此您可以在不担心密钥或钱包的情况下构建原生 DApp。按照以下说明将 TrustSDK 集成到您的原生 DApp 中。
演示
安装
TrustSDK 可通过 CocoaPods 获得。要安装它,请简单地将其添加到您的 Podfile 中。
pod 'TrustSDK'
运行 pod install
。
配置
按照以下步骤在您的应用程序中配置 TrustSDK
。
模式配置
打开 Xcode 并点击你的项目。转到“信息”标签并将“URL 类型”分组展开。点击 + 按钮添加新的模式。并在 ‘URL 模式’ 中输入自定义的模式名称。
初始化
打开 AppDelegate.swift
文件,并在 application(_:didFinishLaunchingWithOptions:)
方法中初始化 TrustSDK。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
TrustSDK.initialize(with: TrustSDK.Configuration(scheme: "trustexample"))
return true
}
处理回调
通过在 application(_:open:options:)
方法中调用 TrustSDK,让 TrustSDK 捕获 deep link 的响应。
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return TrustSDK.application(app, open: url, options: options)
}
API
要使用 TrustSDK,您必须导入 TrustSDK
和 TrustWalletCore
模块。
签名交易
TrustSDK 提供了一个易于使用的通用 API 来签名交易。每个区块链接受一个 SigningInput
对象,并响应一个 SigningOutput
,该响应可以直接广播到节点。每个输入和输出对象是 wallet-core 的 protobuf 消息的 Swift 实现。要签名一个以太坊交易,您有以下 SigningInput
let input = EthereumSigningInput.with {
$0.toAddress = "0x3D60643Bf82b928602bce34EE426a7d392157b69"
$0.chainID = BigInt("1").serialize()!
$0.nonce = BigInt("464").serialize()!
$0.gasPrice = BigInt("11500000000").serialize()!
$0.gasLimit = BigInt("21000").serialize()!
$0.amount = BigInt("1000000000000000").serialize()!
}
TrustSDK 提供了一些方便的扩展,以方便地处理
Data
和BigInt
序列化。
一旦定义了输入,只需调用区块链签名器以签名交易即可
TrustSDK.signers.ethereum.sign(input: input) { result in
switch result {
case .success(let output):
// Handle the signing output
case .failure(let error):
// Handle failres like user rejections
}
}
签名消息
要请求签名消息,您首先必须以十六进制格式对消息进行编码或散列,然后从TrustSDK.signers
调用sign(message:)
,以下是一个以太坊示例消息:
let data = Data("Some message".utf8)
let message = Data("\u{19}Ethereum Signed Message:\n\(data.count)".utf8) + data
let hash = message.sha3(.keccak256)
TrustSDK.signers.ethereum.sign(message: hash) { result in
switch result {
case .success(let signature):
// Handle the signature
case .failure(let error):
// Handle failure
}
}
获取地址
要获取用户的地址,直接从TrustSDK
调用getAccounts(for:)
并传递一个CoinType
数组即可。
TrustSDK.getAccounts(for: [.ethereum, .bitcoin]) { result in
switch result {
case .success(let addresses):
// Handle the address array
case .failure(let error):
// Handle failure
}
}
钱包开发者
如果您的钱包已经使用TrustWalletCore
并希望集成TrustSDK
,请按照以下步骤操作:
安装WalletSDK
将以下行添加到您的Podfile中:
pod 'TrustSDK/Wallet'
运行 pod install
。
处理TrustSDK命令
导入TrustSDK
并实现WalletSDKRequestHandler.handle(request:callback:)
。命令必须异步处理,完成后,您的实现必须通过调用带有命令响应的callback
参数。
class WalletSDKRequestHandlerImplementation: WalletSDKRequestHandler {
func handle(request: WalletSDK.Request, callback: @escaping ((WalletSDK.Response) -> Void)) {
switch request.command {
case .getAccounts(let coins):
// Handle get accoutns command
let accounts = ...
callback(.accounts(accounts))
case .sign(let coin, let input):
// Handle sign command
let output = ...
callback(.sign(coin: coin, output: output))
}
// You can respond with a failure response in case of exception
callback(.failure(.unknown))
}
}
在您的应用程序初始化方法中,设置处理程序实现WalletSDK.handler
,然后通过在application(_:open:options:)
方法中调用它来让WalletSDK
处理深链接。
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return WalletSDK.application(app, open: url, options: options)
}
如果您已处理应用程序中的深链接,或者您必须自行解析WalletSDK.Request
结构并使用WalletSDK.dispatch(request:)
方法分发。
支持您的钱包
一旦您为钱包配置了WalletSDK
,告诉dApp开发者在使用TrustSDK.Configureation
时设置您的钱包的scheme
和installURL
属性
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let wallet = WalletApp(
scheme: "walletscheme",
installURL: URL(string: "https://apps.apple.com/app/walletapp")!
)
TrustSDK.initialize(with: TrustSDK.Configuration(scheme: "trustexample", walletApp: wallet))
return true
}
示例
Trust SDK包含一个使用上述代码的示例项目。要运行示例项目,克隆repo并从Example
目录运行pod install。打开TrustSDK.xcworkspace
并运行。确保您的设备或模拟器已安装Trust钱包,以便测试完整的回调流程。
作者
- Leone Parise
- Viktor Radchenko
许可证
TrustSDK遵循MIT许可证。有关更多信息,请参阅LICENSE文件。