def registerUser(payloadobj, signer, context, txId): formDetails = payloadobj.createAccount LOGGER.debug("USER FOUND-") LOGGER.debug(formDetails) # is_admin=utils.check_admin_signer(signer) # if not is_admin: # raise InvalidTransaction("User Not admin") write_sets = {} new_account = assets.Account() LOGGER.debug("PUB KEY FOUND-") LOGGER.debug(signer) new_account.address = Addressing.get_user_address(signer) new_account.userName = formDetails.name new_account.email = formDetails.email new_account.publicKey = signer LOGGER.debug("Account-") LOGGER.debug(new_account) user_check = context.get_state([new_account.address]) if len(user_check) != 0: raise InvalidTransaction("User already created") trail = utils.store_Trail(new_account.userName, txId, payloadobj.issueTimestamp, new_account.address, assets.Trail.ACCOUNT_CREATED) LOGGER.debug("TRAIL") LOGGER.debug(trail[1]) write_sets[trail[0]] = trail[1] write_sets[new_account.address] = new_account.SerializeToString() context.set_state(write_sets)
def transact(payloadobj, signer, context, txId): formDetails = payloadobj.transact LOGGER.debug(formDetails) write_sets = {} address = Addressing.get_user_address(signer) data_check = context.get_state([address]) if len(data_check) == 0: raise InvalidTransaction("User Doesn't Exist In Distributed Ledger") # if data_check[2].address == orderAddress: # raise InvalidTransaction("Order Id has already created") # LOGGER.debug("Reaching here 44") user = assets.Account() LOGGER.debug(signer) user.ParseFromString(data_check[0].data) LOGGER.debug(formDetails.type) if (formDetails.type == assets.Trail.CREDIT): LOGGER.debug("CREDITING") user.balance += formDetails.amount elif (formDetails.type == assets.Trail.DEBIT): if (user.balance < formDetails.amount): raise InvalidTransaction("Insufficient Balance..!") user.balance -= formDetails.amount LOGGER.debug("DEBITING") LOGGER.debug("balance") LOGGER.debug(user.balance) write_sets[address] = user.SerializeToString() context.set_state(write_sets)
def create_order(payloadobj, signer, context, txId): """ Factory Part Creation on Blockchain Args: param1 (payloadObj): Payload associated with transaction param2 (signer): Public key of transaction Signer(transactor) param3 (context): stat context instance param4 (txId): Unique Id associated with each transaction used to track their status Returns: """ LOGGER.info("Create Part - Factory") write_sets = {} formDetails = payloadobj.factoryAsset LOGGER.debug(formDetails) factoryAddress = Addressing.get_user_address(signer) data_check = context.get_state([factoryAddress]) LOGGER.info("States Read") LOGGER.debug(len(data_check)) if len(data_check) == 0: raise InvalidTransaction("Factory Doesn't Exist") account = assets.Account() account.ParseFromString(data_check[0].data) LOGGER.info("Signer Type FACTORY === Confirming") LOGGER.debug(account.type) if account.type != assets.OrgType.ORG_TYPE_PARTS_FACTORY: raise InvalidTransaction("WRONG SIGNER TYPE") assetAddress = Addressing.get_asset_address(formDetails.assetId, formDetails.type) LOGGER.debug(assetAddress) new_order = assets.FactoryAsset() new_order.address = assetAddress new_order.assetId = formDetails.assetId new_order.type = formDetails.type new_order.currentOwner = account.type new_order.factoryAddress = account.address parts_trail = utils.store_Trail(formDetails.assetId, txId, signer, account.organizationName, account.OrgType, new_order.type) write_sets[parts_trail[0]] = parts_trail[1] write_sets[assetAddress] = new_order.SerializeToString() LOGGER.info("Setting Context for States") LOGGER.debug(write_sets) context.set_state(write_sets)
def register_user(payloadobj,signer,context,txId): """ Registering User On Blockchain - With its PublicKey and Type Args: param1 (payloadObj): Payload associated with transaction param2 (signer): Public key of transaction Signer(transactor) param3 (context): stat context instance param4 (txId): Unique Id associated with each transaction used to track their status Returns: """ user_details=payloadobj.userEnrollment LOGGER.debug("IN register_user") LOGGER.debug(user_details) is_admin=utils.check_admin_signer(signer) LOGGER.info("Verify Signer") if not is_admin: raise InvalidTransaction("Signer Not Admin") if(not user_details.publicKey or not user_details.type or not user_details.accountNumber or not user_details.organizationName ) : raise InvalidTransaction("Wrong Input") length_pub = len(user_details.publicKey) LOGGER.debug(length_pub) if (length_pub != 66): raise InvalidTransaction("Wrong Public Length Key") LOGGER.info("Verify Public Key") userAddress = Addressing.get_user_address(user_details.publicKey) user_check=context.get_state([userAddress]) if len(user_check)!= 0: raise InvalidTransaction("Public Already Used") write_sets={} new_account=assets.Account() new_account.address= userAddress new_account.organizationName=user_details.organizationName new_account.accountNumber=user_details.accountNumber new_account.type=user_details.type new_account.publicKey=user_details.publicKey LOGGER.info("Account Field Updated") LOGGER.debug(new_account) write_sets[userAddress]=new_account.SerializeToString() LOGGER.info("Setting Context for States") LOGGER.debug(write_sets) context.set_state(write_sets)
def change_ownership(payloadobj, signer, context, txId): """ Change OwnerShip of Factory assets Args: param1 (payloadObj): Payload associated with transaction param2 (signer): Public key of transaction Signer(transactor) param3 (context): stat context instance param4 (txId): Unique Id associated with each transaction used to track their status Returns: """ LOGGER.info("Welcome to afInspect") write_sets = {} formDetails = payloadobj.changeOwnership LOGGER.debug(formDetails) userAddress = Addressing.get_user_address(signer) selectedUserAddress = Addressing.get_user_address( formDetails.newOwnerAddress) assetAddress = Addressing.get_asset_address(formDetails.assetId, formDetails.type) data_check = context.get_state( [userAddress, selectedUserAddress, assetAddress]) LOGGER.debug(len(data_check)) if len(data_check) == 3: LOGGER.info("All Addresses Found") else: raise InvalidTransaction("Addresses not found") user_account = assets.Account() user_account.ParseFromString(data_check[0].data) LOGGER.info("Data loading Done - 1") selected_user_account = assets.Account() selected_user_account.ParseFromString(data_check[1].data) LOGGER.info("Data loading Done - 2") asset = assets.FactoryAsset() asset.ParseFromString(data_check[2].data) LOGGER.info("Data loading Done - 3") if user_account.address == asset.currentOwnerAddress: LOGGER.info("Signer Doesn't Own the asset") else: raise InvalidTransaction("Wrong Signer for the asset") if selected_user_account.type == formDetails.assignToType: LOGGER.info("User Type Correct") else: raise InvalidTransaction("Wrong Assignee") # Change Ownership of CAR Asset if asset.type == assets.AssetType.CAR: if formDetails.assignToType == assets.OrgType.ORG_TYPE_DEALER: asset.dealerAddress = selected_user_account.address else: LOGGER.info("CAR ASSET CAN NOT BE ASSIGNED TO DEALERS") # Updating factory assets engineAddress = Addressing.get_asset_address(asset.engineId, assets.AssetType.ENGINE) wheelAddress = Addressing.get_asset_address(asset.wheelId, assets.AssetType.WHEEL) transmissionsAddress = Addressing.get_asset_address( asset.transmissionsId, assets.AssetType.TRANSMISSIONS) entries = context.get_state( [engineAddress, wheelAddress, transmissionsAddress]) if len(entries) == 3: LOGGER.info("All Addresses Found") else: raise InvalidTransaction("Addresses not found") engine_asset.currentOwner = assets.OrgType.ORG_TYPE_DEALER engine_asset.currentOwnerAddress = selectedUserAddress engine_asset.dealerAddress = selectedUserAddress engine_trail = utils.store_Trail( engine_asset.assetId, txId, signer, selected_user_account.organizationName, engine_asset.type) wheel_asset.currentOwner = assets.OrgType.ORG_TYPE_DEALER wheel_asset.currentOwnerAddress = selectedUserAddress wheel_asset.dealerAddress = selectedUserAddress wheel_trail = utils.store_Trail(wheel_asset.assetId, txId, signer, selected_user_account.organizationName, wheel_asset.type) transmissions_asset.currentOwner = assets.OrgType.ORG_TYPE_DEALER transmissions_asset.currentOwnerAddress = selectedUserAddress transmissions_asset.dealerAddress = selectedUserAddress transmissions_trail = utils.store_Trail( transmissions_asset.assetId, txId, signer, selected_user_account.organizationName, transmissions_asset.type) else: # Change ownership of Factory assets if formDetails.assignToType == assets.OrgType.ORG_TYPE_CAR_FACTORY: asset.carAddress = selected_user_account.address elif formDetails.assignToType == assets.OrgType.ORG_TYPE_DEALER: asset.dealerAddress = selected_user_account.address else: LOGGER.info("FACTORY ASSET CAN NOT BE ASSIGNED TO OTHER FACTORY") asset.currentOwner = selected_user_account.type asset.currentOwnerAddress = selected_user_account.address trail = utils.store_Trail(asset.assetId, txId, signer, selected_user_account.organizationName, asset.type) write_sets[trail[0]] = trail[1] write_sets[engine_trail[0]] = engine_trail[1] write_sets[wheel_trail[0]] = wheel_trail[1] write_sets[transmissions_trail[0]] = transmissions_trail[1] write_sets[assetAddress] = asset.SerializeToString() LOGGER.info("Setting Context for States") LOGGER.debug(write_sets) context.set_state(write_sets)
def create_car(payloadobj, signer, context, txId): formDetails = payloadobj.car LOGGER.debug(formDetails) write_sets = {} address = Addressing.get_user_address(signer) data_check = context.get_state([address]) userAddress = Addressing.get_user_address(signer) engineAddress = Addressing.get_asset_address(formDetails.assetId, assets.AssetType.ENGINE) wheelAddress = Addressing.get_asset_address(formDetails.assetId, assets.AssetType.WHEEL) transmissionsAddress = Addressing.get_asset_address( formDetails.assetId, assets.AssetType.TRANSMISSIONS) data_check = context.get_state( [userAddress, engineAddress, wheelAddress, transmissionsAddress]) LOGGER.debug(len(data_check)) if len(data_check) != 4: raise InvalidTransaction("One of the assets is not registered") else: LOGGER.info("All Assets found --- Lets create a car") # generate an address for car assetAddress = Addressing.get_asset_address(formDetails.assetId, assets.AssetType.CAR) entries = context.get_state( [userAddress, engineAddress, wheelAddress, transmissionsAddress]) if len(entries) == 1: raise InvalidTransaction("Car Already Exists") else: LOGGER.info("Lets create a car") user_account = assets.Account() user_account.ParseFromString(data_check[0].data) LOGGER.info("Data loading Done - 1") if user_account.type != assets.OrgType.ORG_TYPE_CAR_FACTORY: raise InvalidTransaction("WRONG SIGNER TYPE") engine_asset = assets.FactoryAsset() engine_asset.ParseFromString(data_check[1].data) wheel_asset = assets.FactoryAsset() wheel_asset.ParseFromString(data_check[2].data) transmissions_asset = assets.FactoryAsset() transmissions_asset.ParseFromString(data_check[3].data) new_order = assets.Car() new_order.address = assetAddress new_order.assetId = formDetails.assetId new_order.wheelId = wheel_asset.assetId new_order.engineId = engine_asset.assetId new_order.transmissionsId = transmissions_asset.assetId new_order.currentOwner = assets.OrgType.ORG_TYPE_CAR_FACTORY new_order.currentOwnerAddress = userAddress if (engine_asset.currentOwner == assets.OrgType.ORG_TYPE_PARTS_FACTORY): engine_asset.currentOwner = assets.OrgType.ORG_TYPE_CAR_FACTORY engine_asset.currentOwnerAddress = userAddress engine_asset.carAddress = assetAddress engine_trail = utils.store_Trail(engine_asset.assetId, txId, signer, user_account.organizationName, engine_asset.type) else: raise InvalidTransaction("=== Asset Already in use ===") if (wheel_asset.currentOwner == assets.OrgType.ORG_TYPE_PARTS_FACTORY): wheel_asset.currentOwner = assets.OrgType.ORG_TYPE_CAR_FACTORY wheel_asset.currentOwnerAddress = userAddress wheel_asset.carAddress = assetAddress wheel_trail = utils.store_Trail(wheel_asset.assetId, txId, signer, user_account.organizationName, wheel_asset.type) else: raise InvalidTransaction("=== Asset Already in use ===") if (transmissions_asset.currentOwner == assets.OrgType.ORG_TYPE_PARTS_FACTORY): transmissions_asset.currentOwner = assets.OrgType.ORG_TYPE_CAR_FACTORY transmissions_asset.currentOwnerAddress = userAddress transmissions_asset.carAddress = assetAddress transmissions_trail = utils.store_Trail(transmissions_trail.assetId, txId, signer, user_account.organizationName, transmissions_trail.type) else: raise InvalidTransaction("=== Asset Already in use ===") trail = utils.store_Trail(asset.assetId, txId, signer, user_account.organizationName, asset.type) write_sets[trail[0]] = trail[1] write_sets[userAddress] = user_account.SerializeToString() write_sets[assetAddress] = new_order.SerializeToString() write_sets[wheelAddress] = wheel_asset.SerializeToString() write_sets[engineAddress] = engine_asset.SerializeToString() write_sets[transmissionsAddress] = transmissions_asset.SerializeToString() write_sets[engine_trail[0]] = engine_trail[1] write_sets[wheel_trail[0]] = wheel_trail[1] write_sets[transmissions_trail[0]] = transmissions_trail[1] context.set_state(write_sets)