2023-10-30 16:33:49 +00:00
|
|
|
import 'package:coinlib_flutter/coinlib_flutter.dart' as coinlib;
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../../../models/isar/models/blockchain_data/address.dart';
|
|
|
|
import '../../../models/node_model.dart';
|
|
|
|
import '../../../utilities/amount/amount.dart';
|
|
|
|
import '../../../utilities/default_nodes.dart';
|
|
|
|
import '../../../utilities/enums/derive_path_type_enum.dart';
|
|
|
|
import '../crypto_currency.dart';
|
|
|
|
import '../interfaces/electrumx_currency_interface.dart';
|
|
|
|
import '../interfaces/paynym_currency_interface.dart';
|
|
|
|
import '../intermediate/bip39_hd_currency.dart';
|
2023-09-14 23:34:01 +00:00
|
|
|
|
2024-05-15 21:20:45 +00:00
|
|
|
class Bitcoin extends Bip39HDCurrency
|
|
|
|
with ElectrumXCurrencyInterface, PaynymCurrencyInterface {
|
2023-09-14 23:34:01 +00:00
|
|
|
Bitcoin(super.network) {
|
2024-05-15 21:20:45 +00:00
|
|
|
_idMain = "bitcoin";
|
|
|
|
_uriScheme = "bitcoin";
|
2023-09-14 23:34:01 +00:00
|
|
|
switch (network) {
|
|
|
|
case CryptoCurrencyNetwork.main:
|
2024-05-15 21:20:45 +00:00
|
|
|
_id = _idMain;
|
|
|
|
_name = "Bitcoin";
|
|
|
|
_ticker = "BTC";
|
2023-09-14 23:34:01 +00:00
|
|
|
case CryptoCurrencyNetwork.test:
|
2024-05-15 21:20:45 +00:00
|
|
|
_id = "bitcoinTestNet";
|
|
|
|
_name = "tBitcoin";
|
|
|
|
_ticker = "tBTC";
|
2023-09-14 23:34:01 +00:00
|
|
|
default:
|
|
|
|
throw Exception("Unsupported network: $network");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 21:20:45 +00:00
|
|
|
late final String _id;
|
|
|
|
@override
|
|
|
|
String get identifier => _id;
|
|
|
|
|
|
|
|
late final String _idMain;
|
|
|
|
@override
|
|
|
|
String get mainNetId => _idMain;
|
|
|
|
|
|
|
|
late final String _name;
|
|
|
|
@override
|
|
|
|
String get prettyName => _name;
|
|
|
|
|
|
|
|
late final String _uriScheme;
|
|
|
|
@override
|
|
|
|
String get uriScheme => _uriScheme;
|
|
|
|
|
|
|
|
late final String _ticker;
|
|
|
|
@override
|
|
|
|
String get ticker => _ticker;
|
|
|
|
|
2023-11-16 21:30:01 +00:00
|
|
|
@override
|
|
|
|
// change this to change the number of confirms a tx needs in order to show as confirmed
|
|
|
|
int get minConfirms => 1;
|
|
|
|
|
2024-04-16 22:42:51 +00:00
|
|
|
@override
|
|
|
|
bool get torSupport => true;
|
|
|
|
|
2023-11-07 18:19:42 +00:00
|
|
|
@override
|
|
|
|
List<DerivePathType> get supportedDerivationPathTypes => [
|
|
|
|
DerivePathType.bip44,
|
2024-01-11 19:36:53 +00:00
|
|
|
DerivePathType.bip49,
|
2023-11-07 18:19:42 +00:00
|
|
|
DerivePathType.bip84,
|
2024-03-26 04:03:00 +00:00
|
|
|
DerivePathType.bip86, // P2TR.
|
2023-11-07 18:19:42 +00:00
|
|
|
];
|
|
|
|
|
2023-10-30 17:41:52 +00:00
|
|
|
@override
|
|
|
|
String get genesisHash {
|
|
|
|
switch (network) {
|
|
|
|
case CryptoCurrencyNetwork.main:
|
|
|
|
return "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f";
|
|
|
|
case CryptoCurrencyNetwork.test:
|
|
|
|
return "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943";
|
|
|
|
default:
|
|
|
|
throw Exception("Unsupported network: $network");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 21:28:31 +00:00
|
|
|
@override
|
|
|
|
Amount get dustLimit => Amount(
|
|
|
|
rawValue: BigInt.from(294),
|
|
|
|
fractionDigits: fractionDigits,
|
|
|
|
);
|
|
|
|
|
2023-09-14 23:34:01 +00:00
|
|
|
@override
|
2024-03-22 21:19:48 +00:00
|
|
|
coinlib.Network get networkParams {
|
2023-09-14 23:34:01 +00:00
|
|
|
switch (network) {
|
|
|
|
case CryptoCurrencyNetwork.main:
|
2024-03-22 21:19:48 +00:00
|
|
|
return coinlib.Network(
|
2023-09-18 21:28:31 +00:00
|
|
|
wifPrefix: 0x80,
|
|
|
|
p2pkhPrefix: 0x00,
|
|
|
|
p2shPrefix: 0x05,
|
|
|
|
privHDPrefix: 0x0488ade4,
|
|
|
|
pubHDPrefix: 0x0488b21e,
|
|
|
|
bech32Hrp: "bc",
|
|
|
|
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
2024-03-22 21:19:48 +00:00
|
|
|
minFee: BigInt.from(1), // TODO [prio=high].
|
2024-04-15 19:22:30 +00:00
|
|
|
minOutput: dustLimit.raw, // TODO.
|
2024-03-22 21:19:48 +00:00
|
|
|
feePerKb: BigInt.from(1), // TODO.
|
2023-09-18 21:28:31 +00:00
|
|
|
);
|
2023-09-14 23:34:01 +00:00
|
|
|
case CryptoCurrencyNetwork.test:
|
2024-03-22 21:19:48 +00:00
|
|
|
return coinlib.Network(
|
2023-09-18 21:28:31 +00:00
|
|
|
wifPrefix: 0xef,
|
|
|
|
p2pkhPrefix: 0x6f,
|
|
|
|
p2shPrefix: 0xc4,
|
|
|
|
privHDPrefix: 0x04358394,
|
|
|
|
pubHDPrefix: 0x043587cf,
|
|
|
|
bech32Hrp: "tb",
|
|
|
|
messagePrefix: "\x18Bitcoin Signed Message:\n",
|
2024-03-22 21:19:48 +00:00
|
|
|
minFee: BigInt.from(1), // TODO [prio=high].
|
2024-04-15 19:22:30 +00:00
|
|
|
minOutput: dustLimit.raw, // TODO.
|
2024-03-22 21:19:48 +00:00
|
|
|
feePerKb: BigInt.from(1), // TODO.
|
2023-09-18 21:28:31 +00:00
|
|
|
);
|
2023-09-14 23:34:01 +00:00
|
|
|
default:
|
|
|
|
throw Exception("Unsupported network: $network");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String constructDerivePath({
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
int account = 0,
|
|
|
|
required int chain,
|
|
|
|
required int index,
|
|
|
|
}) {
|
|
|
|
String coinType;
|
|
|
|
|
2023-09-18 21:28:31 +00:00
|
|
|
switch (networkParams.wifPrefix) {
|
|
|
|
case 0x80:
|
|
|
|
coinType = "0"; // btc mainnet
|
|
|
|
break;
|
|
|
|
case 0xef:
|
|
|
|
coinType = "1"; // btc testnet
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Exception("Invalid Bitcoin network wif used!");
|
2023-09-14 23:34:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 21:30:01 +00:00
|
|
|
final int purpose;
|
2023-09-14 23:34:01 +00:00
|
|
|
switch (derivePathType) {
|
|
|
|
case DerivePathType.bip44:
|
|
|
|
purpose = 44;
|
|
|
|
break;
|
2024-01-11 19:36:53 +00:00
|
|
|
case DerivePathType.bip49:
|
|
|
|
purpose = 49;
|
|
|
|
break;
|
2023-09-14 23:34:01 +00:00
|
|
|
case DerivePathType.bip84:
|
|
|
|
purpose = 84;
|
|
|
|
break;
|
2024-03-26 04:03:00 +00:00
|
|
|
case DerivePathType.bip86:
|
|
|
|
purpose = 86;
|
|
|
|
break;
|
2023-09-14 23:34:01 +00:00
|
|
|
default:
|
|
|
|
throw Exception("DerivePathType $derivePathType not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
return "m/$purpose'/$coinType'/$account'/$chain/$index";
|
|
|
|
}
|
|
|
|
|
2023-09-18 21:28:31 +00:00
|
|
|
@override
|
2023-09-14 23:34:01 +00:00
|
|
|
({coinlib.Address address, AddressType addressType}) getAddressForPublicKey({
|
|
|
|
required coinlib.ECPublicKey publicKey,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
}) {
|
|
|
|
switch (derivePathType) {
|
|
|
|
case DerivePathType.bip44:
|
|
|
|
final addr = coinlib.P2PKHAddress.fromPublicKey(
|
|
|
|
publicKey,
|
|
|
|
version: networkParams.p2pkhPrefix,
|
|
|
|
);
|
|
|
|
|
2023-11-06 21:55:49 +00:00
|
|
|
return (address: addr, addressType: AddressType.p2pkh);
|
2023-10-30 17:41:32 +00:00
|
|
|
|
2024-04-15 19:22:30 +00:00
|
|
|
// TODO: [prio=high] verify this works similarly to bitcoindart's p2sh or something(!!)
|
2024-01-11 19:36:53 +00:00
|
|
|
case DerivePathType.bip49:
|
|
|
|
final p2wpkhScript = coinlib.P2WPKHAddress.fromPublicKey(
|
|
|
|
publicKey,
|
|
|
|
hrp: networkParams.bech32Hrp,
|
|
|
|
).program.script;
|
|
|
|
|
2024-03-22 21:19:57 +00:00
|
|
|
final addr = coinlib.P2SHAddress.fromRedeemScript(
|
2024-01-11 19:36:53 +00:00
|
|
|
p2wpkhScript,
|
|
|
|
version: networkParams.p2shPrefix,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (address: addr, addressType: AddressType.p2sh);
|
2023-09-14 23:34:01 +00:00
|
|
|
|
|
|
|
case DerivePathType.bip84:
|
|
|
|
final addr = coinlib.P2WPKHAddress.fromPublicKey(
|
|
|
|
publicKey,
|
|
|
|
hrp: networkParams.bech32Hrp,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (address: addr, addressType: AddressType.p2wpkh);
|
2023-10-30 17:41:32 +00:00
|
|
|
|
2024-03-26 04:03:00 +00:00
|
|
|
case DerivePathType.bip86:
|
|
|
|
final taproot = coinlib.Taproot(internalKey: publicKey);
|
|
|
|
|
|
|
|
final addr = coinlib.P2TRAddress.fromTaproot(
|
|
|
|
taproot,
|
2024-03-26 18:15:37 +00:00
|
|
|
hrp: networkParams.bech32Hrp,
|
2024-03-26 04:03:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (address: addr, addressType: AddressType.p2tr);
|
|
|
|
|
2023-09-14 23:34:01 +00:00
|
|
|
default:
|
|
|
|
throw Exception("DerivePathType $derivePathType not supported");
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 21:28:31 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool validateAddress(String address) {
|
2023-10-30 17:42:57 +00:00
|
|
|
try {
|
|
|
|
coinlib.Address.fromString(address, networkParams);
|
|
|
|
return true;
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-18 21:28:31 +00:00
|
|
|
}
|
2023-11-17 01:44:17 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
NodeModel get defaultNode {
|
|
|
|
switch (network) {
|
|
|
|
case CryptoCurrencyNetwork.main:
|
2024-05-15 21:20:45 +00:00
|
|
|
return NodeModel(
|
|
|
|
host: "bitcoin.stackwallet.com",
|
|
|
|
port: 50002,
|
|
|
|
name: DefaultNodes.defaultName,
|
|
|
|
id: DefaultNodes.buildId(this),
|
|
|
|
useSSL: true,
|
|
|
|
enabled: true,
|
|
|
|
coinName: identifier,
|
|
|
|
isFailover: true,
|
|
|
|
isDown: false,
|
|
|
|
);
|
2023-11-17 01:44:17 +00:00
|
|
|
|
|
|
|
case CryptoCurrencyNetwork.test:
|
2024-05-15 21:20:45 +00:00
|
|
|
return NodeModel(
|
|
|
|
host: "bitcoin-testnet.stackwallet.com",
|
|
|
|
port: 51002,
|
|
|
|
name: DefaultNodes.defaultName,
|
|
|
|
id: DefaultNodes.buildId(this),
|
|
|
|
useSSL: true,
|
|
|
|
enabled: true,
|
|
|
|
coinName: identifier,
|
|
|
|
isFailover: true,
|
|
|
|
isDown: false,
|
|
|
|
);
|
2023-11-17 01:44:17 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 18:04:10 +00:00
|
|
|
|
|
|
|
@override
|
2024-05-15 21:20:45 +00:00
|
|
|
int get defaultSeedPhraseLength => 12;
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get fractionDigits => 8;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get hasBuySupport => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get hasMnemonicPassphraseSupport => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<int> get possibleMnemonicLengths => [defaultSeedPhraseLength, 24];
|
|
|
|
|
|
|
|
@override
|
|
|
|
AddressType get primaryAddressType => AddressType.p2wpkh;
|
|
|
|
|
|
|
|
@override
|
|
|
|
BigInt get satsPerCoin => BigInt.from(100000000);
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get targetBlockTimeSeconds => 600;
|
2024-04-17 18:04:10 +00:00
|
|
|
|
|
|
|
@override
|
2024-05-15 21:20:45 +00:00
|
|
|
DerivePathType get primaryDerivePathType => DerivePathType.bip84;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Uri defaultBlockExplorer(String txid) {
|
|
|
|
switch (network) {
|
|
|
|
case CryptoCurrencyNetwork.main:
|
|
|
|
return Uri.parse("https://mempool.space/tx/$txid");
|
|
|
|
case CryptoCurrencyNetwork.test:
|
|
|
|
return Uri.parse("https://mempool.space/testnet/tx/$txid");
|
|
|
|
default:
|
|
|
|
throw Exception(
|
|
|
|
"Unsupported network for defaultBlockExplorer(): $network",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 23:34:01 +00:00
|
|
|
}
|