web3 coinbase async fix

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
Andrey Shovkoplyas 2018-05-27 12:13:02 +03:00
parent 26dd1c1f5a
commit af7c356134
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
1 changed files with 38 additions and 25 deletions

View File

@ -24,26 +24,57 @@ var StatusHttpProvider = function (host, timeout) {
StatusHttpProvider.prototype.isStatus = true; StatusHttpProvider.prototype.isStatus = true;
function syncResponse(payload, result){ function web3Response (payload, result){
return {id: payload.id, return {id: payload.id,
jsonrpc: "2.0", jsonrpc: "2.0",
result: result}; result: result};
} }
function getSyncResponse (payload) {
if (payload.method == "eth_accounts"){
return web3Response(payload, [currentAccountAddress])
} else if (payload.method == "eth_coinbase"){
return web3Response(payload, currentAccountAddress)
} else if (payload.method == "net_version"){
return web3Response(payload, networkId)
} else {
return null;
}
}
StatusHttpProvider.prototype.send = function (payload) { 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 //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
if (payload.method == "eth_accounts"){ var syncResponse = getSyncResponse(payload);
return syncResponse(payload, [currentAccountAddress]) if (syncResponse){
} else if (payload.method == "eth_coinbase"){ return syncResponse;
return syncResponse(payload, currentAccountAddress)
} else if (payload.method == "net_version"){
return syncResponse(payload, networkId)
} else { } else {
alert('You tried to send "' + payload.method + '" synchronously. Synchronous requests are not supported, sorry.'); alert('You tried to send "' + payload.method + '" synchronously. Synchronous requests are not supported, sorry.');
return null; return null;
} }
}; };
StatusHttpProvider.prototype.sendAsync = function (payload, callback) {
var syncResponse = getSyncResponse(payload);
if (syncResponse){
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));
}
}
};
StatusHttpProvider.prototype.prepareRequest = function () { StatusHttpProvider.prototype.prepareRequest = function () {
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
@ -52,24 +83,6 @@ StatusHttpProvider.prototype.prepareRequest = function () {
return request; return request;
}; };
function sendAsync(payload, callback) {
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));
}
};
StatusHttpProvider.prototype.sendAsync = sendAsync;
/** /**
* Synchronously tries to make Http request * Synchronously tries to make Http request
* *