whisky_pallas/wrapper/transaction_body/
anchor.rs1use std::str::FromStr;
2
3use pallas::{
4 crypto::hash::Hash,
5 ledger::primitives::{conway::Anchor as PallasAnchor, Fragment},
6};
7use whisky_common::WError;
8
9#[derive(Debug, PartialEq, Eq, Clone)]
10pub struct Anchor {
11 pub inner: PallasAnchor,
12}
13
14impl Anchor {
15 pub fn new(url: String, content_hash: String) -> Result<Self, WError> {
16 let inner = PallasAnchor {
17 url,
18 content_hash: Hash::from_str(&content_hash)
19 .map_err(|e| WError::new("Anchor - Invalid content hash", &e.to_string()))?,
20 };
21 Ok(Self { inner })
22 }
23
24 pub fn to_whisky_anchor(&self) -> whisky_common::Anchor {
25 whisky_common::Anchor {
26 anchor_url: self.inner.url.clone(),
27 anchor_data_hash: self.inner.content_hash.to_string(),
28 }
29 }
30
31 pub fn encode(&self) -> Result<String, WError> {
32 let encoded_fragment = self
33 .inner
34 .encode_fragment()
35 .map_err(|e| WError::new("Anchor - Fragment encode error", &e.to_string()))?;
36 Ok(hex::encode(encoded_fragment))
37 }
38
39 pub fn decode_bytes(bytes: &[u8]) -> Result<Self, WError> {
40 let inner = PallasAnchor::decode_fragment(&bytes)
41 .map_err(|e| WError::new("Anchor - Fragment decode error", &e.to_string()))?;
42 Ok(Self { inner })
43 }
44}