Пример #1
0
    def create_agent(self, args):       # args [create_agent, private_key, name]
        if len(args) < 1:
            print("\nA private key is required to create and agent.\n")
            exit()

        private_key = _get_prv_key(args)
        signer = _create_signer(private_key)

        if 2 < len(args):
            name = args[2]
        else:
            name = "noname"

        # make the agent_payload, and specific attributes
        agent_action = CreateAgentAction(
                #public_key = self.public_key,
                public_key = signer.pubkey.serialize().hex(),
                name = name,
        )

        agent_payload = Payload(
            action = 1,
            timestamp = _get_time(),
            create_agent = agent_action,
        )

        # serialize before sending
        payload_bytes = agent_payload.SerializeToString()

        # Pack it all up and ship it out
        self.create_transaction(signer, payload_bytes)
Пример #2
0
    def touch_asset(self, args):    # args [touch_asset, private_key, rfid]
        # sawtooth-supply-chain/server/system/config.js takes in a json object that
        # contains a private key and a secret, plus network info, and 
        # must know the rfid of an asset to touch it (in theory, obtained through scan)
        if len(args) < 1:
            print("A private key must be provided to touch an asset")
            exit()
        elif len(args) < 2:
            print("A rfid is necessary to discern the item being touched")
            exit()
        
        private_key = _get_prv_key(args)    # verify the private key is valid in handler.py
        signer = _create_signer(private_key)

        rfid = args[2]

        touch_action = TouchAssetAction(
            rfid = rfid,
            longitude = 2,
            latitude = 3,
        )

        touch_payload = Payload(
            action = 2,
            timestamp = _get_time(),
            touch_asset = touch_action,
        )

        payload_bytes = touch_payload.SerializeToString()

        self.create_transaction(signer, payload_bytes)
Пример #3
0
    def create_asset(self, args):    # args [create_asset, private_key]
        # must have private key, otherwise the user could create an asset 
        # without the proper credentials, potentially polluting state.
        private_key = _get_prv_key(args)
        signer = _create_signer(private_key)

        # make the asset_payload, and specific attributes
        asset_action = CreateAssetAction(
            rfid = _make_rfid(),
            size = "Mens 10",
            sku = "0 123456789 1",
            longitude = 1,
            latitude = 1,
        )

        ###### GLOBAL RFID FOR TESTING PURPOSES ######
        global rfid0
        rfid0 = asset_action.rfid
        ##############################################

        asset_payload = Payload(
            action = 0,
            timestamp = _get_time(),
            create_asset = asset_action,        
        )

        # serialize before sending
        payload_bytes = asset_payload.SerializeToString()

        # Pack it all up and ship it out
        self.create_transaction(signer, payload_bytes)
Пример #4
0
def _unpack_transaction(transaction, state):
    """Return the transaction signing key, the SCPayload timestamp, the
    appropriate SCPayload action attribute, and the appropriate
    handler function (with the latter two determined by the constant
    TYPE_TO_ACTION_HANDLER table.
    """ 

    signer = transaction.header.signer_public_key

    payload_header = Payload()
    payload_header.ParseFromString(transaction.payload)

    action = payload_header.action
    timestamp = payload_header.timestamp

    try:
        attribute, handler = TYPE_TO_ACTION_HANDLER[action]
    except KeyError:
        raise Exception('Specified action is invalid')

    payload = getattr(payload_header, attribute)

    company = {
        Payload.CREATE_AGENT:True,
        Payload.CREATE_ASSET:True,
        Payload.TOUCH_ASSET:True,
        Payload.LOCK_ASSET:True,
        Payload.UNLOCK_ASSET:True,
    }

    factory = {
        Payload.CREATE_AGENT:True,
        Payload.CREATE_ASSET:True,
        Payload.TOUCH_ASSET:True,
        Payload.LOCK_ASSET:True,
        Payload.UNLOCK_ASSET:True,
    }

    shipper = {
        Payload.CREATE_AGENT:True,
        Payload.CREATE_ASSET:False,
        Payload.TOUCH_ASSET:True,
        Payload.LOCK_ASSET:False,
        Payload.UNLOCK_ASSET:False,
    }

    if action == Payload.CREATE_AGENT:
        return signer, timestamp, payload, handler

    if action not in company:
        raise InvalidTransaction('\'' + action + '\' is not a valid action.')

    agent_role = _get_Agent_Role(state, signer)

    if agent_role <= 0 and (not company[action]):
        raise InvalidTransaction('Not authorized to perform this action.')
    elif agent_role == 1 and (not factory[action]):
        raise InvalidTransaction('Not authorized to perform this action.')
    elif agent_role >= 2 and (not shipper[action]):
        raise InvalidTransaction('Not authorized to perform this action.')

    return signer, timestamp, payload, handler
Пример #5
0
if test == "make_agent":
    if len(args) <= 1:
        print("\nA name is required to create an agent.\n")
        exit()
    name = args[1]

    print("\nAgent information:")
    print("Name = '" + name + "'")
    print("Public key = '" + public_key + "'")
    print("Private key = '" + private_key_bytes.hex() + "'\n")

    agent_action.name = name

    agent_payload = Payload(
        action=1,
        timestamp=_get_time(),
        create_agent=agent_action,
    )

    payload_bytes = agent_payload.SerializeToString()

elif test == "make_asset":
    if len(args) <= 1:
        rfid = _make_rfid()
    else:
        rfid = args[1]

    asset_action.rfid = rfid

    asset_payload = Payload(
        action=0,