From c13b54aff6a951e233d338f04c3b5bb015644044 Mon Sep 17 00:00:00 2001 From: Mike Turner Date: Thu, 5 Dec 2024 14:44:58 -0600 Subject: [PATCH] Use object macro for object formation and iterators for array formation --- wasm/src/ledger/transaction.rs | 229 +++++++------------- wasm/src/ledger/transition.rs | 61 +++--- wasm/src/lib.rs | 8 +- wasm/src/programs/data/helpers/future.rs | 37 ++-- wasm/src/programs/data/helpers/input.rs | 26 ++- wasm/src/programs/data/helpers/literal.rs | 5 +- wasm/src/programs/data/helpers/output.rs | 13 +- wasm/src/programs/data/helpers/plaintext.rs | 16 +- wasm/src/programs/data/helpers/record.rs | 17 +- wasm/src/programs/data/plaintext.rs | 4 +- website/src/workers/worker.js | 2 - 11 files changed, 162 insertions(+), 256 deletions(-) diff --git a/wasm/src/ledger/transaction.rs b/wasm/src/ledger/transaction.rs index cbf571c18..fefb07d5b 100644 --- a/wasm/src/ledger/transaction.rs +++ b/wasm/src/ledger/transaction.rs @@ -16,6 +16,7 @@ use crate::{ input_to_js_value, + object, output_to_js_value, types::native::{FromBytes, ToBytes, TransactionNative, U64Native}, Field, @@ -77,9 +78,7 @@ impl Transaction { #[wasm_bindgen(js_name = toBytesLe)] pub fn to_bytes_le(&self) -> Result { let bytes = self.0.to_bytes_le().map_err(|e| e.to_string())?; - let array = Uint8Array::new_with_length(bytes.len() as u32); - array.copy_from(bytes.as_slice()); - Ok(array) + Ok(Uint8Array::from(bytes.as_slice())) } /// Returns true if the transaction contains the given serial number. @@ -145,30 +144,33 @@ impl Transaction { /// /// @returns {Array} Array of record plaintext objects pub fn owned_records(&self, view_key: &ViewKey) -> Array { - let array = Array::new(); - self.0.records().for_each(|(_, record_ciphertext)| { - if let Ok(record_plaintext) = record_ciphertext.decrypt(view_key) { - let record_ciphertext = RecordPlaintext::from(record_plaintext); - array.push(&JsValue::from(record_ciphertext)); - } - }); - array + self.0 + .records() + .filter_map(|(_, record_ciphertext)| { + if let Ok(record_plaintext) = record_ciphertext.decrypt(view_key) { + Some(JsValue::from(RecordPlaintext::from(record_plaintext))) + } else { + None + } + }) + .collect::() } /// Get the records present in a transaction and their commitments. /// /// @returns {Array<{commitment: Field, record: RecordCiphertext}>} Array of record ciphertext objects pub fn records(&self) -> Array { - let array = Array::new(); - self.0.records().for_each(|(commitment, record_ciphertext)| { - let object = Object::new(); - let commitment = Field::from(commitment); - let record_ciphertext = RecordCiphertext::from(record_ciphertext); - Reflect::set(&object, &JsValue::from_str("commitment"), &JsValue::from(commitment)).unwrap(); - Reflect::set(&object, &JsValue::from_str("record"), &JsValue::from(record_ciphertext)).unwrap(); - array.push(&object); - }); - array + self.0 + .records() + .map(|(commitment, record_ciphertext)| { + let commitment = Field::from(commitment); + let record_ciphertext = RecordCiphertext::from(record_ciphertext); + object! { + "commitment" : commitment, + "record" : record_ciphertext, + } + }) + .collect::() } /// Get a summary of the transaction within a javascript object. @@ -183,139 +185,62 @@ impl Transaction { /// if false the inputs and outputs will be in wasm format. /// /// @returns {Object} Transaction summary - pub fn summary(&self, convert_to_js: bool) -> Result { - // Create a transaction object. - let transaction = Object::new(); - let fee = *self.0.fee_amount().unwrap_or(U64Native::new(0)); - let base_fee = *self.0.base_fee_amount().unwrap_or(U64Native::new(0)); - let priority_fee = *self.0.priority_fee_amount().unwrap_or(U64Native::new(0)); - Reflect::set( - &transaction, - &JsValue::from_str("transactionId"), - &JsValue::from_str(&self.transaction_id().to_string()), - ) - .unwrap(); - Reflect::set( - &transaction, - &JsValue::from_str("transactionType"), - &JsValue::from_str(&self.transaction_type().to_string()), - ) - .unwrap(); - Reflect::set(&transaction, &JsValue::from_str("baseFee"), &JsValue::from(base_fee)).unwrap(); - Reflect::set(&transaction, &JsValue::from_str("fee"), &JsValue::from(fee)).unwrap(); - Reflect::set(&transaction, &JsValue::from_str("priorityFee"), &JsValue::from(priority_fee)).unwrap(); - + pub fn summary(&self, convert_to_js: bool) -> Object { // If the transaction is an execution, create an array of transitions. - if self.0.is_execute() { - let transitions = Array::new(); - for transition in self.0.transitions() { - let transition_object = Object::new(); - Reflect::set( - &transition_object, - &JsValue::from_str("programId"), - &JsValue::from_str(&transition.program_id().to_string()), - ) - .unwrap(); - Reflect::set( - &transition_object, - &JsValue::from_str("functionName"), - &JsValue::from_str(&transition.function_name().to_string()), - ) - .unwrap(); - Reflect::set( - &transition_object, - &JsValue::from_str("transitionId"), - &JsValue::from_str(&transition.id().to_string()), - ) - .unwrap(); - - let tpk = Group::from(transition.tpk()); - let tcm = Field::from(transition.tcm()); - let scm = Field::from(transition.scm()); - if convert_to_js { - Reflect::set(&transition_object, &JsValue::from_str("tpk"), &JsValue::from_str(&tpk.to_string())) - .unwrap(); - Reflect::set(&transition_object, &JsValue::from_str("tcm"), &JsValue::from_str(&tcm.to_string())) - .unwrap(); - Reflect::set(&transition_object, &JsValue::from_str("scm"), &JsValue::from_str(&scm.to_string())) - .unwrap(); - } else { - Reflect::set(&transition_object, &JsValue::from_str("tpk"), &JsValue::from(tpk)).unwrap(); - Reflect::set(&transition_object, &JsValue::from_str("tcm"), &JsValue::from(tcm)).unwrap(); - Reflect::set(&transition_object, &JsValue::from_str("scm"), &JsValue::from(scm)).unwrap(); + let transitions = if self.0.is_execute() { + let transitions = self.0.transitions().map(|transition| { + // Collect the inputs and outputs. + let inputs = transition.inputs().iter().map(|input| { + input_to_js_value(input, convert_to_js) + }).collect::(); + let outputs = transition.outputs().iter().map(|output| { + output_to_js_value(output, convert_to_js) + }).collect::(); + object! { + "programId" : transition.program_id().to_string(), + "functionName" : transition.function_name().to_string(), + "transitionID" : transition.id().to_string(), + "inputs" : inputs, + "outputs" : outputs, + "tpk" : if convert_to_js { JsValue::from_str(&transition.tpk().to_string()) } else { JsValue::from(Group::from(transition.tpk())) }, + "tcm" : if convert_to_js { JsValue::from_str(&transition.tcm().to_string()) } else { JsValue::from(Field::from(transition.tcm())) }, + "scm" : if convert_to_js { JsValue::from_str(&transition.scm().to_string()) } else { JsValue::from(Field::from(transition.scm())) }, } - - // Get the inputs. - let inputs = Array::new(); - for input in transition.inputs() { - inputs.push(&input_to_js_value(input, convert_to_js)); - } - Reflect::set(&transition_object, &JsValue::from_str("inputs"), &inputs).unwrap(); - - // Get the outputs. - let outputs = Array::new(); - for output in transition.outputs() { - outputs.push(&output_to_js_value(output, convert_to_js)?); - } - Reflect::set(&transition_object, &JsValue::from_str("outputs"), &outputs).unwrap(); - transitions.push(&transition_object); - } - Reflect::set(&transaction, &JsValue::from_str("transitions"), &transitions).unwrap(); - } + }).collect::(); + JsValue::from(transitions) + } else { + JsValue::NULL + }; // If the transaction is a deployment, summarize the deployment. - if let Some(deployment) = self.0.deployment() { - let deployment_object = Object::new(); - Reflect::set( - &deployment_object, - &JsValue::from_str("programId"), - &JsValue::from_str(&deployment.program_id().to_string()), - ) - .unwrap(); - let functions = Array::new(); - for (function_name, (verifying_key, _)) in deployment.verifying_keys() { - let function = Object::new(); - Reflect::set( - &function, - &JsValue::from_str("functionName"), - &JsValue::from_str(&function_name.to_string()), - ) - .unwrap(); - if convert_to_js { - Reflect::set( - &function, - &JsValue::from_str("verifyingKey"), - &JsValue::from_str(&verifying_key.to_string()), - ) - .unwrap(); - } else { - Reflect::set( - &function, - &JsValue::from_str("verifyingKey"), - &JsValue::from(VerifyingKey::from(verifying_key)), - ) - .unwrap(); + let deployment = if let Some(deployment) = self.0.deployment() { + let functions = deployment.verifying_keys().iter().map(|(function_name, (verifying_key, _))| { + // Create the initial function object. + object! { + "functionName" : function_name.to_string(), + "constraints" : verifying_key.circuit_info.num_constraints as u32, + "variables" : verifying_key.num_variables() as u32, + "verifyingKey": if convert_to_js { JsValue::from_str(&verifying_key.to_string()) } else { JsValue::from(VerifyingKey::from(verifying_key)) }, } - Reflect::set( - &function, - &JsValue::from_str("constraints"), - &JsValue::from(verifying_key.circuit_info.num_constraints as u32), - ) - .unwrap(); - Reflect::set( - &function, - &JsValue::from_str("variables"), - &JsValue::from(verifying_key.num_variables() as u32), - ) - .unwrap(); - functions.push(&function); - } - Reflect::set(&deployment_object, &JsValue::from_str("functions"), &functions).unwrap(); - Reflect::set(&transaction, &JsValue::from_str("deployment"), &deployment_object).unwrap(); + }).collect::(); + // Create the deployment object. + JsValue::from(object! { + "programId" : deployment.program_id().to_string(), + "functions" : functions, + }) + } else { + JsValue::NULL + }; + + object! { + "transactionId" : self.transaction_id().to_string(), + "transactionType" : self.transaction_type().to_string(), + "fee" : *self.0.fee_amount().unwrap_or(U64Native::new(0)), + "baseFee" : *self.0.base_fee_amount().unwrap_or(U64Native::new(0)), + "priorityFee" : *self.0.priority_fee_amount().unwrap_or(U64Native::new(0)), + "transitions" : transitions, + "deployment" : deployment, } - - // Return the transaction summary. - Ok(transaction) } /// Get the id of the transaction. This is the merkle root of the transaction's inclusion proof. @@ -346,11 +271,7 @@ impl Transaction { /// Get the transitions in a transaction. pub fn transitions(&self) -> Array { - let transitions = Array::new(); - self.0.transitions().for_each(|transition| { - transitions.push(&JsValue::from(Transition::from(transition))); - }); - transitions + self.0.transitions().map(|transition| JsValue::from(Transition::from(transition))).collect::() } } @@ -450,7 +371,7 @@ mod tests { #[wasm_bindgen_test] fn test_transaction_summary_provides_expected_values() { let transaction = Transaction::from_string(TRANSACTION_STRING).unwrap(); - let summary = transaction.summary(true).unwrap(); + let summary = transaction.summary(true); let transaction_id = Reflect::get(&summary, &JsValue::from_str("transactionId")).unwrap().as_string().unwrap(); assert_eq!(transaction_id, TRANSACTION_ID); let transaction_type = diff --git a/wasm/src/ledger/transition.rs b/wasm/src/ledger/transition.rs index 2a2ff12ba..4f1884f59 100644 --- a/wasm/src/ledger/transition.rs +++ b/wasm/src/ledger/transition.rs @@ -16,6 +16,7 @@ use crate::{ input_to_js_value, + object, output_to_js_value, types::native::{FromBytes, ToBytes, TransitionNative}, Field, @@ -25,7 +26,7 @@ use crate::{ ViewKey, }; -use js_sys::{Array, Object, Reflect, Uint8Array}; +use js_sys::{Array, Reflect, Uint8Array}; use std::{ops::Deref, str::FromStr}; use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; @@ -77,9 +78,7 @@ impl Transition { #[wasm_bindgen(js_name = toBytesLe)] pub fn to_bytes_le(&self) -> Result { let bytes = self.0.to_bytes_le().map_err(|e| e.to_string())?; - let array = Uint8Array::new_with_length(bytes.len() as u32); - array.copy_from(bytes.as_slice()); - Ok(array) + Ok(Uint8Array::from(bytes.as_slice())) } #[wasm_bindgen(js_name = programId)] @@ -121,30 +120,31 @@ impl Transition { /// /// @returns {Array} Array of record plaintext objects pub fn owned_records(&self, view_key: &ViewKey) -> Array { - let array = Array::new(); - self.0.records().for_each(|(_, record_ciphertext)| { - if let Ok(record_plaintext) = record_ciphertext.decrypt(view_key) { - let record_ciphertext = RecordPlaintext::from(record_plaintext); - array.push(&JsValue::from(record_ciphertext)); - } - }); - array + self.0 + .records() + .filter_map(|(_, record_ciphertext)| { + if let Ok(record_plaintext) = record_ciphertext.decrypt(view_key) { + Some(JsValue::from(RecordPlaintext::from(record_plaintext))) + } else { + None + } + }) + .collect::() } /// Get the records present in a transition and their commitments. /// /// @returns {Array<{commitment: Field, record: RecordCiphertext}>} Array of record ciphertext objects pub fn records(&self) -> Array { - let array = Array::new(); - self.0.records().for_each(|(commitment, record_ciphertext)| { - let object = Object::new(); - let commitment = Field::from(commitment); - let record_ciphertext = RecordCiphertext::from(record_ciphertext); - Reflect::set(&object, &JsValue::from_str("commitment"), &JsValue::from(commitment)).unwrap(); - Reflect::set(&object, &JsValue::from_str("record"), &JsValue::from(record_ciphertext)).unwrap(); - array.push(&object); - }); - array + self.0 + .records() + .map(|(commitment, record_ciphertext)| { + object! { + "commitment" : Field::from(commitment), + "record" : RecordCiphertext::from(record_ciphertext), + } + }) + .collect::() } /// Get the inputs of the transition. @@ -154,11 +154,7 @@ impl Transition { /// /// @returns {Array} Array of inputs pub fn inputs(&self, convert_to_js: bool) -> Array { - let js_array = Array::new(); - for input in self.0.inputs() { - js_array.push(&input_to_js_value(input, convert_to_js)); - } - js_array + self.0.inputs().iter().map(|input| input_to_js_value(input, convert_to_js)).collect::() } /// Get the outputs of the transition. @@ -167,12 +163,8 @@ impl Transition { /// the outputs will be in wasm format. /// /// @returns {Array} Array of outputs - pub fn outputs(&self, convert_to_js: bool) -> Result { - let js_array = Array::new(); - for output in self.0.outputs() { - js_array.push(&output_to_js_value(output, convert_to_js)?); - } - Ok(js_array) + pub fn outputs(&self, convert_to_js: bool) -> Array { + self.0.outputs().iter().map(|output| output_to_js_value(output, convert_to_js)).collect::() } /// Get the transition public key of the transition. @@ -235,6 +227,7 @@ impl FromStr for Transition { mod tests { use super::*; use crate::PrivateKey; + use js_sys::Object; use wasm_bindgen_test::wasm_bindgen_test; @@ -340,7 +333,7 @@ mod tests { #[wasm_bindgen_test] fn test_output_correctness() { let transition = Transition::from_string(TRANSITION).unwrap(); - let outputs = transition.outputs(true).unwrap(); + let outputs = transition.outputs(true); let output_1 = Object::from(outputs.get(0)); let output_2 = Object::from(outputs.get(1)); diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 1ed8a2e79..32121a1be 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -202,9 +202,9 @@ extern "C" { #[macro_export] macro_rules! array { ($($value:expr),*$(,)?) => {{ - let array = Array::new(); + let array = ::js_sys::Array::new(); - $(array.push(&JsValue::from($value));)* + $(array.push(&::wasm_bindgen::JsValue::from($value));)* array }}; @@ -213,9 +213,9 @@ macro_rules! array { #[macro_export] macro_rules! object { ($($key:literal: $value:expr,)*) => {{ - let object = Object::new(); + let object = ::js_sys::Object::new(); - $(Reflect::set(&object, &JsValue::from_str($key), &JsValue::from($value)).unwrap();)* + $(Reflect::set(&object, &::wasm_bindgen::JsValue::from_str($key), &::wasm_bindgen::JsValue::from($value)).unwrap();)* object }}; diff --git a/wasm/src/programs/data/helpers/future.rs b/wasm/src/programs/data/helpers/future.rs index 8015f4410..6cd35dd2e 100644 --- a/wasm/src/programs/data/helpers/future.rs +++ b/wasm/src/programs/data/helpers/future.rs @@ -15,41 +15,36 @@ // along with the Aleo SDK library. If not, see . use crate::{ + object, plaintext_to_js_value, types::native::{ArgumentNative, FutureNative}, Plaintext, }; -use js_sys::{Array, JsString, Object, Reflect}; +use js_sys::{Array, JsString, Reflect}; use wasm_bindgen::JsValue; /// Convert a future to a javascript value. pub fn future_to_js_value(argument: &FutureNative, convert_to_js: bool) -> JsValue { - let future_object = Object::new(); - Reflect::set(&future_object, &JsString::from("type"), &JsString::from("future")).unwrap(); - Reflect::set(&future_object, &JsString::from("programId"), &JsString::from(argument.program_id().to_string())) - .unwrap(); - Reflect::set( - &future_object, - &JsString::from("functionName"), - &JsString::from(argument.function_name().to_string()), - ) - .unwrap(); - let arguments = Array::new(); - for argument in argument.arguments() { - match argument { + let future_object = object! { + "type" : "future", + "programId" : argument.program_id().to_string(), + "functionName" : argument.function_name().to_string(), + }; + let arguments = argument + .arguments() + .iter() + .map(|argument| match argument { ArgumentNative::Plaintext(plaintext) => { if convert_to_js { - arguments.push(&plaintext_to_js_value(plaintext)); + plaintext_to_js_value(plaintext) } else { - arguments.push(&JsValue::from(Plaintext::from(plaintext))); + JsValue::from(Plaintext::from(plaintext)) } } - ArgumentNative::Future(future) => { - arguments.push(&future_to_js_value(future, convert_to_js)); - } - } - } + ArgumentNative::Future(future) => future_to_js_value(future, convert_to_js), + }) + .collect::(); Reflect::set(&future_object, &JsString::from("arguments"), &JsValue::from(&arguments)).unwrap(); JsValue::from(future_object) } diff --git a/wasm/src/programs/data/helpers/input.rs b/wasm/src/programs/data/helpers/input.rs index 63ecab307..2f928b559 100644 --- a/wasm/src/programs/data/helpers/input.rs +++ b/wasm/src/programs/data/helpers/input.rs @@ -14,9 +14,9 @@ // You should have received a copy of the GNU General Public License // along with the Aleo SDK library. If not, see . -use crate::{plaintext_to_js_value, types::native::InputNative, Ciphertext, Field, Plaintext}; +use crate::{object, plaintext_to_js_value, types::native::InputNative, Ciphertext, Field, Plaintext}; -use js_sys::{JsString, Object, Reflect}; +use js_sys::{JsString, Reflect}; use wasm_bindgen::JsValue; pub fn input_to_js_value(input: &InputNative, convert_to_js: bool) -> JsValue { @@ -47,8 +47,9 @@ pub fn input_to_js_value(input: &InputNative, convert_to_js: bool) -> JsValue { } } InputNative::Record(serial_number, tag) => { - let record = Object::new(); - Reflect::set(&record, &JsString::from("type"), &JsValue::from_str("record")).unwrap(); + let record = object! { + "type": "record", + }; if convert_to_js { Reflect::set(&record, &JsString::from("serialNumber"), &JsValue::from(serial_number.to_string())) .unwrap(); @@ -61,20 +62,25 @@ pub fn input_to_js_value(input: &InputNative, convert_to_js: bool) -> JsValue { JsValue::from(record) } InputNative::ExternalRecord(input_commitment) => { - let record = Object::new(); - Reflect::set(&record, &JsString::from("type"), &JsValue::from_str("externalRecord")).unwrap(); + let external_record = object! { + "type": "extneralRecord", + }; if convert_to_js { - Reflect::set(&record, &JsString::from("inputCommitment"), &JsValue::from(input_commitment.to_string())) - .unwrap(); + Reflect::set( + &external_record, + &JsString::from("inputCommitment"), + &JsValue::from(input_commitment.to_string()), + ) + .unwrap(); } else { Reflect::set( - &record, + &external_record, &JsString::from("inputCommitment"), &JsValue::from(Field::from(input_commitment)), ) .unwrap(); } - JsValue::from(record) + JsValue::from(external_record) } } } diff --git a/wasm/src/programs/data/helpers/literal.rs b/wasm/src/programs/data/helpers/literal.rs index 5ab9308b2..fea061a05 100644 --- a/wasm/src/programs/data/helpers/literal.rs +++ b/wasm/src/programs/data/helpers/literal.rs @@ -85,9 +85,6 @@ pub fn literal_to_js_value(literal: &LiteralNative) -> JsValue { let js_string = literal.to_string(); (&js_string).into() } - LiteralNative::String(literal) => { - let js_string = literal.to_string(); - (&js_string).into() - } + LiteralNative::String(literal) => (&**literal).into(), } } diff --git a/wasm/src/programs/data/helpers/output.rs b/wasm/src/programs/data/helpers/output.rs index 243878470..385cdd3cd 100644 --- a/wasm/src/programs/data/helpers/output.rs +++ b/wasm/src/programs/data/helpers/output.rs @@ -16,6 +16,7 @@ use crate::{ future_to_js_value, + object, plaintext_to_js_value, types::native::OutputNative, Ciphertext, @@ -27,7 +28,7 @@ use crate::{ use js_sys::{JsString, Object, Reflect}; use wasm_bindgen::JsValue; -pub fn output_to_js_value(output: &OutputNative, convert_to_js: bool) -> Result { +pub fn output_to_js_value(output: &OutputNative, convert_to_js: bool) -> JsValue { let js_value = match output { OutputNative::Constant(_, plaintext) => { if let Some(plaintext) = plaintext { @@ -55,8 +56,12 @@ pub fn output_to_js_value(output: &OutputNative, convert_to_js: bool) -> Result< } } OutputNative::Record(commitment, checksum, record_ciphertext) => { - let record = Object::new(); - Reflect::set(&record, &JsString::from("type"), &JsString::from("record")).unwrap(); + // Create a record object. + let record = object! { + "type": "record", + }; + + // Create a record object. if convert_to_js { Reflect::set(&record, &JsString::from("commitment"), &JsValue::from(commitment.to_string())).unwrap(); Reflect::set(&record, &JsString::from("checksum"), &JsValue::from(checksum.to_string())).unwrap(); @@ -110,5 +115,5 @@ pub fn output_to_js_value(output: &OutputNative, convert_to_js: bool) -> Result< } } }; - Ok(js_value) + js_value } diff --git a/wasm/src/programs/data/helpers/plaintext.rs b/wasm/src/programs/data/helpers/plaintext.rs index ae001c72b..27ecf9f45 100644 --- a/wasm/src/programs/data/helpers/plaintext.rs +++ b/wasm/src/programs/data/helpers/plaintext.rs @@ -20,7 +20,7 @@ use crate::{ }; use indexmap::IndexMap; -use js_sys::{Object, Reflect}; +use js_sys::{Array, Object, Reflect}; use wasm_bindgen::JsValue; /// Insert a plaintext value into a javascript object. @@ -34,11 +34,8 @@ pub fn insert_plaintext(js_object: &Object, key: &IdentifierNative, plaintext: & let struct_object = struct_to_js_object(struct_members); Reflect::set(&js_object, &key.to_string().into(), &struct_object.into()).unwrap(); } - PlaintextNative::Array(plaintext, ..) => { - let js_array = js_sys::Array::new(); - for value in plaintext.iter() { - js_array.push(&plaintext_to_js_value(value)); - } + PlaintextNative::Array(array, ..) => { + let js_array = array.iter().map(|plaintext| plaintext_to_js_value(plaintext)).collect::(); Reflect::set(&js_object, &key.to_string().into(), &js_array.into()).unwrap(); } } @@ -49,11 +46,8 @@ pub fn plaintext_to_js_value(plaintext: &PlaintextNative) -> JsValue { match plaintext { PlaintextNative::Literal(literal, _) => literal_to_js_value(literal), PlaintextNative::Struct(struct_members, ..) => JsValue::from(struct_to_js_object(struct_members)), - PlaintextNative::Array(plaintext, ..) => { - let js_array = js_sys::Array::new(); - for value in plaintext.iter() { - js_array.push(&plaintext_to_js_value(value)); - } + PlaintextNative::Array(array, ..) => { + let js_array = array.iter().map(|plaintext| plaintext_to_js_value(plaintext)).collect::(); JsValue::from(js_array) } } diff --git a/wasm/src/programs/data/helpers/record.rs b/wasm/src/programs/data/helpers/record.rs index 8ec2dc564..0a39dc47a 100644 --- a/wasm/src/programs/data/helpers/record.rs +++ b/wasm/src/programs/data/helpers/record.rs @@ -16,18 +16,21 @@ use crate::{ insert_plaintext, + object, types::native::{EntryNative, RecordPlaintextNative}, }; use js_sys::{Object, Reflect}; /// Convert a record to a javascript object. pub fn record_to_js_object(record: &RecordPlaintextNative) -> Result { - // Create a new javascript object to house the record data. - let js_record = Object::new(); - - // Insert the owner into the javascript object. + let nonce = record.nonce().to_string(); let owner = record.owner().to_string(); - Reflect::set(&js_record, &"owner".into(), &owner.into()).unwrap(); + + // Insert the owner and nonce. + let js_record = object! { + "owner": &owner, + "_nonce": &nonce, + }; // Get the metadata from the record and insert it into the javascript object. record.data().iter().for_each(|(key, value)| { @@ -44,10 +47,6 @@ pub fn record_to_js_object(record: &RecordPlaintextNative) -> Result Result { let rust_bytes = self.0.to_bytes_le().map_err(|e| e.to_string())?; - let array = Uint8Array::new_with_length(rust_bytes.len() as u32); - array.copy_from(rust_bytes.as_slice()); - Ok(array) + Ok(Uint8Array::from(rust_bytes.as_slice())) } /// Returns the string representation of the plaintext. diff --git a/website/src/workers/worker.js b/website/src/workers/worker.js index 5c1f2259f..49cc380b8 100644 --- a/website/src/workers/worker.js +++ b/website/src/workers/worker.js @@ -418,7 +418,5 @@ self.addEventListener("message", (ev) => { programManager.setHost(defaultHost); } })(); - } else if (ev.data.type === "PROPOSE_GAME") { - } });