childKey method

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

Generates a child key based on the given index.

Parameters:

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

Throws:

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

Implementation

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

  if (!isPublic) {
    if (!index.isHardened && !isPublicDerivationSupported) {
      throw Bip32KeyError.notHardenedIndexNotSupported;
    }
    assert(!isPublicOnly);
    final result = keyDerivator.deriveFromSecret(
      parent: privateKey,
      ctx: publicKey,
      index: index,
      type: curveType,
    );

    return Bip32KholawEd25519._(
      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 Bip32KeyError.publicDerivationNotSupported;
  }
  if (index.isHardened) {
    throw Bip32KeyError.publicHardenedIndexNotSupported;
  }
  final result = keyDerivator.deriveFromPublic(
    parent: publicKey,
    index: index,
    type: curveType,
  );
  return Bip32KholawEd25519._(
    keyData: Bip32KeyData(
      chainCode: result.chainCode,
      depth: depth.increase(),
      index: index,
      fingerPrint: fingerPrint,
    ),
    keyNetVer: keyNetVersions,
    privKey: null,
    pubKey: result.key,
  );
}