mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 09:47:37 +00:00
214 lines
6.1 KiB
Dart
214 lines
6.1 KiB
Dart
import 'package:coinlib_flutter/coinlib_flutter.dart' as coinlib;
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
|
import 'package:stackwallet/models/node_model.dart';
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
|
import 'package:stackwallet/utilities/default_nodes.dart';
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
|
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
|
|
import 'package:stackwallet/wallets/crypto_currency/interfaces/paynym_currency_interface.dart';
|
|
import 'package:stackwallet/wallets/crypto_currency/intermediate/bip39_hd_currency.dart';
|
|
|
|
class Bitcoin extends Bip39HDCurrency with PaynymCurrencyInterface {
|
|
Bitcoin(super.network) {
|
|
switch (network) {
|
|
case CryptoCurrencyNetwork.main:
|
|
coin = Coin.bitcoin;
|
|
case CryptoCurrencyNetwork.test:
|
|
coin = Coin.bitcoinTestNet;
|
|
default:
|
|
throw Exception("Unsupported network: $network");
|
|
}
|
|
}
|
|
|
|
@override
|
|
// change this to change the number of confirms a tx needs in order to show as confirmed
|
|
int get minConfirms => 1;
|
|
|
|
@override
|
|
bool get torSupport => true;
|
|
|
|
@override
|
|
List<DerivePathType> get supportedDerivationPathTypes => [
|
|
DerivePathType.bip44,
|
|
DerivePathType.bip49,
|
|
DerivePathType.bip84,
|
|
DerivePathType.bip86, // P2TR.
|
|
];
|
|
|
|
@override
|
|
String get genesisHash {
|
|
switch (network) {
|
|
case CryptoCurrencyNetwork.main:
|
|
return "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f";
|
|
case CryptoCurrencyNetwork.test:
|
|
return "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943";
|
|
default:
|
|
throw Exception("Unsupported network: $network");
|
|
}
|
|
}
|
|
|
|
@override
|
|
Amount get dustLimit => Amount(
|
|
rawValue: BigInt.from(294),
|
|
fractionDigits: fractionDigits,
|
|
);
|
|
|
|
@override
|
|
coinlib.Network get networkParams {
|
|
switch (network) {
|
|
case CryptoCurrencyNetwork.main:
|
|
return coinlib.Network(
|
|
wifPrefix: 0x80,
|
|
p2pkhPrefix: 0x00,
|
|
p2shPrefix: 0x05,
|
|
privHDPrefix: 0x0488ade4,
|
|
pubHDPrefix: 0x0488b21e,
|
|
bech32Hrp: "bc",
|
|
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
minFee: BigInt.from(1), // TODO [prio=high].
|
|
minOutput: dustLimit.raw, // TODO.
|
|
feePerKb: BigInt.from(1), // TODO.
|
|
);
|
|
case CryptoCurrencyNetwork.test:
|
|
return coinlib.Network(
|
|
wifPrefix: 0xef,
|
|
p2pkhPrefix: 0x6f,
|
|
p2shPrefix: 0xc4,
|
|
privHDPrefix: 0x04358394,
|
|
pubHDPrefix: 0x043587cf,
|
|
bech32Hrp: "tb",
|
|
messagePrefix: "\x18Bitcoin Signed Message:\n",
|
|
minFee: BigInt.from(1), // TODO [prio=high].
|
|
minOutput: dustLimit.raw, // TODO.
|
|
feePerKb: BigInt.from(1), // TODO.
|
|
);
|
|
default:
|
|
throw Exception("Unsupported network: $network");
|
|
}
|
|
}
|
|
|
|
@override
|
|
String constructDerivePath({
|
|
required DerivePathType derivePathType,
|
|
int account = 0,
|
|
required int chain,
|
|
required int index,
|
|
}) {
|
|
String coinType;
|
|
|
|
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!");
|
|
}
|
|
|
|
final int purpose;
|
|
switch (derivePathType) {
|
|
case DerivePathType.bip44:
|
|
purpose = 44;
|
|
break;
|
|
case DerivePathType.bip49:
|
|
purpose = 49;
|
|
break;
|
|
case DerivePathType.bip84:
|
|
purpose = 84;
|
|
break;
|
|
case DerivePathType.bip86:
|
|
purpose = 86;
|
|
break;
|
|
default:
|
|
throw Exception("DerivePathType $derivePathType not supported");
|
|
}
|
|
|
|
return "m/$purpose'/$coinType'/$account'/$chain/$index";
|
|
}
|
|
|
|
@override
|
|
({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,
|
|
);
|
|
|
|
return (address: addr, addressType: AddressType.p2pkh);
|
|
|
|
// TODO: [prio=high] verify this works similarly to bitcoindart's p2sh or something(!!)
|
|
case DerivePathType.bip49:
|
|
final p2wpkhScript = coinlib.P2WPKHAddress.fromPublicKey(
|
|
publicKey,
|
|
hrp: networkParams.bech32Hrp,
|
|
).program.script;
|
|
|
|
final addr = coinlib.P2SHAddress.fromRedeemScript(
|
|
p2wpkhScript,
|
|
version: networkParams.p2shPrefix,
|
|
);
|
|
|
|
return (address: addr, addressType: AddressType.p2sh);
|
|
|
|
case DerivePathType.bip84:
|
|
final addr = coinlib.P2WPKHAddress.fromPublicKey(
|
|
publicKey,
|
|
hrp: networkParams.bech32Hrp,
|
|
);
|
|
|
|
return (address: addr, addressType: AddressType.p2wpkh);
|
|
|
|
case DerivePathType.bip86:
|
|
final taproot = coinlib.Taproot(internalKey: publicKey);
|
|
|
|
final addr = coinlib.P2TRAddress.fromTaproot(
|
|
taproot,
|
|
hrp: networkParams.bech32Hrp,
|
|
);
|
|
|
|
return (address: addr, addressType: AddressType.p2tr);
|
|
|
|
default:
|
|
throw Exception("DerivePathType $derivePathType not supported");
|
|
}
|
|
}
|
|
|
|
@override
|
|
bool validateAddress(String address) {
|
|
try {
|
|
coinlib.Address.fromString(address, networkParams);
|
|
return true;
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
NodeModel get defaultNode {
|
|
switch (network) {
|
|
case CryptoCurrencyNetwork.main:
|
|
return DefaultNodes.bitcoin;
|
|
|
|
case CryptoCurrencyNetwork.test:
|
|
return DefaultNodes.bitcoinTestnet;
|
|
|
|
default:
|
|
throw UnimplementedError();
|
|
}
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) {
|
|
return other is Bitcoin && other.network == network;
|
|
}
|
|
|
|
@override
|
|
int get hashCode => Object.hash(Bitcoin, network);
|
|
}
|