BIP32HWallet.fromXPrivateKey constructor

BIP32HWallet.fromXPrivateKey(
  1. String xPrivateKey, {
  2. bool? foreRootKey,
  3. CurrencySymbol currencySymbol = CurrencySymbol.btc,
})

Creates a BIP32 hierarchical wallet from an extended private key.

xPrivateKey is the extended private key. foreRootKey is an optional flag indicating whether the key is expected to be a root key. currencySymbol is the currency symbol (default is CurrencySymbol.btc).

Returns a BIP32 hierarchical wallet instance.

Implementation

factory BIP32HWallet.fromXPrivateKey(String xPrivateKey,
    {bool? foreRootKey, CurrencySymbol currencySymbol = CurrencySymbol.btc}) {
  /// Get the cryptocurrency configuration based on the currency symbol.
  final currency = Cryptocurrency.fromSymbol(currencySymbol);

  /// Check if the key is a root key, and optionally verify if it's expected to be a root key.
  final check = isRootKey(xPrivateKey, currency);
  if (foreRootKey != null) {
    if (check.$1 != foreRootKey) {
      throw ArgumentError(
          "The key is not a valid ${foreRootKey ? "rootXPrivateKey" : "xPrivateKey"}");
    }
  }

  /// Decode the key to extract relevant information.
  final decode = _decodeXKeys(check.$2);
  final chain = decode[4];
  final private = decode[5];
  final index = intFromBytes(decode[3], Endian.big);
  final depth = intFromBytes(decode[1], Endian.big);

  /// Create a BIP32 hierarchical wallet from the decoded key components.
  return BIP32HWallet._fromPrivateKey(
      privateKey: private,
      chainCode: chain,
      depth: depth,
      fingerPrint: decode[2],
      index: index);
}