@@ -13,40 +13,48 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
1313use crate :: encode:: { self , Decodable , Encodable } ;
1414use crate :: issuance:: AssetId ;
1515
16+ type ExplicitInner = AssetId ;
17+ type ConfInner = Generator ;
18+
19+ const EXPLICIT_LEN : usize = 32 ;
20+ const CONFIDENTIAL_LEN : usize = 33 ;
21+ const CONF_PREFIX_1 : u8 = 0x0a ;
22+ const CONF_PREFIX_2 : u8 = 0x0b ;
23+
1624/// A CT commitment to an asset
1725#[ derive( Copy , Clone , Debug , Default , Eq , Hash , PartialEq , PartialOrd , Ord ) ]
1826pub enum Asset {
1927 /// No value
2028 #[ default]
2129 Null ,
2230 /// Asset entropy is explicitly encoded
23- Explicit ( AssetId ) ,
31+ Explicit ( ExplicitInner ) ,
2432 /// Asset is committed
25- Confidential ( Generator ) ,
33+ Confidential ( ConfInner ) ,
2634}
2735
2836impl Asset {
2937 /// Create asset commitment.
3038 pub fn new_confidential < C : Signing > (
3139 secp : & Secp256k1 < C > ,
3240 asset : AssetId ,
33- bf : AssetBlindingFactor ,
41+ bf : BlindingFactor ,
3442 ) -> Self {
35- Self :: Confidential ( Generator :: new_blinded ( secp, asset. into_tag ( ) , bf. into_inner ( ) ) )
43+ Self :: Confidential ( ConfInner :: new_blinded ( secp, asset. into_tag ( ) , bf. into_inner ( ) ) )
3644 }
3745
3846 /// Serialized length, in bytes
3947 pub fn encoded_length ( & self ) -> usize {
4048 match * self {
4149 Self :: Null => 1 ,
42- Self :: Explicit ( ..) => 33 ,
43- Self :: Confidential ( ..) => 33 ,
50+ Self :: Explicit ( ..) => 1 + EXPLICIT_LEN ,
51+ Self :: Confidential ( ..) => CONFIDENTIAL_LEN ,
4452 }
4553 }
4654
4755 /// Create from commitment.
4856 pub fn from_commitment ( bytes : & [ u8 ] ) -> Result < Self , encode:: Error > {
49- Ok ( Self :: Confidential ( Generator :: from_slice ( bytes) ?) )
57+ Ok ( Self :: Confidential ( ConfInner :: from_slice ( bytes) ?) )
5058 }
5159
5260 /// Check if the object is null.
@@ -60,7 +68,7 @@ impl Asset {
6068
6169 /// Returns the explicit inner value.
6270 /// Returns [None] if [`Self::is_explicit`] returns false.
63- pub fn explicit ( & self ) -> Option < AssetId > {
71+ pub fn explicit ( & self ) -> Option < ExplicitInner > {
6472 match * self {
6573 Self :: Explicit ( i) => Some ( i) ,
6674 _ => None ,
@@ -69,7 +77,7 @@ impl Asset {
6977
7078 /// Returns the confidential commitment in case of a confidential value.
7179 /// Returns [None] if [`Self::is_confidential`] returns false.
72- pub fn commitment ( & self ) -> Option < Generator > {
80+ pub fn commitment ( & self ) -> Option < ConfInner > {
7381 match * self {
7482 Self :: Confidential ( i) => Some ( i) ,
7583 _ => None ,
@@ -84,19 +92,19 @@ impl Asset {
8492 pub fn into_asset_gen < C : secp256k1_zkp:: Signing > (
8593 self ,
8694 secp : & Secp256k1 < C > ,
87- ) -> Option < Generator > {
95+ ) -> Option < ConfInner > {
8896 match self {
8997 // Only error is Null error which is dealt with later
9098 // when we have more context information about it.
9199 Self :: Null => None ,
92- Self :: Explicit ( x) => Some ( Generator :: new_unblinded ( secp, x. into_tag ( ) ) ) ,
100+ Self :: Explicit ( x) => Some ( ConfInner :: new_unblinded ( secp, x. into_tag ( ) ) ) ,
93101 Self :: Confidential ( gen) => Some ( gen) ,
94102 }
95103 }
96104}
97105
98- impl From < Generator > for Asset {
99- fn from ( from : Generator ) -> Self { Self :: Confidential ( from) }
106+ impl From < ConfInner > for Asset {
107+ fn from ( from : ConfInner ) -> Self { Self :: Confidential ( from) }
100108}
101109
102110impl fmt:: Display for Asset {
@@ -119,7 +127,7 @@ impl Encodable for Asset {
119127 }
120128 Self :: Confidential ( generator) => {
121129 s. write_all ( & generator. serialize ( ) ) ?;
122- Ok ( 33 )
130+ Ok ( CONFIDENTIAL_LEN )
123131 }
124132 }
125133 }
@@ -135,11 +143,11 @@ impl Decodable for Asset {
135143 let explicit = Decodable :: consensus_decode ( & mut d) ?;
136144 Ok ( Self :: Explicit ( explicit) )
137145 }
138- p if p == 0x0a || p == 0x0b => {
139- let mut comm = [ 0u8 ; 33 ] ;
146+ p if p == CONF_PREFIX_1 || p == CONF_PREFIX_2 => {
147+ let mut comm = [ 0u8 ; CONFIDENTIAL_LEN ] ;
140148 comm[ 0 ] = p;
141149 d. read_exact ( & mut comm[ 1 ..] ) ?;
142- Ok ( Self :: Confidential ( Generator :: from_slice ( & comm[ ..] ) ?) )
150+ Ok ( Self :: Confidential ( ConfInner :: from_slice ( & comm[ ..] ) ?) )
143151 }
144152 p => Err ( encode:: Error :: InvalidConfidentialPrefix ( p) ) ,
145153 }
@@ -208,9 +216,9 @@ impl<'de> Deserialize<'de> for Asset {
208216
209217/// Blinding factor used for asset commitments.
210218#[ derive( Copy , Clone , Eq , PartialEq , PartialOrd , Ord , Hash ) ]
211- pub struct AssetBlindingFactor ( pub ( crate ) Tweak ) ;
219+ pub struct BlindingFactor ( pub ( crate ) Tweak ) ;
212220
213- impl AssetBlindingFactor {
221+ impl BlindingFactor {
214222 /// Generate random asset blinding factor.
215223 pub fn new < R : Rng > ( rng : & mut R ) -> Self { Self ( Tweak :: new ( rng) ) }
216224
@@ -235,18 +243,18 @@ impl AssetBlindingFactor {
235243 pub fn zero ( ) -> Self { Self ( ZERO_TWEAK ) }
236244}
237245
238- impl core:: borrow:: Borrow < [ u8 ] > for AssetBlindingFactor {
246+ impl core:: borrow:: Borrow < [ u8 ] > for BlindingFactor {
239247 fn borrow ( & self ) -> & [ u8 ] { & self . 0 [ ..] }
240248}
241249
242250hex:: impl_fmt_traits! {
243251 #[ display_backward( true ) ]
244- impl fmt_traits for AssetBlindingFactor {
252+ impl fmt_traits for BlindingFactor {
245253 const LENGTH : usize = 32 ;
246254 }
247255}
248256
249- impl str:: FromStr for AssetBlindingFactor {
257+ impl str:: FromStr for BlindingFactor {
250258 type Err = encode:: Error ;
251259
252260 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
@@ -259,7 +267,7 @@ impl str::FromStr for AssetBlindingFactor {
259267}
260268
261269#[ cfg( feature = "serde" ) ]
262- impl Serialize for AssetBlindingFactor {
270+ impl Serialize for BlindingFactor {
263271 fn serialize < S : Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
264272 if s. is_human_readable ( ) {
265273 s. collect_str ( & self )
@@ -270,13 +278,13 @@ impl Serialize for AssetBlindingFactor {
270278}
271279
272280#[ cfg( feature = "serde" ) ]
273- impl < ' de > Deserialize < ' de > for AssetBlindingFactor {
281+ impl < ' de > Deserialize < ' de > for BlindingFactor {
274282 fn deserialize < D : Deserializer < ' de > > ( d : D ) -> Result < Self , D :: Error > {
275283 if d. is_human_readable ( ) {
276284 struct HexVisitor ;
277285
278286 impl :: serde:: de:: Visitor < ' _ > for HexVisitor {
279- type Value = AssetBlindingFactor ;
287+ type Value = BlindingFactor ;
280288
281289 fn expecting ( & self , formatter : & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
282290 formatter. write_str ( "an ASCII hex string" )
@@ -306,7 +314,7 @@ impl<'de> Deserialize<'de> for AssetBlindingFactor {
306314 struct BytesVisitor ;
307315
308316 impl :: serde:: de:: Visitor < ' _ > for BytesVisitor {
309- type Value = AssetBlindingFactor ;
317+ type Value = BlindingFactor ;
310318
311319 fn expecting ( & self , formatter : & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
312320 formatter. write_str ( "a bytestring" )
@@ -321,7 +329,7 @@ impl<'de> Deserialize<'de> for AssetBlindingFactor {
321329 match <[ u8 ; 32 ] >:: try_from ( v) {
322330 Ok ( ret) => {
323331 let inner = Tweak :: from_inner ( ret) . map_err ( E :: custom) ?;
324- Ok ( AssetBlindingFactor ( inner) )
332+ Ok ( BlindingFactor ( inner) )
325333 }
326334 Err ( _) => Err ( E :: invalid_length ( v. len ( ) , & stringify ! ( $len) ) ) ,
327335 }
0 commit comments