Module libra.local_account

Provides LocalAccount class for holding local account private key.

LocalAccount provides operations we need for creating auth key, account address and signing raw transaction.

Expand source code
# Copyright (c) The Libra Core Contributors
# SPDX-License-Identifier: Apache-2.0

"""Provides LocalAccount class for holding local account private key.

LocalAccount provides operations we need for creating auth key, account address and signing
raw transaction.
"""

from . import (
    libra_types,
    utils,
)

from .auth_key import AuthKey
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey


class LocalAccount:
    """LocalAccount is like a wallet account

    WARN: This is handy class for creating tests for your application, but may not ideal for your
    production code, because it uses a specific implementaion of ed25519 and requires loading your
    private key into memory and hand over to code from external.
    You should always choose more secure way to handle your private key
    (e.g. https://en.wikipedia.org/wiki/Hardware_security_module) in production and do not give
    your private key to any code from external if possible.
    """

    @staticmethod
    def generate() -> "LocalAccount":
        """Generate a random private key and initialize local account"""

        private_key = Ed25519PrivateKey.generate()
        return LocalAccount(private_key)

    private_key: Ed25519PrivateKey

    def __init__(self, private_key: Ed25519PrivateKey) -> None:
        self.private_key = private_key

    @property
    def auth_key(self) -> AuthKey:
        return AuthKey.from_public_key(self.public_key)

    @property
    def account_address(self) -> libra_types.AccountAddress:
        return self.auth_key.account_address()

    @property
    def public_key_bytes(self) -> bytes:
        return utils.public_key_bytes(self.public_key)

    @property
    def public_key(self) -> Ed25519PublicKey:
        return self.private_key.public_key()

    def sign(self, txn: libra_types.RawTransaction) -> libra_types.SignedTransaction:
        """Create signed transaction for given raw transaction"""

        signature = self.private_key.sign(utils.raw_transaction_signing_msg(txn))
        return utils.create_signed_transaction(txn, self.public_key_bytes, signature)

Classes

class LocalAccount (private_key: cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PrivateKey)

LocalAccount is like a wallet account

WARN: This is handy class for creating tests for your application, but may not ideal for your production code, because it uses a specific implementaion of ed25519 and requires loading your private key into memory and hand over to code from external. You should always choose more secure way to handle your private key (e.g. https://en.wikipedia.org/wiki/Hardware_security_module) in production and do not give your private key to any code from external if possible.

Expand source code
class LocalAccount:
    """LocalAccount is like a wallet account

    WARN: This is handy class for creating tests for your application, but may not ideal for your
    production code, because it uses a specific implementaion of ed25519 and requires loading your
    private key into memory and hand over to code from external.
    You should always choose more secure way to handle your private key
    (e.g. https://en.wikipedia.org/wiki/Hardware_security_module) in production and do not give
    your private key to any code from external if possible.
    """

    @staticmethod
    def generate() -> "LocalAccount":
        """Generate a random private key and initialize local account"""

        private_key = Ed25519PrivateKey.generate()
        return LocalAccount(private_key)

    private_key: Ed25519PrivateKey

    def __init__(self, private_key: Ed25519PrivateKey) -> None:
        self.private_key = private_key

    @property
    def auth_key(self) -> AuthKey:
        return AuthKey.from_public_key(self.public_key)

    @property
    def account_address(self) -> libra_types.AccountAddress:
        return self.auth_key.account_address()

    @property
    def public_key_bytes(self) -> bytes:
        return utils.public_key_bytes(self.public_key)

    @property
    def public_key(self) -> Ed25519PublicKey:
        return self.private_key.public_key()

    def sign(self, txn: libra_types.RawTransaction) -> libra_types.SignedTransaction:
        """Create signed transaction for given raw transaction"""

        signature = self.private_key.sign(utils.raw_transaction_signing_msg(txn))
        return utils.create_signed_transaction(txn, self.public_key_bytes, signature)

Class variables

var private_key : cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PrivateKey

Static methods

def generate() ‑> LocalAccount

Generate a random private key and initialize local account

Expand source code
@staticmethod
def generate() -> "LocalAccount":
    """Generate a random private key and initialize local account"""

    private_key = Ed25519PrivateKey.generate()
    return LocalAccount(private_key)

Instance variables

var account_addressAccountAddress
Expand source code
@property
def account_address(self) -> libra_types.AccountAddress:
    return self.auth_key.account_address()
var auth_keyAuthKey
Expand source code
@property
def auth_key(self) -> AuthKey:
    return AuthKey.from_public_key(self.public_key)
var public_key : cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PublicKey
Expand source code
@property
def public_key(self) -> Ed25519PublicKey:
    return self.private_key.public_key()
var public_key_bytes : bytes
Expand source code
@property
def public_key_bytes(self) -> bytes:
    return utils.public_key_bytes(self.public_key)

Methods

def sign(self, txn: RawTransaction) ‑> SignedTransaction

Create signed transaction for given raw transaction

Expand source code
def sign(self, txn: libra_types.RawTransaction) -> libra_types.SignedTransaction:
    """Create signed transaction for given raw transaction"""

    signature = self.private_key.sign(utils.raw_transaction_signing_msg(txn))
    return utils.create_signed_transaction(txn, self.public_key_bytes, signature)