diff --git a/Cargo.lock b/Cargo.lock index d518d86768..d172b0aa8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5990,6 +5990,7 @@ version = "0.1.0" dependencies = [ "dash-sdk", "wasm-bindgen", + "wasm-bindgen-futures", "web-sys", ] diff --git a/packages/wasm-sdk/Cargo.toml b/packages/wasm-sdk/Cargo.toml index f93a19c161..846027d5a7 100644 --- a/packages/wasm-sdk/Cargo.toml +++ b/packages/wasm-sdk/Cargo.toml @@ -14,8 +14,10 @@ web-sys = { version = "0.3.4", features = [ 'HtmlElement', 'Node', 'Window', + 'console', ] } wasm-bindgen = { version = "=0.2.99" } +wasm-bindgen-futures = { version = "0.4.49" } # tenderdash-proto = { path = "../../../rs-tenderdash-abci/proto", default-features = false, features = [ # "client", # ] } diff --git a/packages/wasm-sdk/src/lib.rs b/packages/wasm-sdk/src/lib.rs index f2e774f8dd..c053225969 100644 --- a/packages/wasm-sdk/src/lib.rs +++ b/packages/wasm-sdk/src/lib.rs @@ -1,4 +1,55 @@ +use std::{ + ops::{Deref, DerefMut}, + str::FromStr, +}; + +use dash_sdk::{ + dpp::{ + block::extended_epoch_info::ExtendedEpochInfo, + dashcore::{ + key::Secp256k1, + secp256k1::{self, SecretKey}, + Network, PrivateKey, + }, + data_contract::accessors::v0::DataContractV0Getters, + document::serialization_traits::DocumentPlatformConversionMethodsV0, + identity::signer::Signer, + prelude::AssetLockProof, + serialization::{PlatformSerializable, PlatformSerializableWithPlatformVersion}, + }, + platform::{ + transition::{broadcast::BroadcastStateTransition, put_identity::PutIdentity}, + DataContract, Document, DocumentQuery, Fetch, Identifier, Identity, + }, + sdk::AddressList, + Sdk, SdkBuilder, +}; use wasm_bindgen::prelude::*; +use web_sys::{console, js_sys}; + +#[wasm_bindgen] +pub struct WasmSdk(Sdk); +// Dereference JsSdk to Sdk so that we can use &JsSdk everywhere where &sdk is needed +impl std::ops::Deref for WasmSdk { + type Target = Sdk; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +#[wasm_bindgen] +pub struct WasmSdkBuilder(SdkBuilder); +impl Deref for WasmSdkBuilder { + type Target = SdkBuilder; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for WasmSdkBuilder { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} #[wasm_bindgen] extern "C" { @@ -6,6 +57,101 @@ extern "C" { } #[wasm_bindgen] -pub fn greet(name: &str) { - alert(&format!("Hello, {}!", name)); +pub async fn identity_testing() { + let sdk = SdkBuilder::new(AddressList::new()) + .build() + .expect("build sdk"); + + let id = Identifier::random(); + let identity = Identity::fetch_by_identifier(&sdk, id.clone()) + .await + .expect("fetch identity") + .expect("identity not found"); + + let identity_serialized = + PlatformSerializable::serialize_to_bytes(&identity).expect("serialize"); + + let asset_lock_proof = AssetLockProof::default(); + let asset_lock_proof_private_key = + PrivateKey::from_slice(&[0; 32], Network::Testnet).expect("create private key"); + + let signer = MockSigner; + let pushed: Identity = identity + .put_to_platform( + &sdk, + asset_lock_proof, + &asset_lock_proof_private_key, + &signer, + None, + ) + .await + .expect("put identity") + .broadcast_and_wait(&sdk, None) + .await + .unwrap(); +} + +#[wasm_bindgen] +pub async fn epoch_testing() { + let sdk = SdkBuilder::new(AddressList::new()) + .build() + .expect("build sdk"); + + let ei = ExtendedEpochInfo::fetch(&sdk, 0) + .await + .expect("fetch extended epoch info") + .expect("extended epoch info not found"); +} + +#[wasm_bindgen] +pub async fn setup_sdk() -> WasmSdk { + let sdk = SdkBuilder::new(AddressList::new()) + .build() + .expect("build sdk"); + WasmSdk(sdk) +} + +#[wasm_bindgen] +pub async fn docs_testing(sdk: &WasmSdk) { + let id = Identifier::random(); + + let dc = DataContract::fetch(&sdk, id) + .await + .expect("fetch data contract") + .expect("data contract not found"); + + let dcs = dc + .serialize_to_bytes_with_platform_version(sdk.version()) + .expect("serialize data contract"); + + let query = DocumentQuery::new(dc.clone(), "asd").expect("create query"); + let doc = Document::fetch(sdk, query) + .await + .expect("fetch document") + .expect("document not found"); + + let document_type = dc + .document_type_for_name("aaa") + .expect("document type for name"); + let doc_serialized = doc + .serialize(document_type, sdk.version()) + .expect("serialize document"); + + let msg = js_sys::JsString::from_str(&format!("{:?} {:?} ", dcs, doc_serialized)) + .expect("create js string"); + console::log_1(&msg); +} +#[derive(Clone, Debug)] +struct MockSigner; +impl Signer for MockSigner { + fn can_sign_with(&self, identity_public_key: &dash_sdk::platform::IdentityPublicKey) -> bool { + true + } + fn sign( + &self, + identity_public_key: &dash_sdk::platform::IdentityPublicKey, + data: &[u8], + ) -> Result { + todo!() + } }