deriveExtendedPrivateChildKey function

ExtendedPrivateKey deriveExtendedPrivateChildKey(
  1. ExtendedPrivateKey parent,
  2. int childNumber
)

CKDpriv in the specficiation

Implementation

ExtendedPrivateKey deriveExtendedPrivateChildKey(
    ExtendedPrivateKey parent, int childNumber) {
  var message = childNumber >= firstHardenedChild
      ? _derivePrivateMessage(parent, childNumber)
      : _derivePublicMessage(parent.publicKey(), childNumber);
  var hash = hmacSha512(parent.chainCode!, message);

  var leftSide = utils.decodeBigIntWithSign(1, _leftFrom(hash));
  if (leftSide >= curve.n) {
    throw KeyBiggerThanOrder();
  }

  var childPrivateKey = (leftSide + parent.key!) % curve.n;
  if (childPrivateKey == BigInt.zero) {
    throw KeyZero();
  }

  var chainCode = _rightFrom(hash);

  return ExtendedPrivateKey(
    key: childPrivateKey,
    chainCode: chainCode,
    childNumber: childNumber,
    depth: parent.depth! + 1,
    parentFingerprint: parent.fingerprint,
  );
}