From 6acf953c66afdd73eaa23aaeb647eddcc4baa64a Mon Sep 17 00:00:00 2001 From: hewigovens <360470+hewigovens@users.noreply.github.com> Date: Tue, 16 Mar 2021 16:41:29 +0900 Subject: [PATCH] [EIP-3085] handle wallet_addEthereumChain (#131) * handle wallet_addEthereumChain * update example to handle new methods --- dist/trust-min.js | 4 +- ios/TrustWeb3Provider/DAppMethod.swift | 2 + .../DAppWebViewController.swift | 39 ++++++++++++++++--- src/index.js | 13 +++++-- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/dist/trust-min.js b/dist/trust-min.js index 8a221a69..240dc101 100644 --- a/dist/trust-min.js +++ b/dist/trust-min.js @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:254b8604051a596649cee74efe03a0354c3b3c23f1860ea492af8c5e0e58b747 -size 535462 +oid sha256:122b36451d32ff5076cbfa594e8234da049735ed5f9e1ed59d3a066895afb87c +size 535632 diff --git a/ios/TrustWeb3Provider/DAppMethod.swift b/ios/TrustWeb3Provider/DAppMethod.swift index 93a68db5..01106e2f 100644 --- a/ios/TrustWeb3Provider/DAppMethod.swift +++ b/ios/TrustWeb3Provider/DAppMethod.swift @@ -13,4 +13,6 @@ enum DAppMethod: String, Decodable, CaseIterable { case signTypedMessage case ecRecover case requestAccounts + case watchAsset + case addEthereumChain } diff --git a/ios/TrustWeb3Provider/DAppWebViewController.swift b/ios/TrustWeb3Provider/DAppWebViewController.swift index 7cf5166d..1eb327ff 100644 --- a/ios/TrustWeb3Provider/DAppWebViewController.swift +++ b/ios/TrustWeb3Provider/DAppWebViewController.swift @@ -13,7 +13,7 @@ class DAppWebViewController: UIViewController { @IBOutlet weak var urlField: UITextField! var homepage: String { - return "https://js-eth-sign.surge.sh" + return "https://chainlist.org" } let privateKey = PrivateKey(data: Data(hexString: "0x4646464646464646464646464646464646464646464646464646464646464646")!)! @@ -123,6 +123,9 @@ extension DAppWebViewController: WKScriptMessageHandler { self.webview.sendResult(recovered, to: id) } break + case .addEthereumChain: + guard let (chainId, name, rpcUrls) = extractChainInfo(json: json) else { return } + alert(title: name, message: "chainId: \(chainId)\n \(rpcUrls.joined(separator: "\n")))") default: break } @@ -161,24 +164,50 @@ extension DAppWebViewController: WKScriptMessageHandler { present(alert, animated: true, completion: nil) } + func alert(title: String, message: String) { + let alert = UIAlertController( + title: title, + message: message, + preferredStyle: .alert + ) + alert.addAction(.init(title: "OK", style: .default, handler: nil)) + present(alert, animated: true, completion: nil) + } + private func extractMessage(json: [String: Any]) -> Data? { - guard let params = json["object"] as? [String: Any], + guard + let params = json["object"] as? [String: Any], let string = params["data"] as? String, - let data = Data(hexString: string) else { + let data = Data(hexString: string) + else { return nil } return data } private func extractSignature(json: [String: Any]) -> (signature: Data, message: Data)? { - guard let params = json["object"] as? [String: Any], + guard + let params = json["object"] as? [String: Any], let signature = params["signature"] as? String, - let message = params["message"] as? String else { + let message = params["message"] as? String + else { return nil } return (Data(hexString: signature)!, Data(hexString: message)!) } + private func extractChainInfo(json: [String: Any]) ->(chainId: String, name: String, rpcUrls: [String])? { + guard + let params = json["object"] as? [String: Any], + let string = params["chainId"] as? String, + let name = params["chainName"] as? String, + let urls = params["rpcUrls"] as? [String] + else { + return nil + } + return (chainId: string, name: name, rpcUrls: urls) + } + private func signMessage(data: Data, addPrefix: Bool = true) -> Data { let message = addPrefix ? Hash.keccak256(data: ethereumMessage(for: data)) : data var signed = privateKey.sign(digest: message, curve: .secp256k1)! diff --git a/src/index.js b/src/index.js index fc09562e..6417ef86 100644 --- a/src/index.js +++ b/src/index.js @@ -162,6 +162,8 @@ class TrustWeb3Provider extends EventEmitter { return this.eth_requestAccounts(payload); case "wallet_watchAsset": return this.wallet_watchAsset(payload); + case "wallet_addEthereumChain": + return this.wallet_addEthereumChain(payload); case "eth_newFilter": case "eth_newBlockFilter": case "eth_newPendingTransactionFilter": @@ -252,14 +254,19 @@ class TrustWeb3Provider extends EventEmitter { } wallet_watchAsset(payload) { + let options = payload.params.options; this.postMessage("watchAsset", payload.id, { type: payload.type, - contract: payload.options.address, - symbol: payload.options.symbol, - decimals: payload.options.decimals || 0, + contract: options.address, + symbol: options.symbol, + decimals: options.decimals || 0, }); } + wallet_addEthereumChain(payload) { + this.postMessage("addEthereumChain", payload.id, payload.params[0]); + } + /** * @private Internal js -> native message handler */