def create_account(txn_key, batch_key, label, description):
    """Create a CreateAccount 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.
        label (str): The account's label.
        description (str): The description of the account.

    Returns:
        tuple: List of Batch, signature tuple
    """

    inputs = [addresser.make_account_address(
        account_id=txn_key.get_public_key().as_hex())]

    outputs = [addresser.make_account_address(
        account_id=txn_key.get_public_key().as_hex())]

    account = payload_pb2.CreateAccount(
        label=label,
        description=description)
    payload = payload_pb2.TransactionPayload(
        payload_type=payload_pb2.TransactionPayload.CREATE_ACCOUNT,
        create_account=account)

    return make_header_and_batch(
        payload=payload,
        inputs=inputs,
        outputs=outputs,
        txn_key=txn_key,
        batch_key=batch_key)
def create_account(txn_key, batch_key, label, description):
    """Create a CreateAccount 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.
        label (str): The account's label.
        description (str): The description of the account.

    Returns:
        tuple: List of Batch, signature tuple
    """

    inputs = [addresser.make_account_address(
        account_id=txn_key.get_public_key().as_hex())]

    outputs = [addresser.make_account_address(
        account_id=txn_key.get_public_key().as_hex())]

    account = payload_pb2.CreateAccount(
        label=label,
        description=description)
    payload = payload_pb2.TransactionPayload(
        payload_type=payload_pb2.TransactionPayload.CREATE_ACCOUNT,
        create_account=account)

    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_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_resource(txn_key, batch_key, name, description, rules):
    """Create a CreateResource 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 resource.
        description (str): A description of the resource.
        rules (list): List of protobuf.rule_pb2.Rule

    Returns:
        tuple: List of Batch, signature tuple
    """

    inputs = [
        addresser.make_resource_address(resource_id=name),
        addresser.make_account_address(
            account_id=txn_key.get_public_key().as_hex())
    ]

    outputs = [addresser.make_resource_address(resource_id=name)]

    resource = payload_pb2.CreateResource(name=name,
                                          description=description,
                                          rules=rules)

    payload = payload_pb2.TransactionPayload(
        payload_type=payload_pb2.TransactionPayload.CREATE_RESOURCE,
        create_resource=resource)

    return make_header_and_batch(payload=payload,
                                 inputs=inputs,
                                 outputs=outputs,
                                 txn_key=txn_key,
                                 batch_key=batch_key)
    def test_account_address(self):
        account_address = addresser.make_account_address(uuid4().hex)

        self.assertEqual(len(account_address), 70, "The address is valid.")

        self.assertEqual(addresser.address_is(account_address),
                         addresser.AddressSpace.ACCOUNT,
                         "The address is correctly identified as an Account.")
    def test_account_address(self):
        account_address = addresser.make_account_address(uuid4().hex)

        self.assertEqual(len(account_address), 70, "The address is valid.")

        self.assertEqual(addresser.address_is(account_address),
                         addresser.AddressSpace.ACCOUNT,
                         "The address is correctly identified as an Account.")
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 add_holding_to_account(self, public_key, holding_id):
        address = addresser.make_account_address(account_id=public_key)

        container = _get_account_container(self._state_entries, address)

        try:
            account = _get_account_from_container(container, public_key)
        except KeyError:
            account = container.entries.add()

        account.holdings.append(holding_id)

        state_entries_send = {}
        state_entries_send[address] = container.SerializeToString()
        return self._context.set_state(state_entries_send, self._timeout)
    def get_account(self, public_key):
        address = addresser.make_account_address(account_id=public_key)

        self._state_entries.extend(
            self._context.get_state(addresses=[address],
                                    timeout=self._timeout))

        container = _get_account_container(self._state_entries, address)
        account = None
        try:
            account = _get_account_from_container(container,
                                                  identifier=public_key)
        except KeyError:
            # We are fine with returning None for an account that doesn't
            # exist in state.
            pass
        return account
    def get_account(self, public_key):
        address = addresser.make_account_address(account_id=public_key)

        self._state_entries.extend(self._context.get_state(
            addresses=[address],
            timeout=self._timeout))

        container = _get_account_container(self._state_entries, address)
        account = None
        try:
            account = _get_account_from_container(
                container,
                identifier=public_key)
        except KeyError:
            # We are fine with returning None for an account that doesn't
            # exist in state.
            pass
        return account
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 add_holding_to_account(self, public_key, holding_id):
        address = addresser.make_account_address(account_id=public_key)

        container = _get_account_container(self._state_entries, address)

        try:
            account = _get_account_from_container(
                container,
                public_key)
        except KeyError:
            account = container.entries.add()

        account.holdings.append(holding_id)

        state_entries_send = {}
        state_entries_send[address] = container.SerializeToString()
        return self._context.set_state(
            state_entries_send,
            self._timeout)
    def set_account(self, public_key, label, description, holdings):
        address = addresser.make_account_address(account_id=public_key)

        container = _get_account_container(self._state_entries, address)

        try:
            account = _get_account_from_container(container, public_key)
        except KeyError:
            account = container.entries.add()

        account.public_key = public_key
        account.label = label
        account.description = description
        for holding in holdings:
            account.holdings.append(holding)

        state_entries_send = {}
        state_entries_send[address] = container.SerializeToString()
        return self._context.set_state(state_entries_send, self._timeout)
    def set_account(self, public_key, label, description, holdings):
        address = addresser.make_account_address(account_id=public_key)

        container = _get_account_container(self._state_entries, address)

        try:
            account = _get_account_from_container(
                container,
                public_key)
        except KeyError:
            account = container.entries.add()

        account.public_key = public_key
        account.label = label
        account.description = description
        for holding in holdings:
            account.holdings.append(holding)

        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 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)