BIP32HWallet.fromXpublicKey constructor

BIP32HWallet.fromXpublicKey(
  1. String xPublicKey, {
  2. CurrencySymbol currencySymbol = CurrencySymbol.btc,
  3. bool? forceRootKey,
})

Creates a BIP32 hierarchical wallet from an extended public key (xPub).

xPublicKey is the extended public key to create the wallet from. currencySymbol is the currency symbol (default is CurrencySymbol.btc). forceRootKey is a flag to force treating the key as a root key (optional).

Returns a BIP32 hierarchical wallet instance initialized with the provided extended public key.

Implementation

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

  /// Check if the provided xPublicKey is a valid root key.
  final check = isRootKey(xPublicKey, currency, isPublicKey: true);

  /// If forceRootKey is specified, verify that the key matches the expected type.
  if (forceRootKey != null) {
    if (check.$1 != forceRootKey) {
      throw ArgumentError(
          "The provided key is not a valid ${forceRootKey ? "rootPublicKey" : "publicKey"}");
    }
  }

  /// Decode the components of the extended public key.
  final decode = _decodeXKeys(check.$2, isPublic: true);

  /// Extract chain code, public key, index, and depth from the decoded components.
  final chain = decode[4];
  final publicKey = decode[5];
  final index = intFromBytes(decode[3], Endian.big);
  final deph = intFromBytes(decode[1], Endian.big);

  /// Create and return a BIP32 hierarchical wallet from the extracted components.
  return BIP32HWallet._fromPublicKey(
      public: publicKey,
      chainCode: chain,
      depth: deph,
      fingerPrint: decode[2],
      index: index);
}