2022-09-26 20:32:53 +00:00
import ' dart:async ' ;
import ' dart:convert ' ;
import ' dart:io ' ;
import ' package:bech32/bech32.dart ' ;
import ' package:bip32/bip32.dart ' as bip32 ;
import ' package:bip39/bip39.dart ' as bip39 ;
2022-11-04 19:55:13 +00:00
import ' package:bitbox/bitbox.dart ' as bitbox ;
2022-09-26 20:32:53 +00:00
import ' package:bitcoindart/bitcoindart.dart ' ;
import ' package:bs58check/bs58check.dart ' as bs58check ;
import ' package:decimal/decimal.dart ' ;
import ' package:flutter/foundation.dart ' ;
2023-01-11 19:30:03 +00:00
import ' package:isar/isar.dart ' ;
2023-03-01 21:52:13 +00:00
import ' package:stackwallet/db/isar/main_db.dart ' ;
2022-09-26 20:32:53 +00:00
import ' package:stackwallet/electrumx_rpc/cached_electrumx.dart ' ;
import ' package:stackwallet/electrumx_rpc/electrumx.dart ' ;
2023-01-11 19:30:03 +00:00
import ' package:stackwallet/models/balance.dart ' ;
2023-02-03 19:22:21 +00:00
import ' package:stackwallet/models/isar/models/blockchain_data/address.dart ' ;
2023-01-11 19:30:03 +00:00
import ' package:stackwallet/models/isar/models/isar_models.dart ' as isar_models ;
2022-09-26 20:32:53 +00:00
import ' package:stackwallet/models/paymint/fee_object_model.dart ' ;
import ' package:stackwallet/services/coins/coin_service.dart ' ;
import ' package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart ' ;
import ' package:stackwallet/services/event_bus/events/global/refresh_percent_changed_event.dart ' ;
import ' package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart ' ;
import ' package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart ' ;
import ' package:stackwallet/services/event_bus/global_event_bus.dart ' ;
2023-03-08 22:11:46 +00:00
import ' package:stackwallet/services/mixins/coin_control_interface.dart ' ;
2023-01-12 18:15:28 +00:00
import ' package:stackwallet/services/mixins/wallet_cache.dart ' ;
2023-01-12 18:46:01 +00:00
import ' package:stackwallet/services/mixins/wallet_db.dart ' ;
2023-04-08 00:44:43 +00:00
import ' package:stackwallet/services/mixins/xpubable.dart ' ;
2022-09-26 20:32:53 +00:00
import ' package:stackwallet/services/node_service.dart ' ;
import ' package:stackwallet/services/notifications_api.dart ' ;
import ' package:stackwallet/services/transaction_notification_tracker.dart ' ;
2023-01-11 19:30:03 +00:00
import ' package:stackwallet/utilities/address_utils.dart ' ;
2023-04-06 21:24:56 +00:00
import ' package:stackwallet/utilities/amount/amount.dart ' ;
2022-09-26 20:32:53 +00:00
import ' package:stackwallet/utilities/assets.dart ' ;
2023-02-03 22:34:06 +00:00
import ' package:stackwallet/utilities/bip32_utils.dart ' ;
2022-09-26 20:32:53 +00:00
import ' package:stackwallet/utilities/constants.dart ' ;
import ' package:stackwallet/utilities/default_nodes.dart ' ;
import ' package:stackwallet/utilities/enums/coin_enum.dart ' ;
2023-01-25 18:08:48 +00:00
import ' package:stackwallet/utilities/enums/derive_path_type_enum.dart ' ;
2022-09-26 20:32:53 +00:00
import ' package:stackwallet/utilities/enums/fee_rate_type_enum.dart ' ;
import ' package:stackwallet/utilities/flutter_secure_storage_interface.dart ' ;
import ' package:stackwallet/utilities/format.dart ' ;
import ' package:stackwallet/utilities/logger.dart ' ;
import ' package:stackwallet/utilities/prefs.dart ' ;
import ' package:tuple/tuple.dart ' ;
import ' package:uuid/uuid.dart ' ;
2023-03-23 15:09:30 +00:00
const int MINIMUM_CONFIRMATIONS = 0 ;
2023-04-05 22:06:31 +00:00
final Amount DUST_LIMIT = Amount (
rawValue: BigInt . from ( 546 ) ,
fractionDigits: Coin . particl . decimals ,
) ;
2022-09-26 20:32:53 +00:00
const String GENESIS_HASH_MAINNET =
" 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f " ;
const String GENESIS_HASH_TESTNET =
" 000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943 " ;
2023-02-03 22:34:06 +00:00
String constructDerivePath ( {
required DerivePathType derivePathType ,
required int networkWIF ,
int account = 0 ,
required int chain ,
required int index ,
} ) {
2023-02-04 00:05:19 +00:00
int coinType ;
2023-02-03 22:34:06 +00:00
switch ( networkWIF ) {
2022-09-26 20:32:53 +00:00
case 0x80 : // bch mainnet wif
2023-02-04 00:05:19 +00:00
switch ( derivePathType ) {
case DerivePathType . bip44:
case DerivePathType . bip49:
coinType = 145 ; // bch mainnet
break ;
case DerivePathType . bch44: // bitcoin.com wallet specific
coinType = 0 ; // bch mainnet
break ;
default :
throw Exception (
" DerivePathType $ derivePathType not supported for coinType " ) ;
}
2022-09-26 20:32:53 +00:00
break ;
case 0xef : // bch testnet wif
2023-02-04 00:05:19 +00:00
coinType = 1 ; // bch testnet
2022-09-26 20:32:53 +00:00
break ;
default :
2023-02-03 22:34:06 +00:00
throw Exception ( " Invalid Bitcoincash network wif used! " ) ;
2022-09-26 20:32:53 +00:00
}
2023-02-03 22:34:06 +00:00
int purpose ;
2022-09-26 20:32:53 +00:00
switch ( derivePathType ) {
case DerivePathType . bip44:
2023-02-02 15:24:26 +00:00
case DerivePathType . bch44:
2023-02-03 22:34:06 +00:00
purpose = 44 ;
break ;
2023-02-03 23:22:34 +00:00
case DerivePathType . bip49:
purpose = 49 ;
2023-02-03 22:34:06 +00:00
break ;
2022-09-26 20:32:53 +00:00
default :
2023-02-03 22:34:06 +00:00
throw Exception ( " DerivePathType $ derivePathType not supported " ) ;
2022-09-26 20:32:53 +00:00
}
2023-02-03 22:34:06 +00:00
return " m/ $ purpose '/ $ coinType '/ $ account '/ $ chain / $ index " ;
2022-09-26 20:32:53 +00:00
}
2023-03-08 22:11:46 +00:00
class BitcoinCashWallet extends CoinServiceAPI
2023-04-08 00:44:43 +00:00
with WalletCache , WalletDB , CoinControlInterface
implements XPubAble {
2023-02-03 22:48:16 +00:00
BitcoinCashWallet ( {
required String walletId ,
required String walletName ,
required Coin coin ,
required ElectrumX client ,
required CachedElectrumX cachedClient ,
required TransactionNotificationTracker tracker ,
required SecureStorageInterface secureStore ,
MainDB ? mockableOverride ,
} ) {
txTracker = tracker ;
_walletId = walletId ;
_walletName = walletName ;
_coin = coin ;
_electrumXClient = client ;
_cachedElectrumXClient = cachedClient ;
_secureStore = secureStore ;
initCache ( walletId , coin ) ;
initWalletDB ( mockableOverride: mockableOverride ) ;
2023-03-08 22:11:46 +00:00
initCoinControlInterface (
walletId: walletId ,
walletName: walletName ,
coin: coin ,
db: db ,
getChainHeight: ( ) = > chainHeight ,
refreshedBalanceCallback: ( balance ) async {
_balance = balance ;
await updateCachedBalance ( _balance ! ) ;
} ,
) ;
2023-02-03 22:48:16 +00:00
}
2022-09-26 20:32:53 +00:00
static const integrationTestFlag =
bool . fromEnvironment ( " IS_INTEGRATION_TEST " ) ;
final _prefs = Prefs . instance ;
Timer ? timer ;
2023-01-12 21:32:25 +00:00
late final Coin _coin ;
2022-09-26 20:32:53 +00:00
late final TransactionNotificationTracker txTracker ;
NetworkType get _network {
switch ( coin ) {
case Coin . bitcoincash:
return bitcoincash ;
case Coin . bitcoincashTestnet:
return bitcoincashtestnet ;
default :
throw Exception ( " Bitcoincash network type not set! " ) ;
}
}
@ override
2023-01-16 21:04:03 +00:00
Future < List < isar_models . UTXO > > get utxos = > db . getUTXOs ( walletId ) . findAll ( ) ;
2022-09-26 20:32:53 +00:00
@ override
2023-01-11 19:30:03 +00:00
Future < List < isar_models . Transaction > > get transactions = >
2023-01-16 21:04:03 +00:00
db . getTransactions ( walletId ) . sortByTimestampDesc ( ) . findAll ( ) ;
2022-09-26 20:32:53 +00:00
@ override
2023-01-11 19:30:03 +00:00
Coin get coin = > _coin ;
2022-09-26 20:32:53 +00:00
@ override
2023-01-11 19:30:03 +00:00
Future < String > get currentReceivingAddress async = >
( await _currentReceivingAddress ) . value ;
2023-01-23 16:32:53 +00:00
Future < isar_models . Address > get _currentReceivingAddress async = >
( await db
. getAddresses ( walletId )
. filter ( )
. typeEqualTo ( isar_models . AddressType . p2pkh )
. subTypeEqualTo ( isar_models . AddressSubType . receiving )
2023-02-04 00:51:30 +00:00
. derivationPath ( ( q ) = > q . not ( ) . valueStartsWith ( " m/44'/0' " ) )
2023-01-23 16:32:53 +00:00
. sortByDerivationIndexDesc ( )
. findFirst ( ) ) ? ?
2023-01-25 19:49:14 +00:00
await _generateAddressForChain ( 0 , 0 , DerivePathTypeExt . primaryFor ( coin ) ) ;
2023-01-11 19:30:03 +00:00
Future < String > get currentChangeAddress async = >
( await _currentChangeAddress ) . value ;
2023-01-23 16:32:53 +00:00
Future < isar_models . Address > get _currentChangeAddress async = >
( await db
. getAddresses ( walletId )
. filter ( )
. typeEqualTo ( isar_models . AddressType . p2pkh )
. subTypeEqualTo ( isar_models . AddressSubType . change )
2023-02-04 00:51:30 +00:00
. derivationPath ( ( q ) = > q . not ( ) . valueStartsWith ( " m/44'/0' " ) )
2023-01-23 16:32:53 +00:00
. sortByDerivationIndexDesc ( )
. findFirst ( ) ) ? ?
2023-01-25 19:49:14 +00:00
await _generateAddressForChain ( 1 , 0 , DerivePathTypeExt . primaryFor ( coin ) ) ;
2022-09-23 12:31:14 +00:00
2022-09-26 20:32:53 +00:00
@ override
Future < void > exit ( ) async {
_hasCalledExit = true ;
timer ? . cancel ( ) ;
timer = null ;
stopNetworkAlivePinging ( ) ;
}
bool _hasCalledExit = false ;
@ override
bool get hasCalledExit = > _hasCalledExit ;
@ override
Future < FeeObject > get fees = > _feeObject ? ? = _getFees ( ) ;
Future < FeeObject > ? _feeObject ;
@ override
Future < int > get maxFee async {
2023-04-05 22:06:31 +00:00
throw UnimplementedError ( " Not used in bch " ) ;
2022-09-26 20:32:53 +00:00
}
@ override
Future < List < String > > get mnemonic = > _getMnemonicList ( ) ;
2023-02-03 22:34:06 +00:00
@ override
Future < String ? > get mnemonicString = >
_secureStore . read ( key: ' ${ _walletId } _mnemonic ' ) ;
@ override
Future < String ? > get mnemonicPassphrase = > _secureStore . read (
key: ' ${ _walletId } _mnemonicPassphrase ' ,
) ;
2022-09-26 20:32:53 +00:00
Future < int > get chainHeight async {
try {
final result = await _electrumXClient . getBlockHeadTip ( ) ;
2023-01-12 21:32:25 +00:00
final height = result [ " height " ] as int ;
await updateCachedChainHeight ( height ) ;
2023-01-30 17:06:28 +00:00
if ( height > storedChainHeight ) {
GlobalEventBus . instance . fire (
UpdatedInBackgroundEvent (
" Updated current chain height in $ walletId $ walletName ! " ,
walletId ,
) ,
) ;
}
2023-01-12 21:32:25 +00:00
return height ;
2022-09-26 20:32:53 +00:00
} catch ( e , s ) {
Logging . instance . log ( " Exception caught in chainHeight: $ e \n $ s " ,
level: LogLevel . Error ) ;
2023-01-12 21:32:25 +00:00
return storedChainHeight ;
2022-09-26 20:32:53 +00:00
}
}
2023-01-10 23:50:22 +00:00
@ override
2023-01-12 21:32:25 +00:00
int get storedChainHeight = > getCachedChainHeight ( ) ;
2022-09-26 20:32:53 +00:00
DerivePathType addressType ( { required String address } ) {
Uint8List ? decodeBase58 ;
Segwit ? decodeBech32 ;
2022-10-19 22:16:00 +00:00
try {
2022-11-04 20:10:30 +00:00
if ( bitbox . Address . detectFormat ( address ) = =
bitbox . Address . formatCashAddr ) {
2022-11-07 20:46:36 +00:00
if ( validateCashAddr ( address ) ) {
address = bitbox . Address . toLegacyAddress ( address ) ;
} else {
throw ArgumentError ( ' $ address is not currently supported ' ) ;
}
2022-10-19 22:16:00 +00:00
}
2023-02-03 22:34:06 +00:00
} catch ( _ ) {
// invalid cash addr format
}
2022-09-26 20:32:53 +00:00
try {
decodeBase58 = bs58check . decode ( address ) ;
} catch ( err ) {
// Base58check decode fail
}
if ( decodeBase58 ! = null ) {
if ( decodeBase58 [ 0 ] = = _network . pubKeyHash ) {
// P2PKH
return DerivePathType . bip44 ;
}
2022-09-23 12:31:14 +00:00
if ( decodeBase58 [ 0 ] = = _network . scriptHash ) {
// P2SH
return DerivePathType . bip49 ;
}
2022-09-26 20:32:53 +00:00
throw ArgumentError ( ' Invalid version or Network mismatch ' ) ;
} else {
try {
decodeBech32 = segwit . decode ( address ) ;
} catch ( err ) {
// Bech32 decode fail
}
2022-11-07 20:46:36 +00:00
if ( decodeBech32 ! = null ) {
if ( _network . bech32 ! = decodeBech32 . hrp ) {
throw ArgumentError ( ' Invalid prefix or Network mismatch ' ) ;
}
if ( decodeBech32 . version ! = 0 ) {
throw ArgumentError ( ' Invalid address version ' ) ;
}
2022-09-26 20:32:53 +00:00
}
}
throw ArgumentError ( ' $ address has no matching Script ' ) ;
}
bool longMutex = false ;
@ override
Future < void > recoverFromMnemonic ( {
required String mnemonic ,
2023-02-03 22:34:06 +00:00
String ? mnemonicPassphrase ,
2022-09-26 20:32:53 +00:00
required int maxUnusedAddressGap ,
required int maxNumberOfIndexesToCheck ,
required int height ,
} ) async {
longMutex = true ;
final start = DateTime . now ( ) ;
try {
Logging . instance . log ( " IS_INTEGRATION_TEST: $ integrationTestFlag " ,
level: LogLevel . Info ) ;
if ( ! integrationTestFlag ) {
final features = await electrumXClient . getServerFeatures ( ) ;
Logging . instance . log ( " features: $ features " , level: LogLevel . Info ) ;
switch ( coin ) {
case Coin . bitcoincash:
if ( features [ ' genesis_hash ' ] ! = GENESIS_HASH_MAINNET ) {
throw Exception ( " genesis hash does not match main net! " ) ;
}
break ;
case Coin . bitcoincashTestnet:
if ( features [ ' genesis_hash ' ] ! = GENESIS_HASH_TESTNET ) {
throw Exception ( " genesis hash does not match test net! " ) ;
}
break ;
default :
throw Exception (
" Attempted to generate a BitcoinCashWallet using a non bch coin type: ${ coin . name } " ) ;
}
}
// check to make sure we aren't overwriting a mnemonic
// this should never fail
2023-02-03 22:34:06 +00:00
if ( ( await mnemonicString ) ! = null | |
( await this . mnemonicPassphrase ) ! = null ) {
2022-09-26 20:32:53 +00:00
longMutex = false ;
throw Exception ( " Attempted to overwrite mnemonic on restore! " ) ;
}
await _secureStore . write (
key: ' ${ _walletId } _mnemonic ' , value: mnemonic . trim ( ) ) ;
2023-02-03 22:34:06 +00:00
await _secureStore . write (
key: ' ${ _walletId } _mnemonicPassphrase ' ,
value: mnemonicPassphrase ? ? " " ,
) ;
2022-09-26 20:32:53 +00:00
await _recoverWalletFromBIP32SeedPhrase (
mnemonic: mnemonic . trim ( ) ,
2023-02-03 22:34:06 +00:00
mnemonicPassphrase: mnemonicPassphrase ? ? " " ,
2022-09-26 20:32:53 +00:00
maxUnusedAddressGap: maxUnusedAddressGap ,
maxNumberOfIndexesToCheck: maxNumberOfIndexesToCheck ,
2023-02-02 15:24:26 +00:00
coin: coin ,
2022-09-26 20:32:53 +00:00
) ;
} catch ( e , s ) {
Logging . instance . log (
" Exception rethrown from recoverFromMnemonic(): $ e \n $ s " ,
level: LogLevel . Error ) ;
longMutex = false ;
rethrow ;
}
longMutex = false ;
final end = DateTime . now ( ) ;
Logging . instance . log (
" $ walletName recovery time: ${ end . difference ( start ) . inMilliseconds } millis " ,
level: LogLevel . Info ) ;
}
2023-01-11 19:30:03 +00:00
Future < Map < String , dynamic > > _checkGaps (
int maxNumberOfIndexesToCheck ,
int maxUnusedAddressGap ,
int txCountBatchSize ,
bip32 . BIP32 root ,
DerivePathType type ,
int chain ) async {
List < isar_models . Address > addressArray = [ ] ;
int returningIndex = - 1 ;
Map < String , Map < String , String > > derivations = { } ;
int gapCounter = 0 ;
for ( int index = 0 ;
index < maxNumberOfIndexesToCheck & & gapCounter < maxUnusedAddressGap ;
index + = txCountBatchSize ) {
List < String > iterationsAddressArray = [ ] ;
Logging . instance . log (
" index: $ index , \t GapCounter $ chain ${ type . name } : $ gapCounter " ,
level: LogLevel . Info ) ;
final _id = " k_ $ index " ;
Map < String , String > txCountCallArgs = { } ;
final Map < String , dynamic > receivingNodes = { } ;
for ( int j = 0 ; j < txCountBatchSize ; j + + ) {
2023-02-03 22:34:06 +00:00
final derivePath = constructDerivePath (
derivePathType: type ,
networkWIF: root . network . wif ,
chain: chain ,
index: index + j ,
2023-01-11 19:30:03 +00:00
) ;
2023-02-03 22:34:06 +00:00
final node = await Bip32Utils . getBip32NodeFromRoot ( root , derivePath ) ;
2023-01-11 19:30:03 +00:00
String addressString ;
final data = PaymentData ( pubkey: node . publicKey ) ;
isar_models . AddressType addrType ;
switch ( type ) {
case DerivePathType . bip44:
2023-02-02 15:24:26 +00:00
case DerivePathType . bch44:
addressString = P2PKH ( data: data , network: _network ) . data . address ! ;
2023-02-04 00:51:30 +00:00
addrType = isar_models . AddressType . p2pkh ;
2023-02-02 15:24:26 +00:00
addressString = bitbox . Address . toCashAddress ( addressString ) ;
break ;
2023-01-11 19:30:03 +00:00
case DerivePathType . bip49:
addressString = P2SH (
data: PaymentData (
redeem: P2WPKH ( data: data , network: _network ) . data ) ,
network: _network )
. data
. address ! ;
addrType = isar_models . AddressType . p2sh ;
break ;
default :
2023-02-02 15:24:26 +00:00
throw Exception ( " DerivePathType $ type not supported " ) ;
2023-01-11 19:30:03 +00:00
}
2023-01-16 22:37:00 +00:00
final address = isar_models . Address (
walletId: walletId ,
value: addressString ,
publicKey: node . publicKey ,
type: addrType ,
derivationIndex: index + j ,
2023-02-03 23:30:32 +00:00
derivationPath: isar_models . DerivationPath ( ) . . value = derivePath ,
2023-01-16 22:37:00 +00:00
subType: chain = = 0
2023-01-11 19:30:03 +00:00
? isar_models . AddressSubType . receiving
2023-01-16 22:37:00 +00:00
: isar_models . AddressSubType . change ,
) ;
2023-01-11 19:30:03 +00:00
receivingNodes . addAll ( {
" ${ _id } _ $ j " : {
" node " : node ,
" address " : address ,
}
} ) ;
txCountCallArgs . addAll ( {
" ${ _id } _ $ j " : addressString ,
} ) ;
}
// get address tx counts
final counts = await _getBatchTxCount ( addresses: txCountCallArgs ) ;
if ( kDebugMode ) {
print ( " Counts $ counts " ) ;
}
// check and add appropriate addresses
for ( int k = 0 ; k < txCountBatchSize ; k + + ) {
int count = counts [ " ${ _id } _ $ k " ] ! ;
if ( count > 0 ) {
final node = receivingNodes [ " ${ _id } _ $ k " ] ;
final address = node [ " address " ] as isar_models . Address ;
// add address to array
addressArray . add ( address ) ;
iterationsAddressArray . add ( address . value ) ;
// set current index
returningIndex = index + k ;
// reset counter
gapCounter = 0 ;
// add info to derivations
2023-01-13 15:26:37 +00:00
derivations [ address . value ] = {
2023-01-11 19:30:03 +00:00
" pubKey " : Format . uint8listToString (
( node [ " node " ] as bip32 . BIP32 ) . publicKey ) ,
" wif " : ( node [ " node " ] as bip32 . BIP32 ) . toWIF ( ) ,
} ;
}
// increase counter when no tx history found
if ( count = = 0 ) {
gapCounter + + ;
}
}
// cache all the transactions while waiting for the current function to finish.
unawaited ( getTransactionCacheEarly ( iterationsAddressArray ) ) ;
}
return {
" addressArray " : addressArray ,
" index " : returningIndex ,
" derivations " : derivations
} ;
}
Future < void > getTransactionCacheEarly ( List < String > allAddresses ) async {
try {
final List < Map < String , dynamic > > allTxHashes =
await _fetchHistory ( allAddresses ) ;
for ( final txHash in allTxHashes ) {
try {
unawaited ( cachedElectrumXClient . getTransaction (
txHash: txHash [ " tx_hash " ] as String ,
verbose: true ,
coin: coin ,
) ) ;
} catch ( e ) {
continue ;
}
}
} catch ( e ) {
//
}
}
2022-09-26 20:32:53 +00:00
Future < void > _recoverWalletFromBIP32SeedPhrase ( {
required String mnemonic ,
2023-02-03 22:34:06 +00:00
required String mnemonicPassphrase ,
2022-09-26 20:32:53 +00:00
int maxUnusedAddressGap = 20 ,
int maxNumberOfIndexesToCheck = 1000 ,
2023-02-02 15:03:57 +00:00
bool isRescan = false ,
2023-02-02 15:29:30 +00:00
Coin ? coin ,
2022-09-26 20:32:53 +00:00
} ) async {
longMutex = true ;
2023-02-02 15:24:26 +00:00
Map < String , Map < String , String > > bip44P2pkhReceiveDerivations = { } ;
Map < String , Map < String , String > > bch44P2pkhReceiveDerivations = { } ;
2022-09-23 12:31:14 +00:00
Map < String , Map < String , String > > p2shReceiveDerivations = { } ;
2023-02-02 15:24:26 +00:00
Map < String , Map < String , String > > bip44P2pkhChangeDerivations = { } ;
Map < String , Map < String , String > > bch44P2pkhChangeDerivations = { } ;
2022-09-23 12:31:14 +00:00
Map < String , Map < String , String > > p2shChangeDerivations = { } ;
2022-09-26 20:32:53 +00:00
2023-02-03 22:34:06 +00:00
final root = await Bip32Utils . getBip32Root (
mnemonic ,
mnemonicPassphrase ,
_network ,
) ;
2022-09-26 20:32:53 +00:00
2023-02-02 15:24:26 +00:00
List < isar_models . Address > bip44P2pkhReceiveAddressArray = [ ] ;
List < isar_models . Address > bch44P2pkhReceiveAddressArray = [ ] ;
2023-01-11 19:30:03 +00:00
List < isar_models . Address > p2shReceiveAddressArray = [ ] ;
2023-02-02 15:24:26 +00:00
int bch44P2pkhReceiveIndex = - 1 ;
int bip44P2pkhReceiveIndex = - 1 ;
2022-09-23 12:31:14 +00:00
int p2shReceiveIndex = - 1 ;
2022-09-26 20:32:53 +00:00
2023-02-02 15:24:26 +00:00
List < isar_models . Address > bip44P2pkhChangeAddressArray = [ ] ;
List < isar_models . Address > bch44P2pkhChangeAddressArray = [ ] ;
2023-01-11 19:30:03 +00:00
List < isar_models . Address > p2shChangeAddressArray = [ ] ;
2023-02-02 15:24:26 +00:00
int bch44P2pkhChangeIndex = - 1 ;
int bip44P2pkhChangeIndex = - 1 ;
2022-09-23 12:31:14 +00:00
int p2shChangeIndex = - 1 ;
2022-09-26 20:32:53 +00:00
// The gap limit will be capped at [maxUnusedAddressGap]
2022-09-23 12:31:14 +00:00
// int receivingGapCounter = 0;
// int changeGapCounter = 0;
2022-09-26 20:32:53 +00:00
2022-09-23 12:31:14 +00:00
// actual size is 24 due to p2pkh and p2sh so 12x2
2022-09-26 20:32:53 +00:00
const txCountBatchSize = 12 ;
try {
2023-02-03 22:34:06 +00:00
bool testnet = ( ( coin ) = = Coin . bitcoincashTestnet ) ? true : false ;
2022-09-26 20:32:53 +00:00
// receiving addresses
Logging . instance
. log ( " checking receiving addresses... " , level: LogLevel . Info ) ;
2023-02-02 15:24:26 +00:00
final resultReceiveBip44 = _checkGaps ( maxNumberOfIndexesToCheck ,
2022-09-23 12:31:14 +00:00
maxUnusedAddressGap , txCountBatchSize , root , DerivePathType . bip44 , 0 ) ;
2022-09-26 20:32:53 +00:00
2022-09-23 12:31:14 +00:00
final resultReceive49 = _checkGaps ( maxNumberOfIndexesToCheck ,
maxUnusedAddressGap , txCountBatchSize , root , DerivePathType . bip49 , 0 ) ;
2022-09-26 20:32:53 +00:00
Logging . instance
. log ( " checking change addresses... " , level: LogLevel . Info ) ;
// change addresses
2023-02-02 15:24:26 +00:00
final bip44ResultChange = _checkGaps ( maxNumberOfIndexesToCheck ,
2022-09-23 12:31:14 +00:00
maxUnusedAddressGap , txCountBatchSize , root , DerivePathType . bip44 , 1 ) ;
2022-09-06 14:17:45 +00:00
2022-09-23 12:31:14 +00:00
final resultChange49 = _checkGaps ( maxNumberOfIndexesToCheck ,
maxUnusedAddressGap , txCountBatchSize , root , DerivePathType . bip49 , 1 ) ;
2022-09-06 14:17:45 +00:00
2023-01-11 19:30:03 +00:00
await Future . wait ( [
2023-02-02 15:24:26 +00:00
resultReceiveBip44 ,
2023-01-11 19:30:03 +00:00
resultReceive49 ,
2023-02-02 15:24:26 +00:00
bip44ResultChange ,
2023-01-11 19:30:03 +00:00
resultChange49 ,
] ) ;
2022-09-06 14:17:45 +00:00
2023-02-02 15:24:26 +00:00
bip44P2pkhReceiveAddressArray = ( await resultReceiveBip44 ) [ ' addressArray ' ]
as List < isar_models . Address > ;
bip44P2pkhReceiveIndex = ( await resultReceiveBip44 ) [ ' index ' ] as int ;
bip44P2pkhReceiveDerivations = ( await resultReceiveBip44 ) [ ' derivations ' ]
2022-09-23 12:31:14 +00:00
as Map < String , Map < String , String > > ;
2022-09-06 14:17:45 +00:00
2022-09-23 12:31:14 +00:00
p2shReceiveAddressArray =
2023-01-11 19:30:03 +00:00
( await resultReceive49 ) [ ' addressArray ' ] as List < isar_models . Address > ;
2022-09-23 12:31:14 +00:00
p2shReceiveIndex = ( await resultReceive49 ) [ ' index ' ] as int ;
p2shReceiveDerivations = ( await resultReceive49 ) [ ' derivations ' ]
as Map < String , Map < String , String > > ;
2023-02-02 15:24:26 +00:00
bip44P2pkhChangeAddressArray = ( await bip44ResultChange ) [ ' addressArray ' ]
as List < isar_models . Address > ;
bip44P2pkhChangeIndex = ( await bip44ResultChange ) [ ' index ' ] as int ;
bip44P2pkhChangeDerivations = ( await bip44ResultChange ) [ ' derivations ' ]
2022-09-23 12:31:14 +00:00
as Map < String , Map < String , String > > ;
p2shChangeAddressArray =
2023-01-11 19:30:03 +00:00
( await resultChange49 ) [ ' addressArray ' ] as List < isar_models . Address > ;
2022-09-23 12:31:14 +00:00
p2shChangeIndex = ( await resultChange49 ) [ ' index ' ] as int ;
p2shChangeDerivations = ( await resultChange49 ) [ ' derivations ' ]
as Map < String , Map < String , String > > ;
2022-09-26 20:32:53 +00:00
// save the derivations (if any)
2023-02-02 15:24:26 +00:00
if ( bip44P2pkhReceiveDerivations . isNotEmpty ) {
2022-09-26 20:32:53 +00:00
await addDerivations (
chain: 0 ,
derivePathType: DerivePathType . bip44 ,
2023-02-02 15:24:26 +00:00
derivationsToAdd: bip44P2pkhReceiveDerivations ) ;
2022-09-26 20:32:53 +00:00
}
2022-09-23 12:31:14 +00:00
if ( p2shReceiveDerivations . isNotEmpty ) {
await addDerivations (
chain: 0 ,
derivePathType: DerivePathType . bip49 ,
derivationsToAdd: p2shReceiveDerivations ) ;
}
2023-02-02 15:24:26 +00:00
if ( bip44P2pkhChangeDerivations . isNotEmpty ) {
2022-09-26 20:32:53 +00:00
await addDerivations (
chain: 1 ,
derivePathType: DerivePathType . bip44 ,
2023-02-02 15:24:26 +00:00
derivationsToAdd: bip44P2pkhChangeDerivations ) ;
2022-09-26 20:32:53 +00:00
}
2022-09-23 12:31:14 +00:00
if ( p2shChangeDerivations . isNotEmpty ) {
await addDerivations (
chain: 1 ,
derivePathType: DerivePathType . bip49 ,
derivationsToAdd: p2shChangeDerivations ) ;
}
2022-09-26 20:32:53 +00:00
// If restoring a wallet that never received any funds, then set receivingArray manually
// If we didn't do this, it'd store an empty array
2023-02-02 15:24:26 +00:00
if ( bip44P2pkhReceiveIndex = = - 1 ) {
2022-09-26 20:32:53 +00:00
final address =
await _generateAddressForChain ( 0 , 0 , DerivePathType . bip44 ) ;
2023-02-02 15:24:26 +00:00
bip44P2pkhReceiveAddressArray . add ( address ) ;
2022-09-26 20:32:53 +00:00
}
2022-09-23 12:31:14 +00:00
if ( p2shReceiveIndex = = - 1 ) {
final address =
await _generateAddressForChain ( 0 , 0 , DerivePathType . bip49 ) ;
p2shReceiveAddressArray . add ( address ) ;
}
2022-09-26 20:32:53 +00:00
// If restoring a wallet that never sent any funds with change, then set changeArray
// manually. If we didn't do this, it'd store an empty array.
2023-02-02 15:24:26 +00:00
if ( bip44P2pkhChangeIndex = = - 1 ) {
2022-09-26 20:32:53 +00:00
final address =
await _generateAddressForChain ( 1 , 0 , DerivePathType . bip44 ) ;
2023-02-02 15:24:26 +00:00
bip44P2pkhChangeAddressArray . add ( address ) ;
2022-09-26 20:32:53 +00:00
}
2022-09-23 12:31:14 +00:00
if ( p2shChangeIndex = = - 1 ) {
final address =
await _generateAddressForChain ( 1 , 0 , DerivePathType . bip49 ) ;
p2shChangeAddressArray . add ( address ) ;
2023-01-11 19:30:03 +00:00
}
2023-02-02 15:41:33 +00:00
final addressesToStore = [
. . . bip44P2pkhReceiveAddressArray ,
. . . bip44P2pkhChangeAddressArray ,
. . . p2shReceiveAddressArray ,
. . . p2shChangeAddressArray ,
] ;
2023-02-02 15:24:26 +00:00
if ( ! testnet ) {
final resultReceiveBch44 = _checkGaps (
maxNumberOfIndexesToCheck ,
maxUnusedAddressGap ,
txCountBatchSize ,
root ,
DerivePathType . bch44 ,
0 ) ;
final Future < Map < String , dynamic > > bch44ResultChange = _checkGaps (
maxNumberOfIndexesToCheck ,
maxUnusedAddressGap ,
txCountBatchSize ,
root ,
DerivePathType . bch44 ,
1 ) ;
await Future . wait ( [
resultReceiveBch44 ,
bch44ResultChange ,
2023-02-02 15:03:57 +00:00
] ) ;
2023-02-02 15:24:26 +00:00
bch44P2pkhReceiveAddressArray =
( await resultReceiveBch44 ) [ ' addressArray ' ]
as List < isar_models . Address > ;
bch44P2pkhReceiveIndex = ( await resultReceiveBch44 ) [ ' index ' ] as int ;
bch44P2pkhReceiveDerivations = ( await resultReceiveBch44 ) [ ' derivations ' ]
as Map < String , Map < String , String > > ;
bch44P2pkhChangeAddressArray = ( await bch44ResultChange ) [ ' addressArray ' ]
as List < isar_models . Address > ;
bch44P2pkhChangeIndex = ( await bch44ResultChange ) [ ' index ' ] as int ;
bch44P2pkhChangeDerivations = ( await bch44ResultChange ) [ ' derivations ' ]
as Map < String , Map < String , String > > ;
if ( bch44P2pkhReceiveDerivations . isNotEmpty ) {
await addDerivations (
chain: 0 ,
derivePathType: DerivePathType . bch44 ,
derivationsToAdd: bch44P2pkhReceiveDerivations ) ;
}
if ( bch44P2pkhChangeDerivations . isNotEmpty ) {
await addDerivations (
chain: 1 ,
derivePathType: DerivePathType . bch44 ,
derivationsToAdd: bch44P2pkhChangeDerivations ) ;
}
if ( bch44P2pkhReceiveIndex = = - 1 ) {
final address =
await _generateAddressForChain ( 0 , 0 , DerivePathType . bch44 ) ;
bch44P2pkhReceiveAddressArray . add ( address ) ;
}
if ( bch44P2pkhChangeIndex = = - 1 ) {
final address =
await _generateAddressForChain ( 1 , 0 , DerivePathType . bch44 ) ;
bch44P2pkhChangeAddressArray . add ( address ) ;
}
2023-02-02 15:41:33 +00:00
addressesToStore . addAll ( [
. . . bch44P2pkhReceiveAddressArray ,
. . . bch44P2pkhChangeAddressArray ,
] ) ;
}
if ( isRescan ) {
await db . updateOrPutAddresses ( addressesToStore ) ;
2023-02-02 15:03:57 +00:00
} else {
2023-02-02 15:41:33 +00:00
await db . putAddresses ( addressesToStore ) ;
2023-02-02 15:03:57 +00:00
}
2023-01-11 19:30:03 +00:00
await _updateUTXOs ( ) ;
2023-01-12 21:32:25 +00:00
await Future . wait ( [
updateCachedId ( walletId ) ,
updateCachedIsFavorite ( false ) ,
] ) ;
2022-09-26 20:32:53 +00:00
longMutex = false ;
} catch ( e , s ) {
Logging . instance . log (
" Exception rethrown from _recoverWalletFromBIP32SeedPhrase(): $ e \n $ s " ,
level: LogLevel . Info ) ;
longMutex = false ;
rethrow ;
}
}
Future < bool > refreshIfThereIsNewData ( ) async {
if ( longMutex ) return false ;
if ( _hasCalledExit ) return false ;
Logging . instance . log ( " refreshIfThereIsNewData " , level: LogLevel . Info ) ;
try {
bool needsRefresh = false ;
Logging . instance . log (
" notified unconfirmed transactions: ${ txTracker . pendings } " ,
level: LogLevel . Info ) ;
Set < String > txnsToCheck = { } ;
for ( final String txid in txTracker . pendings ) {
if ( ! txTracker . wasNotifiedConfirmed ( txid ) ) {
txnsToCheck . add ( txid ) ;
}
}
for ( String txid in txnsToCheck ) {
final txn = await electrumXClient . getTransaction ( txHash: txid ) ;
var confirmations = txn [ " confirmations " ] ;
if ( confirmations is ! int ) continue ;
bool isUnconfirmed = confirmations < MINIMUM_CONFIRMATIONS ;
if ( ! isUnconfirmed ) {
// unconfirmedTxs = {};
needsRefresh = true ;
break ;
}
}
if ( ! needsRefresh ) {
var allOwnAddresses = await _fetchAllOwnAddresses ( ) ;
2023-01-11 19:30:03 +00:00
List < Map < String , dynamic > > allTxs = await _fetchHistory (
allOwnAddresses . map ( ( e ) = > e . value ) . toList ( growable: false ) ) ;
2022-09-26 20:32:53 +00:00
for ( Map < String , dynamic > transaction in allTxs ) {
2023-01-11 19:30:03 +00:00
final txid = transaction [ ' tx_hash ' ] as String ;
2023-01-16 21:04:03 +00:00
if ( ( await db
. getTransactions ( walletId )
2023-01-11 19:30:03 +00:00
. filter ( )
. txidMatches ( txid )
. findFirst ( ) ) = =
2022-09-26 20:32:53 +00:00
null ) {
Logging . instance . log (
" txid not found in address history already ${ transaction [ ' tx_hash ' ] } " ,
level: LogLevel . Info ) ;
needsRefresh = true ;
break ;
}
}
}
return needsRefresh ;
} catch ( e , s ) {
Logging . instance . log (
" Exception caught in refreshIfThereIsNewData: $ e \n $ s " ,
level: LogLevel . Info ) ;
rethrow ;
}
}
2023-01-11 19:30:03 +00:00
Future < void > getAllTxsToWatch ( ) async {
2022-09-26 20:32:53 +00:00
if ( _hasCalledExit ) return ;
2023-01-11 19:30:03 +00:00
List < isar_models . Transaction > unconfirmedTxnsToNotifyPending = [ ] ;
List < isar_models . Transaction > unconfirmedTxnsToNotifyConfirmed = [ ] ;
final currentChainHeight = await chainHeight ;
2022-09-26 20:32:53 +00:00
2023-01-16 21:04:03 +00:00
final txCount = await db . getTransactions ( walletId ) . count ( ) ;
2023-01-11 19:30:03 +00:00
const paginateLimit = 50 ;
for ( int i = 0 ; i < txCount ; i + = paginateLimit ) {
2023-01-16 21:04:03 +00:00
final transactions = await db
. getTransactions ( walletId )
2023-01-11 19:30:03 +00:00
. offset ( i )
. limit ( paginateLimit )
. findAll ( ) ;
for ( final tx in transactions ) {
if ( tx . isConfirmed ( currentChainHeight , MINIMUM_CONFIRMATIONS ) ) {
2022-09-26 20:32:53 +00:00
if ( txTracker . wasNotifiedPending ( tx . txid ) & &
! txTracker . wasNotifiedConfirmed ( tx . txid ) ) {
unconfirmedTxnsToNotifyConfirmed . add ( tx ) ;
}
} else {
if ( ! txTracker . wasNotifiedPending ( tx . txid ) ) {
unconfirmedTxnsToNotifyPending . add ( tx ) ;
}
}
}
}
// notify on new incoming transaction
for ( final tx in unconfirmedTxnsToNotifyPending ) {
2023-01-11 19:30:03 +00:00
final confirmations = tx . getConfirmations ( currentChainHeight ) ;
if ( tx . type = = isar_models . TransactionType . incoming ) {
2022-11-04 19:55:13 +00:00
unawaited (
NotificationApi . showNotification (
title: " Incoming transaction " ,
body: walletName ,
walletId: walletId ,
iconAssetName: Assets . svg . iconFor ( coin: coin ) ,
date: DateTime . now ( ) ,
2023-01-11 19:30:03 +00:00
shouldWatchForUpdates: confirmations < MINIMUM_CONFIRMATIONS ,
2022-11-04 19:55:13 +00:00
coinName: coin . name ,
txid: tx . txid ,
2023-01-11 19:30:03 +00:00
confirmations: confirmations ,
2022-11-04 19:55:13 +00:00
requiredConfirmations: MINIMUM_CONFIRMATIONS ,
) ,
2022-09-26 20:32:53 +00:00
) ;
await txTracker . addNotifiedPending ( tx . txid ) ;
2023-01-11 19:30:03 +00:00
} else if ( tx . type = = isar_models . TransactionType . outgoing ) {
2022-11-04 19:55:13 +00:00
unawaited (
NotificationApi . showNotification (
title: " Sending transaction " ,
body: walletName ,
walletId: walletId ,
iconAssetName: Assets . svg . iconFor ( coin: coin ) ,
date: DateTime . fromMillisecondsSinceEpoch ( tx . timestamp * 1000 ) ,
2023-01-11 19:30:03 +00:00
shouldWatchForUpdates: confirmations < MINIMUM_CONFIRMATIONS ,
2022-11-04 19:55:13 +00:00
coinName: coin . name ,
txid: tx . txid ,
2023-01-11 19:30:03 +00:00
confirmations: confirmations ,
2022-11-04 19:55:13 +00:00
requiredConfirmations: MINIMUM_CONFIRMATIONS ,
) ,
2022-09-26 20:32:53 +00:00
) ;
await txTracker . addNotifiedPending ( tx . txid ) ;
}
}
// notify on confirmed
for ( final tx in unconfirmedTxnsToNotifyConfirmed ) {
2023-01-11 19:30:03 +00:00
if ( tx . type = = isar_models . TransactionType . incoming ) {
2022-11-04 19:55:13 +00:00
unawaited (
NotificationApi . showNotification (
title: " Incoming transaction confirmed " ,
body: walletName ,
walletId: walletId ,
iconAssetName: Assets . svg . iconFor ( coin: coin ) ,
date: DateTime . now ( ) ,
shouldWatchForUpdates: false ,
coinName: coin . name ,
) ,
2022-09-26 20:32:53 +00:00
) ;
await txTracker . addNotifiedConfirmed ( tx . txid ) ;
2023-01-11 19:30:03 +00:00
} else if ( tx . type = = isar_models . TransactionType . outgoing ) {
2022-11-04 19:55:13 +00:00
unawaited (
NotificationApi . showNotification (
title: " Outgoing transaction confirmed " ,
body: walletName ,
walletId: walletId ,
iconAssetName: Assets . svg . iconFor ( coin: coin ) ,
date: DateTime . now ( ) ,
shouldWatchForUpdates: false ,
coinName: coin . name ,
) ,
2022-09-26 20:32:53 +00:00
) ;
await txTracker . addNotifiedConfirmed ( tx . txid ) ;
}
}
}
bool refreshMutex = false ;
bool _shouldAutoSync = false ;
@ override
bool get shouldAutoSync = > _shouldAutoSync ;
@ override
set shouldAutoSync ( bool shouldAutoSync ) {
if ( _shouldAutoSync ! = shouldAutoSync ) {
_shouldAutoSync = shouldAutoSync ;
if ( ! shouldAutoSync ) {
timer ? . cancel ( ) ;
timer = null ;
stopNetworkAlivePinging ( ) ;
} else {
startNetworkAlivePinging ( ) ;
refresh ( ) ;
}
}
}
//TODO Show percentages properly/more consistently
/// Refreshes display data for the wallet
@ override
Future < void > refresh ( ) async {
if ( refreshMutex ) {
Logging . instance . log ( " $ walletId $ walletName refreshMutex denied " ,
level: LogLevel . Info ) ;
return ;
} else {
refreshMutex = true ;
}
try {
GlobalEventBus . instance . fire (
WalletSyncStatusChangedEvent (
WalletSyncStatus . syncing ,
walletId ,
coin ,
) ,
) ;
GlobalEventBus . instance . fire ( RefreshPercentChangedEvent ( 0.0 , walletId ) ) ;
GlobalEventBus . instance . fire ( RefreshPercentChangedEvent ( 0.1 , walletId ) ) ;
final currentHeight = await chainHeight ;
const storedHeight = 1 ; //await storedChainHeight;
Logging . instance
. log ( " chain height: $ currentHeight " , level: LogLevel . Info ) ;
Logging . instance
. log ( " cached height: $ storedHeight " , level: LogLevel . Info ) ;
if ( currentHeight ! = storedHeight ) {
GlobalEventBus . instance . fire ( RefreshPercentChangedEvent ( 0.2 , walletId ) ) ;
2023-01-11 19:30:03 +00:00
await _checkChangeAddressForTransactions ( ) ;
2022-09-26 20:32:53 +00:00
GlobalEventBus . instance . fire ( RefreshPercentChangedEvent ( 0.3 , walletId ) ) ;
await _checkCurrentReceivingAddressesForTransactions ( ) ;
2023-01-11 19:30:03 +00:00
final fetchFuture = _refreshTransactions ( ) ;
final utxosRefreshFuture = _updateUTXOs ( ) ;
2022-09-26 20:32:53 +00:00
GlobalEventBus . instance
. fire ( RefreshPercentChangedEvent ( 0.50 , walletId ) ) ;
final feeObj = _getFees ( ) ;
GlobalEventBus . instance
. fire ( RefreshPercentChangedEvent ( 0.60 , walletId ) ) ;
GlobalEventBus . instance
. fire ( RefreshPercentChangedEvent ( 0.70 , walletId ) ) ;
_feeObject = Future ( ( ) = > feeObj ) ;
2023-01-11 19:30:03 +00:00
await utxosRefreshFuture ;
2022-09-26 20:32:53 +00:00
GlobalEventBus . instance
. fire ( RefreshPercentChangedEvent ( 0.80 , walletId ) ) ;
2023-01-11 19:30:03 +00:00
await fetchFuture ;
await getAllTxsToWatch ( ) ;
2022-09-26 20:32:53 +00:00
GlobalEventBus . instance
. fire ( RefreshPercentChangedEvent ( 0.90 , walletId ) ) ;
}
GlobalEventBus . instance . fire ( RefreshPercentChangedEvent ( 1.0 , walletId ) ) ;
GlobalEventBus . instance . fire (
WalletSyncStatusChangedEvent (
WalletSyncStatus . synced ,
walletId ,
coin ,
) ,
) ;
refreshMutex = false ;
if ( shouldAutoSync ) {
timer ? ? = Timer . periodic ( const Duration ( seconds: 150 ) , ( timer ) async {
// chain height check currently broken
// if ((await chainHeight) != (await storedChainHeight)) {
if ( await refreshIfThereIsNewData ( ) ) {
await refresh ( ) ;
GlobalEventBus . instance . fire ( UpdatedInBackgroundEvent (
" New data found in $ walletId $ walletName in background! " ,
walletId ) ) ;
}
// }
} ) ;
}
} catch ( error , strace ) {
refreshMutex = false ;
GlobalEventBus . instance . fire (
NodeConnectionStatusChangedEvent (
NodeConnectionStatus . disconnected ,
walletId ,
coin ,
) ,
) ;
GlobalEventBus . instance . fire (
WalletSyncStatusChangedEvent (
WalletSyncStatus . unableToSync ,
walletId ,
coin ,
) ,
) ;
Logging . instance . log (
" Caught exception in refreshWalletData(): $ error \n $ strace " ,
level: LogLevel . Error ) ;
}
}
@ override
Future < Map < String , dynamic > > prepareSend ( {
required String address ,
2023-04-05 22:06:31 +00:00
required Amount amount ,
2022-09-26 20:32:53 +00:00
Map < String , dynamic > ? args ,
} ) async {
try {
final feeRateType = args ? [ " feeRate " ] ;
final feeRateAmount = args ? [ " feeRateAmount " ] ;
2023-03-08 22:11:46 +00:00
final utxos = args ? [ " UTXOs " ] as Set < isar_models . UTXO > ? ;
2022-09-26 20:32:53 +00:00
if ( feeRateType is FeeRateType | | feeRateAmount is int ) {
late final int rate ;
if ( feeRateType is FeeRateType ) {
int fee = 0 ;
final feeObject = await fees ;
switch ( feeRateType ) {
case FeeRateType . fast:
fee = feeObject . fast ;
break ;
case FeeRateType . average:
fee = feeObject . medium ;
break ;
case FeeRateType . slow:
fee = feeObject . slow ;
break ;
}
rate = fee ;
} else {
rate = feeRateAmount as int ;
}
// check for send all
bool isSendAll = false ;
2023-04-05 22:06:31 +00:00
if ( amount = = balance . spendable ) {
2022-09-26 20:32:53 +00:00
isSendAll = true ;
}
2023-03-09 16:30:10 +00:00
final bool coinControl = utxos ! = null ;
2023-03-08 22:11:46 +00:00
final result = await coinSelection (
2023-04-05 22:06:31 +00:00
satoshiAmountToSend: amount . raw . toInt ( ) ,
2023-03-08 22:11:46 +00:00
selectedTxFeeRate: rate ,
recipientAddress: address ,
isSendAll: isSendAll ,
utxos: utxos ? . toList ( ) ,
2023-03-09 16:30:10 +00:00
coinControl: coinControl ,
2023-03-08 22:11:46 +00:00
) ;
Logging . instance
. log ( " PREPARE SEND RESULT: $ result " , level: LogLevel . Info ) ;
2022-09-26 20:32:53 +00:00
if ( result is int ) {
switch ( result ) {
case 1 :
throw Exception ( " Insufficient balance! " ) ;
case 2 :
throw Exception ( " Insufficient funds to pay for transaction fee! " ) ;
default :
throw Exception ( " Transaction failed with error code $ result " ) ;
}
} else {
final hex = result [ " hex " ] ;
if ( hex is String ) {
final fee = result [ " fee " ] as int ;
final vSize = result [ " vSize " ] as int ;
Logging . instance . log ( " txHex: $ hex " , level: LogLevel . Info ) ;
Logging . instance . log ( " fee: $ fee " , level: LogLevel . Info ) ;
Logging . instance . log ( " vsize: $ vSize " , level: LogLevel . Info ) ;
// fee should never be less than vSize sanity check
if ( fee < vSize ) {
throw Exception (
" Error in fee calculation: Transaction fee cannot be less than vSize " ) ;
}
return result as Map < String , dynamic > ;
} else {
throw Exception ( " sent hex is not a String!!! " ) ;
}
}
} else {
throw ArgumentError ( " Invalid fee rate argument provided! " ) ;
}
} catch ( e , s ) {
Logging . instance . log ( " Exception rethrown from prepareSend(): $ e \n $ s " ,
level: LogLevel . Error ) ;
rethrow ;
}
}
@ override
Future < String > confirmSend ( { dynamic txData } ) async {
try {
Logging . instance . log ( " confirmSend txData: $ txData " , level: LogLevel . Info ) ;
final txHash = await _electrumXClient . broadcastTransaction (
rawTx: txData [ " hex " ] as String ) ;
Logging . instance . log ( " Sent txHash: $ txHash " , level: LogLevel . Info ) ;
2023-03-08 22:11:46 +00:00
final utxos = txData [ " usedUTXOs " ] as List < isar_models . UTXO > ;
// mark utxos as used
await db . putUTXOs ( utxos . map ( ( e ) = > e . copyWith ( used: true ) ) . toList ( ) ) ;
2022-09-26 20:32:53 +00:00
return txHash ;
} catch ( e , s ) {
Logging . instance . log ( " Exception rethrown from confirmSend(): $ e \n $ s " ,
level: LogLevel . Error ) ;
rethrow ;
}
}
@ override
Future < bool > testNetworkConnection ( ) async {
try {
final result = await _electrumXClient . ping ( ) ;
return result ;
} catch ( _ ) {
return false ;
}
}
Timer ? _networkAliveTimer ;
void startNetworkAlivePinging ( ) {
// call once on start right away
_periodicPingCheck ( ) ;
// then periodically check
_networkAliveTimer = Timer . periodic (
Constants . networkAliveTimerDuration ,
( _ ) async {
_periodicPingCheck ( ) ;
} ,
) ;
}
void _periodicPingCheck ( ) async {
bool hasNetwork = await testNetworkConnection ( ) ;
_isConnected = hasNetwork ;
if ( _isConnected ! = hasNetwork ) {
NodeConnectionStatus status = hasNetwork
? NodeConnectionStatus . connected
: NodeConnectionStatus . disconnected ;
GlobalEventBus . instance
. fire ( NodeConnectionStatusChangedEvent ( status , walletId , coin ) ) ;
}
}
void stopNetworkAlivePinging ( ) {
_networkAliveTimer ? . cancel ( ) ;
_networkAliveTimer = null ;
}
bool _isConnected = false ;
@ override
bool get isConnected = > _isConnected ;
@ override
Future < void > initializeNew ( ) async {
Logging . instance
. log ( " Generating new ${ coin . prettyName } wallet. " , level: LogLevel . Info ) ;
2023-01-12 21:32:25 +00:00
if ( getCachedId ( ) ! = null ) {
2022-09-26 20:32:53 +00:00
throw Exception (
" Attempted to initialize a new wallet using an existing wallet ID! " ) ;
}
await _prefs . init ( ) ;
try {
await _generateNewWallet ( ) ;
} catch ( e , s ) {
Logging . instance . log ( " Exception rethrown from initializeNew(): $ e \n $ s " ,
level: LogLevel . Fatal ) ;
rethrow ;
}
await Future . wait ( [
2023-01-12 21:32:25 +00:00
updateCachedId ( walletId ) ,
updateCachedIsFavorite ( false ) ,
2022-09-26 20:32:53 +00:00
] ) ;
}
@ override
Future < void > initializeExisting ( ) async {
2023-02-13 18:13:30 +00:00
Logging . instance . log ( " initializeExisting() ${ coin . prettyName } wallet. " ,
2022-09-26 20:32:53 +00:00
level: LogLevel . Info ) ;
2023-01-12 21:32:25 +00:00
if ( getCachedId ( ) = = null ) {
2022-09-26 20:32:53 +00:00
throw Exception (
" Attempted to initialize an existing wallet using an unknown wallet ID! " ) ;
}
2023-01-11 19:30:03 +00:00
2022-09-26 20:32:53 +00:00
await _prefs . init ( ) ;
2023-02-13 18:13:30 +00:00
// await _checkCurrentChangeAddressesForTransactions();
// await _checkCurrentReceivingAddressesForTransactions();
2022-09-26 20:32:53 +00:00
}
2022-11-07 16:24:08 +00:00
// hack to add tx to txData before refresh completes
// required based on current app architecture where we don't properly store
// transactions locally in a good way
@ override
Future < void > updateSentCachedTxData ( Map < String , dynamic > txData ) async {
2023-02-02 22:19:14 +00:00
final transaction = isar_models . Transaction (
walletId: walletId ,
txid: txData [ " txid " ] as String ,
timestamp: DateTime . now ( ) . millisecondsSinceEpoch ~ / 1000 ,
type: isar_models . TransactionType . outgoing ,
subType: isar_models . TransactionSubType . none ,
2023-04-06 23:49:13 +00:00
// precision may be lost here hence the following amountString
amount: ( txData [ " recipientAmt " ] as Amount ) . raw . toInt ( ) ,
amountString: ( txData [ " recipientAmt " ] as Amount ) . toJsonString ( ) ,
2023-02-02 22:19:14 +00:00
fee: txData [ " fee " ] as int ,
height: null ,
isCancelled: false ,
isLelantus: false ,
otherData: null ,
slateId: null ,
2023-03-31 16:15:42 +00:00
nonce: null ,
2023-02-03 19:22:21 +00:00
inputs: [ ] ,
outputs: [ ] ,
2023-02-02 22:19:14 +00:00
) ;
final address = txData [ " address " ] is String
? await db . getAddress ( walletId , txData [ " address " ] as String )
: null ;
await db . addNewTransactionData (
[
2023-02-03 19:22:21 +00:00
Tuple2 ( transaction , address ) ,
2023-02-02 22:19:14 +00:00
] ,
walletId ,
) ;
2022-11-07 16:24:08 +00:00
}
2022-11-07 20:46:36 +00:00
bool validateCashAddr ( String cashAddr ) {
String addr = cashAddr ;
if ( cashAddr . contains ( " : " ) ) {
addr = cashAddr . split ( " : " ) . last ;
}
return addr . startsWith ( " q " ) ;
}
2022-09-26 20:32:53 +00:00
@ override
bool validateAddress ( String address ) {
try {
// 0 for bitcoincash: address scheme, 1 for legacy address
2022-11-04 19:55:13 +00:00
final format = bitbox . Address . detectFormat ( address ) ;
if ( kDebugMode ) {
print ( " format $ format " ) ;
}
2022-11-04 23:22:22 +00:00
2022-11-05 00:18:25 +00:00
if ( _coin = = Coin . bitcoincashTestnet ) {
return true ;
}
2022-11-04 23:22:22 +00:00
if ( format = = bitbox . Address . formatCashAddr ) {
2022-11-07 20:46:36 +00:00
return validateCashAddr ( address ) ;
2022-11-04 23:22:22 +00:00
} else {
return address . startsWith ( " 1 " ) ;
}
2022-11-04 19:55:13 +00:00
} catch ( e ) {
2022-09-26 20:32:53 +00:00
return false ;
}
}
@ override
String get walletId = > _walletId ;
2023-01-12 21:32:25 +00:00
late final String _walletId ;
2022-09-26 20:32:53 +00:00
@ override
String get walletName = > _walletName ;
late String _walletName ;
// setter for updating on rename
@ override
set walletName ( String newName ) = > _walletName = newName ;
late ElectrumX _electrumXClient ;
ElectrumX get electrumXClient = > _electrumXClient ;
late CachedElectrumX _cachedElectrumXClient ;
CachedElectrumX get cachedElectrumXClient = > _cachedElectrumXClient ;
2022-11-09 23:48:43 +00:00
late SecureStorageInterface _secureStore ;
2022-09-26 20:32:53 +00:00
@ override
Future < void > updateNode ( bool shouldRefresh ) async {
2022-11-09 22:43:26 +00:00
final failovers = NodeService ( secureStorageInterface: _secureStore )
2022-09-26 20:32:53 +00:00
. failoverNodesFor ( coin: coin )
. map ( ( e ) = > ElectrumXNode (
address: e . host ,
port: e . port ,
name: e . name ,
id: e . id ,
useSSL: e . useSSL ,
) )
. toList ( ) ;
final newNode = await getCurrentNode ( ) ;
_cachedElectrumXClient = CachedElectrumX . from (
node: newNode ,
prefs: _prefs ,
failovers: failovers ,
) ;
_electrumXClient = ElectrumX . from (
node: newNode ,
prefs: _prefs ,
failovers: failovers ,
) ;
if ( shouldRefresh ) {
2022-11-04 19:55:13 +00:00
unawaited ( refresh ( ) ) ;
2022-09-26 20:32:53 +00:00
}
}
Future < List < String > > _getMnemonicList ( ) async {
2023-02-03 22:34:06 +00:00
final _mnemonicString = await mnemonicString ;
if ( _mnemonicString = = null ) {
2022-09-26 20:32:53 +00:00
return [ ] ;
}
2023-02-03 22:34:06 +00:00
final List < String > data = _mnemonicString . split ( ' ' ) ;
2022-09-26 20:32:53 +00:00
return data ;
}
Future < ElectrumXNode > getCurrentNode ( ) async {
2022-11-09 22:43:26 +00:00
final node = NodeService ( secureStorageInterface: _secureStore )
. getPrimaryNodeFor ( coin: coin ) ? ?
2022-09-26 20:32:53 +00:00
DefaultNodes . getNodeFor ( coin ) ;
return ElectrumXNode (
address: node . host ,
port: node . port ,
name: node . name ,
useSSL: node . useSSL ,
id: node . id ,
) ;
}
2023-01-11 19:30:03 +00:00
Future < List < isar_models . Address > > _fetchAllOwnAddresses ( ) async {
2023-01-16 21:04:03 +00:00
final allAddresses = await db
. getAddresses ( walletId )
2023-01-11 19:30:03 +00:00
. filter ( )
2023-01-18 23:20:23 +00:00
. not ( )
. typeEqualTo ( isar_models . AddressType . nonWallet )
. and ( )
. group ( ( q ) = > q
. subTypeEqualTo ( isar_models . AddressSubType . receiving )
. or ( )
. subTypeEqualTo ( isar_models . AddressSubType . change ) )
2023-01-11 19:30:03 +00:00
. findAll ( ) ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
// for (var i = 0; i < receivingAddressesP2PKH.length; i++) {
// if (!allAddresses.contains(receivingAddressesP2PKH[i])) {
// allAddresses.add(receivingAddressesP2PKH[i] as String);
2022-09-26 20:32:53 +00:00
// }
// }
2023-01-11 19:30:03 +00:00
// for (var i = 0; i < changeAddressesP2PKH.length; i++) {
// if (!allAddresses.contains(changeAddressesP2PKH[i])) {
// allAddresses.add(changeAddressesP2PKH[i] as String);
2022-09-26 20:32:53 +00:00
// }
// }
return allAddresses ;
}
Future < FeeObject > _getFees ( ) async {
try {
//TODO adjust numbers for different speeds?
const int f = 1 , m = 5 , s = 20 ;
final fast = await electrumXClient . estimateFee ( blocks: f ) ;
final medium = await electrumXClient . estimateFee ( blocks: m ) ;
final slow = await electrumXClient . estimateFee ( blocks: s ) ;
final feeObject = FeeObject (
numberOfBlocksFast: f ,
numberOfBlocksAverage: m ,
numberOfBlocksSlow: s ,
2023-04-05 22:06:31 +00:00
fast: Amount . fromDecimal (
fast ,
fractionDigits: coin . decimals ,
) . raw . toInt ( ) ,
medium: Amount . fromDecimal (
medium ,
fractionDigits: coin . decimals ,
) . raw . toInt ( ) ,
slow: Amount . fromDecimal (
slow ,
fractionDigits: coin . decimals ,
) . raw . toInt ( ) ,
2022-09-26 20:32:53 +00:00
) ;
Logging . instance . log ( " fetched fees: $ feeObject " , level: LogLevel . Info ) ;
return feeObject ;
} catch ( e ) {
Logging . instance
. log ( " Exception rethrown from _getFees(): $ e " , level: LogLevel . Error ) ;
rethrow ;
}
}
Future < void > _generateNewWallet ( ) async {
2022-11-30 15:46:28 +00:00
Logging . instance
. log ( " IS_INTEGRATION_TEST: $ integrationTestFlag " , level: LogLevel . Info ) ;
if ( ! integrationTestFlag ) {
2022-11-30 15:54:46 +00:00
try {
final features = await electrumXClient
. getServerFeatures ( )
. timeout ( const Duration ( seconds: 3 ) ) ;
Logging . instance . log ( " features: $ features " , level: LogLevel . Info ) ;
switch ( coin ) {
case Coin . bitcoincash:
if ( features [ ' genesis_hash ' ] ! = GENESIS_HASH_MAINNET ) {
throw Exception ( " genesis hash does not match main net! " ) ;
}
break ;
case Coin . bitcoincashTestnet:
if ( features [ ' genesis_hash ' ] ! = GENESIS_HASH_TESTNET ) {
throw Exception ( " genesis hash does not match test net! " ) ;
}
break ;
default :
throw Exception (
" Attempted to generate a BitcoinWallet using a non bitcoin coin type: ${ coin . name } " ) ;
}
} catch ( e , s ) {
Logging . instance . log ( " $ e /n $ s " , level: LogLevel . Info ) ;
2022-11-30 15:46:28 +00:00
}
}
2022-09-26 20:32:53 +00:00
// this should never fail
2023-02-03 22:34:06 +00:00
if ( ( await mnemonicString ) ! = null | | ( await mnemonicPassphrase ) ! = null ) {
2022-09-26 20:32:53 +00:00
throw Exception (
" Attempted to overwrite mnemonic on generate new wallet! " ) ;
}
await _secureStore . write (
key: ' ${ _walletId } _mnemonic ' ,
value: bip39 . generateMnemonic ( strength: 256 ) ) ;
2023-02-03 22:34:06 +00:00
await _secureStore . write ( key: ' ${ _walletId } _mnemonicPassphrase ' , value: " " ) ;
2022-09-26 20:32:53 +00:00
// Generate and add addresses to relevant arrays
2023-01-11 19:30:03 +00:00
final initialAddresses = await Future . wait ( [
// P2PKH
_generateAddressForChain ( 0 , 0 , DerivePathType . bip44 ) ,
_generateAddressForChain ( 1 , 0 , DerivePathType . bip44 ) ,
// P2SH
_generateAddressForChain ( 0 , 0 , DerivePathType . bip49 ) ,
_generateAddressForChain ( 1 , 0 , DerivePathType . bip49 ) ,
] ) ;
2022-09-23 12:31:14 +00:00
2023-01-16 21:04:03 +00:00
await db . putAddresses ( initialAddresses ) ;
2022-09-26 20:32:53 +00:00
Logging . instance . log ( " _generateNewWalletFinished " , level: LogLevel . Info ) ;
}
2022-09-23 12:31:14 +00:00
/// Generates a new internal or external chain address for the wallet using a BIP44 or BIP49 derivation path.
2022-09-26 20:32:53 +00:00
/// [chain] - Use 0 for receiving (external), 1 for change (internal). Should not be any other value!
/// [index] - This can be any integer >= 0
2023-01-11 19:30:03 +00:00
Future < isar_models . Address > _generateAddressForChain (
2022-09-26 20:32:53 +00:00
int chain ,
int index ,
DerivePathType derivePathType ,
) async {
2023-02-03 22:34:06 +00:00
final _mnemonic = await mnemonicString ;
final _mnemonicPassphrase = await mnemonicPassphrase ;
2023-02-13 22:45:54 +00:00
if ( _mnemonicPassphrase = = null ) {
Logging . instance . log (
" Exception in _generateAddressForChain: mnemonic passphrase null, possible migration issue; if using internal builds, delete wallet and restore from seed, if using a release build, please file bug report " ,
level: LogLevel . Error ) ;
}
2023-02-03 22:34:06 +00:00
final derivePath = constructDerivePath (
derivePathType: derivePathType ,
networkWIF: _network . wif ,
chain: chain ,
index: index ,
) ;
final node = await Bip32Utils . getBip32Node (
_mnemonic ! ,
_mnemonicPassphrase ! ,
_network ,
derivePath ,
2022-09-26 20:32:53 +00:00
) ;
2023-02-03 22:34:06 +00:00
2022-09-26 20:32:53 +00:00
final data = PaymentData ( pubkey: node . publicKey ) ;
2023-02-02 15:24:26 +00:00
2022-09-26 20:32:53 +00:00
String address ;
2023-01-11 19:30:03 +00:00
isar_models . AddressType addrType ;
2022-09-26 20:32:53 +00:00
switch ( derivePathType ) {
case DerivePathType . bip44:
2023-02-02 15:24:26 +00:00
case DerivePathType . bch44:
address = P2PKH ( data: data , network: _network ) . data . address ! ;
2023-02-04 00:51:30 +00:00
addrType = isar_models . AddressType . p2pkh ;
2023-02-02 15:24:26 +00:00
address = bitbox . Address . toCashAddress ( address ) ;
break ;
2022-09-23 12:31:14 +00:00
case DerivePathType . bip49:
2023-01-12 15:52:41 +00:00
address = P2SH (
data: PaymentData (
redeem: P2WPKH ( data: data , network: _network ) . data ) ,
network: _network )
. data
. address ! ;
2023-01-11 19:30:03 +00:00
addrType = isar_models . AddressType . p2sh ;
2022-09-23 12:31:14 +00:00
break ;
2023-01-25 18:08:48 +00:00
case DerivePathType . bip84:
throw UnsupportedError ( " bip84 not supported by BCH " ) ;
2023-02-02 15:24:26 +00:00
default :
throw Exception ( " DerivePathType $ derivePathType not supported " ) ;
2022-09-26 20:32:53 +00:00
}
// add generated address & info to derivations
await addDerivation (
chain: chain ,
address: address ,
pubKey: Format . uint8listToString ( node . publicKey ) ,
wif: node . toWIF ( ) ,
derivePathType: derivePathType ,
) ;
2023-01-16 22:37:00 +00:00
return isar_models . Address (
walletId: walletId ,
value: address ,
publicKey: node . publicKey ,
type: addrType ,
derivationIndex: index ,
2023-02-03 23:30:32 +00:00
derivationPath: isar_models . DerivationPath ( ) . . value = derivePath ,
2023-01-16 22:37:00 +00:00
subType: chain = = 0
2023-01-11 19:30:03 +00:00
? isar_models . AddressSubType . receiving
2023-01-16 22:37:00 +00:00
: isar_models . AddressSubType . change ,
) ;
2022-09-26 20:32:53 +00:00
}
/// Returns the latest receiving/change (external/internal) address for the wallet depending on [chain]
/// and
/// [chain] - Use 0 for receiving (external), 1 for change (internal). Should not be any other value!
Future < String > _getCurrentAddressForChain (
2023-01-11 19:30:03 +00:00
int chain ,
DerivePathType derivePathType ,
) async {
final subType = chain = = 0 // Here, we assume that chain == 1 if it isn't 0
? isar_models . AddressSubType . receiving
: isar_models . AddressSubType . change ;
isar_models . AddressType type ;
2023-02-04 00:51:30 +00:00
String coinType ;
String purpose ;
2022-09-26 20:32:53 +00:00
switch ( derivePathType ) {
case DerivePathType . bip44:
2023-01-11 19:30:03 +00:00
type = isar_models . AddressType . p2pkh ;
2023-02-04 00:51:30 +00:00
coinType = coin = = Coin . bitcoincash ? " 145 " : " 1 " ;
purpose = " 44 " ;
2022-09-26 20:32:53 +00:00
break ;
2023-02-02 15:24:26 +00:00
case DerivePathType . bch44:
2023-02-04 00:51:30 +00:00
type = isar_models . AddressType . p2pkh ;
coinType = coin = = Coin . bitcoincash ? " 0 " : " 1 " ;
purpose = " 44 " ;
2023-02-02 15:24:26 +00:00
break ;
2022-09-23 12:31:14 +00:00
case DerivePathType . bip49:
2023-01-11 19:30:03 +00:00
type = isar_models . AddressType . p2sh ;
2023-02-04 00:51:30 +00:00
coinType = coin = = Coin . bitcoincash ? " 145 " : " 1 " ;
purpose = " 49 " ;
2022-09-23 12:31:14 +00:00
break ;
2023-01-25 18:08:48 +00:00
case DerivePathType . bip84:
throw UnsupportedError ( " bip84 not supported by BCH " ) ;
2023-02-02 15:24:26 +00:00
default :
throw Exception ( " DerivePathType $ derivePathType not supported " ) ;
2022-09-26 20:32:53 +00:00
}
2022-09-23 15:14:22 +00:00
2023-01-16 21:04:03 +00:00
final address = await db
. getAddresses ( walletId )
2023-01-11 19:30:03 +00:00
. filter ( )
. typeEqualTo ( type )
. subTypeEqualTo ( subType )
2023-02-04 00:51:30 +00:00
. derivationPath ( ( q ) = > q . valueStartsWith ( " m/ $ purpose '/ $ coinType " ) )
2023-01-11 19:30:03 +00:00
. sortByDerivationIndexDesc ( )
. findFirst ( ) ;
return address ! . value ;
2022-09-26 20:32:53 +00:00
}
2023-01-11 19:30:03 +00:00
String _buildDerivationStorageKey ( {
required int chain ,
required DerivePathType derivePathType ,
} ) {
2022-09-26 20:32:53 +00:00
String key ;
String chainId = chain = = 0 ? " receive " : " change " ;
switch ( derivePathType ) {
case DerivePathType . bip44:
key = " ${ walletId } _ ${ chainId } DerivationsP2PKH " ;
break ;
2023-02-02 15:24:26 +00:00
case DerivePathType . bch44:
key = " ${ walletId } _ ${ chainId } DerivationsBch44P2PKH " ;
break ;
2022-09-23 12:31:14 +00:00
case DerivePathType . bip49:
key = " ${ walletId } _ ${ chainId } DerivationsP2SH " ;
break ;
2023-02-14 17:43:48 +00:00
default :
throw UnsupportedError (
" ${ derivePathType . name } not supported by ${ coin . prettyName } " ) ;
2022-09-26 20:32:53 +00:00
}
return key ;
}
Future < Map < String , dynamic > > _fetchDerivations (
{ required int chain , required DerivePathType derivePathType } ) async {
// build lookup key
final key = _buildDerivationStorageKey (
chain: chain , derivePathType: derivePathType ) ;
// fetch current derivations
final derivationsString = await _secureStore . read ( key: key ) ;
return Map < String , dynamic > . from (
jsonDecode ( derivationsString ? ? " {} " ) as Map ) ;
}
/// Add a single derivation to the local secure storage for [chain] and
/// [derivePathType] where [chain] must either be 1 for change or 0 for receive.
/// This will overwrite a previous entry where the address of the new derivation
/// matches a derivation currently stored.
Future < void > addDerivation ( {
required int chain ,
required String address ,
required String pubKey ,
required String wif ,
required DerivePathType derivePathType ,
} ) async {
// build lookup key
final key = _buildDerivationStorageKey (
chain: chain , derivePathType: derivePathType ) ;
// fetch current derivations
final derivationsString = await _secureStore . read ( key: key ) ;
final derivations =
Map < String , dynamic > . from ( jsonDecode ( derivationsString ? ? " {} " ) as Map ) ;
// add derivation
derivations [ address ] = {
" pubKey " : pubKey ,
" wif " : wif ,
} ;
// save derivations
final newReceiveDerivationsString = jsonEncode ( derivations ) ;
await _secureStore . write ( key: key , value: newReceiveDerivationsString ) ;
}
/// Add multiple derivations to the local secure storage for [chain] and
/// [derivePathType] where [chain] must either be 1 for change or 0 for receive.
/// This will overwrite any previous entries where the address of the new derivation
/// matches a derivation currently stored.
/// The [derivationsToAdd] must be in the format of:
/// {
/// addressA : {
/// "pubKey": <the pubKey string>,
/// "wif": <the wif string>,
/// },
/// addressB : {
/// "pubKey": <the pubKey string>,
/// "wif": <the wif string>,
/// },
/// }
Future < void > addDerivations ( {
required int chain ,
required DerivePathType derivePathType ,
required Map < String , dynamic > derivationsToAdd ,
} ) async {
// build lookup key
final key = _buildDerivationStorageKey (
chain: chain , derivePathType: derivePathType ) ;
// fetch current derivations
final derivationsString = await _secureStore . read ( key: key ) ;
final derivations =
Map < String , dynamic > . from ( jsonDecode ( derivationsString ? ? " {} " ) as Map ) ;
// add derivation
derivations . addAll ( derivationsToAdd ) ;
// save derivations
final newReceiveDerivationsString = jsonEncode ( derivations ) ;
await _secureStore . write ( key: key , value: newReceiveDerivationsString ) ;
}
2023-01-11 19:30:03 +00:00
Future < void > _updateUTXOs ( ) async {
final allAddresses = await _fetchAllOwnAddresses ( ) ;
2022-09-26 20:32:53 +00:00
try {
final fetchedUtxoList = < List < Map < String , dynamic > > > [ ] ;
final Map < int , Map < String , List < dynamic > > > batches = { } ;
const batchSizeMax = 10 ;
int batchNumber = 0 ;
for ( int i = 0 ; i < allAddresses . length ; i + + ) {
if ( batches [ batchNumber ] = = null ) {
batches [ batchNumber ] = { } ;
}
2023-01-11 19:30:03 +00:00
final scripthash =
_convertToScriptHash ( allAddresses [ i ] . value , _network ) ;
2022-11-04 19:55:13 +00:00
if ( kDebugMode ) {
print ( " SCRIPT_HASH_FOR_ADDRESS ${ allAddresses [ i ] } IS $ scripthash " ) ;
}
2022-09-26 20:32:53 +00:00
batches [ batchNumber ] ! . addAll ( {
scripthash: [ scripthash ]
} ) ;
if ( i % batchSizeMax = = batchSizeMax - 1 ) {
batchNumber + + ;
}
}
for ( int i = 0 ; i < batches . length ; i + + ) {
final response =
await _electrumXClient . getBatchUTXOs ( args: batches [ i ] ! ) ;
for ( final entry in response . entries ) {
if ( entry . value . isNotEmpty ) {
fetchedUtxoList . add ( entry . value ) ;
}
}
}
2023-01-11 19:30:03 +00:00
final List < isar_models . UTXO > outputArray = [ ] ;
2022-09-26 20:32:53 +00:00
for ( int i = 0 ; i < fetchedUtxoList . length ; i + + ) {
for ( int j = 0 ; j < fetchedUtxoList [ i ] . length ; j + + ) {
2023-03-08 22:11:46 +00:00
final jsonUTXO = fetchedUtxoList [ i ] [ j ] ;
2022-09-26 20:32:53 +00:00
final txn = await cachedElectrumXClient . getTransaction (
2023-03-08 22:11:46 +00:00
txHash: jsonUTXO [ " tx_hash " ] as String ,
2022-09-26 20:32:53 +00:00
verbose: true ,
coin: coin ,
) ;
2023-03-08 22:11:46 +00:00
final vout = jsonUTXO [ " tx_pos " ] as int ;
final outputs = txn [ " vout " ] as List ;
String ? utxoOwnerAddress ;
// get UTXO owner address
for ( final output in outputs ) {
if ( output [ " n " ] = = vout ) {
utxoOwnerAddress =
output [ " scriptPubKey " ] ? [ " addresses " ] ? [ 0 ] as String ? ? ?
output [ " scriptPubKey " ] ? [ " address " ] as String ? ;
}
}
2023-01-16 22:37:00 +00:00
final utxo = isar_models . UTXO (
walletId: walletId ,
txid: txn [ " txid " ] as String ,
2023-03-08 22:11:46 +00:00
vout: vout ,
value: jsonUTXO [ " value " ] as int ,
2023-01-16 22:37:00 +00:00
name: " " ,
isBlocked: false ,
blockedReason: null ,
isCoinbase: txn [ " is_coinbase " ] as bool ? ? ? false ,
blockHash: txn [ " blockhash " ] as String ? ,
2023-03-08 22:11:46 +00:00
blockHeight: jsonUTXO [ " height " ] as int ? ,
2023-01-16 22:37:00 +00:00
blockTime: txn [ " blocktime " ] as int ? ,
2023-03-08 22:11:46 +00:00
address: utxoOwnerAddress ,
2023-01-16 22:37:00 +00:00
) ;
2023-01-11 19:30:03 +00:00
2022-09-26 20:32:53 +00:00
outputArray . add ( utxo ) ;
}
}
2023-01-11 19:30:03 +00:00
Logging . instance
. log ( ' Outputs fetched: $ outputArray ' , level: LogLevel . Info ) ;
2022-09-26 20:32:53 +00:00
2023-03-08 22:11:46 +00:00
await db . updateUTXOs ( walletId , outputArray ) ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
// finally update balance
2023-03-08 22:11:46 +00:00
await _updateBalance ( ) ;
2022-09-26 20:32:53 +00:00
} catch ( e , s ) {
Logging . instance
. log ( " Output fetch unsuccessful: $ e \n $ s " , level: LogLevel . Error ) ;
}
}
2023-03-08 22:11:46 +00:00
Future < void > _updateBalance ( ) async {
await refreshBalance ( ) ;
}
2023-01-11 19:30:03 +00:00
@ override
2023-01-12 21:32:25 +00:00
Balance get balance = > _balance ? ? = getCachedBalance ( ) ;
2023-01-11 19:30:03 +00:00
Balance ? _balance ;
2022-09-26 20:32:53 +00:00
Future < int > getTxCount ( { required String address } ) async {
String ? scripthash ;
try {
scripthash = _convertToScriptHash ( address , _network ) ;
final transactions =
await electrumXClient . getHistory ( scripthash: scripthash ) ;
return transactions . length ;
} catch ( e ) {
Logging . instance . log (
" Exception rethrown in _getTxCount(address: $ address , scripthash: $ scripthash ): $ e " ,
level: LogLevel . Error ) ;
rethrow ;
}
}
Future < Map < String , int > > _getBatchTxCount ( {
required Map < String , String > addresses ,
} ) async {
try {
final Map < String , List < dynamic > > args = { } ;
2022-11-04 19:55:13 +00:00
if ( kDebugMode ) {
print ( " Address $ addresses " ) ;
}
2022-09-26 20:32:53 +00:00
for ( final entry in addresses . entries ) {
args [ entry . key ] = [ _convertToScriptHash ( entry . value , _network ) ] ;
}
2022-11-04 19:55:13 +00:00
if ( kDebugMode ) {
print ( " Args ${ jsonEncode ( args ) } " ) ;
}
2022-09-26 20:32:53 +00:00
final response = await electrumXClient . getBatchHistory ( args: args ) ;
2022-11-04 19:55:13 +00:00
if ( kDebugMode ) {
print ( " Response ${ jsonEncode ( response ) } " ) ;
}
2022-09-26 20:32:53 +00:00
final Map < String , int > result = { } ;
for ( final entry in response . entries ) {
result [ entry . key ] = entry . value . length ;
}
2022-11-04 19:55:13 +00:00
if ( kDebugMode ) {
print ( " result ${ jsonEncode ( result ) } " ) ;
}
2022-09-26 20:32:53 +00:00
return result ;
} catch ( e , s ) {
Logging . instance . log (
" Exception rethrown in _getBatchTxCount(address: $ addresses : $ e \n $ s " ,
level: LogLevel . Error ) ;
rethrow ;
}
}
2023-01-11 19:30:03 +00:00
Future < void > _checkReceivingAddressForTransactions ( ) async {
2022-09-26 20:32:53 +00:00
try {
2023-01-11 19:30:03 +00:00
final currentReceiving = await _currentReceivingAddress ;
final int txCount = await getTxCount ( address: currentReceiving . value ) ;
2022-09-26 20:32:53 +00:00
Logging . instance . log (
2023-01-11 19:30:03 +00:00
' Number of txs for current receiving address $ currentReceiving : $ txCount ' ,
2022-09-26 20:32:53 +00:00
level: LogLevel . Info ) ;
2023-01-23 16:32:53 +00:00
if ( txCount > = 1 | | currentReceiving . derivationIndex < 0 ) {
2022-09-26 20:32:53 +00:00
// First increment the receiving index
2023-01-11 19:30:03 +00:00
final newReceivingIndex = currentReceiving . derivationIndex + 1 ;
2022-09-26 20:32:53 +00:00
// Use new index to derive a new receiving address
final newReceivingAddress = await _generateAddressForChain (
2023-01-25 19:49:14 +00:00
0 , newReceivingIndex , DerivePathTypeExt . primaryFor ( coin ) ) ;
2022-09-26 20:32:53 +00:00
2023-01-18 22:55:59 +00:00
final existing = await db
. getAddresses ( walletId )
. filter ( )
. valueEqualTo ( newReceivingAddress . value )
. findFirst ( ) ;
if ( existing = = null ) {
// Add that new change address
await db . putAddress ( newReceivingAddress ) ;
} else {
// we need to update the address
await db . updateAddress ( existing , newReceivingAddress ) ;
}
2023-01-23 16:32:53 +00:00
// keep checking until address with no tx history is set as current
await _checkReceivingAddressForTransactions ( ) ;
2022-09-26 20:32:53 +00:00
}
} on SocketException catch ( se , s ) {
Logging . instance . log (
2023-01-25 19:49:14 +00:00
" SocketException caught in _checkReceivingAddressForTransactions( ${ DerivePathTypeExt . primaryFor ( coin ) } ): $ se \n $ s " ,
2022-09-26 20:32:53 +00:00
level: LogLevel . Error ) ;
return ;
} catch ( e , s ) {
Logging . instance . log (
2023-01-25 19:49:14 +00:00
" Exception rethrown from _checkReceivingAddressForTransactions( ${ DerivePathTypeExt . primaryFor ( coin ) } ): $ e \n $ s " ,
2022-09-26 20:32:53 +00:00
level: LogLevel . Error ) ;
rethrow ;
}
}
2023-01-11 19:30:03 +00:00
Future < void > _checkChangeAddressForTransactions ( ) async {
2022-09-26 20:32:53 +00:00
try {
2023-01-11 19:30:03 +00:00
final currentChange = await _currentChangeAddress ;
final int txCount = await getTxCount ( address: currentChange . value ) ;
2022-09-26 20:32:53 +00:00
Logging . instance . log (
2023-01-11 19:30:03 +00:00
' Number of txs for current change address $ currentChange : $ txCount ' ,
2022-09-26 20:32:53 +00:00
level: LogLevel . Info ) ;
2023-01-23 16:32:53 +00:00
if ( txCount > = 1 | | currentChange . derivationIndex < 0 ) {
2022-09-26 20:32:53 +00:00
// First increment the change index
2023-01-11 19:30:03 +00:00
final newChangeIndex = currentChange . derivationIndex + 1 ;
2022-09-26 20:32:53 +00:00
// Use new index to derive a new change address
2023-01-11 19:30:03 +00:00
final newChangeAddress = await _generateAddressForChain (
2023-01-25 19:49:14 +00:00
1 , newChangeIndex , DerivePathTypeExt . primaryFor ( coin ) ) ;
2022-09-26 20:32:53 +00:00
2023-01-18 22:55:59 +00:00
final existing = await db
. getAddresses ( walletId )
. filter ( )
. valueEqualTo ( newChangeAddress . value )
. findFirst ( ) ;
if ( existing = = null ) {
// Add that new change address
await db . putAddress ( newChangeAddress ) ;
} else {
// we need to update the address
await db . updateAddress ( existing , newChangeAddress ) ;
}
2023-01-23 16:32:53 +00:00
// keep checking until address with no tx history is set as current
await _checkChangeAddressForTransactions ( ) ;
2022-09-26 20:32:53 +00:00
}
2023-01-11 19:30:03 +00:00
} on SocketException catch ( se , s ) {
Logging . instance . log (
2023-01-25 19:49:14 +00:00
" SocketException caught in _checkReceivingAddressForTransactions( ${ DerivePathTypeExt . primaryFor ( coin ) } ): $ se \n $ s " ,
2023-01-11 19:30:03 +00:00
level: LogLevel . Error ) ;
return ;
2022-09-26 20:32:53 +00:00
} catch ( e , s ) {
Logging . instance . log (
2023-01-25 19:49:14 +00:00
" Exception rethrown from _checkReceivingAddressForTransactions( ${ DerivePathTypeExt . primaryFor ( coin ) } ): $ e \n $ s " ,
2022-09-26 20:32:53 +00:00
level: LogLevel . Error ) ;
rethrow ;
}
}
Future < void > _checkCurrentReceivingAddressesForTransactions ( ) async {
try {
2023-01-11 19:30:03 +00:00
// for (final type in DerivePathType.values) {
await _checkReceivingAddressForTransactions ( ) ;
// }
2022-09-26 20:32:53 +00:00
} catch ( e , s ) {
Logging . instance . log (
" Exception rethrown from _checkCurrentReceivingAddressesForTransactions(): $ e \n $ s " ,
level: LogLevel . Info ) ;
rethrow ;
}
}
/// public wrapper because dart can't test private...
Future < void > checkCurrentReceivingAddressesForTransactions ( ) async {
if ( Platform . environment [ " FLUTTER_TEST " ] = = " true " ) {
try {
return _checkCurrentReceivingAddressesForTransactions ( ) ;
} catch ( _ ) {
rethrow ;
}
}
}
Future < void > _checkCurrentChangeAddressesForTransactions ( ) async {
try {
2023-01-11 19:30:03 +00:00
// for (final type in DerivePathType.values) {
await _checkChangeAddressForTransactions ( ) ;
// }
2022-09-26 20:32:53 +00:00
} catch ( e , s ) {
Logging . instance . log (
" Exception rethrown from _checkCurrentChangeAddressesForTransactions(): $ e \n $ s " ,
level: LogLevel . Error ) ;
rethrow ;
}
}
/// public wrapper because dart can't test private...
Future < void > checkCurrentChangeAddressesForTransactions ( ) async {
if ( Platform . environment [ " FLUTTER_TEST " ] = = " true " ) {
try {
return _checkCurrentChangeAddressesForTransactions ( ) ;
} catch ( _ ) {
rethrow ;
}
}
}
/// attempts to convert a string to a valid scripthash
///
/// Returns the scripthash or throws an exception on invalid bch address
String _convertToScriptHash ( String bchAddress , NetworkType network ) {
try {
2022-11-04 20:10:30 +00:00
if ( bitbox . Address . detectFormat ( bchAddress ) = =
2022-11-07 20:46:36 +00:00
bitbox . Address . formatCashAddr & &
validateCashAddr ( bchAddress ) ) {
2022-11-04 19:55:13 +00:00
bchAddress = bitbox . Address . toLegacyAddress ( bchAddress ) ;
2022-10-19 20:21:48 +00:00
}
2023-01-11 19:30:03 +00:00
return AddressUtils . convertToScriptHash ( bchAddress , network ) ;
2022-09-26 20:32:53 +00:00
} catch ( e ) {
rethrow ;
}
}
Future < List < Map < String , dynamic > > > _fetchHistory (
List < String > allAddresses ) async {
try {
List < Map < String , dynamic > > allTxHashes = [ ] ;
final Map < int , Map < String , List < dynamic > > > batches = { } ;
final Map < String , String > requestIdToAddressMap = { } ;
const batchSizeMax = 10 ;
int batchNumber = 0 ;
for ( int i = 0 ; i < allAddresses . length ; i + + ) {
if ( batches [ batchNumber ] = = null ) {
batches [ batchNumber ] = { } ;
}
final scripthash = _convertToScriptHash ( allAddresses [ i ] , _network ) ;
final id = Logger . isTestEnv ? " $ i " : const Uuid ( ) . v1 ( ) ;
requestIdToAddressMap [ id ] = allAddresses [ i ] ;
batches [ batchNumber ] ! . addAll ( {
id: [ scripthash ]
} ) ;
if ( i % batchSizeMax = = batchSizeMax - 1 ) {
batchNumber + + ;
}
}
for ( int i = 0 ; i < batches . length ; i + + ) {
final response =
await _electrumXClient . getBatchHistory ( args: batches [ i ] ! ) ;
for ( final entry in response . entries ) {
for ( int j = 0 ; j < entry . value . length ; j + + ) {
entry . value [ j ] [ " address " ] = requestIdToAddressMap [ entry . key ] ;
if ( ! allTxHashes . contains ( entry . value [ j ] ) ) {
allTxHashes . add ( entry . value [ j ] ) ;
}
}
}
}
return allTxHashes ;
} catch ( e , s ) {
Logging . instance . log ( " _fetchHistory: $ e \n $ s " , level: LogLevel . Error ) ;
rethrow ;
}
}
bool _duplicateTxCheck (
List < Map < String , dynamic > > allTransactions , String txid ) {
for ( int i = 0 ; i < allTransactions . length ; i + + ) {
if ( allTransactions [ i ] [ " txid " ] = = txid ) {
return true ;
}
}
return false ;
}
2023-01-11 19:30:03 +00:00
Future < void > _refreshTransactions ( ) async {
List < isar_models . Address > allAddressesOld = await _fetchAllOwnAddresses ( ) ;
Set < String > receivingAddresses = allAddressesOld
. where ( ( e ) = > e . subType = = isar_models . AddressSubType . receiving )
. map ( ( e ) {
if ( bitbox . Address . detectFormat ( e . value ) = = bitbox . Address . formatLegacy & &
2023-02-02 15:24:26 +00:00
( addressType ( address: e . value ) = = DerivePathType . bip44 | |
addressType ( address: e . value ) = = DerivePathType . bch44 ) ) {
2023-01-11 19:30:03 +00:00
return bitbox . Address . toCashAddress ( e . value ) ;
2022-10-19 20:21:48 +00:00
} else {
2023-01-11 19:30:03 +00:00
return e . value ;
2022-10-19 20:21:48 +00:00
}
2023-01-11 19:30:03 +00:00
} ) . toSet ( ) ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
Set < String > changeAddresses = allAddressesOld
. where ( ( e ) = > e . subType = = isar_models . AddressSubType . change )
. map ( ( e ) {
if ( bitbox . Address . detectFormat ( e . value ) = = bitbox . Address . formatLegacy & &
2023-02-02 15:24:26 +00:00
( addressType ( address: e . value ) = = DerivePathType . bip44 | |
addressType ( address: e . value ) = = DerivePathType . bch44 ) ) {
2023-01-11 19:30:03 +00:00
return bitbox . Address . toCashAddress ( e . value ) ;
2022-10-19 20:21:48 +00:00
} else {
2023-01-11 19:30:03 +00:00
return e . value ;
2022-10-19 20:21:48 +00:00
}
2023-01-11 19:30:03 +00:00
} ) . toSet ( ) ;
2022-09-26 20:32:53 +00:00
final List < Map < String , dynamic > > allTxHashes =
2023-01-11 19:30:03 +00:00
await _fetchHistory ( [ . . . receivingAddresses , . . . changeAddresses ] ) ;
2022-09-26 20:32:53 +00:00
List < Map < String , dynamic > > allTransactions = [ ] ;
2023-01-13 21:36:19 +00:00
final currentHeight = await chainHeight ;
2022-09-26 20:32:53 +00:00
for ( final txHash in allTxHashes ) {
2023-01-16 21:04:03 +00:00
final storedTx = await db
. getTransactions ( walletId )
. filter ( )
2023-01-13 21:36:19 +00:00
. txidEqualTo ( txHash [ " tx_hash " ] as String )
. findFirst ( ) ;
2023-03-24 22:43:34 +00:00
if ( storedTx = = null | | storedTx . address . value = = null
// zero conf messes this up
// !storedTx.isConfirmed(currentHeight, MINIMUM_CONFIRMATIONS)
) {
2023-01-13 21:36:19 +00:00
final tx = await cachedElectrumXClient . getTransaction (
txHash: txHash [ " tx_hash " ] as String ,
verbose: true ,
coin: coin ,
) ;
2022-09-26 20:32:53 +00:00
2023-01-13 21:36:19 +00:00
// Logging.instance.log("TRANSACTION: ${jsonEncode(tx)}");
if ( ! _duplicateTxCheck ( allTransactions , tx [ " txid " ] as String ) ) {
2023-01-16 21:04:03 +00:00
tx [ " address " ] = await db
. getAddresses ( walletId )
2023-01-13 21:36:19 +00:00
. filter ( )
. valueEqualTo ( txHash [ " address " ] as String )
. findFirst ( ) ;
tx [ " height " ] = txHash [ " height " ] ;
allTransactions . add ( tx ) ;
}
2022-09-26 20:32:53 +00:00
}
}
2023-01-11 19:30:03 +00:00
//
// Logging.instance.log("addAddresses: $allAddresses", level: LogLevel.Info);
// Logging.instance.log("allTxHashes: $allTxHashes", level: LogLevel.Info);
//
// Logging.instance.log("allTransactions length: ${allTransactions.length}",
// level: LogLevel.Info);
2022-09-26 20:32:53 +00:00
2023-02-03 19:22:21 +00:00
final List < Tuple2 < isar_models . Transaction , isar_models . Address ? > > txns = [ ] ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
for ( final txData in allTransactions ) {
Set < String > inputAddresses = { } ;
Set < String > outputAddresses = { } ;
2022-09-26 20:32:53 +00:00
2023-04-05 22:06:31 +00:00
Amount totalInputValue = Amount (
rawValue: BigInt . from ( 0 ) ,
fractionDigits: coin . decimals ,
) ;
Amount totalOutputValue = Amount (
rawValue: BigInt . from ( 0 ) ,
fractionDigits: coin . decimals ,
) ;
2022-09-26 20:32:53 +00:00
2023-04-05 22:06:31 +00:00
Amount amountSentFromWallet = Amount (
rawValue: BigInt . from ( 0 ) ,
fractionDigits: coin . decimals ,
) ;
Amount amountReceivedInWallet = Amount (
rawValue: BigInt . from ( 0 ) ,
fractionDigits: coin . decimals ,
) ;
Amount changeAmount = Amount (
rawValue: BigInt . from ( 0 ) ,
fractionDigits: coin . decimals ,
) ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
// parse inputs
for ( final input in txData [ " vin " ] as List ) {
2022-09-26 20:32:53 +00:00
final prevTxid = input [ " txid " ] as String ;
final prevOut = input [ " vout " ] as int ;
2023-01-11 19:30:03 +00:00
// fetch input tx to get address
final inputTx = await cachedElectrumXClient . getTransaction (
txHash: prevTxid ,
coin: coin ,
) ;
for ( final output in inputTx [ " vout " ] as List ) {
// check matching output
if ( prevOut = = output [ " n " ] ) {
// get value
2023-04-05 22:06:31 +00:00
final value = Amount . fromDecimal (
2023-01-11 19:30:03 +00:00
Decimal . parse ( output [ " value " ] . toString ( ) ) ,
2023-04-05 22:06:31 +00:00
fractionDigits: coin . decimals ,
2023-01-11 19:30:03 +00:00
) ;
// add value to total
2023-04-05 22:06:31 +00:00
totalInputValue = totalInputValue + value ;
2023-01-11 19:30:03 +00:00
// get input(prevOut) address
final address =
output [ " scriptPubKey " ] ? [ " addresses " ] ? [ 0 ] as String ? ? ?
output [ " scriptPubKey " ] ? [ " address " ] as String ? ;
2022-09-26 20:32:53 +00:00
if ( address ! = null ) {
2023-01-11 19:30:03 +00:00
inputAddresses . add ( address ) ;
// if input was from my wallet, add value to amount sent
if ( receivingAddresses . contains ( address ) | |
changeAddresses . contains ( address ) ) {
2023-04-05 22:06:31 +00:00
amountSentFromWallet = amountSentFromWallet + value ;
2023-01-11 19:30:03 +00:00
}
2022-09-26 20:32:53 +00:00
}
}
}
}
2023-01-11 19:30:03 +00:00
// parse outputs
for ( final output in txData [ " vout " ] as List ) {
// get value
2023-04-05 22:06:31 +00:00
final value = Amount . fromDecimal (
2023-01-11 19:30:03 +00:00
Decimal . parse ( output [ " value " ] . toString ( ) ) ,
2023-04-05 22:06:31 +00:00
fractionDigits: coin . decimals ,
2023-01-11 19:30:03 +00:00
) ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
// add value to total
totalOutputValue + = value ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
// get output address
final address = output [ " scriptPubKey " ] ? [ " addresses " ] ? [ 0 ] as String ? ? ?
output [ " scriptPubKey " ] ? [ " address " ] as String ? ;
if ( address ! = null ) {
outputAddresses . add ( address ) ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
// if output was to my wallet, add value to amount received
if ( receivingAddresses . contains ( address ) ) {
amountReceivedInWallet + = value ;
} else if ( changeAddresses . contains ( address ) ) {
changeAmount + = value ;
2022-09-26 20:32:53 +00:00
}
}
2023-01-11 19:30:03 +00:00
}
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
final mySentFromAddresses = [
. . . receivingAddresses . intersection ( inputAddresses ) ,
. . . changeAddresses . intersection ( inputAddresses )
] ;
final myReceivedOnAddresses =
receivingAddresses . intersection ( outputAddresses ) ;
final myChangeReceivedOnAddresses =
changeAddresses . intersection ( outputAddresses ) ;
2022-09-26 20:32:53 +00:00
2023-01-11 19:30:03 +00:00
final fee = totalInputValue - totalOutputValue ;
2022-09-26 20:32:53 +00:00
2023-01-13 21:36:19 +00:00
// this is the address initially used to fetch the txid
isar_models . Address transactionAddress =
txData [ " address " ] as isar_models . Address ;
2023-01-16 22:37:00 +00:00
isar_models . TransactionType type ;
2023-04-05 22:06:31 +00:00
Amount amount ;
2023-01-11 19:30:03 +00:00
if ( mySentFromAddresses . isNotEmpty & & myReceivedOnAddresses . isNotEmpty ) {
// tx is sent to self
2023-01-16 22:37:00 +00:00
type = isar_models . TransactionType . sentToSelf ;
amount =
2023-01-11 19:30:03 +00:00
amountSentFromWallet - amountReceivedInWallet - fee - changeAmount ;
} else if ( mySentFromAddresses . isNotEmpty ) {
// outgoing tx
2023-01-16 22:37:00 +00:00
type = isar_models . TransactionType . outgoing ;
amount = amountSentFromWallet - changeAmount - fee ;
2023-01-13 21:36:19 +00:00
final possible =
outputAddresses . difference ( myChangeReceivedOnAddresses ) . first ;
if ( transactionAddress . value ! = possible ) {
2023-01-16 22:37:00 +00:00
transactionAddress = isar_models . Address (
walletId: walletId ,
value: possible ,
publicKey: [ ] ,
type: AddressType . nonWallet ,
derivationIndex: - 1 ,
2023-02-03 23:30:32 +00:00
derivationPath: null ,
2023-01-16 22:37:00 +00:00
subType: AddressSubType . nonWallet ,
) ;
2023-01-13 21:36:19 +00:00
}
2022-09-26 20:32:53 +00:00
} else {
2023-01-11 19:30:03 +00:00
// incoming tx
2023-01-16 22:37:00 +00:00
type = isar_models . TransactionType . incoming ;
amount = amountReceivedInWallet ;
2023-01-11 19:30:03 +00:00
}
2023-01-13 21:36:19 +00:00
List < isar_models . Input > inputs = [ ] ;
List < isar_models . Output > outputs = [ ] ;
2023-01-11 19:30:03 +00:00
for ( final json in txData [ " vin " ] as List ) {
bool isCoinBase = json [ ' coinbase ' ] ! = null ;
2023-01-16 22:37:00 +00:00
final input = isar_models . Input (
txid: json [ ' txid ' ] as String ,
vout: json [ ' vout ' ] as int ? ? ? - 1 ,
scriptSig: json [ ' scriptSig ' ] ? [ ' hex ' ] as String ? ,
scriptSigAsm: json [ ' scriptSig ' ] ? [ ' asm ' ] as String ? ,
isCoinbase: isCoinBase ? isCoinBase : json [ ' is_coinbase ' ] as bool ? ,
sequence: json [ ' sequence ' ] as int ? ,
innerRedeemScriptAsm: json [ ' innerRedeemscriptAsm ' ] as String ? ,
) ;
2023-01-13 21:36:19 +00:00
inputs . add ( input ) ;
2023-01-11 19:30:03 +00:00
}
for ( final json in txData [ " vout " ] as List ) {
2023-01-16 22:37:00 +00:00
final output = isar_models . Output (
scriptPubKey: json [ ' scriptPubKey ' ] ? [ ' hex ' ] as String ? ,
scriptPubKeyAsm: json [ ' scriptPubKey ' ] ? [ ' asm ' ] as String ? ,
scriptPubKeyType: json [ ' scriptPubKey ' ] ? [ ' type ' ] as String ? ,
scriptPubKeyAddress:
json [ " scriptPubKey " ] ? [ " addresses " ] ? [ 0 ] as String ? ? ?
json [ ' scriptPubKey ' ] [ ' type ' ] as String ,
2023-04-05 22:06:31 +00:00
value: Amount . fromDecimal (
2023-01-16 22:37:00 +00:00
Decimal . parse ( json [ " value " ] . toString ( ) ) ,
2023-04-05 22:06:31 +00:00
fractionDigits: coin . decimals ,
) . raw . toInt ( ) ,
2023-01-11 19:30:03 +00:00
) ;
2023-01-13 21:36:19 +00:00
outputs . add ( output ) ;
2022-09-26 20:32:53 +00:00
}
2023-02-03 19:22:21 +00:00
final tx = isar_models . Transaction (
walletId: walletId ,
txid: txData [ " txid " ] as String ,
timestamp: txData [ " blocktime " ] as int ? ? ?
( DateTime . now ( ) . millisecondsSinceEpoch ~ / 1000 ) ,
type: type ,
subType: isar_models . TransactionSubType . none ,
2023-04-05 22:06:31 +00:00
amount: amount . raw . toInt ( ) ,
amountString: amount . toJsonString ( ) ,
fee: fee . raw . toInt ( ) ,
2023-02-03 19:22:21 +00:00
height: txData [ " height " ] as int ? ,
isCancelled: false ,
isLelantus: false ,
slateId: null ,
otherData: null ,
2023-03-31 16:15:42 +00:00
nonce: null ,
2023-02-03 19:22:21 +00:00
inputs: inputs ,
outputs: outputs ,
) ;
txns . add ( Tuple2 ( tx , transactionAddress ) ) ;
2023-01-11 19:30:03 +00:00
}
2022-09-26 20:32:53 +00:00
2023-01-19 21:13:03 +00:00
await db . addNewTransactionData ( txns , walletId ) ;
2023-01-19 16:29:00 +00:00
// quick hack to notify manager to call notifyListeners if
// transactions changed
if ( txns . isNotEmpty ) {
GlobalEventBus . instance . fire (
UpdatedInBackgroundEvent (
" Transactions updated/added for: $ walletId $ walletName " ,
walletId ,
) ,
) ;
}
2022-09-26 20:32:53 +00:00
}
int estimateTxFee ( { required int vSize , required int feeRatePerKB } ) {
return vSize * ( feeRatePerKB / 1000 ) . ceil ( ) ;
}
/// The coinselection algorithm decides whether or not the user is eligible to make the transaction
/// with [satoshiAmountToSend] and [selectedTxFeeRate]. If so, it will call buildTrasaction() and return
/// a map containing the tx hex along with other important information. If not, then it will return
/// an integer (1 or 2)
2023-03-08 22:11:46 +00:00
dynamic coinSelection ( {
required int satoshiAmountToSend ,
required int selectedTxFeeRate ,
required String recipientAddress ,
required bool coinControl ,
required bool isSendAll ,
2023-01-11 19:30:03 +00:00
int additionalOutputs = 0 ,
List < isar_models . UTXO > ? utxos ,
} ) async {
2022-09-26 20:32:53 +00:00
Logging . instance
. log ( " Starting coinSelection ---------- " , level: LogLevel . Info ) ;
2023-01-11 19:30:03 +00:00
final List < isar_models . UTXO > availableOutputs = utxos ? ? await this . utxos ;
final currentChainHeight = await chainHeight ;
final List < isar_models . UTXO > spendableOutputs = [ ] ;
2022-09-26 20:32:53 +00:00
int spendableSatoshiValue = 0 ;
// Build list of spendable outputs and totaling their satoshi amount
2023-03-08 22:11:46 +00:00
for ( final utxo in availableOutputs ) {
if ( utxo . isBlocked = = false & &
utxo . isConfirmed ( currentChainHeight , MINIMUM_CONFIRMATIONS ) & &
utxo . used ! = true ) {
spendableOutputs . add ( utxo ) ;
spendableSatoshiValue + = utxo . value ;
2022-09-26 20:32:53 +00:00
}
}
2023-03-08 22:11:46 +00:00
if ( coinControl ) {
if ( spendableOutputs . length < availableOutputs . length ) {
throw ArgumentError ( " Attempted to use an unavailable utxo " ) ;
}
}
// don't care about sorting if using all utxos
if ( ! coinControl ) {
// sort spendable by age (oldest first)
spendableOutputs . sort ( ( a , b ) = > b . blockTime ! . compareTo ( a . blockTime ! ) ) ;
}
2022-09-26 20:32:53 +00:00
Logging . instance . log ( " spendableOutputs.length: ${ spendableOutputs . length } " ,
level: LogLevel . Info ) ;
Logging . instance
. log ( " spendableOutputs: $ spendableOutputs " , level: LogLevel . Info ) ;
Logging . instance . log ( " spendableSatoshiValue: $ spendableSatoshiValue " ,
level: LogLevel . Info ) ;
Logging . instance
. log ( " satoshiAmountToSend: $ satoshiAmountToSend " , level: LogLevel . Info ) ;
// If the amount the user is trying to send is smaller than the amount that they have spendable,
// then return 1, which indicates that they have an insufficient balance.
if ( spendableSatoshiValue < satoshiAmountToSend ) {
return 1 ;
// If the amount the user wants to send is exactly equal to the amount they can spend, then return
// 2, which indicates that they are not leaving enough over to pay the transaction fee
} else if ( spendableSatoshiValue = = satoshiAmountToSend & & ! isSendAll ) {
return 2 ;
}
// If neither of these statements pass, we assume that the user has a spendable balance greater
// than the amount they're attempting to send. Note that this value still does not account for
// the added transaction fee, which may require an extra input and will need to be checked for
// later on.
// Possible situation right here
int satoshisBeingUsed = 0 ;
int inputsBeingConsumed = 0 ;
2023-01-11 19:30:03 +00:00
List < isar_models . UTXO > utxoObjectsToUse = [ ] ;
2022-09-26 20:32:53 +00:00
2023-03-08 22:11:46 +00:00
if ( ! coinControl ) {
for ( var i = 0 ;
satoshisBeingUsed < satoshiAmountToSend & &
i < spendableOutputs . length ;
i + + ) {
utxoObjectsToUse . add ( spendableOutputs [ i ] ) ;
satoshisBeingUsed + = spendableOutputs [ i ] . value ;
inputsBeingConsumed + = 1 ;
}
for ( int i = 0 ;
i < additionalOutputs & &
inputsBeingConsumed < spendableOutputs . length ;
i + + ) {
utxoObjectsToUse . add ( spendableOutputs [ inputsBeingConsumed ] ) ;
satoshisBeingUsed + = spendableOutputs [ inputsBeingConsumed ] . value ;
inputsBeingConsumed + = 1 ;
}
} else {
satoshisBeingUsed = spendableSatoshiValue ;
utxoObjectsToUse = spendableOutputs ;
2023-03-09 16:30:10 +00:00
inputsBeingConsumed = spendableOutputs . length ;
2022-09-26 20:32:53 +00:00
}
Logging . instance
. log ( " satoshisBeingUsed: $ satoshisBeingUsed " , level: LogLevel . Info ) ;
Logging . instance
. log ( " inputsBeingConsumed: $ inputsBeingConsumed " , level: LogLevel . Info ) ;
Logging . instance
. log ( ' utxoObjectsToUse: $ utxoObjectsToUse ' , level: LogLevel . Info ) ;
Logging . instance
. log ( ' satoshiAmountToSend $ satoshiAmountToSend ' , level: LogLevel . Info ) ;
// numberOfOutputs' length must always be equal to that of recipientsArray and recipientsAmtArray
2023-03-08 22:11:46 +00:00
List < String > recipientsArray = [ recipientAddress ] ;
2022-09-26 20:32:53 +00:00
List < int > recipientsAmtArray = [ satoshiAmountToSend ] ;
// gather required signing data
final utxoSigningData = await fetchBuildTxData ( utxoObjectsToUse ) ;
if ( isSendAll ) {
Logging . instance
. log ( " Attempting to send all $ coin " , level: LogLevel . Info ) ;
final int vSizeForOneOutput = ( await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
2023-03-08 22:11:46 +00:00
recipients: [ recipientAddress ] ,
2022-09-26 20:32:53 +00:00
satoshiAmounts: [ satoshisBeingUsed - 1 ] ,
) ) [ " vSize " ] as int ;
int feeForOneOutput = estimateTxFee (
vSize: vSizeForOneOutput ,
feeRatePerKB: selectedTxFeeRate ,
) ;
if ( feeForOneOutput < ( vSizeForOneOutput + 1 ) ) {
feeForOneOutput = ( vSizeForOneOutput + 1 ) ;
}
final int amount = satoshiAmountToSend - feeForOneOutput ;
dynamic txn = await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
recipients: recipientsArray ,
satoshiAmounts: [ amount ] ,
) ;
Map < String , dynamic > transactionObject = {
" hex " : txn [ " hex " ] ,
" recipient " : recipientsArray [ 0 ] ,
" recipientAmt " : amount ,
" fee " : feeForOneOutput ,
" vSize " : txn [ " vSize " ] ,
2023-03-08 22:11:46 +00:00
" usedUTXOs " : utxoObjectsToUse ,
2022-09-26 20:32:53 +00:00
} ;
return transactionObject ;
}
final int vSizeForOneOutput = ( await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
2023-03-08 22:11:46 +00:00
recipients: [ recipientAddress ] ,
2022-09-26 20:32:53 +00:00
satoshiAmounts: [ satoshisBeingUsed - 1 ] ,
) ) [ " vSize " ] as int ;
final int vSizeForTwoOutPuts = ( await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
recipients: [
2023-03-08 22:11:46 +00:00
recipientAddress ,
2023-01-25 19:49:14 +00:00
await _getCurrentAddressForChain ( 1 , DerivePathTypeExt . primaryFor ( coin ) ) ,
2022-09-26 20:32:53 +00:00
] ,
satoshiAmounts: [
satoshiAmountToSend ,
satoshisBeingUsed - satoshiAmountToSend - 1 ,
] , // dust limit is the minimum amount a change output should be
) ) [ " vSize " ] as int ;
2022-12-13 00:17:02 +00:00
//todo: check if print needed
2022-12-19 16:29:47 +00:00
// debugPrint("vSizeForOneOutput $vSizeForOneOutput");
// debugPrint("vSizeForTwoOutPuts $vSizeForTwoOutPuts");
2022-09-26 20:32:53 +00:00
// Assume 1 output, only for recipient and no change
var feeForOneOutput = estimateTxFee (
vSize: vSizeForOneOutput ,
feeRatePerKB: selectedTxFeeRate ,
) ;
// Assume 2 outputs, one for recipient and one for change
var feeForTwoOutputs = estimateTxFee (
vSize: vSizeForTwoOutPuts ,
feeRatePerKB: selectedTxFeeRate ,
) ;
Logging . instance
. log ( " feeForTwoOutputs: $ feeForTwoOutputs " , level: LogLevel . Info ) ;
Logging . instance
. log ( " feeForOneOutput: $ feeForOneOutput " , level: LogLevel . Info ) ;
if ( feeForOneOutput < ( vSizeForOneOutput + 1 ) ) {
feeForOneOutput = ( vSizeForOneOutput + 1 ) ;
}
if ( feeForTwoOutputs < ( ( vSizeForTwoOutPuts + 1 ) ) ) {
feeForTwoOutputs = ( ( vSizeForTwoOutPuts + 1 ) ) ;
}
Logging . instance
. log ( " feeForTwoOutputs: $ feeForTwoOutputs " , level: LogLevel . Info ) ;
Logging . instance
. log ( " feeForOneOutput: $ feeForOneOutput " , level: LogLevel . Info ) ;
if ( satoshisBeingUsed - satoshiAmountToSend > feeForOneOutput ) {
if ( satoshisBeingUsed - satoshiAmountToSend >
2023-04-05 22:06:31 +00:00
feeForOneOutput + DUST_LIMIT . raw . toInt ( ) ) {
2022-09-26 20:32:53 +00:00
// Here, we know that theoretically, we may be able to include another output(change) but we first need to
// factor in the value of this output in satoshis.
int changeOutputSize =
satoshisBeingUsed - satoshiAmountToSend - feeForTwoOutputs ;
// We check to see if the user can pay for the new transaction with 2 outputs instead of one. If they can and
// the second output's size > 546 satoshis, we perform the mechanics required to properly generate and use a new
// change address.
2023-04-05 22:06:31 +00:00
if ( changeOutputSize > DUST_LIMIT . raw . toInt ( ) & &
2022-09-26 20:32:53 +00:00
satoshisBeingUsed - satoshiAmountToSend - changeOutputSize = =
feeForTwoOutputs ) {
// generate new change address if current change address has been used
2023-01-11 19:30:03 +00:00
await _checkChangeAddressForTransactions ( ) ;
2023-01-25 19:49:14 +00:00
final String newChangeAddress = await _getCurrentAddressForChain (
1 , DerivePathTypeExt . primaryFor ( coin ) ) ;
2022-09-26 20:32:53 +00:00
int feeBeingPaid =
satoshisBeingUsed - satoshiAmountToSend - changeOutputSize ;
recipientsArray . add ( newChangeAddress ) ;
recipientsAmtArray . add ( changeOutputSize ) ;
// At this point, we have the outputs we're going to use, the amounts to send along with which addresses
// we intend to send these amounts to. We have enough to send instructions to build the transaction.
Logging . instance . log ( ' 2 outputs in tx ' , level: LogLevel . Info ) ;
Logging . instance
. log ( ' Input size: $ satoshisBeingUsed ' , level: LogLevel . Info ) ;
Logging . instance . log ( ' Recipient output size: $ satoshiAmountToSend ' ,
level: LogLevel . Info ) ;
Logging . instance . log ( ' Change Output Size: $ changeOutputSize ' ,
level: LogLevel . Info ) ;
Logging . instance . log (
' Difference (fee being paid): $ feeBeingPaid sats ' ,
level: LogLevel . Info ) ;
Logging . instance
. log ( ' Estimated fee: $ feeForTwoOutputs ' , level: LogLevel . Info ) ;
dynamic txn = await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
recipients: recipientsArray ,
satoshiAmounts: recipientsAmtArray ,
) ;
// make sure minimum fee is accurate if that is being used
if ( txn [ " vSize " ] - feeBeingPaid = = 1 ) {
int changeOutputSize =
satoshisBeingUsed - satoshiAmountToSend - ( txn [ " vSize " ] as int ) ;
feeBeingPaid =
satoshisBeingUsed - satoshiAmountToSend - changeOutputSize ;
recipientsAmtArray . removeLast ( ) ;
recipientsAmtArray . add ( changeOutputSize ) ;
Logging . instance . log ( ' Adjusted Input size: $ satoshisBeingUsed ' ,
level: LogLevel . Info ) ;
Logging . instance . log (
' Adjusted Recipient output size: $ satoshiAmountToSend ' ,
level: LogLevel . Info ) ;
Logging . instance . log (
' Adjusted Change Output Size: $ changeOutputSize ' ,
level: LogLevel . Info ) ;
Logging . instance . log (
' Adjusted Difference (fee being paid): $ feeBeingPaid sats ' ,
level: LogLevel . Info ) ;
Logging . instance . log ( ' Adjusted Estimated fee: $ feeForTwoOutputs ' ,
level: LogLevel . Info ) ;
txn = await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
recipients: recipientsArray ,
satoshiAmounts: recipientsAmtArray ,
) ;
}
Map < String , dynamic > transactionObject = {
" hex " : txn [ " hex " ] ,
" recipient " : recipientsArray [ 0 ] ,
" recipientAmt " : recipientsAmtArray [ 0 ] ,
" fee " : feeBeingPaid ,
" vSize " : txn [ " vSize " ] ,
2023-03-08 22:11:46 +00:00
" usedUTXOs " : utxoObjectsToUse ,
2022-09-26 20:32:53 +00:00
} ;
return transactionObject ;
} else {
// Something went wrong here. It either overshot or undershot the estimated fee amount or the changeOutputSize
// is smaller than or equal to [DUST_LIMIT]. Revert to single output transaction.
Logging . instance . log ( ' 1 output in tx ' , level: LogLevel . Info ) ;
Logging . instance
. log ( ' Input size: $ satoshisBeingUsed ' , level: LogLevel . Info ) ;
Logging . instance . log ( ' Recipient output size: $ satoshiAmountToSend ' ,
level: LogLevel . Info ) ;
Logging . instance . log (
' Difference (fee being paid): ${ satoshisBeingUsed - satoshiAmountToSend } sats ' ,
level: LogLevel . Info ) ;
Logging . instance
. log ( ' Estimated fee: $ feeForOneOutput ' , level: LogLevel . Info ) ;
dynamic txn = await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
recipients: recipientsArray ,
satoshiAmounts: recipientsAmtArray ,
) ;
Map < String , dynamic > transactionObject = {
" hex " : txn [ " hex " ] ,
" recipient " : recipientsArray [ 0 ] ,
" recipientAmt " : recipientsAmtArray [ 0 ] ,
" fee " : satoshisBeingUsed - satoshiAmountToSend ,
" vSize " : txn [ " vSize " ] ,
2023-03-08 22:11:46 +00:00
" usedUTXOs " : utxoObjectsToUse ,
2022-09-26 20:32:53 +00:00
} ;
return transactionObject ;
}
} else {
// No additional outputs needed since adding one would mean that it'd be smaller than 546 sats
// which makes it uneconomical to add to the transaction. Here, we pass data directly to instruct
// the wallet to begin crafting the transaction that the user requested.
Logging . instance . log ( ' 1 output in tx ' , level: LogLevel . Info ) ;
Logging . instance
. log ( ' Input size: $ satoshisBeingUsed ' , level: LogLevel . Info ) ;
Logging . instance . log ( ' Recipient output size: $ satoshiAmountToSend ' ,
level: LogLevel . Info ) ;
Logging . instance . log (
' Difference (fee being paid): ${ satoshisBeingUsed - satoshiAmountToSend } sats ' ,
level: LogLevel . Info ) ;
Logging . instance
. log ( ' Estimated fee: $ feeForOneOutput ' , level: LogLevel . Info ) ;
dynamic txn = await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
recipients: recipientsArray ,
satoshiAmounts: recipientsAmtArray ,
) ;
Map < String , dynamic > transactionObject = {
" hex " : txn [ " hex " ] ,
" recipient " : recipientsArray [ 0 ] ,
" recipientAmt " : recipientsAmtArray [ 0 ] ,
" fee " : satoshisBeingUsed - satoshiAmountToSend ,
" vSize " : txn [ " vSize " ] ,
2023-03-08 22:11:46 +00:00
" usedUTXOs " : utxoObjectsToUse ,
2022-09-26 20:32:53 +00:00
} ;
return transactionObject ;
}
} else if ( satoshisBeingUsed - satoshiAmountToSend = = feeForOneOutput ) {
// In this scenario, no additional change output is needed since inputs - outputs equal exactly
// what we need to pay for fees. Here, we pass data directly to instruct the wallet to begin
// crafting the transaction that the user requested.
Logging . instance . log ( ' 1 output in tx ' , level: LogLevel . Info ) ;
Logging . instance
. log ( ' Input size: $ satoshisBeingUsed ' , level: LogLevel . Info ) ;
Logging . instance . log ( ' Recipient output size: $ satoshiAmountToSend ' ,
level: LogLevel . Info ) ;
Logging . instance . log (
' Fee being paid: ${ satoshisBeingUsed - satoshiAmountToSend } sats ' ,
level: LogLevel . Info ) ;
Logging . instance
. log ( ' Estimated fee: $ feeForOneOutput ' , level: LogLevel . Info ) ;
dynamic txn = await buildTransaction (
utxosToUse: utxoObjectsToUse ,
utxoSigningData: utxoSigningData ,
recipients: recipientsArray ,
satoshiAmounts: recipientsAmtArray ,
) ;
Map < String , dynamic > transactionObject = {
" hex " : txn [ " hex " ] ,
" recipient " : recipientsArray [ 0 ] ,
" recipientAmt " : recipientsAmtArray [ 0 ] ,
" fee " : feeForOneOutput ,
" vSize " : txn [ " vSize " ] ,
2023-03-08 22:11:46 +00:00
" usedUTXOs " : utxoObjectsToUse ,
2022-09-26 20:32:53 +00:00
} ;
return transactionObject ;
} else {
// Remember that returning 2 indicates that the user does not have a sufficient balance to
// pay for the transaction fee. Ideally, at this stage, we should check if the user has any
// additional outputs they're able to spend and then recalculate fees.
Logging . instance . log (
' Cannot pay tx fee - checking for more outputs and trying again ' ,
level: LogLevel . Warning ) ;
// try adding more outputs
if ( spendableOutputs . length > inputsBeingConsumed ) {
2023-03-08 22:11:46 +00:00
return coinSelection (
satoshiAmountToSend: satoshiAmountToSend ,
selectedTxFeeRate: selectedTxFeeRate ,
recipientAddress: recipientAddress ,
isSendAll: isSendAll ,
additionalOutputs: additionalOutputs + 1 ,
utxos: utxos ,
coinControl: coinControl ,
) ;
2022-09-26 20:32:53 +00:00
}
return 2 ;
}
}
Future < Map < String , dynamic > > fetchBuildTxData (
2023-01-11 19:30:03 +00:00
List < isar_models . UTXO > utxosToUse ,
2022-09-26 20:32:53 +00:00
) async {
// return data
Map < String , dynamic > results = { } ;
Map < String , List < String > > addressTxid = { } ;
// addresses to check
List < String > addressesP2PKH = [ ] ;
2022-09-23 12:31:14 +00:00
List < String > addressesP2SH = [ ] ;
2022-09-26 20:32:53 +00:00
try {
// Populating the addresses to check
for ( var i = 0 ; i < utxosToUse . length ; i + + ) {
final txid = utxosToUse [ i ] . txid ;
final tx = await _cachedElectrumXClient . getTransaction (
txHash: txid ,
coin: coin ,
) ;
for ( final output in tx [ " vout " ] as List ) {
final n = output [ " n " ] ;
if ( n ! = null & & n = = utxosToUse [ i ] . vout ) {
2022-10-19 20:21:48 +00:00
String address = output [ " scriptPubKey " ] [ " addresses " ] [ 0 ] as String ;
2023-02-01 16:38:49 +00:00
if ( bitbox . Address . detectFormat ( address ) ! =
2022-11-04 20:10:30 +00:00
bitbox . Address . formatCashAddr ) {
2023-02-01 16:38:49 +00:00
try {
address = bitbox . Address . toCashAddress ( address ) ;
} catch ( _ ) {
rethrow ;
2022-11-07 20:46:36 +00:00
}
2022-10-19 20:21:48 +00:00
}
2022-09-26 20:32:53 +00:00
if ( ! addressTxid . containsKey ( address ) ) {
addressTxid [ address ] = < String > [ ] ;
}
( addressTxid [ address ] as List ) . add ( txid ) ;
2023-02-14 17:43:48 +00:00
final deriveType = addressType ( address: address ) ;
switch ( deriveType ) {
2022-09-26 20:32:53 +00:00
case DerivePathType . bip44:
2023-02-02 15:24:26 +00:00
case DerivePathType . bch44:
2022-09-26 20:32:53 +00:00
addressesP2PKH . add ( address ) ;
break ;
2022-09-23 12:31:14 +00:00
case DerivePathType . bip49:
addressesP2SH . add ( address ) ;
break ;
2023-02-14 17:43:48 +00:00
default :
throw UnsupportedError (
" ${ deriveType . name } not supported by ${ coin . prettyName } " ) ;
2022-09-26 20:32:53 +00:00
}
}
}
}
// p2pkh / bip44
final p2pkhLength = addressesP2PKH . length ;
if ( p2pkhLength > 0 ) {
2023-02-02 17:38:41 +00:00
final receiveDerivationsBip44 = await _fetchDerivations (
2022-09-26 20:32:53 +00:00
chain: 0 ,
derivePathType: DerivePathType . bip44 ,
) ;
2023-02-02 17:38:41 +00:00
final changeDerivationsBip44 = await _fetchDerivations (
2022-09-26 20:32:53 +00:00
chain: 1 ,
derivePathType: DerivePathType . bip44 ,
) ;
2023-02-02 17:38:41 +00:00
final receiveDerivationsBch44 = await _fetchDerivations (
chain: 0 ,
derivePathType: DerivePathType . bch44 ,
) ;
final changeDerivationsBch44 = await _fetchDerivations (
chain: 1 ,
derivePathType: DerivePathType . bch44 ,
) ;
2022-09-26 20:32:53 +00:00
for ( int i = 0 ; i < p2pkhLength ; i + + ) {
2022-10-19 20:21:48 +00:00
String address = addressesP2PKH [ i ] ;
2022-09-26 20:32:53 +00:00
// receives
2023-02-02 17:38:41 +00:00
final receiveDerivation = receiveDerivationsBip44 [ address ] ? ?
receiveDerivationsBch44 [ address ] ;
2022-09-26 20:32:53 +00:00
// if a match exists it will not be null
if ( receiveDerivation ! = null ) {
final data = P2PKH (
data: PaymentData (
pubkey: Format . stringToUint8List (
receiveDerivation [ " pubKey " ] as String ) ) ,
network: _network ,
) . data ;
2022-10-19 20:21:48 +00:00
for ( String tx in addressTxid [ address ] ! ) {
2022-09-26 20:32:53 +00:00
results [ tx ] = {
" output " : data . output ,
" keyPair " : ECPair . fromWIF (
receiveDerivation [ " wif " ] as String ,
network: _network ,
) ,
} ;
}
} else {
// if its not a receive, check change
2023-02-02 17:38:41 +00:00
final changeDerivation = changeDerivationsBip44 [ address ] ? ?
changeDerivationsBch44 [ address ] ;
2022-09-26 20:32:53 +00:00
// if a match exists it will not be null
if ( changeDerivation ! = null ) {
final data = P2PKH (
data: PaymentData (
pubkey: Format . stringToUint8List (
changeDerivation [ " pubKey " ] as String ) ) ,
network: _network ,
) . data ;
2022-10-19 20:21:48 +00:00
for ( String tx in addressTxid [ address ] ! ) {
2022-09-26 20:32:53 +00:00
results [ tx ] = {
" output " : data . output ,
" keyPair " : ECPair . fromWIF (
changeDerivation [ " wif " ] as String ,
network: _network ,
) ,
} ;
}
}
}
}
}
2022-09-23 12:31:14 +00:00
// p2sh / bip49
final p2shLength = addressesP2SH . length ;
if ( p2shLength > 0 ) {
final receiveDerivations = await _fetchDerivations (
chain: 0 ,
derivePathType: DerivePathType . bip49 ,
) ;
final changeDerivations = await _fetchDerivations (
chain: 1 ,
derivePathType: DerivePathType . bip49 ,
) ;
for ( int i = 0 ; i < p2shLength ; i + + ) {
// receives
final receiveDerivation = receiveDerivations [ addressesP2SH [ i ] ] ;
// if a match exists it will not be null
if ( receiveDerivation ! = null ) {
final p2wpkh = P2WPKH (
data: PaymentData (
pubkey: Format . stringToUint8List (
receiveDerivation [ " pubKey " ] as String ) ) ,
network: _network )
. data ;
final redeemScript = p2wpkh . output ;
final data =
P2SH ( data: PaymentData ( redeem: p2wpkh ) , network: _network ) . data ;
for ( String tx in addressTxid [ addressesP2SH [ i ] ] ! ) {
results [ tx ] = {
" output " : data . output ,
" keyPair " : ECPair . fromWIF (
receiveDerivation [ " wif " ] as String ,
network: _network ,
) ,
" redeemScript " : redeemScript ,
} ;
}
} else {
// if its not a receive, check change
final changeDerivation = changeDerivations [ addressesP2SH [ i ] ] ;
// if a match exists it will not be null
if ( changeDerivation ! = null ) {
final p2wpkh = P2WPKH (
data: PaymentData (
pubkey: Format . stringToUint8List (
changeDerivation [ " pubKey " ] as String ) ) ,
network: _network )
. data ;
final redeemScript = p2wpkh . output ;
final data =
P2SH ( data: PaymentData ( redeem: p2wpkh ) , network: _network )
. data ;
for ( String tx in addressTxid [ addressesP2SH [ i ] ] ! ) {
results [ tx ] = {
" output " : data . output ,
" keyPair " : ECPair . fromWIF (
changeDerivation [ " wif " ] as String ,
network: _network ,
) ,
" redeemScript " : redeemScript ,
} ;
}
}
}
}
}
2022-09-26 20:32:53 +00:00
return results ;
} catch ( e , s ) {
Logging . instance
. log ( " fetchBuildTxData() threw: $ e , \n $ s " , level: LogLevel . Error ) ;
rethrow ;
}
}
/// Builds and signs a transaction
Future < Map < String , dynamic > > buildTransaction ( {
2023-01-11 19:30:03 +00:00
required List < isar_models . UTXO > utxosToUse ,
2022-09-26 20:32:53 +00:00
required Map < String , dynamic > utxoSigningData ,
required List < String > recipients ,
required List < int > satoshiAmounts ,
} ) async {
2022-11-04 19:55:13 +00:00
final builder = bitbox . Bitbox . transactionBuilder ( ) ;
2022-09-26 20:32:53 +00:00
// retrieve address' utxos from the rest api
2022-11-04 19:55:13 +00:00
List < bitbox . Utxo > _utxos =
2022-09-26 20:32:53 +00:00
[ ] ; // await Bitbox.Address.utxo(address) as List<Bitbox.Utxo>;
2022-11-04 19:55:13 +00:00
for ( var element in utxosToUse ) {
_utxos . add ( bitbox . Utxo (
2022-09-26 20:32:53 +00:00
element . txid ,
element . vout ,
2022-11-04 19:55:13 +00:00
bitbox . BitcoinCash . fromSatoshi ( element . value ) ,
2022-09-26 20:32:53 +00:00
element . value ,
0 ,
MINIMUM_CONFIRMATIONS + 1 ) ) ;
2022-11-04 19:55:13 +00:00
}
Logger . print ( " bch utxos: $ _utxos " ) ;
2022-09-26 20:32:53 +00:00
// placeholder for input signatures
2022-11-04 19:55:13 +00:00
final List < Map < dynamic , dynamic > > signatures = [ ] ;
2022-09-26 20:32:53 +00:00
// placeholder for total input balance
2022-11-04 19:55:13 +00:00
// int totalBalance = 0;
2022-09-26 20:32:53 +00:00
// iterate through the list of address _utxos and use them as inputs for the
// withdrawal transaction
2022-11-04 19:55:13 +00:00
for ( var utxo in _utxos ) {
2022-09-26 20:32:53 +00:00
// add the utxo as an input for the transaction
builder . addInput ( utxo . txid , utxo . vout ) ;
final ec = utxoSigningData [ utxo . txid ] [ " keyPair " ] as ECPair ;
2022-11-04 19:55:13 +00:00
final bitboxEC = bitbox . ECPair . fromWIF ( ec . toWIF ( ) ) ;
2022-09-26 20:32:53 +00:00
// add a signature to the list to be used later
signatures . add ( {
" vin " : signatures . length ,
" key_pair " : bitboxEC ,
" original_amount " : utxo . satoshis
} ) ;
2022-11-04 19:55:13 +00:00
// totalBalance += utxo.satoshis;
}
2022-09-26 20:32:53 +00:00
// calculate the fee based on number of inputs and one expected output
2022-11-04 19:55:13 +00:00
// final fee =
// bitbox.BitcoinCash.getByteCount(signatures.length, recipients.length);
2022-09-26 20:32:53 +00:00
// calculate how much balance will be left over to spend after the fee
2022-11-04 19:55:13 +00:00
// final sendAmount = totalBalance - fee;
2022-09-26 20:32:53 +00:00
// add the output based on the address provided in the testing data
for ( int i = 0 ; i < recipients . length ; i + + ) {
String recipient = recipients [ i ] ;
int satoshiAmount = satoshiAmounts [ i ] ;
builder . addOutput ( recipient , satoshiAmount ) ;
}
// sign all inputs
2022-11-04 19:55:13 +00:00
for ( var signature in signatures ) {
2022-09-26 20:32:53 +00:00
builder . sign (
signature [ " vin " ] as int ,
2022-11-04 19:55:13 +00:00
signature [ " key_pair " ] as bitbox . ECPair ,
2022-09-26 20:32:53 +00:00
signature [ " original_amount " ] as int ) ;
2022-11-04 19:55:13 +00:00
}
2022-09-26 20:32:53 +00:00
// build the transaction
final tx = builder . build ( ) ;
final txHex = tx . toHex ( ) ;
final vSize = tx . virtualSize ( ) ;
2022-12-13 00:17:02 +00:00
//todo: check if print needed
2022-09-26 20:32:53 +00:00
Logger . print ( " bch raw hex: $ txHex " ) ;
return { " hex " : txHex , " vSize " : vSize } ;
}
@ override
Future < void > fullRescan (
int maxUnusedAddressGap ,
int maxNumberOfIndexesToCheck ,
) async {
Logging . instance . log ( " Starting full rescan! " , level: LogLevel . Info ) ;
longMutex = true ;
GlobalEventBus . instance . fire (
WalletSyncStatusChangedEvent (
WalletSyncStatus . syncing ,
walletId ,
coin ,
) ,
) ;
// clear cache
2022-11-04 19:55:13 +00:00
await _cachedElectrumXClient . clearSharedTransactionCache ( coin: coin ) ;
2022-09-26 20:32:53 +00:00
2023-01-12 18:15:28 +00:00
// clear blockchain info
2023-01-16 21:04:03 +00:00
await db . deleteWalletBlockchainData ( walletId ) ;
2023-01-17 14:19:30 +00:00
await _deleteDerivations ( ) ;
2022-09-26 20:32:53 +00:00
try {
2023-02-03 22:34:06 +00:00
final _mnemonic = await mnemonicString ;
final _mnemonicPassphrase = await mnemonicPassphrase ;
2023-02-13 22:45:54 +00:00
if ( _mnemonicPassphrase = = null ) {
Logging . instance . log (
" Exception in fullRescan: mnemonic passphrase null, possible migration issue; if using internal builds, delete wallet and restore from seed, if using a release build, please file bug report " ,
level: LogLevel . Error ) ;
}
2023-02-03 22:34:06 +00:00
2022-09-26 20:32:53 +00:00
await _recoverWalletFromBIP32SeedPhrase (
2023-02-03 22:34:06 +00:00
mnemonic: _mnemonic ! ,
mnemonicPassphrase: _mnemonicPassphrase ! ,
2022-09-26 20:32:53 +00:00
maxUnusedAddressGap: maxUnusedAddressGap ,
maxNumberOfIndexesToCheck: maxNumberOfIndexesToCheck ,
2023-02-02 15:03:57 +00:00
isRescan: true ,
2022-09-26 20:32:53 +00:00
) ;
longMutex = false ;
2023-01-19 16:29:00 +00:00
await refresh ( ) ;
2022-09-26 20:32:53 +00:00
Logging . instance . log ( " Full rescan complete! " , level: LogLevel . Info ) ;
GlobalEventBus . instance . fire (
WalletSyncStatusChangedEvent (
WalletSyncStatus . synced ,
walletId ,
coin ,
) ,
) ;
} catch ( e , s ) {
GlobalEventBus . instance . fire (
WalletSyncStatusChangedEvent (
WalletSyncStatus . unableToSync ,
walletId ,
coin ,
) ,
) ;
longMutex = false ;
Logging . instance . log ( " Exception rethrown from fullRescan(): $ e \n $ s " ,
level: LogLevel . Error ) ;
rethrow ;
}
}
2023-01-17 14:19:30 +00:00
Future < void > _deleteDerivations ( ) async {
// P2PKH derivations
await _secureStore . delete ( key: " ${ walletId } _receiveDerivationsP2PKH " ) ;
await _secureStore . delete ( key: " ${ walletId } _changeDerivationsP2PKH " ) ;
// P2SH derivations
await _secureStore . delete ( key: " ${ walletId } _receiveDerivationsP2SH " ) ;
await _secureStore . delete ( key: " ${ walletId } _changeDerivationsP2SH " ) ;
}
2022-09-26 20:32:53 +00:00
@ override
set isFavorite ( bool markFavorite ) {
2023-01-12 21:32:25 +00:00
_isFavorite = markFavorite ;
updateCachedIsFavorite ( markFavorite ) ;
2022-09-26 20:32:53 +00:00
}
@ override
2023-01-12 21:32:25 +00:00
bool get isFavorite = > _isFavorite ? ? = getCachedIsFavorite ( ) ;
bool ? _isFavorite ;
2022-09-26 20:32:53 +00:00
@ override
bool get isRefreshing = > refreshMutex ;
bool isActive = false ;
@ override
void Function ( bool ) ? get onIsActiveWalletChanged = >
( isActive ) = > this . isActive = isActive ;
@ override
2023-04-05 22:06:31 +00:00
Future < Amount > estimateFeeFor ( Amount amount , int feeRate ) async {
2023-01-11 19:30:03 +00:00
final available = balance . spendable ;
2022-09-26 20:32:53 +00:00
2023-04-05 22:06:31 +00:00
if ( available = = amount ) {
return amount - ( await sweepAllEstimate ( feeRate ) ) ;
} else if ( amount < = Amount . zero | | amount > available ) {
2022-09-26 20:32:53 +00:00
return roughFeeEstimate ( 1 , 2 , feeRate ) ;
}
2023-04-05 22:06:31 +00:00
Amount runningBalance = Amount (
rawValue: BigInt . zero ,
fractionDigits: coin . decimals ,
) ;
2022-09-26 20:32:53 +00:00
int inputCount = 0 ;
2023-01-11 19:30:03 +00:00
for ( final output in ( await utxos ) ) {
if ( ! output . isBlocked ) {
2023-04-05 22:06:31 +00:00
runningBalance + = Amount (
rawValue: BigInt . from ( output . value ) ,
fractionDigits: coin . decimals ,
) ;
2023-01-11 19:30:03 +00:00
inputCount + + ;
2023-04-05 22:06:31 +00:00
if ( runningBalance > amount ) {
2023-01-11 19:30:03 +00:00
break ;
}
2022-09-26 20:32:53 +00:00
}
}
final oneOutPutFee = roughFeeEstimate ( inputCount , 1 , feeRate ) ;
final twoOutPutFee = roughFeeEstimate ( inputCount , 2 , feeRate ) ;
2023-04-05 22:06:31 +00:00
if ( runningBalance - amount > oneOutPutFee ) {
if ( runningBalance - amount > oneOutPutFee + DUST_LIMIT ) {
final change = runningBalance - amount - twoOutPutFee ;
2022-09-26 20:32:53 +00:00
if ( change > DUST_LIMIT & &
2023-04-05 22:06:31 +00:00
runningBalance - amount - change = = twoOutPutFee ) {
return runningBalance - amount - change ;
2022-09-26 20:32:53 +00:00
} else {
2023-04-05 22:06:31 +00:00
return runningBalance - amount ;
2022-09-26 20:32:53 +00:00
}
} else {
2023-04-05 22:06:31 +00:00
return runningBalance - amount ;
2022-09-26 20:32:53 +00:00
}
2023-04-05 22:06:31 +00:00
} else if ( runningBalance - amount = = oneOutPutFee ) {
2022-09-26 20:32:53 +00:00
return oneOutPutFee ;
} else {
return twoOutPutFee ;
}
}
// TODO: correct formula for bch?
2023-04-05 22:06:31 +00:00
Amount roughFeeEstimate ( int inputCount , int outputCount , int feeRatePerKB ) {
return Amount (
rawValue: BigInt . from ( ( ( 181 * inputCount ) + ( 34 * outputCount ) + 10 ) *
( feeRatePerKB / 1000 ) . ceil ( ) ) ,
fractionDigits: coin . decimals ,
) ;
2022-09-26 20:32:53 +00:00
}
2023-04-05 22:06:31 +00:00
Future < Amount > sweepAllEstimate ( int feeRate ) async {
2022-09-26 20:32:53 +00:00
int available = 0 ;
int inputCount = 0 ;
2023-01-11 19:30:03 +00:00
for ( final output in ( await utxos ) ) {
if ( ! output . isBlocked & &
output . isConfirmed ( storedChainHeight , MINIMUM_CONFIRMATIONS ) ) {
2022-09-26 20:32:53 +00:00
available + = output . value ;
inputCount + + ;
}
}
// transaction will only have 1 output minus the fee
final estimatedFee = roughFeeEstimate ( inputCount , 1 , feeRate ) ;
2023-04-05 22:06:31 +00:00
return Amount (
rawValue: BigInt . from ( available ) ,
fractionDigits: coin . decimals ,
) -
estimatedFee ;
2022-09-26 20:32:53 +00:00
}
@ override
Future < bool > generateNewAddress ( ) async {
try {
2023-01-11 19:30:03 +00:00
final currentReceiving = await _currentReceivingAddress ;
final newReceivingIndex = currentReceiving . derivationIndex + 1 ;
// Use new index to derive a new receiving address
2022-09-26 20:32:53 +00:00
final newReceivingAddress = await _generateAddressForChain (
2023-01-25 19:49:14 +00:00
0 , newReceivingIndex , DerivePathTypeExt . primaryFor ( coin ) ) ;
2023-01-11 19:30:03 +00:00
// Add that new receiving address
2023-01-16 21:04:03 +00:00
await db . putAddress ( newReceivingAddress ) ;
2022-09-26 20:32:53 +00:00
return true ;
} catch ( e , s ) {
Logging . instance . log (
" Exception rethrown from generateNewAddress(): $ e \n $ s " ,
level: LogLevel . Error ) ;
return false ;
}
}
2023-04-08 00:44:43 +00:00
@ override
Future < String > get xpub async {
final node = await Bip32Utils . getBip32Root (
( await mnemonic ) . join ( " " ) ,
await mnemonicPassphrase ? ? " " ,
_network ,
) ;
return node . neutered ( ) . toBase58 ( ) ;
}
2022-09-26 20:32:53 +00:00
}
// Bitcoincash Network
final bitcoincash = NetworkType (
messagePrefix: ' \x18 Bitcoin Signed Message: \n ' ,
bech32: ' bc ' ,
bip32: Bip32Type ( public: 0x0488b21e , private: 0x0488ade4 ) ,
pubKeyHash: 0x00 ,
scriptHash: 0x05 ,
wif: 0x80 ) ;
final bitcoincashtestnet = NetworkType (
messagePrefix: ' \x18 Bitcoin Signed Message: \n ' ,
bech32: ' tb ' ,
bip32: Bip32Type ( public: 0x043587cf , private: 0x04358394 ) ,
pubKeyHash: 0x6f ,
scriptHash: 0xc4 ,
wif: 0xef ) ;