Skip to content

Commit e7943e3

Browse files
authored
Merge pull request #7 from xtruan/develop
Crypto as lib & QR address gen
2 parents 8413b87 + 0150732 commit e7943e3

File tree

151 files changed

+324
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+324
-133
lines changed

.github/workflows/build.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
9+
env:
10+
firmware_version: '0.78.1'
11+
12+
jobs:
13+
build:
14+
name: Build
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout Flipper Zero Firmware
18+
uses: actions/checkout@v3
19+
with:
20+
repository: 'flipperdevices/flipperzero-firmware'
21+
ref: ${{ env.firmware_version }}
22+
submodules: true
23+
- name: Checkout FlipBIP
24+
uses: actions/checkout@v3
25+
with:
26+
path: 'applications_user/FlipBIP'
27+
- name: Build FAPs
28+
run: ./fbt COMPACT=1 DEBUG=0 faps

.github/workflows/release.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v[0-9]+.[0-9]+.[0-9]+'
7+
8+
env:
9+
firmware_version: '0.78.1'
10+
11+
jobs:
12+
build:
13+
name: Build
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout Flipper Zero Firmware
17+
uses: actions/checkout@v3
18+
with:
19+
repository: 'flipperdevices/flipperzero-firmware'
20+
ref: ${{ env.firmware_version }}
21+
submodules: true
22+
- name: Checkout FlipBIP
23+
uses: actions/checkout@v3
24+
with:
25+
path: 'applications_user/FlipBIP'
26+
- name: Build FAPs
27+
run: ./fbt COMPACT=1 DEBUG=0 faps
28+
- name: Get tag
29+
id: tag
30+
uses: dawidd6/action-get-tag@v1
31+
with:
32+
strip_v: false
33+
- name: Publish FlipBIP
34+
uses: softprops/action-gh-release@v1
35+
with:
36+
files: |
37+
build/f7-firmware-C/.extapps/FlipBIP.fap
38+
applications_user/FlipBIP/README.md
39+
name: ${{steps.tag.outputs.tag}}
40+
body: Built against Flipper Zero firmware v${{ env.firmware_version }}
41+
generate_release_notes: true
42+
fail_on_unmatched_files: true

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ The application will be compiled and copied onto your device
5454
- Import your own mnemonic
5555
- Lots of typing required but you can now use the wallet with an existing mnemonic you have saved
5656
- Useful to convert paper backup to keys and receive addresses without relying on a laptop or phone
57+
- Improved receive address generation features
58+
- Addresses are now generated at the same time as other pieces of wallet info
59+
- This slows down initial wallet load, but makes UI much more responsive
60+
- QR code files are now generated for each address and stored in the `apps_data/flipbip` directory
61+
- This app is required to view the QR code files: https://github.com/bmatcuk/flipperzero-qrcode (included in RM firmware)
62+
- NOTE: This happens during the `View Wallet` step; you must view a wallet after generating/importing a wallet in order to ensure the address QR files are correct
63+
- Broke out crypto functionality into its own library using `fap_private_libs` feature
5764

5865
### Work in Progress
5966

