coinex-api 0.0.89__py3-none-any.whl → 0.0.110__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of coinex-api might be problematic. Click here for more details.

Files changed (197) hide show
  1. coinex/ccxt/__init__.py +1 -1
  2. coinex/ccxt/async_support/__init__.py +1 -1
  3. coinex/ccxt/async_support/base/exchange.py +139 -10
  4. coinex/ccxt/async_support/base/throttler.py +1 -1
  5. coinex/ccxt/async_support/base/ws/cache.py +1 -0
  6. coinex/ccxt/async_support/base/ws/client.py +26 -4
  7. coinex/ccxt/async_support/coinex.py +2 -2
  8. coinex/ccxt/base/exchange.py +587 -91
  9. coinex/ccxt/base/types.py +11 -2
  10. coinex/ccxt/coinex.py +2 -2
  11. coinex/ccxt/pro/__init__.py +1 -1
  12. coinex/ccxt/pro/coinex.py +10 -7
  13. coinex/ccxt/static_dependencies/bip/__init__.py +6 -0
  14. coinex/ccxt/static_dependencies/bip/addr/P2PKH_addr.py +205 -0
  15. coinex/ccxt/static_dependencies/bip/addr/__init__.py +5 -0
  16. coinex/ccxt/static_dependencies/bip/addr/addr_dec_utils.py +125 -0
  17. coinex/ccxt/static_dependencies/bip/addr/addr_key_validator.py +162 -0
  18. coinex/ccxt/static_dependencies/bip/addr/iaddr_decoder.py +48 -0
  19. coinex/ccxt/static_dependencies/bip/addr/iaddr_encoder.py +50 -0
  20. coinex/ccxt/static_dependencies/bip/base58/__init__.py +3 -0
  21. coinex/ccxt/static_dependencies/bip/base58/base58.py +207 -0
  22. coinex/ccxt/static_dependencies/bip/base58/base58_ex.py +25 -0
  23. coinex/ccxt/static_dependencies/bip/base58/base58_xmr.py +155 -0
  24. coinex/ccxt/static_dependencies/bip/bech32/__init__.py +4 -0
  25. coinex/ccxt/static_dependencies/bip/bech32/bch_bech32.py +220 -0
  26. coinex/ccxt/static_dependencies/bip/bech32/bech32.py +235 -0
  27. coinex/ccxt/static_dependencies/bip/bech32/bech32_base.py +246 -0
  28. coinex/ccxt/static_dependencies/bip/bech32/bech32_ex.py +25 -0
  29. coinex/ccxt/static_dependencies/bip/bech32/segwit_bech32.py +173 -0
  30. coinex/ccxt/static_dependencies/bip/bip32/__init__.py +14 -0
  31. coinex/ccxt/static_dependencies/bip/bip32/base/__init__.py +3 -0
  32. coinex/ccxt/static_dependencies/bip/bip32/base/bip32_base.py +581 -0
  33. coinex/ccxt/static_dependencies/bip/bip32/base/ibip32_key_derivator.py +83 -0
  34. coinex/ccxt/static_dependencies/bip/bip32/base/ibip32_mst_key_generator.py +47 -0
  35. coinex/ccxt/static_dependencies/bip/bip32/bip32_const.py +35 -0
  36. coinex/ccxt/static_dependencies/bip/bip32/bip32_ex.py +29 -0
  37. coinex/ccxt/static_dependencies/bip/bip32/bip32_key_data.py +500 -0
  38. coinex/ccxt/static_dependencies/bip/bip32/bip32_key_net_ver.py +83 -0
  39. coinex/ccxt/static_dependencies/bip/bip32/bip32_key_ser.py +294 -0
  40. coinex/ccxt/static_dependencies/bip/bip32/bip32_keys.py +457 -0
  41. coinex/ccxt/static_dependencies/bip/bip32/bip32_path.py +247 -0
  42. coinex/ccxt/static_dependencies/bip/bip32/bip32_utils.py +72 -0
  43. coinex/ccxt/static_dependencies/bip/bip32/kholaw/__init__.py +4 -0
  44. coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_ed25519.py +82 -0
  45. coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_ed25519_key_derivator.py +118 -0
  46. coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_key_derivator_base.py +204 -0
  47. coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_mst_key_generator.py +119 -0
  48. coinex/ccxt/static_dependencies/bip/bip32/slip10/__init__.py +1 -0
  49. coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_key_derivator.py +200 -0
  50. coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_mst_key_generator.py +168 -0
  51. coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_secp256k1.py +82 -0
  52. coinex/ccxt/static_dependencies/bip/bip44/__init__.py +1 -0
  53. coinex/ccxt/static_dependencies/bip/bip44/bip44.py +265 -0
  54. coinex/ccxt/static_dependencies/bip/bip44_base/__init__.py +3 -0
  55. coinex/ccxt/static_dependencies/bip/bip44_base/bip44_base.py +624 -0
  56. coinex/ccxt/static_dependencies/bip/bip44_base/bip44_base_ex.py +25 -0
  57. coinex/ccxt/static_dependencies/bip/bip44_base/bip44_keys.py +225 -0
  58. coinex/ccxt/static_dependencies/bip/coin_conf/__init__.py +2 -0
  59. coinex/ccxt/static_dependencies/bip/coin_conf/coin_conf.py +68 -0
  60. coinex/ccxt/static_dependencies/bip/coin_conf/coins_conf.py +890 -0
  61. coinex/ccxt/static_dependencies/bip/conf/__init__.py +0 -0
  62. coinex/ccxt/static_dependencies/bip/conf/bip44/__init__.py +3 -0
  63. coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_coins.py +126 -0
  64. coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_conf.py +1360 -0
  65. coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_conf_getter.py +153 -0
  66. coinex/ccxt/static_dependencies/bip/conf/bip49/__init__.py +3 -0
  67. coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_coins.py +53 -0
  68. coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_conf.py +366 -0
  69. coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_conf_getter.py +80 -0
  70. coinex/ccxt/static_dependencies/bip/conf/bip84/__init__.py +3 -0
  71. coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_coins.py +39 -0
  72. coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_conf.py +113 -0
  73. coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_conf_getter.py +66 -0
  74. coinex/ccxt/static_dependencies/bip/conf/bip86/__init__.py +3 -0
  75. coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_coins.py +37 -0
  76. coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_conf.py +83 -0
  77. coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_conf_getter.py +64 -0
  78. coinex/ccxt/static_dependencies/bip/conf/common/__init__.py +8 -0
  79. coinex/ccxt/static_dependencies/bip/conf/common/atom_addr.py +104 -0
  80. coinex/ccxt/static_dependencies/bip/conf/common/bip_bitcoin_cash_conf.py +106 -0
  81. coinex/ccxt/static_dependencies/bip/conf/common/bip_coin_conf.py +217 -0
  82. coinex/ccxt/static_dependencies/bip/conf/common/bip_coins.py +28 -0
  83. coinex/ccxt/static_dependencies/bip/conf/common/bip_conf_const.py +30 -0
  84. coinex/ccxt/static_dependencies/bip/conf/common/bip_litecoin_conf.py +121 -0
  85. coinex/ccxt/static_dependencies/bip/ecc/__init__.py +42 -0
  86. coinex/ccxt/static_dependencies/bip/ecc/common/__init__.py +0 -0
  87. coinex/ccxt/static_dependencies/bip/ecc/common/dummy_point.py +219 -0
  88. coinex/ccxt/static_dependencies/bip/ecc/common/ikeys.py +263 -0
  89. coinex/ccxt/static_dependencies/bip/ecc/common/ipoint.py +190 -0
  90. coinex/ccxt/static_dependencies/bip/ecc/conf.py +28 -0
  91. coinex/ccxt/static_dependencies/bip/ecc/curve/__init__.py +0 -0
  92. coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve.py +121 -0
  93. coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve_getter.py +74 -0
  94. coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve_types.py +37 -0
  95. coinex/ccxt/static_dependencies/bip/ecc/ecdsa/__init__.py +0 -0
  96. coinex/ccxt/static_dependencies/bip/ecc/ecdsa/ecdsa_keys.py +36 -0
  97. coinex/ccxt/static_dependencies/bip/ecc/secp256k1/__init__.py +0 -0
  98. coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1.py +36 -0
  99. coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_const.py +59 -0
  100. coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_keys_ecdsa.py +248 -0
  101. coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_point_ecdsa.py +234 -0
  102. coinex/ccxt/static_dependencies/bip/slip/__init__.py +0 -0
  103. coinex/ccxt/static_dependencies/bip/slip/slip173/__init__.py +1 -0
  104. coinex/ccxt/static_dependencies/bip/slip/slip173/slip173.py +60 -0
  105. coinex/ccxt/static_dependencies/bip/slip/slip32/__init__.py +4 -0
  106. coinex/ccxt/static_dependencies/bip/slip/slip32/slip32.py +322 -0
  107. coinex/ccxt/static_dependencies/bip/slip/slip32/slip32_key_net_ver.py +62 -0
  108. coinex/ccxt/static_dependencies/bip/slip/slip44/__init__.py +1 -0
  109. coinex/ccxt/static_dependencies/bip/slip/slip44/slip44.py +81 -0
  110. coinex/ccxt/static_dependencies/bip/utils/__init__.py +0 -0
  111. coinex/ccxt/static_dependencies/bip/utils/conf/__init__.py +1 -0
  112. coinex/ccxt/static_dependencies/bip/utils/conf/coin_names.py +59 -0
  113. coinex/ccxt/static_dependencies/bip/utils/crypto/__init__.py +10 -0
  114. coinex/ccxt/static_dependencies/bip/utils/crypto/aes_ecb.py +152 -0
  115. coinex/ccxt/static_dependencies/bip/utils/crypto/blake2.py +191 -0
  116. coinex/ccxt/static_dependencies/bip/utils/crypto/chacha20_poly1305.py +101 -0
  117. coinex/ccxt/static_dependencies/bip/utils/crypto/hash160.py +57 -0
  118. coinex/ccxt/static_dependencies/bip/utils/crypto/hmac.py +118 -0
  119. coinex/ccxt/static_dependencies/bip/utils/crypto/pbkdf2.py +66 -0
  120. coinex/ccxt/static_dependencies/bip/utils/crypto/ripemd.py +58 -0
  121. coinex/ccxt/static_dependencies/bip/utils/crypto/scrypt.py +66 -0
  122. coinex/ccxt/static_dependencies/bip/utils/crypto/sha2.py +182 -0
  123. coinex/ccxt/static_dependencies/bip/utils/crypto/sha3.py +99 -0
  124. coinex/ccxt/static_dependencies/bip/utils/misc/__init__.py +7 -0
  125. coinex/ccxt/static_dependencies/bip/utils/misc/algo.py +108 -0
  126. coinex/ccxt/static_dependencies/bip/utils/misc/base32.py +151 -0
  127. coinex/ccxt/static_dependencies/bip/utils/misc/bit.py +115 -0
  128. coinex/ccxt/static_dependencies/bip/utils/misc/bytes.py +200 -0
  129. coinex/ccxt/static_dependencies/bip/utils/misc/data_bytes.py +181 -0
  130. coinex/ccxt/static_dependencies/bip/utils/misc/integer.py +97 -0
  131. coinex/ccxt/static_dependencies/bip/utils/misc/string.py +54 -0
  132. coinex/ccxt/static_dependencies/bip/utils/typing/__init__.py +1 -0
  133. coinex/ccxt/static_dependencies/bip/utils/typing/literal.py +27 -0
  134. coinex/ccxt/static_dependencies/bip/wif/__init__.py +1 -0
  135. coinex/ccxt/static_dependencies/bip/wif/wif.py +144 -0
  136. coinex/ccxt/static_dependencies/dydx_v4_client/amino/amino_pb2.py +31 -0
  137. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/base/v1beta1/coin_pb2.py +47 -0
  138. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/multisig/keys_pb2.py +33 -0
  139. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/multisig/v1beta1/multisig_pb2.py +33 -0
  140. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/secp256k1/keys_pb2.py +34 -0
  141. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/msg/v1/msg_pb2.py +27 -0
  142. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/tx/signing/v1beta1/signing_pb2.py +38 -0
  143. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/tx/v1beta1/tx_pb2.py +75 -0
  144. coinex/ccxt/static_dependencies/dydx_v4_client/cosmos_proto/cosmos_pb2.py +36 -0
  145. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/accountplus_pb2.py +31 -0
  146. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/genesis_pb2.py +40 -0
  147. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/models_pb2.py +26 -0
  148. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/params_pb2.py +29 -0
  149. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/query_pb2.py +57 -0
  150. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/tx_pb2.py +51 -0
  151. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/block_rate_limit_config_pb2.py +37 -0
  152. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/clob_pair_pb2.py +41 -0
  153. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/equity_tier_limit_config_pb2.py +35 -0
  154. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/finalize_block_pb2.py +27 -0
  155. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/liquidations_config_pb2.py +39 -0
  156. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/liquidations_pb2.py +38 -0
  157. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/matches_pb2.py +55 -0
  158. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/mev_pb2.py +49 -0
  159. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/operation_pb2.py +32 -0
  160. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/order_pb2.py +86 -0
  161. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/order_removals_pb2.py +32 -0
  162. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/process_proposer_matches_events_pb2.py +42 -0
  163. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/query_pb2.py +124 -0
  164. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/streaming_pb2.py +29 -0
  165. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/tx_pb2.py +117 -0
  166. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/genesis_pb2.py +26 -0
  167. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/query_pb2.py +26 -0
  168. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/transfer_pb2.py +61 -0
  169. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/tx_pb2.py +37 -0
  170. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/asset_position_pb2.py +29 -0
  171. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/genesis_pb2.py +30 -0
  172. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/perpetual_position_pb2.py +33 -0
  173. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/query_pb2.py +63 -0
  174. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/streaming_pb2.py +31 -0
  175. coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/subaccount_pb2.py +33 -0
  176. coinex/ccxt/static_dependencies/dydx_v4_client/gogoproto/gogo_pb2.py +102 -0
  177. coinex/ccxt/static_dependencies/dydx_v4_client/registry.py +38 -0
  178. coinex/ccxt/static_dependencies/ecdsa/ellipticcurve.py +842 -0
  179. coinex/ccxt/static_dependencies/ecdsa/keys.py +15 -4
  180. coinex/ccxt/static_dependencies/mnemonic/__init__.py +4 -0
  181. coinex/ccxt/static_dependencies/mnemonic/mnemonic.py +282 -0
  182. coinex/ccxt/static_dependencies/mnemonic/py.typed +1 -0
  183. coinex/ccxt/static_dependencies/mnemonic/wordlist/chinese_simplified.txt +2048 -0
  184. coinex/ccxt/static_dependencies/mnemonic/wordlist/chinese_traditional.txt +2048 -0
  185. coinex/ccxt/static_dependencies/mnemonic/wordlist/czech.txt +2048 -0
  186. coinex/ccxt/static_dependencies/mnemonic/wordlist/english.txt +2048 -0
  187. coinex/ccxt/static_dependencies/mnemonic/wordlist/french.txt +2048 -0
  188. coinex/ccxt/static_dependencies/mnemonic/wordlist/italian.txt +2048 -0
  189. coinex/ccxt/static_dependencies/mnemonic/wordlist/japanese.txt +2048 -0
  190. coinex/ccxt/static_dependencies/mnemonic/wordlist/korean.txt +2048 -0
  191. coinex/ccxt/static_dependencies/mnemonic/wordlist/portuguese.txt +2048 -0
  192. coinex/ccxt/static_dependencies/mnemonic/wordlist/russian.txt +2048 -0
  193. coinex/ccxt/static_dependencies/mnemonic/wordlist/spanish.txt +2048 -0
  194. coinex/ccxt/static_dependencies/mnemonic/wordlist/turkish.txt +2048 -0
  195. {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/METADATA +3 -3
  196. {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/RECORD +197 -17
  197. {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/WHEEL +1 -1
@@ -0,0 +1,581 @@
1
+ # Copyright (c) 2021 Emanuele Bellocchia
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ """Module with BIP32 base class."""
22
+
23
+ # Imports
24
+ from __future__ import annotations
25
+
26
+ from abc import ABC, abstractmethod
27
+ from typing import Optional, Type, Union
28
+
29
+ from .ibip32_key_derivator import IBip32KeyDerivator
30
+ from .ibip32_mst_key_generator import IBip32MstKeyGenerator
31
+ from ..bip32_ex import Bip32KeyError
32
+ from ..bip32_key_data import Bip32ChainCode, Bip32Depth, Bip32FingerPrint, Bip32KeyData, Bip32KeyIndex
33
+ from ..bip32_key_net_ver import Bip32KeyNetVersions
34
+ from ..bip32_key_ser import Bip32KeyDeserializer
35
+ from ..bip32_keys import Bip32PrivateKey, Bip32PublicKey
36
+ from ..bip32_path import Bip32Path, Bip32PathParser
37
+ from ...ecc import EllipticCurve, EllipticCurveGetter, EllipticCurveTypes, IPoint, IPrivateKey, IPublicKey
38
+
39
+
40
+ class Bip32Base(ABC):
41
+ """
42
+ BIP32 base class.
43
+ It allows master key generation and children keys derivation in according to BIP-0032/SLIP-0010.
44
+ It shall be derived to implement derivation for a specific elliptic curve.
45
+ """
46
+
47
+ m_priv_key: Optional[Bip32PrivateKey]
48
+ m_pub_key: Bip32PublicKey
49
+
50
+ #
51
+ # Class methods for construction
52
+ #
53
+
54
+ @classmethod
55
+ def FromSeed(cls,
56
+ seed_bytes: bytes,
57
+ key_net_ver: Optional[Bip32KeyNetVersions] = None) -> Bip32Base:
58
+ """
59
+ Create a Bip32 object from the specified seed (e.g. BIP39 seed).
60
+
61
+ Args:
62
+ seed_bytes (bytes) : Seed bytes
63
+ key_net_ver (Bip32KeyNetVersions object, optional): Bip32KeyNetVersions object
64
+ (default: specific class key net version)
65
+
66
+ Returns:
67
+ Bip32Base object: Bip32Base object
68
+
69
+ Raises:
70
+ ValueError: If the seed is too short
71
+ Bip32KeyError: If the seed is not suitable for master key generation
72
+ """
73
+ priv_key_bytes, chain_code_bytes = cls._MasterKeyGenerator().GenerateFromSeed(seed_bytes)
74
+
75
+ return cls(
76
+ priv_key=priv_key_bytes,
77
+ pub_key=None,
78
+ key_data=Bip32KeyData(chain_code=chain_code_bytes),
79
+ key_net_ver=key_net_ver or cls._DefaultKeyNetVersion()
80
+ )
81
+
82
+ @classmethod
83
+ def FromSeedAndPath(cls,
84
+ seed_bytes: bytes,
85
+ path: Union[str, Bip32Path],
86
+ key_net_ver: Optional[Bip32KeyNetVersions] = None) -> Bip32Base:
87
+ """
88
+ Create a Bip32 object from the specified seed (e.g. BIP39 seed) and path.
89
+
90
+ Args:
91
+ seed_bytes (bytes) : Seed bytes
92
+ path (str or Bip32Path object) : Path
93
+ key_net_ver (Bip32KeyNetVersions object, optional): Bip32KeyNetVersions object
94
+ (default: specific class key net version)
95
+
96
+ Returns:
97
+ Bip32Base object: Bip32Base object
98
+
99
+ Raises:
100
+ ValueError: If the seed length is too short
101
+ Bip32PathError: If the path is not valid
102
+ Bip32KeyError: If the seed is not suitable for master key generation
103
+ """
104
+ key_net_ver = key_net_ver or cls._DefaultKeyNetVersion()
105
+ return cls.FromSeed(seed_bytes, key_net_ver).DerivePath(path)
106
+
107
+ @classmethod
108
+ def FromExtendedKey(cls,
109
+ ex_key_str: str,
110
+ key_net_ver: Optional[Bip32KeyNetVersions] = None) -> Bip32Base:
111
+ """
112
+ Create a Bip32 object from the specified extended key.
113
+
114
+ Args:
115
+ ex_key_str (str) : Extended key string
116
+ key_net_ver (Bip32KeyNetVersions object, optional): Bip32KeyNetVersions object
117
+ (default: specific class key net version)
118
+
119
+ Returns:
120
+ Bip32Base object: Bip32Base object
121
+
122
+ Raises:
123
+ Bip32KeyError: If the key is not valid
124
+ """
125
+ key_net_ver = key_net_ver or cls._DefaultKeyNetVersion()
126
+
127
+ # De-serialize key
128
+ deser_key = Bip32KeyDeserializer.DeserializeKey(ex_key_str, key_net_ver)
129
+ # Get key parts
130
+ key_bytes, key_data, is_public = deser_key.KeyBytes(), deser_key.KeyData(), deser_key.IsPublic()
131
+
132
+ # If depth is zero, fingerprint shall be the master one and child index shall be zero
133
+ if key_data.Depth() == 0:
134
+ if not key_data.ParentFingerPrint().IsMasterKey():
135
+ raise Bip32KeyError(
136
+ f"Invalid extended master key (wrong fingerprint: {key_data.ParentFingerPrint().ToHex()})"
137
+ )
138
+ if key_data.Index() != 0:
139
+ raise Bip32KeyError(f"Invalid extended master key (wrong child index: {key_data.Index().ToInt()})")
140
+
141
+ return cls(
142
+ priv_key=key_bytes if not is_public else None,
143
+ pub_key=key_bytes if is_public else None,
144
+ key_data=key_data,
145
+ key_net_ver=key_net_ver
146
+ )
147
+
148
+ @classmethod
149
+ def FromPrivateKey(cls,
150
+ priv_key: Union[bytes, IPrivateKey],
151
+ key_data: Bip32KeyData = Bip32KeyData(),
152
+ key_net_ver: Optional[Bip32KeyNetVersions] = None) -> Bip32Base:
153
+ """
154
+ Create a Bip32 object from the specified private key and derivation data.
155
+ If only the private key bytes are specified, the key will be considered a master key with
156
+ the chain code set to zero, since there is no way to recover the key derivation data.
157
+
158
+ Args:
159
+ priv_key (bytes or IPrivateKey) : Private key
160
+ key_data (Bip32KeyData object, optional) : Key data (default: all zeros)
161
+ key_net_ver (Bip32KeyNetVersions object, optional): Bip32KeyNetVersions object
162
+ (default: specific class key net version)
163
+
164
+ Returns:
165
+ Bip32Base object: Bip32Base object
166
+
167
+ Raises:
168
+ Bip32KeyError: If the key is not valid
169
+ """
170
+ return cls(
171
+ priv_key=priv_key,
172
+ pub_key=None,
173
+ key_data=key_data,
174
+ key_net_ver=key_net_ver or cls._DefaultKeyNetVersion()
175
+ )
176
+
177
+ @classmethod
178
+ def FromPublicKey(cls,
179
+ pub_key: Union[bytes, IPoint, IPublicKey],
180
+ key_data: Bip32KeyData = Bip32KeyData(),
181
+ key_net_ver: Optional[Bip32KeyNetVersions] = None) -> Bip32Base:
182
+ """
183
+ Create a Bip32 object from the specified public key and derivation data.
184
+ If only the public key bytes are specified, the key will be considered a master key with
185
+ the chain code set to zero, since there is no way to recover the key derivation data.
186
+
187
+ Args:
188
+ pub_key (bytes, IPoint or IPublicKey) : Public key
189
+ key_data (Bip32KeyData object, optional) : Key data (default: all zeros)
190
+ key_net_ver (Bip32KeyNetVersions object, optional): Bip32KeyNetVersions object
191
+ (default: specific class key net version)
192
+
193
+ Returns:
194
+ Bip32Base object: Bip32Base object
195
+
196
+ Raises:
197
+ Bip32KeyError: If the key is not valid
198
+ """
199
+ return cls(
200
+ priv_key=None,
201
+ pub_key=pub_key,
202
+ key_data=key_data,
203
+ key_net_ver=key_net_ver or cls._DefaultKeyNetVersion()
204
+ )
205
+
206
+ #
207
+ # Public methods
208
+ #
209
+
210
+ def __init__(self,
211
+ priv_key: Optional[Union[bytes, IPrivateKey]],
212
+ pub_key: Optional[Union[bytes, IPoint, IPublicKey]],
213
+ key_data: Bip32KeyData,
214
+ key_net_ver: Bip32KeyNetVersions) -> None:
215
+ """
216
+ Construct class.
217
+
218
+ Args:
219
+ priv_key (bytes or IPrivateKey) : Private key (None for a public-only object)
220
+ pub_key (bytes, IPoint or IPublicKey) : Public key (only needed for a public-only object)
221
+ If priv_key is not None, it'll be discarded
222
+ key_data (Bip32KeyData object) : Key data
223
+ key_net_ver (Bip32KeyNetVersions object): Bip32KeyNetVersions object
224
+
225
+ Raises:
226
+ Bip32KeyError: If the constructed key is not valid
227
+ """
228
+ curve = self.Curve()
229
+
230
+ # Private key object
231
+ if priv_key is not None:
232
+ # Check that key type matches the Bip curve
233
+ if not isinstance(priv_key, bytes) and not isinstance(priv_key, curve.PrivateKeyClass()):
234
+ raise Bip32KeyError(f"Invalid private key class, a {curve.Name()} key is required")
235
+
236
+ self.m_priv_key = Bip32PrivateKey.FromBytesOrKeyObject(priv_key,
237
+ key_data,
238
+ key_net_ver,
239
+ self.CurveType())
240
+ self.m_pub_key = self.m_priv_key.PublicKey()
241
+ # Public-only object
242
+ else:
243
+ # Check that key type matches the Bip curve
244
+ if (not isinstance(pub_key, bytes)
245
+ and not isinstance(pub_key, curve.PointClass())
246
+ and not isinstance(pub_key, curve.PublicKeyClass())):
247
+ raise Bip32KeyError(f"Invalid public key class, a {curve.Name()} key or point is required")
248
+
249
+ self.m_priv_key = None
250
+ self.m_pub_key = Bip32PublicKey.FromBytesOrKeyObject(pub_key,
251
+ key_data,
252
+ key_net_ver,
253
+ self.CurveType())
254
+
255
+ def ChildKey(self,
256
+ index: Union[int, Bip32KeyIndex]) -> Bip32Base:
257
+ """
258
+ Create and return a child key of the current one with the specified index.
259
+ The index shall be hardened using HardenIndex method to use the private derivation algorithm.
260
+
261
+ Args:
262
+ index (int or Bip32KeyIndex object): Index
263
+
264
+ Returns:
265
+ Bip32Base object: Bip32Base object
266
+
267
+ Raises:
268
+ Bip32KeyError: If the index results in an invalid key
269
+ """
270
+ index = self.__GetIndex(index)
271
+ return self.__ValidateAndCkdPriv(index) if not self.IsPublicOnly() else self.__ValidateAndCkdPub(index)
272
+
273
+ def DerivePath(self,
274
+ path: Union[str, Bip32Path]) -> Bip32Base:
275
+ """
276
+ Derive children keys from the specified path.
277
+
278
+ Args:
279
+ path (str or Bip32Path object): Path
280
+
281
+ Returns:
282
+ Bip32Base object: Bip32Base object
283
+
284
+ Raises:
285
+ Bip32KeyError: If the index results in an invalid key
286
+ Bip32PathError: If the path is not valid
287
+ ValueError: If the path is a master path and the key is a child key
288
+ """
289
+ path = self.__GetPath(path)
290
+ if self.Depth() > 0 and path.IsAbsolute():
291
+ raise ValueError("Absolute paths can only be derived from a master key, not child ones")
292
+
293
+ bip32_obj = self
294
+ # Derive children keys
295
+ for path_elem in path:
296
+ bip32_obj = bip32_obj.ChildKey(path_elem)
297
+
298
+ return bip32_obj
299
+
300
+ def ConvertToPublic(self) -> None:
301
+ """Convert the object into a public one."""
302
+ self.m_priv_key = None
303
+
304
+ def IsPublicOnly(self) -> bool:
305
+ """
306
+ Get if it's public-only.
307
+
308
+ Returns:
309
+ bool: True if public-only, false otherwise
310
+ """
311
+ return self.m_priv_key is None
312
+
313
+ def PrivateKey(self) -> Bip32PrivateKey:
314
+ """
315
+ Return private key object.
316
+
317
+ Returns:
318
+ Bip32PrivateKey object: Bip32PrivateKey object
319
+
320
+ Raises:
321
+ Bip32KeyError: If internal key is public-only
322
+ """
323
+ if self.IsPublicOnly():
324
+ raise Bip32KeyError("Public-only deterministic keys have no private half")
325
+
326
+ assert isinstance(self.m_priv_key, Bip32PrivateKey)
327
+ return self.m_priv_key
328
+
329
+ def PublicKey(self) -> Bip32PublicKey:
330
+ """
331
+ Return public key object.
332
+
333
+ Returns:
334
+ Bip32PublicKey object: Bip32PublicKey object
335
+ """
336
+ return self.m_pub_key
337
+
338
+ def KeyNetVersions(self) -> Bip32KeyNetVersions:
339
+ """
340
+ Get key net versions.
341
+
342
+ Returns:
343
+ Bip32KeyNetVersions object: Bip32KeyNetVersions object
344
+ """
345
+ return self.m_pub_key.KeyNetVersions()
346
+
347
+ def Depth(self) -> Bip32Depth:
348
+ """
349
+ Get current depth.
350
+
351
+ Returns:
352
+ Bip32Depth object: Current depth
353
+ """
354
+ return self.m_pub_key.Data().Depth()
355
+
356
+ def Index(self) -> Bip32KeyIndex:
357
+ """
358
+ Get current index.
359
+
360
+ Returns:
361
+ Bip32KeyIndex object: Current index
362
+ """
363
+ return self.m_pub_key.Data().Index()
364
+
365
+ def ChainCode(self) -> Bip32ChainCode:
366
+ """
367
+ Get chain code.
368
+
369
+ Returns:
370
+ Bip32ChainCode: Chain code
371
+ """
372
+ return self.m_pub_key.ChainCode()
373
+
374
+ def FingerPrint(self) -> Bip32FingerPrint:
375
+ """
376
+ Get public key fingerprint.
377
+
378
+ Returns:
379
+ Bip32FingerPrint object: Public key fingerprint bytes
380
+ """
381
+ return self.m_pub_key.FingerPrint()
382
+
383
+ def ParentFingerPrint(self) -> Bip32FingerPrint:
384
+ """
385
+ Get parent fingerprint.
386
+
387
+ Returns:
388
+ Bip32FingerPrint object: Parent fingerprint bytes
389
+ """
390
+ return self.m_pub_key.Data().ParentFingerPrint()
391
+
392
+ @classmethod
393
+ def Curve(cls) -> EllipticCurve:
394
+ """
395
+ Return the elliptic curve.
396
+
397
+ Returns:
398
+ EllipticCurve object: EllipticCurve object
399
+ """
400
+ return EllipticCurveGetter.FromType(cls.CurveType())
401
+
402
+ @classmethod
403
+ def IsPublicDerivationSupported(cls) -> bool:
404
+ """
405
+ Get if public derivation is supported.
406
+
407
+ Returns:
408
+ bool: True if supported, false otherwise.
409
+ """
410
+ return cls._KeyDerivator().IsPublicDerivationSupported()
411
+
412
+ #
413
+ # Private methods
414
+ #
415
+
416
+ def __ValidateAndCkdPriv(self,
417
+ index: Bip32KeyIndex) -> Bip32Base:
418
+ """
419
+ Check the key index validity and create a child key with the specified index using private derivation.
420
+
421
+ Args:
422
+ index (Bip32KeyIndex object): Key index
423
+
424
+ Returns:
425
+ Bip32Base object: Bip32Base object
426
+
427
+ Raises:
428
+ Bip32KeyError: If the index results in an invalid key
429
+ """
430
+ return self.__CkdPriv(index)
431
+
432
+ def __ValidateAndCkdPub(self,
433
+ index: Bip32KeyIndex) -> Bip32Base:
434
+ """
435
+ Check the key index validity and create a child key with the specified index using public derivation.
436
+
437
+ Args:
438
+ index (Bip32KeyIndex object): Key index
439
+
440
+ Returns:
441
+ Bip32Base object: Bip32Base object
442
+
443
+ Raises:
444
+ Bip32KeyError: If the index results in an invalid key
445
+ """
446
+
447
+ # Hardened index is not supported for public derivation
448
+ if index.IsHardened():
449
+ raise Bip32KeyError("Public child derivation cannot be used to create a hardened child key")
450
+
451
+ return self.__CkdPub(index)
452
+
453
+ def __CkdPriv(self,
454
+ index: Bip32KeyIndex) -> Bip32Base:
455
+ """
456
+ Derive a child key with the specified index using private derivation.
457
+
458
+ Args:
459
+ index (Bip32KeyIndex object): Key index
460
+
461
+ Returns:
462
+ Bip32Base object: Bip32Base object
463
+
464
+ Raises:
465
+ Bip32KeyError: If the index results in an invalid key
466
+ """
467
+ assert self.m_priv_key is not None
468
+
469
+
470
+ priv_key_bytes, chain_code_bytes = self._KeyDerivator().CkdPriv(self.m_priv_key,
471
+ self.m_pub_key,
472
+ index)
473
+ return self.__class__(
474
+ priv_key=priv_key_bytes,
475
+ pub_key=None,
476
+ key_data=Bip32KeyData(
477
+ chain_code=chain_code_bytes,
478
+ depth=self.Depth().Increase(),
479
+ index=index,
480
+ parent_fprint=self.FingerPrint()
481
+ ),
482
+ key_net_ver=self.KeyNetVersions()
483
+ )
484
+
485
+ def __CkdPub(self,
486
+ index: Bip32KeyIndex) -> Bip32Base:
487
+ """
488
+ Derive a child key with the specified index using public derivation.
489
+
490
+ Args:
491
+ index (Bip32KeyIndex object): Key index
492
+
493
+ Returns:
494
+ Bip32Base object: Bip32Base object
495
+
496
+ Raises:
497
+ Bip32KeyError: If the index results in an invalid key
498
+ """
499
+ pub_key_bytes, chain_code_bytes = self._KeyDerivator().CkdPub(self.m_pub_key,
500
+ index)
501
+ return self.__class__(
502
+ priv_key=None,
503
+ pub_key=pub_key_bytes,
504
+ key_data=Bip32KeyData(
505
+ chain_code=chain_code_bytes,
506
+ depth=self.Depth().Increase(),
507
+ index=index,
508
+ parent_fprint=self.FingerPrint()
509
+ ),
510
+ key_net_ver=self.KeyNetVersions()
511
+ )
512
+
513
+ @staticmethod
514
+ def __GetIndex(index: Union[int, Bip32KeyIndex]) -> Bip32KeyIndex:
515
+ """
516
+ Get index object.
517
+
518
+ Args:
519
+ index (int or Bip32KeyIndex): Index
520
+
521
+ Returns:
522
+ Bip32KeyIndex object: Bip32KeyIndex object
523
+ """
524
+ return Bip32KeyIndex(index) if isinstance(index, int) else index
525
+
526
+ @staticmethod
527
+ def __GetPath(path: Union[str, Bip32Path]) -> Bip32Path:
528
+ """
529
+ Get path object.
530
+
531
+ Args:
532
+ path (str or Bip32Path): Path
533
+
534
+ Returns:
535
+ Bip32Path object: Bip32Path object
536
+ """
537
+ return Bip32PathParser.Parse(path) if isinstance(path, str) else path
538
+
539
+ #
540
+ # Abstract methods
541
+ #
542
+
543
+ @staticmethod
544
+ @abstractmethod
545
+ def CurveType() -> EllipticCurveTypes:
546
+ """
547
+ Return the elliptic curve type.
548
+
549
+ Returns:
550
+ EllipticCurveTypes: Curve type
551
+ """
552
+
553
+ @staticmethod
554
+ @abstractmethod
555
+ def _DefaultKeyNetVersion() -> Bip32KeyNetVersions:
556
+ """
557
+ Return the default key net version.
558
+
559
+ Returns:
560
+ Bip32KeyNetVersions object: Bip32KeyNetVersions object
561
+ """
562
+
563
+ @staticmethod
564
+ @abstractmethod
565
+ def _KeyDerivator() -> Type[IBip32KeyDerivator]:
566
+ """
567
+ Return the key derivator class.
568
+
569
+ Returns:
570
+ IBip32KeyDerivator class: Key derivator class
571
+ """
572
+
573
+ @staticmethod
574
+ @abstractmethod
575
+ def _MasterKeyGenerator() -> Type[IBip32MstKeyGenerator]:
576
+ """
577
+ Return the master key generator class.
578
+
579
+ Returns:
580
+ IBip32MstKeyGenerator class: Master key generator class
581
+ """
@@ -0,0 +1,83 @@
1
+ # Copyright (c) 2021 Emanuele Bellocchia
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ """Module for BIP32 SLIP-0010 keys derivation."""
22
+
23
+ # Imports
24
+ from abc import ABC, abstractmethod
25
+ from typing import Tuple, Union
26
+
27
+ from ..bip32_key_data import Bip32KeyIndex
28
+ from ..bip32_keys import Bip32PrivateKey, Bip32PublicKey
29
+ from ...ecc import IPoint
30
+
31
+
32
+ class IBip32KeyDerivator(ABC):
33
+ """Interface for generic BIP32 key derivator."""
34
+
35
+ @staticmethod
36
+ @abstractmethod
37
+ def IsPublicDerivationSupported() -> bool:
38
+ """
39
+ Get if public derivation is supported.
40
+
41
+ Returns:
42
+ bool: True if supported, false otherwise.
43
+ """
44
+
45
+ @classmethod
46
+ @abstractmethod
47
+ def CkdPriv(cls,
48
+ priv_key: Bip32PrivateKey,
49
+ pub_key: Bip32PublicKey,
50
+ index: Bip32KeyIndex) -> Tuple[bytes, bytes]:
51
+ """
52
+ Derive a child key with the specified index using private derivation.
53
+
54
+ Args:
55
+ priv_key (Bip32PrivateKey object): Bip32PrivateKey object
56
+ pub_key (Bip32PublicKey object) : Bip32PublicKey object
57
+ index (Bip32KeyIndex object) : Key index
58
+
59
+ Returns:
60
+ tuple[bytes, bytes]: Private key bytes (index 0) and chain code bytes (index 1)
61
+
62
+ Raises:
63
+ Bip32KeyError: If the index results in an invalid key
64
+ """
65
+
66
+ @classmethod
67
+ @abstractmethod
68
+ def CkdPub(cls,
69
+ pub_key: Bip32PublicKey,
70
+ index: Bip32KeyIndex) -> Tuple[Union[bytes, IPoint], bytes]:
71
+ """
72
+ Derive a child key with the specified index using public derivation.
73
+
74
+ Args:
75
+ pub_key (Bip32PublicKey object): Bip32PublicKey object
76
+ index (Bip32KeyIndex object) : Key index
77
+
78
+ Returns:
79
+ tuple[bytes or IPoint, bytes]: Public key bytes or point (index 0) and chain code bytes (index 1)
80
+
81
+ Raises:
82
+ Bip32KeyError: If the index results in an invalid key
83
+ """