Skip to content

Commit

Permalink
improve iframe compatibility (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
hewigovens authored Sep 28, 2020
1 parent d75dfef commit a72dd84
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
4 changes: 2 additions & 2 deletions dist/trust-min.js
Git LFS file not shown
19 changes: 19 additions & 0 deletions ios/TrustWeb3Provider/DAppWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
23 changes: 16 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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: [] });
}

Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

"use strict";

import { Buffer } from 'buffer';
import { Buffer } from "buffer";

class Utils {
static genId() {
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit a72dd84

Please sign in to comment.