whisky_common/data/
credentials.rs1use serde_json::{json, Value};
2
3use crate::{
4 data::{ByteString, Constr0, Constr1, PlutusDataJson},
5 impl_constr_type,
6};
7
8use super::{byte_string, constr0, constr1};
9
10#[derive(Clone, Debug)]
11pub enum Credential {
12 VerificationKey(VerificationKey),
13 Script(Script),
14}
15
16impl Credential {
17 pub fn new((hash, is_script): (&str, bool)) -> Self {
18 if is_script {
19 Credential::Script(Script::from(hash))
20 } else {
21 Credential::VerificationKey(VerificationKey::from(hash))
22 }
23 }
24}
25
26impl PlutusDataJson for Credential {
27 fn to_json(&self) -> Value {
28 match self {
29 Credential::VerificationKey(vk) => vk.to_json(),
30 Credential::Script(script) => script.to_json(),
31 }
32 }
33}
34
35pub type VerificationKey = Constr0<ByteString>;
36impl_constr_type!(VerificationKey, 0,(pub_key_hash: ByteString, &str));
37
38pub type Script = Constr1<ByteString>;
39impl_constr_type!(Script, 1, (script_hash: ByteString, &str));
40
41#[derive(Clone, Debug)]
42pub struct Address {
43 pub payment_key_hash: String,
44 pub stake_credential: Option<String>,
45 pub is_script_payment_key: bool,
46 pub is_script_stake_key: bool,
47}
48
49impl Address {
50 pub fn new(
51 payment_key_hash: &str,
52 stake_credential: Option<&str>,
53 is_script_payment_key: bool,
54 is_script_stake_key: bool,
55 ) -> Self {
56 Address {
57 payment_key_hash: payment_key_hash.to_string(),
58 stake_credential: stake_credential.map(|s| s.to_string()),
59 is_script_payment_key,
60 is_script_stake_key,
61 }
62 }
63}
64
65impl PlutusDataJson for Address {
66 fn to_json(&self) -> Value {
67 if self.is_script_payment_key {
68 script_address(
69 &self.payment_key_hash,
70 self.stake_credential.as_deref(),
71 self.is_script_stake_key,
72 )
73 } else {
74 pub_key_address(
75 &self.payment_key_hash,
76 self.stake_credential.as_deref(),
77 self.is_script_stake_key,
78 )
79 }
80 }
81
82 fn to_json_string(&self) -> String {
83 self.to_json().to_string()
84 }
85
86 fn to_constr_field(&self) -> Vec<serde_json::Value> {
87 vec![self.to_json()]
88 }
89}
90
91pub fn payment_pub_key_hash(pub_key_hash: &str) -> Value {
92 byte_string(pub_key_hash)
93}
94
95pub fn pub_key_hash(pub_key_hash: &str) -> Value {
96 byte_string(pub_key_hash)
97}
98
99pub fn maybe_staking_hash(stake_credential: &str, is_script_stake_key: bool) -> Value {
100 if stake_credential.is_empty() {
101 constr1(json!([]))
102 } else if is_script_stake_key {
103 constr0(vec![constr0(vec![constr1(vec![byte_string(
104 stake_credential,
105 )])])])
106 } else {
107 constr0(vec![constr0(vec![constr0(vec![byte_string(
108 stake_credential,
109 )])])])
110 }
111}
112
113pub fn pub_key_address(
114 bytes: &str,
115 stake_credential: Option<&str>,
116 is_script_stake_key: bool,
117) -> Value {
118 constr0(vec![
119 constr0(vec![byte_string(bytes)]),
120 maybe_staking_hash(stake_credential.unwrap_or(""), is_script_stake_key),
121 ])
122}
123
124pub fn script_address(
125 bytes: &str,
126 stake_credential: Option<&str>,
127 is_script_stake_key: bool,
128) -> Value {
129 constr0(vec![
130 constr1(vec![byte_string(bytes)]),
131 maybe_staking_hash(stake_credential.unwrap_or(""), is_script_stake_key),
132 ])
133}