Skip to content

Commit

Permalink
WIP: chrome extension message api
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh committed Mar 15, 2024
1 parent 72f9b29 commit 4329202
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 112 deletions.
36 changes: 4 additions & 32 deletions core/src/adaptor.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
// Chrome Extension Wallet Adaptor

use crate::imports::*;
use wasm_bindgen::prelude::*;

struct Inner {
#[allow(dead_code)]
runtime: Runtime,
pub enum WebEvent {
AccountSelection(AccountId),
}

impl Inner {
pub fn new(runtime: Runtime) -> Self {
Self { runtime }
}
pub trait AdaptorApi: Sync + Send {
fn post_to_server(&self, event: WebEvent);
}

#[wasm_bindgen]
pub struct Adaptor {
#[allow(dead_code)]
inner: Arc<Inner>,
}

impl Adaptor {
pub fn new(runtime: Runtime) -> Self {
Self {
inner: Arc::new(Inner::new(runtime.clone())),
}
}
}

#[wasm_bindgen]
impl Adaptor {
#[wasm_bindgen(js_name = "sendTransaction")]
pub fn send_transaction(&self) -> Result<()> {
Ok(())
}
}

// static mut ADAPTOR : Option<Adaptor> = None;
24 changes: 13 additions & 11 deletions core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ cfg_if! {
"Kaspa NG",
native_options,
Box::new(move |cc| {
let runtime = runtime::Runtime::new(&cc.egui_ctx, &settings, wallet_api, application_events);
let runtime = runtime::Runtime::new(&cc.egui_ctx, &settings, wallet_api, application_events, None);
delegate.lock().unwrap().replace(runtime.clone());
runtime::signals::Signals::bind(&runtime);
runtime.start();
Expand All @@ -279,9 +279,9 @@ cfg_if! {
} else {

// use crate::result::Result;
use crate::adaptor::AdaptorApi;

pub async fn kaspa_ng_main(wallet_api : Option<Arc<dyn WalletApi>>, application_events : Option<ApplicationEventsChannel>) -> Result<()> {
use wasm_bindgen::prelude::*;
pub async fn kaspa_ng_main(wallet_api : Option<Arc<dyn WalletApi>>, application_events : Option<ApplicationEventsChannel>, adaptor: Option<Arc<dyn AdaptorApi>>) -> Result<()> {
use workflow_dom::utils::document;

// ------------------------------------------------------------
Expand Down Expand Up @@ -330,16 +330,18 @@ cfg_if! {

// wallet_api.ping()

let runtime = runtime::Runtime::new(&cc.egui_ctx, &settings, wallet_api, application_events);
// let adaptor = kaspa_ng_core::adaptor::Adaptor::new(runtime.clone());
// let window = web_sys::window().expect("no global `window` exists");
// js_sys::Reflect::set(
// &window,
// &JsValue::from_str("adaptor"),
// &JsValue::from(adaptor),
// ).expect("failed to set adaptor");

let runtime = runtime::Runtime::new(&cc.egui_ctx, &settings, wallet_api, application_events, adaptor);
runtime.start();

let adaptor = kaspa_ng_core::adaptor::Adaptor::new(runtime.clone());
let window = web_sys::window().expect("no global `window` exists");
js_sys::Reflect::set(
&window,
&JsValue::from_str("adaptor"),
&JsValue::from(adaptor),
).expect("failed to set adaptor");


Box::new(kaspa_ng_core::Core::new(cc, runtime, settings, false))
}),
Expand Down
2 changes: 1 addition & 1 deletion core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ impl Core {

self.purge_secure_stack();
}
CoreWallet::AccountSelection { id: _ } => { }
CoreWallet::AccountSelection { id: _ } => {}
CoreWallet::DaaScoreChange { current_daa_score } => {
self.state.current_daa_score.replace(current_daa_score);
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/modules/account_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,9 @@ impl AccountManager {
account_collection.iter().for_each(|account_select| {
if ui.account_selector_button(account_select, &network_type, false, core.balance_padding()).clicked() {
this.select(Some(account_select.clone()), core.device().clone());
// let id = account_select.id();
let id = account_select.id();
// let _ = core.sender().try_send(Events::Wallet { event: Box::new(kaspa_wallet_core::events::Events::AccountSelection{id: Some(id)}) });
runtime().post_to_server(crate::adaptor::WebEvent::AccountSelection(id));
if core.device().single_pane() {
this.section = AccountManagerSection::Overview;
} else {
Expand Down
15 changes: 15 additions & 0 deletions core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod channel;
pub mod payload;
pub mod services;
pub mod system;
use crate::adaptor::{AdaptorApi, WebEvent};
pub use payload::Payload;
pub use services::Service;
use services::*;
Expand All @@ -27,6 +28,8 @@ pub struct Inner {
start_time: Instant,
system: Option<System>,

adaptor: Option<Arc<dyn AdaptorApi>>,

kaspa: Arc<KaspaService>,
peer_monitor_service: Arc<PeerMonitorService>,
update_monitor_service: Arc<UpdateMonitorService>,
Expand All @@ -52,6 +55,7 @@ impl Runtime {
settings: &Settings,
wallet_api: Option<Arc<dyn WalletApi>>,
application_events: Option<ApplicationEventsChannel>,
adaptor: Option<Arc<dyn AdaptorApi>>,
) -> Self {
let system = System::new();

Expand Down Expand Up @@ -113,6 +117,7 @@ impl Runtime {
is_running: Arc::new(AtomicBool::new(false)),
start_time: Instant::now(),
system: Some(system),
adaptor,
// #[cfg(not(feature = "lean"))]
metrics_service,
#[cfg(not(feature = "lean"))]
Expand All @@ -125,6 +130,16 @@ impl Runtime {
runtime
}

// pub fn set_adaptor(&self, adapter:Adaptor){
// self.inner.adaptor.lock().unwrap().adaptor = Some(adapter);
// }

pub fn post_to_server(&self, event: WebEvent) {
if let Some(adaptor) = self.inner.adaptor.as_ref() {
adaptor.post_to_server(event);
}
}

pub fn uptime(&self) -> Duration {
self.inner.start_time.elapsed()
}
Expand Down
27 changes: 25 additions & 2 deletions extensions/chrome/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ pub mod imports;
pub mod ipc;
pub mod server;

use kaspa_ng_core::adaptor::AdaptorApi;
use workflow_wasm::extensions::ObjectExtension;

use crate::imports::*;

static mut SERVER: Option<Arc<Server>> = None;
Expand Down Expand Up @@ -33,7 +36,25 @@ pub async fn kaspa_ng_background() {
server.start().await;
}

struct Adaptor {}

impl Adaptor {
pub fn new() -> Self {
Self {}
}
}

impl AdaptorApi for Adaptor {
fn post_to_server(&self, _event: kaspa_ng_core::adaptor::WebEvent) {
let obj = js_sys::Object::new();
let _ = obj.set("type", &"WebApi".into());
let _ = obj.set("data", &"Web".into());
// chrome_sys::bindings::send_message();
}
}

// extension popup
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub async fn kaspa_ng_main() {
log_info!("kaspa_ng_main called successfully in the popup!");
Expand All @@ -54,8 +75,10 @@ pub async fn kaspa_ng_main() {
.await
.expect("ping failed");
log_info!("Client received response: {response:?}");

if let Err(err) = app::kaspa_ng_main(Some(wallet_client), Some(application_events)).await {
let adaptor = Arc::new(Adaptor::new());
if let Err(err) =
app::kaspa_ng_main(Some(wallet_client), Some(application_events), Some(adaptor)).await
{
log_error!("Error: {err}");
}
}
Loading

0 comments on commit 4329202

Please sign in to comment.