def create_offer(txn_key, batch_key, identifier, label, description, source, target, rules): """Create a CreateOffer txn and wrap it in a batch and list. Args: txn_key (sawtooth_signing.Signer): The Txn signer key pair. batch_key (sawtooth_signing.Signer): The Batch signer key pair. identifier (str): The identifier of the Offer. label (str): The offer's label. description (str): The description of the offer. source (MarketplaceAsset): The asset id, quantity, resource to be drawn from. target (MarketplaceAsset): The asset id, quantity, resource to be paid into. rules (list): List of protobuf.rule_pb2.Rule Returns: tuple: List of Batch, signature tuple """ inputs = [ addresser.make_account_address( account_id=txn_key.get_public_key().as_hex()), addresser.make_asset_address(asset_id=source.asset_id), addresser.make_offer_address(offer_id=identifier), addresser.make_resource_address(resource_id=source.resource) ] if target.asset_id: inputs.append(addresser.make_asset_address(asset_id=target.asset_id)) inputs.append(addresser.make_resource_address(target.resource)) outputs = [addresser.make_offer_address(offer_id=identifier)] offer_txn = payload_pb2.CreateOffer(id=identifier, label=label, description=description, source=source.asset_id, source_quantity=source.quantity, target=target.asset_id, target_quantity=target.quantity, rules=rules) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.CREATE_OFFER, create_offer=offer_txn) return make_header_and_batch(payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def accept_offer(txn_key, batch_key, identifier, offerer, receiver, count): """Create an AcceptOffer txn and wrap it in a Batch and list. Args: txn_key (sawtooth_signing.Signer): The Txn signer key pair. batch_key (sawtooth_signing.Signer): The Batch signer key pair. identifier (str): The identifier of the Offer. offerer (OfferParticipant): The participant who made the offer. receiver (OfferParticipant): The participant who is accepting the offer. count (int): The number of units of exchange. Returns: tuple: List of Batch, signature tuple """ inputs = [ addresser.make_asset_address(receiver.target), addresser.make_asset_address(offerer.source), addresser.make_resource_address(receiver.target_resource), addresser.make_resource_address(offerer.source_resource), addresser.make_offer_history_address(offer_id=identifier), addresser.make_offer_account_address( offer_id=identifier, account=txn_key.get_public_key().as_hex()), addresser.make_offer_address(identifier) ] outputs = [ addresser.make_asset_address(receiver.target), addresser.make_asset_address(offerer.source), addresser.make_offer_history_address(offer_id=identifier), addresser.make_offer_account_address( offer_id=identifier, account=txn_key.get_public_key().as_hex()) ] if receiver.source is not None: inputs.append(addresser.make_asset_address(receiver.source)) inputs.append(addresser.make_resource_address( receiver.source_resource)) outputs.append(addresser.make_asset_address(receiver.source)) if offerer.target is not None: inputs.append(addresser.make_asset_address(offerer.target)) inputs.append(addresser.make_resource_address(offerer.target_resource)) outputs.append(addresser.make_asset_address(offerer.target)) accept_txn = payload_pb2.AcceptOffer(id=identifier, source=receiver.source, target=receiver.target, count=count) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.ACCEPT_OFFER, accept_offer=accept_txn) return make_header_and_batch(payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def transfer_asset(txn_key, batch_key, identifier, label, sender, amount): """Create an AcceptOffer txn and wrap it in a Batch and list. Args: txn_key (sawtooth_signing.Signer): The Txn signer key pair. batch_key (sawtooth_signing.Signer): The Batch signer key pair. identifier (str): The identifier of the Offer. sender (TransferParticipant): The participant who made the offer. receiver (TransferParticipant): The participant who is accepting the offer. count (int): The number of units of exchange. Returns: tuple: List of Batch, signature tuple """ inputs = [ addresser.make_asset_address(sender.source), addresser.make_asset_address(sender.target), addresser.make_resource_address(sender.resource), addresser.make_transfer_address( transfer_id=identifier, account=txn_key.get_public_key().as_hex()) ] outputs = [ addresser.make_asset_address(sender.source), addresser.make_asset_address(sender.target), addresser.make_resource_address(sender.resource), addresser.make_transfer_address( transfer_id=identifier, account=txn_key.get_public_key().as_hex()) ] transfer_txn = payload_pb2.TransferAsset(id=identifier, label=label, source=sender.source, target=sender.target, amount=amount) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.TRANSFER_ASSET, transfer_asset=transfer_txn) return make_header_and_batch(payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def test_asset_address(self): asset_address = addresser.make_asset_address(uuid4().hex) self.assertEqual(len(asset_address), 70, "The address is valid.") self.assertEqual(addresser.address_is(asset_address), addresser.AddressSpace.ASSET, "The address is correctly identified as an Asset.")
def get_asset(self, identifier): address = addresser.make_asset_address(asset_id=identifier) self._state_entries.extend( self._context.get_state(addresses=[address], timeout=self._timeout)) return self._get_asset(address=address, identifier=identifier)
def _return_offer_rules( self, asset_id, ): asset_addr = addresser.make_asset_address(asset_id) asset = self._get_asset(asset_addr, asset_id) resource = self.get_resource(asset.resource) return [r for r in resource.rules if r.type in OFFER_RULES]
def get_asset(self, name): address = addresser.make_asset_address(asset_id=name) self._state_entries.extend(self._context.get_state( addresses=[address], timeout=self._timeout)) return self._get_asset(address=address, name=name)
def get_asset(self, name): address = addresser.make_asset_address(asset_id=name) self._state_entries.extend( self._context.get_state(addresses=[address], timeout=self._timeout)) return self._get_asset(address=address, name=name)
def test_asset_address(self): asset_address = addresser.make_asset_address(uuid4().hex) self.assertEqual(len(asset_address), 70, "The address is valid.") self.assertEqual(addresser.address_is(asset_address), addresser.AddressSpace.ASSET, "The address is correctly identified as an Asset.")
def create_asset(txn_key, batch_key, identifier, label, description, resource, quantity): """Create a CreateAsset txn and wrap it in a batch and list. Args: txn_key (sawtooth_signing.Signer): The txn signer key pair. batch_key (sawtooth_signing.Signer): The batch signer key pair. identifier (str): The identifier of the Asset. label (str): The label of the Asset. description (str): The description of the Asset. quantity (int): The amount of the Resource. Returns: tuple: List of Batch, signature tuple """ inputs = [ addresser.make_account_address( account_id=txn_key.get_public_key().as_hex()), addresser.make_resource_address(resource_id=resource), addresser.make_asset_address(asset_id=identifier) ] outputs = [ addresser.make_asset_address(asset_id=identifier), addresser.make_account_address( account_id=txn_key.get_public_key().as_hex()) ] asset_txn = payload_pb2.CreateAsset(id=identifier, label=label, description=description, resource=resource, quantity=quantity) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.CREATE_ASSET, create_asset=asset_txn) return make_header_and_batch(payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def create_holding(txn_key, batch_key, identifier, label, description, asset, quantity): """Create a CreateHolding txn and wrap it in a batch and list. Args: txn_key (sawtooth_signing.Signer): The txn signer key pair. batch_key (sawtooth_signing.Signer): The batch signer key pair. identifier (str): The identifier of the Holding. label (str): The label of the Holding. description (str): The description of the Holding. quantity (int): The amount of the Asset. Returns: tuple: List of Batch, signature tuple """ inputs = [ addresser.make_account_address( account_id=txn_key.get_public_key().as_hex()), addresser.make_asset_address(asset_id=asset), addresser.make_holding_address(holding_id=identifier) ] outputs = [addresser.make_holding_address(holding_id=identifier), addresser.make_account_address( account_id=txn_key.get_public_key().as_hex())] holding_txn = payload_pb2.CreateHolding( id=identifier, label=label, description=description, asset=asset, quantity=quantity) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.CREATE_HOLDING, create_holding=holding_txn) return make_header_and_batch( payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def create_asset(txn_key, batch_key, name, description, rules): """Create a CreateAsset txn and wrap it in a batch and list. Args: txn_key (sawtooth_signing.Signer): The txn signer key pair. batch_key (sawtooth_signing.Signer): The batch signer key pair. name (str): The name of the asset. description (str): A description of the asset. rules (list): List of protobuf.rule_pb2.Rule Returns: tuple: List of Batch, signature tuple """ inputs = [addresser.make_asset_address(asset_id=name), addresser.make_account_address( account_id=txn_key.get_public_key().as_hex())] outputs = [addresser.make_asset_address(asset_id=name)] asset = payload_pb2.CreateAsset( name=name, description=description, rules=rules ) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.CREATE_ASSET, create_asset=asset) return make_header_and_batch( payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def change_asset_quantity(self, identifier, new_quantity): address = addresser.make_asset_address(asset_id=identifier) container = _get_asset_container(self._state_entries, address) try: asset = _get_asset_from_container(container, identifier) except KeyError: asset = container.entries.add() asset.quantity = new_quantity state_entries_send = {} state_entries_send[address] = container.SerializeToString() return self._context.set_state(state_entries_send, self._timeout)
def create_asset(txn_key, batch_key, name, description, rules): """Create a CreateAsset txn and wrap it in a batch and list. Args: txn_key (sawtooth_signing.Signer): The txn signer key pair. batch_key (sawtooth_signing.Signer): The batch signer key pair. name (str): The name of the asset. description (str): A description of the asset. rules (list): List of protobuf.rule_pb2.Rule Returns: tuple: List of Batch, signature tuple """ inputs = [addresser.make_asset_address(asset_id=name), addresser.make_account_address( account_id=txn_key.get_public_key().as_hex())] outputs = [addresser.make_asset_address(asset_id=name)] asset = payload_pb2.CreateAsset( name=name, description=description, rules=rules ) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.CREATE_ASSET, create_asset=asset) return make_header_and_batch( payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def get_asset(self, name): address = addresser.make_asset_address(asset_id=name) self._state_entries.extend( self._context.get_state(addresses=[address], timeout=self._timeout)) container = _get_asset_container(self._state_entries, address) asset = None try: asset = _get_asset_from_container(container, name) except KeyError: # We are fine with returning None for an asset that doesn't exist pass return asset
def set_asset(self, name, description, owners, rules): address = addresser.make_asset_address(name) container = _get_asset_container(self._state_entries, address) try: asset = _get_asset_from_container(container, name) except KeyError: asset = container.entries.add() asset.name = name asset.description = description asset.owners.extend(owners) asset.rules.extend(rules) state_entries_send = {} state_entries_send[address] = container.SerializeToString() return self._context.set_state(state_entries_send, self._timeout)
def create_feedback(txn_key, batch_key, identifier, asset, text, rating): """Create a CreateFeedback txn and wrap it in a batch and list. Args: txn_key (sawtooth_signing.Signer): The txn signer key pair. batch_key (sawtooth_signing.Signer): The batch signer key pair. asset (str): The name of the asset which feedback is about. text (str): Text of the feedback. rating (int): Num in [1;5] estimating service/good Returns: tuple: List of Batch, signature tuple """ inputs = [addresser.make_feedback_address( feedback_id=identifier), addresser.make_account_address( account_id=txn_key.get_public_key().as_hex()), addresser.make_asset_address(asset_id=asset)] outputs = [addresser.make_feedback_address(feedback_id=identifier)] feedback_txn = payload_pb2.CreateFeedback( id=identifier, asset=asset, text=text, rating=rating ) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.CREATE_FEEDBACK, create_feedback=feedback_txn) return make_header_and_batch( payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def set_asset(self, name, description, owners, rules): address = addresser.make_asset_address(name) container = _get_asset_container(self._state_entries, address) try: asset = _get_asset_from_container(container, name) except KeyError: asset = container.entries.add() asset.name = name asset.description = description asset.owners.extend(owners) asset.rules.extend(rules) state_entries_send = {} state_entries_send[address] = container.SerializeToString() return self._context.set_state( state_entries_send, self._timeout)
def set_asset(self, identifier, label, description, account, resource, quantity): address = addresser.make_asset_address(asset_id=identifier) container = _get_asset_container(self._state_entries, address) try: asset = _get_asset_from_container(container, identifier) except KeyError: asset = container.entries.add() asset.id = identifier asset.label = label asset.description = description asset.account = account asset.resource = resource asset.quantity = quantity state_entries_send = {} state_entries_send[address] = container.SerializeToString() return self._context.set_state(state_entries_send, self._timeout)
def accept_offer(txn_key, batch_key, identifier, offerer, receiver, count): """Create an AcceptOffer txn and wrap it in a Batch and list. Args: txn_key (sawtooth_signing.Signer): The Txn signer key pair. batch_key (sawtooth_signing.Signer): The Batch signer key pair. identifier (str): The identifier of the Offer. offerer (OfferParticipant): The participant who made the offer. receiver (OfferParticipant): The participant who is accepting the offer. count (int): The number of units of exchange. Returns: tuple: List of Batch, signature tuple """ inputs = [addresser.make_holding_address(receiver.target), addresser.make_holding_address(offerer.source), addresser.make_asset_address(receiver.target_asset), addresser.make_asset_address(offerer.source_asset), addresser.make_offer_history_address(offer_id=identifier), addresser.make_offer_account_address( offer_id=identifier, account=txn_key.get_public_key().as_hex()), addresser.make_offer_address(identifier)] outputs = [addresser.make_holding_address(receiver.target), addresser.make_holding_address(offerer.source), addresser.make_offer_history_address(offer_id=identifier), addresser.make_offer_account_address( offer_id=identifier, account=txn_key.get_public_key().as_hex())] if receiver.source is not None: inputs.append(addresser.make_holding_address(receiver.source)) inputs.append(addresser.make_asset_address(receiver.source_asset)) outputs.append(addresser.make_holding_address(receiver.source)) if offerer.target is not None: inputs.append(addresser.make_holding_address(offerer.target)) inputs.append(addresser.make_asset_address(offerer.target_asset)) outputs.append(addresser.make_holding_address(offerer.target)) accept_txn = payload_pb2.AcceptOffer( id=identifier, source=receiver.source, target=receiver.target, count=count) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.ACCEPT_OFFER, accept_offer=accept_txn) return make_header_and_batch( payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)
def create_offer(txn_key, batch_key, identifier, label, description, source, target, rules): """Create a CreateOffer txn and wrap it in a batch and list. Args: txn_key (sawtooth_signing.Signer): The Txn signer key pair. batch_key (sawtooth_signing.Signer): The Batch signer key pair. identifier (str): The identifier of the Offer. label (str): The offer's label. description (str): The description of the offer. source (MarketplaceHolding): The holding id, quantity, asset to be drawn from. target (MarketplaceHolding): The holding id, quantity, asset to be paid into. rules (list): List of protobuf.rule_pb2.Rule Returns: tuple: List of Batch, signature tuple """ inputs = [ addresser.make_account_address( account_id=txn_key.get_public_key().as_hex()), addresser.make_holding_address( holding_id=source.holding_id), addresser.make_offer_address(offer_id=identifier), addresser.make_asset_address(asset_id=source.asset) ] if target.holding_id: inputs.append(addresser.make_holding_address( holding_id=target.holding_id)) inputs.append(addresser.make_asset_address(target.asset)) outputs = [addresser.make_offer_address(offer_id=identifier)] offer_txn = payload_pb2.CreateOffer( id=identifier, label=label, description=description, source=source.holding_id, source_quantity=source.quantity, target=target.holding_id, target_quantity=target.quantity, rules=rules) payload = payload_pb2.TransactionPayload( payload_type=payload_pb2.TransactionPayload.CREATE_OFFER, create_offer=offer_txn) return make_header_and_batch( payload=payload, inputs=inputs, outputs=outputs, txn_key=txn_key, batch_key=batch_key)