示例#1
0
    def from_bytes(cls, data: bytes, validate=True, cache_binary=True):
        env = super().from_bytes(data=data, validate=validate)

        if cache_binary:
            set_lazy_property(env, 'serialize', data)

        return env
示例#2
0
    def create_from_message(cls,
                            message: MessageBase,
                            signing_key: str,
                            verifying_key: str = None,
                            uuid: int = -1):
        assert issubclass(
            type(message),
            MessageBase), "message arg must be a MessageBase subclass"
        assert type(message) in MessageBase.registry, "Message type {} not found in registry {}"\
            .format(type(message), MessageBase.registry)
        # TODO -- verify sk (valid hex, 128 char)

        # Create MessageMeta
        t = MessageBase.registry[type(message)]
        timestamp = str(time.time())
        meta = MessageMeta.create(type=t, timestamp=timestamp, uuid=uuid)

        # Create Seal
        if not verifying_key:
            verifying_key = Constants.Protocol.Wallets.get_vk(signing_key)
        seal_sig = EnvelopeAuth.seal(signing_key=signing_key,
                                     meta=meta,
                                     message=message)
        seal = Seal.create(signature=seal_sig, verifying_key=verifying_key)

        # Create Envelope
        obj = cls.create_from_objects(seal=seal,
                                      meta=meta,
                                      message=message.serialize())
        set_lazy_property(obj, 'message', message)

        return obj
示例#3
0
    def create_from_message(cls, message: MessageBase, signing_key: str, verifying_key: str=None, uuid: int=-1):
        """
        Creates an Envelope to package a MessageBase instance

        :param message: The MessageBase instance to create an envelope for
        :param signing_key: The sender's signing key, which is used to create the Seal.
        :param verifying_key: The sender's verifying key. This should be passed in for computational efficiency, but
        can be computed from the signing key if it is ommited
        :param uuid: The UUID to use for the Envelope's MessageMeta. If -1, a random UUID will be generated.
        :return: An Envelope instance
        """
        assert issubclass(type(message), MessageBase), "message arg must be a MessageBase subclass"
        assert type(message) in MessageBase.registry, "Message type {} not found in registry {}"\
            .format(type(message), MessageBase.registry)
        # TODO -- verify sk (valid hex, 128 char)

        # Create MessageMeta
        t = MessageBase.registry[type(message)]
        timestamp = str(time.time())
        meta = MessageMeta.create(type=t, timestamp=timestamp, uuid=uuid)

        # Create Seal
        if not verifying_key:
            verifying_key = Constants.Protocol.Wallets.get_vk(signing_key)
        seal_sig = EnvelopeAuth.seal(signing_key=signing_key, meta=meta, message=message)
        seal = Seal.create(signature=seal_sig, verifying_key=verifying_key)

        # Create Envelope
        obj = cls._create_from_objects(seal=seal, meta=meta, message=message.serialize())
        set_lazy_property(obj, 'message', message)

        return obj
示例#4
0
    def create(cls, signatures: List[MerkleSignature],
               merkle_leaves: List[str], prev_block_hash: str):
        """
        Creates a new block contender. Created by delegates to propose a block to Masternodes.
        :param signatures: A list of MerkleSignature objects
        :param merkle_leaves: A list merkle leaves contained within this proposed block. Each leaf is a byte string
        :param prev_block_hash: The hash of the previous (parent) block upon which this proposed block would build upon
        :return: A BlockContender object
        """
        # Serialize list of signatures
        sigs_binary = []

        for sig in signatures:
            assert isinstance(
                sig, MerkleSignature
            ), "signatures must be a list of MerkleSignatures"
            sigs_binary.append(sig.serialize())

        data = {
            cls.SIGS: sigs_binary,
            cls.LEAVES: merkle_leaves,
            cls.PREV_BLOCK: prev_block_hash
        }
        obj = cls.from_data(data)

        set_lazy_property(obj, 'signatures', signatures)

        return obj
示例#5
0
    def _create_from_objects(cls, seal: Seal, meta: MessageMeta, message: bytes):
        assert type(message) is bytes, "Message arg must be bytes"
        data = envelope_capnp.Envelope.new_message()
        data.seal = seal._data
        data.meta = meta._data
        data.message = message

        obj = cls.from_data(data)

        set_lazy_property(obj, 'seal', seal)
        set_lazy_property(obj, 'meta', meta)

        return obj
示例#6
0
    def create(cls, signatures: list, nodes: list):
        # Serialize list of signatures
        sigs_binary = []

        for sig in signatures:
            assert isinstance(
                sig, MerkleSignature
            ), "signatures must be a list of MerkleSignatures"
            sigs_binary.append(sig.serialize())

        data = {cls.SIGS: sigs_binary, cls.NODES: nodes}
        obj = cls.from_data(data)

        set_lazy_property(obj, 'signatures', signatures)

        return obj
示例#7
0
    def create(cls, signatures: List[MerkleSignature], merkle_leaves: List[str]):
        """
        Creates a new block contender. Created by delegates to propose a block to Masternodes.
        :param signatures: A list of MerkleSignature objects
        :param merkle_leaves: A list merkle leaves contained within this proposed block. Each leaf is a byte string
        :return: A BlockContender object
        """
        # Serialize list of signatures
        sigs_binary = []

        for sig in signatures:
            assert isinstance(sig, MerkleSignature), "signatures must be a list of MerkleSignatures"
            sigs_binary.append(sig.serialize())

        data = {cls.SIGS: sigs_binary, cls.LEAVES: merkle_leaves}
        obj = cls.from_data(data)

        set_lazy_property(obj, 'signatures', signatures)

        return obj