Created
June 29, 2017 21:56
-
-
Save anonymous/fe6997cd1b9ad2866de442259178e4c6 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js&optimize=undefined&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity ^0.4.9; | |
| /* | |
| decentralized microblogging | |
| Copyright (C) 2015 Jahn Bertsch | |
| This program is free software: you can redistribute it and/or modify | |
| it under the terms of the GNU General Public License as published by | |
| the Free Software Foundation in version 3. | |
| This program is distributed in the hope that it will be useful, | |
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| GNU General Public License for more details. | |
| You should have received a copy of the GNU General Public License | |
| along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| */ | |
| // "class" TweetAccount | |
| contract TweetAccount { | |
| // data structure of a single tweet | |
| struct Tweet { | |
| uint timestamp; | |
| string tweetString; | |
| } | |
| // "array" of all tweets of this account: maps the tweet id to the actual tweet | |
| mapping (uint => Tweet) _tweets; | |
| // total number of tweets in the above _tweets mapping | |
| uint _numberOfTweets; | |
| // "owner" of this account: only admin is allowed to tweet | |
| address _adminAddress; | |
| // constructor | |
| function TweetAccount() { | |
| _numberOfTweets = 0; | |
| _adminAddress = msg.sender; | |
| } | |
| // returns true if caller of function ("sender") is admin | |
| function isAdmin() constant returns (bool isAdmin) { | |
| return msg.sender == _adminAddress; | |
| } | |
| // create new tweet | |
| function tweet(string tweetString) returns (int result) { | |
| if (!isAdmin()) { | |
| // only owner is allowed to create tweets for this account | |
| result = -1; | |
| } else if (bytes(tweetString).length > 160) { | |
| // tweet contains more than 160 bytes | |
| result = -2; | |
| } else { | |
| _tweets[_numberOfTweets].timestamp = now; | |
| _tweets[_numberOfTweets].tweetString = tweetString; | |
| _numberOfTweets++; | |
| result = 0; // success | |
| } | |
| } | |
| function getTweet(uint tweetId) constant returns (string tweetString, uint timestamp) { | |
| // returns two values | |
| tweetString = _tweets[tweetId].tweetString; | |
| timestamp = _tweets[tweetId].timestamp; | |
| } | |
| function getLatestTweet() constant returns (string tweetString, uint timestamp, uint numberOfTweets) { | |
| // returns three values | |
| tweetString = _tweets[_numberOfTweets - 1].tweetString; | |
| timestamp = _tweets[_numberOfTweets - 1].timestamp; | |
| numberOfTweets = _numberOfTweets; | |
| } | |
| function getOwnerAddress() constant returns (address adminAddress) { | |
| return _adminAddress; | |
| } | |
| function getNumberOfTweets() constant returns (uint numberOfTweets) { | |
| return _numberOfTweets; | |
| } | |
| // other users can send donations to your account: use this function for donation withdrawal | |
| function adminRetrieveDonations(address receiver) { | |
| if (isAdmin()) { | |
| receiver.send(this.balance); | |
| } | |
| } | |
| function adminDeleteAccount() { | |
| if (isAdmin()) { | |
| suicide(_adminAddress); // this is a predefined function, it deletes the contract and returns all funds to the owner's address | |
| } | |
| } | |
| } | |
| /* | |
| decentralized microblogging | |
| Copyright (C) 2015 Jahn Bertsch | |
| This program is free software: you can redistribute it and/or modify | |
| it under the terms of the GNU General Public License as published by | |
| the Free Software Foundation in version 3. | |
| This program is distributed in the hope that it will be useful, | |
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| GNU General Public License for more details. | |
| You should have received a copy of the GNU General Public License | |
| along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| */ | |
| // "class" TweetRegistry | |
| contract TweetRegistry { | |
| // mappings to look up account names, account ids and addresses | |
| mapping (address => string) _addressToAccountName; | |
| mapping (uint => address) _accountIdToAccountAddress; | |
| mapping (string => address) _accountNameToAddress; | |
| // might be interesting to see how many people use the system | |
| uint _numberOfAccounts; | |
| // owner | |
| address _registryAdmin; | |
| // allowed to administrate accounts only, not everything | |
| address _accountAdmin; | |
| // if a newer version of this registry is available, force users to use it | |
| bool _registrationDisabled; | |
| function TweetRegistry() { | |
| _registryAdmin = msg.sender; | |
| _accountAdmin = msg.sender; // can be changed later | |
| _numberOfAccounts = 0; | |
| _registrationDisabled = false; | |
| } | |
| function register(string name, address accountAddress) returns (int result) { | |
| if (_accountNameToAddress[name] != address(0)) { | |
| // name already taken | |
| result = -1; | |
| } else if (bytes(_addressToAccountName[accountAddress]).length != 0) { | |
| // account address is already registered | |
| result = -2; | |
| } else if (bytes(name).length >= 64) { | |
| // name too long | |
| result = -3; | |
| } else if (_registrationDisabled){ | |
| // registry is disabled because a newer version is available | |
| result = -4; | |
| } else { | |
| _addressToAccountName[accountAddress] = name; | |
| _accountNameToAddress[name] = accountAddress; | |
| _accountIdToAccountAddress[_numberOfAccounts] = accountAddress; | |
| _numberOfAccounts++; | |
| result = 0; // success | |
| } | |
| } | |
| function getNumberOfAccounts() constant returns (uint numberOfAccounts) { | |
| numberOfAccounts = _numberOfAccounts; | |
| } | |
| function getAddressOfName(string name) constant returns (address addr) { | |
| addr = _accountNameToAddress[name]; | |
| } | |
| function getNameOfAddress(address addr) constant returns (string name) { | |
| name = _addressToAccountName[addr]; | |
| } | |
| function getAddressOfId(uint id) constant returns (address addr) { | |
| addr = _accountIdToAccountAddress[id]; | |
| } | |
| function unregister() returns (string unregisteredAccountName) { | |
| unregisteredAccountName = _addressToAccountName[msg.sender]; | |
| _addressToAccountName[msg.sender] = ""; | |
| _accountNameToAddress[unregisteredAccountName] = address(0); | |
| // _accountIdToAccountAddress is never deleted on purpose | |
| } | |
| function adminUnregister(string name) { | |
| if (msg.sender == _registryAdmin || msg.sender == _accountAdmin) { | |
| address addr = _accountNameToAddress[name]; | |
| _addressToAccountName[addr] = ""; | |
| _accountNameToAddress[name] = address(0); | |
| // _accountIdToAccountAddress is never deleted on purpose | |
| } | |
| } | |
| function adminSetRegistrationDisabled(bool registrationDisabled) { | |
| // currently, the code of the registry can not be updated once it is | |
| // deployed. if a newer version of the registry is available, account | |
| // registration can be disabled | |
| if (msg.sender == _registryAdmin) { | |
| _registrationDisabled = registrationDisabled; | |
| } | |
| } | |
| function adminSetAccountAdministrator(address accountAdmin) { | |
| if (msg.sender == _registryAdmin) { | |
| _accountAdmin = accountAdmin; | |
| } | |
| } | |
| function adminRetrieveDonations() { | |
| if (msg.sender == _registryAdmin) { | |
| _registryAdmin.send(this.balance); | |
| } | |
| } | |
| function adminDeleteRegistry() { | |
| if (msg.sender == _registryAdmin) { | |
| suicide(_registryAdmin); // this is a predefined function, it deletes the contract and returns all funds to the admin's address | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment