deterministic.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. """
  2. Generate Heirarchical Deterministic Wallets (HDWallet).
  3. Partially implements the BIP-0032, BIP-0043, and BIP-0044 specifications:
  4. * BIP-0032: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
  5. * BIP-0043: https://github.com/bitcoin/bips/blob/master/bip-0043.mediawiki
  6. * BIP-0044: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
  7. Skips serialization and public key derivation as unnecssary for this library's purposes.
  8. Notes
  9. -----
  10. * Integers are modulo the order of the curve (referred to as n).
  11. * Addition (+) of two coordinate pair is defined as application of
  12. the EC group operation.
  13. * Concatenation (||) is the operation of appending one byte sequence onto another.
  14. Definitions
  15. -----------
  16. * point(p): returns the coordinate pair resulting from EC point multiplication
  17. (repeated application of the EC group operation) of the secp256k1 base point
  18. with the integer p.
  19. * ser_32(i): serialize a 32-bit unsigned integer i as a 4-byte sequence,
  20. most significant byte first.
  21. * ser_256(p): serializes the integer p as a 32-byte sequence, most significant
  22. byte first.
  23. * ser_P(P): serializes the coordinate pair P = (x,y) as a byte sequence using SEC1's
  24. compressed form: (0x02 or 0x03) || ser_256(x), where the header byte depends on the
  25. parity of the omitted y coordinate.
  26. * parse_256(p): interprets a 32-byte sequence as a 256-bit number, most significant
  27. byte first.
  28. """ # blocklint: URL pragma
  29. # Additional notes:
  30. # - This module currently only implements private parent key => private child key
  31. # CKD function, as it is not necessary to the HD key derivation functions used
  32. # in this library to implement the other functions yet (as this module is only
  33. # used for derivation of private keys). That could change, but wasn't deemed
  34. # necessary at the time this module was introduced.
  35. # - Unlike other libraries, this library does not use Bitcoin key serialization,
  36. # because it is not intended to be ultimately used for Bitcoin key derivations.
  37. # This presents a simplified API, and no expectation is given for `xpub/xpriv`
  38. # key derivation.
  39. from typing import (
  40. Tuple,
  41. Type,
  42. Union,
  43. )
  44. from eth_utils import (
  45. ValidationError,
  46. to_int,
  47. )
  48. from ._utils import (
  49. SECP256K1_N,
  50. ec_point,
  51. hmac_sha512,
  52. )
  53. BASE_NODE_IDENTIFIERS = {"m", "M"}
  54. HARD_NODE_SUFFIXES = {"'", "H"}
  55. class Node(int):
  56. """
  57. A base node class.
  58. """
  59. TAG = "" # No tag
  60. OFFSET = 0x0 # No offset
  61. index: int
  62. def __new__(cls, index: int) -> "Node":
  63. if 0 > index or index > 2**31:
  64. raise ValidationError(f"{cls} cannot be initialized with value {index}")
  65. obj = int.__new__(cls, index + cls.OFFSET)
  66. obj.index = index
  67. return obj
  68. def __repr__(self) -> str:
  69. return f"{self.__class__.__name__}({self.index})"
  70. def __add__(self, other: int) -> "Node":
  71. return self.__class__(self.index + other)
  72. def serialize(self) -> bytes:
  73. return self.to_bytes(4, byteorder="big")
  74. def encode(self) -> str:
  75. return str(self.index) + self.TAG
  76. @staticmethod
  77. def decode(node: str) -> Union["SoftNode", "HardNode"]:
  78. if len(node) < 1:
  79. raise ValidationError("Cannot use empty string")
  80. node_class: Union[Type["SoftNode"], Type["HardNode"]]
  81. if node[-1] in HARD_NODE_SUFFIXES:
  82. node_class = HardNode
  83. node_index = node[:-1]
  84. else:
  85. node_class = SoftNode
  86. node_index = node
  87. try:
  88. node_value = int(node_index)
  89. except ValueError as err:
  90. raise ValidationError(f"'{node_index}' is not a valid node index.") from err
  91. return node_class(node_value)
  92. class SoftNode(Node):
  93. """
  94. Soft node (unhardened), where value = index .
  95. """
  96. TAG = "" # No tag
  97. OFFSET = 0x0 # No offset
  98. class HardNode(Node):
  99. """
  100. Hard node, where value = index + BIP32_HARDENED_CONSTANT .
  101. """
  102. TAG = "H" # "H" (or "'") means hard node (but use "H" for clarity)
  103. OFFSET = 0x80000000 # 2**31, BIP32 "Hardening constant"
  104. def derive_child_key(
  105. parent_key: bytes,
  106. parent_chain_code: bytes,
  107. node: Node,
  108. ) -> Tuple[bytes, bytes]:
  109. """
  110. Compute a derivative key from the parent key.
  111. From BIP32:
  112. The function CKDpriv((k_par, c_par), i) → (k_i, c_i) computes a child extended
  113. private key from the parent extended private key:
  114. 1. Check whether the child is a hardened key (i ≥ 2**31).
  115. If the child is a hardened key,
  116. let I = HMAC-SHA512(Key = c_par, Data = 0x00 || ser_256(k_par) || ser_32(i)).
  117. (Note: The 0x00 pads the private key to make it 33 bytes long.)
  118. If it is not a hardened key, then
  119. let I = HMAC-SHA512(Key = c_par, Data = ser_P(point(k_par)) || ser_32(i)).
  120. 2. Split I into two 32-byte sequences, I_L and I_R.
  121. 3. The returned child key k_i is parse_256(I_L) + k_par (mod n).
  122. 4. The returned chain code c_i is I_R.
  123. 5. In case parse_256(I_L) ≥ n or k_i = 0, the resulting key is invalid,
  124. and one should proceed with the next value for i.
  125. (Note: this has probability lower than 1 in 2**127.)
  126. """
  127. assert len(parent_chain_code) == 32
  128. if isinstance(node, HardNode):
  129. # NOTE Empty byte is added to align to SoftNode case
  130. assert len(parent_key) == 32 # Should be guaranteed here in return statement
  131. child = hmac_sha512(parent_chain_code, b"\x00" + parent_key + node.serialize())
  132. elif isinstance(node, SoftNode):
  133. assert len(ec_point(parent_key)) == 33 # Should be guaranteed by Account class
  134. child = hmac_sha512(parent_chain_code, ec_point(parent_key) + node.serialize())
  135. else:
  136. raise ValidationError(f"Cannot process: {node}")
  137. assert len(child) == 64
  138. if to_int(child[:32]) >= SECP256K1_N:
  139. # Invalid key, compute using next node (< 2**-127 probability)
  140. return derive_child_key(parent_key, parent_chain_code, node + 1)
  141. child_key = (to_int(child[:32]) + to_int(parent_key)) % SECP256K1_N
  142. if child_key == 0:
  143. # Invalid key, compute using next node (< 2**-127 probability)
  144. return derive_child_key(parent_key, parent_chain_code, node + 1)
  145. child_key_bytes = child_key.to_bytes(32, byteorder="big")
  146. child_chain_code = child[32:]
  147. return child_key_bytes, child_chain_code
  148. class HDPath:
  149. def __init__(self, path: str):
  150. """
  151. Create a new Hierarchical Deterministic path by decoding the given path.
  152. Initializes an hd account generator using the
  153. given path string (from BIP-0032). The path is decoded into nodes of the
  154. derivation key tree, which define a pathway from a given main seed to
  155. the child key that is used for a given purpose. Please also reference BIP-
  156. 0043 (which definites the first level as the "purpose" field of an HD path)
  157. and BIP-0044 (which defines a commonly-used, 5-level scheme for BIP32 paths)
  158. for examples of how this object may be used. Please note however that this
  159. object makes no such assumptions of the use of BIP43 or BIP44, or later BIPs.
  160. :param path : BIP32-compatible derivation path
  161. :type path : str as "m/idx_0/.../idx_n" or "m/idx_0/.../idx_n"
  162. where idx_* is either an integer value (soft node)
  163. or an integer value followed by either the "'" char
  164. or the "H" char (hardened node)
  165. """
  166. if len(path) < 1:
  167. raise ValidationError("Cannot parse path from empty string.")
  168. nodes = path.split("/") # Should at least make 1 entry in resulting list
  169. if nodes[0] not in BASE_NODE_IDENTIFIERS:
  170. raise ValidationError(f'Path is not valid: "{path}". Must start with "m"')
  171. decoded_path = []
  172. for _idx, node in enumerate(nodes[1:]): # We don't need the root node 'm'
  173. try:
  174. decoded_path.append(Node.decode(node))
  175. except ValidationError as err:
  176. raise ValidationError(
  177. f'Path "{path}" is not valid. Issue with node "{node}": {err}'
  178. ) from err
  179. self._path = decoded_path
  180. def __repr__(self) -> str:
  181. return f'{self.__class__.__name__}(path="{self.encode()}")'
  182. def encode(self) -> str:
  183. """
  184. Encodes this class to a string (reversing the decoding in the constructor).
  185. """
  186. encoded_path = ("m",) + tuple(node.encode() for node in self._path)
  187. return "/".join(encoded_path)
  188. def derive(self, seed: bytes) -> bytes:
  189. """
  190. Perform the BIP32 Hierarchical Derivation recursive loop with the given Path.
  191. Note that the key and chain_code are initialized with the main seed, and that
  192. the key that is returned is the child key at the end of derivation process (and
  193. the chain code is discarded)
  194. """
  195. main_node = hmac_sha512(b"Bitcoin seed", seed)
  196. key = main_node[:32]
  197. chain_code = main_node[32:]
  198. for node in self._path:
  199. key, chain_code = derive_child_key(key, chain_code, node)
  200. return key