Skip to content

Keypair

KeypairType

Type of cryptography, used in Keypair instance to encrypt and sign data

  • ED25519 = 0
  • SR25519 = 1
  • ECDSA = 2

MnemonicLanguageCode

Available language codes to generate mnemonics

  • ENGLISH = 'en'
  • CHINESE_SIMPLIFIED = 'zh-hans'
  • CHINESE_TRADITIONAL = 'zh-hant'
  • FRENCH = 'fr'
  • ITALIAN = 'it'
  • JAPANESE = 'ja'
  • KOREAN = 'ko'
  • SPANISH = 'es'

Keypair

__init__(ss58_address: str = None, public_key: Union[bytes, str] = None, private_key: Union[bytes, str] = None, ss58_format: int = None, seed_hex: Union[str, bytes] = None, crypto_type: int = KeypairType.SR25519)

Allows generation of Keypairs from a variety of input combination, such as a public/private key combination, mnemonic or URI containing soft and hard derivation paths. With these Keypairs data can be signed and verified

Parameters:

Name Type Description Default
ss58_address str None
public_key Union[bytes, str] None
private_key Union[bytes, str] None
ss58_format int None
seed_hex Union[str, bytes] None
crypto_type int KeypairType.SR25519

generate_mnemonic(words: int = 12, language_code: str = MnemonicLanguageCode.ENGLISH) -> str classmethod

Generates a new seed phrase with given amount of words (default 12)

Parameters:

Name Type Description Default
words int 12
language_code str MnemonicLanguageCode.ENGLISH

Returns:

Name Type Description
str Seed phrase

validate_mnemonic(mnemonic: str, language_code: str = MnemonicLanguageCode.ENGLISH) -> bool classmethod

Verify if specified mnemonic is valid

Parameters:

Name Type Description Default
mnemonic str required
language_code str MnemonicLanguageCode.ENGLISH

Returns:

Type Description
bool

create_from_mnemonic(mnemonic: str, ss58_format = 42, crypto_type = KeypairType.SR25519, language_code: str = MnemonicLanguageCode.ENGLISH) -> Keypair classmethod

Create a Keypair for given memonic

Parameters:

Name Type Description Default
mnemonic str required
ss58_format 42
crypto_type KeypairType.SR25519
language_code str MnemonicLanguageCode.ENGLISH

Returns:

Type Description
Keypair

create_from_seed(seed_hex: Union[bytes, str], ss58_format: Optional[int] = 42, crypto_type = KeypairType.SR25519) -> Keypair classmethod

Create a Keypair for given seed

Parameters:

Name Type Description Default
seed_hex Union[bytes, str] required
ss58_format Optional[int] 42
crypto_type KeypairType.SR25519

Returns:

Type Description
Keypair

create_from_uri(suri: str, ss58_format: Optional[int] = 42, crypto_type = KeypairType.SR25519, language_code: str = MnemonicLanguageCode.ENGLISH) -> Keypair classmethod

Creates Keypair for specified suri in following format: [mnemonic]/[soft-path]//[hard-path]

Parameters:

Name Type Description Default
suri str required
ss58_format Optional[int] 42
crypto_type KeypairType.SR25519
language_code str MnemonicLanguageCode.ENGLISH

Returns:

Type Description
Keypair

create_from_private_key(private_key: Union[bytes, str], public_key: Union[bytes, str] = None, ss58_address: str = None, ss58_format: int = None, crypto_type: int = KeypairType.SR25519) -> Keypair classmethod

Creates Keypair for specified public/private keys

Parameters:

Name Type Description Default
private_key Union[bytes, str] required
public_key Union[bytes, str] None
ss58_address str None
ss58_format int None
crypto_type int KeypairType.SR25519

Returns:

Type Description
Keypair

create_from_encrypted_json(json_data: Union[str, dict], passphrase: str, ss58_format: int = None) -> Keypair classmethod

Create a Keypair from a PolkadotJS format encrypted JSON file

Parameters:

Name Type Description Default
json_data Union[str, dict] required
passphrase str required
ss58_format int None

Returns:

Type Description
Keypair

export_to_encrypted_json(passphrase: str, name: str = None) -> dict

Export Keypair to PolkadotJS format encrypted JSON file

Parameters:

Name Type Description Default
passphrase str required
name str None

Returns:

Type Description
dict

sign(data: Union[ScaleBytes, bytes, str]) -> bytes

Creates a signature for given data

Parameters:

Name Type Description Default
data Union[ScaleBytes, bytes, str] required

Returns:

Type Description
signature in bytes

verify(data: Union[ScaleBytes, bytes, str], signature: Union[bytes, str]) -> bool

Verifies data with specified signature

Parameters:

Name Type Description Default
data Union[ScaleBytes, bytes, str] required
signature Union[bytes, str] required

Returns:

Type Description
True if data is signed with this Keypair, otherwise False

encrypt_message(message: Union[bytes, str], recipient_public_key: bytes, nonce: bytes = secrets.token_bytes(24)) -> bytes

Encrypts message with for specified recipient

Parameters:

Name Type Description Default
message Union[bytes, str] required
recipient_public_key bytes required
nonce bytes secrets.token_bytes(24)

Returns:

Type Description
Encrypted message

decrypt_message(encrypted_message_with_nonce: bytes, sender_public_key: bytes) -> bytes

Decrypts message from a specified sender

Parameters:

Name Type Description Default
encrypted_message_with_nonce bytes required
sender_public_key bytes required

Returns:

Type Description
Decrypted message