Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Password.sol #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Password.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;


// Contract
contract SimpleStorage {

//Variables
string[] sites;
string[] usernames;
string[] passwords;

/*
We use this to ensure that all transactions are only performed by the authorized user.

We check the validity using
require(
msg.sender == user_adress,
"Only the authorizer of the contract can add a password."
);

in all functions that alter the variables after the contract has been created.
*/
address user_adress;

// When a new contract is created
constructor() public {
user_adress = msg.sender;
sites = new string[](0);
usernames = new string[](0);
passwords = new string[](0);
}

/*
Code to check if two strings are equivalent.
We do this by splitting them up and then checking if all of the individual characters match each other.
*/
function compareStrings(string memory a, string memory b) private returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}


// Used to store a new uername password pair in the contract
function addPassword(string sitename, string username, string password) public {
require(
msg.sender == user_adress,
"Only the authorizer of the contract can add a password."
);
passwords.push(password);
usernames.push(username);
sites.push(sitename);
}

// Get a saved password for an individual site
function getFromSite(string site) public view returns (string details) {
require(
msg.sender == user_adress,
"Only the authorizer of the contract can request access to a password."
);
for (uint i = 0; i < sites.length; i++) {
if (compareStrings(site, sites[i])) {
return string(abi.encodePacked("Site name: ", site, " Username: ", usernames[i], " Password: ", passwords[i]));
}
}
}

// Get a list of all passwords saved
function getAllPasswords() public view returns (string details) {
details = "";
require(
msg.sender == user_adress,
"Only the authorizer of the contract can request access to a password."
);
for (uint i = 0; i < sites.length; i++) {
details = string(abi.encodePacked(details, "Site name: ", sites[i], " Username: ", usernames[i], " Password: ", passwords[i]));
details = string(abi.encodePacked(details, " ; "));
}
return details;
}
}