toXpriveKey method
String
toXpriveKey({
- ExtendedKeyType semantic = ExtendedKeyType.p2pkh,
- CurrencySymbol currencySymbol = CurrencySymbol.btc,
Generates an extended private key (xPrv) string for this hierarchical wallet.
semantic
is the semantic version for the extended private key (default is ExtendedKeyType.p2pkh).
currencySymbol
is the currency symbol (default is CurrencySymbol.btc).
Returns an extended private key string based on the provided semantic version and currency.
Throws ArgumentError if attempting to access private key from a public key wallet. Throws ArgumentError if the network does not support the specified semantic version.
Implementation
String toXpriveKey(
{ExtendedKeyType semantic = ExtendedKeyType.p2pkh,
CurrencySymbol currencySymbol = CurrencySymbol.btc}) {
/// Check if the wallet is derived from an extended public key (xPub).
if (_fromXpub) {
throw ArgumentError("Cannot access private key from a publicKey wallet");
}
/// Get the cryptocurrency configuration based on the currency symbol.
final n = Cryptocurrency.fromSymbol(currencySymbol);
/// Retrieve the semantic version for the extended private key.
final versionHex = n.extendedPrivateKey.getExtended(semantic);
/// Ensure that the network supports the specified semantic version.
if (versionHex == null) {
throw ArgumentError("Network does not support this semantic version");
}
/// Convert the version to bytes.
final version = hexToBytes(versionHex);
/// Create bytes for depth, fingerprint, index, and private key data.
final depthBytes = Uint8List.fromList([depth]);
final fingerPrintBytes = _fingerPrint;
final indexBytes = packUint32BE(index);
final data = Uint8List.fromList([0, ..._private]);
/// Prepend 0 to indicate private key.
/// Combine all bytes to create the extended private key.
final result = Uint8List.fromList([
...version,
...depthBytes,
...fingerPrintBytes,
...indexBytes,
..._chainCode,
...data
]);
/// Encode and return the extended private key as a string with a checksum.
final check = bs.encodeCheck(result);
return check;
}