flipbip_scene_1.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. #include "../flipbip.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. //#include <dolphin/dolphin.h>
  7. #include <storage/storage.h>
  8. #include <string.h>
  9. #include "FlipBIP_icons.h"
  10. #include "../helpers/flipbip_haptic.h"
  11. #include "../helpers/flipbip_led.h"
  12. #include "../helpers/flipbip_string.h"
  13. #include "../helpers/flipbip_file.h"
  14. // From: /lib/crypto
  15. #include <memzero.h>
  16. #include <rand.h>
  17. #include <curves.h>
  18. #include <bip32.h>
  19. #include <bip39.h>
  20. #define DERIV_PURPOSE 44
  21. #define DERIV_ACCOUNT 0
  22. #define DERIV_CHANGE 0
  23. #define MAX_TEXT_LEN 30 // 30 = max length of text
  24. #define MAX_TEXT_BUF (MAX_TEXT_LEN + 1) // max length of text + null terminator
  25. #define MAX_ADDR_BUF (42 + 1) // 42 = max length of address + null terminator
  26. #define NUM_ADDRS 6
  27. #define PAGE_LOADING 0
  28. #define PAGE_INFO 1
  29. #define PAGE_MNEMONIC 2
  30. #define PAGE_SEED 3
  31. #define PAGE_XPRV_ROOT 4
  32. #define PAGE_XPRV_ACCT 5
  33. #define PAGE_XPUB_ACCT 6
  34. #define PAGE_XPRV_EXTD 7
  35. #define PAGE_XPUB_EXTD 8
  36. #define PAGE_ADDR_BEGIN 9
  37. #define PAGE_ADDR_END (PAGE_ADDR_BEGIN + NUM_ADDRS - 1)
  38. #define TEXT_LOADING "Loading..."
  39. #define TEXT_NEW_WALLET "New wallet"
  40. #define TEXT_DEFAULT_COIN "Coin"
  41. #define TEXT_RECEIVE_ADDRESS "receive address:"
  42. #define TEXT_DEFAULT_DERIV "m/44'/X'/0'/0"
  43. const char* TEXT_INFO = "-Scroll pages with up/down-"
  44. "p1,2) BIP39 Mnemonic/Seed"
  45. "p3) BIP32 Root Key "
  46. "p4,5) Prv/Pub Account Keys"
  47. "p6,7) Prv/Pub BIP32 Keys "
  48. "p8+) Receive Addresses ";
  49. // #define TEXT_SAVE_QR "Save QR"
  50. #define TEXT_QRFILE_EXT ".qrcode" // 7 chars + 1 null
  51. // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
  52. const uint32_t COIN_INFO_ARRAY[3][6] = {
  53. {COIN_BTC, 0x0488ade4, 0x0488b21e, 0x00, 0x80, FlipBipCoinBTC0},
  54. {COIN_ETH, 0x0488ade4, 0x0488b21e, 0x00, 0x80, FlipBipCoinETH60},
  55. {COIN_DOGE, 0x02fac398, 0x02facafd, 0x1e, 0x9e, FlipBipCoinBTC0}};
  56. // coin_name, derivation_path
  57. const char* COIN_TEXT_ARRAY[3][3] = {
  58. {"BTC", "m/44'/0'/0'/0", "bitcoin:"},
  59. {"ETH", "m/44'/60'/0'/0", "ethereum:"},
  60. {"DOGE", "m/44'/3'/0'/0", "dogecoin:"}};
  61. struct FlipBipScene1 {
  62. View* view;
  63. FlipBipScene1Callback callback;
  64. void* context;
  65. };
  66. typedef struct {
  67. int page;
  68. int strength;
  69. uint32_t coin;
  70. bool overwrite;
  71. bool mnemonic_only;
  72. CONFIDENTIAL const char* mnemonic;
  73. CONFIDENTIAL uint8_t seed[64];
  74. CONFIDENTIAL const HDNode* node;
  75. CONFIDENTIAL const char* xprv_root;
  76. CONFIDENTIAL const char* xprv_account;
  77. CONFIDENTIAL const char* xpub_account;
  78. CONFIDENTIAL const char* xprv_extended;
  79. CONFIDENTIAL const char* xpub_extended;
  80. char* recv_addresses[NUM_ADDRS];
  81. } FlipBipScene1Model;
  82. // Node for the receive address
  83. static CONFIDENTIAL HDNode* s_addr_node = NULL;
  84. // Generic display text
  85. static CONFIDENTIAL char* s_disp_text1 = NULL;
  86. static CONFIDENTIAL char* s_disp_text2 = NULL;
  87. static CONFIDENTIAL char* s_disp_text3 = NULL;
  88. static CONFIDENTIAL char* s_disp_text4 = NULL;
  89. static CONFIDENTIAL char* s_disp_text5 = NULL;
  90. static CONFIDENTIAL char* s_disp_text6 = NULL;
  91. // Derivation path text
  92. static const char* s_derivation_text = TEXT_DEFAULT_DERIV;
  93. //static bool s_busy = false;
  94. void flipbip_scene_1_set_callback(
  95. FlipBipScene1* instance,
  96. FlipBipScene1Callback callback,
  97. void* context) {
  98. furi_assert(instance);
  99. furi_assert(callback);
  100. instance->callback = callback;
  101. instance->context = context;
  102. }
  103. static void flipbip_scene_1_init_address(
  104. char* addr_text,
  105. const HDNode* node,
  106. uint32_t coin_type,
  107. uint32_t addr_index) {
  108. //s_busy = true;
  109. // buffer for address serialization
  110. // subtract 2 for "0x", 1 for null terminator
  111. const size_t buflen = MAX_ADDR_BUF - (2 + 1);
  112. // subtract 2 for "0x"
  113. char buf[MAX_ADDR_BUF - 2] = {0};
  114. // Use static node for address generation
  115. memcpy(s_addr_node, node, sizeof(HDNode));
  116. memzero(addr_text, MAX_ADDR_BUF);
  117. hdnode_private_ckd(s_addr_node, addr_index);
  118. hdnode_fill_public_key(s_addr_node);
  119. // coin info
  120. // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
  121. uint32_t coin_info[6] = {0};
  122. for(size_t i = 0; i < 6; i++) {
  123. coin_info[i] = COIN_INFO_ARRAY[coin_type][i];
  124. }
  125. if(coin_info[5] == FlipBipCoinBTC0) { // BTC / DOGE style address
  126. // BTC / DOGE style address
  127. ecdsa_get_address(
  128. s_addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  129. strcpy(addr_text, buf);
  130. //ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
  131. } else if(coin_info[5] == FlipBipCoinETH60) { // ETH
  132. // ETH style address
  133. hdnode_get_ethereum_pubkeyhash(s_addr_node, (uint8_t*)buf);
  134. addr_text[0] = '0';
  135. addr_text[1] = 'x';
  136. // Convert the hash to a hex string
  137. flipbip_btox((uint8_t*)buf, 20, addr_text + 2);
  138. }
  139. // Clear the address node
  140. memzero(s_addr_node, sizeof(HDNode));
  141. //s_busy = false;
  142. }
  143. static void
  144. flipbip_scene_1_draw_generic(const char* text, const size_t line_len, const bool chunk) {
  145. // Split the text into parts
  146. size_t len = line_len;
  147. if(len > MAX_TEXT_LEN) {
  148. len = MAX_TEXT_LEN;
  149. }
  150. for(size_t si = 1; si <= 6; si++) {
  151. char* ptr = NULL;
  152. if(si == 1)
  153. ptr = s_disp_text1;
  154. else if(si == 2)
  155. ptr = s_disp_text2;
  156. else if(si == 3)
  157. ptr = s_disp_text3;
  158. else if(si == 4)
  159. ptr = s_disp_text4;
  160. else if(si == 5)
  161. ptr = s_disp_text5;
  162. else if(si == 6)
  163. ptr = s_disp_text6;
  164. memzero(ptr, MAX_TEXT_BUF);
  165. strncpy(ptr, text + ((si - 1) * len), len);
  166. // add a space every 4 characters and shift the text
  167. if(len < 23 && chunk) {
  168. for(size_t i = 0; i < strlen(ptr); i++) {
  169. if(i % 5 == 0) {
  170. for(size_t j = strlen(ptr); j > i; j--) {
  171. ptr[j] = ptr[j - 1];
  172. }
  173. ptr[i] = ' ';
  174. }
  175. }
  176. }
  177. }
  178. }
  179. static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
  180. // Delineate sections of the mnemonic every 4 words
  181. const size_t mnemonic_working_len = strlen(mnemonic) + 1;
  182. char* mnemonic_working = malloc(mnemonic_working_len);
  183. strcpy(mnemonic_working, mnemonic);
  184. int word = 0;
  185. for(size_t i = 0; i < strlen(mnemonic_working); i++) {
  186. if(mnemonic_working[i] == ' ') {
  187. word++;
  188. if(word % 4 == 0) {
  189. mnemonic_working[i] = ',';
  190. }
  191. }
  192. }
  193. // Split the mnemonic into parts
  194. char* mnemonic_part = flipbip_strtok(mnemonic_working, ",");
  195. int mi = 0;
  196. while(mnemonic_part != NULL) {
  197. char* ptr = NULL;
  198. mi++;
  199. if(mi == 1)
  200. ptr = s_disp_text1;
  201. else if(mi == 2)
  202. ptr = s_disp_text2;
  203. else if(mi == 3)
  204. ptr = s_disp_text3;
  205. else if(mi == 4)
  206. ptr = s_disp_text4;
  207. else if(mi == 5)
  208. ptr = s_disp_text5;
  209. else if(mi == 6)
  210. ptr = s_disp_text6;
  211. memzero(ptr, MAX_TEXT_BUF);
  212. if(strlen(mnemonic_part) > MAX_TEXT_LEN) {
  213. strncpy(ptr, mnemonic_part, MAX_TEXT_LEN);
  214. } else {
  215. strncpy(ptr, mnemonic_part, strlen(mnemonic_part));
  216. }
  217. mnemonic_part = flipbip_strtok(NULL, ",");
  218. }
  219. // Free the working mnemonic memory
  220. memzero(mnemonic_working, mnemonic_working_len);
  221. free(mnemonic_working);
  222. }
  223. static void flipbip_scene_1_draw_seed(FlipBipScene1Model* const model) {
  224. const size_t seed_working_len = 64 * 2 + 1;
  225. char* seed_working = malloc(seed_working_len);
  226. // Convert the seed to a hex string
  227. flipbip_btox(model->seed, 64, seed_working);
  228. flipbip_scene_1_draw_generic(seed_working, 22, false);
  229. // Free the working seed memory
  230. memzero(seed_working, seed_working_len);
  231. free(seed_working);
  232. }
  233. static void flipbip_scene_1_clear_text() {
  234. memzero((void*)s_disp_text1, MAX_TEXT_BUF);
  235. memzero((void*)s_disp_text2, MAX_TEXT_BUF);
  236. memzero((void*)s_disp_text3, MAX_TEXT_BUF);
  237. memzero((void*)s_disp_text4, MAX_TEXT_BUF);
  238. memzero((void*)s_disp_text5, MAX_TEXT_BUF);
  239. memzero((void*)s_disp_text6, MAX_TEXT_BUF);
  240. }
  241. void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
  242. //UNUSED(model);
  243. canvas_clear(canvas);
  244. canvas_set_color(canvas, ColorBlack);
  245. flipbip_scene_1_clear_text();
  246. if(model->page == PAGE_INFO) {
  247. flipbip_scene_1_draw_generic(TEXT_INFO, 27, false);
  248. } else if(model->page == PAGE_MNEMONIC) {
  249. flipbip_scene_1_draw_mnemonic(model->mnemonic);
  250. } else if(model->page == PAGE_SEED) {
  251. flipbip_scene_1_draw_seed(model);
  252. } else if(model->page == PAGE_XPRV_ROOT) {
  253. flipbip_scene_1_draw_generic(model->xprv_root, 20, false);
  254. } else if(model->page == PAGE_XPRV_ACCT) {
  255. flipbip_scene_1_draw_generic(model->xprv_account, 20, false);
  256. } else if(model->page == PAGE_XPUB_ACCT) {
  257. flipbip_scene_1_draw_generic(model->xpub_account, 20, false);
  258. } else if(model->page == PAGE_XPRV_EXTD) {
  259. flipbip_scene_1_draw_generic(model->xprv_extended, 20, false);
  260. } else if(model->page == PAGE_XPUB_EXTD) {
  261. flipbip_scene_1_draw_generic(model->xpub_extended, 20, false);
  262. } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  263. size_t line_len = 12;
  264. if(model->coin == FlipBipCoinETH60) {
  265. line_len = 14;
  266. }
  267. flipbip_scene_1_draw_generic(
  268. model->recv_addresses[model->page - PAGE_ADDR_BEGIN], line_len, true);
  269. }
  270. if(model->page == PAGE_LOADING) {
  271. canvas_set_font(canvas, FontPrimary);
  272. canvas_draw_str(canvas, 2, 10, TEXT_LOADING);
  273. canvas_draw_str(canvas, 7, 30, s_derivation_text);
  274. canvas_draw_icon(canvas, 86, 25, &I_Keychain_39x36);
  275. } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  276. // draw address header
  277. canvas_set_font(canvas, FontSecondary);
  278. // coin_name, derivation_path
  279. const char* receive_text = COIN_TEXT_ARRAY[model->coin][0];
  280. if(receive_text == NULL) {
  281. receive_text = TEXT_DEFAULT_COIN;
  282. }
  283. const size_t receive_len = strlen(receive_text) * 7;
  284. canvas_draw_str_aligned(canvas, 2, 2, AlignLeft, AlignTop, receive_text);
  285. canvas_draw_str_aligned(
  286. canvas, receive_len + 1, 2, AlignLeft, AlignTop, TEXT_RECEIVE_ADDRESS);
  287. // draw address number
  288. const unsigned char addr_num[1] = {(unsigned char)(model->page - PAGE_ADDR_BEGIN)};
  289. char addr_num_text[3] = {0};
  290. flipbip_btox(addr_num, 1, addr_num_text);
  291. addr_num_text[0] = '/';
  292. canvas_draw_str_aligned(canvas, 125, 2, AlignRight, AlignTop, addr_num_text);
  293. // draw QR code file path
  294. char addr_name_text[14] = {0};
  295. strcpy(addr_name_text, COIN_TEXT_ARRAY[model->coin][0]);
  296. flipbip_btox(addr_num, 1, addr_name_text + strlen(addr_name_text));
  297. strcpy(addr_name_text + strlen(addr_name_text), TEXT_QRFILE_EXT);
  298. //elements_button_right(canvas, addr_name_text);
  299. canvas_draw_str_aligned(canvas, 125, 53, AlignRight, AlignTop, addr_name_text);
  300. // draw address
  301. canvas_set_font(canvas, FontPrimary);
  302. canvas_draw_str(canvas, 7, 22, s_disp_text1);
  303. canvas_draw_str(canvas, 7, 34, s_disp_text2);
  304. canvas_draw_str(canvas, 7, 46, s_disp_text3);
  305. canvas_draw_str(canvas, 7, 58, s_disp_text4);
  306. } else {
  307. canvas_set_font(canvas, FontSecondary);
  308. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, s_disp_text1);
  309. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, s_disp_text2);
  310. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, s_disp_text3);
  311. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, s_disp_text4);
  312. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, s_disp_text5);
  313. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, s_disp_text6);
  314. }
  315. }
  316. static int flipbip_scene_1_model_init(
  317. FlipBipScene1Model* const model,
  318. const int strength,
  319. const uint32_t coin,
  320. const bool overwrite,
  321. const char* passphrase_text) {
  322. model->page = PAGE_LOADING;
  323. model->mnemonic_only = false;
  324. model->strength = strength;
  325. model->coin = coin;
  326. model->overwrite = overwrite;
  327. // Allocate memory for mnemonic
  328. char* mnemonic = malloc(TEXT_BUFFER_SIZE);
  329. memzero(mnemonic, TEXT_BUFFER_SIZE);
  330. // Check if the mnemonic key & data is already saved in persistent storage, or overwrite is true
  331. if(overwrite || (!flipbip_has_file(FlipBipFileKey, NULL, false) &&
  332. !flipbip_has_file(FlipBipFileDat, NULL, false))) {
  333. // Set mnemonic only mode
  334. model->mnemonic_only = true;
  335. // Generate a random mnemonic using trezor-crypto
  336. const char* mnemonic_gen = mnemonic_generate(strength);
  337. // Check if the mnemonic is valid
  338. if(mnemonic_check(mnemonic_gen) == 0)
  339. return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  340. // Save the mnemonic to persistent storage
  341. else if(!flipbip_save_file_secure(mnemonic_gen))
  342. return FlipBipStatusSaveError; // 12 = save error
  343. // Clear the generated mnemonic from memory
  344. mnemonic_clear();
  345. }
  346. // Load the mnemonic from persistent storage
  347. if(!flipbip_load_file_secure(mnemonic)) {
  348. // Set mnemonic only mode for this error for memory cleanup purposes
  349. model->mnemonic_only = true;
  350. return FlipBipStatusLoadError; // 11 = load error
  351. }
  352. model->mnemonic = mnemonic;
  353. // Check if the mnemonic is valid
  354. if(mnemonic_check(model->mnemonic) == 0) {
  355. // Set mnemonic only mode for this error for memory cleanup purposes
  356. model->mnemonic_only = true;
  357. return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  358. }
  359. // test return values
  360. //model->mnemonic_only = true;
  361. //return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  362. // if we are only generating the mnemonic, return
  363. if(model->mnemonic_only) {
  364. return FlipBipStatusReturn; // 10 = mnemonic only, return from parent
  365. }
  366. // Generate a BIP39 seed from the mnemonic
  367. mnemonic_to_seed(model->mnemonic, passphrase_text, model->seed, 0);
  368. // Generate a BIP32 root HD node from the mnemonic
  369. HDNode* root = malloc(sizeof(HDNode));
  370. hdnode_from_seed(model->seed, 64, SECP256K1_NAME, root);
  371. // buffer for key serialization
  372. const size_t buflen = 128;
  373. char buf[128 + 1] = {0};
  374. // coin info
  375. // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
  376. uint32_t coin_info[6] = {0};
  377. for(size_t i = 0; i < 6; i++) {
  378. coin_info[i] = COIN_INFO_ARRAY[coin][i];
  379. }
  380. // root
  381. uint32_t fingerprint = 0;
  382. hdnode_serialize_private(root, fingerprint, coin_info[1], buf, buflen);
  383. char* xprv_root = malloc(buflen + 1);
  384. strncpy(xprv_root, buf, buflen);
  385. model->xprv_root = xprv_root;
  386. HDNode* node = root;
  387. // purpose m/44'
  388. fingerprint = hdnode_fingerprint(node);
  389. hdnode_private_ckd_prime(node, DERIV_PURPOSE); // purpose
  390. // coin m/44'/0' or m/44'/60'
  391. fingerprint = hdnode_fingerprint(node);
  392. hdnode_private_ckd_prime(node, coin_info[0]); // coin
  393. // account m/44'/0'/0' or m/44'/60'/0'
  394. fingerprint = hdnode_fingerprint(node);
  395. hdnode_private_ckd_prime(node, DERIV_ACCOUNT); // account
  396. hdnode_serialize_private(node, fingerprint, coin_info[1], buf, buflen);
  397. char* xprv_acc = malloc(buflen + 1);
  398. strncpy(xprv_acc, buf, buflen);
  399. model->xprv_account = xprv_acc;
  400. hdnode_serialize_public(node, fingerprint, coin_info[2], buf, buflen);
  401. char* xpub_acc = malloc(buflen + 1);
  402. strncpy(xpub_acc, buf, buflen);
  403. model->xpub_account = xpub_acc;
  404. // external/internal (change) m/44'/0'/0'/0 or m/44'/60'/0'/0
  405. fingerprint = hdnode_fingerprint(node);
  406. hdnode_private_ckd(node, DERIV_CHANGE); // external/internal (change)
  407. hdnode_serialize_private(node, fingerprint, coin_info[1], buf, buflen);
  408. char* xprv_ext = malloc(buflen + 1);
  409. strncpy(xprv_ext, buf, buflen);
  410. model->xprv_extended = xprv_ext;
  411. hdnode_serialize_public(node, fingerprint, coin_info[2], buf, buflen);
  412. char* xpub_ext = malloc(buflen + 1);
  413. strncpy(xpub_ext, buf, buflen);
  414. model->xpub_extended = xpub_ext;
  415. model->node = node;
  416. // Initialize addresses
  417. for(uint8_t a = 0; a < NUM_ADDRS; a++) {
  418. model->recv_addresses[a] = malloc(MAX_ADDR_BUF);
  419. memzero(model->recv_addresses[a], MAX_ADDR_BUF);
  420. flipbip_scene_1_init_address(model->recv_addresses[a], node, coin, a);
  421. // Save QR code file
  422. memzero(buf, buflen);
  423. strcpy(buf, COIN_TEXT_ARRAY[coin][0]);
  424. const unsigned char addr_num[1] = {a};
  425. flipbip_btox(addr_num, 1, buf + strlen(buf));
  426. strcpy(buf + strlen(buf), TEXT_QRFILE_EXT);
  427. flipbip_save_qrfile(COIN_TEXT_ARRAY[coin][2], model->recv_addresses[a], buf);
  428. memzero(buf, buflen);
  429. }
  430. model->page = PAGE_INFO;
  431. #if USE_BIP39_CACHE
  432. // Clear the BIP39 cache
  433. bip39_cache_clear();
  434. #endif
  435. // 0 = success
  436. return FlipBipStatusSuccess;
  437. }
  438. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  439. furi_assert(context);
  440. FlipBipScene1* instance = context;
  441. // Ignore input if busy
  442. // if(s_busy) {
  443. // return false;
  444. // }
  445. if(event->type == InputTypeRelease) {
  446. switch(event->key) {
  447. case InputKeyBack:
  448. with_view_model(
  449. instance->view,
  450. FlipBipScene1Model * model,
  451. {
  452. UNUSED(model);
  453. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  454. },
  455. true);
  456. break;
  457. case InputKeyRight:
  458. case InputKeyDown:
  459. with_view_model(
  460. instance->view,
  461. FlipBipScene1Model * model,
  462. {
  463. //UNUSED(model);
  464. int page = (model->page + 1) % (PAGE_ADDR_END + 1);
  465. if(page == 0) {
  466. page = PAGE_INFO;
  467. }
  468. model->page = page;
  469. },
  470. true);
  471. break;
  472. case InputKeyLeft:
  473. case InputKeyUp:
  474. with_view_model(
  475. instance->view,
  476. FlipBipScene1Model * model,
  477. {
  478. //UNUSED(model);
  479. int page = (model->page - 1) % (PAGE_ADDR_END + 1);
  480. if(page == 0) {
  481. page = PAGE_ADDR_END;
  482. }
  483. model->page = page;
  484. },
  485. true);
  486. break;
  487. // case InputKeyRight:
  488. case InputKeyOk:
  489. // with_view_model(
  490. // instance->view,
  491. // FlipBipScene1Model * model,
  492. // {
  493. // if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  494. // }
  495. // },
  496. // true);
  497. // break;
  498. // case InputKeyLeft:
  499. case InputKeyMAX:
  500. break;
  501. }
  502. }
  503. return true;
  504. }
  505. void flipbip_scene_1_exit(void* context) {
  506. furi_assert(context);
  507. FlipBipScene1* instance = (FlipBipScene1*)context;
  508. with_view_model(
  509. instance->view,
  510. FlipBipScene1Model * model,
  511. {
  512. model->page = PAGE_LOADING;
  513. model->strength = FlipBipStrength256;
  514. model->coin = FlipBipCoinBTC0;
  515. memzero(model->seed, 64);
  516. // if mnemonic_only is true, then we don't need to free the data here
  517. if(!model->mnemonic_only) {
  518. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  519. free((void*)model->mnemonic);
  520. memzero((void*)model->node, sizeof(HDNode));
  521. free((void*)model->node);
  522. memzero((void*)model->xprv_root, strlen(model->xprv_root));
  523. memzero((void*)model->xprv_account, strlen(model->xprv_account));
  524. memzero((void*)model->xpub_account, strlen(model->xpub_account));
  525. memzero((void*)model->xprv_extended, strlen(model->xprv_extended));
  526. memzero((void*)model->xpub_extended, strlen(model->xpub_extended));
  527. free((void*)model->xprv_root);
  528. free((void*)model->xprv_account);
  529. free((void*)model->xpub_account);
  530. free((void*)model->xprv_extended);
  531. free((void*)model->xpub_extended);
  532. for(int a = 0; a < NUM_ADDRS; a++) {
  533. memzero((void*)model->recv_addresses[a], MAX_ADDR_BUF);
  534. free((void*)model->recv_addresses[a]);
  535. }
  536. }
  537. },
  538. true);
  539. flipbip_scene_1_clear_text();
  540. }
  541. void flipbip_scene_1_enter(void* context) {
  542. furi_assert(context);
  543. FlipBipScene1* instance = (FlipBipScene1*)context;
  544. FlipBip* app = instance->context;
  545. // BIP39 Strength setting
  546. int strength = 256; // FlipBipStrength256 // 24 words (256 bit)
  547. if(app->bip39_strength == FlipBipStrength128) {
  548. strength = 128; // 12 words (128 bit)
  549. } else if(app->bip39_strength == FlipBipStrength192) {
  550. strength = 192; // 18 words (192 bit)
  551. }
  552. // BIP39 Passphrase setting
  553. const char* passphrase_text = "";
  554. if(app->passphrase == FlipBipPassphraseOn && strlen(app->passphrase_text) > 0) {
  555. passphrase_text = app->passphrase_text;
  556. }
  557. // BIP44 Coin setting
  558. const uint32_t coin = app->bip44_coin;
  559. // coin_name, derivation_path
  560. s_derivation_text = COIN_TEXT_ARRAY[coin][1];
  561. // Overwrite the saved seed with a new one setting
  562. bool overwrite = app->overwrite_saved_seed != 0;
  563. if(overwrite) {
  564. s_derivation_text = TEXT_NEW_WALLET;
  565. }
  566. flipbip_play_happy_bump(app);
  567. //notification_message(app->notification, &sequence_blink_cyan_100);
  568. flipbip_led_set_rgb(app, 255, 0, 0);
  569. with_view_model(
  570. instance->view,
  571. FlipBipScene1Model * model,
  572. {
  573. // s_busy = true;
  574. const int status =
  575. flipbip_scene_1_model_init(model, strength, coin, overwrite, passphrase_text);
  576. // nonzero status, free the mnemonic
  577. if(status != FlipBipStatusSuccess) {
  578. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  579. free((void*)model->mnemonic);
  580. }
  581. // if error, set the error message
  582. if(status == FlipBipStatusSaveError) {
  583. model->mnemonic = "ERROR:,Save error";
  584. model->page = PAGE_MNEMONIC;
  585. flipbip_play_long_bump(app);
  586. } else if(status == FlipBipStatusLoadError) {
  587. model->mnemonic = "ERROR:,Load error";
  588. model->page = PAGE_MNEMONIC;
  589. flipbip_play_long_bump(app);
  590. } else if(status == FlipBipStatusMnemonicCheckError) {
  591. model->mnemonic = "ERROR:,Mnemonic check error";
  592. model->page = PAGE_MNEMONIC;
  593. flipbip_play_long_bump(app);
  594. }
  595. // s_busy = false;
  596. // if overwrite is set and mnemonic generated, return from scene immediately
  597. if(status == FlipBipStatusReturn) {
  598. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  599. }
  600. },
  601. true);
  602. }
  603. FlipBipScene1* flipbip_scene_1_alloc() {
  604. FlipBipScene1* instance = malloc(sizeof(FlipBipScene1));
  605. instance->view = view_alloc();
  606. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBipScene1Model));
  607. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  608. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip_scene_1_draw);
  609. view_set_input_callback(instance->view, flipbip_scene_1_input);
  610. view_set_enter_callback(instance->view, flipbip_scene_1_enter);
  611. view_set_exit_callback(instance->view, flipbip_scene_1_exit);
  612. // allocate the address node
  613. s_addr_node = (HDNode*)malloc(sizeof(HDNode));
  614. // allocate the display text
  615. s_disp_text1 = (char*)malloc(MAX_TEXT_BUF);
  616. s_disp_text2 = (char*)malloc(MAX_TEXT_BUF);
  617. s_disp_text3 = (char*)malloc(MAX_TEXT_BUF);
  618. s_disp_text4 = (char*)malloc(MAX_TEXT_BUF);
  619. s_disp_text5 = (char*)malloc(MAX_TEXT_BUF);
  620. s_disp_text6 = (char*)malloc(MAX_TEXT_BUF);
  621. return instance;
  622. }
  623. void flipbip_scene_1_free(FlipBipScene1* instance) {
  624. furi_assert(instance);
  625. with_view_model(
  626. instance->view, FlipBipScene1Model * model, { UNUSED(model); }, true);
  627. // free the address node
  628. memzero(s_addr_node, sizeof(HDNode));
  629. free(s_addr_node);
  630. // free the display text
  631. flipbip_scene_1_clear_text();
  632. free(s_disp_text1);
  633. free(s_disp_text2);
  634. free(s_disp_text3);
  635. free(s_disp_text4);
  636. free(s_disp_text5);
  637. free(s_disp_text6);
  638. view_free(instance->view);
  639. free(instance);
  640. }
  641. View* flipbip_scene_1_get_view(FlipBipScene1* instance) {
  642. furi_assert(instance);
  643. return instance->view;
  644. }