duniterpy.key package

Submodules

duniterpy.key.ascii_armor module

class duniterpy.key.ascii_armor.AsciiArmor

Bases: object

Class to handle writing and parsing of ascii armor messages

static create(message: str, pubkey: str | None = None, signing_keys: List[SigningKey] | None = None, message_comment: str | None = None, signatures_comment: str | None = None) str

Encrypt a message in ascii armor format, optionally signing it

Parameters:
  • message – Utf-8 message

  • pubkey – Public key of recipient for encryption

  • signing_keys – Optional list of SigningKey instances

  • message_comment – Optional message comment field

  • signatures_comment – Optional signatures comment field

Returns:

static parse(ascii_armor_message: str, signing_key: SigningKey | None = None, sender_pubkeys: List[str] | None = None) dict

Return a dict with parsed content (decrypted message, signature validation)

 {
    'message':
        {
            'fields': {},
            'content': str,
         },
    'signatures': [
        {'pubkey': str, 'valid': bool, fields: {}}
    ]
}
Parameters:
  • ascii_armor_message – The Ascii Armor Message Block including BEGIN and END headers

  • signing_key – Optional Libnacl SigningKey instance to decrypt message

  • sender_pubkeys – Optional sender’s public keys list to verify signatures

Raises:
  • libnacl.CryptError – Raise an exception if keypair fail to decrypt the message

  • MissingSigningKeyException – Raise an exception if no keypair given for encrypted message

Returns:

exception duniterpy.key.ascii_armor.MissingPublickeyAndSigningKeyException

Bases: Exception

Raise when the message created is not encrypted and not signed…

exception duniterpy.key.ascii_armor.ParserMissingPublicKeysException

Bases: Exception

Raise when there is at least one signature but no public keys are provided

exception duniterpy.key.ascii_armor.ParserMissingSigningKeyException

Bases: Exception

Raise when the message is encrypted but no SigningKey instance is provided

duniterpy.key.base58 module

class duniterpy.key.base58.Base58Encoder

Bases: object

static decode(data: str) bytes

Decode Base58 string data and return bytes

Parameters:

data – Base58 string

static encode(data: str | bytes) str

Return Base58 string from data

Parameters:

data – Bytes or string data

duniterpy.key.constants module

duniterpy.key.crc_pubkey module

class duniterpy.key.crc_pubkey.CRCPubkey(pubkey: str, crc: str)

Bases: object

Class to implement a crc on a pubkey

classmethod from_pubkey(pubkey: str) CRCPubkeyType

Return CRCPubkey instance from public key string

Parameters:

pubkey – Public key

Returns:

classmethod from_str(crc_pubkey: str) CRCPubkeyType

Return CRCPubkey instance from CRC public key string

Parameters:

crc_pubkey – CRC public key

Returns:

is_valid() bool

Return True if CRC is valid :return:

re_crc_pubkey = re.compile('((?![OIl])[1-9A-Za-z]{42,45}):([A-Za-z0-9]{3})')
exception duniterpy.key.crc_pubkey.CRCPubkeyException

Bases: Exception

duniterpy.key.encryption_key module

class duniterpy.key.encryption_key.PublicKey(pubkey: str)

Bases: PublicKey

base58() str

Return a base58 encoded string of the public key

encrypt_seal(data: str | bytes) bytes

Encrypt data with a curve25519 version of the ed25519 public key

Parameters:

data – Bytes data to encrypt

class duniterpy.key.encryption_key.SecretKey(salt: str | bytes, password: str | bytes, scrypt_params: ScryptParams | None = None)

Bases: SecretKey

Raw Public Key Encryption Class

decrypt(pubkey: str, nonce: str | bytes, text: str) str

Decrypt encrypted message text with recipient public key and the unique nonce used by the sender.

Parameters:
  • pubkey – Public key of the recipient

  • nonce – Unique nonce used by the sender

  • text – Encrypted message

Returns:

encrypt(pubkey: str, nonce: str | bytes, text: str | bytes) str

Encrypt message text with the public key of the recipient and a nonce

The nonce must be a 24 character string (you can use libnacl.utils.rand_nonce() to get one) and unique for each encrypted message.

Return base58 encoded encrypted message

Parameters:
  • pubkey – Base58 encoded public key of the recipient

  • nonce – Unique nonce

  • text – Message to encrypt

Returns:

duniterpy.key.scrypt_params module

class duniterpy.key.scrypt_params.ScryptParams(n: int = 4096, r: int = 16, p: int = 1, seed_length: int = 32)

Bases: object

Class to simplify handling of scrypt parameters

duniterpy.key.signing_key module

class duniterpy.key.signing_key.SigningKey(seed: bytes)

Bases: Signer

