git ssb

1+

Daan Patchwork / manyverse



Commit a56d419ca9247333f4feb40c7a963347719718f0

ux: auto export secrets to upcoming beta version

Andre Staltz committed on 9/20/2018, 10:08:05 PM
Parent: cae3efbc7e68ca868ed2de7c87f5c079d5f1233d

Files changed

android/app/src/main/AndroidManifest.xmlchanged
src/nodejs-project/index.tschanged
src/nodejs-project/export-secret.tsadded
src/nodejs-project/import-secret.tsadded
android/app/src/main/AndroidManifest.xmlView
@@ -7,8 +7,9 @@
77 <uses-permission android:name="android.permission.INTERNET" />
88 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
99 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
1010 <uses-permission android:name="android.permission.VIBRATE"/>
11+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1112
1213 <uses-sdk
1314 android:minSdkVersion="19"
1415 android:targetSdkVersion="22" />
src/nodejs-project/index.tsView
@@ -16,25 +16,44 @@
1616 * You should have received a copy of the GNU General Public License
1717 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1818 */
1919
20-const fs = require('fs');
20+import fs = require('fs');
2121 const path = require('path');
2222 const ssbKeys = require('ssb-keys');
2323 const mkdirp = require('mkdirp');
2424 const DHT = require('multiserver-dht');
2525 import syncingPlugin = require('./plugins/syncing');
2626 import manifest = require('./manifest');
27+import exportSecret = require('./export-secret');
28+import importSecret = require('./import-secret');
2729
2830 // 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');
3233 if (!fs.existsSync(ssbPath)) {
3334 mkdirp.sync(ssbPath);
3435 }
35-const keys = ssbKeys.loadOrCreateSync(path.join(ssbPath, '/secret'));
36+const keysPath = path.join(ssbPath, '/secret');
3637
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+
3756 const config = require('ssb-config/inject')();
3857 config.path = ssbPath;
3958 config.keys = keys;
4059 config.manifest = manifest;
src/nodejs-project/export-secret.tsView
@@ -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.tsView
@@ -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