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)
| 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 | |
| 92 | class WifDecoder: |
no test coverage detected