diff --git a/wasm-attestation-bindings/src/lib.rs b/wasm-attestation-bindings/src/lib.rs index fd5af23..37bb640 100644 --- a/wasm-attestation-bindings/src/lib.rs +++ b/wasm-attestation-bindings/src/lib.rs @@ -217,3 +217,24 @@ pub fn get_user_data(attestation_doc: &str) -> Result>, JsValue> ))), } } + +/// Given an attestation document, return the nonce if it exists +#[wasm_bindgen(js_name = getNonce)] +pub fn get_nonce(attestation_doc: &str) -> Result>, JsValue> { + let decoded_ad = match BASE64_STANDARD.decode(attestation_doc.as_bytes()) { + Ok(ad) => ad, + Err(e) => { + return Err(JsValue::from_str(&format!( + "Failed to decode attestation document: {:?}", + e + ))) + } + }; + match decode_attestation_document(&decoded_ad) { + Ok((_, doc)) => Ok(doc.nonce.map(|buf| buf.to_vec())), + Err(e) => Err(JsValue::from_str(&format!( + "Failed to decode attestation document: {:?}", + e + ))), + } +}