mirror of
https://github.com/status-im/status-react.git
synced 2025-02-27 10:00:49 +00:00
parent
1377ba3c1a
commit
8217cff62e
@ -44,8 +44,7 @@
|
|||||||
if (callback.reject) {
|
if (callback.reject) {
|
||||||
callback.reject(new Error("Cancelled"));
|
callback.reject(new Error("Cancelled"));
|
||||||
}
|
}
|
||||||
}
|
} else if (regex.test(result)) {
|
||||||
else if (regex.test(result)) {
|
|
||||||
if (callback.resolve) {
|
if (callback.resolve) {
|
||||||
callback.resolve(result);
|
callback.resolve(result);
|
||||||
}
|
}
|
||||||
@ -62,6 +61,7 @@
|
|||||||
this.code = 4100;
|
this.code = 4100;
|
||||||
this.message = "The requested method and/or account has not been authorized by the user.";
|
this.message = "The requested method and/or account has not been authorized by the user.";
|
||||||
}
|
}
|
||||||
|
|
||||||
Unauthorized.prototype = Object.create(Error.prototype);
|
Unauthorized.prototype = Object.create(Error.prototype);
|
||||||
|
|
||||||
function UserRejectedRequest() {
|
function UserRejectedRequest() {
|
||||||
@ -70,9 +70,9 @@
|
|||||||
this.code = 4001;
|
this.code = 4001;
|
||||||
this.message = "The user rejected the request.";
|
this.message = "The user rejected the request.";
|
||||||
}
|
}
|
||||||
|
|
||||||
UserRejectedRequest.prototype = Object.create(Error.prototype);
|
UserRejectedRequest.prototype = Object.create(Error.prototype);
|
||||||
ReactNativeWebView.onMessage = function (message)
|
ReactNativeWebView.onMessage = function (message) {
|
||||||
{
|
|
||||||
data = JSON.parse(message);
|
data = JSON.parse(message);
|
||||||
var id = data.messageId;
|
var id = data.messageId;
|
||||||
var callback = callbacks[id];
|
var callback = callbacks[id];
|
||||||
@ -93,71 +93,79 @@
|
|||||||
} else {
|
} else {
|
||||||
callback.reject(new UserRejectedRequest());
|
callback.reject(new UserRejectedRequest());
|
||||||
}
|
}
|
||||||
}
|
} else if (data.type === "web3-send-async-callback") {
|
||||||
else if (data.type === "web3-send-async-callback")
|
if (callback.beta) {
|
||||||
{
|
if (data.error) {
|
||||||
if (callback.beta)
|
|
||||||
{
|
|
||||||
if (data.error)
|
|
||||||
{
|
|
||||||
if (data.error.code == 4100)
|
if (data.error.code == 4100)
|
||||||
callback.reject(new Unauthorized());
|
callback.reject(new Unauthorized());
|
||||||
else
|
else
|
||||||
callback.reject(data.error);
|
callback.reject(data.error);
|
||||||
|
} else {
|
||||||
|
if (window.statusAppDebug) {
|
||||||
|
console.log("resolve " + callback.method + " :" + JSON.stringify(data.result.result));
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (window.statusAppDebug) { console.log("resolve " + callback.method + " :" + JSON.stringify(data.result.result)); }
|
|
||||||
callback.resolve(data.result.result);
|
callback.resolve(data.result.result);
|
||||||
}
|
}
|
||||||
}
|
} else if (callback.results) {
|
||||||
else if (callback.results)
|
|
||||||
{
|
|
||||||
callback.results.push(data.error || data.result);
|
callback.results.push(data.error || data.result);
|
||||||
if (callback.results.length == callback.num)
|
if (callback.results.length == callback.num)
|
||||||
callback.callback(undefined, callback.results);
|
callback.callback(undefined, callback.results);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
callback.callback(data.error, data.result);
|
callback.callback(data.error, data.result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function web3Response (payload, result){
|
function web3SuccessResponse(payload, result) {
|
||||||
return {id: payload.id,
|
return {
|
||||||
|
id: payload.id,
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
result: result};
|
method: payload.method,
|
||||||
|
result: result
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function web3ErrorResponse(payload, error) {
|
||||||
|
return {
|
||||||
|
id: payload.id,
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
method: payload.method,
|
||||||
|
error: error
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSyncResponse(payload) {
|
function getSyncResponse(payload) {
|
||||||
if (payload.method == "eth_accounts" && (typeof window.statusAppcurrentAccountAddress !== "undefined")) {
|
if (payload.method == "eth_accounts" && (typeof window.statusAppcurrentAccountAddress !== "undefined")) {
|
||||||
return web3Response(payload, [window.statusAppcurrentAccountAddress])
|
return web3SuccessResponse(payload, [window.statusAppcurrentAccountAddress])
|
||||||
} else if (payload.method == "eth_coinbase" && (typeof window.statusAppcurrentAccountAddress !== "undefined")) {
|
} else if (payload.method == "eth_coinbase" && (typeof window.statusAppcurrentAccountAddress !== "undefined")) {
|
||||||
return web3Response(payload, window.statusAppcurrentAccountAddress)
|
return web3SuccessResponse(payload, window.statusAppcurrentAccountAddress)
|
||||||
} else if (payload.method == "net_version") {
|
} else if (payload.method == "net_version") {
|
||||||
return web3Response(payload, window.statusAppNetworkId)
|
return web3SuccessResponse(payload, window.statusAppNetworkId)
|
||||||
} else if (payload.method == "eth_chainId") {
|
} else if (payload.method == "eth_chainId") {
|
||||||
return web3Response(payload, "0x" + Number(window.statusAppNetworkId).toString(16))
|
return web3SuccessResponse(payload, "0x" + Number(window.statusAppNetworkId).toString(16))
|
||||||
} else if (payload.method == "eth_uninstallFilter") {
|
} else if (payload.method == "eth_uninstallFilter") {
|
||||||
return web3Response(payload, true);
|
return web3SuccessResponse(payload, true);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var StatusAPI = function () {};
|
var StatusAPI = function () {
|
||||||
|
};
|
||||||
|
|
||||||
StatusAPI.prototype.getContactCode = function () {
|
StatusAPI.prototype.getContactCode = function () {
|
||||||
return sendAPIrequest('contact-code');
|
return sendAPIrequest('contact-code');
|
||||||
};
|
};
|
||||||
|
|
||||||
var EthereumProvider = function () {};
|
var EthereumProvider = function () {
|
||||||
|
};
|
||||||
|
|
||||||
EthereumProvider.prototype.isStatus = true;
|
EthereumProvider.prototype.isStatus = true;
|
||||||
EthereumProvider.prototype.status = new StatusAPI();
|
EthereumProvider.prototype.status = new StatusAPI();
|
||||||
EthereumProvider.prototype.isConnected = function () { return true; };
|
EthereumProvider.prototype.isConnected = function () {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
// Set legacy metamask fields https://docs.metamask.io/guide/ethereum-provider.html#legacy-api
|
// Set legacy metamask fields https://docs.metamask.io/guide/ethereum-provider.html#legacy-api
|
||||||
EthereumProvider.prototype.networkVersion = window.statusAppNetworkId;
|
EthereumProvider.prototype.networkVersion = window.statusAppNetworkId;
|
||||||
EthereumProvider.prototype.chainId = "0x" + Number(window.statusAppNetworkId).toString(16);
|
EthereumProvider.prototype.chainId = "0x" + Number(window.statusAppNetworkId).toString(16);
|
||||||
@ -194,11 +202,16 @@
|
|||||||
try {
|
try {
|
||||||
cb(data)
|
cb(data)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setTimeout((()=>{throw e}))
|
setTimeout((() => {
|
||||||
}});
|
throw e
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
EthereumProvider.prototype.enable = function () {
|
EthereumProvider.prototype.enable = function () {
|
||||||
if (window.statusAppDebug) { console.log("enable"); }
|
if (window.statusAppDebug) {
|
||||||
|
console.log("enable");
|
||||||
|
}
|
||||||
return sendAPIrequest('web3');
|
return sendAPIrequest('web3');
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -206,79 +219,122 @@
|
|||||||
return sendAPIrequest('qr-code', {regex: regex});
|
return sendAPIrequest('qr-code', {regex: regex});
|
||||||
};
|
};
|
||||||
|
|
||||||
EthereumProvider.prototype.request = function (requestArguments)
|
EthereumProvider.prototype.request = function (requestArguments) {
|
||||||
{
|
if (window.statusAppDebug) {
|
||||||
if (window.statusAppDebug) { console.log("request: " + JSON.stringify(requestArguments)); }
|
console.log("request: " + JSON.stringify(requestArguments));
|
||||||
|
}
|
||||||
if (!requestArguments) {
|
if (!requestArguments) {
|
||||||
return new Error('Request is not valid.');
|
return Promise.reject(new Error('Request is not valid.'));
|
||||||
}
|
}
|
||||||
var method = requestArguments.method;
|
var method = requestArguments.method;
|
||||||
|
|
||||||
if (!method) {
|
if (!method) {
|
||||||
return new Error('Request is not valid.');
|
return Promise.reject(new Error('Request is not valid.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Support for legacy send method
|
if (method === 'eth_requestAccounts') {
|
||||||
if (typeof method !== 'string') {
|
|
||||||
return this.sendSync(method);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method == 'eth_requestAccounts'){
|
|
||||||
return sendAPIrequest('web3');
|
return sendAPIrequest('web3');
|
||||||
}
|
}
|
||||||
|
|
||||||
var syncResponse = getSyncResponse({method: method});
|
var syncResponse = getSyncResponse({method: method});
|
||||||
if (syncResponse) {
|
if (syncResponse) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
if (window.statusAppDebug) { console.log("resolve sync: " + resolve + " " + JSON.stringify(syncResponse.result)); }
|
if (window.statusAppDebug) {
|
||||||
|
console.log("resolved sync method: " + method + ", result: " + JSON.stringify(syncResponse.result));
|
||||||
|
}
|
||||||
resolve(syncResponse.result);
|
resolve(syncResponse.result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var messageId = callbackId++;
|
var messageId = callbackId++;
|
||||||
var payload = {id: messageId,
|
var payload = {
|
||||||
|
id: messageId,
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
method: method,
|
method: method,
|
||||||
params: requestArguments.params};
|
params: requestArguments.params
|
||||||
|
};
|
||||||
|
|
||||||
bridgeSend({type: 'web3-send-async-read-only',
|
bridgeSend({
|
||||||
|
type: 'web3-send-async-read-only',
|
||||||
messageId: messageId,
|
messageId: messageId,
|
||||||
payload: payload});
|
payload: payload
|
||||||
|
});
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
callbacks[messageId] = {beta: true,
|
callbacks[messageId] = {
|
||||||
|
beta: true,
|
||||||
method: method,
|
method: method,
|
||||||
resolve: resolve,
|
resolve: resolve,
|
||||||
reject: reject};
|
reject: reject
|
||||||
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// (DEPRECATED) Support for legacy send method
|
// (DEPRECATED) Support for legacy send method
|
||||||
EthereumProvider.prototype.send = function (method, params = [])
|
EthereumProvider.prototype.send = function (param1, param2) {
|
||||||
{
|
// reference: https://docs.metamask.io/guide/ethereum-provider.html#legacy-methods
|
||||||
if (window.statusAppDebug) { console.log("send (legacy): " + method);}
|
if (typeof param1 == 'object') {
|
||||||
|
//maybe ways of:
|
||||||
|
//1.ethereum.send(payload: JsonRpcRequest, callback: JsonRpcCallback): void;
|
||||||
|
//2.ethereum.send(payload: JsonRpcRequest): unknown;
|
||||||
|
if (window.statusAppDebug) {
|
||||||
|
console.log("send (legacy), payload: " + JSON.stringify(param1) + ", callback: " + param2);
|
||||||
|
}
|
||||||
|
|
||||||
|
var syncResponse = getSyncResponse(param1);
|
||||||
|
if(syncResponse){
|
||||||
|
if(param2){
|
||||||
|
param2(null, syncResponse);
|
||||||
|
return;
|
||||||
|
}else
|
||||||
|
return syncResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.request(param1).then(result => {
|
||||||
|
if (param2) {
|
||||||
|
param2(null, web3SuccessResponse(param1, result));
|
||||||
|
}
|
||||||
|
}, reason => {
|
||||||
|
if (window.statusAppDebug) {
|
||||||
|
console.log("send (legacy) failed. payload: " + JSON.stringify(param1) + ", reason: " + reason)
|
||||||
|
}
|
||||||
|
if (param2) {
|
||||||
|
param2(reason, web3ErrorResponse(param1, reason))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (typeof param1 == 'string') {
|
||||||
|
var method = param1;
|
||||||
|
var params = param2;
|
||||||
|
if (window.statusAppDebug) {
|
||||||
|
console.log("send (legacy), method: " + method + ", params: " + JSON.stringify(params));
|
||||||
|
}
|
||||||
return this.request({method: method, params: params});
|
return this.request({method: method, params: params});
|
||||||
|
} else {
|
||||||
|
throw new Error("unsupported call to send, param1: " + param1 + ", param2: " + param2)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// (DEPRECATED) Support for legacy sendSync method
|
// (DEPRECATED) Support for legacy sendSync method
|
||||||
EthereumProvider.prototype.sendSync = function (payload)
|
EthereumProvider.prototype.sendSync = function (payload) {
|
||||||
{
|
if (window.statusAppDebug) {
|
||||||
if (window.statusAppDebug) { console.log("sendSync (legacy)" + JSON.stringify(payload));}
|
console.log("sendSync (legacy)" + JSON.stringify(payload));
|
||||||
|
}
|
||||||
if (payload.method == "eth_uninstallFilter") {
|
if (payload.method == "eth_uninstallFilter") {
|
||||||
this.sendAsync(payload, function (res, err) {})
|
this.sendAsync(payload, function (res, err) {
|
||||||
|
})
|
||||||
}
|
}
|
||||||
var syncResponse = getSyncResponse(payload);
|
var syncResponse = getSyncResponse(payload);
|
||||||
if (syncResponse) {
|
if (syncResponse) {
|
||||||
return syncResponse;
|
return syncResponse;
|
||||||
} else {
|
} else {
|
||||||
return web3Response(payload, null);
|
return web3SuccessResponse(payload, null);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// (DEPRECATED) Support for legacy sendAsync method
|
// (DEPRECATED) Support for legacy sendAsync method
|
||||||
EthereumProvider.prototype.sendAsync = function (payload, callback)
|
EthereumProvider.prototype.sendAsync = function (payload, callback) {
|
||||||
{
|
if (window.statusAppDebug) {
|
||||||
if (window.statusAppDebug) { console.log("sendAsync (legacy)" + JSON.stringify(payload));}
|
console.log("sendAsync (legacy)" + JSON.stringify(payload));
|
||||||
|
}
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
return new Error('Request is not valid.');
|
return new Error('Request is not valid.');
|
||||||
}
|
}
|
||||||
@ -288,28 +344,29 @@
|
|||||||
var syncResponse = getSyncResponse(payload);
|
var syncResponse = getSyncResponse(payload);
|
||||||
if (syncResponse && callback) {
|
if (syncResponse && callback) {
|
||||||
callback(null, syncResponse);
|
callback(null, syncResponse);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
var messageId = callbackId++;
|
var messageId = callbackId++;
|
||||||
|
|
||||||
if (Array.isArray(payload))
|
if (Array.isArray(payload)) {
|
||||||
{
|
callbacks[messageId] = {
|
||||||
callbacks[messageId] = {num: payload.length,
|
num: payload.length,
|
||||||
results: [],
|
results: [],
|
||||||
callback: callback};
|
callback: callback
|
||||||
|
};
|
||||||
for (var i in payload) {
|
for (var i in payload) {
|
||||||
bridgeSend({type: 'web3-send-async-read-only',
|
bridgeSend({
|
||||||
|
type: 'web3-send-async-read-only',
|
||||||
messageId: messageId,
|
messageId: messageId,
|
||||||
payload: payload[i]});
|
payload: payload[i]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
callbacks[messageId] = {callback: callback};
|
callbacks[messageId] = {callback: callback};
|
||||||
bridgeSend({type: 'web3-send-async-read-only',
|
bridgeSend({
|
||||||
|
type: 'web3-send-async-read-only',
|
||||||
messageId: messageId,
|
messageId: messageId,
|
||||||
payload: payload});
|
payload: payload
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user