From 4b4af253472a529a748865e031642eb5565e1832 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Sat, 2 Nov 2024 11:57:06 -0400 Subject: [PATCH 1/5] Add JS Peer and close function --- Cargo.lock | 2 + crates/chia-sdk-client/src/peer.rs | 5 +++ napi/Cargo.toml | 5 ++- napi/index.d.ts | 13 ++++++ napi/index.js | 4 +- napi/src/coin_state.rs | 34 +++++++++++++++ napi/src/lib.rs | 4 ++ napi/src/peer.rs | 69 ++++++++++++++++++++++++++++++ 8 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 napi/src/coin_state.rs create mode 100644 napi/src/peer.rs diff --git a/Cargo.lock b/Cargo.lock index e2bb116d..db4fe933 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -605,6 +605,7 @@ dependencies = [ "napi-derive", "num-bigint", "paste", + "tokio", ] [[package]] @@ -1383,6 +1384,7 @@ dependencies = [ "napi-derive", "napi-sys", "once_cell", + "tokio", ] [[package]] diff --git a/crates/chia-sdk-client/src/peer.rs b/crates/chia-sdk-client/src/peer.rs index 69c8bae9..df42369d 100644 --- a/crates/chia-sdk-client/src/peer.rs +++ b/crates/chia-sdk-client/src/peer.rs @@ -290,6 +290,11 @@ impl Peer { self.0.sink.lock().await.send(message).await?; Ok(receiver.await?) } + + pub async fn close(&self) -> Result<(), ClientError> { + self.0.sink.lock().await.close().await?; + Ok(()) + } } impl Drop for PeerInner { diff --git a/napi/Cargo.toml b/napi/Cargo.toml index 33aec50f..8485f954 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -19,14 +19,15 @@ workspace = true crate-type = ["cdylib"] [dependencies] -napi = { workspace = true, features = ["napi6"] } +napi = { workspace = true, features = ["napi6", "async"] } napi-derive = { workspace = true } -chia-wallet-sdk = { workspace = true } +chia-wallet-sdk = { workspace = true, features = ["native-tls"] } chia = { workspace = true } clvmr = { workspace = true } num-bigint = { workspace = true } hex = { workspace = true } paste = { workspace = true } +tokio = { workspace = true, features = ["sync"] } [build-dependencies] napi-build = "2.0.1" diff --git a/napi/index.d.ts b/napi/index.d.ts index d97e43eb..ff10904e 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -145,6 +145,11 @@ export interface Spend { puzzle: Program solution: Program } +export interface CoinState { + coin: Coin + spentHeight?: number + createdHeight?: number +} export interface LineageProof { parentParentCoinInfo: Uint8Array parentInnerPuzzleHash?: Uint8Array @@ -294,6 +299,14 @@ export declare class ClvmAllocator { softfork(cost: bigint, rest: Program): Program parseSoftfork(program: Program): Softfork | null } +export declare class Tls { + constructor(certPath: string, keyPath: string) +} +export declare class Peer { + static connect(uri: string, tls: Tls, networkId: string): Promise + requestChildren(coinId: Uint8Array): Promise> + close(): Promise +} export declare class Program { isAtom(): boolean isPair(): boolean diff --git a/napi/index.js b/napi/index.js index 86a69cad..1f1c6af4 100644 --- a/napi/index.js +++ b/napi/index.js @@ -310,13 +310,15 @@ if (!nativeBinding) { throw new Error(`Failed to load native binding`) } -const { ClvmAllocator, curryTreeHash, intToSignedBytes, signedBytesToInt, toCoinId, Program, Simulator, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding +const { ClvmAllocator, curryTreeHash, intToSignedBytes, signedBytesToInt, toCoinId, Tls, Peer, Program, Simulator, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding module.exports.ClvmAllocator = ClvmAllocator module.exports.curryTreeHash = curryTreeHash module.exports.intToSignedBytes = intToSignedBytes module.exports.signedBytesToInt = signedBytesToInt module.exports.toCoinId = toCoinId +module.exports.Tls = Tls +module.exports.Peer = Peer module.exports.Program = Program module.exports.Simulator = Simulator module.exports.compareBytes = compareBytes diff --git a/napi/src/coin_state.rs b/napi/src/coin_state.rs new file mode 100644 index 00000000..d189dbec --- /dev/null +++ b/napi/src/coin_state.rs @@ -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, + pub created_height: Option, +} + +impl IntoJs for protocol::CoinState { + fn into_js(self) -> Result { + Ok(CoinState { + coin: self.coin.into_js()?, + spent_height: self.spent_height, + created_height: self.created_height, + }) + } +} + +impl FromJs for protocol::CoinState { + fn from_js(value: CoinState) -> Result { + Ok(Self { + coin: value.coin.into_rust()?, + spent_height: value.spent_height, + created_height: value.created_height, + }) + } +} diff --git a/napi/src/lib.rs b/napi/src/lib.rs index cac4d54e..63c93a2a 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -12,8 +12,10 @@ mod clvm; mod clvm_value; mod coin; mod coin_spend; +mod coin_state; mod lineage_proof; mod nft; +mod peer; mod program; mod simulator; mod traits; @@ -22,7 +24,9 @@ mod utils; pub use clvm::*; pub use coin::*; pub use coin_spend::*; +pub use coin_state::*; pub use lineage_proof::*; pub use nft::*; +pub use peer::*; pub use program::*; pub use utils::*; diff --git a/napi/src/peer.rs b/napi/src/peer.rs new file mode 100644 index 00000000..5d3f8997 --- /dev/null +++ b/napi/src/peer.rs @@ -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 { + 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, network_id: String) -> Result { + let (peer, mut receiver) = connect_peer( + network_id, + tls.0.clone(), + uri.parse::() + .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> { + 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(()) + } +} From cc5ab9c9793c916b0aa3356c49eba2f170134d34 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Sat, 2 Nov 2024 12:24:26 -0400 Subject: [PATCH 2/5] rustls napi --- napi/Cargo.toml | 2 +- napi/src/peer.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/napi/Cargo.toml b/napi/Cargo.toml index 8485f954..c62e71c5 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -21,7 +21,7 @@ crate-type = ["cdylib"] [dependencies] napi = { workspace = true, features = ["napi6", "async"] } napi-derive = { workspace = true } -chia-wallet-sdk = { workspace = true, features = ["native-tls"] } +chia-wallet-sdk = { workspace = true, features = ["rustls"] } chia = { workspace = true } clvmr = { workspace = true } num-bigint = { workspace = true } diff --git a/napi/src/peer.rs b/napi/src/peer.rs index 5d3f8997..c3b126ed 100644 --- a/napi/src/peer.rs +++ b/napi/src/peer.rs @@ -1,7 +1,7 @@ use std::net::SocketAddr; use chia_wallet_sdk::{ - self as sdk, connect_peer, create_native_tls_connector, load_ssl_cert, Connector, + self as sdk, connect_peer, create_rustls_connector, load_ssl_cert, Connector, }; use napi::bindgen_prelude::*; @@ -19,7 +19,7 @@ impl Tls { pub fn new(cert_path: String, key_path: String) -> Result { 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) + let tls = create_rustls_connector(&cert) .map_err(|error| Error::from_reason(error.to_string()))?; Ok(Self(tls)) } From e4519d763f222a8b92c6acd01b0f367536d01849 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Sat, 2 Nov 2024 12:31:03 -0400 Subject: [PATCH 3/5] bindgwn --- Cargo.lock | 17 +++++++++++++++++ napi/Cargo.toml | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index db4fe933..5cacb3ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,12 +95,28 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "aws-lc-fips-sys" +version = "0.12.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf12b67bc9c5168f68655aadb2a12081689a58f1d9b1484705e4d1810ed6e4ac" +dependencies = [ + "bindgen", + "cc", + "cmake", + "dunce", + "fs_extra", + "libc", + "paste", +] + [[package]] name = "aws-lc-rs" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae74d9bd0a7530e8afd1770739ad34b36838829d6ad61818f9230f683f5ad77" dependencies = [ + "aws-lc-fips-sys", "aws-lc-sys", "mirai-annotations", "paste", @@ -596,6 +612,7 @@ dependencies = [ name = "chia-wallet-sdk-napi" version = "0.0.0" dependencies = [ + "aws-lc-rs", "chia", "chia-wallet-sdk", "clvmr", diff --git a/napi/Cargo.toml b/napi/Cargo.toml index c62e71c5..82339bcc 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -29,5 +29,9 @@ hex = { workspace = true } paste = { workspace = true } tokio = { workspace = true, features = ["sync"] } +# This is to ensure that the bindgen feature is enabled for the aws-lc-rs crate. +# https://aws.github.io/aws-lc-rs/platform_support.html#tested-platforms +aws-lc-rs = { version = "1", features = ["bindgen"] } + [build-dependencies] napi-build = "2.0.1" From ad843916900b8163b8308f2b95eb9e848fdb6f95 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Sun, 3 Nov 2024 10:26:02 -0500 Subject: [PATCH 4/5] vendored openssl --- Cargo.lock | 29 ++++++++++++----------------- napi/Cargo.toml | 22 +++++++++++++++++----- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5cacb3ca..3481baf3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,28 +95,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" -[[package]] -name = "aws-lc-fips-sys" -version = "0.12.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf12b67bc9c5168f68655aadb2a12081689a58f1d9b1484705e4d1810ed6e4ac" -dependencies = [ - "bindgen", - "cc", - "cmake", - "dunce", - "fs_extra", - "libc", - "paste", -] - [[package]] name = "aws-lc-rs" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae74d9bd0a7530e8afd1770739ad34b36838829d6ad61818f9230f683f5ad77" dependencies = [ - "aws-lc-fips-sys", "aws-lc-sys", "mirai-annotations", "paste", @@ -612,7 +596,6 @@ dependencies = [ name = "chia-wallet-sdk-napi" version = "0.0.0" dependencies = [ - "aws-lc-rs", "chia", "chia-wallet-sdk", "clvmr", @@ -621,6 +604,8 @@ dependencies = [ "napi-build", "napi-derive", "num-bigint", + "openssl", + "openssl-sys", "paste", "tokio", ] @@ -1604,6 +1589,15 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-src" +version = "300.4.0+3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a709e02f2b4aca747929cca5ed248880847c650233cf8b8cdc48f40aaf4898a6" +dependencies = [ + "cc", +] + [[package]] name = "openssl-sys" version = "0.9.103" @@ -1612,6 +1606,7 @@ checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" dependencies = [ "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] diff --git a/napi/Cargo.toml b/napi/Cargo.toml index 82339bcc..97505cd3 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -21,7 +21,7 @@ crate-type = ["cdylib"] [dependencies] napi = { workspace = true, features = ["napi6", "async"] } napi-derive = { workspace = true } -chia-wallet-sdk = { workspace = true, features = ["rustls"] } +chia-wallet-sdk = { workspace = true, features = ["native-tls"] } chia = { workspace = true } clvmr = { workspace = true } num-bigint = { workspace = true } @@ -29,9 +29,21 @@ hex = { workspace = true } paste = { workspace = true } tokio = { workspace = true, features = ["sync"] } -# This is to ensure that the bindgen feature is enabled for the aws-lc-rs crate. -# https://aws.github.io/aws-lc-rs/platform_support.html#tested-platforms -aws-lc-rs = { version = "1", features = ["bindgen"] } - [build-dependencies] napi-build = "2.0.1" + +[target.aarch64-unknown-linux-gnu.dependencies] +openssl = { version = "0.10.64", features = ["vendored"] } +openssl-sys = { version = "0.9.102", features = ["vendored"] } + +[target.aarch64-unknown-linux-musl.dependencies] +openssl = { version = "0.10.64", features = ["vendored"] } +openssl-sys = { version = "0.9.102", features = ["vendored"] } + +[target.x86_64-unknown-linux-gnu.dependencies] +openssl = { version = "0.10.64", features = ["vendored"] } +openssl-sys = { version = "0.9.102", features = ["vendored"] } + +[target.x86_64-unknown-linux-musl.dependencies] +openssl = { version = "0.10.64", features = ["vendored"] } +openssl-sys = { version = "0.9.102", features = ["vendored"] } From 968736d994a77caf47c225f2b9e3ec1cba6aa1f8 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Sun, 3 Nov 2024 10:27:23 -0500 Subject: [PATCH 5/5] native-tls-fix --- napi/src/peer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/napi/src/peer.rs b/napi/src/peer.rs index c3b126ed..5d3f8997 100644 --- a/napi/src/peer.rs +++ b/napi/src/peer.rs @@ -1,7 +1,7 @@ use std::net::SocketAddr; use chia_wallet_sdk::{ - self as sdk, connect_peer, create_rustls_connector, load_ssl_cert, Connector, + self as sdk, connect_peer, create_native_tls_connector, load_ssl_cert, Connector, }; use napi::bindgen_prelude::*; @@ -19,7 +19,7 @@ impl Tls { pub fn new(cert_path: String, key_path: String) -> Result { let cert = load_ssl_cert(&cert_path, &key_path) .map_err(|error| Error::from_reason(error.to_string()))?; - let tls = create_rustls_connector(&cert) + let tls = create_native_tls_connector(&cert) .map_err(|error| Error::from_reason(error.to_string()))?; Ok(Self(tls)) }