mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
TMP 4
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class BitcoinAddressRecord {
|
||||
BitcoinAddressRecord(this.address, {this.label});
|
||||
|
||||
factory BitcoinAddressRecord.fromJSON(String jsonSource) {
|
||||
final decoded = json.decode(jsonSource) as Map;
|
||||
|
||||
return BitcoinAddressRecord(decoded['address'] as String,
|
||||
label: decoded['label'] as String);
|
||||
}
|
||||
|
||||
final String address;
|
||||
String label;
|
||||
|
||||
String toJSON() => json.encode({'label': label, 'address': address});
|
||||
}
|
||||
@@ -1,14 +1,35 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
|
||||
import 'package:cake_wallet/src/domain/common/balance.dart';
|
||||
|
||||
class BitcoinBalance extends Balance {
|
||||
BitcoinBalance({@required this.confirmed, @required this.unconfirmed});
|
||||
const BitcoinBalance({@required this.confirmed, @required this.unconfirmed}) : super();
|
||||
|
||||
factory BitcoinBalance.fromJSON(String jsonSource) {
|
||||
if (jsonSource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final decoded = json.decode(jsonSource) as Map;
|
||||
|
||||
return BitcoinBalance(
|
||||
confirmed: decoded['confirmed'] as int ?? 0,
|
||||
unconfirmed: decoded['unconfirmed'] as int ?? 0);
|
||||
}
|
||||
|
||||
final int confirmed;
|
||||
final int unconfirmed;
|
||||
|
||||
int get total => confirmed + unconfirmed;
|
||||
|
||||
String get confirmedFormatted => bitcoinAmountToString(amount: confirmed);
|
||||
|
||||
String get unconfirmedFormatted => bitcoinAmountToString(amount: unconfirmed);
|
||||
|
||||
String get totalFormatted => bitcoinAmountToString(amount: total);
|
||||
|
||||
String toJSON() =>
|
||||
json.encode({'confirmed': confirmed, 'unconfirmed': unconfirmed});
|
||||
}
|
||||
|
||||
@@ -1,61 +1,78 @@
|
||||
import 'dart:convert';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/transaction_history.dart';
|
||||
import 'package:cake_wallet/bitcoin/file.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_transaction_info.dart';
|
||||
import 'package:cake_wallet/bitcoin/electrum.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_history.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_info.dart';
|
||||
import 'package:cake_wallet/bitcoin/file.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_direction.dart';
|
||||
|
||||
class BitcoinTransactionHistory extends TransactionHistory {
|
||||
BitcoinTransactionHistory(
|
||||
{@required this.eclient,
|
||||
@required this.path,
|
||||
@required String password,
|
||||
@required this.wallet})
|
||||
: _transactions = BehaviorSubject<List<TransactionInfo>>.seeded([]),
|
||||
part 'bitcoin_transaction_history.g.dart';
|
||||
|
||||
// TODO: Think about another transaction store for bitcoin transaction history..
|
||||
|
||||
const _transactionsHistoryFileName = 'transactions.json';
|
||||
|
||||
class BitcoinTransactionHistory = BitcoinTransactionHistoryBase
|
||||
with _$BitcoinTransactionHistory;
|
||||
|
||||
abstract class BitcoinTransactionHistoryBase
|
||||
extends TransactionHistoryBase<BitcoinTransactionInfo> with Store {
|
||||
BitcoinTransactionHistoryBase(
|
||||
{this.eclient, String dirPath, @required String password})
|
||||
: path = '$dirPath/$_transactionsHistoryFileName',
|
||||
_password = password,
|
||||
_height = 0;
|
||||
|
||||
final BitcoinWallet wallet;
|
||||
BitcoinWallet wallet;
|
||||
final ElectrumClient eclient;
|
||||
final String path;
|
||||
final String _password;
|
||||
int _height;
|
||||
|
||||
@override
|
||||
Observable<List<TransactionInfo>> get transactions => _transactions.stream;
|
||||
List<TransactionInfo> get transactionsAll => _transactions.value;
|
||||
final BehaviorSubject<List<TransactionInfo>> _transactions;
|
||||
bool _isUpdating = false;
|
||||
|
||||
Future<void> init() async {
|
||||
// TODO: throw exeption if wallet is null;
|
||||
final info = await _read();
|
||||
_height = (info['height'] as int) ?? _height;
|
||||
_transactions.value = info['transactions'] as List<TransactionInfo>;
|
||||
// FIXME: remove hardcoded value
|
||||
transactions = ObservableList.of([
|
||||
BitcoinTransactionInfo(
|
||||
id: 'test',
|
||||
height: 12,
|
||||
amount: 12,
|
||||
direction: TransactionDirection.incoming,
|
||||
date: DateTime.now(),
|
||||
isPending: false)
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<TransactionInfo>> getAll() async => _transactions.value;
|
||||
Future update() async {
|
||||
await super.update();
|
||||
_updateHeight();
|
||||
}
|
||||
|
||||
@override
|
||||
Future update() async {
|
||||
if (_isUpdating) {
|
||||
return;
|
||||
}
|
||||
Future<List<BitcoinTransactionInfo>> fetchTransactions() async {
|
||||
final addresses = wallet.addresses;
|
||||
final histories =
|
||||
addresses.map((record) => eclient.getHistory(address: record.address));
|
||||
final _historiesWithDetails = await Future.wait(histories)
|
||||
.then((histories) => histories
|
||||
.map((h) => h.where((tx) => (tx['height'] as int) > _height))
|
||||
.expand((i) => i)
|
||||
.toList())
|
||||
.then((histories) => histories.map((tx) => fetchTransactionInfo(
|
||||
hash: tx['tx_hash'] as String, height: tx['height'] as int)));
|
||||
final historiesWithDetails = await Future.wait(_historiesWithDetails);
|
||||
|
||||
try {
|
||||
_isUpdating = true;
|
||||
final newTransasctions = await fetchTransactions();
|
||||
_transactions.value = _transactions.value + newTransasctions;
|
||||
_updateHeight();
|
||||
await save();
|
||||
_isUpdating = false;
|
||||
} catch (e) {
|
||||
_isUpdating = false;
|
||||
rethrow;
|
||||
}
|
||||
return historiesWithDetails
|
||||
.map((info) => BitcoinTransactionInfo.fromHexAndHeader(
|
||||
info['raw'] as String, info['header'] as Map<String, Object>,
|
||||
addresses: addresses.map((record) => record.address).toList()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<Map<String, Object>> fetchTransactionInfo(
|
||||
@@ -69,51 +86,20 @@ class BitcoinTransactionHistory extends TransactionHistory {
|
||||
return {'raw': raw, 'header': header};
|
||||
}
|
||||
|
||||
Future<List<BitcoinTransactionInfo>> fetchTransactions() async {
|
||||
final addresses = wallet.getAddresses();
|
||||
final histories =
|
||||
addresses.map((address) => eclient.getHistory(address: address));
|
||||
final _historiesWithDetails = await Future.wait(histories)
|
||||
.then((histories) => histories
|
||||
.map((h) => h.where((tx) => (tx['height'] as int) > _height))
|
||||
.expand((i) => i)
|
||||
.toList())
|
||||
.then((histories) => histories.map((tx) => fetchTransactionInfo(
|
||||
hash: tx['tx_hash'] as String, height: tx['height'] as int)));
|
||||
final historiesWithDetails = await Future.wait(_historiesWithDetails);
|
||||
|
||||
return historiesWithDetails
|
||||
.map((info) => BitcoinTransactionInfo.fromHexAndHeader(
|
||||
info['raw'] as String, info['header'] as Map<String, Object>,
|
||||
addresses: addresses))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> add(List<BitcoinTransactionInfo> transactions) async {
|
||||
final txs = await getAll()
|
||||
..addAll(transactions);
|
||||
await writeData(
|
||||
path: path,
|
||||
password: _password,
|
||||
data: json
|
||||
.encode(txs.map((tx) => (tx as BitcoinTransactionInfo).toJson())));
|
||||
this.transactions.addAll(transactions);
|
||||
await save();
|
||||
}
|
||||
|
||||
Future<void> addOne(BitcoinTransactionInfo tx) async {
|
||||
final txs = await getAll()
|
||||
..add(tx);
|
||||
await writeData(
|
||||
path: path,
|
||||
password: _password,
|
||||
data: json
|
||||
.encode(txs.map((tx) => (tx as BitcoinTransactionInfo).toJson())));
|
||||
transactions.add(tx);
|
||||
await save();
|
||||
}
|
||||
|
||||
Future<void> save() async => writeData(
|
||||
path: path,
|
||||
password: _password,
|
||||
data: json
|
||||
.encode({'height': _height, 'transactions': _transactions.value}));
|
||||
data: json.encode({'height': _height, 'transactions': transactions}));
|
||||
|
||||
Future<Map<String, Object>> _read() async {
|
||||
try {
|
||||
@@ -133,13 +119,13 @@ class BitcoinTransactionHistory extends TransactionHistory {
|
||||
|
||||
return {'transactions': transactions, 'height': height};
|
||||
} catch (_) {
|
||||
return {'transactions': List<TransactionInfo>(), 'height': 0};
|
||||
return {'transactions': <TransactionInfo>[], 'height': 0};
|
||||
}
|
||||
}
|
||||
|
||||
void _updateHeight() {
|
||||
final int newHeight = _transactions.value
|
||||
.fold(0, (acc, val) => val.height > acc ? val.height : acc);
|
||||
final newHeight = transactions.fold(
|
||||
0, (int acc, val) => val.height > acc ? val.height : acc);
|
||||
_height = newHeight > _height ? newHeight : _height;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ class BitcoinTransactionInfo extends TransactionInfo {
|
||||
String amountFormatted() => bitcoinAmountToString(amount: amount);
|
||||
|
||||
@override
|
||||
String fiatAmount() => '';
|
||||
String fiatAmount() => '\$ 24.5';
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final m = Map<String, dynamic>();
|
||||
|
||||
+136
-204
@@ -1,251 +1,184 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_balance.dart';
|
||||
import 'package:cake_wallet/src/domain/common/sync_status.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:bip39/bip39.dart' as bip39;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
||||
import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData;
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_transaction_history.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_address_record.dart';
|
||||
import 'package:cake_wallet/bitcoin/file.dart';
|
||||
import 'package:cake_wallet/bitcoin/electrum.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_transaction_history.dart';
|
||||
import 'package:cake_wallet/src/domain/common/pathForWallet.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_balance.dart';
|
||||
import 'package:cake_wallet/src/domain/common/node.dart';
|
||||
import 'package:cake_wallet/src/domain/common/pending_transaction.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_creation_credentials.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_history.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
|
||||
class BitcoinWallet extends Wallet {
|
||||
BitcoinWallet(
|
||||
{@required this.hdwallet,
|
||||
@required this.eclient,
|
||||
@required this.path,
|
||||
@required String password,
|
||||
int accountIndex = 0,
|
||||
this.mnemonic})
|
||||
: _accountIndex = accountIndex,
|
||||
_password = password,
|
||||
_syncStatus = BehaviorSubject<SyncStatus>(),
|
||||
_onBalanceChange = BehaviorSubject<BitcoinBalance>(),
|
||||
_onAddressChange = BehaviorSubject<String>(),
|
||||
_onNameChange = BehaviorSubject<String>();
|
||||
part 'bitcoin_wallet.g.dart';
|
||||
|
||||
@override
|
||||
Observable<BitcoinBalance> get onBalanceChange => _onBalanceChange.stream;
|
||||
/* TODO: Save balance to a wallet file.
|
||||
Load balance from the wallet file in `init` method.
|
||||
*/
|
||||
|
||||
@override
|
||||
Observable<SyncStatus> get syncStatus => _syncStatus.stream;
|
||||
class BitcoinWallet = BitcoinWalletBase with _$BitcoinWallet;
|
||||
|
||||
@override
|
||||
String get name => path.split('/').last ?? '';
|
||||
@override
|
||||
String get address => hdwallet.address;
|
||||
String get xpub => hdwallet.base58;
|
||||
|
||||
final String path;
|
||||
final bitcoin.HDWallet hdwallet;
|
||||
final ElectrumClient eclient;
|
||||
final String mnemonic;
|
||||
BitcoinTransactionHistory history;
|
||||
|
||||
final BehaviorSubject<SyncStatus> _syncStatus;
|
||||
final BehaviorSubject<BitcoinBalance> _onBalanceChange;
|
||||
final BehaviorSubject<String> _onAddressChange;
|
||||
final BehaviorSubject<String> _onNameChange;
|
||||
BehaviorSubject<Object> _addressUpdatesSubject;
|
||||
StreamSubscription<Object> _addressUpdatesSubscription;
|
||||
final String _password;
|
||||
int _accountIndex;
|
||||
|
||||
static Future<BitcoinWallet> load(
|
||||
{@required String name, @required String password}) async {
|
||||
final walletDirPath =
|
||||
await pathForWalletDir(name: name, type: WalletType.bitcoin);
|
||||
final walletPath = '$walletDirPath/$name';
|
||||
final walletJSONRaw = await read(path: walletPath, password: password);
|
||||
final jsoned = json.decode(walletJSONRaw) as Map<String, Object>;
|
||||
final mnemonic = jsoned['mnemonic'] as String;
|
||||
abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
|
||||
static BitcoinWallet fromJSON(
|
||||
{@required String password,
|
||||
@required String name,
|
||||
@required String dirPath,
|
||||
String jsonSource}) {
|
||||
final data = json.decode(jsonSource) as Map;
|
||||
final mnemonic = data['mnemonic'] as String;
|
||||
final accountIndex =
|
||||
(jsoned['account_index'] == "null" || jsoned['account_index'] == null)
|
||||
(data['account_index'] == "null" || data['account_index'] == null)
|
||||
? 0
|
||||
: int.parse(jsoned['account_index'] as String);
|
||||
: int.parse(data['account_index'] as String);
|
||||
final _addresses = data['addresses'] as List;
|
||||
final addresses = <BitcoinAddressRecord>[];
|
||||
final balance = BitcoinBalance.fromJSON(data['balance'] as String) ??
|
||||
BitcoinBalance(confirmed: 0, unconfirmed: 0);
|
||||
|
||||
return await build(
|
||||
_addresses?.forEach((Object el) {
|
||||
if (el is String) {
|
||||
addresses.add(BitcoinAddressRecord.fromJSON(el));
|
||||
}
|
||||
});
|
||||
|
||||
return BitcoinWalletBase.build(
|
||||
dirPath: dirPath,
|
||||
mnemonic: mnemonic,
|
||||
password: password,
|
||||
name: name,
|
||||
accountIndex: accountIndex);
|
||||
accountIndex: accountIndex,
|
||||
initialAddresses: addresses,
|
||||
initialBalance: balance);
|
||||
}
|
||||
|
||||
static Future<BitcoinWallet> build(
|
||||
static BitcoinWallet build(
|
||||
{@required String mnemonic,
|
||||
@required String password,
|
||||
@required String name,
|
||||
int accountIndex = 0}) async {
|
||||
final hd = bitcoin.HDWallet.fromSeed(bip39.mnemonicToSeed(mnemonic),
|
||||
network: bitcoin.bitcoin);
|
||||
final walletDirPath =
|
||||
await pathForWalletDir(name: name, type: WalletType.bitcoin);
|
||||
final walletPath = '$walletDirPath/$name';
|
||||
final historyPath = '$walletDirPath/transactions.json';
|
||||
@required String dirPath,
|
||||
List<BitcoinAddressRecord> initialAddresses,
|
||||
BitcoinBalance initialBalance,
|
||||
int accountIndex = 0}) {
|
||||
final walletPath = '$dirPath/$name';
|
||||
final eclient = ElectrumClient();
|
||||
final wallet = BitcoinWallet(
|
||||
hdwallet: hd,
|
||||
final history = BitcoinTransactionHistory(
|
||||
eclient: eclient, dirPath: dirPath, password: password);
|
||||
|
||||
return BitcoinWallet._internal(
|
||||
eclient: eclient,
|
||||
path: walletPath,
|
||||
name: name,
|
||||
mnemonic: mnemonic,
|
||||
password: password,
|
||||
accountIndex: accountIndex);
|
||||
final history = BitcoinTransactionHistory(
|
||||
eclient: eclient,
|
||||
path: historyPath,
|
||||
password: password,
|
||||
wallet: wallet);
|
||||
wallet.history = history;
|
||||
await history.init();
|
||||
await wallet.updateInfo();
|
||||
|
||||
return wallet;
|
||||
accountIndex: accountIndex,
|
||||
initialAddresses: initialAddresses,
|
||||
initialBalance: initialBalance,
|
||||
transactionHistory: history);
|
||||
}
|
||||
|
||||
List<String> getAddresses() => _accountIndex == 0
|
||||
? [address]
|
||||
: List<String>.generate(
|
||||
_accountIndex, (i) => _getAddress(hd: hdwallet, index: i));
|
||||
BitcoinWalletBase._internal(
|
||||
{@required this.eclient,
|
||||
@required this.path,
|
||||
@required String password,
|
||||
@required this.name,
|
||||
List<BitcoinAddressRecord> initialAddresses,
|
||||
int accountIndex = 0,
|
||||
this.transactionHistory,
|
||||
this.mnemonic,
|
||||
BitcoinBalance initialBalance}) {
|
||||
balance = initialBalance ?? BitcoinBalance(confirmed: 0, unconfirmed: 0);
|
||||
hd = bitcoin.HDWallet.fromSeed(bip39.mnemonicToSeed(mnemonic),
|
||||
network: bitcoin.bitcoin);
|
||||
addresses = initialAddresses != null
|
||||
? ObservableList<BitcoinAddressRecord>.of(initialAddresses)
|
||||
: ObservableList<BitcoinAddressRecord>();
|
||||
|
||||
Future<String> newAddress() async {
|
||||
if (addresses.isEmpty) {
|
||||
addresses.add(BitcoinAddressRecord(hd.address));
|
||||
}
|
||||
|
||||
address = addresses.first.address;
|
||||
|
||||
_password = password;
|
||||
_accountIndex = accountIndex;
|
||||
}
|
||||
|
||||
@override
|
||||
final BitcoinTransactionHistory transactionHistory;
|
||||
final String path;
|
||||
bitcoin.HDWallet hd;
|
||||
final ElectrumClient eclient;
|
||||
final String mnemonic;
|
||||
int _accountIndex;
|
||||
String _password;
|
||||
|
||||
@override
|
||||
String name;
|
||||
|
||||
@override
|
||||
@observable
|
||||
String address;
|
||||
|
||||
@override
|
||||
@observable
|
||||
BitcoinBalance balance;
|
||||
|
||||
@override
|
||||
final type = WalletType.bitcoin;
|
||||
|
||||
ObservableList<BitcoinAddressRecord> addresses;
|
||||
|
||||
String get xpub => hd.base58;
|
||||
|
||||
Future<void> init() async {
|
||||
await transactionHistory.init();
|
||||
}
|
||||
|
||||
Future<BitcoinAddressRecord> generateNewAddress({String label}) async {
|
||||
_accountIndex += 1;
|
||||
final address = _getAddress(hd: hdwallet, index: _accountIndex);
|
||||
final address = BitcoinAddressRecord(
|
||||
_getAddress(hd: hd, index: _accountIndex),
|
||||
label: label);
|
||||
addresses.add(address);
|
||||
|
||||
await save();
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
@override
|
||||
Future close() async {
|
||||
await _addressUpdatesSubscription?.cancel();
|
||||
}
|
||||
|
||||
@override
|
||||
Future connectToNode(
|
||||
{Node node, bool useSSL = false, bool isLightWallet = false}) async {
|
||||
try {
|
||||
// FIXME: Hardcoded server address
|
||||
// final uri = Uri.parse(node.uri);
|
||||
// https://electrum2.hodlister.co:50002
|
||||
await eclient.connect(host: 'electrum2.hodlister.co', port: 50002);
|
||||
_syncStatus.value = ConnectedSyncStatus();
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
_syncStatus.value = FailedSyncStatus();
|
||||
Future<void> updateAddress(String address, {String label}) async {
|
||||
for (final addr in addresses) {
|
||||
if (addr.address == address) {
|
||||
addr.label = label;
|
||||
await save();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PendingTransaction> createTransaction(
|
||||
TransactionCreationCredentials credentials) async {
|
||||
final txb = bitcoin.TransactionBuilder(network: bitcoin.bitcoin);
|
||||
final transactions = history.transactionsAll;
|
||||
history.transactionsAll.sort((q, w) => q.height.compareTo(w.height));
|
||||
final prevTx = transactions.first;
|
||||
|
||||
txb.setVersion(1);
|
||||
txb.addInput(prevTx, 0);
|
||||
txb.addOutput('address', 112);
|
||||
txb.sign(vin: null, keyPair: null);
|
||||
|
||||
final hex = txb.build().toHex();
|
||||
|
||||
// broadcast transaction to electrum
|
||||
return null;
|
||||
}
|
||||
Future<void> startSync() async {}
|
||||
|
||||
@override
|
||||
Future<String> getAddress() async => address;
|
||||
Future<void> connectToNode({@required Node node}) async {}
|
||||
|
||||
@override
|
||||
Future<int> getCurrentHeight() async => 0;
|
||||
Future<void> createTransaction(Object credentials) async {}
|
||||
|
||||
@override
|
||||
Future<String> getFilename() async => path.split('/').last ?? '';
|
||||
Future<void> save() async =>
|
||||
await write(path: path, password: _password, data: toJSON());
|
||||
|
||||
@override
|
||||
Future<String> getFullBalance() async =>
|
||||
bitcoinAmountToString(amount: _onBalanceChange.value.total);
|
||||
|
||||
@override
|
||||
TransactionHistory getHistory() => history;
|
||||
|
||||
@override
|
||||
Future<Map<String, String>> getKeys() async =>
|
||||
{'publicKey': hdwallet.pubKey, 'privateKey': hdwallet.privKey};
|
||||
|
||||
@override
|
||||
Future<String> getName() async => path.split('/').last ?? '';
|
||||
|
||||
@override
|
||||
Future<int> getNodeHeight() async => 0;
|
||||
|
||||
@override
|
||||
Future<String> getSeed() async => mnemonic;
|
||||
|
||||
@override
|
||||
WalletType getType() => WalletType.bitcoin;
|
||||
|
||||
@override
|
||||
Future<String> getUnlockedBalance() async =>
|
||||
bitcoinAmountToString(amount: _onBalanceChange.value.total);
|
||||
|
||||
@override
|
||||
Future<bool> isConnected() async => eclient.isConnected;
|
||||
|
||||
@override
|
||||
Observable<String> get onAddressChange => _onAddressChange.stream;
|
||||
|
||||
@override
|
||||
Observable<String> get onNameChange => _onNameChange.stream;
|
||||
|
||||
@override
|
||||
Future rescan({int restoreHeight = 0}) {
|
||||
// TODO: implement rescan
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future startSync() async {
|
||||
_addressUpdatesSubject = eclient.addressUpdate(address: address);
|
||||
_addressUpdatesSubscription =
|
||||
_addressUpdatesSubject.listen((obj) => print('new obj: $obj'));
|
||||
_onBalanceChange.value = await fetchBalance();
|
||||
getHistory().update();
|
||||
}
|
||||
|
||||
@override
|
||||
Future updateInfo() async {
|
||||
_onNameChange.value = await getName();
|
||||
// _addressUpdatesSubject = eclient.addressUpdate(address: address);
|
||||
// _addressUpdatesSubscription =
|
||||
// _addressUpdatesSubject.listen((obj) => print('new obj: $obj'));
|
||||
_onBalanceChange.value = BitcoinBalance(confirmed: 0, unconfirmed: 0);
|
||||
print(await getKeys());
|
||||
}
|
||||
|
||||
Future<BitcoinBalance> fetchBalance() async {
|
||||
final balance = await _fetchBalances();
|
||||
|
||||
return BitcoinBalance(
|
||||
confirmed: balance['confirmed'], unconfirmed: balance['unconfirmed']);
|
||||
}
|
||||
|
||||
Future<void> save() async => await write(
|
||||
path: path,
|
||||
password: _password,
|
||||
obj: {'mnemonic': mnemonic, 'account_index': _accountIndex.toString()});
|
||||
String toJSON() => json.encode({
|
||||
'mnemonic': mnemonic,
|
||||
'account_index': _accountIndex.toString(),
|
||||
'addresses': addresses.map((addr) => addr.toJSON()).toList(),
|
||||
'balance': balance?.toJSON()
|
||||
});
|
||||
|
||||
String _getAddress({bitcoin.HDWallet hd, int index}) => bitcoin
|
||||
.P2PKH(
|
||||
@@ -256,9 +189,8 @@ class BitcoinWallet extends Wallet {
|
||||
|
||||
Future<Map<String, int>> _fetchBalances() async {
|
||||
final balances = await Future.wait(
|
||||
getAddresses().map((address) => eclient.getBalance(address: address)));
|
||||
final balance =
|
||||
balances.fold(Map<String, int>(), (Map<String, int> acc, val) {
|
||||
addresses.map((record) => eclient.getBalance(address: record.address)));
|
||||
final balance = balances.fold(<String, int>{}, (Map<String, int> acc, val) {
|
||||
acc['confirmed'] =
|
||||
(val['confirmed'] as int ?? 0) + (acc['confirmed'] ?? 0);
|
||||
acc['unconfirmed'] =
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
import 'dart:io';
|
||||
import 'package:bip39/bip39.dart' as bip39;
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
||||
import 'package:cake_wallet/src/domain/common/pathForWallet.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_description.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallets_manager.dart';
|
||||
|
||||
class BitcoinWalletManager extends WalletsManager {
|
||||
@override
|
||||
Future<Wallet> create(String name, String password, String language) async {
|
||||
final wallet = await BitcoinWallet.build(
|
||||
mnemonic: bip39.generateMnemonic(), password: password, name: name);
|
||||
await wallet.save();
|
||||
|
||||
return wallet;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isWalletExit(String name) async =>
|
||||
File(await pathForWallet(name: name, type: WalletType.bitcoin))
|
||||
.existsSync();
|
||||
|
||||
@override
|
||||
Future<Wallet> openWallet(String name, String password) async {
|
||||
return BitcoinWallet.load(
|
||||
name: name, password: password);
|
||||
}
|
||||
|
||||
@override
|
||||
Future remove(WalletDescription wallet) async {
|
||||
final path = await pathForWalletDir(name: wallet.name, type: wallet.type);
|
||||
final f = File(path);
|
||||
|
||||
if (!f.existsSync()) {
|
||||
return;
|
||||
}
|
||||
|
||||
f.deleteSync();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Wallet> restoreFromKeys(String name, String password, String language,
|
||||
int restoreHeight, String address, String viewKey, String spendKey) {
|
||||
// TODO: implement restoreFromKeys
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Wallet> restoreFromSeed(
|
||||
String name, String password, String seed, int restoreHeight) async {
|
||||
final wallet = await BitcoinWallet.build(
|
||||
name: name, password: password, mnemonic: seed);
|
||||
await wallet.save();
|
||||
|
||||
return wallet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:cake_wallet/core/wallet_credentials.dart';
|
||||
|
||||
class BitcoinNewWalletCredentials extends WalletCredentials {
|
||||
BitcoinNewWalletCredentials({String name}) : super(name: name);
|
||||
}
|
||||
|
||||
class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials {
|
||||
BitcoinRestoreWalletFromSeedCredentials(
|
||||
{String name, String password, this.mnemonic})
|
||||
: super(name: name, password: password);
|
||||
|
||||
final String mnemonic;
|
||||
}
|
||||
|
||||
class BitcoinRestoreWalletFromWIFCredentials extends WalletCredentials {
|
||||
BitcoinRestoreWalletFromWIFCredentials(
|
||||
{String name, String password, this.wif})
|
||||
: super(name: name, password: password);
|
||||
|
||||
final String wif;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'package:bip39/bip39.dart' as bip39;
|
||||
import 'package:cake_wallet/bitcoin/file.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet_creation_credentials.dart';
|
||||
import 'package:cake_wallet/core/wallet_service.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
||||
import 'package:cake_wallet/src/domain/common/pathForWallet.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
|
||||
class BitcoinWalletService extends WalletService<
|
||||
BitcoinNewWalletCredentials,
|
||||
BitcoinRestoreWalletFromSeedCredentials,
|
||||
BitcoinRestoreWalletFromWIFCredentials> {
|
||||
@override
|
||||
Future<BitcoinWallet> create(BitcoinNewWalletCredentials credentials) async {
|
||||
final dirPath = await pathForWalletDir(
|
||||
type: WalletType.bitcoin, name: credentials.name);
|
||||
final wallet = BitcoinWalletBase.build(
|
||||
dirPath: dirPath,
|
||||
mnemonic: bip39.generateMnemonic(),
|
||||
password: credentials.password,
|
||||
name: credentials.name);
|
||||
await wallet.save();
|
||||
await wallet.init();
|
||||
|
||||
return wallet;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isWalletExit(String name) async =>
|
||||
File(await pathForWallet(name: name, type: WalletType.bitcoin))
|
||||
.existsSync();
|
||||
|
||||
@override
|
||||
Future<BitcoinWallet> openWallet(String name, String password) async {
|
||||
final walletDirPath =
|
||||
await pathForWalletDir(name: name, type: WalletType.bitcoin);
|
||||
final walletPath = '$walletDirPath/$name';
|
||||
final walletJSONRaw = await read(path: walletPath, password: password);
|
||||
final wallet = BitcoinWalletBase.fromJSON(
|
||||
password: password,
|
||||
name: name,
|
||||
dirPath: walletDirPath,
|
||||
jsonSource: walletJSONRaw);
|
||||
await wallet.init();
|
||||
|
||||
return wallet;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> remove(String wallet) {
|
||||
// TODO: implement remove
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<BitcoinWallet> restoreFromKeys(
|
||||
BitcoinRestoreWalletFromWIFCredentials credentials) async {
|
||||
// TODO: implement restoreFromKeys
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<BitcoinWallet> restoreFromSeed(
|
||||
BitcoinRestoreWalletFromSeedCredentials credentials) async {
|
||||
final dirPath = await pathForWalletDir(
|
||||
type: WalletType.bitcoin, name: credentials.name);
|
||||
final wallet = BitcoinWalletBase.build(
|
||||
dirPath: dirPath,
|
||||
name: credentials.name,
|
||||
password: credentials.password,
|
||||
mnemonic: credentials.mnemonic);
|
||||
await wallet.save();
|
||||
await wallet.init();
|
||||
|
||||
return wallet;
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,11 @@ import 'package:flutter/foundation.dart';
|
||||
Future<void> write(
|
||||
{@required String path,
|
||||
@required String password,
|
||||
@required Map<String, String> obj}) async {
|
||||
final jsoned = json.encode(obj);
|
||||
@required String data}) async {
|
||||
final keys = extractKeys(password);
|
||||
final key = encrypt.Key.fromBase64(keys.first);
|
||||
final iv = encrypt.IV.fromBase64(keys.last);
|
||||
final encrypted = await encode(key: key, iv: iv, data: jsoned);
|
||||
final encrypted = await encode(key: key, iv: iv, data: data);
|
||||
final f = File(path);
|
||||
f.writeAsStringSync(encrypted);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user