fix: bch remove bad address type

This commit is contained in:
julian 2023-05-15 07:48:50 -06:00
parent 3e51624631
commit 127564f2ac
3 changed files with 33 additions and 59 deletions

View file

@ -71,7 +71,6 @@ String constructDerivePath({
case 0x80: // bch mainnet wif
switch (derivePathType) {
case DerivePathType.bip44:
case DerivePathType.bip49:
coinType = 145; // bch mainnet
break;
case DerivePathType.bch44: // bitcoin.com wallet specific
@ -95,9 +94,6 @@ String constructDerivePath({
case DerivePathType.bch44:
purpose = 44;
break;
case DerivePathType.bip49:
purpose = 49;
break;
default:
throw Exception("DerivePathType $derivePathType not supported");
}
@ -283,10 +279,6 @@ class BitcoinCashWallet extends CoinServiceAPI
return DerivePathType.bip44;
}
if (decodeBase58[0] == _network.scriptHash) {
// P2SH
return DerivePathType.bip49;
}
throw ArgumentError('Invalid version or Network mismatch');
} else {
try {
@ -419,15 +411,6 @@ class BitcoinCashWallet extends CoinServiceAPI
addrType = isar_models.AddressType.p2pkh;
addressString = bitbox.Address.toCashAddress(addressString);
break;
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:
throw Exception("DerivePathType $type not supported");
}
@ -518,7 +501,6 @@ class BitcoinCashWallet extends CoinServiceAPI
final deriveTypes = [
DerivePathType.bip44,
DerivePathType.bip49,
];
if (coin != Coin.bitcoincashTestnet) {
@ -1397,10 +1379,6 @@ class BitcoinCashWallet extends CoinServiceAPI
// P2PKH
_generateAddressForChain(0, 0, DerivePathType.bip44),
_generateAddressForChain(1, 0, DerivePathType.bip44),
// P2SH
_generateAddressForChain(0, 0, DerivePathType.bip49),
_generateAddressForChain(1, 0, DerivePathType.bip49),
]);
await db.putAddresses(initialAddresses);
@ -1408,7 +1386,7 @@ class BitcoinCashWallet extends CoinServiceAPI
Logging.instance.log("_generateNewWalletFinished", level: LogLevel.Info);
}
/// Generates a new internal or external chain address for the wallet using a BIP44 or BIP49 derivation path.
/// Generates a new internal or external chain address for the wallet using a BIP44 derivation path.
/// [chain] - Use 0 for receiving (external), 1 for change (internal). Should not be any other value!
/// [index] - This can be any integer >= 0
Future<isar_models.Address> _generateAddressForChain(
@ -1449,17 +1427,6 @@ class BitcoinCashWallet extends CoinServiceAPI
addrType = isar_models.AddressType.p2pkh;
address = bitbox.Address.toCashAddress(address);
break;
case DerivePathType.bip49:
address = P2SH(
data: PaymentData(
redeem: P2WPKH(data: data, network: _network).data),
network: _network)
.data
.address!;
addrType = isar_models.AddressType.p2sh;
break;
case DerivePathType.bip84:
throw UnsupportedError("bip84 not supported by BCH");
default:
throw Exception("DerivePathType $derivePathType not supported");
}
@ -1502,13 +1469,6 @@ class BitcoinCashWallet extends CoinServiceAPI
coinType = coin == Coin.bitcoincash ? "0" : "1";
purpose = "44";
break;
case DerivePathType.bip49:
type = isar_models.AddressType.p2sh;
coinType = coin == Coin.bitcoincash ? "145" : "1";
purpose = "49";
break;
case DerivePathType.bip84:
throw UnsupportedError("bip84 not supported by BCH");
default:
throw Exception("DerivePathType $derivePathType not supported");
}
@ -1537,9 +1497,6 @@ class BitcoinCashWallet extends CoinServiceAPI
case DerivePathType.bch44:
key = "${walletId}_${chainId}DerivationsBch44P2PKH";
break;
case DerivePathType.bip49:
key = "${walletId}_${chainId}DerivationsP2SH";
break;
default:
throw UnsupportedError(
"${derivePathType.name} not supported by ${coin.prettyName}");
@ -2721,20 +2678,6 @@ class BitcoinCashWallet extends CoinServiceAPI
redeemScript = null;
break;
case DerivePathType.bip49:
final p2wpkh = P2WPKH(
data: PaymentData(
pubkey: Format.stringToUint8List(pubKey),
),
network: _network,
).data;
redeemScript = p2wpkh.output;
data = P2SH(
data: PaymentData(redeem: p2wpkh),
network: _network,
).data;
break;
default:
throw Exception("DerivePathType unsupported");
}

View file

@ -42,7 +42,7 @@ abstract class Constants {
// Enable Logger.print statements
static const bool disableLogger = false;
static const int currentHiveDbVersion = 8;
static const int currentHiveDbVersion = 9;
static const int rescanV1 = 1;

View file

@ -290,6 +290,37 @@ class DbVersionMigrator with WalletDB {
// try to continue migrating
return await migrate(8, secureStore: secureStore);
case 8:
// migrate
await Hive.openBox<dynamic>(DB.boxNameAllWalletsData);
final walletsService =
WalletsService(secureStorageInterface: secureStore);
final walletInfoList = await walletsService.walletNames;
await MainDB.instance.initMainDB();
for (final walletId in walletInfoList.keys) {
final info = walletInfoList[walletId]!;
if (info.coin == Coin.bitcoincash ||
info.coin == Coin.bitcoincashTestnet) {
final ids = await MainDB.instance
.getAddresses(walletId)
.filter()
.typeEqualTo(isar_models.AddressType.p2sh)
.idProperty()
.findAll();
await MainDB.instance.isar.writeTxn(() async {
await MainDB.instance.isar.addresses.deleteAll(ids);
});
}
}
// update version
await DB.instance.put<dynamic>(
boxName: DB.boxNameDBInfo, key: "hive_data_version", value: 9);
// try to continue migrating
return await migrate(9, secureStore: secureStore);
default:
// finally return
return;