Skip to content

Commit

Permalink
Update network_address.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Oct 10, 2024
1 parent 7a82c69 commit d42a8be
Showing 1 changed file with 55 additions and 60 deletions.
115 changes: 55 additions & 60 deletions lib/src/bitcoin/address/network_address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,105 @@ part of 'package:bitcoin_base/src/bitcoin/address/address.dart';

/// An abstract class representing a forked address for a specific network.
abstract class BitcoinNetworkAddress<T extends BasedUtxoNetwork> {
const BitcoinNetworkAddress();
const BitcoinNetworkAddress._(
{required this.address,
required this.network,
required this.baseAddress});

/// The underlying Bitcoin base address.
abstract final BitcoinBaseAddress baseAddress;
final BitcoinBaseAddress baseAddress;

/// Converts the address to a string representation for the specified network [T].
String toAddress([T? network]) {
return network == null ? address : baseAddress.toAddress(network);
String toAddress([T? updateNetwork]) {
return updateNetwork == null
? address
: baseAddress.toAddress(updateNetwork);
}

/// The type of the Bitcoin address.
BitcoinAddressType get type => baseAddress.type;

/// The string representation of the address.
abstract final String address;
final String address;

final T network;
}

/// A concrete implementation of [BitcoinNetworkAddress] for Bitcoin network.
class BitcoinAddress extends BitcoinNetworkAddress<BitcoinNetwork> {
const BitcoinAddress._(this.baseAddress, this.address);
const BitcoinAddress._(
BitcoinBaseAddress baseAddress, String address, BitcoinNetwork network)
: super._(address: address, baseAddress: baseAddress, network: network);
factory BitcoinAddress(String address,
{BitcoinNetwork network = BitcoinNetwork.mainnet}) {
return BitcoinAddress._(
_BitcoinAddressUtils.decodeAddress(address, network), address);
_BitcoinAddressUtils.decodeAddress(address, network), address, network);
}
factory BitcoinAddress.fromBaseAddress(BitcoinBaseAddress address,
{DashNetwork network = DashNetwork.mainnet}) {
{BitcoinNetwork network = BitcoinNetwork.mainnet}) {
final baseAddress = _BitcoinAddressUtils.validateAddress(address, network);
return BitcoinAddress._(baseAddress, baseAddress.toAddress(network));
return BitcoinAddress._(
baseAddress, baseAddress.toAddress(network), network);
}
@override
final BitcoinBaseAddress baseAddress;
@override
final String address;
}

/// A concrete implementation of [BitcoinNetworkAddress] for Doge network.
class DogeAddress extends BitcoinNetworkAddress<DogecoinNetwork> {
const DogeAddress._(this.baseAddress, this.address);
const DogeAddress._(
BitcoinBaseAddress baseAddress, String address, DogecoinNetwork network)
: super._(address: address, baseAddress: baseAddress, network: network);
factory DogeAddress(String address,
{DogecoinNetwork network = DogecoinNetwork.mainnet}) {
return DogeAddress._(
_BitcoinAddressUtils.decodeAddress(address, network), address);
_BitcoinAddressUtils.decodeAddress(address, network), address, network);
}
factory DogeAddress.fromBaseAddress(BitcoinBaseAddress address,
{DogecoinNetwork network = DogecoinNetwork.mainnet}) {
final baseAddress = _BitcoinAddressUtils.validateAddress(address, network);
return DogeAddress._(baseAddress, baseAddress.toAddress(network));
return DogeAddress._(baseAddress, baseAddress.toAddress(network), network);
}
@override
final BitcoinBaseAddress baseAddress;

@override
final String address;
}

/// A concrete implementation of [BitcoinNetworkAddress] for Pepecoin network.
class PepeAddress extends BitcoinNetworkAddress<PepeNetwork> {
const PepeAddress._(this.baseAddress, this.address);
const PepeAddress._(
BitcoinBaseAddress baseAddress, String address, PepeNetwork network)
: super._(address: address, network: network, baseAddress: baseAddress);
factory PepeAddress(String address,
{PepeNetwork network = PepeNetwork.mainnet}) {
return PepeAddress._(
_BitcoinAddressUtils.decodeAddress(address, network), address);
_BitcoinAddressUtils.decodeAddress(address, network), address, network);
}
factory PepeAddress.fromBaseAddress(BitcoinBaseAddress address,
{PepeNetwork network = PepeNetwork.mainnet}) {
final baseAddress = _BitcoinAddressUtils.validateAddress(address, network);
return PepeAddress._(baseAddress, baseAddress.toAddress(network));
return PepeAddress._(baseAddress, baseAddress.toAddress(network), network);
}
@override
final BitcoinBaseAddress baseAddress;

@override
final String address;
}

