Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add address book repository #502

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/util/storage/database/address_book_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:sembast/sembast.dart';

class AddressBookEntry {
String name;
String address;

AddressBookEntry(this.name, this.address);

factory AddressBookEntry.fromJson(Map<String, dynamic> data) {
return AddressBookEntry(data['name'], data['address']);
}

Map<String, dynamic> jsonMap() {
return {
"name": this.name,
"address": this.address,
};
}
}

class AddressBookRepository {
final StoreRef _store = stringMapStoreFactory.store("address_book");

Future<bool> insertAddressBookEntry(
AddressBookEntry addressBookEntry,
DatabaseClient databaseClient,
) async {
return true;
}

Future<bool> updateAddressBookEntry(
AddressBookEntry addressBookEntry, DatabaseClient databaseClient) async {
return true;
}

Future<bool> deleteAddressBookEntry(
AddressBookEntry addressBookEntry,
DatabaseClient databaseClient,
) async {
return true;
}

Future<AddressBookEntry?> getAddressBookEntry(
String address, DatabaseClient databaseClient) async {
try {
dynamic addressBookEntryJson =
await _store.record(address).get(databaseClient);
AddressBookEntry addressBookEntry =
AddressBookEntry.fromJson(addressBookEntryJson);
return addressBookEntry;
} catch (e) {
return null;
}
}

Future<List<AddressBookEntry>> getEntries(
DatabaseClient databaseClient) async {
final List<RecordSnapshot<dynamic, dynamic>> snapshots =
await _store.find(databaseClient);
List<AddressBookEntry> accounts = snapshots
.map((snapshot) => AddressBookEntry.fromJson(snapshot.value))
.toList(growable: false);
return accounts;
}
}
22 changes: 22 additions & 0 deletions lib/util/storage/database/database_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

import 'package:my_wit_wallet/util/storage/database/address_book_repository.dart';
import 'package:my_wit_wallet/util/storage/database/stats.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:witnet/explorer.dart';
Expand All @@ -16,6 +17,7 @@ Map<String, Function(DatabaseService, SendPort, Map<String, dynamic>)>
'configure': _configure,
'delete': _deleteRecord,
'deleteDatabase': _deleteDatabase,
'getAddressBookEntry': _getAddressBookEntry,
'getKeychain': _getKeychain,
'getStatsByAddress': _getStatsByAddress,
'getVtt': _getVtt,
Expand Down Expand Up @@ -157,6 +159,9 @@ Future<void> _addRecord(
case 'stats':
value = await dbService.add(AccountStats.fromJson(params['value']));
break;
case 'address_book_entry':
value = await dbService.add(AddressBookEntry.fromJson(params['value']));
break;
default:
value = false;
break;
Expand Down Expand Up @@ -184,6 +189,9 @@ Future<void> _deleteRecord(
case 'stats':
value = await dbService.delete(AccountStats.fromJson(params['value']));
break;
case 'address_book_entry':
value =
await dbService.delete(AddressBookEntry.fromJson(params['value']));
default:
value = false;
break;
Expand Down Expand Up @@ -220,6 +228,10 @@ Future<void> _updateRecord(
case 'stats':
value = await dbService.update(AccountStats.fromJson(params['value']));
break;
case 'address_book_entry':
value =
await dbService.update(AddressBookEntry.fromJson(params['value']));
break;
default:
value = false;
break;
Expand Down Expand Up @@ -277,3 +289,13 @@ Future<void> _getMint(
MintEntry? mint = await dbService.getMint(params);
port.send(mint);
}

Future<void> _getAddressBookEntry(
DatabaseService dbService,
SendPort port,
Map<String, dynamic> params,
) async {
AddressBookEntry? addressBookEntry =
await dbService.getAddressBookEntry(params);
port.send(addressBookEntry);
}
17 changes: 17 additions & 0 deletions lib/util/storage/database/database_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'package:my_wit_wallet/util/storage/database/address_book_repository.dart';
import 'package:my_wit_wallet/util/storage/database/get_account_mints_map.dart';
import 'package:my_wit_wallet/util/storage/database/get_account_vtts_map.dart';
import 'package:my_wit_wallet/util/storage/database/stats.dart';
Expand Down Expand Up @@ -57,6 +58,7 @@ class DatabaseService {
AccountRepository accountRepository = AccountRepository();
MintRepository mintRepository = MintRepository();
StatsRepository statsRepository = StatsRepository();
AddressBookRepository addressBookRepository = AddressBookRepository();

KeyChain keyChain = KeyChain();

Expand Down Expand Up @@ -115,6 +117,9 @@ class DatabaseService {
case AccountStats:
await statsRepository.insertStats(item, _database);
break;
case AddressBookEntry:
await addressBookRepository.insertAddressBookEntry(item, _database);
break;
default:
return false;
}
Expand Down Expand Up @@ -156,6 +161,8 @@ class DatabaseService {
case AccountStats:
await statsRepository.deleteStats(item.address, _database);
break;
case AddressBookEntry:
await addressBookRepository.deleteAddressBookEntry(item, _database);
default:
return false;
}
Expand Down Expand Up @@ -183,6 +190,8 @@ class DatabaseService {
case AccountStats:
await statsRepository.updateStats(item, _database);
break;
case AddressBookEntry:
await addressBookRepository.updateAddressBookEntry(item, _database);
default:
return false;
}
Expand Down Expand Up @@ -260,6 +269,8 @@ class DatabaseService {
Map<String, Account> accountMap = {};
Map<String, ValueTransferInfo> vttMap = {};
Map<String, MintEntry> mintMap = {};
List<AddressBookEntry> addressBook =
await addressBookRepository.getEntries(_database);
for (int i = 0; i < wallets.length; i++) {
walletMap[wallets[i].id] = wallets[i];
}
Expand Down Expand Up @@ -314,6 +325,7 @@ class DatabaseService {

/// Load current wallet and address from preferences
WalletStorage _walletStorage = WalletStorage(wallets: walletMap);
_walletStorage.setAddressBookEntries(addressBook);
_walletStorage.setAccounts(accountMap);
_walletStorage.setTransactions(vttMap);
_walletStorage.setMints(mintMap);
Expand Down Expand Up @@ -349,4 +361,9 @@ class DatabaseService {
Future<Account?> getAccount(params) async {
return await accountRepository.getAccount(params["address"], _database);
}

Future<AddressBookEntry?> getAddressBookEntry(params) async {
return await addressBookRepository.getAddressBookEntry(
params["address"], _database);
}
}
14 changes: 14 additions & 0 deletions lib/util/storage/database/wallet_storage.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:my_wit_wallet/util/storage/database/address_book_repository.dart';
import 'package:my_wit_wallet/util/storage/database/stats.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
Expand Down Expand Up @@ -52,6 +53,9 @@ class WalletStorage {
//<block_id, MintEntry>
Map<String, MintEntry> _mints = {};

//<address, name>
Map<String, String> _addressBook = {};

// WalletType type;
String? _currentWalletId;
String? _currentAddress;
Expand Down Expand Up @@ -126,6 +130,16 @@ class WalletStorage {
wallets[walletId]!.setTransaction(mint);
}

void setAddressBookEntry(AddressBookEntry addressBookEntry) {
_addressBook[addressBookEntry.address] = addressBookEntry.name;
}

void setAddressBookEntries(List<AddressBookEntry> entries) {
entries.forEach((element) {
setAddressBookEntry(element);
});
}

void setAccount(Account account) {
_accounts[account.address] = account;
if (wallets[account.walletId] != null)
Expand Down
Loading