2020-09-22 19:16:44 +00:00
|
|
|
(function(){
|
|
|
|
// Based on
|
|
|
|
// https://github.com/status-im/status-react/blob/f9fb4d6974138a276b0cdcc6e4ea1611063e70ca/resources/js/provider.js
|
|
|
|
|
|
|
|
if(typeof EthereumProvider === "undefined"){
|
2020-09-24 02:52:41 +00:00
|
|
|
let callbackId = 0;
|
|
|
|
let callbacks = {};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
const onMessage = function(message){
|
|
|
|
const data = JSON.parse(message);
|
|
|
|
const id = data.messageId;
|
|
|
|
const callback = callbacks[id];
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
if (callback) {
|
|
|
|
if (data.type === "api-response") {
|
|
|
|
if (data.permission == "qr-code") {
|
|
|
|
qrCodeResponse(data, callback); // TODO: are we going to support the qr-code permission?
|
|
|
|
} else if (data.isAllowed) {
|
|
|
|
if (data.permission == "web3") {
|
|
|
|
window.statusAppcurrentAccountAddress = data.data[0];
|
|
|
|
}
|
|
|
|
callback.resolve(data.data);
|
|
|
|
} else {
|
|
|
|
callback.reject(new UserRejectedRequest());
|
|
|
|
}
|
|
|
|
} else if (data.type === "web3-send-async-callback") {
|
|
|
|
if (callback.beta) {
|
|
|
|
if (data.error) {
|
|
|
|
if (data.error.code == 4100) {
|
|
|
|
callback.reject(new Unauthorized());
|
|
|
|
} else {
|
|
|
|
callback.reject(data.error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback.resolve(data.result.result);
|
|
|
|
}
|
|
|
|
} else 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let backend;
|
|
|
|
new QWebChannel(qt.webChannelTransport, function(channel) {
|
|
|
|
backend = channel.objects.backend;
|
|
|
|
backend.web3Response.connect(onMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
const bridgeSend = data => backend.postMessage(JSON.stringify(data));
|
|
|
|
|
|
|
|
let history = window.history;
|
|
|
|
let pushState = history.pushState;
|
|
|
|
history.pushState = function(state) {
|
2020-09-22 19:16:44 +00:00
|
|
|
setTimeout(function () {
|
2020-09-24 02:52:41 +00:00
|
|
|
bridgeSend({
|
|
|
|
type: "history-state-changed",
|
|
|
|
navState: { url: location.href, title: document.title },
|
|
|
|
});
|
2020-09-22 19:16:44 +00:00
|
|
|
}, 100);
|
|
|
|
return pushState.apply(history, arguments);
|
2020-09-24 02:52:41 +00:00
|
|
|
};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
function sendAPIrequest(permission, params) {
|
|
|
|
const messageId = callbackId++;
|
|
|
|
params = params || {};
|
|
|
|
|
2020-09-22 19:16:44 +00:00
|
|
|
bridgeSend({
|
|
|
|
type: 'api-request',
|
|
|
|
permission: permission,
|
|
|
|
messageId: messageId,
|
|
|
|
params: params
|
2020-09-24 02:52:41 +00:00
|
|
|
});
|
|
|
|
|
2020-09-22 19:16:44 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
2020-09-24 02:52:41 +00:00
|
|
|
params['resolve'] = resolve;
|
|
|
|
params['reject'] = reject;
|
|
|
|
callbacks[messageId] = params;
|
2020-09-22 19:16:44 +00:00
|
|
|
});
|
2020-09-24 02:52:41 +00:00
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
function qrCodeResponse(data, callback){
|
|
|
|
const result = data.data;
|
|
|
|
const regex = new RegExp(callback.regex);
|
2020-09-22 19:16:44 +00:00
|
|
|
if (!result) {
|
2020-09-24 02:52:41 +00:00
|
|
|
if (callback.reject) {
|
|
|
|
callback.reject(new Error("Cancelled"));
|
|
|
|
}
|
|
|
|
} else if (regex.test(result)) {
|
|
|
|
if (callback.resolve) {
|
|
|
|
callback.resolve(result);
|
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
} else {
|
2020-09-24 02:52:41 +00:00
|
|
|
if (callback.reject) {
|
|
|
|
callback.reject(new Error("Doesn't match"));
|
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
}
|
2020-09-24 02:52:41 +00:00
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
function Unauthorized() {
|
|
|
|
this.name = "Unauthorized";
|
|
|
|
this.id = 4100;
|
|
|
|
this.code = 4100;
|
|
|
|
this.message = "The requested method and/or account has not been authorized by the user.";
|
|
|
|
}
|
|
|
|
Unauthorized.prototype = Object.create(Error.prototype);
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
function UserRejectedRequest() {
|
|
|
|
this.name = "UserRejectedRequest";
|
|
|
|
this.id = 4001;
|
|
|
|
this.code = 4001;
|
|
|
|
this.message = "The user rejected the request.";
|
|
|
|
}
|
|
|
|
UserRejectedRequest.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
|
|
function web3Response (payload, result){
|
|
|
|
return {
|
|
|
|
id: payload.id,
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
result: result
|
|
|
|
};
|
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
function getSyncResponse (payload) {
|
|
|
|
if (payload.method == "eth_accounts" && (typeof window.statusAppcurrentAccountAddress !== "undefined")) {
|
|
|
|
return web3Response(payload, [window.statusAppcurrentAccountAddress])
|
|
|
|
} else if (payload.method == "eth_coinbase" && (typeof window.statusAppcurrentAccountAddress !== "undefined")) {
|
|
|
|
return web3Response(payload, window.statusAppcurrentAccountAddress)
|
|
|
|
} else if (payload.method == "net_version" || payload.method == "eth_chainId"){
|
|
|
|
return web3Response(payload, backend.networkId)
|
|
|
|
} else if (payload.method == "eth_uninstallFilter"){
|
|
|
|
return web3Response(payload, true);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
var StatusAPI = function () {};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
StatusAPI.prototype.getContactCode = function () {
|
|
|
|
return sendAPIrequest('contact-code');
|
|
|
|
};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
var EthereumProvider = function () {};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
EthereumProvider.prototype.isStatus = true;
|
|
|
|
EthereumProvider.prototype.status = new StatusAPI();
|
|
|
|
EthereumProvider.prototype.isConnected = function () { return true; };
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
EthereumProvider.prototype.enable = function () {
|
|
|
|
return sendAPIrequest('web3');
|
|
|
|
};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
EthereumProvider.prototype.scanQRCode = function (regex) {
|
|
|
|
return sendAPIrequest('qr-code', {regex: regex});
|
|
|
|
};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
EthereumProvider.prototype.request = function (requestArguments) {
|
|
|
|
if (!requestArguments) return new Error("Request is not valid.");
|
|
|
|
|
|
|
|
const method = requestArguments.method;
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
if (!method) return new Error("Request is not valid.");
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
//Support for legacy send method
|
|
|
|
if (typeof method !== "string") return this.sendSync(method);
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
if (method == "eth_requestAccounts") return sendAPIrequest("web3");
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
const syncResponse = getSyncResponse({ method: method });
|
|
|
|
if (syncResponse) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
resolve(syncResponse.result);
|
|
|
|
});
|
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
const messageId = callbackId++;
|
|
|
|
const payload = {
|
|
|
|
id: messageId,
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
method: method,
|
|
|
|
params: requestArguments.params,
|
|
|
|
};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
bridgeSend({
|
|
|
|
type: "web3-send-async-read-only",
|
|
|
|
messageId: messageId,
|
|
|
|
payload: payload,
|
|
|
|
});
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
callbacks[messageId] = {
|
|
|
|
beta: true,
|
|
|
|
resolve: resolve,
|
|
|
|
reject: reject,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
// (DEPRECATED) Support for legacy send method
|
|
|
|
EthereumProvider.prototype.send = function (method, params = []) {
|
2020-09-22 19:16:44 +00:00
|
|
|
return this.request({method: method, params: params});
|
2020-09-24 02:52:41 +00:00
|
|
|
}
|
2020-09-22 19:16:44 +00:00
|
|
|
|
2020-09-24 02:52:41 +00:00
|
|
|
// (DEPRECATED) Support for legacy sendSync method
|
|
|
|
EthereumProvider.prototype.sendSync = function (payload) {
|
|
|
|
if (payload.method == "eth_uninstallFilter") {
|
|
|
|
this.sendAsync(payload, function (res, err) {});
|
2020-09-22 19:16:44 +00:00
|
|
|
}
|
2020-09-24 02:52:41 +00:00
|
|
|
const syncResponse = getSyncResponse(payload);
|
|
|
|
if (syncResponse) return syncResponse;
|
|
|
|
|
|
|
|
return web3Response(payload, null);
|
|
|
|
};
|
|
|
|
|
|
|
|
// (DEPRECATED) Support for legacy sendAsync method
|
|
|
|
EthereumProvider.prototype.sendAsync = function (payload, callback) {
|
|
|
|
const syncResponse = getSyncResponse(payload);
|
|
|
|
if (syncResponse && callback) {
|
2020-09-22 19:16:44 +00:00
|
|
|
callback(null, syncResponse);
|
2020-09-24 02:52:41 +00:00
|
|
|
} else {
|
|
|
|
const messageId = callbackId++;
|
|
|
|
|
|
|
|
if (Array.isArray(payload)) {
|
|
|
|
callbacks[messageId] = {
|
|
|
|
num: payload.length,
|
|
|
|
results: [],
|
|
|
|
callback: callback,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i in payload) {
|
|
|
|
bridgeSend({
|
|
|
|
type: "web3-send-async-read-only",
|
|
|
|
messageId: messageId,
|
|
|
|
payload: payload[i],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callbacks[messageId] = { callback: callback };
|
|
|
|
bridgeSend({
|
|
|
|
type: "web3-send-async-read-only",
|
|
|
|
messageId: messageId,
|
|
|
|
payload: payload,
|
|
|
|
});
|
2020-09-22 19:16:44 +00:00
|
|
|
}
|
2020-09-24 02:52:41 +00:00
|
|
|
}
|
|
|
|
};
|
2020-09-22 19:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.ethereum = new EthereumProvider();
|
|
|
|
})();
|