Switching networks (#845) (#852)

* Switching networks (#845)

* pass RPC host to webview bridge
This commit is contained in:
Alexander Pantyuhov 2017-03-08 14:45:02 +03:00 committed by Roman Volosovskyi
parent e7926c8b37
commit 874bf16f97
7 changed files with 53 additions and 37 deletions

View File

@ -70,6 +70,7 @@ var TopLevel = {
"getCardId" : function () {},
"getExample" : function () {},
"getInitialOrientation" : function () {},
"getIPAddress" : function () {},
"getLayout" : function () {},
"getNumber" : function () {},
"getSyncing" : function () {},

View File

@ -73,7 +73,7 @@
"react-native-tcp": "^2.0.4",
"react-native-udp": "^1.2.6",
"react-native-vector-icons": "^2.0.3",
"react-native-webview-bridge": "github:status-im/react-native-webview-bridge#0.33.7",
"react-native-webview-bridge": "github:status-im/react-native-webview-bridge#0.33.8",
"readable-stream": "^1.0.33",
"realm": "^0.14.3",
"stream-browserify": "^1.0.0",

View File

@ -46,7 +46,7 @@ StatusHttpProvider.prototype.send = function (payload) {
return result;
} else {
result = StatusBridge.sendRequestSync(JSON.stringify(payload));
result = StatusBridge.sendRequestSync(this.host, JSON.stringify(payload));
try {
result = JSON.parse(result);
@ -61,7 +61,7 @@ StatusHttpProvider.prototype.send = function (payload) {
StatusHttpProvider.prototype.prepareRequest = function () {
var request = new XMLHttpRequest();
request.open('POST', "http://localhost:8545", false);
request.open('POST', this.host, false);
request.setRequestHeader('Content-Type', 'application/json');
return request;
};
@ -73,12 +73,13 @@ function sendAsync(payload, callback) {
if (typeof StatusBridge == "undefined") {
var data = {
payload: JSON.stringify(payload),
callbackId: JSON.stringify(messageId)
callbackId: JSON.stringify(messageId),
host: this.host
};
webkit.messageHandlers.sendRequest.postMessage(JSON.stringify(data));
} else {
StatusBridge.sendRequest(JSON.stringify(messageId), JSON.stringify(payload));
StatusBridge.sendRequest(this.host, JSON.stringify(messageId), JSON.stringify(payload));
}
};
@ -107,13 +108,14 @@ StatusHttpProvider.prototype.isConnected = function () {
}
var protocol = window.location.protocol
var address = providerAddress || "http://localhost:8545";
console.log(protocol);
if (typeof web3 === "undefined") {
if (protocol == "https:") {
console.log("StatusHttpProvider");
web3 = new Web3(new StatusHttpProvider("http://localhost:8545"));
web3 = new Web3(new StatusHttpProvider(address));
} else if (protocol == "http:") {
console.log("HttpProvider");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3 = new Web3(new Web3.providers.HttpProvider(address));
}
}

View File

@ -131,7 +131,8 @@
(defview suggestions-web-view []
[url [:web-view-url]
extra-js [:web-view-extra-js]]
extra-js [:web-view-extra-js]
rpc-url [:get :rpc-url]]
(when url
[webview-bridge
{:ref #(dispatch [:set-webview-bridge %])
@ -139,7 +140,9 @@
:source {:uri url}
:render-error web-view-error
:java-script-enabled true
:injected-on-start-loading-java-script (str js-res/web3 js-res/jquery js-res/web3-init)
:injected-on-start-loading-java-script (str js-res/web3
js-res/jquery
(js-res/web3-init rpc-url))
:injected-java-script (str js-res/webview-js extra-js)
:bounces false
:on-navigation-state-change on-navigation-change

View File

@ -47,6 +47,7 @@
"/add-dapp" (dispatch [:debug-add-dapp obj])
"/remove-dapp" (dispatch [:debug-remove-dapp obj])
"/dapp-changed" (dispatch [:debug-dapp-changed obj])
"/switch-node" (dispatch [:debug-switch-node obj])
:default))
(catch js/Error e
(log/debug "Error: " e))))))
@ -80,3 +81,8 @@
webview-bridge)
(.reload webview-bridge)))))
(register-handler :debug-switch-node
(u/side-effect!
(fn [{:keys [current-account-id]} [_ {:keys [url]}]]
(dispatch [:initialize-protocol current-account-id url]))))

View File

@ -21,37 +21,39 @@
[status-im.utils.scheduler :as s]))
(register-handler :initialize-protocol
(fn [db [_ current-account-id]]
(fn [db [_ current-account-id ethereum-rpc-url]]
(let [{:keys [public-key status updates-public-key
updates-private-key]}
(get-in db [:accounts current-account-id])]
(if public-key
(let [groups (chats/get-active-group-chats)
w3 (protocol/init-whisper!
{:rpc-url c/ethereum-rpc-url
:identity public-key
:groups groups
:callback #(dispatch [:incoming-message %1 %2])
:ack-not-received-s-interval 125
:default-ttl 120
:send-online-s-interval 180
:ttl-config {:public-group-message 2400}
:max-attempts-number 3
:delivery-loop-ms-interval 500
:profile-keypair {:public updates-public-key
:private updates-private-key}
:hashtags (u/get-hashtags status)
:pending-messages (pending-messages/get-all)
:contacts (keep (fn [{:keys [whisper-identity
public-key
private-key]}]
(when (and public-key private-key)
{:identity whisper-identity
:keypair {:public public-key
:private private-key}}))
(contacts/get-all))
:post-error-callback #(dispatch [::post-error %])})]
(assoc db :web3 w3))
(let [rpc-url (or ethereum-rpc-url c/ethereum-rpc-url)
groups (chats/get-active-group-chats)
w3 (protocol/init-whisper!
{:rpc-url rpc-url
:identity public-key
:groups groups
:callback #(dispatch [:incoming-message %1 %2])
:ack-not-received-s-interval 125
:default-ttl 120
:send-online-s-interval 180
:ttl-config {:public-group-message 2400}
:max-attempts-number 3
:delivery-loop-ms-interval 500
:profile-keypair {:public updates-public-key
:private updates-private-key}
:hashtags (u/get-hashtags status)
:pending-messages (pending-messages/get-all)
:contacts (keep (fn [{:keys [whisper-identity
public-key
private-key]}]
(when (and public-key private-key)
{:identity whisper-identity
:keypair {:public public-key
:private private-key}}))
(contacts/get-all))
:post-error-callback #(dispatch [::post-error %])})]
(assoc db :web3 w3
:rpc-url rpc-url))
db))))
(register-handler :update-sync-state

View File

@ -18,4 +18,6 @@
(def web3 (str "; if (typeof Web3 == 'undefined') {"
(slurp "resources/web3.0_16_0.min.js")
"}"))
(def web3-init (slurp "resources/web3_init.js"))
(defn web3-init [provider-address]
(str "var providerAddress = \"" provider-address "\";"
(slurp "resources/web3_init.js")))