add some data code

This commit is contained in:
DASHU
2024-08-27 19:06:39 +08:00
parent 54eac18be1
commit 46c1521d32
17 changed files with 291 additions and 1 deletions

7
lib/const/app_type.dart Normal file
View File

@@ -0,0 +1,7 @@
class AppType {
static const WEB = 1;
static const REMOTE = 2;
static const ANDROID_APP = 3;
}

View File

@@ -0,0 +1,5 @@
class AuthResult {
static const int OK = 1;
static const int CANCEL = -1;
}

15
lib/const/auth_type.dart Normal file
View File

@@ -0,0 +1,15 @@
class AuthType {
static const GET_PUBLIC_KEY = 1;
static const SIGN_EVENT = 2;
static const GET_RELAYS = 3;
static const NIP04_ENCRYPT = 4;
static const NIP04_DECRYPT = 5;
static const NIP44_ENCRYPT = 6;
static const NIP44_DECRYPT = 7;
}

25
lib/data/app.dart Normal file
View File

@@ -0,0 +1,25 @@
class App {
int id;
String pubkey;
int appType;
String code;
String name;
String? image;
String? permissions;
App({
required this.id,
required this.pubkey,
required this.appType,
required this.code,
required this.name,
this.image,
this.permissions,
});
}

1
lib/data/app_db.dart Normal file
View File

@@ -0,0 +1 @@
class AppDB {}

28
lib/data/auth_log.dart Normal file
View File

@@ -0,0 +1,28 @@
class AuthLog {
int id;
int appId;
int authType;
int? eventKind;
String? title;
String? content;
int authResult;
int createdAt;
AuthLog({
required this.id,
required this.appId,
required this.authType,
this.eventKind,
this.title,
this.content,
required this.authResult,
required this.createdAt,
});
}

View File

@@ -0,0 +1 @@
class AuthLogDB {}

63
lib/data/db.dart Normal file
View File

@@ -0,0 +1,63 @@
import 'dart:io';
import 'package:nostr_sdk/utils/platform_util.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:process_run/shell_run.dart';
class DB {
static const _VERSION = 1;
static const _dbName = "nowser.db";
static Database? _database;
static init() async {
String path = _dbName;
if (!PlatformUtil.isWeb()) {
var databasesPath = await getDatabasesPath();
path = join(databasesPath, _dbName);
}
try {
_database =
await openDatabase(path, version: _VERSION, onCreate: _onCreate);
} catch (e) {
if (Platform.isLinux) {
// maybe it need install sqlite first, but this command need run by root.
await run('sudo apt-get -y install libsqlite3-0 libsqlite3-dev');
_database =
await openDatabase(path, version: _VERSION, onCreate: _onCreate);
}
}
}
static Future<void> _onCreate(Database db, int version) async {
// init db
db.execute(
"create table app(id integer not null constraint app_pk primary key autoincrement,pubkey text not null,app_type integer not null,code text not null,name text not null,image text,permissions text);");
db.execute(
"create table auth_log(id integer not null constraint auth_log_pk primary key autoincrement,app_id integer not null,auth_type integer not null,event_kind integer,title text,content text,auth_result integer not null,created_at integer not null);");
db.execute("create index auth_log_index on auth_log (app_id);");
}
static Future<Database> getCurrentDatabase() async {
if (_database == null) {
await init();
}
return _database!;
}
static Future<DatabaseExecutor> getDB(DatabaseExecutor? db) async {
if (db != null) {
return db;
}
return getCurrentDatabase();
}
static void close() {
_database?.close();
_database = null;
}
}

View File

