2017-02-22 14:54:32 +00:00
|
|
|
if(typeof StatusHttpProvider === "undefined"){
|
2016-12-29 18:28:30 +00:00
|
|
|
var callbackId = 0;
|
|
|
|
var callbacks = {};
|
|
|
|
|
2018-08-24 09:58:13 +00:00
|
|
|
function bridgeSend(data){
|
|
|
|
WebViewBridge.send(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:31:57 +00:00
|
|
|
window.addEventListener('message', function (event) {
|
|
|
|
if (!event.data || !event.data.type) { return; }
|
|
|
|
if (event.data.type === 'STATUS_API_REQUEST') {
|
|
|
|
bridgeSend({
|
|
|
|
type: 'status-api-request',
|
|
|
|
permissions: event.data.permissions,
|
|
|
|
host: window.location.hostname
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
WebViewBridge.onMessage = function (message) {
|
|
|
|
data = JSON.parse(message);
|
2016-12-29 18:28:30 +00:00
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
if (data.type === "navigate-to-blank")
|
|
|
|
window.location.href = "about:blank";
|
2016-12-29 18:28:30 +00:00
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
else if (data.type === "status-api-success")
|
|
|
|
{
|
2018-09-05 15:31:57 +00:00
|
|
|
window.dispatchEvent(new CustomEvent('statusapi', { detail: { permissions: data.keys,
|
|
|
|
data: data.data
|
|
|
|
} }));
|
2017-02-01 08:51:20 +00:00
|
|
|
}
|
2016-12-29 18:28:30 +00:00
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
else if (data.type === "web3-send-async-callback")
|
|
|
|
{
|
|
|
|
var id = data.messageId;
|
2018-08-24 09:58:13 +00:00
|
|
|
var callback = callbacks[id];
|
|
|
|
if (callback) {
|
|
|
|
if (callback.results)
|
|
|
|
{
|
|
|
|
callback.results.push(data.error || data.result);
|
|
|
|
if (callback.results.length == callback.num)
|
|
|
|
callback.callback(undefined, callback.results);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback.callback(data.error, data.result);
|
|
|
|
}
|
2018-08-10 17:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 18:28:30 +00:00
|
|
|
};
|
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
var StatusHttpProvider = function () {};
|
|
|
|
|
2018-05-11 16:32:38 +00:00
|
|
|
StatusHttpProvider.prototype.isStatus = true;
|
2018-08-10 17:15:16 +00:00
|
|
|
StatusHttpProvider.prototype.isConnected = function () { return true; };
|
2018-05-11 16:32:38 +00:00
|
|
|
|
2018-05-27 09:13:02 +00:00
|
|
|
function web3Response (payload, result){
|
2018-05-08 11:05:15 +00:00
|
|
|
return {id: payload.id,
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
result: result};
|
|
|
|
}
|
2017-02-01 08:51:20 +00:00
|
|
|
|
2018-05-27 09:13:02 +00:00
|
|
|
function getSyncResponse (payload) {
|
2018-05-08 11:05:15 +00:00
|
|
|
if (payload.method == "eth_accounts"){
|
2018-05-27 09:13:02 +00:00
|
|
|
return web3Response(payload, [currentAccountAddress])
|
2018-05-08 11:05:15 +00:00
|
|
|
} else if (payload.method == "eth_coinbase"){
|
2018-05-27 09:13:02 +00:00
|
|
|
return web3Response(payload, currentAccountAddress)
|
2018-05-08 11:05:15 +00:00
|
|
|
} else if (payload.method == "net_version"){
|
2018-05-27 09:13:02 +00:00
|
|
|
return web3Response(payload, networkId)
|
2018-06-26 15:06:18 +00:00
|
|
|
} else if (payload.method == "eth_uninstallFilter"){
|
|
|
|
return web3Response(payload, true);
|
2018-05-27 09:13:02 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusHttpProvider.prototype.send = function (payload) {
|
|
|
|
//TODO to be compatible with MM https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#dizzy-all-async---think-of-metamask-as-a-light-client
|
2018-06-26 15:06:18 +00:00
|
|
|
if (payload.method == "eth_uninstallFilter"){
|
|
|
|
this.sendAsync(payload, function (res, err) {})
|
|
|
|
}
|
2018-05-27 09:13:02 +00:00
|
|
|
var syncResponse = getSyncResponse(payload);
|
|
|
|
if (syncResponse){
|
|
|
|
return syncResponse;
|
2017-02-01 08:51:20 +00:00
|
|
|
} else {
|
2018-05-08 11:05:15 +00:00
|
|
|
alert('You tried to send "' + payload.method + '" synchronously. Synchronous requests are not supported, sorry.');
|
|
|
|
return null;
|
2017-02-01 08:51:20 +00:00
|
|
|
}
|
2016-12-29 18:28:30 +00:00
|
|
|
};
|
|
|
|
|
2018-05-27 09:13:02 +00:00
|
|
|
StatusHttpProvider.prototype.sendAsync = function (payload, callback) {
|
|
|
|
var syncResponse = getSyncResponse(payload);
|
2018-06-26 15:06:18 +00:00
|
|
|
if (syncResponse && callback){
|
2018-05-27 09:13:02 +00:00
|
|
|
callback(null, syncResponse);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var messageId = callbackId++;
|
2018-08-24 09:58:13 +00:00
|
|
|
|
|
|
|
if (Array.isArray(payload))
|
|
|
|
{
|
|
|
|
callbacks[messageId] = {num: payload.length,
|
|
|
|
results: [],
|
|
|
|
callback: callback};
|
|
|
|
for (var i in payload) {
|
|
|
|
bridgeSend({type: 'web3-send-async',
|
|
|
|
messageId: messageId,
|
|
|
|
payload: payload[i]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callbacks[messageId] = {callback: callback};
|
|
|
|
bridgeSend({type: 'web3-send-async',
|
|
|
|
messageId: messageId,
|
|
|
|
payload: payload});
|
|
|
|
}
|
|
|
|
|
2016-12-29 18:28:30 +00:00
|
|
|
}
|
|
|
|
};
|
2018-09-05 15:31:57 +00:00
|
|
|
|
|
|
|
StatusHttpProvider.prototype.enable = function () {
|
|
|
|
return new Promise(function (resolve, reject) { setTimeout(resolve, 1000);});
|
|
|
|
};
|
2017-02-22 14:54:32 +00:00
|
|
|
}
|
2016-12-29 18:28:30 +00:00
|
|
|
|
2017-02-22 14:54:32 +00:00
|
|
|
var protocol = window.location.protocol
|
|
|
|
if (typeof web3 === "undefined") {
|
2018-08-10 17:15:16 +00:00
|
|
|
//why do we need this condition?
|
2017-04-18 14:22:05 +00:00
|
|
|
if (protocol == "https:" || protocol == "http:") {
|
2017-02-22 14:54:32 +00:00
|
|
|
console.log("StatusHttpProvider");
|
2018-09-05 15:31:57 +00:00
|
|
|
ethereum = new StatusHttpProvider();
|
|
|
|
web3 = new Web3(ethereum);
|
|
|
|
web3.eth.defaultAccount = currentAccountAddress;
|
2017-02-22 14:54:32 +00:00
|
|
|
}
|
2017-02-21 14:44:49 +00:00
|
|
|
}
|