radix_transactions/model/hash/
traits.rs1use crate::internal_prelude::*;
2use radix_common::prelude::*;
3
4pub trait IsTransactionHashWithStaticHrp: IsHash {
5 fn static_hrp<'h>(hrp_set: &'h HrpSet) -> &'h str;
6}
7
8pub enum HashCreationError {
9 InvalidHrp,
10}
11
12pub trait IsTransactionHash: Sized {
13 fn hrp<'h>(&self, hrp_set: &'h HrpSet) -> &'h str;
14 fn create_from_hrp_and_hash(
15 hrp: &str,
16 hash: Hash,
17 hrp_set: &HrpSet,
18 ) -> Result<Self, HashCreationError>;
19 fn as_inner_hash(&self) -> &Hash;
20}
21
22impl<H: IsTransactionHashWithStaticHrp> IsTransactionHash for H {
23 fn hrp<'h>(&self, hrp_set: &'h HrpSet) -> &'h str {
24 Self::static_hrp(hrp_set)
25 }
26
27 fn create_from_hrp_and_hash(
28 hrp: &str,
29 hash: Hash,
30 hrp_set: &HrpSet,
31 ) -> Result<Self, HashCreationError> {
32 if Self::static_hrp(hrp_set) == hrp {
33 Ok(Self::from(hash))
34 } else {
35 Err(HashCreationError::InvalidHrp)
36 }
37 }
38
39 fn as_inner_hash(&self) -> &Hash {
40 self.as_hash()
41 }
42}
43
44impl IsTransactionHashWithStaticHrp for TransactionIntentHash {
45 fn static_hrp<'h>(hrp_set: &'h HrpSet) -> &'h str {
46 &hrp_set.transaction_intent
47 }
48}
49
50impl IsTransactionHashWithStaticHrp for SignedTransactionIntentHash {
51 fn static_hrp<'h>(hrp_set: &'h HrpSet) -> &'h str {
52 &hrp_set.signed_transaction_intent
53 }
54}
55
56impl IsTransactionHashWithStaticHrp for SubintentHash {
57 fn static_hrp<'h>(hrp_set: &'h HrpSet) -> &'h str {
58 &hrp_set.subintent
59 }
60}
61
62impl IsTransactionHash for IntentHash {
63 fn hrp<'h>(&self, hrp_set: &'h HrpSet) -> &'h str {
64 match self {
65 IntentHash::Transaction(_) => TransactionIntentHash::static_hrp(hrp_set),
66 IntentHash::Subintent(_) => SubintentHash::static_hrp(hrp_set),
67 }
68 }
69
70 fn create_from_hrp_and_hash(
71 hrp: &str,
72 hash: Hash,
73 hrp_set: &HrpSet,
74 ) -> Result<Self, HashCreationError> {
75 if hrp == TransactionIntentHash::static_hrp(hrp_set) {
76 Ok(IntentHash::Transaction(TransactionIntentHash::from(hash)))
77 } else if hrp == SubintentHash::static_hrp(hrp_set) {
78 Ok(IntentHash::Subintent(SubintentHash::from(hash)))
79 } else {
80 Err(HashCreationError::InvalidHrp)
81 }
82 }
83
84 fn as_inner_hash(&self) -> &Hash {
85 match self {
86 IntentHash::Transaction(inner) => inner.as_hash(),
87 IntentHash::Subintent(inner) => inner.as_hash(),
88 }
89 }
90}
91
92impl IsTransactionHashWithStaticHrp for NotarizedTransactionHash {
93 fn static_hrp<'h>(hrp_set: &'h HrpSet) -> &'h str {
94 &hrp_set.notarized_transaction
95 }
96}
97
98impl IsTransactionHashWithStaticHrp for SystemTransactionHash {
99 fn static_hrp<'h>(hrp_set: &'h HrpSet) -> &'h str {
100 &hrp_set.system_transaction
101 }
102}