示例#1
0
def make_transfer_record_transaction(transaction_signer,
                                     batch_signer,
                                     receiving_agent,
                                     record_id,
                                     timestamp):
    
    sending_agent_address = addresser.get_agent_address(
        transaction_signer.get_public_key().as_hex())
    receiving_agent_address = addresser.get_agent_address(receiving_agent)
    record_address = addresser.get_record_address(record_id)

    inputs = [sending_agent_address, receiving_agent_address, record_address]

    outputs = [record_address]

    action = payload_pb2.TransferRecordAction(
        record_id=record_id,
        receiving_agent=receiving_agent)

    payload = payload_pb2.ScPayload(
        action=payload_pb2.ScPayload.TRANSFER_RECORD,
        transfer_record=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(
        payload_bytes=payload_bytes,
        inputs=inputs,
        outputs=outputs,
        transaction_signer=transaction_signer,
        batch_signer=batch_signer)
示例#2
0
def make_create_agent_transaction(transaction_signer,
                                  batch_signer,
                                  name,
                                  timestamp,
                                  role):

    agent_address = addresser.get_agent_address(
        transaction_signer.get_public_key().as_hex())

    inputs = [agent_address]

    outputs = [agent_address]

    action = payload_pb2.CreateAgentAction(name=name, role=role)

    payload = payload_pb2.ScPayload(
        action=payload_pb2.ScPayload.CREATE_AGENT,
        create_agent=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(
        payload_bytes=payload_bytes,
        inputs=inputs,
        outputs=outputs,
        transaction_signer=transaction_signer,
        batch_signer=batch_signer)
示例#3
0
def make_create_agent_transaction(transaction_signer, batch_signer, name,
                                  timestamp):
    """Make a CreateAgentAction transaction and wrap it in a batch

    Args:
        transaction_signer (sawtooth_signing.Signer): The transaction key pair
        batch_signer (sawtooth_signing.Signer): The batch key pair
        name (str): The agent's name
        timestamp (int): Unix UTC timestamp of when the agent is created

    Returns:
        batch_pb2.Batch: The transaction wrapped in a batch

    """

    agent_address = addresser.get_agent_address(
        transaction_signer.get_public_key().as_hex())

    inputs = [agent_address]

    outputs = [agent_address]

    action = payload_pb2.CreateAgentAction(name=name)

    payload = payload_pb2.SimpleSupplyPayload(
        action=payload_pb2.SimpleSupplyPayload.CREATE_AGENT,
        create_agent=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(payload_bytes=payload_bytes,
                       inputs=inputs,
                       outputs=outputs,
                       transaction_signer=transaction_signer,
                       batch_signer=batch_signer)
示例#4
0
def make_finalize_record_transaction(transaction_signer,
                                   batch_signer,
                                   record_id,
                                   status,
                                   remarks,
                                   timestamp):
    
    agent_address = addresser.get_agent_address(
        transaction_signer.get_public_key().as_hex())
    record_address = addresser.get_record_address(record_id)

    inputs = [agent_address, record_address]

    outputs = [record_address]

    action = payload_pb2.FinalizeRecordAction(
        record_id=record_id,
        status=status,
        remarks=remarks)

    payload = payload_pb2.ScPayload(
        action=payload_pb2.ScPayload.FINALIZE_RECORD,
        finalize_record=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(
        payload_bytes=payload_bytes,
        inputs=inputs,
        outputs=outputs,
        transaction_signer=transaction_signer,
        batch_signer=batch_signer)
示例#5
0
def make_create_document_transaction(transaction_signer, batch_signer,
                                     file_name, document_hash, timestamp):
    """Make a CreateDocumentAction transaction and wrap it in a batch

    Args:
        transaction_signer (sawtooth_signing.Signer): The transaction key pair
        batch_signer (sawtooth_signing.Signer): The batch key pair        
        document_id (str): Unique ID of the record
        timestamp (int): Unix UTC timestamp of when the agent is created

    Returns:
        batch_pb2.Batch: The transaction wrapped in a batch
    """

    inputs = [
        addresser.get_agent_address(
            transaction_signer.get_public_key().as_hex()), document_hash
    ]

    outputs = [document_hash]

    action = payload_pb2.CreateDocumentAction(document_hash=document_hash,
                                              file_name=file_name)

    payload = payload_pb2.SimpleSupplyPayload(
        action=payload_pb2.SimpleSupplyPayload.CREATE_DOCUMENT,
        create_document=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(payload_bytes=payload_bytes,
                       inputs=inputs,
                       outputs=outputs,
                       transaction_signer=transaction_signer,
                       batch_signer=batch_signer)
示例#6
0
def make_transfer_record_transaction(transaction_signer, batch_signer,
                                     receiving_agent, record_id, timestamp):
    """Make a CreateRecordAction transaction and wrap it in a batch

    Args:
        transaction_signer (sawtooth_signing.Signer): The transaction key pair
        batch_signer (sawtooth_signing.Signer): The batch key pair
        receiving_agent (str): Public key of the agent receiving the record
        record_id (str): Unique ID of the record
        timestamp (int): Unix UTC timestamp of when the record is transferred

    Returns:
        batch_pb2.Batch: The transaction wrapped in a batch
    """
    sending_agent_address = addresser.get_agent_address(
        transaction_signer.get_public_key().as_hex())
    receiving_agent_address = addresser.get_agent_address(receiving_agent)
    record_address = addresser.get_record_address(record_id)

    inputs = [sending_agent_address, receiving_agent_address, record_address]

    outputs = [record_address]

    action = payload_pb2.TransferRecordAction(record_id=record_id,
                                              receiving_agent=receiving_agent)

    payload = payload_pb2.SimpleSupplyPayload(
        action=payload_pb2.SimpleSupplyPayload.TRANSFER_RECORD,
        transfer_record=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(payload_bytes=payload_bytes,
                       inputs=inputs,
                       outputs=outputs,
                       transaction_signer=transaction_signer,
                       batch_signer=batch_signer)
示例#7
0
def make_create_record_transaction(transaction_signer,
                                   batch_signer,
                                   record_id,
                                   description,
                                   price,
                                   quantity,
                                   units,
                                   parent_id,
                                   other_fields,
                                   remarks,
                                   new,
                                   timestamp):


    inputs = [
        addresser.get_agent_address(
            transaction_signer.get_public_key().as_hex()),
        addresser.get_record_address(record_id)
    ]

    outputs = [addresser.get_record_address(record_id)]

    action = payload_pb2.CreateRecordAction(
        record_id=record_id,
        description=description,
        price=price,
        quantity=quantity,
        units=units,
        parent_id=parent_id,
        other_fields=other_fields,
        remarks=remarks,
        new=new
        )

    payload = payload_pb2.ScPayload(
        action=payload_pb2.ScPayload.CREATE_RECORD,
        create_record=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(
        payload_bytes=payload_bytes,
        inputs=inputs,
        outputs=outputs,
        transaction_signer=transaction_signer,
        batch_signer=batch_signer)
示例#8
0
    def get_agent(self, public_key):
        """Gets the agent associated with the public_key

        Args:
            public_key (str): The public key of the agent

        Returns:
            agent_pb2.Agent: Agent with the provided public_key
        """
        address = addresser.get_agent_address(public_key)
        state_entries = self._context.get_state(addresses=[address],
                                                timeout=self._timeout)
        if state_entries:
            container = agent_pb2.AgentContainer()
            container.ParseFromString(state_entries[0].data)
            for agent in container.entries:
                if agent.public_key == public_key:
                    return agent

        return None
示例#9
0
def make_update_record_transaction(transaction_signer,
                                   batch_signer,
                                   record_id,
                                   description,
                                   price,
                                   quantity,
                                   units,
                                   parent_id,
                                   other_fields,
                                   timestamp):
    
    agent_address = addresser.get_agent_address(
        transaction_signer.get_public_key().as_hex())
    record_address = addresser.get_record_address(record_id)

    inputs = [agent_address, record_address]

    outputs = [record_address]

    action = payload_pb2.UpdateRecordAction(
        record_id=record_id,
        description=description,
        price=price,
        quantity=quantity,
        units=units,
        parent_id=parent_id,
        other_fields=other_fields)

    payload = payload_pb2.ScPayload(
        action=payload_pb2.ScPayload.UPDATE_RECORD,
        update_record=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(
        payload_bytes=payload_bytes,
        inputs=inputs,
        outputs=outputs,
        transaction_signer=transaction_signer,
        batch_signer=batch_signer)
示例#10
0
def make_create_record_transaction(transaction_signer, batch_signer, latitude,
                                   longitude, record_id, timestamp):
    """Make a CreateRecordAction transaction and wrap it in a batch

    Args:
        transaction_signer (sawtooth_signing.Signer): The transaction key pair
        batch_signer (sawtooth_signing.Signer): The batch key pair
        latitude (int): Initial latitude of the record
        longitude (int): Initial latitude of the record
        record_id (str): Unique ID of the record
        timestamp (int): Unix UTC timestamp of when the agent is created

    Returns:
        batch_pb2.Batch: The transaction wrapped in a batch
    """

    inputs = [
        addresser.get_agent_address(
            transaction_signer.get_public_key().as_hex()),
        addresser.get_record_address(record_id)
    ]

    outputs = [addresser.get_record_address(record_id)]

    action = payload_pb2.CreateRecordAction(record_id=record_id,
                                            latitude=latitude,
                                            longitude=longitude)

    payload = payload_pb2.SimpleSupplyPayload(
        action=payload_pb2.SimpleSupplyPayload.CREATE_RECORD,
        create_record=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(payload_bytes=payload_bytes,
                       inputs=inputs,
                       outputs=outputs,
                       transaction_signer=transaction_signer,
                       batch_signer=batch_signer)
示例#11
0
def make_is_valid_document_transaction(transaction_signer, batch_signer,
                                       document_id, document_hash,
                                       new_document_hash, timestamp):
    """Make a IsValidDocumentAction transaction and wrap it in a batch

    Args:
        transaction_signer (sawtooth_signing.Signer): The transaction key pair
        batch_signer (sawtooth_signing.Signer): The batch key pair        
        document_id (str): Unique ID of the document
        timestamp (int): Unix UTC timestamp of when the agent is created

    Returns:
        batch_pb2.Batch: The transaction wrapped in a batch
    """
    agent_address = addresser.get_agent_address(
        transaction_signer.get_public_key().as_hex())

    inputs = [agent_address, document_hash]

    outputs = [document_hash]

    action = payload_pb2.ValidateDocumentAction(
        document_id=document_id,
        document_hash=document_hash,
        new_document_hash=new_document_hash)

    payload = payload_pb2.SimpleSupplyPayload(
        action=payload_pb2.SimpleSupplyPayload.VALIDATE_DOCUMENT,
        validate_document=action,
        timestamp=timestamp)
    payload_bytes = payload.SerializeToString()

    return _make_batch(payload_bytes=payload_bytes,
                       inputs=inputs,
                       outputs=outputs,
                       transaction_signer=transaction_signer,
                       batch_signer=batch_signer)
示例#12
0
    def set_agent(self, public_key, name, timestamp):
        """Creates a new agent in state

        Args:
            public_key (str): The public key of the agent
            name (str): The human-readable name of the agent
            timestamp (int): Unix UTC timestamp of when the agent was created
        """
        address = addresser.get_agent_address(public_key)
        agent = agent_pb2.Agent(public_key=public_key,
                                name=name,
                                timestamp=timestamp)
        container = agent_pb2.AgentContainer()
        state_entries = self._context.get_state(addresses=[address],
                                                timeout=self._timeout)
        if state_entries:
            container.ParseFromString(state_entries[0].data)

        container.entries.extend([agent])
        data = container.SerializeToString()

        updated_state = {}
        updated_state[address] = data
        self._context.set_state(updated_state, timeout=self._timeout)