gr682_sdv 3 vuotta sitten
commit
5a2e3c5958
2 muutettua tiedostoa jossa 420 lisäystä ja 0 poistoa
  1. 334 0
      Pyth.txt
  2. 86 0
      Sol.txt

+ 334 - 0
Pyth.txt

@@ -0,0 +1,334 @@
+import web3.exceptions
+from web3 import Web3
+import service
+
+current_user_address = ''
+current_user_data = ''
+
+contract_address = '0x041a4dAAD14c913aE81B70BF03599c3568Ccf8D3'
+
+contract_abi = [
+	{
+		"inputs": [
+			{
+				"internalType": "string",
+				"name": "Name",
+				"type": "string"
+			},
+			{
+				"internalType": "uint256",
+				"name": "Price",
+				"type": "uint256"
+			}
+		],
+		"name": "AddNewProduct",
+		"outputs": [],
+		"stateMutability": "nonpayable",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "string",
+				"name": "Name",
+				"type": "string"
+			}
+		],
+		"name": "BuyProduct",
+		"outputs": [],
+		"stateMutability": "nonpayable",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "address",
+				"name": "addressUser",
+				"type": "address"
+			}
+		],
+		"name": "DemotionSellerToBuyer",
+		"outputs": [],
+		"stateMutability": "nonpayable",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "string",
+				"name": "Name",
+				"type": "string"
+			},
+			{
+				"internalType": "uint256",
+				"name": "Price",
+				"type": "uint256"
+			}
+		],
+		"name": "EditProduct",
+		"outputs": [],
+		"stateMutability": "nonpayable",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "address",
+				"name": "addressUser",
+				"type": "address"
+			}
+		],
+		"name": "PromotionToAdmin",
+		"outputs": [],
+		"stateMutability": "nonpayable",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "address",
+				"name": "addressUser",
+				"type": "address"
+			}
+		],
+		"name": "PromotionToSeller",
+		"outputs": [],
+		"stateMutability": "nonpayable",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "string",
+				"name": "Name",
+				"type": "string"
+			},
+			{
+				"internalType": "string",
+				"name": "Password",
+				"type": "string"
+			}
+		],
+		"name": "RegUser",
+		"outputs": [],
+		"stateMutability": "nonpayable",
+		"type": "function"
+	},
+	{
+		"inputs": [],
+		"stateMutability": "nonpayable",
+		"type": "constructor"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "string",
+				"name": "Password",
+				"type": "string"
+			}
+		],
+		"name": "AuthUser",
+		"outputs": [
+			{
+				"internalType": "bool",
+				"name": "",
+				"type": "bool"
+			}
+		],
+		"stateMutability": "view",
+		"type": "function"
+	},
+	{
+		"inputs": [],
+		"name": "GetProducts",
+		"outputs": [
+			{
+				"internalType": "string[]",
+				"name": "",
+				"type": "string[]"
+			}
+		],
+		"stateMutability": "view",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "string",
+				"name": "",
+				"type": "string"
+			}
+		],
+		"name": "products",
+		"outputs": [
+			{
+				"internalType": "uint256",
+				"name": "",
+				"type": "uint256"
+			}
+		],
+		"stateMutability": "view",
+		"type": "function"
+	},
+	{
+		"inputs": [
+			{
+				"internalType": "address",
+				"name": "",
+				"type": "address"
+			}
+		],
+		"name": "users",
+		"outputs": [
+			{
+				"internalType": "string",
+				"name": "Name",
+				"type": "string"
+			},
+			{
+				"internalType": "bytes32",
+				"name": "PasswordHash",
+				"type": "bytes32"
+			},
+			{
+				"internalType": "uint256",
+				"name": "Balance",
+				"type": "uint256"
+			},
+			{
+				"internalType": "uint256",
+				"name": "Role",
+				"type": "uint256"
+			},
+			{
+				"internalType": "bool",
+				"name": "isUserExist",
+				"type": "bool"
+			}
+		],
+		"stateMutability": "view",
+		"type": "function"
+	},
+	{
+		"inputs": [],
+		"name": "ViewBalance",
+		"outputs": [
+			{
+				"internalType": "uint256",
+				"name": "",
+				"type": "uint256"
+			}
+		],
+		"stateMutability": "view",
+		"type": "function"
+	}
+]
+
+#
+w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
+store_contract = w3.eth.contract(address=service.contract_address,
+                                 abi=service.contract_abi)
+
+
+def auth():
+    print('Enter address:')
+    address = input()
+    print('Enter password:')
+    password = input()
+
+    if address != '' and password != '':
+        try:
+            auth_user = store_contract.functions.AuthUser(password).call({'from': address})
+            if auth_user:
+                print('You auth')
+                service.current_user_address = address
+                service.current_user_data = store_contract.functions.users(service.current_user_address).call()
+                if service.current_user_data[3] == 0:
+                    admin_menu()
+                elif service.current_user_data[3] == 1:
+                    seller_menu()
+                elif service.current_user_data[3] == 2:
+                    buyer_menu()
+            else:
+                print('No auth')
+                return
+        except web3.exceptions.ContractLogicError as err:
+            print(err)
+
+
+def reg():
+    print("Enter address: ")
+    address = input()
+    print('Enter name: ')
+    name = input()
+    print('Enter password: ')
+    password = input()
+    if address != '' and password != '' and name != '':
+        try:
+            store_contract.functions.RegUser(name, password).transact({'from': address})
+        except web3.exceptions.ContractLogicError as err:
+            print(err)
+
+
+def admin_menu():
+    while True:
+        print("Главное меню")
+        print("1 - Узнать баланс")
+        print("2 - Повысить пользователя")
+        print("3 - Повысить до продавца")
+        print("4 - Понизить продавца до покупателя")
+        print("0 - Close")
+
+        user_choice = int(input())
+        if user_choice == 1:
+            print(store_contract.functions.ViewBalance().call({'from': service.current_user_address}))
+        elif user_choice == 2:
+            print('Enter address: ')
+            address = input()
+            try:
+                store_contract.functions.PromotionToAdmin(address).transact({'from': service.current_user_address})
+            except web3.exceptions.ContractLogicError as err:
+                print(err)
+        elif user_choice == 3:
+            print('Enter address: ')
+            address = input()
+            try:
+                store_contract.functions.PromotionToSeller(address).transact({'from': service.current_user_address})
+            except web3.exceptions.ContractLogicError as err:
+                print(err)
+        elif user_choice == 4:
+            print('Enter address: ')
+            address = input()
+            try:
+                store_contract.functions.DemotionSellerToBuyer(address).transact({'from': service.current_user_address})
+            except web3.exceptions.ContractLogicError as err:
+                print(err)
+        elif user_choice == 0:
+            break
+        else:
+            break
+
+
+def seller_menu():
+    pass
+
+
+def buyer_menu():
+    pass
+
+
+while True:
+    print("Hi")
+    print("1 - Auth")
+    print("2 - Reg")
+    print("0 - Close")
+    user_choice = int(input())
+
+    if user_choice == 1:
+        auth()
+    elif user_choice == 2:
+        reg()
+    elif user_choice == 0:
+        break
+    else:
+        break

