childKey method
Derives a child key from the current key, based on the provided index.
This method derives a child key from the current key, either privately or publicly, depending on the input and key type. If the current key is private, it can derive both private and public child keys. If the current key is public, it can only derive public child keys.
index
: The index used to derive the child key.
If the current key is private:
- For non-hardened derivation, this method can derive both private and public child keys.
- For hardened derivation, it can only derive private child keys.
Returns a new key instance representing the derived child key. If public derivation is not supported or if there's an issue with the derivation process, an error is thrown.
Implementation
@override
Bip32Slip10Nist256p1 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 Bip32Slip10Nist256p1._(
keyData: Bip32KeyData(
chainCode: Bip32ChainCode(result.item2),
depth: depth.increase(),
index: index,
parentFingerPrint: fingerPrint,
),
keyNetVer: keyNetVersions,
privKey: result.item1,
pubKey: null);
}
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 Bip32Slip10Nist256p1._(
keyData: Bip32KeyData(
chainCode: Bip32ChainCode(result.item2),
depth: depth.increase(),
index: index,
parentFingerPrint: fingerPrint,
),
keyNetVer: keyNetVersions,
privKey: null,
pubKey: result.item1);
}