childKey method
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:
- A new CardanoIcarusBip32 instance representing the child key.
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.ckdPriv(privateKey, publicKey, index, curveType);
return CardanoIcarusBip32._(
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 CardanoIcarusBip32._(
keyData: Bip32KeyData(
chainCode: Bip32ChainCode(result.item2),
depth: depth.increase(),
index: index,
parentFingerPrint: fingerPrint,
),
keyNetVer: keyNetVersions,
privKey: null,
pubKey: result.item1);
}