33use crate :: error:: * ;
44use crate :: handle:: * ;
55use crate :: runtime:: runtime;
6+ use crate :: { check_ptr, unwrap_option_or_return, unwrap_result_or_return} ;
67
78/// Build an asset lock transaction.
89///
@@ -21,54 +22,34 @@ pub unsafe extern "C" fn asset_lock_manager_build_transaction(
2122 out_tx_bytes : * mut * mut u8 ,
2223 out_tx_len : * mut usize ,
2324 out_private_key : * mut [ u8 ; 32 ] ,
24- out_error : * mut PlatformWalletFFIError ,
2525) -> PlatformWalletFFIResult {
26- if out_tx_bytes. is_null ( ) || out_tx_len. is_null ( ) || out_private_key. is_null ( ) {
27- return PlatformWalletFFIResult :: ErrorNullPointer ;
28- }
26+ check_ptr ! ( out_tx_bytes) ;
27+ check_ptr ! ( out_tx_len) ;
28+ check_ptr ! ( out_private_key) ;
29+
30+ let option = parse_funding_type ( funding_type) ;
31+ let funding = unwrap_option_or_return ! ( option) ;
32+
33+ let option = ASSET_LOCK_MANAGER_STORAGE . with_item ( handle, |manager| {
34+ runtime ( ) . block_on ( manager. build_asset_lock_transaction (
35+ amount_duffs,
36+ account_index,
37+ funding,
38+ identity_index,
39+ ) )
40+ } ) ;
41+ let result = unwrap_option_or_return ! ( option) ;
42+ let ( tx, key) = unwrap_result_or_return ! ( result) ;
43+
44+ let serialized = dashcore:: consensus:: serialize ( & tx) ;
45+ let len = serialized. len ( ) ;
46+ let boxed = serialized. into_boxed_slice ( ) ;
47+
48+ * out_tx_bytes = Box :: into_raw ( boxed) as * mut u8 ;
49+ * out_tx_len = len;
50+ * out_private_key = key. inner . secret_bytes ( ) ;
2951
30- let funding = match parse_funding_type ( funding_type) {
31- Some ( f) => f,
32- None => {
33- if !out_error. is_null ( ) {
34- * out_error = PlatformWalletFFIError :: new (
35- PlatformWalletFFIResult :: ErrorInvalidParameter ,
36- format ! ( "Unknown funding type: {}" , funding_type) ,
37- ) ;
38- }
39- return PlatformWalletFFIResult :: ErrorInvalidParameter ;
40- }
41- } ;
42-
43- ASSET_LOCK_MANAGER_STORAGE
44- . with_item ( handle, |manager| {
45- match runtime ( ) . block_on ( manager. build_asset_lock_transaction (
46- amount_duffs,
47- account_index,
48- funding,
49- identity_index,
50- ) ) {
51- Ok ( ( tx, key) ) => {
52- let serialized = dashcore:: consensus:: serialize ( & tx) ;
53- let len = serialized. len ( ) ;
54- let boxed = serialized. into_boxed_slice ( ) ;
55- * out_tx_bytes = Box :: into_raw ( boxed) as * mut u8 ;
56- * out_tx_len = len;
57- * out_private_key = key. inner . secret_bytes ( ) ;
58- PlatformWalletFFIResult :: Success
59- }
60- Err ( e) => {
61- if !out_error. is_null ( ) {
62- * out_error = PlatformWalletFFIError :: new (
63- PlatformWalletFFIResult :: ErrorWalletOperation ,
64- e. to_string ( ) ,
65- ) ;
66- }
67- PlatformWalletFFIResult :: ErrorWalletOperation
68- }
69- }
70- } )
71- . unwrap_or ( PlatformWalletFFIResult :: ErrorInvalidHandle )
52+ PlatformWalletFFIResult :: ok ( )
7253}
7354
7455/// Build, broadcast, and wait for an asset lock proof.
@@ -91,74 +72,40 @@ pub unsafe extern "C" fn asset_lock_manager_create_funded_proof(
9172 out_proof_len : * mut usize ,
9273 out_private_key : * mut [ u8 ; 32 ] ,
9374 out_txid : * mut [ u8 ; 32 ] ,
94- out_error : * mut PlatformWalletFFIError ,
9575) -> PlatformWalletFFIResult {
96- if out_proof_bytes. is_null ( )
97- || out_proof_len. is_null ( )
98- || out_private_key. is_null ( )
99- || out_txid. is_null ( )
100- {
101- return PlatformWalletFFIResult :: ErrorNullPointer ;
102- }
76+ check_ptr ! ( out_proof_bytes) ;
77+ check_ptr ! ( out_proof_len) ;
78+ check_ptr ! ( out_private_key) ;
79+ check_ptr ! ( out_txid) ;
80+
81+ let funding = unwrap_option_or_return ! ( parse_funding_type( funding_type) ) ;
82+
83+ let option = ASSET_LOCK_MANAGER_STORAGE . with_item ( handle, |manager| {
84+ runtime ( ) . block_on ( manager. create_funded_asset_lock_proof (
85+ amount_duffs,
86+ account_index,
87+ funding,
88+ identity_index,
89+ ) )
90+ } ) ;
91+ let result = unwrap_option_or_return ! ( option) ;
92+ let ( proof, key, out_point) = unwrap_result_or_return ! ( result) ;
93+
94+ let bytes = unwrap_result_or_return ! ( dpp:: bincode:: encode_to_vec(
95+ & proof,
96+ dpp:: bincode:: config:: standard( )
97+ ) ) ;
98+
99+ let len = bytes. len ( ) ;
100+ let boxed = bytes. into_boxed_slice ( ) ;
101+ * out_proof_bytes = Box :: into_raw ( boxed) as * mut u8 ;
102+ * out_proof_len = len;
103+ * out_private_key = key. inner . secret_bytes ( ) ;
104+ let mut txid_bytes = [ 0u8 ; 32 ] ;
105+ txid_bytes. copy_from_slice ( & out_point. txid [ ..] ) ;
106+ * out_txid = txid_bytes;
103107
104- let funding = match parse_funding_type ( funding_type) {
105- Some ( f) => f,
106- None => {
107- if !out_error. is_null ( ) {
108- * out_error = PlatformWalletFFIError :: new (
109- PlatformWalletFFIResult :: ErrorInvalidParameter ,
110- format ! ( "Unknown funding type: {}" , funding_type) ,
111- ) ;
112- }
113- return PlatformWalletFFIResult :: ErrorInvalidParameter ;
114- }
115- } ;
116-
117- ASSET_LOCK_MANAGER_STORAGE
118- . with_item ( handle, |manager| {
119- match runtime ( ) . block_on ( manager. create_funded_asset_lock_proof (
120- amount_duffs,
121- account_index,
122- funding,
123- identity_index,
124- ) ) {
125- Ok ( ( proof, key, out_point) ) => {
126- // Serialize proof with bincode
127- match dpp:: bincode:: encode_to_vec ( & proof, dpp:: bincode:: config:: standard ( ) ) {
128- Ok ( bytes) => {
129- let len = bytes. len ( ) ;
130- let boxed = bytes. into_boxed_slice ( ) ;
131- * out_proof_bytes = Box :: into_raw ( boxed) as * mut u8 ;
132- * out_proof_len = len;
133- * out_private_key = key. inner . secret_bytes ( ) ;
134- let mut txid_bytes = [ 0u8 ; 32 ] ;
135- txid_bytes. copy_from_slice ( & out_point. txid [ ..] ) ;
136- * out_txid = txid_bytes;
137- PlatformWalletFFIResult :: Success
138- }
139- Err ( e) => {
140- if !out_error. is_null ( ) {
141- * out_error = PlatformWalletFFIError :: new (
142- PlatformWalletFFIResult :: ErrorSerialization ,
143- format ! ( "Failed to serialize proof: {}" , e) ,
144- ) ;
145- }
146- PlatformWalletFFIResult :: ErrorSerialization
147- }
148- }
149- }
150- Err ( e) => {
151- if !out_error. is_null ( ) {
152- * out_error = PlatformWalletFFIError :: new (
153- PlatformWalletFFIResult :: ErrorWalletOperation ,
154- e. to_string ( ) ,
155- ) ;
156- }
157- PlatformWalletFFIResult :: ErrorWalletOperation
158- }
159- }
160- } )
161- . unwrap_or ( PlatformWalletFFIResult :: ErrorInvalidHandle )
108+ PlatformWalletFFIResult :: ok ( )
162109}
163110
164111/// Free transaction bytes.
0 commit comments