original_docstring: Child Key Derivation (CDK)
  
      Key derivation is normal if the extended parent key is public or
      child_index is less than 0x80000000.
  
      Key derivation is hardened if the extended parent key is private and
      child_index is not less than 0x80000000.
      
docstring: Child Key Derivation (CDK)
  Key derivation is normal if the extended parent key is public or
  child_index is less than 0x80000000.
  
  Key derivation is hardened if the extended parent key is private and
  child_index is not less than 0x80000000.
docstring_tokens: Child Key Derivation ( CDK ) Key derivation is normal if the extended parent key is public or child_index is less than 0x80000000 . Key derivation is hardened if the extended parent key is private and child_index is not less than 0x80000000 .
code: def ckd(xparentkey: Octets, index: Union[Octets, int]) -> bytes:
      """Child Key Derivation (CDK)
  
      Key derivation is normal if the extended parent key is public or
      child_index is less than 0x80000000.
  
      Key derivation is hardened if the extended parent key is private and
      child_index is not less than 0x80000000.
      """
  
      if isinstance(index, int):
          index = index.to_bytes(4, 'big')
      elif isinstance(index, str):  # hex string
          index = bytes.fromhex(index)
  
      if len(index) != 4:
          raise ValueError(f"a 4 bytes int is required, not {len(index)}")
  
      xparent = base58.decode_check(xparentkey, 78)
  
      version = xparent[:4]
  
      # serialization data
      xkey = version                               # version
      xkey += (xparent[4] + 1).to_bytes(1, 'big')  # (increased) depth
  
      if (version in PUB):
          if xparent[45] not in (2, 3):  # not a compressed public key
              raise ValueError("version/key mismatch in extended parent key")
          Parent_bytes = xparent[45:]
          Parent = point_from_octets(ec, Parent_bytes)
          xkey += h160(Parent_bytes)[:4]          # parent pubkey fingerprint
          if index[0] >= 0x80:
              raise ValueError("no private/hardened derivation from pubkey")
          xkey += index                            # child index
          parent_chain_code = xparent[13:45]       # normal derivation
          # actual extended key (key + chain code) derivation
          h = HMAC(parent_chain_code, Parent_bytes + index, sha512).digest()
          offset = int.from_bytes(h[:32], 'big')
          Offset = mult(ec, offset)
          Child = ec.add(Parent, Offset)
          Child_bytes = octets_from_point(ec, Child, True)
          xkey += h[32:]                            # chain code
          xkey += Child_bytes                       # public key
      elif (version in PRV):
          if xparent[45] != 0:    # not a private key
              raise ValueError("version/key mismatch in extended parent key")
          parent = int.from_bytes(xparent[46:], 'big')
          Parent = mult(ec, parent)
          Parent_bytes = octets_from_point(ec, Parent, True)
          xkey += h160(Parent_bytes)[:4]           # parent pubkey fingerprint
          xkey += index                             # child index
          # actual extended key (key + chain code) derivation
          parent_chain_code = xparent[13:45]
          if (index[0] < 0x80):                     # normal derivation
              h = HMAC(parent_chain_code, Parent_bytes + index, sha512).digest()
          else:                                     # hardened derivation
              h = HMAC(parent_chain_code, xparent[45:] + index, sha512).digest()
          offset = int.from_bytes(h[:32], 'big')
          child = (parent + offset) % ec.n
          child_bytes = b'\x00' + child.to_bytes(32, 'big')
          xkey += h[32:]                            # chain code
          xkey += child_bytes                       # private key
      else:
          raise ValueError("invalid extended key version")
  
      return base58.encode_check(xkey)
code_tokens: def ckd ( xparentkey : Octets , index : Union [ Octets , int ] ) -> bytes : if isinstance ( index , int ) : index = index . to_bytes ( 4 , 'big' ) elif isinstance ( index , str ) : index = bytes . fromhex ( index ) if len ( index ) != 4 : raise ValueError ( f"a 4 bytes int is required, not {len(index)}" ) xparent = base58 . decode_check ( xparentkey , 78 ) version = xparent [ : 4 ] xkey = version xkey += ( xparent [ 4 ] + 1 ) . to_bytes ( 1 , 'big' ) if ( version in PUB ) : if xparent [ 45 ] not in ( 2 , 3 ) : raise ValueError ( "version/key mismatch in extended parent key" ) Parent_bytes = xparent [ 45 : ] Parent = point_from_octets ( ec , Parent_bytes ) xkey += h160 ( Parent_bytes ) [ : 4 ] if index [ 0 ] >= 0x80 : raise ValueError ( "no private/hardened derivation from pubkey" ) xkey += index parent_chain_code = xparent [ 13 : 45 ] h = HMAC ( parent_chain_code , Parent_bytes + index , sha512 ) . digest ( ) offset = int . from_bytes ( h [ : 32 ] , 'big' ) Offset = mult ( ec , offset ) Child = ec . add ( Parent , Offset ) Child_bytes = octets_from_point ( ec , Child , True ) xkey += h [ 32 : ] xkey += Child_bytes elif ( version in PRV ) : if xparent [ 45 ] != 0 : raise ValueError ( "version/key mismatch in extended parent key" ) parent = int . from_bytes ( xparent [ 46 : ] , 'big' ) Parent = mult ( ec , parent ) Parent_bytes = octets_from_point ( ec , Parent , True ) xkey += h160 ( Parent_bytes ) [ : 4 ] xkey += index parent_chain_code = xparent [ 13 : 45 ] if ( index [ 0 ] < 0x80 ) : h = HMAC ( parent_chain_code , Parent_bytes + index , sha512 ) . digest ( ) else : h = HMAC ( parent_chain_code , xparent [ 45 : ] + index , sha512 ) . digest ( ) offset = int . from_bytes ( h [ : 32 ] , 'big' ) child = ( parent + offset ) % ec . n child_bytes = b'\x00' + child . to_bytes ( 32 , 'big' ) xkey += h [ 32 : ] xkey += child_bytes else : raise ValueError ( "invalid extended key version" ) return base58 . encode_check ( xkey )
short_docstring: Child Key Derivation (CDK)
  Key derivation is normal if the extended parent key is public or
  child_index is less than 0x80000000.
short_docstring_tokens: Child Key Derivation ( CDK ) Key derivation is normal if the extended parent key is public or child_index is less than 0x80000000 .
comment: """Child Key Derivation (CDK)
  
      Key derivation is normal if the extended parent key is public or
      child_index is less than 0x80000000.
  
      Key derivation is hardened if the extended parent key is private and
      child_index is not less than 0x80000000.
      """$SEP$# hex string$SEP$# serialization data$SEP$# version$SEP$# (increased) depth$SEP$# not a compressed public key$SEP$# parent pubkey fingerprint$SEP$# child index$SEP$# normal derivation$SEP$# actual extended key (key + chain code) derivation$SEP$# chain code$SEP$# public key$SEP$# not a private key$SEP$# parent pubkey fingerprint$SEP$# child index$SEP$# actual extended key (key + chain code) derivation$SEP$# normal derivation$SEP$# hardened derivation$SEP$# chain code$SEP$# private key