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 = {};
|
|
|
|
|
|
|
|
function httpCallback(id, data) {
|
|
|
|
var result = data;
|
|
|
|
var error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = JSON.parse(data);
|
2017-02-01 08:51:20 +00:00
|
|
|
} catch (e) {
|
2016-12-29 18:28:30 +00:00
|
|
|
error = {message: "InvalidResponse"};
|
|
|
|
}
|
|
|
|
|
2017-02-01 08:51:20 +00:00
|
|
|
if (callbacks[id]) {
|
|
|
|
callbacks[id](error, result);
|
|
|
|
}
|
2016-12-29 18:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var StatusHttpProvider = function (host, timeout) {
|
|
|
|
this.host = host || 'http://localhost:8545';
|
|
|
|
this.timeout = timeout || 0;
|
|
|
|
};
|
|
|
|
|
2018-05-11 16:32:38 +00:00
|
|
|
StatusHttpProvider.prototype.isStatus = true;
|
|
|
|
|
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;
|
|
|
|
if (typeof StatusBridge == "undefined") {
|
|
|
|
var data = {
|
|
|
|
payload: JSON.stringify(payload),
|
|
|
|
callbackId: JSON.stringify(messageId),
|
|
|
|
host: this.host
|
|
|
|
};
|
|
|
|
|
|
|
|
webkit.messageHandlers.sendRequest.postMessage(JSON.stringify(data));
|
|
|
|
} else {
|
|
|
|
StatusBridge.sendRequest(this.host, JSON.stringify(messageId), JSON.stringify(payload));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-01 08:51:20 +00:00
|
|
|
StatusHttpProvider.prototype.prepareRequest = function () {
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
|
2017-03-08 11:45:02 +00:00
|
|
|
request.open('POST', this.host, false);
|
2017-02-01 08:51:20 +00:00
|
|
|
request.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
return request;
|
|
|
|
};
|
|
|
|
|
2016-12-29 18:28:30 +00:00
|
|
|
/**
|
|
|
|
* Synchronously tries to make Http request
|
|
|
|
*
|
|
|
|
* @method isConnected
|
|
|
|
* @return {Boolean} returns true if request haven't failed. Otherwise false
|
|
|
|
*/
|
2017-02-01 08:51:20 +00:00
|
|
|
StatusHttpProvider.prototype.isConnected = function () {
|
2016-12-29 18:28:30 +00:00
|
|
|
try {
|
|
|
|
this.sendAsync({
|
|
|
|
id: 9999999999,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
method: 'net_listening',
|
|
|
|
params: []
|
2017-02-01 08:51:20 +00:00
|
|
|
}, function () {
|
|
|
|
});
|
2016-12-29 18:28:30 +00:00
|
|
|
return true;
|
2017-02-01 08:51:20 +00:00
|
|
|
} catch (e) {
|
2016-12-29 18:28:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
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
|
2017-03-08 11:45:02 +00:00
|
|
|
var address = providerAddress || "http://localhost:8545";
|
2017-02-22 14:54:32 +00:00
|
|
|
console.log(protocol);
|
|
|
|
if (typeof web3 === "undefined") {
|
2017-04-18 14:22:05 +00:00
|
|
|
if (protocol == "https:" || protocol == "http:") {
|
2017-02-22 14:54:32 +00:00
|
|
|
console.log("StatusHttpProvider");
|
2017-03-08 11:45:02 +00:00
|
|
|
web3 = new Web3(new StatusHttpProvider(address));
|
2018-04-25 12:41:17 +00:00
|
|
|
web3.eth.defaultAccount = currentAccountAddress;
|
2017-02-22 14:54:32 +00:00
|
|
|
}
|
2017-02-21 14:44:49 +00:00
|
|
|
}
|