Skip to main content

whisky_common/data/primitives/
byte_string.rs

1use serde_json::{json, Value};
2
3use crate::{data::PlutusDataJson, WError};
4
5#[derive(Clone, Debug, PartialEq)]
6pub struct ByteString {
7    pub bytes: String,
8}
9
10impl ByteString {
11    pub fn new(bytes: &str) -> Self {
12        ByteString {
13            bytes: bytes.to_string(),
14        }
15    }
16}
17
18impl PlutusDataJson for ByteString {
19    fn to_json(&self) -> Value {
20        byte_string(&self.bytes)
21    }
22
23    fn from_json(value: &Value) -> Result<Self, WError> {
24        let bytes = value
25            .get("bytes")
26            .ok_or_else(|| WError::new("ByteString::from_json", "missing 'bytes' field"))?
27            .as_str()
28            .ok_or_else(|| WError::new("ByteString::from_json", "invalid 'bytes' value"))?;
29        Ok(ByteString {
30            bytes: bytes.to_string(),
31        })
32    }
33}
34
35pub fn byte_string(bytes: &str) -> Value {
36    json!({ "bytes": bytes })
37}
38
39pub fn builtin_byte_string(bytes: &str) -> Value {
40    json!({ "bytes": bytes })
41}
42
43pub type ByteArray = ByteString;
44
45pub type ScriptHash = ByteString;
46
47pub type PolicyId = ByteString;
48
49pub type AssetName = ByteString;
50
51pub type PubKeyHash = ByteString;
52
53pub type VerificationKeyHash = ByteString;