Skip to content

Commit 9b097c2

Browse files
[Feature] Expose bit, byte, and fields serialization methods for plaintext, ciphertext, records, and addresses (#1010)
* exposed plaintext.to_bits_le function * exposed address.to_bits_le function * added address.plaintextBitsLe function * added bit and byte serialization methods for plaintext, ciphertext, records, and addresses * added all serialization/deserialization methods for address, signature, plaintext, ciphertext, recordplaintext, and recordciphertext which are available from snarkVM * removed plaintext serialization methods from address and signature * added tests for hash functions on plaintext literal types * added signatures to hash tests and added TryFrom field->group methods
1 parent 6ec7b6c commit 9b097c2

File tree

11 files changed

+746
-47
lines changed

11 files changed

+746
-47
lines changed

wasm/src/account/address.rs

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::account::{PrivateKey, Signature, ViewKey};
17+
use crate::{
18+
Field,
19+
Group,
20+
Plaintext,
21+
account::{PrivateKey, Signature, ViewKey, compute_key::ComputeKey},
22+
from_js_typed_array,
23+
from_wasm_object_array,
24+
js_array_from_fields,
25+
native::{FieldNative, LiteralNative},
26+
to_bits_array_le,
27+
types::native::AddressNative,
28+
};
29+
use snarkvm_console::prelude::{FromBits, FromBytes, FromFields, ToBits, ToBytes, ToFields};
1830

19-
use crate::{account::compute_key::ComputeKey, types::native::AddressNative};
2031
use core::{convert::TryFrom, fmt, ops::Deref, str::FromStr};
21-
use wasm_bindgen::prelude::*;
32+
use js_sys::{Array, Uint8Array};
33+
use wasm_bindgen::{convert::TryFromJsValue, prelude::*};
2234

2335
/// Public address of an Aleo account
2436
#[wasm_bindgen]
@@ -50,6 +62,79 @@ impl Address {
5062
compute_key.address()
5163
}
5264

65+
/// Get an address from a series of bytes.
66+
///
67+
/// @param {Uint8Array} bytes A left endian byte array representing the address.
68+
///
69+
/// @returns {Address} The address object.
70+
#[wasm_bindgen(js_name = "fromBytesLe")]
71+
pub fn from_bytes_le(bytes: Uint8Array) -> Result<Self, String> {
72+
let rust_bytes = bytes.to_vec();
73+
let native = AddressNative::from_bytes_le(rust_bytes.as_slice()).map_err(|e| e.to_string())?;
74+
Ok(Self(native))
75+
}
76+
77+
/// Get the left endian byte array representation of the address.
78+
#[wasm_bindgen(js_name = "toBytesLe")]
79+
pub fn to_bytes_le(&self) -> Result<Uint8Array, String> {
80+
let rust_bytes = self.0.to_bytes_le().map_err(|e| e.to_string())?;
81+
Ok(Uint8Array::from(rust_bytes.as_slice()))
82+
}
83+
84+
/// Get an address from a series of bits represented as a boolean array.
85+
///
86+
/// @param {Array} bits A left endian boolean array representing the bits of the address.
87+
///
88+
/// @returns {Address} The address object.
89+
#[wasm_bindgen(js_name = "fromBitsLe")]
90+
pub fn from_bits_le(bits: Array) -> Result<Self, String> {
91+
let rust_bits = from_js_typed_array!(bits, as_bool, "boolean")?;
92+
let native = AddressNative::from_bits_le(&rust_bits).map_err(|e| e.to_string())?;
93+
Ok(Self(native))
94+
}
95+
96+
/// Get the left endian boolean array representation of the bits of the address.
97+
#[wasm_bindgen(js_name = "toBitsLe")]
98+
pub fn to_bits_le(&self) -> Array {
99+
to_bits_array_le!(self)
100+
}
101+
102+
/// Get an address object from an array of fields.
103+
///
104+
/// @param {Array} fields An array of fields.
105+
///
106+
/// @returns {Plaintext} The address object.
107+
#[wasm_bindgen(js_name = "fromFields")]
108+
pub fn from_fields(fields: Array) -> Result<Self, String> {
109+
let native_fields = from_wasm_object_array!(fields, Field)?;
110+
let native = AddressNative::from_fields(&native_fields).map_err(|e| e.to_string())?;
111+
Ok(Self(native))
112+
}
113+
114+
/// Get the field array representation of the address.
115+
#[wasm_bindgen(js_name = "toFields")]
116+
pub fn to_fields(&self) -> Result<Array, String> {
117+
let native = self.0.clone();
118+
let native_fields = native.to_fields().map_err(|e| e.to_string())?;
119+
Ok(js_array_from_fields!(&native_fields))
120+
}
121+
122+
/// Get an address object from a group.
123+
///
124+
/// @param {Group} group The group object.
125+
///
126+
/// @returns {Address} The address object.
127+
#[wasm_bindgen(js_name = "fromGroup")]
128+
pub fn from_group(group: Group) -> Self {
129+
Self::from(group)
130+
}
131+
132+
/// Get the group representation of the address object.
133+
#[wasm_bindgen(js_name = "toGroup")]
134+
pub fn to_group(&self) -> Group {
135+
Group::from(self)
136+
}
137+
53138
/// Create an aleo address object from a string representation of an address
54139
///
55140
/// @param {string} address String representation of an addressm
@@ -67,6 +152,12 @@ impl Address {
67152
self.0.to_string()
68153
}
69154

155+
/// Get the plaintext representation of the address.
156+
#[wasm_bindgen(js_name = "toPlaintext")]
157+
pub fn to_plaintext(&self) -> Plaintext {
158+
Plaintext::from(LiteralNative::Address(self.0))
159+
}
160+
70161
/// Verify a signature for a message signed by the address
71162
///
72163
/// @param {Uint8Array} Byte array representing a message signed by the address
@@ -114,6 +205,31 @@ impl From<&Address> for AddressNative {
114205
}
115206
}
116207

