mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-22 19:49:22 +00:00
Merge pull request #239 from cake-tech/transactions-bug
Transactions bug
This commit is contained in:
commit
751d5066b9
12 changed files with 377 additions and 151 deletions
|
@ -1,14 +1,18 @@
|
|||
import 'dart:convert';
|
||||
|
||||
class BitcoinAddressRecord {
|
||||
BitcoinAddressRecord(this.address, {this.index, bool isHidden})
|
||||
: _isHidden = isHidden;
|
||||
BitcoinAddressRecord(this.address,
|
||||
{this.index, this.isHidden = false, bool isUsed = false})
|
||||
: _isUsed = isUsed;
|
||||
|
||||
factory BitcoinAddressRecord.fromJSON(String jsonSource) {
|
||||
final decoded = json.decode(jsonSource) as Map;
|
||||
|
||||
return BitcoinAddressRecord(decoded['address'] as String,
|
||||
index: decoded['index'] as int, isHidden: decoded['isHidden'] as bool);
|
||||
return BitcoinAddressRecord(
|
||||
decoded['address'] as String,
|
||||
index: decoded['index'] as int,
|
||||
isHidden: decoded['isHidden'] as bool ?? false,
|
||||
isUsed: decoded['isUsed'] as bool ?? false);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -16,13 +20,21 @@ class BitcoinAddressRecord {
|
|||
o is BitcoinAddressRecord && address == o.address;
|
||||
|
||||
final String address;
|
||||
bool get isHidden => _isHidden ?? false;
|
||||
int index;
|
||||
final bool _isHidden;
|
||||
final bool isHidden;
|
||||
final int index;
|
||||
bool get isUsed => _isUsed;
|
||||
|
||||
@override
|
||||
int get hashCode => address.hashCode;
|
||||
|
||||
bool _isUsed;
|
||||
|
||||
void setAsUsed() => _isUsed = true;
|
||||
|
||||
String toJSON() =>
|
||||
json.encode({'address': address, 'index': index, 'isHidden': isHidden});
|
||||
json.encode({
|
||||
'address': address,
|
||||
'index': index,
|
||||
'isHidden': isHidden,
|
||||
'isUsed': isUsed});
|
||||
}
|
||||
|
|
|
@ -23,7 +23,8 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
|||
@required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
||||
List<BitcoinAddressRecord> initialAddresses,
|
||||
ElectrumBalance initialBalance,
|
||||
int accountIndex = 0})
|
||||
int initialRegularAddressIndex = 0,
|
||||
int initialChangeAddressIndex = 0})
|
||||
: super(
|
||||
mnemonic: mnemonic,
|
||||
password: password,
|
||||
|
@ -34,8 +35,10 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
|||
initialBalance: initialBalance) {
|
||||
walletAddresses = BitcoinWalletAddresses(
|
||||
walletInfo,
|
||||
electrumClient: electrumClient,
|
||||
initialAddresses: initialAddresses,
|
||||
accountIndex: accountIndex,
|
||||
initialRegularAddressIndex: initialRegularAddressIndex,
|
||||
initialChangeAddressIndex: initialChangeAddressIndex,
|
||||
mainHd: hd,
|
||||
sideHd: bitcoin.HDWallet.fromSeed(
|
||||
mnemonicToSeedBytes(mnemonic), network: networkType)
|
||||
|
@ -58,6 +61,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
|||
unspentCoinsInfo: unspentCoinsInfo,
|
||||
initialAddresses: snp.addresses,
|
||||
initialBalance: snp.balance,
|
||||
accountIndex: snp.accountIndex);
|
||||
initialRegularAddressIndex: snp.regularAddressIndex,
|
||||
initialChangeAddressIndex: snp.changeAddressIndex);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
||||
import 'package:cw_bitcoin/electrum.dart';
|
||||
import 'package:cw_bitcoin/utils.dart';
|
||||
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
||||
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
|
||||
|
@ -16,18 +17,21 @@ abstract class BitcoinWalletAddressesBase extends ElectrumWalletAddresses
|
|||
BitcoinWalletAddressesBase(
|
||||
WalletInfo walletInfo,
|
||||
{@required List<BitcoinAddressRecord> initialAddresses,
|
||||
int accountIndex = 0,
|
||||
int initialRegularAddressIndex = 0,
|
||||
int initialChangeAddressIndex = 0,
|
||||
ElectrumClient electrumClient,
|
||||
@required bitcoin.HDWallet mainHd,
|
||||
@required bitcoin.HDWallet sideHd,
|
||||
@required this.networkType})
|
||||
@required bitcoin.NetworkType networkType})
|
||||
: super(
|
||||
walletInfo,
|
||||
initialAddresses: initialAddresses,
|
||||
accountIndex: accountIndex,
|
||||
initialRegularAddressIndex: initialRegularAddressIndex,
|
||||
initialChangeAddressIndex: initialChangeAddressIndex,
|
||||
mainHd: mainHd,
|
||||
sideHd: sideHd);
|
||||
|
||||
bitcoin.NetworkType networkType;
|
||||
sideHd: sideHd,
|
||||
electrumClient: electrumClient,
|
||||
networkType: networkType);
|
||||
|
||||
@override
|
||||
String getAddress({@required int index, @required bitcoin.HDWallet hd}) =>
|
||||
|
|
|
@ -217,23 +217,16 @@ class ElectrumClient {
|
|||
return <String, Object>{};
|
||||
});
|
||||
|
||||
Future<Map<String, Object>> getTransactionExpanded(
|
||||
{@required String hash}) async {
|
||||
try {
|
||||
final originalTx = await getTransactionRaw(hash: hash);
|
||||
final vins = originalTx['vin'] as List<Object>;
|
||||
|
||||
for (dynamic vin in vins) {
|
||||
if (vin is Map<String, Object>) {
|
||||
vin['tx'] = await getTransactionRaw(hash: vin['txid'] as String);
|
||||
Future<String> getTransactionHex(
|
||||
{@required String hash}) async =>
|
||||
call(method: 'blockchain.transaction.get', params: [hash, false])
|
||||
.then((dynamic result) {
|
||||
if (result is String) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return originalTx;
|
||||
} catch (_) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
return '';
|
||||
});
|
||||
|
||||
Future<String> broadcastTransaction(
|
||||
{@required String transactionRaw}) async =>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import 'dart:typed_data';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
||||
import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData;
|
||||
|
@ -8,6 +9,34 @@ import 'package:cw_core/transaction_info.dart';
|
|||
import 'package:cw_core/format_amount.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
|
||||
String addressFromOutput(Uint8List script) {
|
||||
try {
|
||||
return bitcoin.P2PKH(
|
||||
data: PaymentData(output: script),
|
||||
network: bitcoin.bitcoin)
|
||||
.data
|
||||
.address;
|
||||
} catch (_) {}
|
||||
|
||||
try {
|
||||
return bitcoin.P2WPKH(
|
||||
data: PaymentData(output: script),
|
||||
network: bitcoin.bitcoin)
|
||||
.data
|
||||
.address;
|
||||
} catch(_) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
class ElectrumTransactionBundle {
|
||||
ElectrumTransactionBundle(this.originalTransaction, {this.ins, this.time, this.confirmations});
|
||||
final bitcoin.Transaction originalTransaction;
|
||||
final List<bitcoin.Transaction> ins;
|
||||
final int time;
|
||||
final int confirmations;
|
||||
}
|
||||
|
||||
class ElectrumTransactionInfo extends TransactionInfo {
|
||||
ElectrumTransactionInfo(this.type,
|
||||
{@required String id,
|
||||
|
@ -84,6 +113,49 @@ class ElectrumTransactionInfo extends TransactionInfo {
|
|||
confirmations: confirmations);
|
||||
}
|
||||
|
||||
factory ElectrumTransactionInfo.fromElectrumBundle(
|
||||
ElectrumTransactionBundle bundle, WalletType type,
|
||||
{@required Set<String> addresses, int height}) {
|
||||
final date = DateTime.fromMillisecondsSinceEpoch(bundle.time * 1000);
|
||||
var direction = TransactionDirection.incoming;
|
||||
var amount = 0;
|
||||
var inputAmount = 0;
|
||||
var totalOutAmount = 0;
|
||||
|
||||
for (var i = 0; i < bundle.originalTransaction.ins.length; i++) {
|
||||
final input = bundle.originalTransaction.ins[i];
|
||||
final inputTransaction = bundle.ins[i];
|
||||
final vout = input.index;
|
||||
final outTransaction = inputTransaction.outs[vout];
|
||||
final address = addressFromOutput(outTransaction.script);
|
||||
inputAmount += outTransaction.value;
|
||||
if (addresses.contains(address)) {
|
||||
direction = TransactionDirection.outgoing;
|
||||
}
|
||||
}
|
||||
|
||||
for (final out in bundle.originalTransaction.outs) {
|
||||
totalOutAmount += out.value;
|
||||
final address = addressFromOutput(out.script);
|
||||
final addressExists = addresses.contains(address);
|
||||
if ((direction == TransactionDirection.incoming && addressExists) ||
|
||||
(direction == TransactionDirection.outgoing && !addressExists)) {
|
||||
amount += out.value;
|
||||
}
|
||||
}
|
||||
|
||||
final fee = inputAmount - totalOutAmount;
|
||||
return ElectrumTransactionInfo(type,
|
||||
id: bundle.originalTransaction.getId(),
|
||||
height: height,
|
||||
isPending: false,
|
||||
fee: fee,
|
||||
direction: direction,
|
||||
amount: amount,
|
||||
date: date,
|
||||
confirmations: bundle.confirmations);
|
||||
}
|
||||
|
||||
factory ElectrumTransactionInfo.fromHexAndHeader(WalletType type, String hex,
|
||||
{List<String> addresses, int height, int timestamp, int confirmations}) {
|
||||
final tx = bitcoin.Transaction.fromHex(hex);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
import 'package:cw_core/unspent_coins_info.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
|
||||
|
@ -31,6 +32,7 @@ import 'package:cw_core/sync_status.dart';
|
|||
import 'package:cw_core/transaction_priority.dart';
|
||||
import 'package:cw_core/wallet_info.dart';
|
||||
import 'package:cw_bitcoin/electrum.dart';
|
||||
import 'package:hex/hex.dart';
|
||||
|
||||
part 'electrum_wallet.g.dart';
|
||||
|
||||
|
@ -123,6 +125,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
Future<void> startSync() async {
|
||||
try {
|
||||
syncStatus = StartingSyncStatus();
|
||||
await walletAddresses.discoverAddresses();
|
||||
await updateTransactions();
|
||||
_subscribeForUpdates();
|
||||
await _updateBalance();
|
||||
|
@ -204,12 +207,10 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
}
|
||||
|
||||
amount = credentialsAmount;
|
||||
|
||||
fee = calculateEstimatedFee(transactionCredentials.priority, amount,
|
||||
outputsCount: outputs.length + 1);
|
||||
} else {
|
||||
final output = outputs.first;
|
||||
|
||||
credentialsAmount = !output.sendAll
|
||||
? output.formattedCryptoAmount
|
||||
: 0;
|
||||
|
@ -221,7 +222,6 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
amount = output.sendAll || allAmount - credentialsAmount < minAmount
|
||||
? allAmount
|
||||
: credentialsAmount;
|
||||
|
||||
fee = output.sendAll || amount == allAmount
|
||||
? allAmountFee
|
||||
: calculateEstimatedFee(transactionCredentials.priority, amount);
|
||||
|
@ -238,7 +238,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
}
|
||||
|
||||
final txb = bitcoin.TransactionBuilder(network: networkType);
|
||||
final changeAddress = walletAddresses.addresses.last.address;
|
||||
final changeAddress = await walletAddresses.getChangeAddress();
|
||||
var leftAmount = totalAmount;
|
||||
var totalInputAmount = 0;
|
||||
|
||||
|
@ -265,7 +265,6 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
}
|
||||
|
||||
txb.setVersion(1);
|
||||
|
||||
inputs.forEach((input) {
|
||||
if (input.isP2wpkh) {
|
||||
final p2wpkh = bitcoin
|
||||
|
@ -286,11 +285,9 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
final outputAmount = hasMultiDestination
|
||||
? item.formattedCryptoAmount
|
||||
: amount;
|
||||
|
||||
final outputAddress = item.isParsedAddress
|
||||
? item.extractedAddress
|
||||
: item.address;
|
||||
|
||||
txb.addOutput(
|
||||
addressToOutputScript(outputAddress, networkType),
|
||||
outputAmount);
|
||||
|
@ -326,7 +323,8 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
|
||||
String toJSON() => json.encode({
|
||||
'mnemonic': mnemonic,
|
||||
'account_index': walletAddresses.accountIndex.toString(),
|
||||
'account_index': walletAddresses.currentReceiveAddressIndex.toString(),
|
||||
'change_address_index': walletAddresses.currentChangeAddressIndex.toString(),
|
||||
'addresses': walletAddresses.addresses.map((addr) => addr.toJSON()).toList(),
|
||||
'balance': balance?.toJSON()
|
||||
});
|
||||
|
@ -479,11 +477,35 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
}
|
||||
}
|
||||
|
||||
Future<ElectrumTransactionBundle> getTransactionExpanded(
|
||||
{@required String hash, @required int height}) async {
|
||||
final verboseTransaction = await electrumClient.getTransactionRaw(hash: hash);
|
||||
final transactionHex = verboseTransaction['hex'] as String;
|
||||
final original = bitcoin.Transaction.fromHex(transactionHex);
|
||||
final ins = <bitcoin.Transaction>[];
|
||||
final time = verboseTransaction['time'] as int;
|
||||
final confirmations = verboseTransaction['time'] as int;
|
||||
|
||||
for (final vin in original.ins) {
|
||||
final id = HEX.encode(vin.hash.reversed.toList());
|
||||
final txHex = await electrumClient.getTransactionHex(hash: id);
|
||||
final tx = bitcoin.Transaction.fromHex(txHex);
|
||||
ins.add(tx);
|
||||
}
|
||||
|
||||
return ElectrumTransactionBundle(
|
||||
original,
|
||||
ins: ins,
|
||||
time: time,
|
||||
confirmations: confirmations);
|
||||
}
|
||||
|
||||
Future<ElectrumTransactionInfo> fetchTransactionInfo(
|
||||
{@required String hash, @required int height}) async {
|
||||
final tx = await electrumClient.getTransactionExpanded(hash: hash);
|
||||
return ElectrumTransactionInfo.fromElectrumVerbose(tx, walletInfo.type,
|
||||
height: height, addresses: walletAddresses.addresses);
|
||||
final tx = await getTransactionExpanded(hash: hash, height: height);
|
||||
final addresses = walletAddresses.addresses.map((addr) => addr.address).toSet();
|
||||
return ElectrumTransactionInfo.fromElectrumBundle(
|
||||
tx,walletInfo.type, addresses: addresses, height: height);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -524,7 +546,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
scriptHashes.forEach((sh) async {
|
||||
await _scripthashesUpdateSubject[sh]?.close();
|
||||
_scripthashesUpdateSubject[sh] = electrumClient.scripthashUpdate(sh);
|
||||
_scripthashesUpdateSubject[sh].listen((event) async {
|
||||
_scripthashesUpdateSubject[sh]?.listen((event) async {
|
||||
try {
|
||||
await _updateBalance();
|
||||
await updateUnspent();
|
||||
|
@ -537,16 +559,34 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
}
|
||||
|
||||
Future<ElectrumBalance> _fetchBalances() async {
|
||||
final balances = await Future.wait(
|
||||
scriptHashes.map((sh) => electrumClient.getBalance(sh)));
|
||||
final balance = balances.fold(
|
||||
ElectrumBalance(confirmed: 0, unconfirmed: 0),
|
||||
(ElectrumBalance acc, val) => ElectrumBalance(
|
||||
confirmed: (val['confirmed'] as int ?? 0) + (acc.confirmed ?? 0),
|
||||
unconfirmed:
|
||||
(val['unconfirmed'] as int ?? 0) + (acc.unconfirmed ?? 0)));
|
||||
final addresses = walletAddresses.addresses.toList();
|
||||
final balanceFutures = <Future<Map<String, dynamic>>>[];
|
||||
|
||||
return balance;
|
||||
for (var i = 0; i < addresses.length; i++) {
|
||||
final addressRecord = addresses[i];
|
||||
final sh = scriptHash(addressRecord.address, networkType: networkType);
|
||||
final balanceFuture = electrumClient.getBalance(sh);
|
||||
balanceFutures.add(balanceFuture);
|
||||
}
|
||||
|
||||
final balances = await Future.wait(balanceFutures);
|
||||
var totalConfirmed = 0;
|
||||
var totalUnconfirmed = 0;
|
||||
|
||||
for (var i = 0; i < balances.length; i++) {
|
||||
final addressRecord = addresses[i];
|
||||
final balance = balances[i];
|
||||
final confirmed = balance['confirmed'] as int ?? 0;
|
||||
final unconfirmed = balance['unconfirmed'] as int ?? 0;
|
||||
totalConfirmed += confirmed;
|
||||
totalUnconfirmed += unconfirmed;
|
||||
|
||||
if (confirmed > 0 || unconfirmed > 0) {
|
||||
addressRecord.setAsUsed();
|
||||
}
|
||||
}
|
||||
|
||||
return ElectrumBalance(confirmed: totalConfirmed, unconfirmed: totalUnconfirmed);
|
||||
}
|
||||
|
||||
Future<void> _updateBalance() async {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
||||
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
||||
import 'package:cw_bitcoin/electrum.dart';
|
||||
import 'package:cw_bitcoin/script_hash.dart';
|
||||
import 'package:cw_core/wallet_addresses.dart';
|
||||
import 'package:cw_core/wallet_info.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
@ -14,120 +16,118 @@ class ElectrumWalletAddresses = ElectrumWalletAddressesBase
|
|||
abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
|
||||
ElectrumWalletAddressesBase(WalletInfo walletInfo,
|
||||
{@required List<BitcoinAddressRecord> initialAddresses,
|
||||
int accountIndex = 0,
|
||||
int initialRegularAddressIndex = 0,
|
||||
int initialChangeAddressIndex = 0,
|
||||
this.mainHd,
|
||||
this.sideHd})
|
||||
this.sideHd,
|
||||
this.electrumClient,
|
||||
this.networkType})
|
||||
: super(walletInfo) {
|
||||
this.accountIndex = accountIndex;
|
||||
currentReceiveAddressIndex = initialRegularAddressIndex;
|
||||
currentChangeAddressIndex = initialChangeAddressIndex;
|
||||
addresses = ObservableList<BitcoinAddressRecord>.of(
|
||||
(initialAddresses ?? []).toSet());
|
||||
}
|
||||
|
||||
static const regularAddressesCount = 22;
|
||||
static const hiddenAddressesCount = 17;
|
||||
static const defaultReceiveAddressesCount = 22;
|
||||
static const defaultChangeAddressesCount = 17;
|
||||
static const gap = 20;
|
||||
|
||||
@override
|
||||
@observable
|
||||
String address;
|
||||
|
||||
int currentReceiveAddressIndex;
|
||||
int currentChangeAddressIndex;
|
||||
ElectrumClient electrumClient;
|
||||
bitcoin.NetworkType networkType;
|
||||
bitcoin.HDWallet mainHd;
|
||||
bitcoin.HDWallet sideHd;
|
||||
|
||||
ObservableList<BitcoinAddressRecord> addresses;
|
||||
|
||||
List<BitcoinAddressRecord> get availableAddresses => addresses
|
||||
.where((addr) => !addr.isHidden)
|
||||
List<BitcoinAddressRecord> get receiveAddresses => addresses
|
||||
.where((addr) => !addr.isHidden && !addr.isUsed)
|
||||
.toList();
|
||||
|
||||
int accountIndex;
|
||||
List<BitcoinAddressRecord> get changeAddresses => addresses
|
||||
.where((addr) => addr.isHidden && !addr.isUsed)
|
||||
.toList();
|
||||
|
||||
Future<void> discoverAddresses() async {
|
||||
await _discoverAddresses(mainHd, false);
|
||||
await _discoverAddresses(sideHd, true);
|
||||
await updateAddressesInBox();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> init() async {
|
||||
await generateAddresses();
|
||||
final _availableAddresses = availableAddresses;
|
||||
await _generateInitialAddresses();
|
||||
|
||||
if (accountIndex >= _availableAddresses.length) {
|
||||
accountIndex = 0;
|
||||
if (receiveAddresses.isEmpty) {
|
||||
final count = currentReceiveAddressIndex + gap;
|
||||
final newAddresses = await _createNewAddresses(
|
||||
count,
|
||||
hd: mainHd,
|
||||
startIndex: currentReceiveAddressIndex,
|
||||
isHidden: false);
|
||||
_addAddresses(newAddresses);
|
||||
} else if (currentReceiveAddressIndex >= receiveAddresses.length) {
|
||||
currentReceiveAddressIndex = 0;
|
||||
}
|
||||
|
||||
address = _availableAddresses[accountIndex].address;
|
||||
address = receiveAddresses[currentReceiveAddressIndex].address;
|
||||
await updateAddressesInBox();
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> nextAddress() async {
|
||||
accountIndex += 1;
|
||||
final _availableAddresses = availableAddresses;
|
||||
|
||||
if (accountIndex >= _availableAddresses.length) {
|
||||
accountIndex = 0;
|
||||
Future<void> nextReceiveAddress() async {
|
||||
if (receiveAddresses.isEmpty) {
|
||||
final count = currentReceiveAddressIndex + gap;
|
||||
final newAddresses = await _createNewAddresses(
|
||||
count,
|
||||
hd: sideHd,
|
||||
startIndex: currentReceiveAddressIndex,
|
||||
isHidden: false);
|
||||
_addAddresses(newAddresses);
|
||||
} else if (currentReceiveAddressIndex >= receiveAddresses.length) {
|
||||
currentReceiveAddressIndex = 0;
|
||||
}
|
||||
|
||||
address = _availableAddresses[accountIndex].address;
|
||||
|
||||
address = receiveAddresses[currentReceiveAddressIndex].address;
|
||||
currentReceiveAddressIndex += 1;
|
||||
await updateAddressesInBox();
|
||||
}
|
||||
|
||||
Future<void> generateAddresses() async {
|
||||
final regularAddresses = <BitcoinAddressRecord>[];
|
||||
final hiddenAddresses = <BitcoinAddressRecord>[];
|
||||
|
||||
addresses.forEach((addr) {
|
||||
if (addr.isHidden) {
|
||||
hiddenAddresses.add(addr);
|
||||
return;
|
||||
}
|
||||
|
||||
regularAddresses.add(addr);
|
||||
});
|
||||
|
||||
if (regularAddresses.length < regularAddressesCount) {
|
||||
final addressesCount = regularAddressesCount - regularAddresses.length;
|
||||
await generateNewAddresses(addressesCount,
|
||||
startIndex: regularAddresses.length, hd: mainHd, isHidden: false);
|
||||
@action
|
||||
Future<String> getChangeAddress() async {
|
||||
if (changeAddresses.isEmpty) {
|
||||
final count = currentChangeAddressIndex + gap;
|
||||
final newAddresses = await _createNewAddresses(
|
||||
count,
|
||||
startIndex: currentChangeAddressIndex,
|
||||
isHidden: true);
|
||||
_addAddresses(newAddresses);
|
||||
} else if (currentChangeAddressIndex >= changeAddresses.length) {
|
||||
currentChangeAddressIndex = 0;
|
||||
}
|
||||
|
||||
if (hiddenAddresses.length < hiddenAddressesCount) {
|
||||
final addressesCount = hiddenAddressesCount - hiddenAddresses.length;
|
||||
await generateNewAddresses(addressesCount,
|
||||
startIndex: hiddenAddresses.length, hd: sideHd, isHidden: true);
|
||||
}
|
||||
|
||||
final address = changeAddresses[currentChangeAddressIndex].address;
|
||||
currentChangeAddressIndex += 1;
|
||||
return address;
|
||||
}
|
||||
|
||||
Future<BitcoinAddressRecord> generateNewAddress(
|
||||
{bool isHidden = false, bitcoin.HDWallet hd}) async {
|
||||
accountIndex += 1;
|
||||
currentReceiveAddressIndex += 1;
|
||||
final address = BitcoinAddressRecord(
|
||||
getAddress(index: accountIndex, hd: hd),
|
||||
index: accountIndex,
|
||||
getAddress(index: currentReceiveAddressIndex, hd: hd),
|
||||
index: currentReceiveAddressIndex,
|
||||
isHidden: isHidden);
|
||||
addresses.add(address);
|
||||
return address;
|
||||
}
|
||||
|
||||
Future<List<BitcoinAddressRecord>> generateNewAddresses(int count,
|
||||
{int startIndex = 0, bitcoin.HDWallet hd, bool isHidden = false}) async {
|
||||
final list = <BitcoinAddressRecord>[];
|
||||
|
||||
for (var i = startIndex; i < count + startIndex; i++) {
|
||||
final address = BitcoinAddressRecord(getAddress(index: i, hd: hd),
|
||||
index: i, isHidden: isHidden);
|
||||
list.add(address);
|
||||
}
|
||||
|
||||
addresses.addAll(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
/*Future<void> updateAddress(String address) async {
|
||||
for (final addr in addresses) {
|
||||
if (addr.address == address) {
|
||||
await save();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
String getAddress({@required int index, @required bitcoin.HDWallet hd}) => '';
|
||||
|
||||
@override
|
||||
|
@ -135,7 +135,6 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
|
|||
try {
|
||||
addressesMap.clear();
|
||||
addressesMap[address] = '';
|
||||
|
||||
await saveAddressesInBox();
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
|
@ -155,4 +154,107 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
|
|||
|
||||
address = availableAddresses[random.nextInt(availableAddresses.length)].address;
|
||||
}
|
||||
|
||||
Future<void> _discoverAddresses(bitcoin.HDWallet hd, bool isHidden) async {
|
||||
var hasAddrUse = true;
|
||||
List<BitcoinAddressRecord> addrs;
|
||||
|
||||
if (addresses.isNotEmpty) {
|
||||
addrs = addresses
|
||||
.where((addr) => addr.isHidden == isHidden)
|
||||
.toList();
|
||||
} else {
|
||||
addrs = await _createNewAddresses(
|
||||
isHidden
|
||||
? defaultChangeAddressesCount
|
||||
: defaultReceiveAddressesCount,
|
||||
startIndex: 0,
|
||||
hd: hd,
|
||||
isHidden: isHidden);
|
||||
}
|
||||
|
||||
while(hasAddrUse) {
|
||||
final addr = addrs.last.address;
|
||||
hasAddrUse = await _validateAddressUsing(addr);
|
||||
|
||||
if (!hasAddrUse) {
|
||||
break;
|
||||
}
|
||||
|
||||
final start = addrs.length;
|
||||
final count = start + gap;
|
||||
final batch = await _createNewAddresses(
|
||||
count,
|
||||
startIndex: start,
|
||||
hd: hd,
|
||||
isHidden: isHidden);
|
||||
addrs.addAll(batch);
|
||||
}
|
||||
|
||||
if (addresses.length < addrs.length) {
|
||||
_addAddresses(addrs);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _generateInitialAddresses() async {
|
||||
var countOfReceiveAddresses = 0;
|
||||
var countOfHiddenAddresses = 0;
|
||||
|
||||
addresses.forEach((addr) {
|
||||
if (addr.isHidden) {
|
||||
countOfHiddenAddresses += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
countOfReceiveAddresses += 1;
|
||||
});
|
||||
|
||||
if (countOfReceiveAddresses < defaultReceiveAddressesCount) {
|
||||
final addressesCount = defaultReceiveAddressesCount - countOfReceiveAddresses;
|
||||
final newAddresses = await _createNewAddresses(
|
||||
addressesCount,
|
||||
startIndex: countOfReceiveAddresses,
|
||||
hd: mainHd,
|
||||
isHidden: false);
|
||||
addresses.addAll(newAddresses);
|
||||
}
|
||||
|
||||
if (countOfHiddenAddresses < defaultChangeAddressesCount) {
|
||||
final addressesCount = defaultChangeAddressesCount - countOfHiddenAddresses;
|
||||
final newAddresses = await _createNewAddresses(
|
||||
addressesCount,
|
||||
startIndex: countOfHiddenAddresses,
|
||||
hd: sideHd,
|
||||
isHidden: true);
|
||||
addresses.addAll(newAddresses);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<BitcoinAddressRecord>> _createNewAddresses(int count,
|
||||
{int startIndex = 0, bitcoin.HDWallet hd, bool isHidden = false}) async {
|
||||
final list = <BitcoinAddressRecord>[];
|
||||
|
||||
for (var i = startIndex; i < count + startIndex; i++) {
|
||||
final address = BitcoinAddressRecord(
|
||||
getAddress(index: i, hd: hd),
|
||||
index: i,
|
||||
isHidden: isHidden);
|
||||
list.add(address);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void _addAddresses(Iterable<BitcoinAddressRecord> addresses) {
|
||||
final addressesSet = this.addresses.toSet();
|
||||
addressesSet.addAll(addresses);
|
||||
this.addresses.removeRange(0, this.addresses.length);
|
||||
this.addresses.addAll(addressesSet);
|
||||
}
|
||||
|
||||
Future<bool> _validateAddressUsing(String address) async {
|
||||
final sh = scriptHash(address, networkType: networkType);
|
||||
final balance = await electrumClient.getBalance(sh);
|
||||
return balance.isEmpty;
|
||||
}
|
||||
}
|
|
@ -15,7 +15,8 @@ class ElectrumWallletSnapshot {
|
|||
String mnemonic;
|
||||
List<BitcoinAddressRecord> addresses;
|
||||
ElectrumBalance balance;
|
||||
int accountIndex;
|
||||
int regularAddressIndex;
|
||||
int changeAddressIndex;
|
||||
|
||||
Future<void> load() async {
|
||||
try {
|
||||
|
@ -30,10 +31,12 @@ class ElectrumWallletSnapshot {
|
|||
.toList();
|
||||
balance = ElectrumBalance.fromJSON(data['balance'] as String) ??
|
||||
ElectrumBalance(confirmed: 0, unconfirmed: 0);
|
||||
accountIndex = 0;
|
||||
regularAddressIndex = 0;
|
||||
changeAddressIndex = 0;
|
||||
|
||||
try {
|
||||
accountIndex = int.parse(data['account_index'] as String);
|
||||
regularAddressIndex = int.parse(data['account_index'] as String);
|
||||
changeAddressIndex = int.parse(data['change_address_index'] as String);
|
||||
} catch (_) {}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
|
|
|
@ -26,7 +26,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
|||
@required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
||||
List<BitcoinAddressRecord> initialAddresses,
|
||||
ElectrumBalance initialBalance,
|
||||
int accountIndex = 0})
|
||||
int initialRegularAddressIndex = 0,
|
||||
int initialChangeAddressIndex = 0})
|
||||
: super(
|
||||
mnemonic: mnemonic,
|
||||
password: password,
|
||||
|
@ -37,8 +38,10 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
|||
initialBalance: initialBalance) {
|
||||
walletAddresses = LitecoinWalletAddresses(
|
||||
walletInfo,
|
||||
electrumClient: electrumClient,
|
||||
initialAddresses: initialAddresses,
|
||||
accountIndex: accountIndex,
|
||||
initialRegularAddressIndex: initialRegularAddressIndex,
|
||||
initialChangeAddressIndex: initialChangeAddressIndex,
|
||||
mainHd: hd,
|
||||
sideHd: bitcoin.HDWallet
|
||||
.fromSeed(mnemonicToSeedBytes(mnemonic), network: networkType)
|
||||
|
@ -61,7 +64,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
|||
unspentCoinsInfo: unspentCoinsInfo,
|
||||
initialAddresses: snp.addresses,
|
||||
initialBalance: snp.balance,
|
||||
accountIndex: snp.accountIndex);
|
||||
initialRegularAddressIndex: snp.regularAddressIndex,
|
||||
initialChangeAddressIndex: snp.changeAddressIndex);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
||||
import 'package:cw_bitcoin/electrum.dart';
|
||||
import 'package:cw_bitcoin/utils.dart';
|
||||
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
||||
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
|
||||
|
@ -16,32 +17,23 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses
|
|||
LitecoinWalletAddressesBase(
|
||||
WalletInfo walletInfo,
|
||||
{@required List<BitcoinAddressRecord> initialAddresses,
|
||||
int accountIndex = 0,
|
||||
int initialRegularAddressIndex = 0,
|
||||
int initialChangeAddressIndex = 0,
|
||||
ElectrumClient electrumClient,
|
||||
@required bitcoin.HDWallet mainHd,
|
||||
@required bitcoin.HDWallet sideHd,
|
||||
@required this.networkType})
|
||||
@required bitcoin.NetworkType networkType})
|
||||
: super(
|
||||
walletInfo,
|
||||
initialAddresses: initialAddresses,
|
||||
accountIndex: accountIndex,
|
||||
initialRegularAddressIndex: initialRegularAddressIndex,
|
||||
initialChangeAddressIndex: initialChangeAddressIndex,
|
||||
mainHd: mainHd,
|
||||
sideHd: sideHd);
|
||||
|
||||
bitcoin.NetworkType networkType;
|
||||
|
||||
sideHd: sideHd,
|
||||
electrumClient: electrumClient,
|
||||
networkType: networkType);
|
||||
|
||||
@override
|
||||
String getAddress({@required int index, @required bitcoin.HDWallet hd}) =>
|
||||
generateP2WPKHAddress(hd: hd, index: index, networkType: networkType);
|
||||
|
||||
@override
|
||||
Future<void> generateAddresses() async {
|
||||
if (addresses.length < 33) {
|
||||
final addressesCount = 22 - addresses.length;
|
||||
await generateNewAddresses(addressesCount,
|
||||
hd: mainHd, startIndex: addresses.length);
|
||||
await generateNewAddresses(11,
|
||||
startIndex: 0, hd: sideHd, isHidden: true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -57,7 +57,7 @@ class CWBitcoin extends Bitcoin {
|
|||
@override
|
||||
Future<void> nextAddress(Object wallet) {
|
||||
final bitcoinWallet = wallet as ElectrumWallet;
|
||||
bitcoinWallet.walletAddresses.nextAddress();
|
||||
bitcoinWallet.walletAddresses.nextReceiveAddress();
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -17,8 +17,8 @@ MONERO_COM_BUILD_NUMBER=6
|
|||
MONERO_COM_BUNDLE_ID="com.cakewallet.monero"
|
||||
|
||||
CAKEWALLET_NAME="Cake Wallet"
|
||||
CAKEWALLET_VERSION="4.3.1"
|
||||
CAKEWALLET_BUILD_NUMBER=73
|
||||
CAKEWALLET_VERSION="4.3.4"
|
||||
CAKEWALLET_BUILD_NUMBER=75
|
||||
CAKEWALLET_BUNDLE_ID="com.fotolockr.cakewallet"
|
||||
|
||||
if ! [[ " ${TYPES[*]} " =~ " ${APP_IOS_TYPE} " ]]; then
|
||||
|
|
Loading…
Reference in a new issue