From a72dd84984986cd8e851505bd656ae022b633e02 Mon Sep 17 00:00:00 2001 From: hewigovens <360470+hewigovens@users.noreply.github.com> Date: Mon, 28 Sep 2020 12:59:34 +0800 Subject: [PATCH] improve iframe compatibility (#103) --- dist/trust-min.js | 4 ++-- .../DAppWebViewController.swift | 19 +++++++++++++++ src/index.js | 23 +++++++++++++------ src/tests/test.js | 8 +++---- src/utils.js | 6 ++--- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/dist/trust-min.js b/dist/trust-min.js index 55687eb8..4c0f9579 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:41ad79db8f78e8c50aeb46d628328f0a870570bee906f068e4a0ba7d9055d1ec -size 534862 +oid sha256:93ffe1eac4c7c904c92ce870e6e334ce8037191b4208ee90fd45b57592168e0a +size 535058 diff --git a/ios/TrustWeb3Provider/DAppWebViewController.swift b/ios/TrustWeb3Provider/DAppWebViewController.swift index ed9dc85e..e1242e40 100644 --- a/ios/TrustWeb3Provider/DAppWebViewController.swift +++ b/ios/TrustWeb3Provider/DAppWebViewController.swift @@ -210,4 +210,23 @@ extension DAppWebViewController: WKUIDelegate { _ = webView.load(navigationAction.request) return nil } + + func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) { + let alert = UIAlertController(title: "", message: message, preferredStyle: .alert) + alert.addAction(.init(title: "OK", style: .default, handler: { _ in + completionHandler() + })) + present(alert, animated: true, completion: nil) + } + + func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) { + let alert = UIAlertController(title: "", message: message, preferredStyle: .alert) + alert.addAction(.init(title: "OK", style: .default, handler: { _ in + completionHandler(true) + })) + alert.addAction(.init(title: "Cancel", style: .cancel, handler: { _ in + completionHandler(false) + })) + present(alert, animated: true, completion: nil) + } } diff --git a/src/index.js b/src/index.js index 7ce04ec4..63a1c677 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ import ProviderRpcError from "./error"; import Utils from "./utils"; import IdMapping from "./id_mapping"; import { EventEmitter } from "events"; -import isUtf8 from 'isutf8'; +import isUtf8 from "isutf8"; class TrustWeb3Provider extends EventEmitter { constructor(config) { @@ -44,7 +44,7 @@ class TrustWeb3Provider extends EventEmitter { // this points to window in methods like web3.eth.getAccounts() var that = this; if (!(this instanceof TrustWeb3Provider)) { - that = window.ethereum + that = window.ethereum; } return that._request(payload, false); } @@ -60,7 +60,7 @@ class TrustWeb3Provider extends EventEmitter { * @deprecated Use request({method: "eth_requestAccounts"}) instead. */ enable() { - console.log('enable() is deprecated, please use window.ethereum.request({method: "eth_requestAccounts"}) instead.') + console.log("enable() is deprecated, please use window.ethereum.request({method: \"eth_requestAccounts\"}) instead."); return this.request({ method: "eth_requestAccounts", params: [] }); } @@ -95,11 +95,11 @@ class TrustWeb3Provider extends EventEmitter { * @deprecated Use request() method instead. */ sendAsync(payload, callback) { - console.log('sendAsync(data, callback) is deprecated, please use window.ethereum.request(data) instead.') + console.log("sendAsync(data, callback) is deprecated, please use window.ethereum.request(data) instead."); // this points to window in methods like web3.eth.getAccounts() var that = this; if (!(this instanceof TrustWeb3Provider)) { - that = window.ethereum + that = window.ethereum; } if (Array.isArray(payload)) { Promise.all(payload.map(that._request.bind(that))) @@ -208,7 +208,7 @@ class TrustWeb3Provider extends EventEmitter { } else { buffer = Buffer.from(data); } - const hex = buffer.toString('hex'); + const hex = buffer.toString("hex"); if (isUtf8(buffer)) { this.postMessage("signPersonalMessage", payload.id, {data: hex}); } else { @@ -266,11 +266,20 @@ class TrustWeb3Provider extends EventEmitter { data.result = result; } if (this.isDebug) { - console.log(`<== sendResponse id: ${id}, result: ${result}, data: ${JSON.stringify(data)}`); + console.log(`<== sendResponse id: ${id}, result: ${JSON.stringify(result)}, data: ${JSON.stringify(data)}`); } if (callback) { wrapResult ? callback(null, data) : callback(null, result); this.callbacks.delete(id); + } else { + console.log(`callback id: ${id} not found`); + // check if it's iframe callback + for (var i = 0; i < window.frames.length; i++) { + const frame = window.frames[i]; + if (frame.ethereum.callbacks.has(id)) { + frame.ethereum.sendResponse(id, result); + } + } } } diff --git a/src/tests/test.js b/src/tests/test.js index b3756e0b..005a2894 100644 --- a/src/tests/test.js +++ b/src/tests/test.js @@ -6,7 +6,7 @@ "use strict"; -var ethUtil = require('ethereumjs-util') +var ethUtil = require("ethereumjs-util"); require("../index"); const Trust = window.Trust; const Web3 = require("web3"); @@ -35,7 +35,7 @@ describe("TrustWeb3Provider constructor tests", () => { chainId: 1, rpcUrl: "" }); - const address = mainnet.address + const address = mainnet.address; expect(provider.address).toBe(""); provider.setAddress(address); @@ -91,7 +91,7 @@ describe("TrustWeb3Provider constructor tests", () => { web3.eth.getAccounts((error, accounts) => { expect(accounts).toEqual(addresses); done(); - }) + }); provider.request({method: "eth_accounts"}).then((accounts) => { expect(accounts).toEqual(addresses); @@ -118,7 +118,7 @@ describe("TrustWeb3Provider constructor tests", () => { } } } - } + }; var hash = ethUtil.keccak256(Buffer.from("An amazing message, for use with MetaMask!", "utf8")); var hex = "0x" + hash.toString("hex"); diff --git a/src/utils.js b/src/utils.js index d0062b78..3e6c34bc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,7 +6,7 @@ "use strict"; -import { Buffer } from 'buffer'; +import { Buffer } from "buffer"; class Utils { static genId() { @@ -40,11 +40,11 @@ class Utils { } static hexToBuffer(str) { - return Buffer.from(str.replace('0x', ''), 'hex'); + return Buffer.from(str.replace("0x", ""), "hex"); } static bufferToHex(buf) { - return Buffer.from(buf).toString('hex'); + return Buffer.from(buf).toString("hex"); } }