如何解决如何在React界面中创建Web3连接?
我已经使用react js创建了一个简单的登录和注册界面,并希望通过web3或任何其他方法与智能合约进行连接,以将注册用户的数据存储在区块链上,并验证其登录凭证以获得访问权限。 我正在使用ganache和metamask为我的应用程序创建测试环境。 有人可以帮助我或指导我阅读特定的文章/教程,以使用智能合约的功能来存储和获取数据吗?
我的智能合约是
pragma solidity ^0.5.0;
contract register {
uint public userCount = 0;
uint i=0;
bool verUser = true;
mapping(uint => user) public users;
struct user {
uint id;
string username;
string email;
address useraddr;
string password;
bool loggedIn;
}
event userCreated(
uint id,string username,string email,address useraddr,string password,bool loggedIn
);
event userLoggedIn(
uint id,bool loggedIn
);
function createuser(string memory _username,string memory _email,string memory _password) public
verification(_username,_password,userCount) {
require(bytes(_username).length > 0);
userCount ++;
users[userCount] = user(userCount,_username,_email,msg.sender,false);
emit userCreated(userCount,false);
}
function loginuser(string memory _username,string memory _password) public
logInVerified(_username,userCount) {
emit userLoggedIn(userCount,true);
}
modifier verification(string memory _username,string memory _password,uint _userCount){
user memory p = users[i];
for (i=0; i<userCount; i++)
{
if((keccak256(bytes(_username))) == keccak256(bytes(users[i].username)) ||
(keccak256(bytes(_password))) == keccak256(bytes(users[i].password)) ){
verUser = false;
}
else{
verUser = true;
}
}
require(verUser == true,'username or password already exists,please try again');
_;
}
modifier logInVerified(string memory _username,uint _userCount){
user memory p = users[i];
for (i=0; i<userCount; i++)
{
if((keccak256(bytes(_username))) == keccak256(bytes(users[i].username)) &&
(keccak256(bytes(_password))) == keccak256(bytes(users[i].password)) ){
verUser = true;
}
else{
verUser = false;
}
}
require(verUser == true,'Wrong username or password');
_;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。