From e8b3f76f2158bc4dd42ddd952ac0dca8a6c0ff50 Mon Sep 17 00:00:00 2001 From: Hjort Date: Thu, 10 Feb 2022 09:09:31 +0100 Subject: [PATCH] Fixes based on feedback --- README.md | 4 ++-- rust-src/aux_functions.rs | 14 ++++++++------ src/deserialization.ts | 2 +- test/deserialization.test.ts | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2e1ebf0f7..3c01d50ff 100644 --- a/README.md +++ b/README.md @@ -671,8 +671,8 @@ The following example demonstrates how to deserialize a contract's state: ```js const contractName = "my-contract-name" -const schema = Buffer.from(schema-source); // Load schema from module or file -const rawContractState = Buffer.from(state-source); // Could be getinstanceInfo(...).model +const schema = Buffer.from(schemaSource); // Load schema from file +const rawContractState = Buffer.from(stateSource); // Could be getinstanceInfo(...).model const state = deserializeContractState(contractName, schema, rawContractState); ``` diff --git a/rust-src/aux_functions.rs b/rust-src/aux_functions.rs index 49b0f78b3..12cadef9a 100644 --- a/rust-src/aux_functions.rs +++ b/rust-src/aux_functions.rs @@ -7,7 +7,7 @@ use std::collections::BTreeMap; type ExampleCurve = G1; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use sha2::{Digest, Sha256}; -use concordium_contracts_common::{from_bytes, Cursor, schema::{Module}}; +use concordium_contracts_common::{from_bytes, Cursor, schema}; use hex; use anyhow::{bail, Result, anyhow}; @@ -179,18 +179,20 @@ pub fn get_credential_deployment_info_aux( Ok(cdi_json.to_string()) } +/// Given the bytes of a contract's state, deserialize them to a json object, using the provided schema. +/// Both the state bytes and the schema are given as hex-encoded strings. pub fn deserialize_state_aux( contract_name: &str, state_bytes: String, schema: String, ) -> Result { - let module: Module = match from_bytes(&hex::decode(schema)?) { + let module_schema: schema::Module = match from_bytes(&hex::decode(schema)?) { Ok(o) => o, Err(e) => return Err(anyhow!("unable to parse schema: {:#?}", e)) }; - let mut cursor = Cursor::new(hex::decode(state_bytes)?); - let contract = module.contracts.get(contract_name).ok_or(anyhow!("Unable to get contract"))?; - let state = contract.state.as_ref().ok_or(anyhow!("No state in schema"))?; - Ok(state.to_json(&mut cursor).expect("Unable to parse state to json").to_string()) + let mut state_cursor = Cursor::new(hex::decode(state_bytes)?); + let contract_schema = module_schema.contracts.get(contract_name).ok_or(anyhow!("Unable to get contract schema: not included in module schema"))?; + let state_schema = contract_schema.state.as_ref().ok_or(anyhow!("Unable to get state schema: not included in contract schema"))?; + Ok(state_schema.to_json(&mut state_cursor).expect("Unable to parse state to json").to_string()) } diff --git a/src/deserialization.ts b/src/deserialization.ts index 0d5ae63a2..f738469a0 100644 --- a/src/deserialization.ts +++ b/src/deserialization.ts @@ -21,6 +21,6 @@ export function deserializeContractState( } catch (e) { throw new Error( 'unable to deserialize state, due to: ' + serializedState - ); // In this case the state is the error message from the rust module + ); // In this case serializedState is the error message from the rust module } } diff --git a/test/deserialization.test.ts b/test/deserialization.test.ts index 2b42f1e33..a06dab7d1 100644 --- a/test/deserialization.test.ts +++ b/test/deserialization.test.ts @@ -5,7 +5,7 @@ import * as fs from 'fs'; const client = getNodeClient(); -test('Deserialize state (two-step-transfer)', async () => { +test('Deserialize state with schema from file (two-step-transfer)', async () => { const blockHash = 'fad0981b0424c6e1af746a39667628861481ac225f90decd233980311c2e19cb'; const contractAddress = { index: BigInt(1646), subindex: BigInt(0) };