/// A concrete implementation of [BitcoinNetworkAddress] for Litecoin network.
class LitecoinAddress extends BitcoinNetworkAddress<LitecoinNetwork> {
LitecoinAddress._(this.baseAddress, this.address);
const LitecoinAddress._(
BitcoinBaseAddress baseAddress, String address, LitecoinNetwork network)
: super._(address: address, baseAddress: baseAddress, network: network);
factory LitecoinAddress(String address,
{LitecoinNetwork network = LitecoinNetwork.mainnet}) {
return LitecoinAddress._(
_BitcoinAddressUtils.decodeAddress(address, network), address);
_BitcoinAddressUtils.decodeAddress(address, network), address, network);
}
factory LitecoinAddress.fromBaseAddress(BitcoinBaseAddress address,
{LitecoinNetwork network = LitecoinNetwork.mainnet}) {
final baseAddress = _BitcoinAddressUtils.validateAddress(address, network);
return LitecoinAddress._(baseAddress, baseAddress.toAddress(network));
return LitecoinAddress._(
baseAddress, baseAddress.toAddress(network), network);
}
@override
final BitcoinBaseAddress baseAddress;
@override
final String address;
}

/// A concrete implementation of [BitcoinNetworkAddress] for Bitcoin cash network.
class BitcoinCashAddress extends BitcoinNetworkAddress<BitcoinCashNetwork> {
const BitcoinCashAddress._(this.baseAddress, this.address);
const BitcoinCashAddress._(BitcoinBaseAddress baseAddress, String address,
BitcoinCashNetwork network)
: super._(address: address, baseAddress: baseAddress, network: network);
factory BitcoinCashAddress(String address,
{BitcoinCashNetwork network = BitcoinCashNetwork.mainnet,
bool validateNetworkPrefix = false}) {
Expand All @@ -109,61 +110,55 @@ class BitcoinCashAddress extends BitcoinNetworkAddress<BitcoinCashNetwork> {
if (decodeAddress == null) {
throw DartBitcoinPluginException("Invalid ${network.value} address.");
}
return BitcoinCashAddress._(decodeAddress, address);
return BitcoinCashAddress._(decodeAddress, address, network);
}
factory BitcoinCashAddress.fromBaseAddress(BitcoinBaseAddress address,
{BitcoinCashNetwork network = BitcoinCashNetwork.mainnet}) {
final baseAddress = _BitcoinAddressUtils.validateAddress(address, network);
return BitcoinCashAddress._(baseAddress, baseAddress.toAddress(network));
return BitcoinCashAddress._(
baseAddress, baseAddress.toAddress(network), network);
}
@override
final BitcoinBaseAddress baseAddress;
@override
final String address;

@override
String toAddress([BitcoinCashNetwork? network, String? prefix]) {
String toAddress([BitcoinCashNetwork? updateNetwork, String? prefix]) {
if (prefix != null) {
return BchAddrConverter.convert(address, prefix, null);
}
return super.toAddress(network);
return super.toAddress(updateNetwork);
}
}

/// A concrete implementation of [BitcoinNetworkAddress] for Dash network.
class DashAddress extends BitcoinNetworkAddress<DashNetwork> {
const DashAddress._(this.baseAddress, this.address);
const DashAddress._(
BitcoinBaseAddress baseAddress, String address, DashNetwork network)
: super._(address: address, baseAddress: baseAddress, network: network);
factory DashAddress(String address,
{DashNetwork network = DashNetwork.mainnet}) {
return DashAddress._(
_BitcoinAddressUtils.decodeAddress(address, network), address);
_BitcoinAddressUtils.decodeAddress(address, network), address, network);
}
factory DashAddress.fromBaseAddress(BitcoinBaseAddress address,
{DashNetwork network = DashNetwork.mainnet}) {
final baseAddress = _BitcoinAddressUtils.validateAddress(address, network);
return DashAddress._(baseAddress, baseAddress.toAddress(network));
return DashAddress._(baseAddress, baseAddress.toAddress(network), network);
}
@override
final BitcoinBaseAddress baseAddress;
@override
final String address;
}

/// A concrete implementation of [BitcoinNetworkAddress] for bitcoinSV network.
class BitcoinSVAddress extends BitcoinNetworkAddress<DashNetwork> {
const BitcoinSVAddress._(this.baseAddress, this.address);
class BitcoinSVAddress extends BitcoinNetworkAddress<BitcoinSVNetwork> {
const BitcoinSVAddress._(
BitcoinBaseAddress baseAddress, String address, BitcoinSVNetwork network)
: super._(address: address, baseAddress: baseAddress, network: network);
factory BitcoinSVAddress(String address,
{BitcoinSVNetwork network = BitcoinSVNetwork.mainnet}) {
return BitcoinSVAddress._(
_BitcoinAddressUtils.decodeAddress(address, network), address);
_BitcoinAddressUtils.decodeAddress(address, network), address, network);
}
factory BitcoinSVAddress.fromBaseAddress(BitcoinBaseAddress address,
{BitcoinSVNetwork network = BitcoinSVNetwork.mainnet}) {
final baseAddress = _BitcoinAddressUtils.validateAddress(address, network);
return BitcoinSVAddress._(baseAddress, baseAddress.toAddress(network));
return BitcoinSVAddress._(
baseAddress, baseAddress.toAddress(network), network);
}
@override
final BitcoinBaseAddress baseAddress;
@override
final String address;
}

0 comments on commit d42a8be

Please sign in to comment.