<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: YesYouKen</title>
    <description>The latest articles on DEV Community by YesYouKen (@kennethtxytqw).</description>
    <link>https://dev.to/kennethtxytqw</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F961247%2F4d9c14e1-bee3-4558-a815-15eaa49990e2.png</url>
      <title>DEV Community: YesYouKen</title>
      <link>https://dev.to/kennethtxytqw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kennethtxytqw"/>
    <language>en</language>
    <item>
      <title>Ethernaut Walkthrough - Part 1</title>
      <dc:creator>YesYouKen</dc:creator>
      <pubDate>Thu, 20 Apr 2023 17:45:28 +0000</pubDate>
      <link>https://dev.to/kennethtxytqw/ethernaut-walkthrough-part-1-o8d</link>
      <guid>https://dev.to/kennethtxytqw/ethernaut-walkthrough-part-1-o8d</guid>
      <description>&lt;p&gt;Hello I am YesYouKen, this is my first time writing a walkthrough and I am just going to write as things come. Enjoy! And, I hope this helps!&lt;/p&gt;

&lt;p&gt;Each level gets its own section and I will talk about how I get to the answer but if you want to go to the hint or the answer they are at the end of each section. &lt;/p&gt;

&lt;h2&gt;
  
  
  Levels
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Hello Ethernaut
&lt;/h3&gt;

&lt;p&gt;This one is easy. Just got to go through everything.&lt;/p&gt;

&lt;p&gt;Take a longer look at the &lt;code&gt;contract&lt;/code&gt; object to see if anything shouts password&lt;/p&gt;

&lt;p&gt;Solution:  
  how to get the password
  &lt;code&gt;contract.password()&lt;/code&gt;

&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Fallback
&lt;/h3&gt;

&lt;p&gt;Our goal is to call &lt;code&gt;contract.withdraw&lt;/code&gt; to get all the balance in the smart contract account&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Notice the &lt;code&gt;onlyOwner&lt;/code&gt; modifier

&lt;ul&gt;
&lt;li&gt;it prevents anyone other than the owner from calling &lt;code&gt;contract.withdraw&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;this is where I found out more about modifiers What are solidity modifiers?
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;There are two lines in the smart contract that allows us to change the owner of the smart contract with the following line
&lt;code&gt;owner = msg.sender;&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;in &lt;code&gt;contribute&lt;/code&gt; and &lt;code&gt;receive&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Notice that &lt;code&gt;receive&lt;/code&gt; looks a little different from other function. Did some research and found out that &lt;code&gt;receive() external payable&lt;/code&gt; is a fallback function that is called "if Ether are sent to the contract and no calldata are provided"  (ref.1)
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are two possible solutions&lt;br&gt;

  Solution A
  &lt;br&gt;

&lt;pre&gt;&lt;code&gt;&lt;span&gt;// There are two answers&lt;/span&gt;

&lt;span&gt;// Either do this multiple times, &lt;/span&gt;
&lt;span&gt;await&lt;/span&gt; &lt;span&gt;contract&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;contribute&lt;/span&gt;&lt;span&gt;({&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt; &lt;span&gt;toWei&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;0.0009&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;)})&lt;/span&gt;
&lt;span&gt;// until we have contributed more the owner&lt;/span&gt;
&lt;span&gt;await&lt;/span&gt; &lt;span&gt;contract&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;withdraw&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;br&gt;

  Solution B
  &lt;br&gt;

&lt;pre&gt;&lt;code&gt;&lt;span&gt;await&lt;/span&gt; &lt;span&gt;contract&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;contribute&lt;/span&gt;&lt;span&gt;({&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt; &lt;span&gt;toWei&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;0.00001&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;)})&lt;/span&gt;
&lt;span&gt;await&lt;/span&gt; &lt;span&gt;contract&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;sendTransaction&lt;/span&gt;&lt;span&gt;({&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt; &lt;span&gt;toWei&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;0.00001&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;)})&lt;/span&gt;
&lt;span&gt;await&lt;/span&gt; &lt;span&gt;contract&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;withdraw&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Fallout
&lt;/h3&gt;

&lt;p&gt;Once again, same goal become the owner of the contract.&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Notice that there is only one function that allows change of &lt;code&gt;owner&lt;/code&gt; which is &lt;code&gt;Fallout&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Tries to call &lt;code&gt;contract.Fallout&lt;/code&gt; but realised it is undefined.&lt;/li&gt;
&lt;li&gt;then notice the typo it is actually &lt;code&gt;Fal1out&lt;/code&gt; , the second &lt;code&gt;l&lt;/code&gt; is actually a &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Just call the following and you will become the owner
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fal1out&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Takeaways
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Turns out that it has to be a typo else the challenge will not work, the challenge simulates a typo where the constructors is misnamed and became a public function instead. &lt;/li&gt;
&lt;li&gt;Seems like a bad idea to even define constructors by the contract name, note to myself, maybe just use the &lt;code&gt;constructor&lt;/code&gt; keyword?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Coin Flip
&lt;/h3&gt;

