Created
June 5, 2018 21:41
-
-
Save dima91/c2a0139616a6eeb261cbbeadbd9e4ebe to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=false&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.24; | |
import "./sharedTypes.sol"; | |
contract BaseContentManagementContract { | |
// Enum that describe type of access which an user has | |
enum AccessType {noneAccess, standardAccess, premiumAccess} | |
// Struct that indentifies a user who was granted to consume current content | |
struct GrantedUser { | |
address userAddress; | |
AccessType accessType; | |
} | |
address private authorAddress; // Address of contract's author | |
address private catalogAddress; // Address of catalog to check if determined functions are called only by catalog | |
SharedTypes.contentType private typeOfContent; // Tpe of content which this contract contains | |
string private contentTitle; // Title which identifies this contract in catalog | |
string private content; // Content of resource | |
uint private numberOfViews; // Number of viwes about content of this contract | |
mapping (string => GrantedUser) allowedUsers; // Map of users that are allowed to access this content | |
constructor (SharedTypes.contentType _conType, string _conTitle, string _cnt, address _catalogAddr) public { | |
typeOfContent = _conType; | |
authorAddress= msg.sender; | |
numberOfViews= 0; | |
contentTitle= _conTitle; | |
content= _cnt; | |
catalogAddress= _catalogAddr; | |
} | |
// ***** ***** // | |
// ***** Modifiers ***** // | |
modifier onlyCatalog () { | |
require (msg.sender == catalogAddress); | |
_; | |
} | |
modifier onlyAllowedUsers (string _username) { | |
require (allowedUsers[_username].accessType == AccessType.standardAccess || allowedUsers[_username].accessType == AccessType.premiumAccess); | |
_; | |
} | |
// ***** ***** // | |
// ***** Helper private/internal functions ***** // | |
/* This function allows subContracts of this contract to retrieve content if and only if user | |
* with addrs _userAddr is into "allowedUsers" map | |
*/ | |
function retrieveContent (string _username) internal onlyAllowedUsers(_username) returns (string) { | |
// TODO call notifyNewView on catalog | |
if (allowedUsers[_username].accessType == AccessType.standardAccess) | |
numberOfViews++; | |
allowedUsers[_username].accessType= AccessType.noneAccess; | |
return content; | |
} | |
// ***** ***** // | |
// ***** Public functions ***** // | |
// Function used by catalog to check that has the same address of catalogAddress local variable | |
function getCatalogAddress () public view returns (address) { | |
return catalogAddress; | |
} | |
function getViewsCount () public view returns (uint) { | |
return numberOfViews; | |
} | |
function getTitle () public view returns (string) { | |
return contentTitle; | |
} | |
function getType () public view returns (SharedTypes.contentType) { | |
return typeOfContent; | |
} | |
// This function allows (only to catalog) to grant access to a user with address _userAddr. | |
function grantAccessToUser (string _username, address _userAddr, bool isPremium) public onlyCatalog () { | |
if (isPremium) | |
allowedUsers[_username].accessType= AccessType.premiumAccess; | |
else | |
allowedUsers[_username].accessType= AccessType.standardAccess; | |
allowedUsers[_username].userAddress= _userAddr; | |
} | |
// ***** ***** // | |
// ***** Abastract functions ***** // | |
function consumeContent (string _username) public returns (string); | |
} |
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.24; | |
contract C { | |
function bytes32ToString(bytes32 x) constant returns (string) { | |
bytes memory bytesString = new bytes(32); | |
uint charCount = 0; | |
for (uint j = 0; j < 32; j++) { | |
byte char = byte(bytes32(uint(x) * 2 ** (8 * j))); | |
if (char != 0) { | |
bytesString[charCount] = char; | |
charCount++; | |
} | |
} | |
bytes memory bytesStringTrimmed = new bytes(charCount); | |
for (j = 0; j < charCount; j++) { | |
bytesStringTrimmed[j] = bytesString[j]; | |
} | |
return string(bytesStringTrimmed); | |
} | |
function bytes32ArrayToString(bytes32[] data) returns (string) { | |
bytes memory bytesString = new bytes(data.length * 32); | |
uint urlLength; | |
for (uint i=0; i<data.length; i++) { | |
for (uint j=0; j<32; j++) { | |
byte char = byte(bytes32(uint(data[i]) * 2 ** (8 * j))); | |
if (char != 0) { | |
bytesString[urlLength] = char; | |
urlLength += 1; | |
} | |
} | |
} | |
bytes memory bytesStringTrimmed = new bytes(urlLength); | |
for (i=0; i<urlLength; i++) { | |
bytesStringTrimmed[i] = bytesString[i]; | |
} | |
return string(bytesStringTrimmed); | |
} | |
} |
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.24; | |
pragma experimental ABIEncoderV2; | |
import "./sharedTypes.sol"; | |
import "./ownable.sol"; | |
import "./baseContentManagementContract.sol"; | |
import "./contentManagementContracts.sol"; | |
import "./stringUtils.sol"; | |
contract CatalogSmartContract is Ownable { | |
// True if 'selfdestruct' function wasn't called --> is this necessary? | |
bool active= false; | |
// Duration of premium account (in block height) | |
uint constant PREMIUM_ACCOUNT_DURATION = 256; // ~20 blocks/hour | |
// Cost of premium account (wei) --> FIXME!!! | |
uint constant PREMIUM_ACCOUNT_COST= 100 wei; | |
// Cost of each single content --> FIXME!! | |
uint constant CONTENT_COST= 10 wei; | |
// Payout for author for each content access (this value have to be minor than CONTENT_COST) --> FIXME!! | |
uint constant PAYOUT_FOR_AUTHOR= 5 wei; | |
// Maximum number of views before sending payment | |
uint constant MAX_VIEWS_LIMIT= 3; | |
// Mapping which contains a User struct for each user that hav published or have requeested a content | |
mapping (string => SharedTypes.User) usersMapping; | |
// FIXME Mapping which link address to user: needed for functions like getContent | |
mapping (address => string) addr2User; | |
// Mapping which contains an ExtendedContent struct for each content published on the platform (identified by a string) | |
mapping (string => SharedTypes.ExtendedContent) contentsMapping; | |
// Array containing addresses of contents published on the system (for loop functions) | |
string [] contentsArray; | |
// Number of contents (also index of next content in the array) | |
uint contentsCount; | |
constructor () public payable { | |
active= true; | |
contentsCount= 0; | |
} | |
// ************************************************************************************************************** // | |
// ************************************************************************************************************** // | |
// Modifiers | |
modifier isActive () { | |
require (active == true); | |
_; | |
} | |
modifier alreadyPublishedM (string _contentTitle) { | |
require (alreadyPublished(_contentTitle) == true); | |
_; | |
} | |
modifier notPublished (string _contentTitle) { | |
require (alreadyPublished(_contentTitle) == false); | |
_; | |
} | |
modifier addressNotRegistered (address _address) { | |
require (StringUtils.equal (addr2User[_address], "")); // Sembra funzionare | |
_; | |
} | |
modifier userDoesntExist (string _username) { | |
require (userExists (_username) == false); | |
_; | |
} | |
modifier userExistsM (string _username) { | |
require (userExists(_username) == true); | |
_; | |
} | |
modifier isPremiumM (string _username) { | |
require (isPremium(_username) == true); | |
_; | |
} | |
/* FIXME*/ | |
modifier enoughValue (uint _msgValue, uint _lim) { | |
require (_msgValue == _lim); | |
_; | |
} | |
// ************************************************************************************************************** // | |
// ************************************************************************************************************** // | |
// Helper private functions | |
// Function to check if a user exists or not in usersMapping | |
function userExists (string _username) private view returns (bool){ | |
return (usersMapping[_username].exists == true); | |
} | |
// Function which returns wether a content is already published on the platform | |
function alreadyPublished (string conTitle) private view returns (bool) { | |
return (contentsMapping[conTitle].exists == true); | |
} | |
// Function to register an user to system | |
function addUser (string _username, address _userAddr) userDoesntExist(_username) addressNotRegistered(_userAddr) private { | |
addr2User[_userAddr]= _username; | |
usersMapping[_username].userAddress= _userAddr; | |
usersMapping[_username].accType= SharedTypes.accountType.standard; | |
usersMapping[_username].exists= true; | |
usersMapping[_username].expirationTime= 0; | |
usersMapping[_username].latestContent = ""; | |
} | |
// Function to add new content with address 'contAddr' registered by user with address 'userAddr' | |
function addContent (string _contentTitle, address _contentAddr, string _username) notPublished(_contentTitle) private { | |
contentsCount++; | |
contentsArray.push (_contentTitle); | |
contentsMapping[_contentTitle].exists= true; | |
contentsMapping[_contentTitle].author= _username; | |
contentsMapping[_contentTitle].contractAddress= _contentAddr; | |
usersMapping[_username].latestContent= _contentTitle; | |
} | |
// ************************************************************************************************************** // | |
// ************************************************************************************************************** // | |
// Public functions | |
function killMe () public onlyOwner() { | |
active= false; | |
selfdestruct (owner); | |
} | |
// Function that allows users to registrate themselves into service | |
function registerMe (string _username) public userDoesntExist (_username) addressNotRegistered (msg.sender) { | |
addUser (_username, msg.sender); | |
} | |
// Function to link a content with the system | |
function publishContent (string _username, string _contentTitle, address _contentAddr) notPublished (_contentTitle) public { | |
if (!userExists(_username)) { | |
// Registering new user | |
addUser (_username, msg.sender); | |
} | |
// Checking wether content's catalogAddress and contentTitle are correct | |
BaseContentManagementContract remoteContract= BaseContentManagementContract (_contentAddr); | |
require (remoteContract.getCatalogAddress() == address (this)); | |
require (StringUtils.equal (remoteContract.getTitle(), _contentTitle)); | |
addContent (_contentTitle, _contentAddr, _username); | |
} | |
// TODO Function to notify new view from another content manager | |
function notifyNewView (string _contentTitle, string _username) alreadyPublishedM (_contentTitle) userExistsM (_username) public { | |
// TODO Increase and check viewsCount | |
// TODO Remove from allowedUsers | |
} | |
// ************************************************************************************************************** // | |
// ************************************************************************************************************** // | |
// Required functions | |
// Returns the number of views for each content | |
function getStatistics () public view returns (uint [], string []) { | |
uint [] memory stats= new uint[] (contentsCount); | |
string [] memory strs= new string[] (contentsCount); | |
uint i= 0; | |
string memory tmpCnt; | |
for (i=0;i<contentsCount; i++) { | |
tmpCnt= contentsArray[i]; | |
stats[i]= (BaseContentManagementContract (contentsMapping[tmpCnt].contractAddress)).getViewsCount(); | |
strs[i]= tmpCnt; | |
} | |
return (stats, strs); | |
} | |
// Returns the list of contents without the number of views | |
function getContentList () public view returns (string []) { | |
string [] memory conList= new string[] (contentsCount); | |
uint i=0; | |
for (i=0; i<contentsCount; i++) { | |
conList[i]= contentsArray[i]; | |
} | |
return conList; | |
} | |
// Returns the list of x newest contents | |
function getNewContentList (uint _n) public view returns (string []) { | |
string [] memory newest; | |
uint i= 0; | |
uint count=0; | |
if (_n>contentsCount) | |
count=contentsCount; | |
else | |
count=_n; | |
while (count!=0) { | |
newest[i]= contentsArray[contentsCount-(++i)]; | |
count--; | |
} | |
return newest; | |
} | |
// Returns the most recent content with genre x | |
function getLatestByGenre (SharedTypes.contentType _ct) public view returns (string) { | |
uint i= contentsCount; | |
bool found= false; | |
string memory reqStr= ""; | |
if (contentsCount == 0) { | |
return ""; | |
} | |
while (i != 0 && !found ) { | |
i--; | |
if ((BaseContentManagementContract(contentsMapping[contentsArray[i]].contractAddress)).getType() == _ct) { | |
found = true; | |
reqStr= (BaseContentManagementContract(contentsMapping[contentsArray[i]].contractAddress)).getTitle(); | |
} | |
} | |
return reqStr; | |
} | |
// Returns the content with genre x, which has received the maximum number of views | |
function getMostPopularByGenre (SharedTypes.contentType _ct) public view returns (string) { | |
string memory reqStr= ""; | |
uint maximum=0; | |
uint i= contentsCount; | |
if (contentsCount == 0) { | |
return ""; | |
} | |
while (i != 0) { | |
i--; | |
BaseContentManagementContract remoteContract= BaseContentManagementContract(contentsMapping[contentsArray[i]].contractAddress); | |
if (remoteContract.getType() == _ct) { | |
if (remoteContract.getViewsCount() > maximum) | |
reqStr= remoteContract.getTitle(); | |
} | |
} | |
return reqStr; | |
} | |
// Returns the most recent content of the author x | |
function getLatestByAuthor (string _author) userExistsM (_author) public view returns (string) { | |
string memory reqStr; | |
bool found= false; | |
uint i= contentsCount; | |
if (contentsCount == 0) { | |
return ""; | |
} | |
while (i != 0 && !found) { | |
i--; | |
string memory author= contentsMapping[contentsArray[i]].author; | |
if (StringUtils.equal (author, _author)) { | |
found = true; | |
reqStr= (BaseContentManagementContract(contentsMapping[contentsArray[i]].contractAddress)).getTitle(); | |
} | |
} | |
return reqStr; | |
} | |
// Returns the content with most views of the author x | |
function getMostPopularByAuthor (string _author) userExistsM (_author) public view returns (string) { | |
string memory reqStr= ""; | |
uint maximum=0; | |
uint i= contentsCount; | |
if (contentsCount == 0) { | |
return ""; | |
} | |
while (i != 0) { | |
i--; | |
BaseContentManagementContract remoteContract= BaseContentManagementContract(contentsMapping[contentsArray[i]].contractAddress); | |
if (StringUtils.equal (contentsMapping[contentsArray[i]].author, _author)) { | |
if (remoteContract.getViewsCount() > maximum) | |
reqStr= remoteContract.getTitle(); | |
} | |
} | |
return reqStr; | |
} | |
// Returns true if x holds a still valid premium account, false otherwise | |
function isPremium (string _username) userExistsM (_username) public view returns (bool) { | |
return (usersMapping[_username].accType == SharedTypes.accountType.premium); | |
} | |
// ************************************************************************************************************** // | |
// ************************************************************************************************************** // | |
// Pays for access to content x (NOT ACCESS TO CONTENT!) | |
function getContent (string _contentTitle) userExistsM(addr2User[msg.sender]) alreadyPublishedM (_contentTitle) enoughValue (msg.value, CONTENT_COST) public payable { | |
// TODO ******* --> Check preconditions (enough value..) + handle payments <-- ******* | |
(BaseContentManagementContract (contentsMapping[_contentTitle].contractAddress)).grantAccessToUser (addr2User[msg.sender], msg.sender, false); | |
// TODO Add user to allowedUsers | |
} | |
// Requests access to content x without paying, premium accounts only | |
function getContentPremium (string _contentTitle) isPremiumM (addr2User[msg.sender]) alreadyPublishedM (_contentTitle) public { | |
// TODO Check expirationTime! | |
(BaseContentManagementContract (contentsMapping[_contentTitle].contractAddress)).grantAccessToUser (addr2User[msg.sender], msg.sender, true); | |
} | |
// Pays for granting access for content x to the user u | |
function giftContent (string _contentTitle, string _receivingUser) userExistsM(_receivingUser) alreadyPublishedM (_contentTitle) enoughValue (msg.value, CONTENT_COST) public payable { | |
// ******* --> Check preconditions (enough value..) + handle payments <-- ******* | |
(BaseContentManagementContract (contentsMapping[_contentTitle].contractAddress)).grantAccessToUser (addr2User[msg.sender], usersMapping[_receivingUser].userAddress, false); | |
// TODO Add user to allowedUsers | |
} | |
// Pays for granting a Premium Account to the user u | |
function giftPremium (string _receivingUser) userExistsM(_receivingUser) enoughValue (msg.value, PREMIUM_ACCOUNT_COST) public payable { | |
// ******* --> check preconditions + handle payments <-- ******* | |
// TODO ******* --> check expirationTime | |
usersMapping[_receivingUser].accType= SharedTypes.accountType.premium; | |
} | |
// Starts a new premium subscription | |
function buyPremium () userExistsM(addr2User[msg.sender]) enoughValue (msg.value, PREMIUM_ACCOUNT_COST) public payable { | |
// ******* --> check preconditions + handle payments <-- ******* | |
// TODO ******* --> check expirationTime | |
usersMapping[addr2User[msg.sender]].accType= SharedTypes.accountType.premium; | |
} | |
} |
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.24; | |
import "./baseContentManagementContract.sol"; | |
contract SongManagementContract is BaseContentManagementContract { | |
constructor (string _title, string _cnt, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.song, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (string _username) public returns (string) { | |
return retrieveContent (_username); | |
} | |
} | |
/* ********************************************* */ | |
/* ********************************************* */ | |
/* ********************************************* */ | |
contract VideoManagementContract is BaseContentManagementContract { | |
constructor (string _title, string _cnt, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.video, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (string _username) public returns (string) { | |
return retrieveContent (_username); | |
} | |
} | |
/* ********************************************* */ | |
/* ********************************************* */ | |
/* ********************************************* */ | |
contract PhotoManagementContract is BaseContentManagementContract { | |
constructor (string _title, string _cnt, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.photo, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (string _username) public returns (string) { | |
return retrieveContent (_username); | |
} | |
} | |
/* ********************************************* */ | |
/* ********************************************* */ | |
/* ********************************************* */ | |
contract DocumentManagementContract is BaseContentManagementContract { | |
constructor (string _title, string _cnt, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.document, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (string _username) public returns (string) { | |
return retrieveContent (_username); | |
} | |
} |
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.24; | |
import "./mortal.sol"; | |
contract Crowdfund { | |
struct Funder { | |
address addr; | |
uint amount; | |
} | |
struct Project { | |
bool created; | |
bool opened; | |
string name; | |
address owner; | |
mapping (uint => Funder) funders; | |
uint fundersSize; | |
uint amount; | |
uint fundingGoal; | |
} | |
event ProjectCreated (string _name, uint _fundingGoal); | |
event FundTransferred (address _backer, string _project, uint _amount, uint _remainingAmount); | |
event fundingGoalReached (string project); | |
address private owner; | |
mapping (string => Project) private projects; | |
modifier ProjectNotExists (string _name) { | |
require (projects[_name].created == false); | |
_; | |
} | |
modifier ProjectExists (string _name) { | |
require (projects[_name].created == true); | |
_; | |
} | |
constructor () public { | |
owner= msg.sender; | |
} | |
function createProject (string _projectName, uint _amount) public ProjectNotExists (_projectName) { | |
projects[_projectName]= Project (true, true, _projectName, msg.sender, 0, 0, _amount); | |
emit ProjectCreated (_projectName, _amount); | |
} | |
function sendMoney (string _projectName, uint _sum) public ProjectExists (_projectName) { | |
Project storage p= projects[_projectName]; | |
p.funders[p.fundersSize]= Funder (msg.sender, _sum); | |
p.fundersSize++; | |
p.amount += _sum; | |
uint remaining= p.fundingGoal - _sum; | |
emit FundTransferred (msg.sender, _projectName, _sum, remaining); | |
checkGoalReached (_projectName); | |
} | |
function checkGoalReached (string _projectName) private returns (bool) { | |
Project storage p= projects[_projectName]; | |
if (p.fundingGoal <= p.amount) { | |
emit fundingGoalReached (_projectName); | |
p.opened= false; | |
return true; | |
} | |
return false; | |
} | |
function getProject (string _projectName) public view returns (bool, address, uint, uint, uint, bool) { | |
Project storage p= projects[_projectName]; | |
return (p.opened, p.owner, p.fundersSize, p.amount, p.fundingGoal, p.opened); | |
} | |
} |
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.24; | |
contract Mortal { | |
address owner; | |
constructor () public { | |
owner= msg.sender; | |
} | |
function kill () public { | |
if (msg.sender == owner) | |
selfdestruct (owner); | |
} | |
} |
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.24; | |
contract Ownable { | |
address owner; | |
constructor () public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner () { | |
require (msg.sender == owner); | |
_; | |
} | |
} |
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.24; | |
library StringUtils { | |
/// @dev Does a byte-by-byte lexicographical comparison of two strings. | |
/// @return a negative number if `_a` is smaller, zero if they are equal | |
/// and a positive numbe if `_b` is smaller. | |
function compare(string _a, string _b) private pure returns (int) { | |
bytes memory a = bytes(_a); | |
bytes memory b = bytes(_b); | |
uint minLength = a.length; | |
if (b.length < minLength) minLength = b.length; | |
//@todo unroll the loop into increments of 32 and do full 32 byte comparisons | |
for (uint i = 0; i < minLength; i ++) | |
if (a[i] < b[i]) | |
return -1; | |
else if (a[i] > b[i]) | |
return 1; | |
if (a.length < b.length) | |
return -1; | |
else if (a.length > b.length) | |
return 1; | |
else | |
return 0; | |
} | |
/// @dev Compares two strings and returns true iff they are equal. | |
function equal(string _a, string _b) public pure returns (bool) { | |
return compare(_a, _b) == 0; | |
} | |
/// @dev Finds the index of the first occurrence of _needle in _haystack | |
function indexOf(string _haystack, string _needle) public pure returns (int) | |
{ | |
bytes memory h = bytes(_haystack); | |
bytes memory n = bytes(_needle); | |
if(h.length < 1 || n.length < 1 || (n.length > h.length)) | |
return -1; | |
else if(h.length > (2**128 -1)) // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1) | |
return -1; | |
else | |
{ | |
uint subindex = 0; | |
for (uint i = 0; i < h.length; i ++) | |
{ | |
if (h[i] == n[0]) // found the first char of b | |
{ | |
subindex = 1; | |
while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) // search until the chars don't match or until we reach the end of a or b | |
{ | |
subindex++; | |
} | |
if(subindex == n.length) | |
return int(i); | |
} | |
} | |
return -1; | |
} | |
} | |
// Function to convert a bytes32 to a string | |
function bytes32ToString (bytes32 x) public pure returns (string) { | |
bytes memory bytesString = new bytes(32); | |
uint charCount = 0; | |
for (uint j = 0; j < 32; j++) { | |
byte char = byte(bytes32(uint(x) * 2 ** (8 * j))); | |
if (char != 0) { | |
bytesString[charCount] = char; | |
charCount++; | |
} | |
} | |
bytes memory bytesStringTrimmed = new bytes(charCount); | |
for (j = 0; j < charCount; j++) { | |
bytesStringTrimmed[j] = bytesString[j]; | |
} | |
return string(bytesStringTrimmed); | |
} | |
// Function to convert a string to bytes32 | |
function stringToBytes32(string memory source) public pure returns (bytes32 result) { | |
bytes memory tempEmptyStringTest = bytes(source); | |
if (tempEmptyStringTest.length == 0) { | |
return 0x0; | |
} | |
assembly { | |
result := mload(add(source, 32)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment