2020-05-12 12:04:54 +00:00
|
|
|
import 'dart:typed_data';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:mobx/mobx.dart';
|
2020-05-12 12:04:54 +00:00
|
|
|
import 'package:bip39/bip39.dart' as bip39;
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-05-12 12:04:54 +00:00
|
|
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
|
|
|
import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData;
|
2020-06-20 07:10:00 +00:00
|
|
|
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';
|
2020-05-12 12:04:54 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/file.dart';
|
|
|
|
import 'package:cake_wallet/bitcoin/electrum.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_balance.dart';
|
2020-05-12 12:04:54 +00:00
|
|
|
import 'package:cake_wallet/src/domain/common/node.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_base.dart';
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
part 'bitcoin_wallet.g.dart';
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
/* TODO: Save balance to a wallet file.
|
|
|
|
Load balance from the wallet file in `init` method.
|
|
|
|
*/
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
class BitcoinWallet = BitcoinWalletBase with _$BitcoinWallet;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
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;
|
2020-05-12 12:04:54 +00:00
|
|
|
final accountIndex =
|
2020-06-20 07:10:00 +00:00
|
|
|
(data['account_index'] == "null" || data['account_index'] == null)
|
2020-05-12 12:04:54 +00:00
|
|
|
? 0
|
2020-06-20 07:10:00 +00:00
|
|
|
: 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);
|
|
|
|
|
|
|
|
_addresses?.forEach((Object el) {
|
|
|
|
if (el is String) {
|
|
|
|
addresses.add(BitcoinAddressRecord.fromJSON(el));
|
|
|
|
}
|
|
|
|
});
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
return BitcoinWalletBase.build(
|
|
|
|
dirPath: dirPath,
|
2020-05-12 12:04:54 +00:00
|
|
|
mnemonic: mnemonic,
|
|
|
|
password: password,
|
|
|
|
name: name,
|
2020-06-20 07:10:00 +00:00
|
|
|
accountIndex: accountIndex,
|
|
|
|
initialAddresses: addresses,
|
|
|
|
initialBalance: balance);
|
2020-05-12 12:04:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
static BitcoinWallet build(
|
2020-05-12 12:04:54 +00:00
|
|
|
{@required String mnemonic,
|
|
|
|
@required String password,
|
|
|
|
@required String name,
|
2020-06-20 07:10:00 +00:00
|
|
|
@required String dirPath,
|
|
|
|
List<BitcoinAddressRecord> initialAddresses,
|
|
|
|
BitcoinBalance initialBalance,
|
|
|
|
int accountIndex = 0}) {
|
|
|
|
final walletPath = '$dirPath/$name';
|
2020-05-12 12:04:54 +00:00
|
|
|
final eclient = ElectrumClient();
|
2020-06-20 07:10:00 +00:00
|
|
|
final history = BitcoinTransactionHistory(
|
|
|
|
eclient: eclient, dirPath: dirPath, password: password);
|
|
|
|
|
|
|
|
return BitcoinWallet._internal(
|
2020-05-12 12:04:54 +00:00
|
|
|
eclient: eclient,
|
|
|
|
path: walletPath,
|
2020-06-20 07:10:00 +00:00
|
|
|
name: name,
|
2020-05-12 12:04:54 +00:00
|
|
|
mnemonic: mnemonic,
|
|
|
|
password: password,
|
2020-06-20 07:10:00 +00:00
|
|
|
accountIndex: accountIndex,
|
|
|
|
initialAddresses: initialAddresses,
|
|
|
|
initialBalance: initialBalance,
|
|
|
|
transactionHistory: history);
|
2020-05-12 12:04:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
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>();
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
if (addresses.isEmpty) {
|
|
|
|
addresses.add(BitcoinAddressRecord(hd.address));
|
2020-05-12 12:04:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
address = addresses.first.address;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
_password = password;
|
|
|
|
_accountIndex = accountIndex;
|
|
|
|
}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
final BitcoinTransactionHistory transactionHistory;
|
|
|
|
final String path;
|
|
|
|
bitcoin.HDWallet hd;
|
|
|
|
final ElectrumClient eclient;
|
|
|
|
final String mnemonic;
|
|
|
|
int _accountIndex;
|
|
|
|
String _password;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
String name;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
@observable
|
|
|
|
String address;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
@observable
|
|
|
|
BitcoinBalance balance;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
final type = WalletType.bitcoin;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
ObservableList<BitcoinAddressRecord> addresses;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
String get xpub => hd.base58;
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
Future<void> init() async {
|
|
|
|
await transactionHistory.init();
|
|
|
|
}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
Future<BitcoinAddressRecord> generateNewAddress({String label}) async {
|
|
|
|
_accountIndex += 1;
|
|
|
|
final address = BitcoinAddressRecord(
|
|
|
|
_getAddress(hd: hd, index: _accountIndex),
|
|
|
|
label: label);
|
|
|
|
addresses.add(address);
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
await save();
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
return address;
|
|
|
|
}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
Future<void> updateAddress(String address, {String label}) async {
|
|
|
|
for (final addr in addresses) {
|
|
|
|
if (addr.address == address) {
|
|
|
|
addr.label = label;
|
|
|
|
await save();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
Future<void> startSync() async {}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
Future<void> connectToNode({@required Node node}) async {}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
Future<void> createTransaction(Object credentials) async {}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
Future<void> save() async =>
|
|
|
|
await write(path: path, password: _password, data: toJSON());
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
String toJSON() => json.encode({
|
|
|
|
'mnemonic': mnemonic,
|
|
|
|
'account_index': _accountIndex.toString(),
|
|
|
|
'addresses': addresses.map((addr) => addr.toJSON()).toList(),
|
|
|
|
'balance': balance?.toJSON()
|
|
|
|
});
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
String _getAddress({bitcoin.HDWallet hd, int index}) => bitcoin
|
|
|
|
.P2PKH(
|
|
|
|
data: PaymentData(
|
|
|
|
pubkey: Uint8List.fromList(hd.derive(index).pubKey.codeUnits)))
|
|
|
|
.data
|
|
|
|
.address;
|
|
|
|
|
|
|
|
Future<Map<String, int>> _fetchBalances() async {
|
|
|
|
final balances = await Future.wait(
|
2020-06-20 07:10:00 +00:00
|
|
|
addresses.map((record) => eclient.getBalance(address: record.address)));
|
|
|
|
final balance = balances.fold(<String, int>{}, (Map<String, int> acc, val) {
|
2020-05-12 12:04:54 +00:00
|
|
|
acc['confirmed'] =
|
|
|
|
(val['confirmed'] as int ?? 0) + (acc['confirmed'] ?? 0);
|
|
|
|
acc['unconfirmed'] =
|
|
|
|
(val['unconfirmed'] as int ?? 0) + (acc['unconfirmed'] ?? 0);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
});
|
|
|
|
|
|
|
|
return balance;
|
|
|
|
}
|
|
|
|
}
|