-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
2 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
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!() | ||
} | ||
} |