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,457 @@
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 keys handling."""
22
+
23
+ # Imports
24
+ from __future__ import annotations
25
+
26
+ from abc import ABC, abstractmethod
27
+ from functools import lru_cache
28
+ from typing import Union
29
+
30
+ from .bip32_ex import Bip32KeyError
31
+ from .bip32_key_data import Bip32ChainCode, Bip32FingerPrint, Bip32KeyData
32
+ from .bip32_key_net_ver import Bip32KeyNetVersions
33
+ from .bip32_key_ser import Bip32PrivateKeySerializer, Bip32PublicKeySerializer
34
+ from ..ecc import EllipticCurve, EllipticCurveGetter, EllipticCurveTypes, IPoint, IPrivateKey, IPublicKey
35
+ from ..utils.crypto import Hash160
36
+ from ..utils.misc import DataBytes
37
+
38
+
39
+ class _Bip32KeyBase(ABC):
40
+ """Base class for a generic BIP32 key."""
41
+
42
+ m_curve: EllipticCurve
43
+ m_curve_type: EllipticCurveTypes
44
+ m_key_data: Bip32KeyData
45
+ m_key_net_ver: Bip32KeyNetVersions
46
+
47
+ def __init__(self,
48
+ key_data: Bip32KeyData,
49
+ key_net_ver: Bip32KeyNetVersions,
50
+ curve_type: EllipticCurveTypes) -> None:
51
+ """
52
+ Construct class.
53
+
54
+ Args:
55
+ key_data (Bip32KeyData object) : Key data
56
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
57
+ curve_type (EllipticCurveTypes) : Elliptic curve type
58
+ """
59
+ self.m_curve = EllipticCurveGetter.FromType(curve_type)
60
+ self.m_curve_type = curve_type
61
+ self.m_key_data = key_data
62
+ self.m_key_net_ver = key_net_ver
63
+
64
+ def Curve(self) -> EllipticCurve:
65
+ """
66
+ Return key elliptic curve.
67
+
68
+ Returns:
69
+ EllipticCurve object: EllipticCurve object
70
+ """
71
+ return self.m_curve
72
+
73
+ def CurveType(self) -> EllipticCurveTypes:
74
+ """
75
+ Return key elliptic curve type.
76
+
77
+ Returns:
78
+ EllipticCurveTypes: Elliptic curve type
79
+ """
80
+ return self.m_curve_type
81
+
82
+ def Data(self) -> Bip32KeyData:
83
+ """
84
+ Return key data.
85
+
86
+ Returns:
87
+ BipKeyData object: BipKeyData object
88
+ """
89
+ return self.m_key_data
90
+
91
+ def ChainCode(self) -> Bip32ChainCode:
92
+ """
93
+ Return the chain code.
94
+
95
+ Returns:
96
+ Bip32ChainCode object: Bip32ChainCode object
97
+ """
98
+ return self.Data().ChainCode()
99
+
100
+ def KeyNetVersions(self) -> Bip32KeyNetVersions:
101
+ """
102
+ Get key net versions.
103
+
104
+ Returns:
105
+ Bip32KeyNetVersions object: Bip32KeyNetVersions object
106
+ """
107
+ return self.m_key_net_ver
108
+
109
+ @abstractmethod
110
+ def ToExtended(self) -> str:
111
+ """
112
+ Return key in serialized extended format.
113
+
114
+ Returns:
115
+ str: Key in serialized extended format
116
+ """
117
+
118
+
119
+ class Bip32PublicKey(_Bip32KeyBase):
120
+ """
121
+ BIP32 public key class.
122
+ It represents a public key used by BIP32 with all the related data (e.g. depth, chain code, etc...).
123
+ """
124
+
125
+ m_pub_key: IPublicKey
126
+
127
+ @classmethod
128
+ def FromBytesOrKeyObject(cls,
129
+ pub_key: Union[bytes, IPoint, IPublicKey],
130
+ key_data: Bip32KeyData,
131
+ key_net_ver: Bip32KeyNetVersions,
132
+ curve_type: EllipticCurveTypes) -> Bip32PublicKey:
133
+ """
134
+ Get the public key from key bytes or object.
135
+
136
+ Args:
137
+ pub_key (bytes, IPoint or IPublicKey) : Public key
138
+ key_data (Bip32KeyData object) : Key data
139
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
140
+ curve_type (EllipticCurveTypes) : Elliptic curve type
141
+
142
+ Returns:
143
+ Bip32PublicKey object: Bip32PublicKey object
144
+
145
+ Raises:
146
+ Bip32KeyError: If the key constructed from the bytes is not valid
147
+ """
148
+ if isinstance(pub_key, bytes):
149
+ return cls.FromBytes(pub_key, key_data, key_net_ver, curve_type)
150
+ if isinstance(pub_key, IPoint):
151
+ return cls.FromPoint(pub_key, key_data, key_net_ver)
152
+ return cls(pub_key, key_data, key_net_ver)
153
+
154
+ @classmethod
155
+ def FromBytes(cls,
156
+ key_bytes: bytes,
157
+ key_data: Bip32KeyData,
158
+ key_net_ver: Bip32KeyNetVersions,
159
+ curve_type: EllipticCurveTypes) -> Bip32PublicKey:
160
+ """
161
+ Create from bytes.
162
+
163
+ Args:
164
+ key_bytes (bytes) : Key bytes
165
+ key_data (Bip32KeyData object) : Key data
166
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
167
+ curve_type (EllipticCurveTypes) : Elliptic curve type
168
+
169
+ Raises:
170
+ Bip32KeyError: If the key constructed from the bytes is not valid
171
+ """
172
+ return cls(cls.__KeyFromBytes(key_bytes, curve_type),
173
+ key_data,
174
+ key_net_ver)
175
+
176
+ @classmethod
177
+ def FromPoint(cls,
178
+ key_point: IPoint,
179
+ key_data: Bip32KeyData,
180
+ key_net_ver: Bip32KeyNetVersions) -> Bip32PublicKey:
181
+ """
182
+ Create from point.
183
+
184
+ Args:
185
+ key_point (IPoint object) : Key point
186
+ key_data (Bip32KeyData object) : Key data
187
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
188
+
189
+ Raises:
190
+ Bip32KeyError: If the key constructed from the bytes is not valid
191
+ """
192
+ return cls(cls.__KeyFromPoint(key_point),
193
+ key_data,
194
+ key_net_ver)
195
+
196
+ def __init__(self,
197
+ pub_key: IPublicKey,
198
+ key_data: Bip32KeyData,
199
+ key_net_ver: Bip32KeyNetVersions) -> None:
200
+ """
201
+ Construct class.
202
+
203
+ Args:
204
+ pub_key (IPublicKey object) : Key object
205
+ key_data (Bip32KeyData object) : Key data
206
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
207
+ """
208
+ super().__init__(key_data, key_net_ver, pub_key.CurveType())
209
+ self.m_pub_key = pub_key
210
+
211
+ def KeyObject(self) -> IPublicKey:
212
+ """
213
+ Return the key object.
214
+
215
+ Returns:
216
+ IPublicKey object: Key object
217
+ """
218
+ return self.m_pub_key
219
+
220
+ @lru_cache()
221
+ def RawCompressed(self) -> DataBytes:
222
+ """
223
+ Return raw compressed public key.
224
+
225
+ Returns:
226
+ DataBytes object: DataBytes object
227
+ """
228
+ return self.m_pub_key.RawCompressed()
229
+
230
+ @lru_cache()
231
+ def RawUncompressed(self) -> DataBytes:
232
+ """
233
+ Return raw uncompressed public key.
234
+
235
+ Returns:
236
+ DataBytes object: DataBytes object
237
+ """
238
+ return self.m_pub_key.RawUncompressed()
239
+
240
+ def Point(self) -> IPoint:
241
+ """
242
+ Get public key point.
243
+
244
+ Returns:
245
+ IPoint object: IPoint object
246
+ """
247
+ return self.m_pub_key.Point()
248
+
249
+ @lru_cache()
250
+ def FingerPrint(self) -> Bip32FingerPrint:
251
+ """
252
+ Get key fingerprint.
253
+
254
+ Returns:
255
+ bytes: Key fingerprint bytes
256
+ """
257
+ return Bip32FingerPrint(self.KeyIdentifier())
258
+
259
+ @lru_cache()
260
+ def KeyIdentifier(self) -> bytes:
261
+ """
262
+ Get key identifier.
263
+
264
+ Returns:
265
+ bytes: Key identifier bytes
266
+ """
267
+ return Hash160.QuickDigest(self.m_pub_key.RawCompressed().ToBytes())
268
+
269
+ @lru_cache()
270
+ def ToExtended(self) -> str:
271
+ """
272
+ Return key in serialized extended format.
273
+
274
+ Returns:
275
+ str: Key in serialized extended format
276
+ """
277
+ return Bip32PublicKeySerializer.Serialize(self.m_pub_key,
278
+ self.m_key_data,
279
+ self.m_key_net_ver)
280
+
281
+ @staticmethod
282
+ def __KeyFromBytes(key_bytes: bytes,
283
+ curve_type: EllipticCurveTypes) -> IPublicKey:
284
+ """
285
+ Construct key from bytes.
286
+
287
+ Args:
288
+ key_bytes (bytes) : Key bytes
289
+ curve_type (EllipticCurveTypes): Elliptic curve type
290
+
291
+ Returns:
292
+ IPublicKey object: IPublicKey object
293
+
294
+ Raises:
295
+ Bip32KeyError: If the key constructed from the bytes is not valid
296
+ """
297
+ try:
298
+ curve = EllipticCurveGetter.FromType(curve_type)
299
+ return curve.PublicKeyClass().FromBytes(key_bytes)
300
+ except ValueError as ex:
301
+ raise Bip32KeyError("Invalid public key bytes") from ex
302
+
303
+ @staticmethod
304
+ def __KeyFromPoint(key_point: IPoint) -> IPublicKey:
305
+ """
306
+ Construct key from point.
307
+
308
+ Args:
309
+ key_point (IPoint object): Key point
310
+
311
+ Returns:
312
+ IPublicKey object: IPublicKey object
313
+
314
+ Raises:
315
+ Bip32KeyError: If the key constructed from the bytes is not valid
316
+ """
317
+ try:
318
+ curve = EllipticCurveGetter.FromType(key_point.CurveType())
319
+ return curve.PublicKeyClass().FromPoint(key_point)
320
+ except ValueError as ex:
321
+ raise Bip32KeyError("Invalid public key point") from ex
322
+
323
+
324
+ class Bip32PrivateKey(_Bip32KeyBase):
325
+ """
326
+ BIP32 private key class.
327
+ It represents a private key used by BIP32 with all the related data (e.g. depth, chain code, etc...).
328
+ """
329
+
330
+ m_priv_key: IPrivateKey
331
+
332
+ @classmethod
333
+ def FromBytesOrKeyObject(cls,
334
+ priv_key: Union[bytes, IPrivateKey],
335
+ key_data: Bip32KeyData,
336
+ key_net_ver: Bip32KeyNetVersions,
337
+ curve_type: EllipticCurveTypes) -> Bip32PrivateKey:
338
+ """
339
+ Get the public key from key bytes or object.
340
+
341
+ Args:
342
+ priv_key (bytes or IPrivateKey) : Private key
343
+ key_data (Bip32KeyData object) : Key data
344
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
345
+ curve_type (EllipticCurveTypes) : Elliptic curve type
346
+
347
+ Returns:
348
+ Bip32PrivateKey object: Bip32PrivateKey object
349
+
350
+ Raises:
351
+ Bip32KeyError: If the key constructed from the bytes is not valid
352
+ """
353
+ return (cls.FromBytes(priv_key, key_data, key_net_ver, curve_type)
354
+ if isinstance(priv_key, bytes)
355
+ else cls(priv_key, key_data, key_net_ver))
356
+
357
+ @classmethod
358
+ def FromBytes(cls,
359
+ key_bytes: bytes,
360
+ key_data: Bip32KeyData,
361
+ key_net_ver: Bip32KeyNetVersions,
362
+ curve_type: EllipticCurveTypes) -> Bip32PrivateKey:
363
+ """
364
+ Create from bytes.
365
+
366
+ Args:
367
+ key_bytes (bytes) : Key bytes
368
+ key_data (Bip32KeyData object) : Key data
369
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
370
+ curve_type (EllipticCurveTypes) : Elliptic curve type
371
+
372
+ Raises:
373
+ Bip32KeyError: If the key constructed from the bytes is not valid
374
+ """
375
+ return cls(cls.__KeyFromBytes(key_bytes, curve_type),
376
+ key_data,
377
+ key_net_ver)
378
+
379
+ def __init__(self,
380
+ priv_key: IPrivateKey,
381
+ key_data: Bip32KeyData,
382
+ key_net_ver: Bip32KeyNetVersions) -> None:
383
+ """
384
+ Construct class.
385
+
386
+ Args:
387
+ priv_key (IPrivateKey object) : Key object
388
+ key_data (Bip32KeyData object) : Key data
389
+ key_net_ver (Bip32KeyNetVersions object): Key net versions
390
+ """
391
+ super().__init__(key_data, key_net_ver, priv_key.CurveType())
392
+ self.m_priv_key = priv_key
393
+
394
+ def KeyObject(self) -> IPrivateKey:
395
+ """
396
+ Return the key object.
397
+
398
+ Returns:
399
+ IPrivateKey object: Key object
400
+ """
401
+ return self.m_priv_key
402
+
403
+ @lru_cache()
404
+ def Raw(self) -> DataBytes:
405
+ """
406
+ Return raw private key.
407
+
408
+ Returns:
409
+ DataBytes object: DataBytes object
410
+ """
411
+ return self.m_priv_key.Raw()
412
+
413
+ @lru_cache()
414
+ def PublicKey(self) -> Bip32PublicKey:
415
+ """
416
+ Get the public key correspondent to the private one.
417
+
418
+ Returns:
419
+ Bip32PublicKey object: Bip32PublicKey object
420
+ """
421
+ return Bip32PublicKey(self.m_priv_key.PublicKey(),
422
+ self.m_key_data,
423
+ self.m_key_net_ver)
424
+
425
+ @lru_cache()
426
+ def ToExtended(self) -> str:
427
+ """
428
+ Return key in serialized extended format.
429
+
430
+ Returns:
431
+ str: Key in serialized extended format
432
+ """
433
+ return Bip32PrivateKeySerializer.Serialize(self.m_priv_key,
434
+ self.m_key_data,
435
+ self.m_key_net_ver)
436
+
437
+ @staticmethod
438
+ def __KeyFromBytes(key_bytes: bytes,
439
+ curve_type: EllipticCurveTypes) -> IPrivateKey:
440
+ """
441
+ Construct key from bytes.
442
+
443
+ Args:
444
+ key_bytes (bytes) : Key bytes
445
+ curve_type (EllipticCurveTypes): Elliptic curve type
446
+
447
+ Returns:
448
+ IPrivateKey object: IPrivateKey object
449
+
450
+ Raises:
451
+ Bip32KeyError: If the key constructed from the bytes is not valid
452
+ """
453
+ try:
454
+ curve = EllipticCurveGetter.FromType(curve_type)
455
+ return curve.PrivateKeyClass().FromBytes(key_bytes)
456
+ except ValueError as ex:
457
+ raise Bip32KeyError("Invalid private key bytes") from ex
@@ -0,0 +1,247 @@
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 paths parsing and handling."""
22
+
23
+ # Import
24
+ from __future__ import annotations
25
+
26
+ from typing import Iterator, List, Optional, Sequence, Tuple, Union
27
+
28
+ from .bip32_ex import Bip32PathError
29
+ from .bip32_key_data import Bip32KeyIndex
30
+
31
+
32
+ class Bip32PathConst:
33
+ """Class container for BIP32 path constants."""
34
+
35
+ # Hardened characters
36
+ HARDENED_CHARS: Tuple[str, str, str] = ("'", "h", "p")
37
+ # Master character
38
+ MASTER_CHAR: str = "m"
39
+
40
+
41
+ class Bip32Path:
42
+ """
43
+ BIP32 path class.
44
+ It represents a BIP-0032 path.
45
+ """
46
+
47
+ m_elems: List[Bip32KeyIndex]
48
+ m_is_absolute: bool
49
+
50
+ def __init__(self,
51
+ elems: Optional[Sequence[Union[int, Bip32KeyIndex]]] = None,
52
+ is_absolute: bool = True) -> None:
53
+ """
54
+ Construct class.
55
+
56
+ Args:
57
+ elems (list, optional) : Path elements (default: empty)
58
+ is_absolute (bool, optional): True if path is an absolute one, false otherwise (default: True)
59
+ """
60
+ try:
61
+ self.m_elems = ([]
62
+ if elems is None
63
+ else [Bip32KeyIndex(elem) if isinstance(elem, int) else elem for elem in elems])
64
+ except ValueError as ex:
65
+ raise Bip32PathError("The path contains some invalid key indexes") from ex
66
+
67
+ self.m_is_absolute = is_absolute
68
+
69
+ def AddElem(self,
70
+ elem: Union[int, Bip32KeyIndex]) -> Bip32Path:
71
+ """
72
+ Return a new path object with the specified element added.
73
+
74
+ Args:
75
+ elem (str or Bip32KeyIndex): Path element
76
+
77
+ Returns:
78
+ Bip32Path object: Bip32Path object
79
+
80
+ Raises:
81
+ Bip32PathError: If the path element is not valid
82
+ """
83
+ if isinstance(elem, int):
84
+ elem = Bip32KeyIndex(elem)
85
+ return Bip32Path(self.m_elems + [elem], self.m_is_absolute)
86
+
87
+ def IsAbsolute(self) -> bool:
88
+ """
89
+ Get if absolute path.
90
+
91
+ Returns:
92
+ bool: True if absolute path, false otherwise
93
+ """
94
+ return self.m_is_absolute
95
+
96
+ def Length(self) -> int:
97
+ """
98
+ Get the number of elements of the path.
99
+
100
+ Returns:
101
+ int: Number of elements
102
+ """
103
+ return len(self.m_elems)
104
+
105
+ def ToList(self) -> List[int]:
106
+ """
107
+ Get the path as a list of integers.
108
+
109
+ Returns:
110
+ list[int]: Path as a list of integers
111
+ """
112
+ return [int(elem) for elem in self.m_elems]
113
+
114
+ def ToStr(self) -> str:
115
+ """
116
+ Get the path as a string.
117
+
118
+ Returns:
119
+ str: Path as a string
120
+ """
121
+ path_str = "" if not self.m_is_absolute else f"{Bip32PathConst.MASTER_CHAR}/"
122
+ for elem in self.m_elems:
123
+ if not elem.IsHardened():
124
+ path_str += f"{str(elem.ToInt())}/"
125
+ else:
126
+ path_str += f"{str(Bip32KeyIndex.UnhardenIndex(elem.ToInt()))}'/"
127
+
128
+ return path_str[:-1]
129
+
130
+ def __str__(self) -> str:
131
+ """
132
+ Get the path as a string.
133
+
134
+ Returns:
135
+ str: Path as a string
136
+ """
137
+ return self.ToStr()
138
+
139
+ def __getitem__(self,
140
+ idx: int) -> Bip32KeyIndex:
141
+ """
142
+ Get the specified element index.
143
+
144
+ Args:
145
+ idx (int): Element index
146
+
147
+ Returns:
148
+ Bip32KeyIndex object: Bip32KeyIndex object
149
+ """
150
+ return self.m_elems[idx]
151
+
152
+ def __iter__(self) -> Iterator[Bip32KeyIndex]:
153
+ """
154
+ Get the iterator to the current element.
155
+
156
+ Returns:
157
+ Iterator object: Iterator to the current element
158
+ """
159
+ yield from self.m_elems
160
+
161
+
162
+ class Bip32PathParser:
163
+ """
164
+ BIP32 path parser class.
165
+ It parses a BIP-0032 path and returns a Bip32Path object.
166
+ """
167
+
168
+ @staticmethod
169
+ def Parse(path: str) -> Bip32Path:
170
+ """
171
+ Parse a path and return a Bip32Path object.
172
+
173
+ Args:
174
+ path (str): Path
175
+
176
+ Returns:
177
+ Bip32Path object: Bip32Path object
178
+
179
+ Raises:
180
+ Bip32PathError: If the path is not valid
181
+ """
182
+
183
+ # Remove trailing "/" if any
184
+ if path.endswith("/"):
185
+ path = path[:-1]
186
+
187
+ # Parse elements
188
+ return Bip32PathParser.__ParseElements(
189
+ list(filter(None, path.split("/")))
190
+ )
191
+
192
+ @staticmethod
193
+ def __ParseElements(path_elems: List[str]) -> Bip32Path:
194
+ """
195
+ Parse path elements and return a Bip32Path object.
196
+
197
+ Args:
198
+ path_elems (list[str]): Path elements
199
+
200
+ Returns:
201
+ Bip32Path object: Bip32Path object
202
+
203
+ Raises:
204
+ Bip32PathError: If the path is not valid
205
+ """
206
+
207
+ # Remove the initial "m" character if any
208
+ if len(path_elems) > 0 and path_elems[0] == Bip32PathConst.MASTER_CHAR:
209
+ path_elems = path_elems[1:]
210
+ is_absolute = True
211
+ else:
212
+ is_absolute = False
213
+
214
+ # Parse elements
215
+ parsed_elems = list(map(Bip32PathParser.__ParseElem, path_elems))
216
+ return Bip32Path(parsed_elems, is_absolute)
217
+
218
+ @staticmethod
219
+ def __ParseElem(path_elem: str) -> int:
220
+ """
221
+ Parse path element and get the correspondent index.
222
+
223
+ Args:
224
+ path_elem (str): Path element
225
+
226
+ Returns:
227
+ int: Index of the element, None if the element is not a valid index
228
+
229
+ Raises:
230
+ Bip32PathError: If the path is not valid
231
+ """
232
+
233
+ # Strip spaces
234
+ path_elem = path_elem.strip()
235
+
236
+ # Get if hardened
237
+ is_hardened = path_elem.endswith(Bip32PathConst.HARDENED_CHARS)
238
+
239
+ # If hardened, remove the last character from the string
240
+ if is_hardened:
241
+ path_elem = path_elem[:-1]
242
+
243
+ # The remaining string shall be numeric
244
+ if not path_elem.isnumeric():
245
+ raise Bip32PathError(f"Invalid path element ({path_elem})")
246
+
247
+ return int(path_elem) if not is_hardened else Bip32KeyIndex.HardenIndex(int(path_elem))