application.fam

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ App(
77
requires=[
88
"gui",
99
],
10-
stack_size=2 * 1024,
10+
stack_size=3 * 1024,
1111
order=10,
1212
fap_icon="flipbip_10px.png",
1313
fap_icon_assets="icons",
14+
fap_private_libs=[
15+
Lib(
16+
name="crypto",
17+
),
18+
],
1419
fap_category="Misc",
1520
fap_description="Crypto toolkit for Flipper",
1621
fap_author="Struan Clark (xtruan)",

flipbip.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "flipbip.h"
2-
#include "crypto/memzero.h"
3-
#include "crypto/bip39.h"
42
#include "helpers/flipbip_file.h"
53
#include "helpers/flipbip_haptic.h"
4+
// From: lib/crypto
5+
#include <memzero.h>
6+
#include <bip39.h>
67

78
bool flipbip_custom_event_callback(void* context, uint32_t event) {
89
furi_assert(context);
@@ -49,7 +50,7 @@ static void text_input_callback(void* context) {
4950
if(mnemonic_check(app->import_mnemonic_text) == 0)
5051
status = FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
5152
// Save the mnemonic to persistent storage
52-
else if(!flipbip_save_settings_secure(app->import_mnemonic_text))
53+
else if(!flipbip_save_file_secure(app->import_mnemonic_text))
5354
status = FlipBipStatusSaveError; // 12 = save error
5455

5556
if(status == FlipBipStatusSuccess) {

flipbip.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "views/flipbip_startscreen.h"
1616
#include "views/flipbip_scene_1.h"
1717

18-
#define FLIPBIP_VERSION "v0.0.6"
18+
#define FLIPBIP_VERSION "v0.0.7"
1919

2020
#define COIN_BTC 0
2121
#define COIN_DOGE 3
@@ -96,4 +96,4 @@ typedef enum {
9696
FlipBipStatusLoadError = 11,
9797
FlipBipStatusSaveError = 12,
9898
FlipBipStatusMnemonicCheckError = 13,
99-
} FlipBipStatus;
99+
} FlipBipStatus;

helpers/flipbip_file.c

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
#include "flipbip_file.h"
2-
#include "../helpers/flipbip_string.h"
3-
4-
#include "../crypto/memzero.h"
5-
#include "../crypto/rand.h"
6-
72
#include <storage/storage.h>
3+
#include <applications.h>
4+
#include <loader/loader.h>
5+
#include "../helpers/flipbip_string.h"
6+
// From: lib/crypto
7+
#include <memzero.h>
8+
#include <rand.h>
89

10+
// #define FLIPBIP_APP_BASE_FOLDER APP_DATA_PATH("flipbip")
911
#define FLIPBIP_APP_BASE_FOLDER EXT_PATH("apps_data/flipbip")
12+
#define FLIPBIP_APP_BASE_FOLDER_PATH(path) FLIPBIP_APP_BASE_FOLDER "/" path
1013
#define FLIPBIP_DAT_FILE_NAME ".flipbip.dat"
1114
// #define FLIPBIP_DAT_FILE_NAME ".flipbip.dat.txt"
1215
#define FLIPBIP_DAT_FILE_NAME_BAK ".flipbip.dat.bak"
1316
#define FLIPBIP_KEY_FILE_NAME ".flipbip.key"
1417
// #define FLIPBIP_KEY_FILE_NAME ".flipbip.key.txt"
1518
#define FLIPBIP_KEY_FILE_NAME_BAK ".flipbip.key.bak"
16-
#define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME
17-
#define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME_BAK
18-
#define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME
19-
#define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME_BAK
20-
21-
const size_t FILE_HLEN = 4;
22-
const size_t FILE_KLEN = 256;
23-
const size_t FILE_SLEN = 512;
19+
#define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME)
20+
#define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME_BAK)
21+
#define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME)
22+
#define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME_BAK)
23+
24+
const char* TEXT_QRFILE = "Filetype: QRCode\n"
25+
"Version: 0\n"
26+
"Message: "; // 37 chars + 1 null
27+
#define FILE_HLEN 4
28+
#define FILE_KLEN 256
29+
#define FILE_SLEN 512
30+
#define FILE_MAX_PATH_LEN 48
2431
const char* FILE_HSTR = "fb01";
2532
const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a"
2633
"baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";
2734

28-
bool flipbip_load_settings(char* settings, bool key_file) {
35+
bool flipbip_load_file(char* settings, const FlipBipFile file_type, const char* file_name) {
2936
bool ret = false;
3037
const char* path;
31-
if(key_file) {
38+
if(file_type == FlipBipFileKey) {
3239
path = FLIPBIP_KEY_PATH;
33-
} else {
40+
} else if(file_type == FlipBipFileDat) {
3441
path = FLIPBIP_DAT_PATH;
42+
} else {
43+
char path_buf[32] = {0};
44+
strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER);
45+
strcpy(path_buf + strlen(path_buf), "/");
46+
strcpy(path_buf + strlen(path_buf), file_name);
47+
path = path_buf;
3548
}
3649

3750
Storage* fs_api = furi_record_open(RECORD_STORAGE);
@@ -74,34 +87,53 @@ bool flipbip_load_settings(char* settings, bool key_file) {
7487
return ret;
7588
}
7689

77-
bool flipbip_has_settings(bool key_file) {
90+
bool flipbip_has_file(const FlipBipFile file_type, const char* file_name, const bool remove) {
7891
bool ret = false;
7992
const char* path;
80-
if(key_file) {
93+
if(file_type == FlipBipFileKey) {
8194
path = FLIPBIP_KEY_PATH;
82-
} else {
95+
} else if(file_type == FlipBipFileDat) {
8396
path = FLIPBIP_DAT_PATH;
97+
} else {
98+
char path_buf[32] = {0};
99+
strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER);
100+
strcpy(path_buf + strlen(path_buf), "/");
101+
strcpy(path_buf + strlen(path_buf), file_name);
102+
path = path_buf;
84103
}
85104

86105
Storage* fs_api = furi_record_open(RECORD_STORAGE);
87-
if(storage_file_exists(fs_api, path)) {
88-
ret = true;
106+
if(remove) {
107+
ret = storage_simply_remove(fs_api, path);
108+
} else {
109+
ret = storage_file_exists(fs_api, path);
89110
}
90111
furi_record_close(RECORD_STORAGE);
91112

92113
return ret;
93114
}
94115

95-
bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
116+
bool flipbip_save_file(
117+
const char* settings,
118+
const FlipBipFile file_type,
119+
const char* file_name,
120+
const bool append) {
96121
bool ret = false;
97122
const char* path;
98123
const char* path_bak;
99-
if(key_file) {
124+
if(file_type == FlipBipFileKey) {
100125
path = FLIPBIP_KEY_PATH;
101126
path_bak = FLIPBIP_KEY_PATH_BAK;
102-
} else {
127+
} else if(file_type == FlipBipFileDat) {
103128
path = FLIPBIP_DAT_PATH;
104129
path_bak = FLIPBIP_DAT_PATH_BAK;
130+
} else {
131+
char path_buf[FILE_MAX_PATH_LEN] = {0};
132+
strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER); // 22
133+
strcpy(path_buf + strlen(path_buf), "/");
134+
strcpy(path_buf + strlen(path_buf), file_name);
135+
path = path_buf;
136+
path_bak = NULL;
105137
}
106138
int open_mode = FSOM_OPEN_ALWAYS;
107139
if(append) {
@@ -116,7 +148,7 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
116148
// return ret;
117149
// }
118150
// try to create the folder
119-
storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
151+
storage_simply_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
120152

121153
File* settings_file = storage_file_alloc(fs_api);
122154
if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) {
@@ -127,28 +159,41 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
127159
storage_file_close(settings_file);
128160
storage_file_free(settings_file);
129161

130-
File* settings_file_bak = storage_file_alloc(fs_api);
131-
if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
132-
storage_file_write(settings_file_bak, settings, strlen(settings));
133-
storage_file_write(settings_file_bak, "\n", 1);
162+
if(path_bak != NULL) {
163+
File* settings_file_bak = storage_file_alloc(fs_api);
164+
if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
165+
storage_file_write(settings_file_bak, settings, strlen(settings));
166+
storage_file_write(settings_file_bak, "\n", 1);
167+
}
168+
storage_file_close(settings_file_bak);
169+
storage_file_free(settings_file_bak);
134170
}
135-
storage_file_close(settings_file_bak);
136-
storage_file_free(settings_file_bak);
137171

138172
furi_record_close(RECORD_STORAGE);
139173

140174
return ret;
141175
}
142176

143-
bool flipbip_load_settings_secure(char* settings) {
177+
bool flipbip_save_qrfile(
178+
const char* qr_msg_prefix,
179+
const char* qr_msg_content,
180+
const char* file_name) {
181+
char qr_buf[90] = {0};
182+
strcpy(qr_buf, TEXT_QRFILE);
183+
strcpy(qr_buf + strlen(qr_buf), qr_msg_prefix);
184+
strcpy(qr_buf + strlen(qr_buf), qr_msg_content);
185+
return flipbip_save_file(qr_buf, FlipBipFileOther, file_name, false);
186+
}
187+
188+
bool flipbip_load_file_secure(char* settings) {
144189
const size_t dlen = FILE_HLEN + FILE_SLEN + 1;
145190

146191
// allocate memory for key/data
147192
char* data = malloc(dlen);
148193
memzero(data, dlen);
149194

150195
// load k2 from file
151-
if(!flipbip_load_settings(data, true)) return false;
196+
if(!flipbip_load_file(data, FlipBipFileKey, NULL)) return false;
152197

153198
// check header
154199
if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] ||
@@ -174,7 +219,7 @@ bool flipbip_load_settings_secure(char* settings) {
174219
data -= FILE_HLEN;
175220

176221
// load data from file
177-
if(!flipbip_load_settings(data, false)) return false;
222+
if(!flipbip_load_file(data, FlipBipFileDat, NULL)) return false;
178223

179224
// check header
180225
if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] ||
@@ -207,7 +252,7 @@ bool flipbip_load_settings_secure(char* settings) {
207252
return true;
208253
}
209254

210-
bool flipbip_save_settings_secure(const char* settings) {
255+
bool flipbip_save_file_secure(const char* settings) {
211256
const size_t dlen = FILE_HLEN + FILE_SLEN + 1;
212257

213258
// cap settings to 256 bytes
@@ -238,7 +283,7 @@ bool flipbip_save_settings_secure(const char* settings) {
238283
// seek <-- header
239284
data -= FILE_HLEN;
240285
// save k2 to file
241-
flipbip_save_settings(data, true, false);
286+
flipbip_save_file(data, FlipBipFileKey, NULL, false);
242287
// seek --> header
243288
data += FILE_HLEN;
244289
// zero k2 memory
@@ -251,7 +296,7 @@ bool flipbip_save_settings_secure(const char* settings) {
251296
// seek <-- header
252297
data -= FILE_HLEN;
253298
// save data to file
254-
flipbip_save_settings(data, false, false);
299+
flipbip_save_file(data, FlipBipFileDat, NULL, false);
255300

256301
// clear memory
257302
memzero(data, dlen);
@@ -260,4 +305,4 @@ bool flipbip_save_settings_secure(const char* settings) {
260305
memzero(k2, FILE_KLEN / 2);
261306

262307
return true;
263-
}
308+
}

0 commit comments

Comments
 (0)