Skip to content

Commit a72dd84

Browse files
authored
improve iframe compatibility (#103)
1 parent d75dfef commit a72dd84

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

dist/trust-min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:41ad79db8f78e8c50aeb46d628328f0a870570bee906f068e4a0ba7d9055d1ec
3-
size 534862
2+
oid sha256:93ffe1eac4c7c904c92ce870e6e334ce8037191b4208ee90fd45b57592168e0a
3+
size 535058

ios/TrustWeb3Provider/DAppWebViewController.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,23 @@ extension DAppWebViewController: WKUIDelegate {
210210
_ = webView.load(navigationAction.request)
211211
return nil
212212
}
213+
214+
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
215+
let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
216+
alert.addAction(.init(title: "OK", style: .default, handler: { _ in
217+
completionHandler()
218+
}))
219+
present(alert, animated: true, completion: nil)
220+
}
221+
222+
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
223+
let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
224+
alert.addAction(.init(title: "OK", style: .default, handler: { _ in
225+
completionHandler(true)
226+
}))
227+
alert.addAction(.init(title: "Cancel", style: .cancel, handler: { _ in
228+
completionHandler(false)
229+
}))
230+
present(alert, animated: true, completion: nil)
231+
}
213232
}

src/index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import ProviderRpcError from "./error";
1212
import Utils from "./utils";
1313
import IdMapping from "./id_mapping";
1414
import { EventEmitter } from "events";
15-
import isUtf8 from 'isutf8';
15+
import isUtf8 from "isutf8";
1616

1717
class TrustWeb3Provider extends EventEmitter {
1818
constructor(config) {
@@ -44,7 +44,7 @@ class TrustWeb3Provider extends EventEmitter {
4444
// this points to window in methods like web3.eth.getAccounts()
4545
var that = this;
4646
if (!(this instanceof TrustWeb3Provider)) {
47-
that = window.ethereum
47+
that = window.ethereum;
4848
}
4949
return that._request(payload, false);
5050
}
@@ -60,7 +60,7 @@ class TrustWeb3Provider extends EventEmitter {
6060
* @deprecated Use request({method: "eth_requestAccounts"}) instead.
6161
*/
6262
enable() {
63-
console.log('enable() is deprecated, please use window.ethereum.request({method: "eth_requestAccounts"}) instead.')
63+
console.log("enable() is deprecated, please use window.ethereum.request({method: \"eth_requestAccounts\"}) instead.");
6464
return this.request({ method: "eth_requestAccounts", params: [] });
6565
}
6666

@@ -95,11 +95,11 @@ class TrustWeb3Provider extends EventEmitter {
9595
* @deprecated Use request() method instead.
9696
*/
9797
sendAsync(payload, callback) {
98-
console.log('sendAsync(data, callback) is deprecated, please use window.ethereum.request(data) instead.')
98+
console.log("sendAsync(data, callback) is deprecated, please use window.ethereum.request(data) instead.");
9999
// this points to window in methods like web3.eth.getAccounts()
100100
var that = this;
101101
if (!(this instanceof TrustWeb3Provider)) {
102-
that = window.ethereum
102+
that = window.ethereum;
103103
}
104104
if (Array.isArray(payload)) {
105105
Promise.all(payload.map(that._request.bind(that)))
@@ -208,7 +208,7 @@ class TrustWeb3Provider extends EventEmitter {
208208
} else {
209209
buffer = Buffer.from(data);
210210
}
211-
const hex = buffer.toString('hex');
211+
const hex = buffer.toString("hex");
212212
if (isUtf8(buffer)) {
213213
this.postMessage("signPersonalMessage", payload.id, {data: hex});
214214
} else {
@@ -266,11 +266,20 @@ class TrustWeb3Provider extends EventEmitter {
266266
data.result = result;
267267
}
268268
if (this.isDebug) {
269-
console.log(`<== sendResponse id: ${id}, result: ${result}, data: ${JSON.stringify(data)}`);
269+
console.log(`<== sendResponse id: ${id}, result: ${JSON.stringify(result)}, data: ${JSON.stringify(data)}`);
270270
}
271271
if (callback) {
272272
wrapResult ? callback(null, data) : callback(null, result);
273273
this.callbacks.delete(id);
274+
} else {
275+
console.log(`callback id: ${id} not found`);
276+
// check if it's iframe callback
277+
for (var i = 0; i < window.frames.length; i++) {
278+
const frame = window.frames[i];
279+
if (frame.ethereum.callbacks.has(id)) {
280+
frame.ethereum.sendResponse(id, result);
281+
}
282+
}
274283
}
275284
}
276285

src/tests/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
"use strict";
88

9-
var ethUtil = require('ethereumjs-util')
9+
var ethUtil = require("ethereumjs-util");
1010
require("../index");
1111
const Trust = window.Trust;
1212
const Web3 = require("web3");
@@ -35,7 +35,7 @@ describe("TrustWeb3Provider constructor tests", () => {
3535
chainId: 1,
3636
rpcUrl: ""
3737
});
38-
const address = mainnet.address
38+
const address = mainnet.address;
3939
expect(provider.address).toBe("");
4040

4141
provider.setAddress(address);
@@ -91,7 +91,7 @@ describe("TrustWeb3Provider constructor tests", () => {
9191
web3.eth.getAccounts((error, accounts) => {
9292
expect(accounts).toEqual(addresses);
9393
done();
94-
})
94+
});
9595

9696
provider.request({method: "eth_accounts"}).then((accounts) => {
9797
expect(accounts).toEqual(addresses);
@@ -118,7 +118,7 @@ describe("TrustWeb3Provider constructor tests", () => {
118118
}
119119
}
120120
}
121-
}
121+
};
122122

123123
var hash = ethUtil.keccak256(Buffer.from("An amazing message, for use with MetaMask!", "utf8"));
124124
var hex = "0x" + hash.toString("hex");

src/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
"use strict";
88

9-
import { Buffer } from 'buffer';
9+
import { Buffer } from "buffer";
1010

1111
class Utils {
1212
static genId() {
@@ -40,11 +40,11 @@ class Utils {
4040
}
4141

4242
static hexToBuffer(str) {
43-
return Buffer.from(str.replace('0x', ''), 'hex');
43+
return Buffer.from(str.replace("0x", ""), "hex");
4444
}
4545

4646
static bufferToHex(buf) {
47-
return Buffer.from(buf).toString('hex');
47+
return Buffer.from(buf).toString("hex");
4848
}
4949
}
5050

0 commit comments

Comments
 (0)