1212import org .unicitylabs .sdk .util .BitString ;
1313import org .unicitylabs .sdk .util .HexConverter ;
1414
15- public class StateId {
15+ /**
16+ * Represents a state identifier for requests.
17+ */
18+ public final class StateId {
1619
1720 private final DataHash hash ;
1821
1922 private StateId (DataHash hash ) {
2023 this .hash = hash ;
2124 }
2225
26+ /**
27+ * Returns the raw hash bytes of this state id.
28+ *
29+ * @return state id hash bytes
30+ */
2331 public byte [] getData () {
2432 return this .hash .getData ();
2533 }
2634
35+ /**
36+ * Returns the hash imprint bytes.
37+ *
38+ * @return state id imprint bytes
39+ */
2740 public byte [] getImprint () {
2841 return this .hash .getImprint ();
2942 }
3043
44+ /**
45+ * Deserializes a state id from CBOR.
46+ *
47+ * @param bytes CBOR byte string containing SHA-256 hash bytes
48+ * @return decoded state id
49+ */
3150 public static StateId fromCbor (byte [] bytes ) {
3251 return new StateId (
3352 new DataHash (HashAlgorithm .SHA256 , CborDeserializer .decodeByteString (bytes )));
3453 }
3554
55+ /**
56+ * Creates a state id from certification data.
57+ *
58+ * @param certificationData certification data carrying lock script and source state hash
59+ * @return created state id
60+ * @throws NullPointerException if {@code certificationData} is {@code null}
61+ */
3662 public static StateId fromCertificationData (CertificationData certificationData ) {
3763 Objects .requireNonNull (certificationData , "Certification data cannot be null" );
3864
3965 return StateId .create (certificationData .getLockScript (),
4066 certificationData .getSourceStateHash ());
4167 }
4268
69+ /**
70+ * Creates a state id from transaction data.
71+ *
72+ * @param transaction transaction carrying lock script and source state hash
73+ * @return created state id
74+ * @throws NullPointerException if {@code transaction} is {@code null}
75+ */
4376 public static StateId fromTransaction (Transaction transaction ) {
4477 Objects .requireNonNull (transaction , "Transaction cannot be null" );
4578
@@ -59,17 +92,22 @@ private static StateId create(Predicate predicate, DataHash stateHash) {
5992 return new StateId (hash );
6093 }
6194
95+ /**
96+ * Serializes this state id as a CBOR bytes.
97+ *
98+ * @return CBOR-encoded state id
99+ */
62100 public byte [] toCbor () {
63101 return CborSerializer .encodeByteString (this .getData ());
64102 }
65103
66104 /**
67- * Converts the StateId to a BitString.
105+ * Converts this state id to a {@link BitString} .
68106 *
69- * @return The BitString representation of the StateId.
107+ * @return bit string representation of this state id
70108 */
71109 public BitString toBitString () {
72- return BitString . fromStateId (this );
110+ return new BitString (this . getImprint () );
73111 }
74112
75113 @ Override
@@ -86,13 +124,8 @@ public int hashCode() {
86124 return Objects .hashCode (this .hash );
87125 }
88126
89- /**
90- * Returns a string representation of the StateId.
91- *
92- * @return The string representation.
93- */
94127 @ Override
95128 public String toString () {
96129 return String .format ("StateId[%s]" , HexConverter .encode (this .getImprint ()));
97130 }
98- }
131+ }
0 commit comments