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.
- coinex/ccxt/__init__.py +1 -1
- coinex/ccxt/async_support/__init__.py +1 -1
- coinex/ccxt/async_support/base/exchange.py +139 -10
- coinex/ccxt/async_support/base/throttler.py +1 -1
- coinex/ccxt/async_support/base/ws/cache.py +1 -0
- coinex/ccxt/async_support/base/ws/client.py +26 -4
- coinex/ccxt/async_support/coinex.py +2 -2
- coinex/ccxt/base/exchange.py +587 -91
- coinex/ccxt/base/types.py +11 -2
- coinex/ccxt/coinex.py +2 -2
- coinex/ccxt/pro/__init__.py +1 -1
- coinex/ccxt/pro/coinex.py +10 -7
- coinex/ccxt/static_dependencies/bip/__init__.py +6 -0
- coinex/ccxt/static_dependencies/bip/addr/P2PKH_addr.py +205 -0
- coinex/ccxt/static_dependencies/bip/addr/__init__.py +5 -0
- coinex/ccxt/static_dependencies/bip/addr/addr_dec_utils.py +125 -0
- coinex/ccxt/static_dependencies/bip/addr/addr_key_validator.py +162 -0
- coinex/ccxt/static_dependencies/bip/addr/iaddr_decoder.py +48 -0
- coinex/ccxt/static_dependencies/bip/addr/iaddr_encoder.py +50 -0
- coinex/ccxt/static_dependencies/bip/base58/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/base58/base58.py +207 -0
- coinex/ccxt/static_dependencies/bip/base58/base58_ex.py +25 -0
- coinex/ccxt/static_dependencies/bip/base58/base58_xmr.py +155 -0
- coinex/ccxt/static_dependencies/bip/bech32/__init__.py +4 -0
- coinex/ccxt/static_dependencies/bip/bech32/bch_bech32.py +220 -0
- coinex/ccxt/static_dependencies/bip/bech32/bech32.py +235 -0
- coinex/ccxt/static_dependencies/bip/bech32/bech32_base.py +246 -0
- coinex/ccxt/static_dependencies/bip/bech32/bech32_ex.py +25 -0
- coinex/ccxt/static_dependencies/bip/bech32/segwit_bech32.py +173 -0
- coinex/ccxt/static_dependencies/bip/bip32/__init__.py +14 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/bip32_base.py +581 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/ibip32_key_derivator.py +83 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/ibip32_mst_key_generator.py +47 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_const.py +35 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_ex.py +29 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_key_data.py +500 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_key_net_ver.py +83 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_key_ser.py +294 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_keys.py +457 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_path.py +247 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_utils.py +72 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/__init__.py +4 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_ed25519.py +82 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_ed25519_key_derivator.py +118 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_key_derivator_base.py +204 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_mst_key_generator.py +119 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_key_derivator.py +200 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_mst_key_generator.py +168 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_secp256k1.py +82 -0
- coinex/ccxt/static_dependencies/bip/bip44/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/bip44/bip44.py +265 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/bip44_base.py +624 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/bip44_base_ex.py +25 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/bip44_keys.py +225 -0
- coinex/ccxt/static_dependencies/bip/coin_conf/__init__.py +2 -0
- coinex/ccxt/static_dependencies/bip/coin_conf/coin_conf.py +68 -0
- coinex/ccxt/static_dependencies/bip/coin_conf/coins_conf.py +890 -0
- coinex/ccxt/static_dependencies/bip/conf/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_coins.py +126 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_conf.py +1360 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_conf_getter.py +153 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_coins.py +53 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_conf.py +366 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_conf_getter.py +80 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_coins.py +39 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_conf.py +113 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_conf_getter.py +66 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_coins.py +37 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_conf.py +83 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_conf_getter.py +64 -0
- coinex/ccxt/static_dependencies/bip/conf/common/__init__.py +8 -0
- coinex/ccxt/static_dependencies/bip/conf/common/atom_addr.py +104 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_bitcoin_cash_conf.py +106 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_coin_conf.py +217 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_coins.py +28 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_conf_const.py +30 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_litecoin_conf.py +121 -0
- coinex/ccxt/static_dependencies/bip/ecc/__init__.py +42 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/dummy_point.py +219 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/ikeys.py +263 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/ipoint.py +190 -0
- coinex/ccxt/static_dependencies/bip/ecc/conf.py +28 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve.py +121 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve_getter.py +74 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve_types.py +37 -0
- coinex/ccxt/static_dependencies/bip/ecc/ecdsa/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/ecdsa/ecdsa_keys.py +36 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1.py +36 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_const.py +59 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_keys_ecdsa.py +248 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_point_ecdsa.py +234 -0
- coinex/ccxt/static_dependencies/bip/slip/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/slip/slip173/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/slip/slip173/slip173.py +60 -0
- coinex/ccxt/static_dependencies/bip/slip/slip32/__init__.py +4 -0
- coinex/ccxt/static_dependencies/bip/slip/slip32/slip32.py +322 -0
- coinex/ccxt/static_dependencies/bip/slip/slip32/slip32_key_net_ver.py +62 -0
- coinex/ccxt/static_dependencies/bip/slip/slip44/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/slip/slip44/slip44.py +81 -0
- coinex/ccxt/static_dependencies/bip/utils/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/utils/conf/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/utils/conf/coin_names.py +59 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/__init__.py +10 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/aes_ecb.py +152 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/blake2.py +191 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/chacha20_poly1305.py +101 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/hash160.py +57 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/hmac.py +118 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/pbkdf2.py +66 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/ripemd.py +58 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/scrypt.py +66 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/sha2.py +182 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/sha3.py +99 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/__init__.py +7 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/algo.py +108 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/base32.py +151 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/bit.py +115 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/bytes.py +200 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/data_bytes.py +181 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/integer.py +97 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/string.py +54 -0
- coinex/ccxt/static_dependencies/bip/utils/typing/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/utils/typing/literal.py +27 -0
- coinex/ccxt/static_dependencies/bip/wif/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/wif/wif.py +144 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/amino/amino_pb2.py +31 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/base/v1beta1/coin_pb2.py +47 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/multisig/keys_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/multisig/v1beta1/multisig_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/secp256k1/keys_pb2.py +34 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/msg/v1/msg_pb2.py +27 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/tx/signing/v1beta1/signing_pb2.py +38 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/tx/v1beta1/tx_pb2.py +75 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos_proto/cosmos_pb2.py +36 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/accountplus_pb2.py +31 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/genesis_pb2.py +40 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/models_pb2.py +26 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/params_pb2.py +29 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/query_pb2.py +57 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/tx_pb2.py +51 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/block_rate_limit_config_pb2.py +37 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/clob_pair_pb2.py +41 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/equity_tier_limit_config_pb2.py +35 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/finalize_block_pb2.py +27 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/liquidations_config_pb2.py +39 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/liquidations_pb2.py +38 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/matches_pb2.py +55 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/mev_pb2.py +49 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/operation_pb2.py +32 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/order_pb2.py +86 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/order_removals_pb2.py +32 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/process_proposer_matches_events_pb2.py +42 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/query_pb2.py +124 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/streaming_pb2.py +29 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/tx_pb2.py +117 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/genesis_pb2.py +26 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/query_pb2.py +26 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/transfer_pb2.py +61 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/tx_pb2.py +37 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/asset_position_pb2.py +29 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/genesis_pb2.py +30 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/perpetual_position_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/query_pb2.py +63 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/streaming_pb2.py +31 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/subaccount_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/gogoproto/gogo_pb2.py +102 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/registry.py +38 -0
- coinex/ccxt/static_dependencies/ecdsa/ellipticcurve.py +842 -0
- coinex/ccxt/static_dependencies/ecdsa/keys.py +15 -4
- coinex/ccxt/static_dependencies/mnemonic/__init__.py +4 -0
- coinex/ccxt/static_dependencies/mnemonic/mnemonic.py +282 -0
- coinex/ccxt/static_dependencies/mnemonic/py.typed +1 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/chinese_simplified.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/chinese_traditional.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/czech.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/english.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/french.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/italian.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/japanese.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/korean.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/portuguese.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/russian.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/spanish.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/turkish.txt +2048 -0
- {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/METADATA +3 -3
- {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/RECORD +197 -17
- {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))
|