Skip to content

Commit

Permalink
chore: some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Dec 20, 2024
1 parent 3f3f76d commit cc1b069
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/wasm-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
# ] }
Expand Down
150 changes: 148 additions & 2 deletions packages/wasm-sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,157 @@
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" {
fn alert(s: &str);
}

#[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<dash_sdk::dpp::platform_value::BinaryData, dash_sdk::dpp::ProtocolError> {
todo!()
}
}

0 comments on commit cc1b069

Please sign in to comment.