KeyPair class
- class convex_api.KeyPair(private_key: Ed25519PrivateKey | None = None)
Bases:
object
Create a new keypair object with a public and private key as a Ed25519PrivateKey. It is better to use one of the following static methods to create an KeyPair object:
- Parameters:
private_key (Ed25519PrivateKey) – The public/private key as an Ed25519PrivateKey object
The Convex KeyPair class, contains the public/private keys.
To re-use the KeyPair again, you can import the keys.
Note For security reasons all of the keys and password text displayed below in the documentation are only truncated ending with a `…`
>>> # import convex-api >>> from convex_api import ConvexAPI >>> # setup the network connection >>> convex_api = ConvexAPI('https://convex.world')
- static add_0x_prefix(hexstr: str) str
Add a 0x prefix to a string.
- Parameters:
hexstr (str) – The string to add the 0x prefix to
- Returns:
The string with the 0x prefix
>>> KeyPair.add_0x_prefix('123') '0x123'
- export_to_file(filename: str, password: str | bytes) None
Export the private key to a file. This uses export_to_text to export as a string. Then saves this in a file.
- Parameters:
>>> # create a keypair >>> key_pair = KeyPair() >>> # export the private key to a file >>> key_pair.export_to_file('my_key_pair.pem', 'secret password')
- property export_to_mnemonic: str
Export the private key as a mnemonic words. You must keep this secret since the private key can be recreated using the words.
- Returns:
mnemonic word list of the private key
>>> # create a keypair >>> key_pair = KeyPair() >>> # export the private key for later use >>> print(key_pair.export_to_mnemonic()) grief stuff resemble dry frozen exercise ...
- export_to_text(password: str | bytes)
Export the private key to an encrypted PEM string.
- Parameters:
password (str) – Password to encrypt the private key value
- Returns:
The private key as a PEM formatted encrypted string
>>> # create a keypair >>> key_pair = KeyPair() >>> # export the private key for later use >>> print(key_pair.export_to_text('secret password')) -----BEGIN ENCRYPTED PRIVATE KEY----- MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAhKG+LC3hJoJQICCAAw DAYIKoZIhvcNAgkFAD ...
- static hex_to_bytes(hexstr: str) bytes
Convert a hex string to bytes.
- Parameters:
hexstr (str) – The hex string to convert
- Returns:
The hex string as bytes
>>> KeyPair.hex_to_bytes('0x123') b'\x01\x23'
- static import_from_bytes(value: bytes) KeyPair
Import an keypair from a private key in bytes.
- Returns:
KeyPair object with the private/public key
- Return type:
>>> # create an KeyPair object from a raw private key >>> key_pair = KeyPair.import_from_bytes(0x0x973f69bcd654b26475917072...)
- static import_from_file(filename: str, password: str | bytes) KeyPair
Load the encrypted private key from file. The file is saved in PEM format encrypted with a password
- Parameters:
- Returns:
KeyPair with the private/public key
- Return type:
>>> # create an KeyPair object from a encrypted pem saved in a file >>> key_pair = KeyPair.import_from_file(my_key_pair_key.pem, 'my secret password')
- static import_from_mnemonic(words: str) KeyPair
Creates a new KeyPair object using a list of words. These words contain the private key and must be kept secret.
- Parameters:
words (str) – List of mnemonic words to read
- Returns:
KeyPair object with the public/private key
- Return type:
>>> # create an KeyPair object from a mnemonic word list >>> key_pair = KeyPair.import_from_text('my word list that is the private key ..', 42)
- static import_from_text(text: str | bytes, password: str | bytes) KeyPair
Import a KeyPair from an encrypted PEM string.
- Parameters:
- Returns:
KeyPair object with the public/private key
- Return type:
>>> # create an KeyPair object from a encrypted pem text >>> pem_text = '''-----BEGIN ENCRYPTED PRIVATE KEY----- MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAi3qm1zgjCO5gICCAAw DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEENjvj1n... ''' >>> key_pair = KeyPair.import_from_text(pem_text, 'my secret password')
- is_equal(public_key_pair: KeyPair | str) bool
Compare the value to see if it is the same as this key_pair
- Param:
str, KeyPair public_key_pair: This can be a string ( public key) or a KeyPair object
- Returns:
True if the public_key_pair str or KeyPair have the same public key as this object.
- static is_hexstr(hexstr: str) bool
Check if a string is a valid hex string.
- Parameters:
hexstr (str) – The string to check
- Returns:
True if the string is a valid hex string
>>> KeyPair.is_hexstr('0x123') True >>> KeyPair.is_hexstr('0x123g') False
- static is_public_key(public_key: str) bool
Check if a public key is a valid public key.
- Parameters:
public_key (str) – The public key to check
- Returns:
True if the public key is a valid public key
- static is_public_key_checksum(public_key: str) bool
Check if a public key is a checksum key.
- Parameters:
public_key (str) – The public key to check
- Returns:
True if the public key is a checksum key
- static is_public_key_hex(public_key: str) bool
Check if a public key is a valid hex public key.
- Parameters:
public_key (str) – The public key to check
- Returns:
True if the public key is a valid hex public key
- property public_key: str
Return the public key of the KeyPair in the format ‘0x….’
- Returns:
public_key with leading ‘0x’
- Return type:
>>> # create a random KeyPair >>> key_pair = KeyPair() >>> # show the public key as a hex string >>> print(key_pair.public_key) 0x36d8c5c40dbe2d1b0131acf41c38b9d37ebe04d85...
- property public_key_api
Return the public key of the KeyPair without the leading ‘0x’
- Returns:
public_key without the leading ‘0x’
- Return type:
>>> # create a random KeyPair >>> key_pair = KeyPair() >>> # show the public key as a hex string with the leading '0x' removed >>> print(key_pair.public_key_api) 36d8c5c40dbe2d1b0131acf41c38b9d37ebe04d85...
- property public_key_bytes
Return the public key of the key pair in the byte format
- Returns:
Address in bytes
- Return type:
byte
>>> # create a keypair >>> key_pair = KeyPair() >>> # show the public key as bytes >>> print(key_pair.public_key_bytes) b'6\xd8\xc5\xc4\r\xbe-\x1b\x011\xac\xf4\x1c8..
- property public_key_checksum: str
Return the public key of the KeyPair with checksum upper/lower case characters
- Returns:
str public_key in checksum format
>>> # create a random KeyPair >>> key_pair = KeyPair() >>> # show the public key as a hex string in checksum format >>> print(key_pair.public_key_checksum) 0x36D8c5C40dbE2D1b0131ACf41c38b9D37eBe04D85...
- static remove_0x_prefix(hexstr: str) str
Remove a 0x prefix from a string.
- Parameters:
hexstr (str) – The string to remove the 0x prefix from
- Returns:
The string without the 0x prefix
>>> KeyPair.remove_0x_prefix('0x123') '123'
- sign(hash_text: str) str
Sign a hash text using the private key.
- Parameters:
hash_text (str) – Hex string of the hash to sign
- Returns:
Hex string of the signed text
>>> sig = key_pair.sign('7e2f1062f5fc51ed65a28b5945b49425aa42df6b7e67107efec357794096e05e') >>> print(sig) '5d41b964c63d1087ad66e58f4f9d3fe2b7bd0560b..'
- static to_bytes(data: int | bytes) bytes
Convert data to bytes.
>>> KeyPair.data_to_bytes(0x123) b'\x01\x23' >>> KeyPair.data_to_bytes(b'\x01\x23') b'\x01\x23'
- static to_hex(data: bytes) str
Convert bytes to a hex string.
- Parameters:
data (bytes) – The bytes to convert
- Returns:
The bytes as a hex string
>>> KeyPair.to_hex(b'\x01\x23') '0x0123'
- static to_public_key_checksum(public_key: str) str
Convert a public key to a checksum key. This will first make all a-f characters lowercase then convert a-f characters to uppercase depending on the hash of the public key.
- Parameters:
public_key (str) – The public key to convert to a checksum key
- Returns:
The checksum key of the public key