toXpublicKey method
String
toXpublicKey({
- ExtendedKeyType semantic = ExtendedKeyType.p2pkh,
- CurrencySymbol currencySymbol = CurrencySymbol.btc,
Converts the BIP32 hierarchical wallet to its extended public key representation.
semantic
specifies the extended key type (default is ExtendedKeyType.p2pkh).
currencySymbol
is the currency symbol (default is CurrencySymbol.btc).
Returns the extended public key as a string.
Implementation
String toXpublicKey(
{ExtendedKeyType semantic = ExtendedKeyType.p2pkh,
CurrencySymbol currencySymbol = CurrencySymbol.btc}) {
/// Get the cryptocurrency configuration based on the currency symbol.
final currency = Cryptocurrency.fromSymbol(currencySymbol);
/// Retrieve the semantic version hex for the given extended key type.
final versionHex = currency.extendedPublicKey.getExtended(semantic);
/// Check if the network supports the specified semantic version.
if (versionHex == null) {
throw ArgumentError("Network does not support this semantic version");
}
/// Convert the version hex to bytes.
final version = hexToBytes(versionHex);
/// Convert depth, fingerprint, and index to bytes.
final depthBytes = Uint8List.fromList([depth]);
final fingerPrintBytes = _fingerPrint;
final indexBytes = packUint32BE(index);
/// Combine all key components into a single byte list.
final data = publicKey;
final result = Uint8List.fromList([
...version,
...depthBytes,
...fingerPrintBytes,
...indexBytes,
..._chainCode,
...data
]);
/// Encode the result into a base58 check-encoded string.
final check = bs.encodeCheck(result);
return check;
}