@@ -30,6 +30,7 @@ use std::io::{Read, Seek, SeekFrom};
3030
3131pub use crate :: hash:: { AddrKeyhash , Hash , ScriptHash } ;
3232pub use crate :: stake_addresses:: { AccountState , StakeAddressState } ;
33+ pub use crate :: StakeCredential ;
3334
3435// -----------------------------------------------------------------------------
3536// Cardano Ledger Types (for decoding with minicbor)
@@ -38,22 +39,41 @@ pub use crate::stake_addresses::{AccountState, StakeAddressState};
3839pub type Epoch = u64 ;
3940pub type Lovelace = u64 ;
4041
42+ /*
43+ * This was replaced with the StakeCredential defined in types.rs, but the implementation here is much
44+ * cleaner for parsing CBOR files from Haskell Node, using hash.rs types. For CBOR parsing, we need to
45+ * change the decode from using d.decode_with(ctx) (which expects arrays) tousing d.bytes() which
46+ * expects raw bytes.
4147/// Stake credential - can be a key hash or script hash
4248/// Order matters for Ord/PartialOrd - ScriptHash must come first for compatibility with Haskell
4349#[derive(Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Hash)]
4450pub enum StakeCredential {
4551 ScriptHash(ScriptHash),
46- AddrKeyhash ( AddrKeyhash ) ,
52+ AddrKeyhash(AddrKeyhash), // NOTE: lower case h from hash.rs version
4753}
54+ */
4855
4956impl < ' b , C > minicbor:: decode:: Decode < ' b , C > for StakeCredential {
50- fn decode ( d : & mut minicbor:: Decoder < ' b > , ctx : & mut C ) -> Result < Self , minicbor:: decode:: Error > {
57+ fn decode (
58+ d : & mut minicbor:: Decoder < ' b > ,
59+ _ctx : & mut C ,
60+ ) -> Result < Self , minicbor:: decode:: Error > {
5161 d. array ( ) ?;
5262 let variant = d. u16 ( ) ?;
5363
5464 match variant {
55- 0 => Ok ( StakeCredential :: AddrKeyhash ( d. decode_with ( ctx) ?) ) ,
56- 1 => Ok ( StakeCredential :: ScriptHash ( d. decode_with ( ctx) ?) ) ,
65+ 0 => {
66+ // ScriptHash variant (first in enum) - decode bytes directly
67+ let bytes = d. bytes ( ) ?;
68+ let key_hash = bytes. to_vec ( ) ;
69+ Ok ( StakeCredential :: ScriptHash ( key_hash) )
70+ }
71+ 1 => {
72+ // AddrKeyHash variant (second in enum) - decode bytes directly
73+ let bytes = d. bytes ( ) ?;
74+ let key_hash = bytes. to_vec ( ) ;
75+ Ok ( StakeCredential :: AddrKeyHash ( key_hash) )
76+ }
5777 _ => Err ( minicbor:: decode:: Error :: message (
5878 "invalid variant id for StakeCredential" ,
5979 ) ) ,
@@ -68,18 +88,18 @@ impl<C> minicbor::encode::Encode<C> for StakeCredential {
6888 ctx : & mut C ,
6989 ) -> Result < ( ) , minicbor:: encode:: Error < W :: Error > > {
7090 match self {
71- StakeCredential :: AddrKeyhash ( a) => {
91+ StakeCredential :: ScriptHash ( key_hash) => {
92+ // ScriptHash is variant 0 (first in enum)
7293 e. array ( 2 ) ?;
7394 e. encode_with ( 0 , ctx) ?;
74- e. encode_with ( a, ctx) ?;
75-
95+ e. encode_with ( key_hash, ctx) ?;
7696 Ok ( ( ) )
7797 }
78- StakeCredential :: ScriptHash ( a) => {
98+ StakeCredential :: AddrKeyHash ( key_hash) => {
99+ // AddrKeyHash is variant 1 (second in enum)
79100 e. array ( 2 ) ?;
80101 e. encode_with ( 1 , ctx) ?;
81- e. encode_with ( a, ctx) ?;
82-
102+ e. encode_with ( key_hash, ctx) ?;
83103 Ok ( ( ) )
84104 }
85105 }
@@ -1009,7 +1029,7 @@ impl StreamingSnapshotParser {
10091029 . map ( |( credential, account) | {
10101030 // Convert StakeCredential to stake address representation
10111031 let stake_address = match & credential {
1012- StakeCredential :: AddrKeyhash ( hash) => {
1032+ StakeCredential :: AddrKeyHash ( hash) => {
10131033 format ! ( "stake_key_{}" , hex:: encode( hash) )
10141034 }
10151035 StakeCredential :: ScriptHash ( hash) => {
@@ -1398,7 +1418,7 @@ impl StreamingSnapshotParser {
13981418 . map ( |( credential, account) | {
13991419 // Convert StakeCredential to stake address representation
14001420 let stake_address = match & credential {
1401- StakeCredential :: AddrKeyhash ( hash) => {
1421+ StakeCredential :: AddrKeyHash ( hash) => {
14021422 format ! ( "stake_key_{}" , hex:: encode( hash) )
14031423 }
14041424 StakeCredential :: ScriptHash ( hash) => {
@@ -1806,8 +1826,10 @@ impl StreamingSnapshotParser {
18061826 . into_iter ( )
18071827 . map ( |( cred, state) | {
18081828 let drep_id = match cred {
1809- StakeCredential :: AddrKeyhash ( hash) => format ! ( "drep_{}" , hash) ,
1810- StakeCredential :: ScriptHash ( hash) => format ! ( "drep_script_{}" , hash) ,
1829+ StakeCredential :: AddrKeyHash ( hash) => format ! ( "drep_{}" , hex:: encode( hash) ) ,
1830+ StakeCredential :: ScriptHash ( hash) => {
1831+ format ! ( "drep_script_{}" , hex:: encode( hash) )
1832+ }
18111833 } ;
18121834
18131835 let anchor = match state. anchor {
0 commit comments