Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.4;
- // Simple Solidity intro/demo contract for BlockGeeks Article
- // Владелец может менять название гостевой книги
- // Любой пользователь может добавлять любое сообщение, в котором необходимо указать имя и текст сообщения
- // Пользователи могут ставить рейтинг за сообщение
- // Владелец может убить контракт
- // Владелец может удалить сообщение
- contract GuestBook {
- address adminAddress; // адрес администратора
- string guestbookName; // название гостевой книги
- mapping ( bytes32 => notarizedImage) notarizedImages; // this allows to look up notarizedImages by their SHA256notaryHash
- bytes32[] imagesByNotaryHash; // this is like a whitepages of all images, by SHA256notaryHash
- mapping ( address => User ) Users; // this allows to look up Users by their ethereum address
- address[] usersByAddress; // this is like a whitepages of all users, by ethereum address
- struct notarizedImage {
- string imageURL; // адрес картинки
- uint timeStamp; // временная метка
- }
- struct User {
- string handle;
- bytes32 city;
- bytes32 state;
- bytes32 country;
- bytes32[] myImages;
- }
- function GuestBook() payable { // this is the CONSTRUCTOR (same name as contract) it gets called ONCE only when contract is first deployed
- adminAddress = msg.sender; // just set the admin, so they can remove bad users or images if needed, but nobody else can
- }
- modifier onlyAdmin() {
- if (msg.sender != adminAddress)
- throw;
- // Do not forget the "_;"! It will be replaced by the actual function body when the modifier is used.
- _;
- }
- function removeUser(address badUser) onlyAdmin returns (bool success) {
- delete Users[badUser];
- return true;
- }
- function removeImage(bytes32 badImage) onlyAdmin returns (bool success) {
- delete notarizedImages[badImage];
- return true;
- }
- function registerNewUser(string handle, bytes32 city, bytes32 state, bytes32 country) returns (bool success) {
- address thisNewAddress = msg.sender;
- // don't overwrite existing entries, and make sure handle isn't null
- if(bytes(Users[msg.sender].handle).length == 0 && bytes(handle).length != 0){
- Users[thisNewAddress].handle = handle;
- Users[thisNewAddress].city = city;
- Users[thisNewAddress].state = state;
- Users[thisNewAddress].country = country;
- usersByAddress.push(thisNewAddress); // adds an entry for this user to the user 'whitepages'
- return true;
- } else {
- return false; // either handle was null, or a user with this handle already existed
- }
- }
- function addImageToUser(string imageURL, bytes32 SHA256notaryHash) returns (bool success) {
- address thisNewAddress = msg.sender;
- if(bytes(Users[thisNewAddress].handle).length != 0){ // make sure this user has created an account first
- if(bytes(imageURL).length != 0){ // ) { // couldn't get bytes32 null check to work, oh well!
- // prevent users from fighting over sha->image listings in the whitepages, but still allow them to add a personal ref to any sha
- if(bytes(notarizedImages[SHA256notaryHash].imageURL).length == 0) {
- imagesByNotaryHash.push(SHA256notaryHash); // adds entry for this image to our image whitepages
- }
- notarizedImages[SHA256notaryHash].imageURL = imageURL;
- notarizedImages[SHA256notaryHash].timeStamp = block.timestamp; // note that updating an image also updates the timestamp
- Users[thisNewAddress].myImages.push(SHA256notaryHash); // add the image hash to this users .myImages array
- return true;
- } else {
- return false; // either imageURL or SHA256notaryHash was null, couldn't store image
- }
- return true;
- } else {
- return false; // user didn't have an account yet, couldn't store image
- }
- }
- function getUsers() constant returns (address[]) { return usersByAddress; }
- function getUser(address userAddress) constant returns (string,bytes32,bytes32,bytes32,bytes32[]) {
- return (Users[userAddress].handle,Users[userAddress].city,Users[userAddress].state,Users[userAddress].country,Users[userAddress].myImages);
- }
- function getAllImages() constant returns (bytes32[]) { return imagesByNotaryHash; }
- function getUserImages(address userAddress) constant returns (bytes32[]) { return Users[userAddress].myImages; }
- function getImage(bytes32 SHA256notaryHash) constant returns (string,uint) {
- return (notarizedImages[SHA256notaryHash].imageURL,notarizedImages[SHA256notaryHash].timeStamp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement