Skip to content

Commit

Permalink
basic
Browse files Browse the repository at this point in the history
  • Loading branch information
1bananagirl committed Jan 23, 2025
1 parent a3e0452 commit 76eb62d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions wallet/pskt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ workflow-wasm.workspace = true

[dev-dependencies]
serde_json.workspace = true
wasm-bindgen-test.workspace = true
111 changes: 111 additions & 0 deletions wallet/pskt/src/wasm/bundle.rs
Original file line number Diff line number Diff line change
@@ -1 +1,112 @@
use super::error::*;
use super::result::*;
use crate::bundle::Bundle;
use crate::pskt::Inner;
use wasm_bindgen::prelude::*;
use crate::wasm::pskt::PSKT;

#[wasm_bindgen(getter_with_clone)]
#[derive(Debug)]
pub struct PsktBundle(Bundle);

impl Clone for Bundle {
fn clone(&self) -> Self {
Bundle(self.0.clone())
}
}

#[wasm_bindgen]
impl PsktBundle {
#[wasm_bindgen(constructor)]
pub fn new() -> Result<PsktBundle> {
let bundle = Bundle::new();
Ok(PsktBundle(bundle))
}

#[wasm_bindgen]
pub fn serialize(&self) -> Result<String> {
self.0.serialize().map_err(Error::from)
}

#[wasm_bindgen]
pub fn deserialize(hex_data: &str) -> Result<PsktBundle> {
let bundle = Bundle::deserialize(hex_data).map_err(Error::from)?;
Ok(PsktBundle(bundle))
}

#[wasm_bindgen]
pub fn pskt_count(&self) -> usize {
self.0.0.len()
}

#[wasm_bindgen]
pub fn add_pskt(&mut self, pskt: &PSKT) -> Result<()> {
// Use the payload_getter to retrieve the inner data
let payload = pskt.payload_getter();
let inner: Inner = serde_wasm_bindgen::from_value(payload)
.map_err(|_| Error::Custom("Failed to convert PSKT payload".to_string()))?;

self.0.add_inner(inner);
Ok(())
}

#[wasm_bindgen]
pub fn merge(&mut self, other: &PsktBundle) {
self.0.merge(other.0.clone());
}
}

impl From<Bundle> for PsktBundle {
fn from(bundle: Bundle) -> Self {
PsktBundle(bundle)
}
}

impl From<PsktBundle> for Bundle {
fn from(bundle: PsktBundle) -> Self {
bundle.0
}
}

impl AsRef<Bundle> for PsktBundle {
fn as_ref(&self) -> &Bundle {
&self.0
}
}

#[cfg(test)]
mod tests {
use crate::wasm;

use super::*;
use wasm_bindgen_test::*;
use wasm_bindgen::convert::IntoWasmAbi;
use wasm_bindgen_test::wasm_bindgen_test;

#[wasm_bindgen_test]
fn test_pskt_bundle_creation() {
let bundle = PsktBundle::new().expect("Failed to create PsktBundle");
assert_eq!(bundle.pskt_count(), 0, "New bundle should have zero PSKTs");
}

#[wasm_bindgen_test]
fn test_pskt_bundle_serialize_deserialize() {
// Create a bundle
let mut bundle = PsktBundle::new().expect("Failed to create PsktBundle");

// Serialize the bundle
let serialized = bundle.serialize().expect("Failed to serialize bundle");

// Deserialize the bundle
let deserialized_bundle = PsktBundle::deserialize(&serialized)
.expect("Failed to deserialize bundle");

// Check that the deserialized bundle has the same number of PSKTs
assert_eq!(
bundle.pskt_count(),
deserialized_bundle.pskt_count(),
"Deserialized bundle should have same PSKT count"
);
}

}

0 comments on commit 76eb62d

Please sign in to comment.