2018-09-05 15:31:57 +00:00
|
|
|
if(typeof ReadOnlyProvider === "undefined"){
|
|
|
|
var callbackId = 0;
|
|
|
|
var callbacks = {};
|
2018-11-08 14:48:11 +00:00
|
|
|
var currentAccountAddress;
|
2018-09-05 15:31:57 +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,
|
2018-12-06 14:57:23 +00:00
|
|
|
messageId: messageId,
|
|
|
|
params: params
|
2018-09-28 13:40:19 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
|
|
|
|
WebViewBridge.onMessage = function (message) {
|
|
|
|
data = JSON.parse(message);
|
2018-09-28 13:40:19 +00:00
|
|
|
var id = data.messageId;
|
|
|
|
var callback = callbacks[id];
|
2018-09-05 15:31:57 +00:00
|
|
|
|
2018-10-02 14:14:35 +00:00
|
|
|
if (callback) {
|
|
|
|
if (data.type === "api-response") {
|
|
|
|
if (data.permission == 'qr-code'){
|
|
|
|
qrCodeResponse(data, callback);
|
|
|
|
} else if (data.isAllowed) {
|
|
|
|
if (data.permission == 'web3') {
|
2018-11-08 14:48:11 +00:00
|
|
|
currentAccountAddress = data.data[0];
|
2018-09-28 13:40:19 +00:00
|
|
|
}
|
2018-11-08 14:48:11 +00:00
|
|
|
callback.resolve(data.data);
|
2018-10-02 14:14:35 +00:00
|
|
|
} else {
|
2018-12-25 14:35:03 +00:00
|
|
|
callback.reject({code: 4001, message: "User denied authorizing any accounts for the dapp."});
|
2018-09-28 13:40:19 +00:00
|
|
|
}
|
2018-10-02 14:14:35 +00:00
|
|
|
} else if (data.type === "web3-send-async-callback") {
|
|
|
|
var id = data.messageId;
|
|
|
|
var callback = callbacks[id];
|
|
|
|
if (callback) {
|
2018-12-25 14:35:03 +00:00
|
|
|
if (callback.beta){
|
|
|
|
if (data.error || data.result.error){
|
|
|
|
callback.reject(data.error || data.result.error);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
callback.resolve(data.result.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (callback.results) {
|
2018-10-02 14:14:35 +00:00
|
|
|
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-05 15:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function web3Response (payload, result){
|
|
|
|
return {id: payload.id,
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
result: result};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSyncResponse (payload) {
|
|
|
|
if (payload.method == "eth_accounts" && currentAccountAddress){
|
|
|
|
return web3Response(payload, [currentAccountAddress])
|
|
|
|
} else if (payload.method == "eth_coinbase" && currentAccountAddress){
|
|
|
|
return web3Response(payload, currentAccountAddress)
|
|
|
|
} else if (payload.method == "net_version"){
|
|
|
|
return web3Response(payload, networkId)
|
|
|
|
} else if (payload.method == "eth_uninstallFilter"){
|
|
|
|
return web3Response(payload, true);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:40:19 +00:00
|
|
|
var StatusAPI = function () {};
|
|
|
|
|
|
|
|
StatusAPI.prototype.getContactCode = function () {
|
|
|
|
return sendAPIrequest('contact-code');
|
|
|
|
};
|
|
|
|
|
2018-12-06 14:57:23 +00:00
|
|
|
StatusAPI.prototype.installExtension = function (uri) {
|
|
|
|
return sendAPIrequest('install-extension', {uri: uri});
|
|
|
|
};
|
|
|
|
|
2018-09-05 15:31:57 +00:00
|
|
|
var ReadOnlyProvider = function () {};
|
|
|
|
|
2018-09-26 10:52:03 +00:00
|
|
|
ReadOnlyProvider.prototype.isStatus = true;
|
2018-10-02 14:14:35 +00:00
|
|
|
ReadOnlyProvider.prototype.status = new StatusAPI();
|
2018-09-05 15:31:57 +00:00
|
|
|
ReadOnlyProvider.prototype.isConnected = function () { return true; };
|
|
|
|
|
2018-10-02 14:14:35 +00:00
|
|
|
ReadOnlyProvider.prototype.enable = function () {
|
|
|
|
return sendAPIrequest('web3');
|
|
|
|
};
|
|
|
|
|
|
|
|
ReadOnlyProvider.prototype.scanQRCode = function (regex) {
|
|
|
|
return sendAPIrequest('qr-code', {regex: regex});
|
|
|
|
};
|
2018-09-28 13:40:19 +00:00
|
|
|
|
2018-09-05 15:31:57 +00:00
|
|
|
ReadOnlyProvider.prototype.send = function (payload) {
|
|
|
|
if (payload.method == "eth_uninstallFilter"){
|
|
|
|
this.sendAsync(payload, function (res, err) {})
|
|
|
|
}
|
|
|
|
var syncResponse = getSyncResponse(payload);
|
|
|
|
if (syncResponse){
|
|
|
|
return syncResponse;
|
|
|
|
} else {
|
|
|
|
return web3Response(payload, null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-25 14:35:03 +00:00
|
|
|
function sendAsync (payload, callback) {
|
2018-09-05 15:31:57 +00:00
|
|
|
var syncResponse = getSyncResponse(payload);
|
|
|
|
if (syncResponse && callback){
|
|
|
|
callback(null, syncResponse);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var messageId = callbackId++;
|
|
|
|
|
|
|
|
if (Array.isArray(payload))
|
|
|
|
{
|
|
|
|
callbacks[messageId] = {num: payload.length,
|
|
|
|
results: [],
|
|
|
|
callback: callback};
|
|
|
|
for (var i in payload) {
|
|
|
|
bridgeSend({type: 'web3-send-async-read-only',
|
|
|
|
messageId: messageId,
|
2018-11-09 14:42:48 +00:00
|
|
|
payload: payload[i]});
|
2018-09-05 15:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callbacks[messageId] = {callback: callback};
|
|
|
|
bridgeSend({type: 'web3-send-async-read-only',
|
|
|
|
messageId: messageId,
|
2018-11-09 14:42:48 +00:00
|
|
|
payload: payload});
|
2018-09-05 15:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2018-12-25 14:35:03 +00:00
|
|
|
|
|
|
|
ReadOnlyProvider.prototype.sendAsync = sendAsync;
|
|
|
|
|
|
|
|
var ReadOnlyBetaProvider = function () {};
|
|
|
|
|
|
|
|
ReadOnlyBetaProvider.prototype.isStatus = true;
|
|
|
|
ReadOnlyBetaProvider.prototype.status = new StatusAPI();
|
|
|
|
|
|
|
|
//web3.js Backwards Compatibility
|
|
|
|
ReadOnlyBetaProvider.prototype.isConnected = function () { return true; };
|
|
|
|
ReadOnlyBetaProvider.prototype.sendAsync = sendAsync;
|
|
|
|
|
|
|
|
ReadOnlyBetaProvider.prototype.scanQRCode = function (regex) {
|
|
|
|
return sendAPIrequest('qr-code', {regex: regex});
|
|
|
|
};
|
|
|
|
|
|
|
|
ReadOnlyBetaProvider.prototype.send = function (method, params = []) {
|
|
|
|
|
|
|
|
if (!method || typeof method !== 'string') {
|
|
|
|
return new Error('Method is not a valid string.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(params instanceof Array)) {
|
|
|
|
return new Error('Params is not a valid array.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method == 'eth_requestAccounts'){
|
|
|
|
return sendAPIrequest('web3');
|
|
|
|
}
|
|
|
|
|
|
|
|
var messageId = callbackId++;
|
|
|
|
var payload = {id: messageId,
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
method: method,
|
|
|
|
params: params};
|
|
|
|
|
|
|
|
bridgeSend({type: 'web3-send-async-read-only',
|
|
|
|
messageId: messageId,
|
|
|
|
payload: payload});
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
callbacks[messageId] = {beta: true,
|
|
|
|
resolve: resolve,
|
|
|
|
reject: reject};
|
|
|
|
});
|
|
|
|
};
|
2018-09-05 15:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log("ReadOnlyProvider");
|
2018-12-25 14:35:03 +00:00
|
|
|
ethereum = new ReadOnlyProvider();
|
|
|
|
console.log("ReadOnlyBetaProvider");
|
|
|
|
ethereumBeta = new ReadOnlyBetaProvider();
|