1use crate::{leader_index as compute_leader_index, Block, Finalized, Notarized, Seed};
2use commonware_cryptography::bls12381::PublicKey;
3use serde::Serialize;
4use wasm_bindgen::prelude::*;
5
6#[derive(Serialize)]
7pub struct SeedJs {
8 pub view: u64,
9 pub signature: Vec<u8>,
10}
11
12#[derive(Serialize)]
13pub struct ProofJs {
14 pub view: u64,
15 pub parent: u64,
16 pub payload: Vec<u8>,
17 pub signature: Vec<u8>,
18}
19
20#[derive(Serialize)]
21pub struct BlockJs {
22 pub parent: Vec<u8>,
23 pub height: u64,
24 pub timestamp: u64,
25 pub digest: Vec<u8>,
26}
27
28#[derive(Serialize)]
29pub struct NotarizedJs {
30 pub proof: ProofJs,
31 pub block: BlockJs,
32}
33
34#[derive(Serialize)]
35pub struct FinalizedJs {
36 pub proof: ProofJs,
37 pub block: BlockJs,
38}
39
40#[wasm_bindgen]
41pub fn parse_seed(public_key: Option<Vec<u8>>, bytes: Vec<u8>) -> JsValue {
42 let mut public = None;
43 if let Some(pk) = public_key {
44 public = Some(PublicKey::try_from(pk).expect("invalid public key"));
45 }
46 match Seed::deserialize(public.as_ref(), &bytes) {
47 Some(s) => {
48 let seed_js = SeedJs {
49 view: s.view,
50 signature: s.signature.to_vec(),
51 };
52 serde_wasm_bindgen::to_value(&seed_js).unwrap_or(JsValue::NULL)
53 }
54 None => JsValue::NULL,
55 }
56}
57
58#[wasm_bindgen]
59pub fn parse_notarized(public_key: Option<Vec<u8>>, bytes: Vec<u8>) -> JsValue {
60 let mut public = None;
61 if let Some(pk) = public_key {
62 public = Some(PublicKey::try_from(pk).expect("invalid public key"));
63 }
64 match Notarized::deserialize(public.as_ref(), &bytes) {
65 Some(n) => {
66 let notarized_js = NotarizedJs {
67 proof: ProofJs {
68 view: n.proof.view,
69 parent: n.proof.parent,
70 payload: n.proof.payload.to_vec(),
71 signature: n.proof.signature.to_vec(),
72 },
73 block: BlockJs {
74 parent: n.block.parent.to_vec(),
75 height: n.block.height,
76 timestamp: n.block.timestamp,
77 digest: n.block.digest().to_vec(),
78 },
79 };
80 serde_wasm_bindgen::to_value(¬arized_js).unwrap_or(JsValue::NULL)
81 }
82 None => JsValue::NULL,
83 }
84}
85
86#[wasm_bindgen]
87pub fn parse_finalized(public_key: Option<Vec<u8>>, bytes: Vec<u8>) -> JsValue {
88 let mut public = None;
89 if let Some(pk) = public_key {
90 public = Some(PublicKey::try_from(pk).expect("invalid public key"));
91 }
92 match Finalized::deserialize(public.as_ref(), &bytes) {
93 Some(f) => {
94 let finalized_js = FinalizedJs {
95 proof: ProofJs {
96 view: f.proof.view,
97 parent: f.proof.parent,
98 payload: f.proof.payload.to_vec(),
99 signature: f.proof.signature.to_vec(),
100 },
101 block: BlockJs {
102 parent: f.block.parent.to_vec(),
103 height: f.block.height,
104 timestamp: f.block.timestamp,
105 digest: f.block.digest().to_vec(),
106 },
107 };
108 serde_wasm_bindgen::to_value(&finalized_js).unwrap_or(JsValue::NULL)
109 }
110 None => JsValue::NULL,
111 }
112}
113
114#[wasm_bindgen]
115pub fn parse_block(bytes: Vec<u8>) -> JsValue {
116 match Block::deserialize(&bytes) {
117 Some(b) => {
118 let block_js = BlockJs {
119 parent: b.parent.to_vec(),
120 height: b.height,
121 timestamp: b.timestamp,
122 digest: b.digest().to_vec(),
123 };
124 serde_wasm_bindgen::to_value(&block_js).unwrap_or(JsValue::NULL)
125 }
126 None => JsValue::NULL,
127 }
128}
129
130#[wasm_bindgen]
131pub fn leader_index(seed: Vec<u8>, participants: usize) -> usize {
132 compute_leader_index(&seed, participants)
133}