Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/wasm-dpp2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ pub use platform_address::{
PlatformAddressWasm, default_fee_strategy, fee_strategy_from_steps,
fee_strategy_from_steps_or_default, outputs_to_btree_map, outputs_to_optional_btree_map,
};
pub use platform_address::transitions::{
AddressCreditWithdrawalTransitionWasm, AddressFundingFromAssetLockTransitionWasm,
AddressFundsTransferTransitionWasm, IdentityCreateFromAddressesTransitionWasm,
IdentityCreditTransferToAddressesTransitionWasm, IdentityTopUpFromAddressesTransitionWasm,
};
pub use state_transitions::base::{GroupStateTransitionInfoWasm, StateTransitionWasm};
pub use state_transitions::proof_result::{StateTransitionProofResultTypeJs, convert_proof_result};
pub use tokens::*;
Expand Down
13 changes: 12 additions & 1 deletion packages/wasm-dpp2/src/platform_address/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use crate::utils::IntoWasm;
use dpp::address_funds::PlatformAddress;
use dpp::dashcore::Network;
use js_sys::Uint8Array;
use serde::de::{self, Error, Visitor};
use serde::de::{self, Error, MapAccess, Visitor};
use serde::ser::Serializer;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value as JsonValue;
use std::fmt;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -197,6 +198,16 @@ impl<'de> Visitor<'de> for PlatformAddressWasmVisitor {
.map(PlatformAddressWasm)
.map_err(|e| A::Error::custom(e.to_string()))
}

fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let value = JsonValue::deserialize(de::value::MapAccessDeserializer::new(map))
.map_err(M::Error::custom)?;
let js_value = serde_wasm_bindgen::to_value(&value).map_err(M::Error::custom)?;
PlatformAddressWasm::try_from(&js_value).map_err(|err| M::Error::custom(err.to_string()))
}
}

impl<'de> Deserialize<'de> for PlatformAddressWasm {
Expand Down
31 changes: 31 additions & 0 deletions packages/wasm-dpp2/src/platform_address/fee_strategy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::error::{WasmDppError, WasmDppResult};
use crate::impl_wasm_type_info;
use crate::utils::{IntoWasm, try_from_options_optional_with, try_to_array};
use dpp::address_funds::{AddressFundsFeeStrategy, AddressFundsFeeStrategyStep};
use serde::Deserialize;
use serde::de::{self, Deserializer, MapAccess, Visitor};
Expand Down Expand Up @@ -56,6 +59,8 @@ impl FeeStrategyStepWasm {
}
}

impl_wasm_type_info!(FeeStrategyStepWasm, FeeStrategyStep);

