childKey method

  1. @override
Bip32KholawEd25519 childKey(
  1. Bip32KeyIndex index
)
override

Generates a child key based on the given index.

This method derives a child key from the current Bip32 key. It can be either a private or public child derivation based on the current key type.

Parameters:

  • index: The index of the child key to generate.

Returns:

Throws:

  • Bip32KeyError: If private child derivation with a non-hardened index is not supported.

Note: This method is used to create child keys from the current key, both for private and public keys, and is determined by the key type (public or private).

Implementation

@override
Bip32KholawEd25519 childKey(Bip32KeyIndex index) {
  final isPublic = isPublicOnly;

  if (!isPublic) {
    if (!index.isHardened && !isPublicDerivationSupported) {
      throw const Bip32KeyError(
          'Private child derivation with not-hardened index is not supported');
    }
    assert(!isPublicOnly);
    final result =
        keyDerivator.ckdPriv(privateKey, publicKey, index, curveType);

    return Bip32KholawEd25519._(
        keyData: Bip32KeyData(
          chainCode: Bip32ChainCode(result.item2),
          depth: depth.increase(),
          index: index,
          parentFingerPrint: fingerPrint,
        ),
        keyNetVer: keyNetVersions,
        privKey: result.item1,
        pubKey: null);
  }

  // Check if supported
  if (!isPublicDerivationSupported) {
    throw const Bip32KeyError('Public child derivation is not supported');
  }
  if (index.isHardened) {
    throw const Bip32KeyError(
        "Public child derivation cannot be used to create an hardened child key");
  }
  final result = keyDerivator.ckdPub(publicKey, index, curveType);
  return Bip32KholawEd25519._(
      keyData: Bip32KeyData(
        chainCode: Bip32ChainCode(result.item2),
        depth: depth.increase(),
        index: index,
        parentFingerPrint: fingerPrint,
      ),
      keyNetVer: keyNetVersions,
      privKey: null,
      pubKey: result.item1);
}