2024-07-03 21:04:19 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
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';
|
2024-06-20 16:16:12 +00:00
|
|
|
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../../../models/balance.dart';
|
|
|
|
import '../../../models/isar/models/blockchain_data/address.dart';
|
|
|
|
import '../../../utilities/amount/amount.dart';
|
|
|
|
import '../../../utilities/enums/derive_path_type_enum.dart';
|
2024-07-03 21:04:19 +00:00
|
|
|
import '../../../utilities/extensions/extensions.dart';
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../../crypto_currency/intermediate/bip39_hd_currency.dart';
|
|
|
|
import '../wallet_mixin_interfaces/multi_address_interface.dart';
|
2024-06-20 16:16:12 +00:00
|
|
|
import 'bip39_wallet.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> {
|
2024-04-24 22:36:12 +00:00
|
|
|
Bip39HDWallet(super.cryptoCurrency);
|
|
|
|
|
|
|
|
Set<AddressType> get supportedAddressTypes =>
|
|
|
|
cryptoCurrency.supportedDerivationPathTypes
|
|
|
|
.where((e) => e != DerivePathType.bip49)
|
|
|
|
.map((e) => e.getAddressType())
|
|
|
|
.toSet();
|
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);
|
|
|
|
}
|
|
|
|
|
2024-07-03 21:04:19 +00:00
|
|
|
Future<String> getPrivateKeyWIF(Address address) async {
|
|
|
|
final keys =
|
|
|
|
(await getRootHDNode()).derivePath(address.derivationPath!.value);
|
|
|
|
|
|
|
|
final List<int> data = [
|
|
|
|
cryptoCurrency.networkParams.wifPrefix,
|
|
|
|
...keys.privateKey.data,
|
|
|
|
if (keys.privateKey.compressed) 1,
|
|
|
|
];
|
|
|
|
final checksum =
|
|
|
|
coinlib.sha256DoubleHash(Uint8List.fromList(data)).sublist(0, 4);
|
|
|
|
data.addAll(checksum);
|
|
|
|
|
|
|
|
return Uint8List.fromList(data).toBase58Encoded;
|
|
|
|
}
|
|
|
|
|
2024-04-24 22:36:12 +00:00
|
|
|
Future<Address> generateNextReceivingAddress({
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
}) async {
|
|
|
|
if (!cryptoCurrency.supportedDerivationPathTypes.contains(derivePathType)) {
|
|
|
|
throw Exception(
|
|
|
|
"Unsupported DerivePathType passed to generateNextReceivingAddress().",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final current = await mainDB.isar.addresses
|
|
|
|
.where()
|
|
|
|
.walletIdEqualTo(walletId)
|
|
|
|
.filter()
|
|
|
|
.typeEqualTo(derivePathType.getAddressType())
|
|
|
|
.sortByDerivationIndexDesc()
|
|
|
|
.findFirst();
|
|
|
|
final index = current == null ? 0 : current.derivationIndex + 1;
|
|
|
|
const chain = 0; // receiving address
|
|
|
|
final address = await _generateAddress(
|
|
|
|
chain: chain,
|
|
|
|
index: index,
|
|
|
|
derivePathType: derivePathType,
|
|
|
|
);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
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,
|
2024-06-20 16:16:12 +00:00
|
|
|
derivePathType: _fromAddressType(info.mainAddressType),
|
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,
|
2024-06-20 16:16:12 +00:00
|
|
|
derivePathType: _fromAddressType(info.mainAddressType),
|
2023-11-08 19:57:38 +00:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2024-01-16 17:58:16 +00:00
|
|
|
@override
|
|
|
|
Future<void> checkSaveInitialReceivingAddress() async {
|
|
|
|
final current = await getCurrentChangeAddress();
|
|
|
|
if (current == null) {
|
|
|
|
final address = await _generateAddress(
|
|
|
|
chain: 0, // receiving
|
|
|
|
index: 0, // initial index
|
2024-06-20 16:16:12 +00:00
|
|
|
derivePathType: _fromAddressType(info.mainAddressType),
|
2024-01-16 17:58:16 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await mainDB.updateOrPutAddresses([address]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ========================================================
|
|
|
|
|
2024-06-20 16:16:12 +00:00
|
|
|
DerivePathType _fromAddressType(AddressType addressType) {
|
|
|
|
switch (addressType) {
|
|
|
|
case AddressType.p2pkh:
|
|
|
|
// DerivePathType.bip44:
|
|
|
|
// DerivePathType.bch44:
|
|
|
|
// DerivePathType.eCash44:
|
|
|
|
// Should be one of the above due to silly case due to bch and ecash
|
|
|
|
return info.coin.defaultDerivePathType;
|
|
|
|
|
|
|
|
case AddressType.p2sh:
|
|
|
|
return DerivePathType.bip49;
|
|
|
|
|
|
|
|
case AddressType.p2wpkh:
|
|
|
|
return DerivePathType.bip84;
|
|
|
|
|
|
|
|
case AddressType.p2tr:
|
|
|
|
return DerivePathType.bip86;
|
|
|
|
|
|
|
|
case AddressType.solana:
|
|
|
|
return DerivePathType.solana;
|
|
|
|
|
|
|
|
case AddressType.ethereum:
|
|
|
|
return DerivePathType.eth;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw ArgumentError(
|
|
|
|
"Incompatible AddressType \"$addressType\" passed to DerivePathType.fromAddressType()",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 23:34:01 +00:00
|
|
|
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
|
|
|
}
|