Commit a56d419ca9247333f4feb40c7a963347719718f0
ux: auto export secrets to upcoming beta version
Andre Staltz committed on 9/20/2018, 10:08:05 PMParent: cae3efbc7e68ca868ed2de7c87f5c079d5f1233d
Files changed
android/app/src/main/AndroidManifest.xml | changed |
src/nodejs-project/index.ts | changed |
src/nodejs-project/export-secret.ts | added |
src/nodejs-project/import-secret.ts | added |
android/app/src/main/AndroidManifest.xml | ||
---|---|---|
@@ -7,8 +7,9 @@ | ||
7 | 7 | <uses-permission android:name="android.permission.INTERNET" /> |
8 | 8 | <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> |
9 | 9 | <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> |
10 | 10 | <uses-permission android:name="android.permission.VIBRATE"/> |
11 | + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
11 | 12 | |
12 | 13 | <uses-sdk |
13 | 14 | android:minSdkVersion="19" |
14 | 15 | android:targetSdkVersion="22" /> |
src/nodejs-project/index.ts | ||
---|---|---|
@@ -16,25 +16,44 @@ | ||
16 | 16 | * You should have received a copy of the GNU General Public License |
17 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 | */ |
19 | 19 | |
20 | -const fs = require('fs'); | |
20 | +import fs = require('fs'); | |
21 | 21 | const path = require('path'); |
22 | 22 | const ssbKeys = require('ssb-keys'); |
23 | 23 | const mkdirp = require('mkdirp'); |
24 | 24 | const DHT = require('multiserver-dht'); |
25 | 25 | import syncingPlugin = require('./plugins/syncing'); |
26 | 26 | import manifest = require('./manifest'); |
27 | +import exportSecret = require('./export-secret'); | |
28 | +import importSecret = require('./import-secret'); | |
27 | 29 | |
28 | 30 | // Hack until appDataDir plugin comes out |
29 | -const writablePath = path.join(__dirname, '..'); | |
30 | -const ssbPath = path.resolve(writablePath, '.ssb'); | |
31 | - | |
31 | +const appExclusivePath = path.join(__dirname, '..'); | |
32 | +const ssbPath = path.resolve(appExclusivePath, '.ssb'); | |
32 | 33 | if (!fs.existsSync(ssbPath)) { |
33 | 34 | mkdirp.sync(ssbPath); |
34 | 35 | } |
35 | -const keys = ssbKeys.loadOrCreateSync(path.join(ssbPath, '/secret')); | |
36 | +const keysPath = path.join(ssbPath, '/secret'); | |
36 | 37 | |
38 | +/** | |
39 | + * This helps us migrate secrets from one location to the other | |
40 | + * because app codename will change from alpha to beta. | |
41 | + */ | |
42 | +type ReleaseType = 'last-alpha' | 'first-beta' | 'other'; | |
43 | + | |
44 | +const releaseType: ReleaseType = 'last-alpha'; | |
45 | + | |
46 | +let keys: any; | |
47 | +if (releaseType === 'last-alpha') { | |
48 | + keys = ssbKeys.loadOrCreateSync(keysPath); | |
49 | + exportSecret(ssbPath, keys); | |
50 | +} else if (releaseType === 'first-beta') { | |
51 | + keys = importSecret(ssbPath, keysPath) || ssbKeys.loadOrCreateSync(keysPath); | |
52 | +} else { | |
53 | + keys = ssbKeys.loadOrCreateSync(keysPath); | |
54 | +} | |
55 | + | |
37 | 56 | const config = require('ssb-config/inject')(); |
38 | 57 | config.path = ssbPath; |
39 | 58 | config.keys = keys; |
40 | 59 | config.manifest = manifest; |
src/nodejs-project/export-secret.ts | ||
---|---|---|
@@ -1,0 +1,40 @@ | ||
1 | +/** | |
2 | + * MMMMM is a mobile app for Secure Scuttlebutt networks | |
3 | + * | |
4 | + * Copyright (C) 2017 Andre 'Staltz' Medeiros | |
5 | + * | |
6 | + * This program is free software: you can redistribute it and/or modify | |
7 | + * it under the terms of the GNU General Public License as published by | |
8 | + * the Free Software Foundation, either version 3 of the License, or | |
9 | + * (at your option) any later version. | |
10 | + * | |
11 | + * This program is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU General Public License | |
17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | + */ | |
19 | + | |
20 | +import fs = require('fs'); | |
21 | +const path = require('path'); | |
22 | + | |
23 | +export = function exportSecret(ssbPath: string, keys: any) { | |
24 | + try { | |
25 | + fs.writeFileSync( | |
26 | + '/storage/emulated/0/ssb-secret', | |
27 | + JSON.stringify(keys, null, 2), | |
28 | + 'ascii', | |
29 | + ); | |
30 | + const gossipPath = path.join(ssbPath, 'gossip.json'); | |
31 | + if (fs.existsSync(gossipPath)) { | |
32 | + const backupGossipPath = '/storage/emulated/0/ssb-gossip.json'; | |
33 | + const gossipContents = fs.readFileSync(gossipPath, 'utf-8'); | |
34 | + fs.writeFileSync(backupGossipPath, gossipContents, 'utf-8'); | |
35 | + } | |
36 | + console.warn('Export of ssb-secret succeeded.'); | |
37 | + } catch (err) { | |
38 | + console.warn('Export of ssb-secret failed.'); | |
39 | + } | |
40 | +}; |
src/nodejs-project/import-secret.ts | ||
---|---|---|
@@ -1,0 +1,47 @@ | ||
1 | +/** | |
2 | + * MMMMM is a mobile app for Secure Scuttlebutt networks | |
3 | + * | |
4 | + * Copyright (C) 2017 Andre 'Staltz' Medeiros | |
5 | + * | |
6 | + * This program is free software: you can redistribute it and/or modify | |
7 | + * it under the terms of the GNU General Public License as published by | |
8 | + * the Free Software Foundation, either version 3 of the License, or | |
9 | + * (at your option) any later version. | |
10 | + * | |
11 | + * This program is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU General Public License | |
17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | + */ | |
19 | + | |
20 | +import fs = require('fs'); | |
21 | +const path = require('path'); | |
22 | + | |
23 | +export = function importSecret(ssbPath: string, keysPath: string): any { | |
24 | + try { | |
25 | + if (fs.existsSync(keysPath)) { | |
26 | + console.warn('Import of ssb-secret ignored, we already have keys.'); | |
27 | + return null; | |
28 | + } | |
29 | + const originKeysPath = '/storage/emulated/0/ssb-secret'; | |
30 | + const keysContents = fs.readFileSync(originKeysPath, 'ascii'); | |
31 | + fs.writeFileSync(keysPath, keysContents, 'ascii'); | |
32 | + fs.unlinkSync(originKeysPath); | |
33 | + | |
34 | + const originGossipPath = '/storage/emulated/0/ssb-gossip.json'; | |
35 | + const gossipPath = path.join(ssbPath, 'gossip.json'); | |
36 | + if (fs.existsSync(originGossipPath) && !fs.existsSync(gossipPath)) { | |
37 | + const gossipContents = fs.readFileSync(originGossipPath, 'utf-8'); | |
38 | + fs.writeFileSync(gossipPath, gossipContents, 'utf-8'); | |
39 | + fs.unlinkSync(originGossipPath); | |
40 | + } | |
41 | + console.warn('Import of ssb-secret succeeded.'); | |
42 | + return JSON.parse(keysContents); | |
43 | + } catch (err) { | |
44 | + console.warn('Import of ssb-secret failed.'); | |
45 | + return null; | |
46 | + } | |
47 | +}; |
Built with git-ssb-web