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;
|
|
|
|
};
|
|
|
|
|
|
|
|
StatusHttpProvider.prototype.send = function (payload) {
|
2017-02-01 08:51:20 +00:00
|
|
|
if (typeof StatusBridge == "undefined") {
|
|
|
|
if (window.location.protocol == "https:") {
|
|
|
|
throw new Error('You tried to send "' + payload.method + '" synchronously. Synchronous requests are not supported, sorry.');
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = this.prepareRequest(false);
|
|
|
|
|
|
|
|
try {
|
|
|
|
request.send(JSON.stringify(payload));
|
|
|
|
} catch (error) {
|
|
|
|
throw errors.InvalidConnection(this.host);
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = request.responseText;
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = JSON.parse(result);
|
|
|
|
} catch (e) {
|
|
|
|
throw errors.InvalidResponse(request.responseText);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} else {
|
2017-03-08 11:45:02 +00:00
|
|
|
result = StatusBridge.sendRequestSync(this.host, JSON.stringify(payload));
|
2017-02-01 08:51:20 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
result = JSON.parse(result);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error("InvalidResponse: " + result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2016-12-29 18:28:30 +00:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
function sendAsync(payload, callback) {
|
2016-12-29 18:28:30 +00:00
|
|
|
|
|
|
|
var messageId = callbackId++;
|
|
|
|
callbacks[messageId] = callback;
|
2017-02-01 08:51:20 +00:00
|
|
|
if (typeof StatusBridge == "undefined") {
|
2016-12-29 18:28:30 +00:00
|
|
|
var data = {
|
|
|
|
payload: JSON.stringify(payload),
|
2017-03-08 11:45:02 +00:00
|
|
|
callbackId: JSON.stringify(messageId),
|
|
|
|
host: this.host
|
2016-12-29 18:28:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
webkit.messageHandlers.sendRequest.postMessage(JSON.stringify(data));
|
|
|
|
} else {
|
2017-03-08 11:45:02 +00:00
|
|
|
StatusBridge.sendRequest(this.host, JSON.stringify(messageId), JSON.stringify(payload));
|
2016-12-29 18:28:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-01 08:51:20 +00:00
|
|
|
StatusHttpProvider.prototype.sendAsync = sendAsync;
|
|
|
|
|
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
|
|
|
}
|