@@ -7,6 +7,8 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:nostr_sdk/utils/string_util.dart';
import 'package:nowser/data/db.dart';
import 'package:nowser/provider/key_provider.dart';
import 'package:nowser/provider/web_provider.dart';
import 'package:nowser/router/index/index_router.dart';
import 'package:nowser/router/web_tabs_select/web_tabs_select_router.dart';
@@ -25,13 +27,19 @@ late WebProvider webProvider;
late SettingProvider settingProvider;
late KeyProvider keyProvider;
late Map<String, WidgetBuilder> routes;
Future<void> main() async {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
keyProvider = KeyProvider();
var dataUtilTask = DataUtil.getInstance();
var dataFutureResultList = await Future.wait([dataUtilTask]);
var keyTask = keyProvider.init();
var dbTask = DB.getCurrentDatabase();
var dataFutureResultList = await Future.wait([dataUtilTask, keyTask, dbTask]);
var settingTask = SettingProvider.getInstance();
var futureResultList = await Future.wait([settingTask]);

View File

@@ -0,0 +1,77 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:nostr_sdk/client_utils/keys.dart';
import 'package:nostr_sdk/utils/string_util.dart';
class KeyProvider extends ChangeNotifier {
static const String KEY_NAME = "nowserKeys";
List<String> keys = [];
List<String> pubkeys = [];
Map<String, String> keysMap = {};
Future<void> init() async {
await reload();
}
Future<void> reload() async {
keys.clear();
pubkeys.clear();
keysMap.clear();
final storage = FlutterSecureStorage();
var strs = await storage.read(key: KEY_NAME);
if (StringUtil.isNotBlank(strs)) {
var jsonObj = jsonDecode(strs!);
if (jsonObj is List) {
for (var jsonObjItem in jsonObj) {
if (jsonObjItem is String) {
keys.add(jsonObjItem);
var pubkey = getPublicKey(jsonObjItem);
keysMap[pubkey] = jsonObjItem;
pubkeys.add(pubkey);
}
}
}
}
}
Future<void> _saveKey() async {
var jsonStr = jsonEncode(keys);
final storage = FlutterSecureStorage();
await storage.write(key: KEY_NAME, value: jsonStr);
}
void addKey(String privateKey) {
if (exist(privateKey)) {
return;
}
keys.add(privateKey);
var pubkey = getPublicKey(privateKey);
keysMap[pubkey] = privateKey;
pubkeys.add(pubkey);
_saveKey();
notifyListeners();
}
void removeKey(String pubkey) {
var privateKey = keysMap.remove(pubkey);
if (StringUtil.isNotBlank(privateKey)) {
keys.remove(privateKey);
pubkeys.remove(pubkey);
}
_saveKey();
notifyListeners();
}
bool exist(String privateKey) {
return keys.contains(privateKey);
}
}

View File

@@ -6,6 +6,10 @@
#include "generated_plugin_registrant.h"
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
}

View File

@@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_linux
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST

View File

@@ -8,6 +8,7 @@ import Foundation
import cryptography_flutter
import device_info_plus
import flutter_inappwebview_macos
import flutter_secure_storage_macos
import path_provider_foundation
import shared_preferences_foundation
import sqflite
@@ -16,6 +17,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
CryptographyFlutterPlugin.register(with: registry.registrar(forPlugin: "CryptographyFlutterPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
InAppWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "InAppWebViewFlutterPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))

View File

@@ -323,6 +323,54 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_secure_storage:
dependency: "direct main"
description:
name: flutter_secure_storage
sha256: "165164745e6afb5c0e3e3fcc72a012fb9e58496fb26ffb92cf22e16a821e85d0"
url: "https://pub.dev"
source: hosted
version: "9.2.2"
flutter_secure_storage_linux:
dependency: transitive
description:
name: flutter_secure_storage_linux
sha256: "4d91bfc23047422cbcd73ac684bc169859ee766482517c22172c86596bf1464b"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
flutter_secure_storage_macos:
dependency: transitive
description:
name: flutter_secure_storage_macos
sha256: "1693ab11121a5f925bbea0be725abfcfbbcf36c1e29e571f84a0c0f436147a81"
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_secure_storage_platform_interface:
dependency: transitive
description:
name: flutter_secure_storage_platform_interface
sha256: cf91ad32ce5adef6fba4d736a542baca9daf3beac4db2d04be350b87f69ac4a8
url: "https://pub.dev"
source: hosted
version: "1.1.2"
flutter_secure_storage_web:
dependency: transitive
description:
name: flutter_secure_storage_web
sha256: f4ebff989b4f07b2656fb16b47852c0aab9fed9b4ec1c70103368337bc1886a9
url: "https://pub.dev"
source: hosted
version: "1.2.1"
flutter_secure_storage_windows:
dependency: transitive
description:
name: flutter_secure_storage_windows
sha256: b20b07cb5ed4ed74fc567b78a72936203f587eba460af1df11281c9326cd3709
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_socks_proxy:
dependency: transitive
description:

View File

@@ -45,6 +45,7 @@ dependencies:
shared_preferences: ^2.2.3
google_fonts: ^6.2.1
flutter_font_picker: ^1.4.0
flutter_secure_storage: ^9.2.2
dev_dependencies:
flutter_test:

View File

@@ -6,6 +6,9 @@
#include "generated_plugin_registrant.h"
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin"));
}

View File

@@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_windows
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST