proof.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use std::ops::Deref;
  2. use cashu::nuts::nut00::Proof;
  3. use wasm_bindgen::prelude::*;
  4. use crate::{nuts::nut01::JsPublicKey, nuts::nut02::JsId, types::JsAmount, types::JsSecret};
  5. #[wasm_bindgen(js_name = Token)]
  6. pub struct JsProof {
  7. inner: Proof,
  8. }
  9. impl Deref for JsProof {
  10. type Target = Proof;
  11. fn deref(&self) -> &Self::Target {
  12. &self.inner
  13. }
  14. }
  15. impl From<Proof> for JsProof {
  16. fn from(inner: Proof) -> JsProof {
  17. JsProof { inner }
  18. }
  19. }
  20. #[wasm_bindgen(js_class = Proof)]
  21. impl JsProof {
  22. #[wasm_bindgen(constructor)]
  23. pub fn new(amount: JsAmount, secret: JsSecret, c: JsPublicKey, id: Option<JsId>) -> JsProof {
  24. Self {
  25. inner: Proof {
  26. amount: *amount.deref(),
  27. secret: secret.deref().clone(),
  28. c: c.deref().clone(),
  29. id: id.map(|i| *i.deref()),
  30. },
  31. }
  32. }
  33. /// Amount
  34. #[wasm_bindgen(getter)]
  35. pub fn amount(&self) -> JsAmount {
  36. self.inner.amount.into()
  37. }
  38. /// Secret
  39. #[wasm_bindgen(getter)]
  40. pub fn secret(&self) -> JsSecret {
  41. self.inner.secret.clone().into()
  42. }
  43. /// C
  44. #[wasm_bindgen(getter)]
  45. pub fn c(&self) -> JsPublicKey {
  46. self.inner.c.clone().into()
  47. }
  48. /// Id
  49. #[wasm_bindgen(getter)]
  50. pub fn id(&self) -> Option<JsId> {
  51. self.inner.id.map(|id| id.into())
  52. }
  53. }