MCPcopy Create free account
hub / github.com/ccxt/ccxt / Encode

Method Encode

python/ccxt/static_dependencies/bip/wif/wif.py:51–89  ·  view source on GitHub ↗

Encode key bytes into a WIF string. Args: priv_key (bytes or IPrivateKey) : Private key bytes or object net_ver (bytes, optional) : Net version (Bitcoin main net by default) pub_key_mode (WifPubKeyModes, optional): Specify if

(priv_key: Union[bytes, IPrivateKey],
               net_ver: bytes = CoinsConf.BitcoinMainNet.ParamByKey("wif_net_ver"),
               pub_key_mode: WifPubKeyModes = WifPubKeyModes.COMPRESSED)

Source from the content-addressed store, hash-verified

49
50 @staticmethod
51 def Encode(priv_key: Union[bytes, IPrivateKey],
52 net_ver: bytes = CoinsConf.BitcoinMainNet.ParamByKey("wif_net_ver"),
53 pub_key_mode: WifPubKeyModes = WifPubKeyModes.COMPRESSED) -> str:
54 """
55 Encode key bytes into a WIF string.
56
57 Args:
58 priv_key (bytes or IPrivateKey) : Private key bytes or object
59 net_ver (bytes, optional) : Net version (Bitcoin main net by default)
60 pub_key_mode (WifPubKeyModes, optional): Specify if the private key corresponds to a compressed public key
61
62 Returns:
63 str: WIF encoded string
64
65 Raises:
66 TypeError: If pub_key_mode is not a WifPubKeyModes enum or
67 the private key is not a valid Secp256k1PrivateKey
68 ValueError: If the key is not valid
69 """
70 if not isinstance(pub_key_mode, WifPubKeyModes):
71 raise TypeError("Public key mode is not an enumerative of WifPubKeyModes")
72
73 # Convert to private key to check if bytes are valid
74 if isinstance(priv_key, bytes):
75 priv_key = Secp256k1PrivateKey.FromBytes(priv_key)
76 elif not isinstance(priv_key, Secp256k1PrivateKey):
77 raise TypeError("A secp256k1 private key is required")
78
79 priv_key = priv_key.Raw().ToBytes()
80
81 # Add suffix if correspond to a compressed public key
82 if pub_key_mode == WifPubKeyModes.COMPRESSED:
83 priv_key += WifConst.COMPR_PUB_KEY_SUFFIX
84
85 # Add net address version
86 priv_key = net_ver + priv_key
87
88 # Encode key
89 return Base58Encoder.CheckEncode(priv_key)
90
91
92class WifDecoder:

Callers 3

EncodeKeyMethod · 0.45
SerializeMethod · 0.45
EncodeKeyMethod · 0.45

Calls 5

ParamByKeyMethod · 0.80
CheckEncodeMethod · 0.80
FromBytesMethod · 0.45
ToBytesMethod · 0.45
RawMethod · 0.45

Tested by

no test coverage detected