&lt;p&gt;We need to rack up some consecutive wins by making the right guesses.&lt;/p&gt;

&lt;p&gt;I faced a few hiccups here. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I tried to be fancy here and did a &lt;code&gt;for-loop&lt;/code&gt; only to realised that the challenge prevents that with the following
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;blockhash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastHash&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;revert&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it will error out with &lt;code&gt;execution reverted&lt;/code&gt; &lt;a href="https://sepolia.etherscan.io/tx/0xe3f8500ee187297c0f4dc29e95fbcc0497758b4084880a0f68572cbffb23e539"&gt;example transaction&lt;/a&gt;&lt;br&gt;

  Solution
  &lt;br&gt;

&lt;pre&gt;&lt;code&gt;
&lt;span&gt;pragma&lt;/span&gt; &lt;span&gt;solidity&lt;/span&gt; &lt;span&gt;&amp;gt;=&lt;/span&gt;&lt;span&gt;0.8&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;2&lt;/span&gt; &lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;0.9&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;  

&lt;span&gt;contract&lt;/span&gt; &lt;span&gt;CoinFlipCheater&lt;/span&gt; &lt;span&gt;{&lt;/span&gt;
    &lt;span&gt;CoinFlip&lt;/span&gt; &lt;span&gt;coinflip&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
    &lt;span&gt;uint256&lt;/span&gt; &lt;span&gt;FACTOR&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; &lt;span&gt;57896044618658097711785492504343953926634992332820282019728792003956564819968&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
    &lt;span&gt;constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;address&lt;/span&gt; &lt;span&gt;a&lt;/span&gt;&lt;span&gt;)&lt;/span&gt; &lt;span&gt;{&lt;/span&gt;
        &lt;span&gt;coinflip&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; &lt;span&gt;CoinFlip&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;
    &lt;span&gt;}&lt;/span&gt;

    &lt;span&gt;function&lt;/span&gt; &lt;span&gt;guess&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;uint&lt;/span&gt; &lt;span&gt;times&lt;/span&gt;&lt;span&gt;)&lt;/span&gt; &lt;span&gt;external&lt;/span&gt; &lt;span&gt;{&lt;/span&gt;
            &lt;span&gt;uint256&lt;/span&gt; &lt;span&gt;blockValue&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; &lt;span&gt;uint256&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;blockhash&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;block&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;number&lt;/span&gt; &lt;span&gt;-&lt;/span&gt; &lt;span&gt;1&lt;/span&gt;&lt;span&gt;));&lt;/span&gt;
            &lt;span&gt;uint256&lt;/span&gt; &lt;span&gt;coinFlip&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; &lt;span&gt;blockValue&lt;/span&gt; &lt;span&gt;/&lt;/span&gt; &lt;span&gt;FACTOR&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
            &lt;span&gt;bool&lt;/span&gt; &lt;span&gt;side&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; &lt;span&gt;coinFlip&lt;/span&gt; &lt;span&gt;==&lt;/span&gt; &lt;span&gt;1&lt;/span&gt; &lt;span&gt;?&lt;/span&gt; &lt;span&gt;true&lt;/span&gt; &lt;span&gt;:&lt;/span&gt; &lt;span&gt;false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
            &lt;span&gt;coinflip&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;flip&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;side&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;
    &lt;span&gt;}&lt;/span&gt;
&lt;span&gt;}&lt;/span&gt;

&lt;span&gt;interface&lt;/span&gt; &lt;span&gt;CoinFlip&lt;/span&gt; &lt;span&gt;{&lt;/span&gt;
    &lt;span&gt;function&lt;/span&gt; &lt;span&gt;flip&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;bool&lt;/span&gt; &lt;span&gt;_guess&lt;/span&gt;&lt;span&gt;)&lt;/span&gt; &lt;span&gt;external&lt;/span&gt; &lt;span&gt;returns&lt;/span&gt; &lt;span&gt;(&lt;/span&gt;&lt;span&gt;bool&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;
&lt;span&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/p&gt;

&lt;h4&gt;
  
  
  Takeaways
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;external&lt;/code&gt; is like &lt;code&gt;public&lt;/code&gt; but cannot be called internally&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://blog.soliditylang.org/2020/03/26/fallback-receive-split/"&gt;https://blog.soliditylang.org/2020/03/26/fallback-receive-split/&lt;/a&gt; ^ref1&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.freecodecamp.org/news/what-are-solidity-modifiers"&gt;https://www.freecodecamp.org/news/what-are-solidity-modifiers&lt;/a&gt; ^ref2&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>web3</category>
      <category>solidity</category>
      <category>ethereum</category>
    </item>
  </channel>
</rss>
