Creating A Scrambled Alphabet Challenge Generator In Ethereum and SolidityThere is a great deal of excitement of the new Proof of Stake (PoS) methods to be used in Ethereum. If it works, it will create a revolution in the creation of truly distributed and trusted applications. We "should" see performance improve and gas prices drop. This will open up a whole new world of trusted code. Building and deploying for local testing (using Ganache)So, let’s create a scrambled alphabet application and test it locally, and then deploy to a test network. First we start with Remix (here) and enter our code: The code I have used is here: pragma solidity ^0.8.0; // SPDX-License-Identifier: MIT contract Scrambled { function scramble() view public returns (string memory) { string [5] memory phrases=["The future of the Internet, especially in expanding the range of applications, involves a much deeper degree of privacy, and authentication.", "The future is thus towards data encryption which is the science of cryptographics, and provides a mechanism for two entities to communicate securely with any other entity being able to read their messages.", "Typically all that is required is a reliable network connection. Our world is changing by the day, as traditional forms of business are being replaced, in many cases, by more reliable and faster ways of operating.", "It is one which, unlike earlier ages, encapsulates virtually the whole World. It is also one which allows the new industries to be based in any location without requiring any natural resources, or to be in any actual physical locations.", "With voting, the slow and cumbersome task of marking voting papers with the preferred candidate, is now being replaced by electronic voting. The traditional systems, though, have been around for hundreds if not thousands of years, and typically use well tried-and-tested mechanisms."]; string [26] memory alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r', 's','t','u','v','w','x','y','z']; for (uint256 i = 0; i < alphabet.length; i++) { uint256 j=random(26,i); string memory tmp=alphabet[i]; alphabet[i]=alphabet[j]; alphabet[j]=tmp; } string memory scrambled=""; for (uint256 i = 0; i < alphabet.length; i++) { scrambled=string(abi.encodePacked(scrambled, alphabet[i])); } bytes memory word=bytes(_toLower(phrases[random(phrases.length,0)])); bytes memory map=bytes(scrambled); string memory rtn="Find the message from: "; for (uint256 i=0;i<word.length;i++) { if (uint8(word[i])<97) rtn=string(abi.encodePacked(rtn, word[i])); else { uint256 pos=uint8(word[i])-uint8(97); rtn=string(abi.encodePacked(rtn, (map[pos]))); } } rtn=string(abi.encodePacked(rtn, "\n\nThe mapping is:\nabcdefghijklmnopqrstuvwxyz\n")); rtn=string(abi.encodePacked(rtn, scrambled)); return string(rtn); } function random(uint number,uint i) view internal returns(uint){ return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty, msg.sender,uint(i)))) % number; } function _toLower(string memory str) internal pure returns (string memory) { bytes memory byteStr = bytes(str); bytes memory byteLower = new bytes(byteStr.length); for (uint i = 0; i < byteStr.length; i++) { if ((uint8(byteStr[i]) >= 65) && (uint8(byteStr[i]) <= 90)) { byteLower[i] = bytes1(uint8(byteStr[i]) + 32); } else { byteLower[i] = byteStr[i]; } } return string(byteLower); } } This basically randomly selects from five phrases, and then scrambles the alphabet with 26 random moves of the characters in the alphabet. We now need to get our local blockchain. One of the best is ganache [here]. Once installed, we then start it to test locally: We then deploy our smart contract: And which will cost us some gas (0.02 ETH): We can now test our smart contract: The output is: Find the message from: ihm vninzm wv ihm poimzomi, maumxpqssr po mfuqogpob ihm zqobm wv quuspxqipwoa, podwsdma q tnxh gmmumz gmbzmm wv uzpdqxr, qog qnihmoipxqipwo. The mapping is: abcdefghijklmnopqrstuvwxyz qkxgmvbhplestowujzaindyfrc We see that ‘i’ maps to ‘t’, an ‘h’ maps to ‘h’, and an ‘m’ to an ‘e’. Thus “ihm” maps to “the”, and the full message is: The future of the Internet, especially in expanding the range of applications, involves a much deeper degree of privacy, and authentication. Building and deploying for the test network (using Ropsten)And so we have successfully locally. Now let’s deploy it to the Ropsten test network. For this, you will need to install the Metamask wallet [here], and get some free Ether for it [here]. Note, that this is not real ether, so it will not cost you anything. Once the wallet is installed, we now select the environment to be "Injected Web3" and which will link to our Metamask wallet: (just check the address for the account is matched to your Metamask ID): We then deploy: And wait for it to be mined: And then it will get deployed: Our contract is then verified: And is deployed [here]: Next we need to verify the contract: [here] We then click on ‘Verify and Publish’ and then add the details about the smart contract (such as the compiler we have used): We then add the code: And we get a successful deployment [here]: Now we can test it again on the Ropsten network: In this case we get: Find the message from: ngp lrnrvp ca ngra nmeyvua uyny phdvfbncmh egcdg ca ngp adcphdp ml dvfbnmovybgcda, yhu bvmxcupa y zpdgyhcaz lmv nem phncncpa nm dmzzrhcdynp apdrvpwf ecng yhf mngpv phncnf kpcho ykwp nm vpyu ngpcv zpaayopa. The mapping is: abcdefghijklmnopqrstuvwxyz ykduplogcjtwzhmbsvanrxeqfi And where ’n’ is ‘t’, ‘g’ is ‘h’, and ‘p’ is ‘e’. We can reset, and try again: This time we get: And so our smart contact is working. If we now want to deploy the mainnet, we just select one of the “JavaScript VM” options, and deploy: The smart contract is here, if you want to play with it. CodingPresentation |