Keys and Addresses¶
There are two types of address format, "base58check" and "hex str". The base58check variant is more user-friendly.
TronPy accepts both type of address. The Base58check type is preferred.
Key API reference¶
to_base58check_address
¶
to_base58check_address(raw_addr: str | bytes) -> str
Convert hex address or base58check address to base58check address(and verify it).
Source code in tronpy/keys/__init__.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
PrivateKey
¶
PrivateKey(private_key_bytes: bytes)
flowchart TD
tronpy.keys.PrivateKey[PrivateKey]
tronpy.keys.BaseKey[BaseKey]
tronpy.keys.BaseKey --> tronpy.keys.PrivateKey
click tronpy.keys.PrivateKey href "" "tronpy.keys.PrivateKey"
click tronpy.keys.BaseKey href "" "tronpy.keys.BaseKey"
The private key.
Methods:
-
from_passphrase–Get a private key from sha256 of a passphrase.
-
fromhex–Construct a key from a hex str.
-
hex–Key as a hex str.
-
random–Generate a random private key.
-
sign_msg–Sign a raw message.
-
sign_msg_hash–Sign a message hash.
Source code in tronpy/keys/__init__.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
from_passphrase
classmethod
¶
from_passphrase(passphrase: bytes) -> PrivateKey
Get a private key from sha256 of a passphrase.
Source code in tronpy/keys/__init__.py
284 285 286 287 | |
fromhex
classmethod
¶
fromhex(hex_str: str) -> BaseKey
Construct a key from a hex str.
Returns:
-
BaseKey–The key object.
Source code in tronpy/keys/__init__.py
149 150 151 152 153 154 155 156 | |
hex
¶
hex() -> str
Key as a hex str.
Returns:
-
str–A hex str.
Source code in tronpy/keys/__init__.py
141 142 143 144 145 146 147 | |
random
classmethod
¶
random() -> PrivateKey
Generate a random private key.
Source code in tronpy/keys/__init__.py
279 280 281 282 | |
sign_msg
¶
sign_msg(message: bytes) -> Signature
Sign a raw message.
Source code in tronpy/keys/__init__.py
265 266 267 268 | |
sign_msg_hash
¶
sign_msg_hash(message_hash: bytes) -> Signature
Sign a message hash.
Source code in tronpy/keys/__init__.py
270 271 272 273 274 275 276 277 | |
PublicKey
¶
PublicKey(public_key_bytes: bytes)
flowchart TD
tronpy.keys.PublicKey[PublicKey]
tronpy.keys.BaseKey[BaseKey]
tronpy.keys.BaseKey --> tronpy.keys.PublicKey
click tronpy.keys.PublicKey href "" "tronpy.keys.PublicKey"
click tronpy.keys.BaseKey href "" "tronpy.keys.BaseKey"
The public key.
Methods:
-
fromhex–Construct a key from a hex str.
-
hex–Key as a hex str.
-
recover_from_msg–Recover public key(address) from raw message and signature.
-
recover_from_msg_hash–Recover public key(address) from message hash and signature.
-
to_base58check_address–Get the base58check address of the public key.
-
verify_msg–Verify message and signature.
-
verify_msg_hash–Verify message hash and signature.
Source code in tronpy/keys/__init__.py
198 199 200 201 202 203 204 205 206 | |
fromhex
classmethod
¶
fromhex(hex_str: str) -> BaseKey
Construct a key from a hex str.
Returns:
-
BaseKey–The key object.
Source code in tronpy/keys/__init__.py
149 150 151 152 153 154 155 156 | |
hex
¶
hex() -> str
Key as a hex str.
Returns:
-
str–A hex str.
Source code in tronpy/keys/__init__.py
141 142 143 144 145 146 147 | |
recover_from_msg
classmethod
¶
recover_from_msg(message: bytes, signature: Signature)
Recover public key(address) from raw message and signature.
Source code in tronpy/keys/__init__.py
208 209 210 211 | |
recover_from_msg_hash
classmethod
¶
recover_from_msg_hash(message_hash: bytes, signature: Signature)
Recover public key(address) from message hash and signature.
Source code in tronpy/keys/__init__.py
213 214 215 216 | |
to_base58check_address
¶
to_base58check_address() -> str
Get the base58check address of the public key.
Source code in tronpy/keys/__init__.py
227 228 229 | |
verify_msg
¶
verify_msg(message: bytes, signature: Signature) -> bool
Verify message and signature.
Source code in tronpy/keys/__init__.py
218 219 220 | |
verify_msg_hash
¶
verify_msg_hash(message_hash: bytes, signature: Signature) -> bool
Verify message hash and signature.
Source code in tronpy/keys/__init__.py
222 223 224 | |
Signature
¶
Signature(signature_bytes: bytes)
flowchart TD
tronpy.keys.Signature[Signature]
click tronpy.keys.Signature href "" "tronpy.keys.Signature"
The signature object.
Methods:
-
fromhex–Construct a Signature from hex str.
-
hex–Return signature as a hex str.
-
recover_public_key_from_msg–Recover public key(address) from message and signature.
-
recover_public_key_from_msg_hash–Recover public key(address) from message hash and signature.
-
tronweb_hex–Return signature as a hex str in TronWeb format (v+27).
-
verify_msg–Verify message and signature.
-
verify_msg_hash–Verify message hash and signature.
Source code in tronpy/keys/__init__.py
295 296 297 298 299 300 301 302 303 304 305 306 | |
fromhex
classmethod
¶
fromhex(hex_str: str) -> Signature
Construct a Signature from hex str.
Source code in tronpy/keys/__init__.py
381 382 383 384 | |
hex
¶
hex() -> str
Return signature as a hex str.
Returns:
-
str–A hex str.
Source code in tronpy/keys/__init__.py
366 367 368 369 370 371 372 | |
recover_public_key_from_msg
¶
recover_public_key_from_msg(message: bytes) -> PublicKey
Recover public key(address) from message and signature.
Source code in tronpy/keys/__init__.py
317 318 319 320 | |
recover_public_key_from_msg_hash
¶
recover_public_key_from_msg_hash(message_hash: bytes) -> PublicKey
Recover public key(address) from message hash and signature.
Source code in tronpy/keys/__init__.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
tronweb_hex
¶
tronweb_hex() -> str
Return signature as a hex str in TronWeb format (v+27).
Source code in tronpy/keys/__init__.py
374 375 376 377 378 379 | |
verify_msg
¶
verify_msg(message: bytes, public_key: PublicKey) -> bool
Verify message and signature.
Source code in tronpy/keys/__init__.py
337 338 339 340 | |
verify_msg_hash
¶
verify_msg_hash(message_hash: bytes, public_key: PublicKey) -> bool
Verify message hash and signature.
Source code in tronpy/keys/__init__.py
342 343 344 345 346 347 348 349 350 351 352 | |