22
33use std:: sync:: Arc ;
44
5- use elements:: confidential:: { AssetBlindingFactor , ValueBlindingFactor } ;
65use elements:: secp256k1_zkp:: { Generator , PedersenCommitment , Tag } ;
76use lwk_wollet:: EC ;
87
9- use crate :: types:: { AssetId , Hex } ;
8+ use crate :: types:: {
9+ AssetBlindingFactor as BindingsAssetBlindingFactor , AssetId , Hex ,
10+ ValueBlindingFactor as BindingsValueBlindingFactor ,
11+ } ;
1012
1113/// Contains unblinded information such as the asset and the value of a transaction output
1214#[ derive( uniffi:: Object , PartialEq , Eq , Debug ) ]
@@ -28,15 +30,33 @@ impl From<&TxOutSecrets> for elements::TxOutSecrets {
2830
2931#[ uniffi:: export]
3032impl TxOutSecrets {
33+ /// Create TxOutSecrets with explicit blinding factors.
34+ #[ uniffi:: constructor]
35+ pub fn new (
36+ asset_id : AssetId ,
37+ asset_bf : & BindingsAssetBlindingFactor ,
38+ value : u64 ,
39+ value_bf : & BindingsValueBlindingFactor ,
40+ ) -> Arc < Self > {
41+ Arc :: new ( Self {
42+ inner : elements:: TxOutSecrets :: new (
43+ asset_id. into ( ) ,
44+ asset_bf. into ( ) ,
45+ value,
46+ value_bf. into ( ) ,
47+ ) ,
48+ } )
49+ }
50+
3151 /// Create TxOutSecrets from explicit (unblinded) values.
3252 #[ uniffi:: constructor]
3353 pub fn from_explicit ( asset_id : AssetId , value : u64 ) -> Arc < Self > {
3454 Arc :: new ( Self {
3555 inner : elements:: TxOutSecrets :: new (
3656 asset_id. into ( ) ,
37- AssetBlindingFactor :: zero ( ) ,
57+ elements :: confidential :: AssetBlindingFactor :: zero ( ) ,
3858 value,
39- ValueBlindingFactor :: zero ( ) ,
59+ elements :: confidential :: ValueBlindingFactor :: zero ( ) ,
4060 ) ,
4161 } )
4262 }
@@ -71,8 +91,8 @@ impl TxOutSecrets {
7191
7292 /// Return true if the output is explicit (no blinding factors).
7393 pub fn is_explicit ( & self ) -> bool {
74- self . inner . asset_bf == AssetBlindingFactor :: zero ( )
75- && self . inner . value_bf == ValueBlindingFactor :: zero ( )
94+ self . inner . asset_bf == elements :: confidential :: AssetBlindingFactor :: zero ( )
95+ && self . inner . value_bf == elements :: confidential :: ValueBlindingFactor :: zero ( )
7696 }
7797
7898 /// Get the asset commitment
@@ -118,10 +138,14 @@ impl TxOutSecrets {
118138
119139#[ cfg( test) ]
120140mod tests {
141+ use crate :: UniffiCustomTypeConverter ;
142+ use crate :: { types, TxOutSecrets } ;
143+
144+ use std:: str:: FromStr ;
145+
121146 use elements:: confidential:: { AssetBlindingFactor , ValueBlindingFactor } ;
122147 use elements:: hex:: FromHex ;
123148 use elements:: AssetId ;
124- use std:: str:: FromStr ;
125149
126150 #[ test]
127151 fn tx_out_secrets ( ) {
@@ -164,4 +188,23 @@ mod tests {
164188 assert_eq ! ( txoutsecrets_blinded. asset_commitment( ) . to_string( ) , ac_hex) ;
165189 assert_eq ! ( txoutsecrets_blinded. value_commitment( ) . to_string( ) , vc_hex) ;
166190 }
191+
192+ #[ test]
193+ fn test_tx_out_secrets_new_with_blinding ( ) {
194+ let asset_hex = "1111111111111111111111111111111111111111111111111111111111111111" ;
195+ let abf_hex = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ;
196+ let vbf_hex = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ;
197+
198+ let asset_id = types:: AssetId :: into_custom ( asset_hex. to_string ( ) ) . unwrap ( ) ;
199+ let asset_bf = types:: AssetBlindingFactor :: from_hex ( abf_hex) . unwrap ( ) ;
200+ let value_bf = types:: ValueBlindingFactor :: from_hex ( vbf_hex) . unwrap ( ) ;
201+
202+ let secrets = TxOutSecrets :: new ( asset_id, & asset_bf, 1000 , & value_bf) ;
203+
204+ assert ! ( !secrets. is_explicit( ) ) ;
205+ assert_eq ! ( secrets. asset( ) . to_string( ) , asset_hex) ;
206+ assert_eq ! ( secrets. asset_bf( ) . to_string( ) , abf_hex) ;
207+ assert_eq ! ( secrets. value( ) , 1000 ) ;
208+ assert_eq ! ( secrets. value_bf( ) . to_string( ) , vbf_hex) ;
209+ }
167210}
0 commit comments