11use crate :: crypto:: Ed25519PubKeyHash ;
22use crate :: ledger_state:: Slot ;
33use crate :: script:: ValidatorHash ;
4+ #[ cfg( feature = "lbf" ) ]
5+ use lbr_prelude:: json:: { self , Error , Json } ;
46use num_bigint:: BigInt ;
57
68#[ cfg( feature = "serde" ) ]
@@ -13,6 +15,7 @@ use serde::{Deserialize, Serialize};
1315/// For a better understanding of all the Cardano address types, read [CIP 19](https://cips.cardano.org/cips/cip19/)
1416#[ derive( Debug , PartialEq , Eq ) ]
1517#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
18+ #[ cfg_attr( feature = "lbf" , derive( Json ) ) ]
1619pub struct Address {
1720 pub credential : Credential ,
1821 pub staking_credential : Option < StakingCredential > ,
@@ -26,6 +29,45 @@ pub enum Credential {
2629 Script ( ValidatorHash ) ,
2730}
2831
32+ #[ cfg( feature = "lbf" ) ]
33+ impl Json for Credential {
34+ fn to_json ( & self ) -> Result < serde_json:: Value , Error > {
35+ match self {
36+ Credential :: PubKey ( pkh) => Ok ( json:: sum_constructor (
37+ "PubKeyCredential" ,
38+ vec ! [ pkh. to_json( ) ?] ,
39+ ) ) ,
40+ Credential :: Script ( val_hash) => Ok ( json:: sum_constructor (
41+ "ScriptCredential" ,
42+ vec ! [ val_hash. to_json( ) ?] ,
43+ ) ) ,
44+ }
45+ }
46+
47+ fn from_json ( value : serde_json:: Value ) -> Result < Self , Error > {
48+ json:: sum_parser ( & value) . and_then ( |obj| match obj {
49+ ( "PubKeyCredential" , ctor_fields) => match & ctor_fields[ ..] {
50+ [ pkh] => Ok ( Credential :: PubKey ( Json :: from_json ( pkh. clone ( ) ) ?) ) ,
51+ _ => Err ( Error :: UnexpectedArrayLength {
52+ wanted : 1 ,
53+ got : ctor_fields. len ( ) ,
54+ } ) ,
55+ } ,
56+ ( "ScriptCredential" , ctor_fields) => match & ctor_fields[ ..] {
57+ [ val_hash] => Ok ( Credential :: Script ( Json :: from_json ( val_hash. clone ( ) ) ?) ) ,
58+ _ => Err ( Error :: UnexpectedArrayLength {
59+ wanted : 1 ,
60+ got : ctor_fields. len ( ) ,
61+ } ) ,
62+ } ,
63+ _ => Err ( Error :: UnexpectedJsonInvariant {
64+ wanted : "constructor names (Nothing, Just)" . to_owned ( ) ,
65+ got : "unknown constructor name" . to_owned ( ) ,
66+ } ) ,
67+ } )
68+ }
69+ }
70+
2971/// Credential (public key hash or pointer) used for staking
3072#[ derive( Debug , PartialEq , Eq ) ]
3173#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
@@ -34,25 +76,68 @@ pub enum StakingCredential {
3476 Pointer ( ChainPointer ) ,
3577}
3678
79+ #[ cfg( feature = "lbf" ) ]
80+ impl Json for StakingCredential {
81+ fn to_json ( & self ) -> Result < serde_json:: Value , Error > {
82+ match self {
83+ StakingCredential :: Hash ( pkh) => {
84+ Ok ( json:: sum_constructor ( "StakingHash" , vec ! [ pkh. to_json( ) ?] ) )
85+ }
86+ StakingCredential :: Pointer ( val_hash) => Ok ( json:: sum_constructor (
87+ "StakingPtr" ,
88+ vec ! [ val_hash. to_json( ) ?] ,
89+ ) ) ,
90+ }
91+ }
92+
93+ fn from_json ( value : serde_json:: Value ) -> Result < Self , Error > {
94+ json:: sum_parser ( & value) . and_then ( |obj| match obj {
95+ ( "StakingHash" , ctor_fields) => match & ctor_fields[ ..] {
96+ [ pkh] => Ok ( StakingCredential :: Hash ( Json :: from_json ( pkh. clone ( ) ) ?) ) ,
97+ _ => Err ( Error :: UnexpectedArrayLength {
98+ wanted : 1 ,
99+ got : ctor_fields. len ( ) ,
100+ } ) ,
101+ } ,
102+ ( "StakingPtr" , ctor_fields) => match & ctor_fields[ ..] {
103+ [ val_hash] => Ok ( StakingCredential :: Pointer ( Json :: from_json (
104+ val_hash. clone ( ) ,
105+ ) ?) ) ,
106+ _ => Err ( Error :: UnexpectedArrayLength {
107+ wanted : 1 ,
108+ got : ctor_fields. len ( ) ,
109+ } ) ,
110+ } ,
111+ _ => Err ( Error :: UnexpectedJsonInvariant {
112+ wanted : "constructor names (Nothing, Just)" . to_owned ( ) ,
113+ got : "unknown constructor name" . to_owned ( ) ,
114+ } ) ,
115+ } )
116+ }
117+ }
118+
37119/// In an address, a chain pointer refers to a point of the chain containing a stake key
38120/// registration certificate. A point is identified by 3 coordinates:
39121/// - An absolute slot number
40122/// - A transaction inder (within that slot)
41123/// - A (delegation) certificate index (within that transacton)
42124#[ derive( Debug , PartialEq , Eq ) ]
43125#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
126+ #[ cfg_attr( feature = "lbf" , derive( Json ) ) ]
44127pub struct ChainPointer {
45- pub slot : Slot ,
46- pub tx_ix : TransactionIndex ,
47- pub cert_ix : CertificateIndex ,
128+ pub slot_number : Slot ,
129+ pub transaction_index : TransactionIndex ,
130+ pub certificate_index : CertificateIndex ,
48131}
49132/// Position of the certificate in a certain transaction
50133#[ derive( Debug , PartialEq , Eq ) ]
51134#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
135+ #[ cfg_attr( feature = "lbf" , derive( Json ) ) ]
52136pub struct CertificateIndex ( pub BigInt ) ;
53137
54138/// Position of a transaction in a given slot
55139/// This is not identical to the index of a `TransactionInput`
56140#[ derive( Debug , PartialEq , Eq ) ]
57141#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
58- pub struct TransactionIndex ( pub u32 ) ;
142+ #[ cfg_attr( feature = "lbf" , derive( Json ) ) ]
143+ pub struct TransactionIndex ( pub BigInt ) ;
0 commit comments