diff --git a/packages/rs-dpp/src/errors/consensus/consensus_error.rs b/packages/rs-dpp/src/errors/consensus/consensus_error.rs index 01aa15699b..21651aca87 100644 --- a/packages/rs-dpp/src/errors/consensus/consensus_error.rs +++ b/packages/rs-dpp/src/errors/consensus/consensus_error.rs @@ -53,10 +53,8 @@ impl ConsensusError { .reject_trailing_bytes() .with_big_endian() .serialize(self) - .map_err(|_| { - ProtocolError::EncodingError(String::from( - "unable to serialize identity public key", - )) + .map_err(|e| { + ProtocolError::EncodingError(format!("unable to serialize consensus error: {e}")) }) } @@ -67,7 +65,7 @@ impl ConsensusError { .with_big_endian() .deserialize(bytes) .map_err(|e| { - ProtocolError::EncodingError(format!("unable to deserialize consensus error {}", e)) + ProtocolError::EncodingError(format!("unable to deserialize consensus error: {e}")) }) } } diff --git a/packages/rs-dpp/src/identity/identity.rs b/packages/rs-dpp/src/identity/identity.rs index 442237f5bd..d2963950d8 100644 --- a/packages/rs-dpp/src/identity/identity.rs +++ b/packages/rs-dpp/src/identity/identity.rs @@ -334,12 +334,6 @@ impl Identity { raw_object.try_into() } - /// Creates an identity from a json object - pub fn from_json_object(raw_object: JsonValue) -> Result { - let value: Value = raw_object.into(); - value.try_into() - } - /// Computes the hash of an identity pub fn hash(&self) -> Result, ProtocolError> { Ok(hash::hash(self.to_buffer()?)) diff --git a/packages/rs-platform-value/src/types/identifier.rs b/packages/rs-platform-value/src/types/identifier.rs index 585bfa5051..564eeaf4d1 100644 --- a/packages/rs-platform-value/src/types/identifier.rs +++ b/packages/rs-platform-value/src/types/identifier.rs @@ -67,13 +67,30 @@ impl<'de> Deserialize<'de> for IdentifierBytes32 { deserializer.deserialize_string(StringVisitor) } else { - let value = Value::deserialize(deserializer)?; + struct BytesVisitor; - Ok(IdentifierBytes32( - value - .into_hash256() - .map_err(|_| D::Error::custom("hello"))?, - )) + impl<'de> Visitor<'de> for BytesVisitor { + type Value = IdentifierBytes32; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("a byte array with length 32") + } + + fn visit_bytes(self, v: &[u8]) -> Result + where + E: serde::de::Error, + { + if v.len() != 32 { + return Err(E::invalid_length(v.len(), &self)); + } + let mut array = [0u8; 32]; + array.copy_from_slice(&v); + + Ok(IdentifierBytes32(array)) + } + } + + deserializer.deserialize_bytes(BytesVisitor) } } } diff --git a/packages/wasm-dpp/src/errors/consensus/basic/decode/protocol_version_parsing_error.rs b/packages/wasm-dpp/src/errors/consensus/basic/decode/protocol_version_parsing_error.rs index f84b5c32e0..cbca00edb4 100644 --- a/packages/wasm-dpp/src/errors/consensus/basic/decode/protocol_version_parsing_error.rs +++ b/packages/wasm-dpp/src/errors/consensus/basic/decode/protocol_version_parsing_error.rs @@ -17,6 +17,13 @@ impl From<&ProtocolVersionParsingError> for ProtocolVersionParsingErrorWasm { #[wasm_bindgen(js_class=ProtocolVersionParsingError)] impl ProtocolVersionParsingErrorWasm { + #[wasm_bindgen(constructor)] + pub fn new(parsing_error: String) -> Self { + Self { + inner: ProtocolVersionParsingError::new(parsing_error), + } + } + #[wasm_bindgen(js_name = getParsingError)] pub fn get_parsing_error(&self) -> String { self.inner.parsing_error().to_string() diff --git a/packages/wasm-dpp/src/errors/consensus/fee/balance_is_not_enough_error.rs b/packages/wasm-dpp/src/errors/consensus/fee/balance_is_not_enough_error.rs index bfe21ea751..ad236f8055 100644 --- a/packages/wasm-dpp/src/errors/consensus/fee/balance_is_not_enough_error.rs +++ b/packages/wasm-dpp/src/errors/consensus/fee/balance_is_not_enough_error.rs @@ -18,6 +18,13 @@ impl From<&BalanceIsNotEnoughError> for BalanceIsNotEnoughErrorWasm { #[wasm_bindgen(js_class=BalanceIsNotEnoughError)] impl BalanceIsNotEnoughErrorWasm { + #[wasm_bindgen(constructor)] + pub fn new(balance: Credits, fee: Credits) -> Self { + Self { + inner: BalanceIsNotEnoughError::new(balance, fee), + } + } + #[wasm_bindgen(js_name=getBalance)] pub fn get_balance(&self) -> Credits { self.inner.balance() diff --git a/packages/wasm-dpp/src/errors/consensus/signature/identity_not_found_error.rs b/packages/wasm-dpp/src/errors/consensus/signature/identity_not_found_error.rs index e7d7f35642..02a4822063 100644 --- a/packages/wasm-dpp/src/errors/consensus/signature/identity_not_found_error.rs +++ b/packages/wasm-dpp/src/errors/consensus/signature/identity_not_found_error.rs @@ -4,6 +4,7 @@ use dpp::consensus::ConsensusError; use wasm_bindgen::prelude::*; use crate::buffer::Buffer; +use crate::identifier::IdentifierWrapper; #[wasm_bindgen(js_name=IdentityNotFoundError)] pub struct IdentityNotFoundErrorWasm { @@ -18,9 +19,16 @@ impl From<&IdentityNotFoundError> for IdentityNotFoundErrorWasm { #[wasm_bindgen(js_class=IdentityNotFoundError)] impl IdentityNotFoundErrorWasm { + #[wasm_bindgen(constructor)] + pub fn new(identity_id: IdentifierWrapper) -> Self { + Self { + inner: IdentityNotFoundError::new(identity_id.into()), + } + } + #[wasm_bindgen(js_name=getIdentityId)] - pub fn get_identity_id(&self) -> Buffer { - Buffer::from_bytes(self.inner.identity_id().as_bytes()) + pub fn get_identity_id(&self) -> IdentifierWrapper { + self.inner.identity_id().into() } #[wasm_bindgen(js_name=getCode)] @@ -37,7 +45,7 @@ impl IdentityNotFoundErrorWasm { pub fn serialize(&self) -> Result { let bytes = ConsensusError::from(self.inner.clone()) .serialize() - .map_err(|e| JsError::from(e))?; + .map_err(JsError::from)?; Ok(Buffer::from_bytes(bytes.as_slice())) } diff --git a/packages/wasm-dpp/src/errors/consensus/state/data_contract/data_contract_already_present_error.rs b/packages/wasm-dpp/src/errors/consensus/state/data_contract/data_contract_already_present_error.rs index e0572742db..ef4b7fa0de 100644 --- a/packages/wasm-dpp/src/errors/consensus/state/data_contract/data_contract_already_present_error.rs +++ b/packages/wasm-dpp/src/errors/consensus/state/data_contract/data_contract_already_present_error.rs @@ -1,7 +1,9 @@ use crate::buffer::Buffer; +use crate::identifier::IdentifierWrapper; use dpp::consensus::codes::ErrorWithCode; use dpp::consensus::state::data_contract::data_contract_already_present_error::DataContractAlreadyPresentError; use dpp::consensus::ConsensusError; +use dpp::identifier::Identifier; use wasm_bindgen::prelude::*; #[wasm_bindgen(js_name=DataContractAlreadyPresentError)] @@ -17,9 +19,16 @@ impl From<&DataContractAlreadyPresentError> for DataContractAlreadyPresentErrorW #[wasm_bindgen(js_class=DataContractAlreadyPresentError)] impl DataContractAlreadyPresentErrorWasm { + #[wasm_bindgen(constructor)] + pub fn new(data_contract_id: IdentifierWrapper) -> Self { + Self { + inner: DataContractAlreadyPresentError::new(data_contract_id.into()), + } + } + #[wasm_bindgen(js_name=getDataContractId)] - pub fn data_contract_id(&self) -> Buffer { - Buffer::from_bytes(self.inner.data_contract_id().as_bytes()) + pub fn data_contract_id(&self) -> IdentifierWrapper { + self.inner.data_contract_id().to_owned().into() } #[wasm_bindgen(js_name=getCode)] diff --git a/packages/wasm-dpp/src/identity/mod.rs b/packages/wasm-dpp/src/identity/mod.rs index 0dd9b77d45..1230c689eb 100644 --- a/packages/wasm-dpp/src/identity/mod.rs +++ b/packages/wasm-dpp/src/identity/mod.rs @@ -50,7 +50,7 @@ impl IdentityWasm { let raw_identity: Value = serde_json::from_str(&identity_json).map_err(|e| e.to_string())?; - let identity = Identity::from_json_object(raw_identity).unwrap(); + let identity = Identity::from_json(raw_identity).unwrap(); Ok(IdentityWasm(identity)) } diff --git a/packages/wasm-dpp/test/integration/error/consensus/deserializeConsensusError.spec.js b/packages/wasm-dpp/test/integration/error/consensus/deserializeConsensusError.spec.js index 8e58f571ec..338b0af120 100644 --- a/packages/wasm-dpp/test/integration/error/consensus/deserializeConsensusError.spec.js +++ b/packages/wasm-dpp/test/integration/error/consensus/deserializeConsensusError.spec.js @@ -2,7 +2,6 @@ const { default: loadWasmDpp } = require('../../../..'); let { deserializeConsensusError, - decodeProtocolEntity, ProtocolVersionParsingError, } = require('../../../..'); @@ -10,25 +9,23 @@ describe('deserializeConsensusError', () => { before(async () => { ({ deserializeConsensusError, - decodeProtocolEntity, ProtocolVersionParsingError, } = await loadWasmDpp()); }); it('should deserialize consensus error', async () => { - try { - await decodeProtocolEntity(Buffer.alloc(0)); - } catch (consensusError) { - expect(consensusError).to.be.instanceOf(ProtocolVersionParsingError); - expect(consensusError.getCode()).to.equals(1000); - expect(consensusError.message).to.equals('Can\'t read protocol version from serialized object: protocol version could not be decoded as a varint'); + const consensusError = new ProtocolVersionParsingError('test'); + const message = 'Can\'t read protocol version from serialized object: test'; - const bytes = consensusError.serialize(); + expect(consensusError).to.be.instanceOf(ProtocolVersionParsingError); + expect(consensusError.getCode()).to.equals(1000); + expect(consensusError.message).to.equals(message); - const recoveredError = deserializeConsensusError(bytes); - expect(recoveredError).to.be.instanceOf(ProtocolVersionParsingError); - expect(recoveredError.getCode()).to.equals(1000); - expect(recoveredError.message).to.equals('Can\'t read protocol version from serialized object: protocol version could not be decoded as a varint'); - } + const serializedConsensusError = consensusError.serialize(); + + const recoveredError = deserializeConsensusError(serializedConsensusError); + expect(recoveredError).to.be.instanceOf(ProtocolVersionParsingError); + expect(recoveredError.getCode()).to.equals(1000); + expect(recoveredError.message).to.equals(message); }); });