decrypt_seal(data: bytes) bytes

Decrypt bytes data with a curve25519 version of the ed25519 key pair

Parameters:

data – Encrypted data

Returns:

classmethod from_credentials(salt: str | bytes, password: str | bytes, scrypt_params: ScryptParams | None = None) SigningKeyType

Create a SigningKey object from credentials

Parameters:
  • salt – Secret salt passphrase credential

  • password – Secret password credential

  • scrypt_params – ScryptParams instance or None

classmethod from_credentials_file(path: Path | str, scrypt_params: ScryptParams | None = None) SigningKeyType

Create a SigningKey object from a credentials file

A file with the salt on the first line and the password on the second line

Parameters:
  • path – Credentials file path

  • scrypt_params – ScryptParams instance or None

Returns:

classmethod from_dubp_mnemonic(mnemonic: str, scrypt_params: ScryptParams | None = None) SigningKeyType

Generate key pair instance from a DUBP mnemonic passphrase

See https://git.duniter.org/documents/rfcs/blob/dubp-mnemonic/rfc/0014_Dubp_Mnemonic.md

Parameters:
  • mnemonic – Passphrase generated from a mnemonic algorithm

  • scrypt_params – ScryptParams instance (default=None)

Returns:

static from_ewif_file(path: Path | str, password: str) Type[SigningKeyType]

Return SigningKey instance from Duniter EWIF file

Parameters:
  • path – Path to EWIF file

  • password – Password of the encrypted seed

classmethod from_ewif_hex(ewif_hex: str, password: str) SigningKeyType

Return SigningKey instance from Duniter EWIF in hexadecimal format

Parameters:
  • ewif_hex – EWIF string in hexadecimal format

  • password – Password of the encrypted seed

static from_private_key(path: Path | str) Type[SigningKeyType]

Read authentication file Add public key attribute

Parameters:

path – Authentication file path

classmethod from_pubsec_file(path: Path | str) SigningKeyType

Return SigningKey instance from Duniter WIF file

Parameters:

path – Path to WIF file

classmethod from_seedhex(seedhex: str) SigningKeyType

Return SigningKey instance from Seedhex

Parameters:

seedhex (str) – Hexadecimal seed string

static from_seedhex_file(path: Path | str) Type[SigningKeyType]

Return SigningKey instance from Seedhex file

Parameters:

path – Hexadecimal seed file path

classmethod from_ssb_file(path: str) SigningKeyType

Return SigningKey instance from ScuttleButt .ssb/secret file

Parameters:

path – Path to Scuttlebutt secret file

static from_wif_file(path: Path | str) Type[SigningKeyType]

Return SigningKey instance from Duniter WIF file

Parameters:

path – Path to WIF file

classmethod from_wif_hex(wif_hex: str) SigningKeyType

Return SigningKey instance from Duniter WIF in hexadecimal format

Parameters:

wif_hex – WIF string in hexadecimal format

static from_wif_or_ewif_file(path: Path | str, password: str | None = None) Type[SigningKeyType]

Return SigningKey instance from Duniter WIF or EWIF file

Parameters:
  • path – Path to WIF of EWIF file

  • password – Password needed for EWIF file

static from_wif_or_ewif_hex(wif_hex: str, password: str | None = None) Type[SigningKeyType]

Return SigningKey instance from Duniter WIF or EWIF in hexadecimal format

Parameters:
  • wif_hex – WIF or EWIF string in hexadecimal format

  • password – Password of EWIF encrypted seed

save_ewif_file(path: Path | str, password: str) None

Save an Encrypted Wallet Import Format file (WIF v2)

Parameters:
  • path – Path to file

  • password

save_private_key(path: Path | str) None

Save authentication file

Parameters:

path – Authentication file path

save_pubsec_file(path: Path | str) None

Save a Duniter PubSec file (PubSec) v1

Parameters:

path – Path to file

save_seedhex_file(path: Path | str) None

Save hexadecimal seed file from seed

Parameters:

path – Authentication file path

save_wif_file(path: Path | str) None

Save a Wallet Import Format file (WIF) v1

Parameters:

path – Path to file

exception duniterpy.key.signing_key.SigningKeyException

Bases: Exception

duniterpy.key.signing_key.opener_user_rw(path, flags)

duniterpy.key.verifying_key module

class duniterpy.key.verifying_key.VerifyingKey(pubkey: str)

Bases: Verifier

Class to verify documents

check_signature(data: str, signature: str) bool

Check if data signature is valid and from self.pubkey

Parameters:
  • data – Data to check

  • signature – Signature to check

Returns:

get_verified_data(data: bytes) bytes

Check specified signed data signature and return data without signature

Raise exception if signature is not valid

Parameters:

data – Data + signature

Returns:

Module contents