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-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")
|
|
|
|
{
|
|
|
|
window.STATUS_API = data.data;
|
|
|
|
window.postMessage({ type: 'STATUS_API_SUCCESS', permissions: data.keys }, "*");
|
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;
|
|
|
|
if (callbacks[id]) {
|
|
|
|
callbacks[id](data.error, data.result);
|
|
|
|
}
|
|
|
|
}
|
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++;
|
|
|
|
callbacks[messageId] = callback;
|
2018-08-10 17:15:16 +00:00
|
|
|
WebViewBridge.send(JSON.stringify({type: 'web3-send-async',
|
|
|
|
messageId: messageId,
|
|
|
|
payload: payload}));
|
2016-12-29 18:28:30 +00:00
|
|
|
}
|
|
|
|
};
|
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-08-10 17:15:16 +00:00
|
|
|
web3 = new Web3(new StatusHttpProvider());
|
|
|
|
web3.eth.defaultAccount = currentAccountAddress; // currentAccountAddress - injected from status-react
|
2017-02-22 14:54:32 +00:00
|
|
|
}
|
2017-02-21 14:44:49 +00:00
|
|
|
}
|