-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from xch-dev/peer-close
Add JS Peer and close function
- Loading branch information
Showing
8 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use chia::protocol; | ||
use napi::bindgen_prelude::*; | ||
|
||
use crate::{ | ||
traits::{FromJs, IntoJs, IntoRust}, | ||
Coin, | ||
}; | ||
|
||
#[napi(object)] | ||
pub struct CoinState { | ||
pub coin: Coin, | ||
pub spent_height: Option<u32>, | ||
pub created_height: Option<u32>, | ||
} | ||
|
||
impl IntoJs<CoinState> for protocol::CoinState { | ||
fn into_js(self) -> Result<CoinState> { | ||
Ok(CoinState { | ||
coin: self.coin.into_js()?, | ||
spent_height: self.spent_height, | ||
created_height: self.created_height, | ||
}) | ||
} | ||
} | ||
|
||
impl FromJs<CoinState> for protocol::CoinState { | ||
fn from_js(value: CoinState) -> Result<Self> { | ||
Ok(Self { | ||
coin: value.coin.into_rust()?, | ||
spent_height: value.spent_height, | ||
created_height: value.created_height, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::net::SocketAddr; | ||
|
||
use chia_wallet_sdk::{ | ||
self as sdk, connect_peer, create_native_tls_connector, load_ssl_cert, Connector, | ||
}; | ||
use napi::bindgen_prelude::*; | ||
|
||
use crate::{ | ||
traits::{IntoJs, IntoRust}, | ||
CoinState, | ||
}; | ||
|
||
#[napi] | ||
pub struct Tls(Connector); | ||
|
||
#[napi] | ||
impl Tls { | ||
#[napi(constructor)] | ||
pub fn new(cert_path: String, key_path: String) -> Result<Self> { | ||
let cert = load_ssl_cert(&cert_path, &key_path) | ||
.map_err(|error| Error::from_reason(error.to_string()))?; | ||
let tls = create_native_tls_connector(&cert) | ||
.map_err(|error| Error::from_reason(error.to_string()))?; | ||
Ok(Self(tls)) | ||
} | ||
} | ||
|
||
#[napi] | ||
pub struct Peer(sdk::Peer); | ||
|
||
#[napi] | ||
impl Peer { | ||
#[napi(ts_args_type = "uri: string, tls: Tls, networkId: string")] | ||
pub async fn connect(uri: String, tls: Reference<Tls>, network_id: String) -> Result<Self> { | ||
let (peer, mut receiver) = connect_peer( | ||
network_id, | ||
tls.0.clone(), | ||
uri.parse::<SocketAddr>() | ||
.map_err(|error| Error::from_reason(error.to_string()))?, | ||
) | ||
.await | ||
.map_err(|error| Error::from_reason(error.to_string()))?; | ||
|
||
tokio::spawn(async move { while let Some(_message) = receiver.recv().await {} }); | ||
|
||
Ok(Self(peer)) | ||
} | ||
|
||
#[napi] | ||
pub async fn request_children(&self, coin_id: Uint8Array) -> Result<Vec<CoinState>> { | ||
self.0 | ||
.request_children(coin_id.into_rust()?) | ||
.await | ||
.map_err(|error| Error::from_reason(error.to_string()))? | ||
.coin_states | ||
.into_iter() | ||
.map(IntoJs::into_js) | ||
.collect() | ||
} | ||
|
||
#[napi] | ||
pub async fn close(&self) -> Result<()> { | ||
self.0 | ||
.close() | ||
.await | ||
.map_err(|error| Error::from_reason(error.to_string()))?; | ||
Ok(()) | ||
} | ||
} |