예제 #1
0
def createSend(
    rpc: RPC,
    inputs: List[Union[Claim, Send]],
    to: bytes,
    key: ed25519.SigningKey = ed25519.SigningKey(b'\0' * 32)) -> Send:
    pub: bytes = key.get_verifying_key().to_bytes()
    actualInputs: List[Tuple[bytes, int]] = []
    outputs: List[Tuple[bytes, int]] = [(to, 1)]
    toSpend: int = 0
    for txInput in inputs:
        if isinstance(txInput, Claim):
            actualInputs.append((txInput.hash, 0))
            toSpend += txInput.amount
        else:
            for n in range(len(txInput.outputs)):
                if txInput.outputs[n][0] == key.get_verifying_key().to_bytes():
                    actualInputs.append((txInput.hash, n))
                    toSpend += txInput.outputs[n][1]
    if toSpend > 1:
        outputs.append((pub, toSpend - 1))

    send: Send = Send(actualInputs, outputs)
    send.sign(key)
    send.beat(SpamFilter(3))
    if rpc.meros.liveTransaction(send) != rpc.meros.live.recv():
        raise TestError("Meros didn't broadcast back a Send.")
    return send
예제 #2
0
class EdDSA(KeyDto, IRemmeKeys):
    """
    EdDSA (ed25519) class implementation.

    References::
        - https://github.com/warner/python-ed25519
    """

    def __init__(self, private_key, public_key):
        """
        Constructor for EdDSA key pair.
        If only private key available then public key will be generate from private.

        Args:
            private_key (bytes): ed25519 private key
            public_key (bytes, optional): ed25519 public key
        """
        super(EdDSA, self).__init__()

        if private_key and public_key:
            self._private_key = private_key
            self._public_key = public_key
            self._private_key_obj = SigningKey(self._private_key)
            self._public_key_obj = VerifyingKey(self._public_key)

        elif private_key:
            self._private_key = private_key
            self._private_key_obj = SigningKey(self._private_key)

            self._public_key_obj = self._private_key_obj.get_verifying_key()
            self._public_key = self._public_key_obj.to_bytes()

        elif public_key:
            self._public_key = public_key
            self._public_key_obj = VerifyingKey(self._public_key)

        if self._private_key:
            self._private_key_hex = self._private_key.hex()

        self._public_key_hex = self._public_key.hex()

        self._address = generate_address(
            _family_name=RemmeFamilyName.PUBLIC_KEY.value,
            _public_key_to=self._public_key,
        )
        self._key_type = KeyType.EdDSA

    @staticmethod
    def generate_key_pair(seed=None):
        """
        Generate public and private key pair.

        Args:
            seed (bytes, optional): seed

        Returns:
            Generated key pair in bytes.
        """
        if seed:
            private_key_obj, public_key_obj = ed25519.SigningKey(seed), ed25519.VerifyingKey(seed)
            return private_key_obj.to_bytes(), public_key_obj.to_bytes()

        private_key_obj, public_key_obj = ed25519.create_keypair(entropy=os.urandom)
        return private_key_obj.to_bytes(), public_key_obj.to_bytes()

    @staticmethod
    def get_address_from_public_key(public_key):
        """
        Get address from public key.
        Args:
            public_key (bytes): public key in bytes

        Returns:
            Address in blockchain generated from public key string.
        """
        return generate_address(RemmeFamilyName.PUBLIC_KEY.value, public_key)

    def sign(self, data, rsa_signature_padding=None):
        """
        Sign provided data with selected key implementation.

        Args:
            data (str): data string which will be signed
            rsa_signature_padding (RsaSignaturePadding, optional): not used in EdDSA

        Returns:
            Hex string of signature.
        """
        if self._private_key_obj is None:
            raise Exception('Private key is not provided!')

        if isinstance(data, str):
            data = utf8_to_bytes(data)

        return self._private_key_obj.sign(msg=hashlib.sha256(data).digest())

    def verify(self, data, signature, rsa_signature_padding=None):
        """
        Verify signature for selected key implementation.

        Args:
            data (str): data string which will be verified
            signature (str): hex string of signature
            rsa_signature_padding (RsaSignaturePadding, optional): not used in EdDSA

        Returns:
            Boolean ``True`` if signature is correct, or ``False`` if invalid.
        """
        if isinstance(data, str):
            data = utf8_to_bytes(data)

        try:
            self._public_key_obj.verify(
                sig=signature,
                msg=hashlib.sha256(data).digest(),
            )
            return True

        except ed25519.BadSignatureError:
            return False
예제 #3
0
 def sign(self, priv: ed25519.SigningKey):
     self.pubkey = priv.get_verifying_key()
     self.signature = priv.sign(self.hash())
ERRORS = 0

UBIRCH_CLIENT = os.getenv("UBIRCH_CLIENT")
if UBIRCH_CLIENT and not UBIRCH_CLIENT.strip():
    UBIRCH_CLIENT = None

UBIRCH_ENV = os.getenv("UBIRCH_ENV")
UBIRCH_AUTH = os.getenv("UBIRCH_AUTH")

# test UUID
uuid = UUID(hex=os.getenv('UBIRCH_DEVICE_UUID',
                          "00000000-0000-0000-0000-000000000000"))

sk = SigningKey(binascii.unhexlify(os.getenv('UBIRCH_PRIV_KEY')))
vk = sk.get_verifying_key()

logger.debug("UBIRCH_CLIENT        = '{}'".format(UBIRCH_CLIENT))
logger.debug("UBIRCH_ENV           = '{}'".format(UBIRCH_ENV))
logger.debug("UBIRCH_AUTH          = '{}'".format(UBIRCH_AUTH))
logger.debug("UBIRCH_DEVICE_UUID   = '{}'".format(uuid))

NAGIOS_OK = 0
NAGIOS_WARNING = 1
NAGIOS_ERROR = 2
NAGIOS_UNKNOWN = 3


def nagios(client, env, service, code, message="OK"):
    global ERRORS