Skip to content

Commit 23a64aa

Browse files
committed
Expose signatures in wasm (0xMiden#1107)
1 parent 06536f0 commit 23a64aa

File tree

17 files changed

+382
-6
lines changed

17 files changed

+382
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* `NoteScript` now has a `toString` method that prints its own MAST source [(#1082)](https://github.com/0xMiden/miden-client/pull/1082).
1515
* Added a `NoteScript` getter for the Web Client `Note` model ([#1135](https://github.com/0xMiden/miden-client/pull/1135/)).
1616
* [BREAKING] Changed `OnNoteReceived` from closure to trait object ([#1080](https://github.com/0xMiden/miden-client/pull/1080)).
17+
* Exposed signatures and serialization for public keys and secret keys [(#1107)](https://github.com/0xMiden/miden-client/pull/1107)
1718

1819
### Features
1920

crates/web-client/js/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
PublicKey,
4545
Rpo256,
4646
SecretKey,
47+
Signature,
4748
SlotAndKeys,
4849
SlotAndKeysArray,
4950
StorageMap,
@@ -106,6 +107,7 @@ export {
106107
PublicKey,
107108
Rpo256,
108109
SecretKey,
110+
Signature,
109111
SlotAndKeys,
110112
SlotAndKeysArray,
111113
StorageMap,

crates/web-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@demox-labs/miden-sdk",
3-
"version": "0.11.0-next.11",
3+
"version": "0.11.0-next.12",
44
"description": "Miden Wasm SDK",
55
"collaborators": [
66
"Miden",

crates/web-client/src/models/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub mod provers;
8181
pub mod public_key;
8282
pub mod rpo256;
8383
pub mod secret_key;
84+
pub mod signature;
8485
pub mod storage_map;
8586
pub mod storage_slot;
8687
pub mod sync_summary;

crates/web-client/src/models/public_key.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1-
use miden_objects::crypto::dsa::rpo_falcon512::PublicKey as NativePublicKey;
1+
use miden_objects::{Word as NativeWord, crypto::dsa::rpo_falcon512::PublicKey as NativePublicKey};
22
use wasm_bindgen::prelude::*;
3+
use wasm_bindgen_futures::js_sys::Uint8Array;
4+
5+
use crate::{
6+
models::{signature::Signature, word::Word},
7+
utils::{deserialize_from_uint8array, serialize_to_uint8array},
8+
};
39

410
#[wasm_bindgen]
511
#[derive(Copy, Clone)]
612
pub struct PublicKey(NativePublicKey);
713

14+
#[wasm_bindgen]
15+
impl PublicKey {
16+
pub fn serialize(&self) -> Uint8Array {
17+
let native_word: NativeWord = self.0.into();
18+
serialize_to_uint8array(&native_word)
19+
}
20+
21+
pub fn deserialize(bytes: &Uint8Array) -> Result<PublicKey, JsValue> {
22+
let native_word = deserialize_from_uint8array::<NativeWord>(bytes)?;
23+
let native_public_key = NativePublicKey::new(native_word);
24+
Ok(PublicKey(native_public_key))
25+
}
26+
27+
pub fn verify(&self, message: &Word, signature: &Signature) -> bool {
28+
let native_signature = signature.into();
29+
self.0.verify(message.into(), &native_signature)
30+
}
31+
}
32+
833
// CONVERSIONS
934
// ================================================================================================
1035

crates/web-client/src/models/secret_key.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
use miden_objects::crypto::dsa::rpo_falcon512::SecretKey as NativeSecretKey;
1+
use miden_objects::{Word as NativeWord, crypto::dsa::rpo_falcon512::SecretKey as NativeSecretKey};
22
use rand::{SeedableRng, rngs::StdRng};
33
use wasm_bindgen::prelude::*;
4+
use wasm_bindgen_futures::js_sys::Uint8Array;
45

5-
use crate::models::public_key::PublicKey;
6+
use crate::{
7+
models::{public_key::PublicKey, signature::Signature, word::Word},
8+
utils::{deserialize_from_uint8array, serialize_to_uint8array},
9+
};
610

711
#[wasm_bindgen]
812
pub struct SecretKey(NativeSecretKey);
@@ -28,6 +32,22 @@ impl SecretKey {
2832
pub fn public_key(&self) -> PublicKey {
2933
self.0.public_key().into()
3034
}
35+
36+
pub fn sign(&self, message: &Word) -> Result<Signature, JsValue> {
37+
let native_message: NativeWord = message.into();
38+
let mut rng = StdRng::from_os_rng();
39+
let signature = self.0.sign_with_rng(native_message, &mut rng);
40+
Ok(signature.into())
41+
}
42+
43+
pub fn serialize(&self) -> Uint8Array {
44+
serialize_to_uint8array(&self.0)
45+
}
46+
47+
pub fn deserialize(bytes: &Uint8Array) -> Result<SecretKey, JsValue> {
48+
let native_secret_key = deserialize_from_uint8array::<NativeSecretKey>(bytes)?;
49+
Ok(SecretKey(native_secret_key))
50+
}
3151
}
3252

3353
// CONVERSIONS
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use miden_objects::crypto::dsa::rpo_falcon512::Signature as NativeSignature;
2+
use wasm_bindgen::prelude::*;
3+
use wasm_bindgen_futures::js_sys::Uint8Array;
4+
5+
use crate::utils::{deserialize_from_uint8array, serialize_to_uint8array};
6+
7+
#[wasm_bindgen]
8+
#[derive(Clone)]
9+
pub struct Signature(NativeSignature);
10+
11+
#[wasm_bindgen]
12+
impl Signature {
13+
pub fn serialize(&self) -> Uint8Array {
14+
serialize_to_uint8array(&self.0)
15+
}
16+
17+
pub fn deserialize(bytes: &Uint8Array) -> Result<Signature, JsValue> {
18+
let native_signature = deserialize_from_uint8array::<NativeSignature>(bytes)?;
19+
Ok(Signature(native_signature))
20+
}
21+
}
22+
23+
// CONVERSIONS
24+
// ================================================================================================
25+
26+
impl From<NativeSignature> for Signature {
27+
fn from(native_signature: NativeSignature) -> Self {
28+
Signature(native_signature)
29+
}
30+
}
31+
32+
impl From<&NativeSignature> for Signature {
33+
fn from(native_signature: &NativeSignature) -> Self {
34+
Signature(native_signature.clone())
35+
}
36+
}
37+
38+
impl From<Signature> for NativeSignature {
39+
fn from(signature: Signature) -> Self {
40+
signature.0
41+
}
42+
}
43+
44+
impl From<&Signature> for NativeSignature {
45+
fn from(signature: &Signature) -> Self {
46+
signature.0.clone()
47+
}
48+
}

crates/web-client/src/models/word.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use miden_objects::{Felt as NativeFelt, Word as NativeWord};
22
use wasm_bindgen::prelude::*;
3+
use wasm_bindgen_futures::js_sys::Uint8Array;
34

45
use super::felt::Felt;
6+
use crate::utils::{deserialize_from_uint8array, serialize_to_uint8array};
57

68
#[wasm_bindgen]
79
#[derive(Clone)]
@@ -45,6 +47,15 @@ impl Word {
4547
self.0.to_hex()
4648
}
4749

50+
pub fn serialize(&self) -> Uint8Array {
51+
serialize_to_uint8array(&self.0)
52+
}
53+
54+
pub fn deserialize(bytes: &Uint8Array) -> Result<Word, JsValue> {
55+
let native_word = deserialize_from_uint8array::<NativeWord>(bytes)?;
56+
Ok(Word(native_word))
57+
}
58+
4859
#[wasm_bindgen(js_name = "toU64s")]
4960
pub fn to_u64s(&self) -> Vec<u64> {
5061
self.0.iter().map(NativeFelt::as_int).collect::<Vec<u64>>()

crates/web-client/src/utils/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use wasm_bindgen_futures::js_sys::Uint8Array;
55

66
pub mod assembler_utils;
77

8+
use crate::js_error_with_context;
9+
810
#[cfg(feature = "testing")]
911
pub mod test_utils;
1012

@@ -20,6 +22,5 @@ pub fn serialize_to_uint8array<T: Serializable>(value: &T) -> Uint8Array {
2022
pub fn deserialize_from_uint8array<T: Deserializable>(bytes: &Uint8Array) -> Result<T, JsValue> {
2123
let vec = bytes.to_vec();
2224
let mut reader = SliceReader::new(&vec);
23-
T::read_from(&mut reader)
24-
.map_err(|e| JsValue::from_str(&format!("Deserialization error: {e:?}")))
25+
T::read_from(&mut reader).map_err(|e| js_error_with_context(e, "failed to deserialize"))
2526
}

crates/web-client/test/global.test.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
PublicKey,
3838
Rpo256,
3939
SecretKey,
40+
Signature,
4041
SlotAndKeys,
4142
SlotAndKeysArray,
4243
StorageMap,
@@ -103,6 +104,7 @@ declare global {
103104
PublicKey: typeof PublicKey;
104105
Rpo256: typeof Rpo256;
105106
SecretKey: typeof SecretKey;
107+
Signature: typeof Signature;
106108
SlotAndKeys: typeof SlotAndKeys;
107109
SlotAndKeysArray: typeof SlotAndKeysArray;
108110
StorageMap: typeof StorageMap;

0 commit comments

Comments
 (0)