Commit 053b9d4c authored by Max's avatar Max

Background: allow to add custom networks from config file

parent 10e00243
......@@ -27,6 +27,7 @@
open Js_min
open Chrome
open Async
let print_conn_recv (port : Runtime_utils.port Js.t) =
Js.Optdef.case
......@@ -39,6 +40,15 @@ let print_conn_recv (port : Runtime_utils.port Js.t) =
(fun url ->
log_str @@ "[Background] " ^ (Js.to_string url) ^ " connected"))
let load_custom_networks () =
let json_url = Runtime.getURL "json/custom_networks.json" in
async (fun () ->
Js_of_ocaml_lwt.XmlHttpRequest.perform_raw_url json_url >>=
Xhr_lwt.handle_response >>|? fun json ->
let config_networks =
EzEncoding.destruct Metal_encoding.custom_networks_encoding json in
Storage_writer.update_custom_networks config_networks)
let () =
Ledger_bridge.make_iframe ();
Browser_action.set_badge_bg ~color:"#c62828" ();
......@@ -69,5 +79,6 @@ let () =
Tabs.create tab);
Storage_types.update_storage ();
load_custom_networks ();
(* WARNING: would be useful to uncomment for the proper release *)
(* Runtime.onUpdateAvailabale (fun _ -> Storage_types.update_storage ()) *)
......@@ -106,3 +106,16 @@ let adv_transactions_encoding =
obj2
(req "kind" (constant "transactions"))
(req "operations" (Api_encoding_min.V1.Op.operations false))
let custom_networks_encoding = list @@ conv
(fun (name, node, api) ->
name, Js_of_ocaml.Url.string_of_url node,
Js_of_ocaml.Url.string_of_url api)
(fun (name, node, api) ->
name,
Misc.unopt_exn (Js_of_ocaml.Url.url_of_string node),
Misc.unopt_exn (Js_of_ocaml.Url.url_of_string api)) @@
obj3
(req "name" string)
(req "node" string)
(req "scan" string)
......@@ -132,35 +132,34 @@ let get_custom_networks f =
Chrome.Storage.get Chrome.sync (fun (o:custom_networks_entry t) ->
f (Of_js.custom_networks o))
let add_custom_network ?callback ?networks name node api =
let aux networks =
match Js_of_ocaml.Url.url_of_string node, Js_of_ocaml.Url.url_of_string api with
| Some node, Some api ->
let network = name, node, api in
let networks =
if List.mem network networks then networks
else networks @ [ network ] in
let callback = match callback with
| None -> None
| Some callback -> Some (fun () -> callback networks) in
Chrome.Storage.set ?callback Chrome.sync (To_js.custom_networks networks)
| _ ->
Js_log.log_str "Cannot get url from string";
match callback with None -> () | Some callback -> callback networks in
let with_custom_networks ?networks f =
match networks with
| Some networks -> aux networks
| None -> get_custom_networks (fun networks -> aux networks)
| Some networks -> f networks
| None -> get_custom_networks f
let remove_custom_network ?callback ?networks name =
let aux networks =
let networks = List.filter (fun (n, _, _) -> n <> name) networks in
let callback = match callback with
let update_custom_networks ?callback ?networks new_networks =
with_custom_networks ?networks @@ fun networks ->
let new_networks = List.fold_left (fun acc n ->
if List.mem n networks then acc else acc @ [ n ]) networks new_networks in
let callback = match callback with
| None -> None
| Some callback -> Some (fun () -> callback networks) in
Chrome.Storage.set ?callback Chrome.sync (To_js.custom_networks networks) in
match networks with
| Some networks -> aux networks
| None -> get_custom_networks (fun networks -> aux networks)
| Some callback -> Some (fun () -> callback new_networks) in
Chrome.Storage.set ?callback Chrome.sync (To_js.custom_networks new_networks)
let add_custom_network ?callback ?networks name node api =
match Js_of_ocaml.Url.url_of_string node, Js_of_ocaml.Url.url_of_string api with
| Some node, Some api ->
let network = name, node, api in
update_custom_networks ?callback ?networks [ network ]
| _ -> Js_log.log_str "Cannot get url from string"
let remove_custom_network ?callback ?networks name =
with_custom_networks ?networks @@ fun networks ->
let networks = List.filter (fun (n, _, _) -> n <> name) networks in
let callback = match callback with
| None -> None
| Some callback -> Some (fun () -> callback networks) in
Chrome.Storage.set ?callback Chrome.sync (To_js.custom_networks networks)
let remove_notif ?callback n =
let id = Mhelpers.notif_id_of_not n in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment