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-10-02 14:14:35 +00:00
|
|
|
function sendAPIrequest(permission, params) {
|
2018-09-28 13:40:19 +00:00
|
|
|
var messageId = callbackId++;
|
2018-10-02 14:14:35 +00:00
|
|
|
var params = params || {};
|
2018-09-28 13:40:19 +00:00
|
|
|
|
|
|
|
bridgeSend({
|
|
|
|
type: 'api-request',
|
|
|
|
permission: permission,
|
|
|
|
messageId: messageId,
|
2018-12-06 14:57:23 +00:00
|
|
|
params: params,
|
2018-09-28 13:40:19 +00:00
|
|
|
host: window.location.hostname
|
|
|
|
});
|
|
|
|
|
2018-10-02 14:14:35 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
params['resolve'] = resolve;
|
|
|
|
params['reject'] = reject;
|
|
|
|
callbacks[messageId] = params;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function qrCodeResponse(data, callback){
|
|
|
|
var result = data.data;
|
|
|
|
var regex = new RegExp(callback.regex);
|
|
|
|
if (!result) {
|
|
|
|
if (callback.reject) {
|
|
|
|
callback.reject(new Error("Cancelled"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (regex.test(result)) {
|
|
|
|
if (callback.resolve) {
|
|
|
|
callback.resolve(result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (callback.reject) {
|
|
|
|
callback.reject(new Error("Doesn't match"));
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 13:40:19 +00:00
|
|
|
}
|
2018-09-05 15:31:57 +00:00
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
WebViewBridge.onMessage = function (message) {
|
|
|
|
data = JSON.parse(message);
|
2018-09-28 13:40:19 +00:00
|
|
|
var id = data.messageId;
|
|
|
|
var callback = callbacks[id];
|
2016-12-29 18:28:30 +00:00
|
|
|
|
2018-09-28 13:40:19 +00:00
|
|
|
if (callback) {
|
|
|
|
if (data.type === "api-response") {
|
2018-10-02 14:14:35 +00:00
|
|
|
if (data.permission == 'qr-code'){
|
|
|
|
qrCodeResponse(data, callback);
|
|
|
|
} else if (data.isAllowed) {
|
2018-09-28 13:40:19 +00:00
|
|
|
callback.resolve(data.data);
|
|
|
|
} else {
|
|
|
|
callback.reject(new Error("Denied"));
|
2018-08-24 09:58:13 +00:00
|
|
|
}
|
2018-09-28 13:40:19 +00:00
|
|
|
} else if (data.type === "web3-send-async-callback") {
|
|
|
|
var id = data.messageId;
|
|
|
|
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-09-13 14:07:42 +00:00
|
|
|
}
|
2018-09-28 13:40:19 +00:00
|
|
|
}
|
2018-09-13 14:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 18:28:30 +00:00
|
|
|
};
|
|
|
|
|
2018-09-28 13:40:19 +00:00
|
|
|
var StatusAPI = function () {};
|
|
|
|
|
|
|
|
StatusAPI.prototype.getContactCode = function () {
|
|
|
|
return sendAPIrequest('contact-code');
|
|
|
|
};
|
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
var StatusHttpProvider = function () {};
|
|
|
|
|
2018-05-11 16:32:38 +00:00
|
|
|
StatusHttpProvider.prototype.isStatus = true;
|
2018-09-28 13:40:19 +00:00
|
|
|
StatusHttpProvider.prototype.status = new StatusAPI();
|
2018-10-02 14:14:35 +00:00
|
|
|
StatusHttpProvider.prototype.isConnected = function () { return true; };
|
2018-09-28 13:40:19 +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);});
|
|
|
|
};
|
2018-10-02 14:14:35 +00:00
|
|
|
|
|
|
|
StatusHttpProvider.prototype.scanQRCode = function (regex) {
|
|
|
|
return sendAPIrequest('qr-code', {regex: regex});
|
|
|
|
};
|
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
|
|
|
}
|