@@ -3,31 +3,35 @@ use crate::nbgl::NbglSpinner;
3
3
use crate :: testing:: debug_print;
4
4
use ledger_secure_sdk_sys:: {
5
5
check_address_parameters_t, create_transaction_parameters_t, get_printable_amount_parameters_t,
6
- libargs_s__bindgen_ty_1, libargs_t,
6
+ libargs_s__bindgen_ty_1, libargs_t, MAX_PRINTABLE_AMOUNT_SIZE ,
7
7
} ;
8
8
9
+ const DPATH_STAGE_SIZE : usize = 16 ;
10
+ const ADDRESS_BUF_SIZE : usize = 64 ;
11
+ const AMOUNT_BUF_SIZE : usize = 16 ;
12
+
9
13
pub struct CheckAddressParams {
10
- pub dpath : [ u8 ; 64 ] ,
14
+ pub dpath : [ u8 ; DPATH_STAGE_SIZE * 4 ] ,
11
15
pub dpath_len : usize ,
12
- pub ref_address : [ u8 ; 64 ] ,
16
+ pub ref_address : [ u8 ; ADDRESS_BUF_SIZE ] ,
13
17
pub ref_address_len : usize ,
14
18
pub result : * mut i32 ,
15
19
}
16
20
17
21
impl Default for CheckAddressParams {
18
22
fn default ( ) -> Self {
19
23
CheckAddressParams {
20
- dpath : [ 0 ; 64 ] ,
24
+ dpath : [ 0 ; DPATH_STAGE_SIZE * 4 ] ,
21
25
dpath_len : 0 ,
22
- ref_address : [ 0 ; 64 ] ,
26
+ ref_address : [ 0 ; ADDRESS_BUF_SIZE ] ,
23
27
ref_address_len : 0 ,
24
28
result : core:: ptr:: null_mut ( ) ,
25
29
}
26
30
}
27
31
}
28
32
29
33
pub struct PrintableAmountParams {
30
- pub amount : [ u8 ; 16 ] ,
34
+ pub amount : [ u8 ; AMOUNT_BUF_SIZE ] ,
31
35
pub amount_len : usize ,
32
36
pub amount_str : * mut i8 ,
33
37
pub is_fee : bool ,
@@ -36,7 +40,7 @@ pub struct PrintableAmountParams {
36
40
impl Default for PrintableAmountParams {
37
41
fn default ( ) -> Self {
38
42
PrintableAmountParams {
39
- amount : [ 0 ; 16 ] ,
43
+ amount : [ 0 ; AMOUNT_BUF_SIZE ] ,
40
44
amount_len : 0 ,
41
45
amount_str : core:: ptr:: null_mut ( ) ,
42
46
is_fee : false ,
@@ -45,112 +49,112 @@ impl Default for PrintableAmountParams {
45
49
}
46
50
47
51
pub struct CreateTxParams {
48
- pub amount : [ u8 ; 16 ] ,
52
+ pub amount : [ u8 ; AMOUNT_BUF_SIZE ] ,
49
53
pub amount_len : usize ,
50
- pub fee_amount : [ u8 ; 16 ] ,
54
+ pub fee_amount : [ u8 ; AMOUNT_BUF_SIZE ] ,
51
55
pub fee_amount_len : usize ,
52
- pub dest_address : [ u8 ; 64 ] ,
56
+ pub dest_address : [ u8 ; ADDRESS_BUF_SIZE ] ,
53
57
pub dest_address_len : usize ,
54
58
pub result : * mut u8 ,
55
59
}
56
60
57
61
impl Default for CreateTxParams {
58
62
fn default ( ) -> Self {
59
63
CreateTxParams {
60
- amount : [ 0 ; 16 ] ,
64
+ amount : [ 0 ; AMOUNT_BUF_SIZE ] ,
61
65
amount_len : 0 ,
62
- fee_amount : [ 0 ; 16 ] ,
66
+ fee_amount : [ 0 ; AMOUNT_BUF_SIZE ] ,
63
67
fee_amount_len : 0 ,
64
- dest_address : [ 0 ; 64 ] ,
68
+ dest_address : [ 0 ; ADDRESS_BUF_SIZE ] ,
65
69
dest_address_len : 0 ,
66
70
result : core:: ptr:: null_mut ( ) ,
67
71
}
68
72
}
69
73
}
70
74
71
75
pub fn get_check_address_params ( arg0 : u32 ) -> CheckAddressParams {
72
- unsafe {
73
- debug_print ( "=> get_check_address_params\n " ) ;
76
+ debug_print ( "=> get_check_address_params\n " ) ;
74
77
75
- let mut libarg: libargs_t = libargs_t:: default ( ) ;
78
+ let mut libarg: libargs_t = libargs_t:: default ( ) ;
76
79
77
- let arg = arg0 as * const u32 ;
80
+ let arg = arg0 as * const u32 ;
78
81
79
- libarg. id = * arg;
80
- libarg. command = * arg. add ( 1 ) ;
81
- libarg. unused = * arg. add ( 2 ) ;
82
+ libarg. id = unsafe { * arg } ;
83
+ libarg. command = unsafe { * arg. add ( 1 ) } ;
84
+ libarg. unused = unsafe { * arg. add ( 2 ) } ;
82
85
83
- libarg. __bindgen_anon_1 = * ( arg. add ( 3 ) as * const libargs_s__bindgen_ty_1 ) ;
86
+ libarg. __bindgen_anon_1 = unsafe { * ( arg. add ( 3 ) as * const libargs_s__bindgen_ty_1 ) } ;
84
87
85
- let params: check_address_parameters_t =
86
- * ( libarg. __bindgen_anon_1 . check_address as * const check_address_parameters_t ) ;
88
+ let params: check_address_parameters_t =
89
+ unsafe { * ( libarg. __bindgen_anon_1 . check_address as * const check_address_parameters_t ) } ;
87
90
88
- let mut check_address_params: CheckAddressParams = Default :: default ( ) ;
91
+ let mut check_address_params: CheckAddressParams = Default :: default ( ) ;
89
92
90
- debug_print ( "==> GET_DPATH_LENGTH\n " ) ;
91
- check_address_params. dpath_len = * ( params. address_parameters as * const u8 ) as usize ;
93
+ debug_print ( "==> GET_DPATH_LENGTH\n " ) ;
94
+ check_address_params. dpath_len =
95
+ DPATH_STAGE_SIZE . min ( unsafe { * ( params. address_parameters as * const u8 ) as usize } ) ;
92
96
93
- debug_print ( "==> GET_DPATH \n " ) ;
94
- for i in 1 ..1 + check_address_params. dpath_len * 4 {
95
- check_address_params. dpath [ i - 1 ] = * ( params. address_parameters . add ( i) ) ;
96
- }
97
+ debug_print ( "==> GET_DPATH \n " ) ;
98
+ for i in 1 ..1 + check_address_params. dpath_len * 4 {
99
+ check_address_params. dpath [ i - 1 ] = unsafe { * ( params. address_parameters . add ( i) ) } ;
100
+ }
97
101
98
- debug_print ( "==> GET_REF_ADDRESS\n " ) ;
99
- let mut address_length = 0usize ;
100
- while * ( params. address_to_check . wrapping_add ( address_length) ) != '\0' as i8 {
101
- check_address_params. ref_address [ address_length] =
102
- * ( params. address_to_check . wrapping_add ( address_length) ) as u8 ;
103
- address_length += 1 ;
104
- }
105
- check_address_params. ref_address_len = address_length;
102
+ debug_print ( "==> GET_REF_ADDRESS\n " ) ;
103
+ let mut address_length = 0usize ;
104
+ let mut c = unsafe { * ( params. address_to_check . add ( address_length) ) } ;
105
+ while c != '\0' as i8 && address_length < ADDRESS_BUF_SIZE {
106
+ check_address_params. ref_address [ address_length] = c as u8 ;
107
+ address_length += 1 ;
108
+ c = unsafe { * ( params. address_to_check . add ( address_length) ) } ;
109
+ }
110
+ check_address_params. ref_address_len = address_length;
106
111
107
- check_address_params. result = & ( * ( libarg. __bindgen_anon_1 . check_address
108
- as * mut check_address_parameters_t ) )
109
- . result as * const i32 as * mut i32 ;
112
+ check_address_params. result = unsafe {
113
+ & ( * ( libarg. __bindgen_anon_1 . check_address as * mut check_address_parameters_t ) ) . result
114
+ as * const i32 as * mut i32
115
+ } ;
110
116
111
- check_address_params
112
- }
117
+ check_address_params
113
118
}
114
119
115
120
pub fn get_printable_amount_params ( arg0 : u32 ) -> PrintableAmountParams {
116
- unsafe {
117
- debug_print ( "=> get_printable_amount_params\n " ) ;
121
+ debug_print ( "=> get_printable_amount_params\n " ) ;
118
122
119
- let mut libarg: libargs_t = libargs_t:: default ( ) ;
123
+ let mut libarg: libargs_t = libargs_t:: default ( ) ;
120
124
121
- let arg = arg0 as * const u32 ;
125
+ let arg = arg0 as * const u32 ;
122
126
123
- libarg. id = * arg;
124
- libarg. command = * arg. add ( 1 ) ;
125
- libarg. unused = * arg. add ( 2 ) ;
127
+ libarg. id = unsafe { * arg } ;
128
+ libarg. command = unsafe { * arg. add ( 1 ) } ;
129
+ libarg. unused = unsafe { * arg. add ( 2 ) } ;
126
130
127
- libarg. __bindgen_anon_1 = * ( arg. add ( 3 ) as * const libargs_s__bindgen_ty_1 ) ;
131
+ libarg. __bindgen_anon_1 = unsafe { * ( arg. add ( 3 ) as * const libargs_s__bindgen_ty_1 ) } ;
128
132
129
- let params: get_printable_amount_parameters_t =
130
- * ( libarg. __bindgen_anon_1 . get_printable_amount
131
- as * const get_printable_amount_parameters_t ) ;
133
+ let params: get_printable_amount_parameters_t = unsafe {
134
+ * ( libarg. __bindgen_anon_1 . get_printable_amount as * const get_printable_amount_parameters_t )
135
+ } ;
132
136
133
- let mut printable_amount_params: PrintableAmountParams = Default :: default ( ) ;
137
+ let mut printable_amount_params: PrintableAmountParams = Default :: default ( ) ;
134
138
135
- debug_print ( "==> GET_IS_FEE\n " ) ;
136
- printable_amount_params. is_fee = params. is_fee == true ;
139
+ debug_print ( "==> GET_IS_FEE\n " ) ;
140
+ printable_amount_params. is_fee = params. is_fee == true ;
137
141
138
- debug_print ( "==> GET_AMOUNT_LENGTH\n " ) ;
139
- printable_amount_params. amount_len = params. amount_length as usize ;
142
+ debug_print ( "==> GET_AMOUNT_LENGTH\n " ) ;
143
+ printable_amount_params. amount_len = AMOUNT_BUF_SIZE . min ( params. amount_length as usize ) ;
140
144
141
- debug_print ( "==> GET_AMOUNT\n " ) ;
142
- for i in 0 ..printable_amount_params. amount_len {
143
- printable_amount_params. amount [ 16 - printable_amount_params. amount_len + i] =
144
- * ( params. amount . add ( i) ) ;
145
- }
145
+ debug_print ( "==> GET_AMOUNT\n " ) ;
146
+ for i in 0 ..printable_amount_params. amount_len {
147
+ printable_amount_params. amount [ AMOUNT_BUF_SIZE - printable_amount_params. amount_len + i] =
148
+ unsafe { * ( params. amount . add ( i) ) } ;
149
+ }
146
150
147
- debug_print ( "==> GET_AMOUNT_STR\n " ) ;
148
- printable_amount_params. amount_str = & ( * ( libarg. __bindgen_anon_1 . get_printable_amount
149
- as * mut get_printable_amount_parameters_t ) )
150
- . printable_amount as * const i8 as * mut i8 ;
151
+ debug_print ( "==> GET_AMOUNT_STR\n " ) ;
152
+ printable_amount_params. amount_str = unsafe {
153
+ & ( * ( libarg. __bindgen_anon_1 . get_printable_amount as * mut get_printable_amount_parameters_t ) )
154
+ . printable_amount as * const i8 as * mut i8
155
+ } ;
151
156
152
- printable_amount_params
153
- }
157
+ printable_amount_params
154
158
}
155
159
156
160
extern "C" {
@@ -159,59 +163,63 @@ extern "C" {
159
163
}
160
164
161
165
pub fn sign_tx_params ( arg0 : u32 ) -> CreateTxParams {
162
- unsafe {
163
- debug_print ( "=> sign_tx_params\n " ) ;
166
+ debug_print ( "=> sign_tx_params\n " ) ;
164
167
165
- let mut libarg: libargs_t = libargs_t:: default ( ) ;
168
+ let mut libarg: libargs_t = libargs_t:: default ( ) ;
166
169
167
- let arg = arg0 as * const u32 ;
170
+ let arg = arg0 as * const u32 ;
168
171
169
- libarg. id = * arg;
170
- libarg. command = * arg. add ( 1 ) ;
171
- libarg. unused = * arg. add ( 2 ) ;
172
+ libarg. id = unsafe { * arg } ;
173
+ libarg. command = unsafe { * arg. add ( 1 ) } ;
174
+ libarg. unused = unsafe { * arg. add ( 2 ) } ;
172
175
173
- libarg. __bindgen_anon_1 = * ( arg. add ( 3 ) as * const libargs_s__bindgen_ty_1 ) ;
176
+ libarg. __bindgen_anon_1 = unsafe { * ( arg. add ( 3 ) as * const libargs_s__bindgen_ty_1 ) } ;
174
177
175
- let params: create_transaction_parameters_t =
176
- * ( libarg. __bindgen_anon_1 . create_transaction as * const create_transaction_parameters_t ) ;
178
+ let params: create_transaction_parameters_t = unsafe {
179
+ * ( libarg. __bindgen_anon_1 . create_transaction as * const create_transaction_parameters_t )
180
+ } ;
177
181
178
- let mut create_tx_params: CreateTxParams = Default :: default ( ) ;
182
+ let mut create_tx_params: CreateTxParams = Default :: default ( ) ;
179
183
180
- debug_print ( "==> GET_AMOUNT\n " ) ;
181
- create_tx_params. amount_len = params. amount_length as usize ;
182
- for i in 0 ..create_tx_params. amount_len {
183
- create_tx_params. amount [ 16 - create_tx_params. amount_len + i] = * ( params. amount . add ( i) ) ;
184
- }
184
+ debug_print ( "==> GET_AMOUNT\n " ) ;
185
+ create_tx_params. amount_len = AMOUNT_BUF_SIZE . min ( params. amount_length as usize ) ;
186
+ for i in 0 ..create_tx_params. amount_len {
187
+ create_tx_params. amount [ AMOUNT_BUF_SIZE - create_tx_params. amount_len + i] =
188
+ unsafe { * ( params. amount . add ( i) ) } ;
189
+ }
185
190
186
- debug_print ( "==> GET_FEE\n " ) ;
187
- create_tx_params. fee_amount_len = params. fee_amount_length as usize ;
188
- for i in 0 ..create_tx_params. fee_amount_len {
189
- create_tx_params. fee_amount [ 16 - create_tx_params. fee_amount_len + i] =
190
- * ( params. fee_amount . add ( i) ) ;
191
- }
191
+ debug_print ( "==> GET_FEE\n " ) ;
192
+ create_tx_params. fee_amount_len = AMOUNT_BUF_SIZE . min ( params. fee_amount_length as usize ) ;
193
+ for i in 0 ..create_tx_params. fee_amount_len {
194
+ create_tx_params. fee_amount [ AMOUNT_BUF_SIZE - create_tx_params. fee_amount_len + i] =
195
+ unsafe { * ( params. fee_amount . add ( i) ) } ;
196
+ }
192
197
193
- debug_print ( "==> GET_DESTINATION_ADDRESS\n " ) ;
194
- let mut dest_address_length = 0usize ;
195
- while * ( params. destination_address . wrapping_add ( dest_address_length) ) != '\0' as i8 {
196
- create_tx_params. dest_address [ dest_address_length] =
197
- * ( params. destination_address . wrapping_add ( dest_address_length) ) as u8 ;
198
- dest_address_length += 1 ;
199
- }
200
- create_tx_params. dest_address_len = dest_address_length;
198
+ debug_print ( "==> GET_DESTINATION_ADDRESS\n " ) ;
199
+ let mut dest_address_length = 0usize ;
200
+ let mut c = unsafe { * params. destination_address . add ( dest_address_length) } ;
201
+ while c != '\0' as i8 && dest_address_length < ADDRESS_BUF_SIZE {
202
+ create_tx_params. dest_address [ dest_address_length] = c as u8 ;
203
+ dest_address_length += 1 ;
204
+ c = unsafe { * params. destination_address . add ( dest_address_length) } ;
205
+ }
206
+ create_tx_params. dest_address_len = dest_address_length;
201
207
202
- create_tx_params. result = & ( * ( libarg. __bindgen_anon_1 . create_transaction
203
- as * mut create_transaction_parameters_t ) )
204
- . result as * const u8 as * mut u8 ;
208
+ create_tx_params. result = unsafe {
209
+ & ( * ( libarg. __bindgen_anon_1 . create_transaction as * mut create_transaction_parameters_t ) )
210
+ . result as * const u8 as * mut u8
211
+ } ;
205
212
206
- /* Reset BSS and complete application boot */
213
+ /* Reset BSS and complete application boot */
214
+ unsafe {
207
215
c_reset_bss ( ) ;
208
216
c_boot_std ( ) ;
217
+ }
209
218
210
- #[ cfg( any( target_os = "stax" , target_os = "flex" ) ) ]
211
- NbglSpinner :: new ( ) . text ( "Signing" ) . show ( ) ;
219
+ #[ cfg( any( target_os = "stax" , target_os = "flex" ) ) ]
220
+ NbglSpinner :: new ( ) . text ( "Signing" ) . show ( ) ;
212
221
213
- create_tx_params
214
- }
222
+ create_tx_params
215
223
}
216
224
217
225
pub enum SwapResult < ' a > {
@@ -221,21 +229,23 @@ pub enum SwapResult<'a> {
221
229
}
222
230
223
231
pub fn swap_return ( res : SwapResult ) {
224
- unsafe {
225
- match res {
226
- SwapResult :: CheckAddressResult ( & mut ref p , r ) => {
227
- * ( p . result ) = r ;
228
- }
229
- SwapResult :: PrintableAmountResult ( & mut ref p , s ) => {
232
+ match res {
233
+ SwapResult :: CheckAddressResult ( & mut ref p , r ) => {
234
+ unsafe { * ( p . result ) = r } ;
235
+ }
236
+ SwapResult :: PrintableAmountResult ( & mut ref p , s ) => {
237
+ if s . len ( ) < ( MAX_PRINTABLE_AMOUNT_SIZE - 1 ) . try_into ( ) . unwrap ( ) {
230
238
for ( i, c) in s. chars ( ) . enumerate ( ) {
231
- * ( p. amount_str . add ( i) ) = c as i8 ;
239
+ unsafe { * ( p. amount_str . add ( i) ) = c as i8 } ;
232
240
}
233
- * ( p. amount_str . add ( s. len ( ) ) ) = '\0' as i8 ;
234
- }
235
- SwapResult :: CreateTxResult ( & mut ref p, r) => {
236
- * ( p. result ) = r;
241
+ unsafe { * ( p. amount_str . add ( s. len ( ) ) ) = '\0' as i8 } ;
242
+ } else {
243
+ unsafe { * ( p. amount_str ) = '\0' as i8 } ;
237
244
}
238
245
}
239
- ledger_secure_sdk_sys:: os_lib_end ( ) ;
246
+ SwapResult :: CreateTxResult ( & mut ref p, r) => {
247
+ unsafe { * ( p. result ) = r } ;
248
+ }
240
249
}
250
+ unsafe { ledger_secure_sdk_sys:: os_lib_end ( ) } ;
241
251
}
0 commit comments