Skip to content

Commit dc88313

Browse files
committed
fix hex-encoded string conversion for chain_proposal_id
An issue for extracting the correct bytes from a hex-encoded string is resolved.
1 parent 339b6ca commit dc88313

File tree

1 file changed

+12
-3
lines changed
  • src/catalyst-toolbox/catalyst-toolbox/src/rewards

1 file changed

+12
-3
lines changed

src/catalyst-toolbox/catalyst-toolbox/src/rewards/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ pub type Funds = Decimal;
1010
pub type Rewards = Decimal;
1111
pub type VoteCount = HashMap<Identifier, HashSet<Hash>>;
1212

13+
use chain_impl_mockchain::certificate::ExternalProposalId;
1314
use jormungandr_lib::crypto::{account::Identifier, hash::Hash};
1415
use std::collections::{HashMap, HashSet};
16+
use std::str::FromStr;
1517
use thiserror::Error;
1618
use vit_servicing_station_lib::db::models::proposals::FullProposalInfo;
1719

@@ -36,9 +38,16 @@ impl Threshold {
3638
let proposals = proposals
3739
.into_iter()
3840
.map(|p| {
39-
<[u8; 32]>::try_from(p.proposal.chain_proposal_id)
40-
.map_err(Error::InvalidHash)
41-
.map(|hash| (p.proposal.challenge_id, Hash::from(hash)))
41+
// the chain_proposal_id comes as hex-encoded string digest of a blake2b256 key
42+
// the first step is to decode the &str
43+
let chain_proposal_str = std::str::from_utf8(&p.proposal.chain_proposal_id)
44+
.map_err(|_| Error::InvalidHash(p.proposal.chain_proposal_id.clone()))?;
45+
// second step is to convert &str into a digest so that it can be converted into
46+
// [u8;32]
47+
let chain_proposal_id = ExternalProposalId::from_str(chain_proposal_str)
48+
.map_err(|_| Error::InvalidHash(p.proposal.chain_proposal_id.clone()))?;
49+
let bytes: [u8; 32] = chain_proposal_id.into();
50+
Ok((p.proposal.challenge_id, Hash::from(bytes)))
4251
})
4352
.collect::<Result<Vec<_>, Error>>()?;
4453
Ok(Self {

0 commit comments

Comments
 (0)