208+
impl From<AddressNative> for Group {
209+
fn from(value: AddressNative) -> Self {
210+
let vm_group = value.to_group();
211+
Self::from(vm_group)
212+
}
213+
}
214+
215+
impl From<Address> for Group {
216+
fn from(value: Address) -> Self {
217+
Self::from(value.0)
218+
}
219+
}
220+
221+
impl From<&AddressNative> for Group {
222+
fn from(value: &AddressNative) -> Self {
223+
Self::from(value.clone())
224+
}
225+
}
226+
227+
impl From<&Address> for Group {
228+
fn from(value: &Address) -> Self {
229+
Self::from(value.0)
230+
}
231+
}
232+
117233
impl FromStr for Address {
118234
type Err = anyhow::Error;
119235

wasm/src/account/signature.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,22 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::{Address, PrivateKey, Scalar, types::native::SignatureNative};
17+
use crate::{
18+
Address,
19+
Field,
20+
Plaintext,
21+
PrivateKey,
22+
Scalar,
23+
from_js_typed_array,
24+
js_array_from_fields,
25+
native::LiteralNative,
26+
to_bits_array_le,
27+
types::native::SignatureNative,
28+
};
29+
use snarkvm_console::prelude::{FromBits, FromBytes, ToBits, ToBytes, ToFields};
1830

1931
use core::{fmt, ops::Deref, str::FromStr};
32+
use js_sys::{Array, Uint8Array};
2033
use rand::{SeedableRng, rngs::StdRng};
2134
use wasm_bindgen::prelude::*;
2235

@@ -61,6 +74,51 @@ impl Signature {
6174
self.0.verify_bytes(address, message)
6275
}
6376

77+
/// Get a signature from a series of bytes.
78+
///
79+
/// @param {Uint8Array} bytes A left endian byte array representing the signature.
80+
///
81+
/// @returns {Signature} The signature object.
82+
#[wasm_bindgen(js_name = "fromBytesLe")]
83+
pub fn from_bytes_le(bytes: Uint8Array) -> Result<Self, String> {
84+
let rust_bytes = bytes.to_vec();
85+
let native = SignatureNative::from_bytes_le(rust_bytes.as_slice()).map_err(|e| e.to_string())?;
86+
Ok(Self(native))
87+
}
88+
89+
/// Get the left endian byte array representation of the signature.
90+
#[wasm_bindgen(js_name = "toBytesLe")]
91+
pub fn to_bytes_le(&self) -> Result<Uint8Array, String> {
92+
let rust_bytes = self.0.to_bytes_le().map_err(|e| e.to_string())?;
93+
Ok(Uint8Array::from(rust_bytes.as_slice()))
94+
}
95+
96+
/// Get a signature from a series of bits represented as a boolean array.
97+
///
98+
/// @param {Array} bits A left endian boolean array representing the bits of the signature.
99+
///
100+
/// @returns {Signature} The signature object.
101+
#[wasm_bindgen(js_name = "fromBitsLe")]
102+
pub fn from_bits_le(bits: Array) -> Result<Self, String> {
103+
let rust_bits = from_js_typed_array!(bits, as_bool, "boolean")?;
104+
let native = SignatureNative::from_bits_le(&rust_bits).map_err(|e| e.to_string())?;
105+
Ok(Self(native))
106+
}
107+
108+
/// Get the left endian boolean array representation of the bits of the signature.
109+
#[wasm_bindgen(js_name = "toBitsLe")]
110+
pub fn to_bits_le(&self) -> Array {
111+
to_bits_array_le!(self)
112+
}
113+
114+
/// Get the field array representation of the signature.
115+
#[wasm_bindgen(js_name = "toFields")]
116+
pub fn to_fields(&self) -> Result<Array, String> {
117+
let native = self.0.clone();
118+
let native_fields = native.to_fields().map_err(|e| e.to_string())?;
119+
Ok(js_array_from_fields!(&native_fields))
120+
}
121+
64122
/// Get a signature from a string representation of a signature
65123
///
66124
/// @param {string} signature String representation of a signature
@@ -76,6 +134,12 @@ impl Signature {
76134
pub fn to_string(&self) -> String {
77135
self.0.to_string()
78136
}
137+
138+
/// Get the plaintext representation of the signature.
139+
#[wasm_bindgen(js_name = "toPlaintext")]
140+
pub fn to_plaintext(&self) -> Plaintext {
141+
Plaintext::from(LiteralNative::Signature(Box::new(self.0)))
142+
}
79143
}
80144

81145
impl Deref for Signature {

0 commit comments

Comments
 (0)