mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-16 09:34:06 +01:00
* add Wallet Accounts page and integrate UI * account model refactor * add walletInfoAccount table and API * add WalletAccountList API * add multi-account support for Electrum wallets * add Bitcoin lightning card and card style handling * refine account filtering and card selection * update current account balance on wallet changes * update configure.dar * Use named parameters for card design * Add Bitcoin account handling and refactor lookup * refactor dashboard * refactor cards UI and account balance handling * Improve Bitcoin account selection and balances * restrict unspent coin usage to current account * filter transactions by current Bitcoin account * refactor electrum wallet * refactor address generation * add accountIndex to address fetch logic * Update configure.dart * reload transactions on Bitcoin account change * restore multiple BTC accounts and improve fetch * show account balance with in btc * track account index in Bitcoin transactions * discover btc BIP39 accounts during restore * fix account switching and card ordering bugs * fix card customizer * limit account name length in modal * update BTC account list balance display * auto-reformat * formating fix * skip HD map validation * fix 0 balance issue * minor fix * quick bitcoin wallet sync * restore shuffle change output * restore address lookup fix * Update electrum.dart * remove account customizer * Merge branch 'CW-1142-Add-accounts-feature-to-BTC' into CW-1551-Quick-Bitcoin-Wallet-Sync * probe only segwit in account discovery * add account discovery limit and tracking * Improve Electrum sync account handling [skip ci] * remove legacy monero account and address list UI * rework BTC account balance refres * avoid unnecessary BTC account reloads * refactor account header in addresses page * fix blank popup when editing an account * fix: dedupe UTXO balance * minor fix * add Bitcoin current account API * Apply batched suggestions from code review [skip ci] Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com> * address PR review comments [skip ci] * remove account customizer and fix accounts init * restrict extra BTC accounts to SegWit * add BTC multi-account toggle * Fix BTC sync gating and account balance update * minor fix * Update electrum_wallet.dart * store selected account on WalletInfo * Refine wallet account reset visibility * refactor BTC address prep and move accounts page * refactor account stack and customizer routing * migrate legacy Lightning card styles * unify dashboard account change state * fix account routing and Electrum balances * fix account setup and wallet cards UI * disable Bitcoin Accounts for all hardware * merge conflicts fix * minor fixes * update cards_view [skip ci] * reverted cards view update [skip ci] * fix asset name * moved the transaction filtering into the electrum wallet * localization * disable accounts for watch-only wallets * fix: stop card render from switching the wallet account * fix: restore Lightning transactions in tx history * minor fix * fix: skip duplicate legacy address generation [skip ci] * fix: correct account balance/design mismatch bugs * addressing review comments --------- Co-authored-by: Robert Malikowski <malikowskirobert@gmail.com> Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
256 lines
7.4 KiB
Dart
256 lines
7.4 KiB
Dart
import 'dart:convert';
|
|
import 'package:cw_bitcoin/electrum_derivations.dart';
|
|
import 'package:cw_core/utils/print_verbose.dart';
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:bitcoin_base/bitcoin_base.dart';
|
|
|
|
abstract class BaseBitcoinAddressRecord {
|
|
BaseBitcoinAddressRecord(
|
|
this.address, {
|
|
required this.index,
|
|
this.accountIndex = 0,
|
|
this.isHidden = false,
|
|
this.isLegacyDerivation = false,
|
|
int txCount = 0,
|
|
int balance = 0,
|
|
String name = '',
|
|
bool isUsed = false,
|
|
required this.type,
|
|
required this.network,
|
|
}) : _txCount = txCount,
|
|
_balance = balance,
|
|
_name = name,
|
|
_isUsed = Observable(isUsed);
|
|
|
|
@override
|
|
bool operator ==(Object o) => o is BaseBitcoinAddressRecord && address == o.address;
|
|
|
|
final String address;
|
|
bool isHidden;
|
|
bool isLegacyDerivation;
|
|
final int index;
|
|
final int accountIndex;
|
|
int _txCount;
|
|
int _balance;
|
|
String _name;
|
|
final Observable<bool> _isUsed;
|
|
BasedUtxoNetwork? network;
|
|
|
|
int get txCount => _txCount;
|
|
|
|
String get name => _name;
|
|
|
|
int get balance => _balance;
|
|
|
|
set txCount(int value) => _txCount = value;
|
|
|
|
set balance(int value) => _balance = value;
|
|
|
|
bool get isUsed => _isUsed.value;
|
|
|
|
void setAsUsed() => _isUsed.value = true;
|
|
void setNewName(String label) => _name = label;
|
|
|
|
int get hashCode => address.hashCode;
|
|
|
|
BitcoinAddressType type;
|
|
|
|
String get derivationPath;
|
|
|
|
String toJSON();
|
|
}
|
|
|
|
class BitcoinAddressRecord extends BaseBitcoinAddressRecord {
|
|
BitcoinAddressRecord(
|
|
super.address, {
|
|
required super.index,
|
|
super.accountIndex = 0,
|
|
super.isHidden = false,
|
|
super.isLegacyDerivation = false,
|
|
super.txCount = 0,
|
|
super.balance = 0,
|
|
super.name = '',
|
|
super.isUsed = false,
|
|
required super.type,
|
|
String? scriptHash,
|
|
required super.network,
|
|
}) {
|
|
try {
|
|
this.scriptHash = scriptHash ??
|
|
(network != null ? BitcoinAddressUtils.scriptHash(address, network: network!) : null);
|
|
} catch (e) {
|
|
printV(e);
|
|
}
|
|
}
|
|
|
|
static bool _legacyDefaultForType(BitcoinAddressType type) {
|
|
// Some address types (p2wpkh, p2wsh) were historically derived from the same account/path as our new standard
|
|
// but using legacy formats. For these, we default to legacy = false.
|
|
if (type == SegwitAddresType.p2wpkh || type == SegwitAddresType.p2wsh) return false;
|
|
|
|
// For other types (p2pkh, p2wpkh-in-p2sh, p2tr, etc.), old wallets used the non-standard
|
|
return true;
|
|
}
|
|
|
|
factory BitcoinAddressRecord.fromJSON(String jsonSource, {BasedUtxoNetwork? network}) {
|
|
final decoded = json.decode(jsonSource) as Map;
|
|
|
|
final parsedType = decoded['type'] != null && decoded['type'] != ''
|
|
? BitcoinAddressType.values
|
|
.firstWhere((type) => type.toString() == decoded['type'] as String)
|
|
: SegwitAddresType.p2wpkh;
|
|
|
|
final parsedIsLegacy = decoded.containsKey('isLegacyDerivation')
|
|
? (decoded['isLegacyDerivation'] as bool? ?? false)
|
|
: _legacyDefaultForType(parsedType);
|
|
|
|
return BitcoinAddressRecord(
|
|
decoded['address'] as String,
|
|
index: decoded['index'] as int,
|
|
accountIndex: decoded['accountIndex'] as int? ?? 0,
|
|
isHidden: decoded['isHidden'] as bool? ?? false,
|
|
isLegacyDerivation: parsedIsLegacy,
|
|
isUsed: decoded['isUsed'] as bool? ?? false,
|
|
txCount: decoded['txCount'] as int? ?? 0,
|
|
name: decoded['name'] as String? ?? '',
|
|
balance: decoded['balance'] as int? ?? 0,
|
|
type: parsedType,
|
|
scriptHash: decoded['scriptHash'] as String?,
|
|
network: network,
|
|
);
|
|
}
|
|
|
|
String? scriptHash;
|
|
|
|
static int _purposeForType(BitcoinAddressType type) {
|
|
if (type == P2pkhAddressType.p2pkh) return 44;
|
|
if (type == P2shAddressType.p2wpkhInP2sh) return 49;
|
|
if (type == SegwitAddresType.p2wsh) return 48;
|
|
if (type == SegwitAddresType.p2tr) return 86;
|
|
return 84;
|
|
}
|
|
|
|
int _coinTypeForNetwork() {
|
|
if (!(network?.isMainnet ?? true)) return 1;
|
|
|
|
switch (network) {
|
|
case BitcoinNetwork.mainnet:
|
|
return 0;
|
|
case LitecoinNetwork.mainnet:
|
|
return 2;
|
|
case BitcoinCashNetwork.mainnet:
|
|
return 145;
|
|
case DogecoinNetwork.mainnet:
|
|
return 3;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
String getScriptHash(BasedUtxoNetwork network) {
|
|
if (scriptHash != null) return scriptHash!;
|
|
try {
|
|
scriptHash = BitcoinAddressUtils.scriptHash(address, network: network);
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
return scriptHash!;
|
|
}
|
|
|
|
@override
|
|
String get derivationPath {
|
|
if (type == SegwitAddresType.mweb) {
|
|
return "m/1000'/$index";
|
|
}
|
|
|
|
final coinType = _coinTypeForNetwork();
|
|
final purpose = _purposeForType(type);
|
|
final accountPath =
|
|
isLegacyDerivation ? electrum_path : "m/$purpose'/$coinType'/$accountIndex'";
|
|
|
|
final chain = isHidden ? 1 : 0;
|
|
return "$accountPath/$chain/$index";
|
|
}
|
|
|
|
@override
|
|
String toJSON() => json.encode({
|
|
'address': address,
|
|
'index': index,
|
|
'accountIndex': accountIndex,
|
|
'isHidden': isHidden,
|
|
'isLegacyDerivation': isLegacyDerivation,
|
|
'isUsed': isUsed,
|
|
'txCount': txCount,
|
|
'name': name,
|
|
'balance': balance,
|
|
'type': type.toString(),
|
|
'scriptHash': scriptHash,
|
|
});
|
|
}
|
|
|
|
class BitcoinSilentPaymentAddressRecord extends BaseBitcoinAddressRecord {
|
|
BitcoinSilentPaymentAddressRecord(
|
|
super.address, {
|
|
required super.index,
|
|
super.accountIndex = 0,
|
|
super.isHidden = false,
|
|
super.txCount = 0,
|
|
super.balance = 0,
|
|
super.name = '',
|
|
super.isUsed = false,
|
|
required this.silentPaymentTweak,
|
|
required super.network,
|
|
required super.type,
|
|
this.spendDerivationPath = SILENT_PAYMENTS_SPEND_PATH_TESTNET,
|
|
});
|
|
|
|
factory BitcoinSilentPaymentAddressRecord.fromJSON(String jsonSource,
|
|
{BasedUtxoNetwork? network}) {
|
|
final decoded = json.decode(jsonSource) as Map;
|
|
|
|
return BitcoinSilentPaymentAddressRecord(
|
|
decoded['address'] as String,
|
|
index: decoded['index'] as int,
|
|
accountIndex: decoded['accountIndex'] as int? ?? 0,
|
|
isHidden: decoded['isHidden'] as bool? ?? false,
|
|
isUsed: decoded['isUsed'] as bool? ?? false,
|
|
txCount: decoded['txCount'] as int? ?? 0,
|
|
name: decoded['name'] as String? ?? '',
|
|
balance: decoded['balance'] as int? ?? 0,
|
|
network: (decoded['network'] as String?) == null
|
|
? network
|
|
: BasedUtxoNetwork.fromName(decoded['network'] as String),
|
|
silentPaymentTweak: decoded['silent_payment_tweak'] as String?,
|
|
type: decoded['type'] != null && decoded['type'] != ''
|
|
? BitcoinAddressType.values
|
|
.firstWhere((type) => type.toString() == decoded['type'] as String)
|
|
: SilentPaymentsAddresType.p2sp,
|
|
spendDerivationPath:
|
|
decoded['spend_derivation_path'] as String? ?? SILENT_PAYMENTS_SPEND_PATH_TESTNET,
|
|
);
|
|
}
|
|
|
|
final String? silentPaymentTweak;
|
|
final String spendDerivationPath;
|
|
|
|
@override
|
|
String get derivationPath => spendDerivationPath;
|
|
|
|
@override
|
|
String toJSON() => json.encode({
|
|
'address': address,
|
|
'index': index,
|
|
'accountIndex': accountIndex,
|
|
'isHidden': isHidden,
|
|
'isUsed': isUsed,
|
|
'txCount': txCount,
|
|
'name': name,
|
|
'balance': balance,
|
|
'type': type.toString(),
|
|
'network': network?.value,
|
|
'silent_payment_tweak': silentPaymentTweak,
|
|
'spend_derivation_path': spendDerivationPath,
|
|
});
|
|
}
|