2023-09-14 23:34:01 +00:00
|
|
|
import 'package:bip39/bip39.dart' as bip39;
|
2023-10-30 20:02:33 +00:00
|
|
|
import 'package:coinlib_flutter/coinlib_flutter.dart' as coinlib;
|
2023-09-14 23:34:01 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2023-10-31 16:06:35 +00:00
|
|
|
import 'package:stackwallet/models/balance.dart';
|
2023-11-08 19:57:38 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
2023-10-31 16:06:35 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-09-14 23:34:01 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
2023-11-06 18:26:33 +00:00
|
|
|
import 'package:stackwallet/wallets/crypto_currency/intermediate/bip39_hd_currency.dart';
|
2023-11-04 01:18:22 +00:00
|
|
|
import 'package:stackwallet/wallets/wallet/intermediate/bip39_wallet.dart';
|
2023-11-16 22:25:20 +00:00
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/multi_address_interface.dart';
|
2023-09-14 23:34:01 +00:00
|
|
|
|
2023-11-06 21:37:44 +00:00
|
|
|
abstract class Bip39HDWallet<T extends Bip39HDCurrency> extends Bip39Wallet<T>
|
2023-11-16 22:25:20 +00:00
|
|
|
with MultiAddressInterface<T> {
|
2023-11-06 18:26:33 +00:00
|
|
|
Bip39HDWallet(T cryptoCurrency) : super(cryptoCurrency);
|
2023-09-14 23:34:01 +00:00
|
|
|
|
2023-11-07 18:19:42 +00:00
|
|
|
Future<coinlib.HDPrivateKey> getRootHDNode() async {
|
|
|
|
final seed = bip39.mnemonicToSeed(
|
|
|
|
await getMnemonic(),
|
|
|
|
passphrase: await getMnemonicPassphrase(),
|
|
|
|
);
|
|
|
|
return coinlib.HDPrivateKey.fromSeed(seed);
|
|
|
|
}
|
|
|
|
|
2023-11-07 16:25:04 +00:00
|
|
|
/// Generates a receiving address. If none
|
2023-09-14 23:34:01 +00:00
|
|
|
/// are in the current wallet db it will generate at index 0, otherwise the
|
|
|
|
/// highest index found in the current wallet db.
|
2023-11-06 21:37:44 +00:00
|
|
|
@override
|
|
|
|
Future<void> generateNewReceivingAddress() async {
|
2023-11-03 19:46:55 +00:00
|
|
|
final current = await getCurrentReceivingAddress();
|
2023-11-08 21:49:28 +00:00
|
|
|
final index = current == null ? 0 : current.derivationIndex + 1;
|
2023-09-14 23:34:01 +00:00
|
|
|
const chain = 0; // receiving address
|
|
|
|
|
|
|
|
final address = await _generateAddress(
|
|
|
|
chain: chain,
|
|
|
|
index: index,
|
2023-11-07 16:25:04 +00:00
|
|
|
derivePathType: DerivePathTypeExt.primaryFor(info.coin),
|
2023-09-14 23:34:01 +00:00
|
|
|
);
|
|
|
|
|
2024-01-10 22:48:41 +00:00
|
|
|
await mainDB.updateOrPutAddresses([address]);
|
2023-11-03 19:46:55 +00:00
|
|
|
await info.updateReceivingAddress(
|
|
|
|
newAddress: address.value,
|
|
|
|
isar: mainDB.isar,
|
|
|
|
);
|
2023-09-14 23:34:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 16:25:04 +00:00
|
|
|
/// Generates a change address. If none
|
|
|
|
/// are in the current wallet db it will generate at index 0, otherwise the
|
|
|
|
/// highest index found in the current wallet db.
|
|
|
|
@override
|
|
|
|
Future<void> generateNewChangeAddress() async {
|
2023-11-08 19:57:38 +00:00
|
|
|
final current = await getCurrentChangeAddress();
|
2023-11-08 21:49:28 +00:00
|
|
|
final index = current == null ? 0 : current.derivationIndex + 1;
|
2023-11-08 19:57:38 +00:00
|
|
|
const chain = 1; // change address
|
|
|
|
|
|
|
|
final address = await _generateAddress(
|
|
|
|
chain: chain,
|
|
|
|
index: index,
|
|
|
|
derivePathType: DerivePathTypeExt.primaryFor(info.coin),
|
|
|
|
);
|
2023-11-07 16:25:04 +00:00
|
|
|
|
2024-01-10 22:48:41 +00:00
|
|
|
await mainDB.updateOrPutAddresses([address]);
|
2023-11-07 16:25:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 20:01:47 +00:00
|
|
|
// ========== Subclasses may override ========================================
|
|
|
|
|
|
|
|
/// To be overridden by crypto currencies that do extra address conversions
|
|
|
|
/// on top of the normal btc style address. (BCH and Ecash for example)
|
|
|
|
String convertAddressString(String address) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2023-09-14 23:34:01 +00:00
|
|
|
// ========== Private ========================================================
|
|
|
|
|
|
|
|
Future<Address> _generateAddress({
|
|
|
|
required int chain,
|
|
|
|
required int index,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
}) async {
|
2023-11-07 18:19:42 +00:00
|
|
|
final root = await getRootHDNode();
|
2023-09-14 23:34:01 +00:00
|
|
|
|
|
|
|
final derivationPath = cryptoCurrency.constructDerivePath(
|
|
|
|
derivePathType: derivePathType,
|
|
|
|
chain: chain,
|
|
|
|
index: index,
|
|
|
|
);
|
|
|
|
|
|
|
|
final keys = root.derivePath(derivationPath);
|
|
|
|
|
|
|
|
final data = cryptoCurrency.getAddressForPublicKey(
|
|
|
|
publicKey: keys.publicKey,
|
|
|
|
derivePathType: derivePathType,
|
|
|
|
);
|
|
|
|
|
|
|
|
final AddressSubType subType;
|
|
|
|
|
|
|
|
if (chain == 0) {
|
|
|
|
subType = AddressSubType.receiving;
|
|
|
|
} else if (chain == 1) {
|
|
|
|
subType = AddressSubType.change;
|
|
|
|
} else {
|
2023-10-30 22:58:15 +00:00
|
|
|
// TODO: [prio=low] others or throw?
|
2023-09-14 23:34:01 +00:00
|
|
|
subType = AddressSubType.unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Address(
|
|
|
|
walletId: walletId,
|
2023-11-14 20:01:47 +00:00
|
|
|
value: convertAddressString(data.address.toString()),
|
2023-09-14 23:34:01 +00:00
|
|
|
publicKey: keys.publicKey.data,
|
|
|
|
derivationIndex: index,
|
|
|
|
derivationPath: DerivationPath()..value = derivationPath,
|
|
|
|
type: data.addressType,
|
|
|
|
subType: subType,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ========== Overrides ======================================================
|
|
|
|
|
2023-10-31 16:06:35 +00:00
|
|
|
@override
|
|
|
|
Future<void> updateBalance() async {
|
|
|
|
final utxos = await mainDB.getUTXOs(walletId).findAll();
|
|
|
|
|
|
|
|
final currentChainHeight = await chainHeight;
|
|
|
|
|
|
|
|
Amount satoshiBalanceTotal = Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
Amount satoshiBalancePending = Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
Amount satoshiBalanceSpendable = Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
Amount satoshiBalanceBlocked = Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
|
|
|
|
for (final utxo in utxos) {
|
|
|
|
final utxoAmount = Amount(
|
|
|
|
rawValue: BigInt.from(utxo.value),
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
|
|
|
|
satoshiBalanceTotal += utxoAmount;
|
|
|
|
|
|
|
|
if (utxo.isBlocked) {
|
|
|
|
satoshiBalanceBlocked += utxoAmount;
|
|
|
|
} else {
|
|
|
|
if (utxo.isConfirmed(
|
|
|
|
currentChainHeight,
|
|
|
|
cryptoCurrency.minConfirms,
|
|
|
|
)) {
|
|
|
|
satoshiBalanceSpendable += utxoAmount;
|
|
|
|
} else {
|
|
|
|
satoshiBalancePending += utxoAmount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final balance = Balance(
|
|
|
|
total: satoshiBalanceTotal,
|
|
|
|
spendable: satoshiBalanceSpendable,
|
|
|
|
blockedTotal: satoshiBalanceBlocked,
|
|
|
|
pendingSpendable: satoshiBalancePending,
|
|
|
|
);
|
|
|
|
|
2023-11-03 19:46:55 +00:00
|
|
|
await info.updateBalance(newBalance: balance, isar: mainDB.isar);
|
2023-10-31 16:06:35 +00:00
|
|
|
}
|
2023-09-14 23:34:01 +00:00
|
|
|
}
|