childKey method

  1. @override
CardanoIcarusBip32 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
CardanoIcarusBip32 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.deriveFromSecret(
      parent: privateKey,
      ctx: publicKey,
      index: index,
      type: curveType,
    );

    return CardanoIcarusBip32._(
      keyData: Bip32KeyData(
        chainCode: result.chainCode,
        depth: depth.increase(),
        index: index,
        fingerPrint: fingerPrint,
      ),
      keyNetVer: keyNetVersions,
      privKey: result.key,
      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.deriveFromPublic(
    parent: publicKey,
    index: index,
    type: curveType,
  );
  return CardanoIcarusBip32._(
    keyData: Bip32KeyData(
      chainCode: result.chainCode,
      depth: depth.increase(),
      index: index,
      fingerPrint: fingerPrint,
    ),
    keyNetVer: keyNetVersions,
    privKey: null,
    pubKey: result.key,
  );
}