+ 86 - 0
Sol.txt

@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-3.0
+pragma solidity >=0.7.0 <0.9.0;
+pragma experimental ABIEncoderV2;
+
+contract StoreQual{
+    struct User{
+        string Name;
+        bytes32 PasswordHash;
+        uint Role; // 0 - Admin, 1 - Seller, 2 - Buyer
+        bool isUserExist;
+    }
+
+        struct Shop
+    {
+        uint IdShop;
+        string NameShop;
+        bool isExist; 
+    }
+
+    Shop[] Shops;
+    mapping(address => User) private users;
+
+    constructor() {
+        users[0x5B38Da6a701c568545dCfcB03FcB875f56beddC4] = User("Admin", keccak256(abi.encodePacked("qwe")), 0, true);
+    }
+
+    function AuthUser(string memory Password) public view returns(bool) {
+        require(users[msg.sender].isUserExist == true, "User not exist");
+
+        if (users[msg.sender].PasswordHash == keccak256(abi.encodePacked(Password))) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    function RegUser(string memory Name, string memory Password) public {
+        require(users[msg.sender].isUserExist == false, "User already exist");
+
+        users[msg.sender] = (User(Name, keccak256(abi.encodePacked(Password)), 2, true));
+    }
+
+    // Admin Func
+    modifier onlyAdmin {
+        require(users[msg.sender].Role == 0);
+        _;
+    }
+
+    function PromotionToAdmin(address addressUser) public onlyAdmin{
+        require(users[addressUser].isUserExist == true, "User not exist");
+
+        users[addressUser].Role = 0;
+    }
+
+    function PromotionToSeller(address addressUser) public onlyAdmin{
+        require(users[addressUser].isUserExist == true, "User not exist");
+        require(users[addressUser].Role != 0, "User is Admin");
+
+        users[addressUser].Role = 1;
+    }
+
+    function DemotionSellerToBuyer(address addressUser) public view onlyAdmin{
+        require(users[addressUser].isUserExist == true, "User not exist");
+        require(users[addressUser].Role == 1, "User not Seller");
+
+        users[addressUser].Role == 2;
+    }
+    // Seller Func
+    modifier onlySeller {
+        require(users[msg.sender].Role == 1);
+        _;
+    }
+
+    function SwitchToBuyer(address addressUser) public view onlySeller{
+        require(users[addressUser].isUserExist == true, "User not exist");
+
+        users[addressUser].Role == 2;
+    } 
+
+    // Buyer Func
+    modifier onlyBuyer {
+        require(users[msg.sender].Role == 2);
+        _;
+    }
+
+}