@@ -930,6 +930,9 @@ static gui_activity_t* make_export_xpub_prompt_activity(void) {
930930
931931static bool export_usb_xpub_fn (const usbstorage_action_context_t * ctx ) {
932932
933+ bool ok = false;
934+ char * fphex = NULL ;
935+ char * xpub = NULL ;
933936 uint32_t qr_flags = storage_get_qr_flags ();
934937
935938 while (true) {
@@ -943,19 +946,16 @@ static bool export_usb_xpub_fn(const usbstorage_action_context_t* ctx) {
943946 handle_xpub_options (& qr_flags );
944947 }
945948 else if (ev == BTN_SETTINGS_USBSTORAGE_EXPORT_XPUB_ACTION ) {
946-
947- goto DO_EXPORT ;
949+ break ;
948950 }
949951 else if (ev == BTN_SETTINGS_EXPORT_XPUB_BACK ) {
950- goto EXIT_EXPORT_XPUB ;
952+ return true ;
951953 }
952954 else {
953955 return false;
954956 }
955957
956958 }
957-
958- DO_EXPORT :
959959 const script_variant_t script_variant = xpub_script_variant_from_flags (qr_flags );
960960 const uint16_t account_index = qr_flags >> ACCOUNT_INDEX_FLAGS_SHIFT ;
961961
@@ -980,31 +980,34 @@ static bool export_usb_xpub_fn(const usbstorage_action_context_t* ctx) {
980980 EXPORT_XPUB_PATH_LEN ,
981981 & path_len );
982982
983- char * xpub = NULL ;
984983 network_t network_id ;
985984 if (keychain_get_network_type_restriction () == NETWORK_TYPE_TEST ) {
986985 network_id = NETWORK_BITCOIN_TESTNET ;
987986 } else {
988987 network_id = NETWORK_BITCOIN ;
989988 }
989+
990990 if (!wallet_get_xpub (network_id , path , path_len , & xpub ) || xpub == NULL ) {
991- //await_error_activity((const char*[]){"Unable to derive xpub"}, 1);
992- return false;
991+ const char * msg [] = { "unable to get" , "xpub from path" };
992+ await_error_activity (msg , 2 );
993+ goto cleanup ;
993994 }
994995
995996 char pathstr [MAX_PATH_STR_LEN (EXPORT_XPUB_PATH_LEN )];
996997 const bool ret = wallet_bip32_path_as_str (path , path_len , pathstr , sizeof (pathstr ));
997998
998999 JADE_ASSERT (ret );
9991000
1000- // get fingerprint
1001- // then take the whole string and put it together with
1002- // scriptvariant([fingerprint/84'/0'accountindex]xpub/0/*)
10031001 uint8_t user_fingerprint [BIP32_KEY_FINGERPRINT_LEN ];
10041002 wallet_get_fingerprint (user_fingerprint , sizeof (user_fingerprint ));
10051003
1006- char * fphex = NULL ;
1007- wally_hex_from_bytes (user_fingerprint , sizeof (user_fingerprint ), & fphex );
1004+ JADE_WALLY_VERIFY (wally_hex_from_bytes (user_fingerprint , sizeof (user_fingerprint ), & fphex ));
1005+ if (!fphex ) {
1006+ const char * msg [] = { "unable to get" , "fingerprint from wallet" };
1007+ await_error_activity (msg , 2 );
1008+ goto cleanup ;
1009+ };
1010+
10081011 map_string (fphex , toupper );
10091012
10101013 char descriptor [512 ];
@@ -1027,7 +1030,7 @@ static bool export_usb_xpub_fn(const usbstorage_action_context_t* ctx) {
10271030 if (written != (size_t )n ) {
10281031 const char * msg [] = { "Failed to save" , "xpub file" };
10291032 await_error_activity (msg , 2 );
1030- return false ;
1033+ goto cleanup ;
10311034 }
10321035
10331036 size_t mountlen = strlen (USBSTORAGE_MOUNT_POINT );
@@ -1036,9 +1039,11 @@ static bool export_usb_xpub_fn(const usbstorage_action_context_t* ctx) {
10361039 outpath + mountlen + 1
10371040 };
10381041 await_message_activity (msg , 2 );
1039- return true;
1040- EXIT_EXPORT_XPUB :
1041- return true;
1042+ ok = true;
1043+ cleanup :
1044+ if (xpub ) wally_free_string (xpub );
1045+ if (fphex ) wally_free_string (fphex );
1046+ return ok ;
10421047}
10431048
10441049bool usbstorage_export_xpub (const char * extra_path ) {
0 commit comments