impl From<FeeStrategyStepWasm> for AddressFundsFeeStrategyStep {
fn from(step: FeeStrategyStepWasm) -> Self {
step.0
Expand Down Expand Up @@ -87,6 +92,32 @@ pub fn fee_strategy_from_steps_or_default(
.unwrap_or_else(default_fee_strategy)
}

/// Extract an optional Vec<FeeStrategyStepWasm> from a JS options object property.
///
/// Returns None if the property is undefined or null.
pub fn fee_strategy_from_js_options(
options: &JsValue,
field_name: &str,
) -> WasmDppResult<Option<Vec<FeeStrategyStepWasm>>> {
try_from_options_optional_with(options, field_name, |v| {
let array = try_to_array(v, field_name)?;
array
.iter()
.enumerate()
.map(|(i, item)| {
item.to_wasm::<FeeStrategyStepWasm>("FeeStrategyStep")
.map(|r| (*r).clone())
.map_err(|_| {
WasmDppError::invalid_argument(format!(
"{}[{}] is not a FeeStrategyStep",
field_name, i
))
})
})
.collect()
})
}

impl<'de> Deserialize<'de> for FeeStrategyStepWasm {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
135 changes: 133 additions & 2 deletions packages/wasm-dpp2/src/platform_address/input_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{PlatformAddressLikeJs, PlatformAddressWasm};
use crate::error::WasmDppResult;
use crate::utils::try_to_u64;
use crate::error::{WasmDppError, WasmDppResult};
use crate::impl_wasm_type_info;
use crate::utils::{try_to_u64, IntoWasm};
use dpp::address_funds::PlatformAddress;
use dpp::fee::Credits;
use dpp::prelude::AddressNonce;
Expand All @@ -22,6 +23,8 @@ pub struct PlatformAddressInputWasm {
amount: Credits,
}

impl_wasm_type_info!(PlatformAddressInputWasm, PlatformAddressInput);

#[wasm_bindgen(js_class = PlatformAddressInput)]
impl PlatformAddressInputWasm {
/// Creates a new PlatformAddressInput.
Expand Down Expand Up @@ -65,6 +68,14 @@ impl PlatformAddressInputWasm {
}

impl PlatformAddressInputWasm {
pub fn new(address: PlatformAddress, nonce: AddressNonce, amount: Credits) -> Self {
Self {
address: address.into(),
nonce,
amount,
}
}

/// Returns the inner values as a tuple suitable for BTreeMap insertion.
pub fn into_inner(self) -> (PlatformAddress, (AddressNonce, Credits)) {
(self.address.into(), (self.nonce, self.amount))
Expand All @@ -86,6 +97,8 @@ pub struct PlatformAddressOutputWasm {
amount: Option<Credits>,
}

impl_wasm_type_info!(PlatformAddressOutputWasm, PlatformAddressOutput);

#[wasm_bindgen(js_class = PlatformAddressOutput)]
impl PlatformAddressOutputWasm {
/// Creates a new PlatformAddressOutput with a specific amount.
Expand Down Expand Up @@ -122,6 +135,20 @@ impl PlatformAddressOutputWasm {
}

impl PlatformAddressOutputWasm {
pub fn new(address: PlatformAddress, amount: Credits) -> Self {
Self {
address: address.into(),
amount: Some(amount),
}
}

pub fn new_optional(address: PlatformAddress, amount: Option<Credits>) -> Self {
Self {
address: address.into(),
amount,
}
}

/// Returns the inner values as a tuple suitable for BTreeMap insertion.
/// Panics if amount is None - use `into_inner_optional` for optional amounts.
pub fn into_inner(self) -> (PlatformAddress, Credits) {
Expand Down Expand Up @@ -156,3 +183,107 @@ pub fn outputs_to_optional_btree_map(
.map(|o| o.into_inner_optional())
.collect()
}

/// Extract a Vec<PlatformAddressInputWasm> from a JS options object property.
///
/// Reads the named property as a JS array, then extracts each element
/// as a PlatformAddressInput wasm-bindgen object via its __wbg_ptr.
pub fn inputs_from_js_options(
options: &JsValue,
field_name: &str,
) -> WasmDppResult<Vec<PlatformAddressInputWasm>> {
let value = js_sys::Reflect::get(options, &JsValue::from_str(field_name)).map_err(|_| {
WasmDppError::invalid_argument(format!("failed to read '{}' from options", field_name))
})?;
if value.is_undefined() || value.is_null() {
return Err(WasmDppError::invalid_argument(format!(
"'{}' is required",
field_name
)));
}
if !js_sys::Array::is_array(&value) {
return Err(WasmDppError::invalid_argument(format!(
"'{}' must be an array",
field_name
)));
}
let array = js_sys::Array::from(&value);
array
.iter()
.enumerate()
.map(|(i, item)| {
item.to_wasm::<PlatformAddressInputWasm>("PlatformAddressInput")
.map(|r| (*r).clone())
.map_err(|_| {
WasmDppError::invalid_argument(format!(
"{}[{}] is not a PlatformAddressInput",
field_name, i
))
})
})
.collect()
}

/// Extract a Vec<PlatformAddressOutputWasm> from a JS options object property.
///
/// Reads the named property as a JS array, then extracts each element
/// as a PlatformAddressOutput wasm-bindgen object via its __wbg_ptr.
pub fn outputs_from_js_options(
options: &JsValue,
field_name: &str,
) -> WasmDppResult<Vec<PlatformAddressOutputWasm>> {
let value = js_sys::Reflect::get(options, &JsValue::from_str(field_name)).map_err(|_| {
WasmDppError::invalid_argument(format!("failed to read '{}' from options", field_name))
})?;
if value.is_undefined() || value.is_null() {
return Err(WasmDppError::invalid_argument(format!(
"'{}' is required",
field_name
)));
}
if !js_sys::Array::is_array(&value) {
return Err(WasmDppError::invalid_argument(format!(
"'{}' must be an array",
field_name
)));
}
let array = js_sys::Array::from(&value);
array
.iter()
.enumerate()
.map(|(i, item)| {
item.to_wasm::<PlatformAddressOutputWasm>("PlatformAddressOutput")
.map(|r| (*r).clone())
.map_err(|_| {
WasmDppError::invalid_argument(format!(
"{}[{}] is not a PlatformAddressOutput",
field_name, i
))
})
})
.collect()
}

/// Extract an optional PlatformAddressOutputWasm from a JS options object property.
///
/// Returns None if the property is undefined or null.
pub fn optional_output_from_js_options(
options: &JsValue,
field_name: &str,
) -> WasmDppResult<Option<PlatformAddressOutputWasm>> {
let value = js_sys::Reflect::get(options, &JsValue::from_str(field_name)).map_err(|_| {
WasmDppError::invalid_argument(format!("failed to read '{}' from options", field_name))
})?;
if value.is_undefined() || value.is_null() {
return Ok(None);
}
value
.to_wasm::<PlatformAddressOutputWasm>("PlatformAddressOutput")
.map(|r| Some((*r).clone()))
.map_err(|_| {
WasmDppError::invalid_argument(format!(
"'{}' is not a PlatformAddressOutput",
field_name
))
})
}
8 changes: 5 additions & 3 deletions packages/wasm-dpp2/src/platform_address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ mod address;
mod fee_strategy;
mod input_output;
mod signer;
pub mod transitions;

pub use address::{PlatformAddressLikeArrayJs, PlatformAddressLikeJs, PlatformAddressWasm};
pub use fee_strategy::{
FeeStrategyStepWasm, default_fee_strategy, fee_strategy_from_steps,
fee_strategy_from_steps_or_default,
FeeStrategyStepWasm, default_fee_strategy, fee_strategy_from_js_options,
fee_strategy_from_steps, fee_strategy_from_steps_or_default,
};
pub use input_output::{
PlatformAddressInputWasm, PlatformAddressOutputWasm, outputs_to_btree_map,
PlatformAddressInputWasm, PlatformAddressOutputWasm, inputs_from_js_options,
optional_output_from_js_options, outputs_from_js_options, outputs_to_btree_map,
outputs_to_optional_btree_map,
};
pub use signer::PlatformAddressSignerWasm;
Loading
Loading