<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jon Evans on Medium]]></title>
        <description><![CDATA[Stories by Jon Evans on Medium]]></description>
        <link>https://medium.com/@rezendi?source=rss-d49235c9da69------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gZzj3mz93o5j4O00.jpg</url>
            <title>Stories by Jon Evans on Medium</title>
            <link>https://medium.com/@rezendi?source=rss-d49235c9da69------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 10:21:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rezendi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Not Even Remotely Possible]]></title>
            <link>https://medium.com/@rezendi/not-even-remotely-possible-4bad6a6202ad?source=rss-d49235c9da69------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bad6a6202ad</guid>
            <category><![CDATA[remote-working]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[remote-team]]></category>
            <dc:creator><![CDATA[Jon Evans]]></dc:creator>
            <pubDate>Sat, 11 Apr 2020 12:31:00 GMT</pubDate>
            <atom:updated>2020-04-11T12:31:00.903Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Exploring the benefits (and tradeoffs) of remote software development</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/0*c2toPUSbq64PRABR" /></figure><p>Down with the tyranny of geography. Down with commuting, “ <a href="https://www.theatlantic.com/national/archive/2010/03/the-secret-to-happiness-marriage-and-a-short-commute/346151/">the daily activity most injurious to human happiness</a>.” Down with allegedly “collaborative” open floor plans built such that “ <a href="https://www.bizjournals.com/sanjose/news/2017/08/08/apple-park-employees-floor-plan-hq-spaceship-aapl.html">high-level executives […] are exempt from this collaborative environment</a>.” Up with more time, greater flexibility, and, believe it or not, higher productivity.</p><p>I’m talking about remote work, of course, a subject that provokes surprising vituperation <a href="https://beta.techcrunch.com/2017/04/02/why-do-developers-who-could-work-anywhere-flock-to-the-worlds-most-expensive-cities/">whenever I write about it</a>. (Armchair-psychologist theory: people get very angry at notions which imply they have spent much of their lives needlessly making themselves very unhappy.) It’s also a subject I know more than a little something about: we at <a href="https://happyfuncorp.com/">HappyFunCorp</a> have been building software with all-remote and/or partly-remote teams for seven years now. I’ve seen what works and what doesn’t.</p><p>What <em>doesn’t</em> work includes confusing physical presence with checked-in code, or in-person meetings with productive communications, or valuing the starry-eyed magic of “serendipitous collisions” above all else and/or common sense. Don’t get me wrong, I’m not saying remote work is a panacea. It too has its failure modes. But the assumption that its failure modes are worse than those of office work, just because office work is the historical default, is sheer intellectual laziness.</p><blockquote><em>My feeling is if a company doesn’t have a way to support remote and flex work, they don’t know how to manage for results </em><a href="https://t.co/DmtkCCDwnJ"><em>https://t.co/DmtkCCDwnJ</em></a></blockquote><blockquote><em>- Christopher Mims 🎆 (@mims) </em><a href="https://twitter.com/mims/status/874948005685850112?ref_src=twsrc%5Etfw"><em>June 14, 2017</em></a></blockquote><p>We at HFC do have a headquarters in Brooklyn, where a plurality of us labor, but I’ve worked successfully with teams scattered across New Delhi, Milan, New York, Berlin, Campina Grande, and San Francisco. In this era of Slack, Zoom, and GitHub, a team of a dozen capable people across a dozen time zones can run like a well-oiled machine.</p><p>Quite apart from the time and rent saved, there’s growing evidence that remote teams can be <em>more</em> productive than in-person ones. Consider: “ <a href="https://www.gsb.stanford.edu/insights/why-working-home-future-looking-technology">We found massive, massive improvement in performance — a 13% improvement in performance from people working at home</a>.” Consider companies like Automattic, Gitlab, InVision, and Zapier, all of which <a href="https://zapier.com/blog/companies-hiring-remote-workers/">thrive</a> as <em>fully</em> remote companies.</p><p>And if still seems bizarre, weird, or experimental, consider <a href="http://www.foxbusiness.com/features/2017/06/04/why-remote-work-cant-be-stopped.html">this</a>:</p><blockquote><em>Surveys done by Gallup indicate that in 2016, the proportion of Americans who did some or all of their work from home was 43%, up from 39% in 2012. Over the same period, the proportion who only work remotely went to 20% from 15%</em></blockquote><p>The biggest transition from office to remote work isn’t the geography; that’s incidental. The biggest transition is the mode of communication, which goes from default-synchronous (walk over to your colleague’s desk) to default-asynchronous (PM them on Slack.) I certainly concede that certain forms of work, and certain people, benefit more from synchronous communications; but I put it to you that “most kinds of software development” is not among them(1), and that an ever-increasing fraction of the world’s work can be described as “most kinds of software development.”</p><p>However. Having said all that. Remote work is not without its flaws and challenges. Some people prefer a tight-knit work community to the broader but more loose-knit ones that remote work fosters, which is fair enough. “Serendipitous collisions” can and do happen among asynchronous communicators, too — posts on shared Slack channels can snowball into full-scale internal projects with surprising ease — but I think it’s fair to say that they’re more likely to occur in-person. And even the largest of all-remote companies have populations measured in the hundreds, not the thousands; once you hit a certain size you need a physical footprint.</p><p>The biggest problem of all, though, is that it’s much harder for (many) junior people to learn from asynchronous rather than synchronous communication. There are exceptions, the kinds of people who learn better from textbooks than from classes; but as a general rule, in my experience, remote work is for people who are already fairly capable and experienced. That’s fine for HFC and companies like us, we have the luxury of limiting ourselves to senior developers and/or the exceptions among juniors, but that solution doesn’t scale.</p><p>Looking at the increasing numbers, though, it seems awfully apparent that remote work is the future for a substantial fraction of the modern work force. I still find it bizarre that the notion apparently makes so many people so angry.</p><p>(1) Pair programming has its place in this world, but I spent six months working at a 100% pair-programming shop once, and it was one of the least pleasant experiences of my professional career. I recognize and celebrate that for many people it is excellent! For most of the rest of us, however, it is the polar opposite.</p><p><em>Jon Evans is </em><a href="https://happyfuncorp.com/"><em>HFC’s</em></a><em> CTO, a </em><a href="https://rezendi.com/"><em>novelist</em></a><em>, and a regular columnist at </em><a href="https://techcrunch.com/"><em>TechCrunch</em></a><em>.</em></p><p><em>Originally published on </em><a href="https://techcrunch.com/2017/08/13/not-even-remotely-possible/"><em>TechCrunch</em></a><em> in 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bad6a6202ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The tools, they are a-changing]]></title>
            <link>https://medium.com/@rezendi/the-tools-they-are-a-changing-ea4eacef7e49?source=rss-d49235c9da69------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea4eacef7e49</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software]]></category>
            <category><![CDATA[software-tools]]></category>
            <category><![CDATA[happyfuncorp]]></category>
            <dc:creator><![CDATA[Jon Evans]]></dc:creator>
            <pubDate>Fri, 21 Feb 2020 18:54:30 GMT</pubDate>
            <atom:updated>2020-02-21T18:54:30.152Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*xKkmbrJG-cXXFQzS" /></figure><p>Building web services and smartphone apps, which is most of what I’ve been doing professionally at <a href="https://happyfuncorp.com/">HappyFunCorp</a> for the last decade or so, used to be pretty straightforward. Not easy, but straightforward, especially when the client was a consumer startup, which so many of them were.</p><p>The more we did the better we got at it. Design and write two native apps, usually iOS first and Android second. Don’t skimp on the design. Connect them to a JSON API, usually written in Ruby on Rails, which also powered the web site. There’s always a web site; consumers might only see the side which is a minimal billboard for the app, but there’s essentially always also an admin site, to control features and aspects of the app.</p><p>Design isn’t as important for the admin site, so you can build that in something crude but effective like ActiveAdmin; why roll your own? Similarly, authentication is tricky and easy to get wrong, so use something like Devise, which comes with built-in hooks to Facebook and Twitter login. Design your database carefully. Use jQuery for dynamic in-browser manipulation since raw Javascript is such a nightmare. Argue about whether to use Rspec or Minitest for your server tests.</p><p>All there? OK, roll it out to your Heroku scaling environment, so you can simply “git push” to push to staging and production, with various levels of Postgres support, autoscaling, pipelines, Redis caching, Resque worker jobs, and so forth. If it’s a startup, keep them on Heroku to see if they catch on, if they find the fabled product-market fit, not least because it helps you iterate faster. If so, at some point you have to graduate them to AWS, because Heroku only scales so far and it does so very expensively. If not, well, “fail fast,” right?</p><p>Those were the days, my friends, those halcyon, long-gone days of (checks notes) five years ago. The days of a lot of grief, sure, but very little decision complexity. The smartphone boom was on, and the web boom was settling down, and everyone was still surfing those two tidal waves.</p><p>Today? Well, today we still are, neither of those waves have broken, per se, software is still eating the world, but things are … different. More of the world is being eaten, but it’s also happening more slowly, like growing 50% a year from a $1 billion base rather than 500% from $1 million. There are fewer starry-eyed founders with an app idea that they’re sure will change the world and funding enough to give it a shot. Those are still out there, sure, and more power to them, but the landscape is more complex, now.</p><p>Instead we see more big businesses, media and industrial and retail alike, realizing they must adapt and be devoured, experimenting with new tech projects with a combination of excitement and trepidation. Or requisitioning custom apps for very specific — but very useful — purposes, and requiring them to interface with their awkward pre-existing custom middleware just so. Or tech companies, even big household-name ones, outsourcing ancillary tools and projects in order to focus their in-house teams purely on their core competencies and business models. Our mix of clients has definitely shifted more towards enterprise in the last few years.</p><p>Which is not to say that startups don’t still come through our doors with bright ideas and inspiring PowerPoints on a fairly regular basis. As do super starry-eyed blockchain founders (granted, <a href="https://techcrunch.com/2018/10/07/im-very-sorry-but-youre-going-to-have-to-learn-to-love-the-blockchain/">I’m sometimes a bit starry-eyed about blockchains myself</a>) replacing the consumer-app founders of yore. I doubt we’re alone in having had a spate of blockchain startup projects late last year and early this, which has diminished to only a couple active at the moment. (Not least because the tooling is still so crude it reminds me of 90s command-line hacking.) But I strongly doubt that sphere is going away.</p><p>We haven’t dealt with as many AI projects as I would have expected by now, probably partly because AI talent is still so scarce and highly valued, and partly because it turns out a lot of seeming “AI” work can be done with simple linear regressions rather than by building and training and tuning deep-learning neural networks… although if you do those linear regressions with TensorFlow, it’s still “AI” buzzword-compliant, right? Right?</p><p>Most of all, though, the tools we use have changed. Nowadays when you want to build an app, you have to ask yourself: really native? (Java or Kotlin? Objective-C or Swift?) Or React Native? Or Xamarin? Or Google’s new Flutter thing? When you want to build a web site, you have to think: traditional? Or single-page, with React or Angular or Vue?</p><p>As for the server — Go is a lot faster than Rails, you know, and oh, that elegant concurrency handling, but, oh, where is my map/filter/reduce? Javascript is still a clumsy language, but there are certain advantages to having one language across the stack, and Node is powerful and package-rich these days. And of course you’ll want it all containerized, because while Docker definitely adds another layer or two of configuration complexity, it’s usually worth it.</p><p>Unless you want to go fully “serverless,” at least for aspects, with Amazon Lambda or Google Firebase? Even if you don’t use Firebase for a datastore, how about for authentication, huh? And if you’re all containerized, and Kubernetized if/as appropriate, though maybe let’s not go the many-microservices route until you’re sure your product-market fit justifies it, then where do you want to roll it out, AWS or Azure or Google Cloud or Digital Ocean? Or do you want to use one of their PaaS services, like App Engine or Beanstalk, which, like Heroku, sorta kinda live between “serverless” and “bare metal virtual machines”?</p><p>I oversimplify, but you get my point. We’ve never had more options, as developers, more tools available to us … and we’ve never had to struggle more with analysis paralysis, because it’s awfully hard to determine which of the possible toolsets is the best one for any particular situation. Sometimes — often — we have to be happy with just selecting a good one. And that selection problem doesn’t look like it’s going to get easier anytime soon, I’m afraid. It’s a strange time to be a coder. We live and work all tangled up in an embarrassment of riches.</p><p><em>Originally published at </em><a href="https://techcrunch.com/2018/10/28/the-tools-they-are-a-changing/"><em>https://techcrunch.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea4eacef7e49" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ethereum programming for web developers: a tutorial]]></title>
            <link>https://medium.com/@rezendi/ethereum-programming-for-web-developers-a-tutorial-9c83f35f531?source=rss-d49235c9da69------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c83f35f531</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[happyfuncorp]]></category>
            <dc:creator><![CDATA[Jon Evans]]></dc:creator>
            <pubDate>Mon, 15 Jan 2018 20:32:54 GMT</pubDate>
            <atom:updated>2018-01-19T17:04:26.981Z</atom:updated>
            <content:encoded><![CDATA[<p>by Jon Evans, CTO, HappyFunCorp (jon@happyfuncorp.com)</p><p>Hello, fellow web developer! If you’re reading this, you’re probably interested in blockchains, smart contracts, etc., as someone who actually wants to write some smart-contract code. I’m going to walk you through setting up, writing, and deploying a smart contract to a real live Ethereum blockchain, and then interacting with that contract in a browser via a web service.</p><p><strong><em>The Ethereum Virtual Machine</em></strong></p><p>I’m not going to explain Blockchains 101 or Ethereum 101: there are many other places to go for that. But it’s probably worth discussing Ethereum at a very high level from a developer’s perspective.</p><p>You don’t need to care about mining or Proof-of-Work vs. Proof-of-Stake, or anything like that. But you should know that Ethereum is a decentralized virtual machine that runs on many nodes scattered around the world, and so-called “smart contracts” are code which runs (along with data which is stored) within that virtual machine, i.e. on every single node.</p><p>This is obviously hugely inefficient, but it has advantages; everyone in the world can rely on this code/data, because no central service or system can tamper with it; and anyone can submit code/data to this machine without the registering or asking permission. They do, however, need to pay. Every line of code and byte of storage in Ethereum has a price.</p><p>Ethereum, like Bitcoin, has a native currency, called “ether”; this is the same Ether currency that is traded on exchanges like Coinbase. When used to pay for Ethereum computing/storage, it is called “gas.” For any given smart contract, gas has a “limit” and a “price.” This is pretty confusing at first, but don’t worry, you’ll wrap your head around it eventually, and anyway this tutorial uses free fake money on a so-called “testnet” Ethereum blockchain.</p><p><strong><em>Blockchain Languages</em></strong></p><p>In principle many languages can be compiled down to the bytecode used by the Ethereum VM, but in practice almost all smart contracts are written in the “Ethereum-native” language called Solidity. Solidity is still arguably somewhere between alpha-release and beta-release quality, and has lots of … idiosyncracies. (See this scathing commentary from six months ago: <a href="https://news.ycombinator.com/item?id=14691212">https://news.ycombinator.com/item?id=14691212</a>) Still, it remains the de facto state of the art.</p><p>Solidity reads a bit like Javascript. It has a lot of quirks and pitfalls, though, especially when it comes to moving money around. Be <strong><em>very</em></strong> careful if writing real money-transfer code; do your security homework, get others to review your code, and seriously consider an official security audit or even formal verification. Again, this tutorial uses fake/test money, not real money.</p><p>Solidity code runs <em>on</em> the blockchain. But talking <em>to</em> the Ethereum blockchain from external code — like, say, a web server — is another matter. In theory, you could roll your own JSON-RPC calls; in practice, you want to use an existing library. The de facto state of the art here is a Javascript library called “web3”. I’m more of a Ruby / Python / Go server-sider myself, but (for better or worse) JS is widely used and understood by web developers everywhere, so much of this tutorial is written in Node. Reminder: “the web framework named Node” and “an Ethereum node” are two completely different uses of the same word. You have been warned.</p><p>Most Solidity tutorials assume you’re running an Ethereum node on your machine, and/or one or more browser plugins. These steps add complexity &amp; cognitive overhead and are not actually necessary. This tutorial is for web developers, who are accustomed to talking to APIs running on external servers; it will get your smart contracts up and running, on a real live blockchain, without ever needing to run an Ethereum node yourself.</p><p><strong><em>Overview</em></strong></p><p>Let’s quickly go over what we’re about to cover:</p><ol><li>Selecting an Ethereum testnet; creating an address; storing its private key; financing it with fake money</li><li>Installing and understanding the Truffle development environment for Solidity</li><li>Writing your first smart contract</li><li>Testing your first smart contract</li><li>Deploying your first smart contract</li><li>Talking to your smart contract from a Node web service</li><li>End-to-end browser &lt;&gt; Node &lt;&gt; blockchain &lt;&gt; Node &lt;&gt; database data flow.</li></ol><p>Also, I’m a believer in entire tutorials contained within a single document, which this is. That said, you can also go look at the Git repository containing the final code at <a href="https://github.com/HappyFunCorp/Webthereum">https://github.com/HappyFunCorp/Webthereum</a> and witness it up and running (in a crude, raw-HTML way) on Heroku at <a href="https://webthereum.herokuapp.com/">https://webthereum.herokuapp.com/</a> though please <em>please</em> read the security discussion at the very end of this document first, or, at least, <strong>don’t</strong> enter real private keys into that app. (It would <em>probably</em> be safe to do so — enforced HTTPS connection, they’re never saved on the server side, etc. — but that’s not the point.)</p><p><strong><em>1. Testnet setup</em></strong></p><p>The “real” Ethereum blockchain is protected by a vast network of miners securing its transactions with billions of hashes per second. However there are also “testnet” blockchains, which are either less secure or are “private” i.e. controlled by a small subset of permissioned miners. We will use the Rinkeby (<a href="https://www.rinkeby.io/">https://www.rinkeby.io/</a>) testnet, an Ethereum blockchain that anyone can connect to remotely, courtesy of the fine folks at Infura (<a href="http://infura.io/">http://infura.io/</a>).</p><p>But first you need an address and private key. You can go make these online too. Just head over to MyEtherWallet (<a href="https://www.myetherwallet.com/">https://www.myetherwallet.com/</a>) and enter a password. You won’t use this for real money, so don’t worry too much about security. If you <em>were</em> using this for real money, you’d be much, much more careful about this process, though, right?</p><p>Once you’ve entered your password, you will be offered the opportunity to “<em>Download Keystore File</em>.” Do so.</p><p>Then go back to MyEtherWallet, click on “<em>View Wallet Info</em>”, select “<em>Keystore / JSON file</em>,” re-upload the file you just downloaded, and re-enter the password you just entered. Two important strings of alphanumeric characters will be revealed to you: your <em>address</em> and your <em>private key</em>. (Click on the little eye icon to reveal the latter.)</p><p>Again, if you were dealing with real amounts of real money, and/or important smart contracts, you would take significant measures to protect and secure your private key. You aren’t — but, still, you don’t want to get into the habit of hardcoding your private key into your code and/or your repo.</p><p>I strongly suggest that you make the address and the private key <em>environment variables</em>. Then later you can deploy your code to AWS or Heroku or GCE and simply set the appropriate environment variables there. This also makes it easy to have different testing / staging / production environments.</p><p>In a Bash environment like OS X, you can do this by appending these two lines to your .bash_profile in your home directory:</p><pre>export RINKEBY_ADDRESS=&quot;0x1234567890ABCDEF…&quot;</pre><pre>export RINKEBY_KEY=&quot;ABCDEF123456789…&quot;</pre><p>Setting environment variables in other environments is left as an exercise for the reader. Note that you’ll have to close and re-open your terminal window to refresh your environment. (Check with “printenv” in bash.)</p><p>Voila: you now have a valid Ethereum address, and the private key used to sign its transactions, available for use in your code. However, to actually deploy and run transactions, you’ll need (fake) money. Conveniently you can get this online, too, via the Rinkeby faucet:</p><p><a href="https://faucet.rinkeby.io/">https://faucet.rinkeby.io/</a></p><p>All you need to do is post your new Ethereum address (<em>not</em> your private key!) to Facebook or Twitter or Google+ (publicly): paste a link to that tweet/post into the input box at that faucet’s web page; and wait at most a minute or two. The minimum amount of ether you’ll receive (3) is already more than you need for basic development, but still, might as well get as much as you can.</p><p>The faucet transaction might take a minute to complete. You can check your balance via Infura’s Etherscan service:</p><p><a href="https://rinkeby.etherscan.io/address/your-address-here">https://rinkeby.etherscan.io/address/[your-address-goes-here]</a></p><p>Success? Congratulations! It is now <em>almost</em> time to move on to writing some code. First, though, we have one more thing to install and configure. Don’t worry, it’s easy; trust me, it’s worth it.</p><p><strong><em>2. Truffle</em></strong></p><p>First — you’re familiar with Javascript, right? If not … this may not be the tutorial for you. OK, you’re vaguely familiar with Node and NPM, right? If not, get thee first to a different tutorial (eg <a href="http://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm/">http://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm/</a>) and study up.</p><p>Know your away around those basics? Great. On to the Truffle Framework (<a href="http://truffleframework.com/docs/">http://truffleframework.com/docs/</a>), “your Ethereum Swiss Army Knife.” You’ll use it to compile and test your Solidity code. You can also use Truffle to deploy your code, but it needs a local Ethereum node, so we’ll deploy in a different way, described below.</p><p>Installing Truffle via NPM is as easy as</p><pre>npm install -g truffle</pre><p>It then offers various commands, the most relevant of which for this tutorial are “truffle init”, “truffle compile”, and “truffle test.” Once installed, open up a terminal window, create a new directory for your new Ethereum project — I suggest the name “webthereum” — go to it, create a “truffle” subdirectory, go to <em>it</em>, and type</p><pre>truffle init</pre><p>Voila: a scaffolding of files and directories will be created for you.</p><p>Now, open up the text editor of your choice and create a new file called “MyPrize.sol” in the “contracts” sub-subdirectory that Truffle just created for you. It’s finally time to start writing yourself some smart contracts.</p><p><strong><em>3. Baby’s First Smart Contract</em></strong></p><p>Our smart contract, MyPrize, is going to manage ownership of 10 geolocated augmented-reality entities, called “prizes.” Each prize has an owner; a location; and a “metadata” field for a URL which points to the metadata that describes its contents, i.e. what it looks like in AR. For our smart-contract purposes, all we care about is that there’s a string called “metadata.”</p><p>After a prize has been placed, it stays where it is until 1000 new blocks have been added to the blockchain (i.e. about 4 hours), and then anyone else can claim it, rewrite the metadata (e.g. what this entity looks like you point a custom AR app at the location where it has been placed) and then place it somewhere else. It’s up to some external system (e.g. a custom Android/iOS/web app talking to a web service) to handle parsing the metadata, showing the AR object, etc. Our smart contract just handles ownership and location. Simple enough. Right?</p><p>There can be many subtle complexities when writing Solidity smart contracts. Contracts can send and receive money. Contracts can spawn, own, and link to other contracts. Issues of identity, ownership, and versioning come up. It’s common for whoever first deploys a contract to be its “owner,” and for important functionality to be calved off to separate subcontracts, which the owner can replace if a bug needs to be fixed or when a new version is ready.</p><p>Does this “code written so that its owner, and/or a set of other designated admins, can control, replace, and disable it” pattern play a little awkwardly with the “smart contracts are permissionless and irrevocable with no central control” hype? You betcha! But we’re not going to worry about theoretical philosophical concerns; we’re just going to write some running code. And here it is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b059ec0080fd5224fc66f8e2fc0e1dd/href">https://medium.com/media/4b059ec0080fd5224fc66f8e2fc0e1dd/href</a></iframe><p>Save that file, go back to your command line, go back to the “truffle” subdirectory beneath your root “webthereum” directory, and type</p><pre>truffle compile</pre><p>You should see something like:</p><pre>Compiling ./contracts/MyPrize.sol…</pre><pre>Writing artifacts to ./build/contracts</pre><p><em>4. Testing Baby’s First Smart Contract</em></p><p>Writing automated tests for your software is always a good idea; but it’s a <em>really, really excellently awesome and important idea</em> for Solidity code. If you don’t find a bug until the code is on the blockchain, not only have you wasted a ton of time and a slew of money, your diagnosis and debugging options are very, very limited. Did I mention that they are expensive and time-consuming, too?</p><p>So test. To its credit, Truffle lets you write test in both Solidity and Javascript. You could make a good case for writing both kinds of tests (so that you test both the VM operations, with the former, and the JS-blockchain interface) but in the interests of expediency let’s just whip up a Solidity test for MyPrize, called TestMyPrize.sol, within the “test” sub-subdirectory Truffle created for us:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4548b195429e859a8a1baabacb74a35f/href">https://medium.com/media/4548b195429e859a8a1baabacb74a35f/href</a></iframe><p>You’ll notice that we don’t test the metadata, and that the “claim” tests are commented out. This is because we can’t actually test either of those things; Truffle’s <em>Assert.equa</em>l doesn’t work with strings returned from contract calls, and (as far as I can tell) we can’t easily advance the block number within Truffle. So the claim call will fail ungracefully if we try, teaching us nothing. I did mention that the whole ecosystem is still somewhere between alpha and beta software, right?</p><p>But we test what we can. Back to the command line, and</p><pre>truffle test</pre><p>Et voila!</p><pre>TestMyPrize</pre><pre>✓ testMyPrize (105ms)</pre><pre>1 passing (691ms)</pre><p><strong><em>5. Deploying Baby’s First Smart Contract</em></strong></p><p>So you wrote (well, you copy-and-pasted, but it’s a start) your first smart contract. You tested your first smart contract. You’ve got a blockchain address and a private key. Now how do all these ingredients bake together into a delicious cake running in production? (Pardon the mixed metaphor; I’m hungry as I type this.)</p><p>You could run an Ethereum client like Geth or Parity or MetaMask on your local machine, point Truffle to it, and simply type “truffle deploy”. But let’s go a little more low-level, and at the same time, keep you from having to run your own local node. Infura lets you submit signed Ethereum transactions to the Rinkeby testnet via the Internet, and contract deployment is just another kind of Ethereum transaction.</p><p>But you don’t want to have to deal with a slew of cryptic bash commands either. Good news: you don’t need to. You can compile and deploy entirely via Javascript, courtesy of the web3 library and the solc compiler. Let’s take a look at that in a little more detail:</p><p>First off, create a new “node” subdirectory under your root “webthereum” directory. Then go there, run</p><pre>npm init</pre><p>and tap ENTER through all the default values, or tweak them as you like. (You do have NPM installed and running on your system, right?) Next, install a few new NPM packages. Note that we’re using a particular version of web3, as the latest version released two days after this tutorial was published uses different method signatures (only temporarily, though, as I understand it, until 1.0 is formally released):</p><pre>npm install web3@1.0.0-beta.27</pre><pre>npm install solc</pre><pre>npm install fs</pre><pre>npm install ethereumjs-tx</pre><p>And now, save the following code to “deploy.js” in that newly created “node” directory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd20e4de33cde3883bfe4644a02a01a5/href">https://medium.com/media/fd20e4de33cde3883bfe4644a02a01a5/href</a></iframe><p>Note that this code depends on the RINKEBY_KEY and RINKEBY_ADDRESS environment variables discussed above, so make sure those are set and available. Once that’s done, simply return to your command line and type</p><pre>node deploy.js</pre><p>If you have followed all the above faithfully, and if the tutorial gods smile upon you, you will see a result like:</p><pre>txn count 0</pre><pre>contract deploying…</pre><pre>txn result 0x935c3a2849064d45673b6afd5989e656d5adaff0dc6644b08e33fa2cee57acd9</pre><p>Congratulations! You have successfully submitted your smart contract to the Rinkeby blockchain. (And unless there’s some unforeseen problem it will soon be mined and deployed.)</p><p>Let’s go over what we just did. First we built a web3 Ethereum client as a Javascript object, and pointed it to the Rinkeby testnet blockchain. Then, in deployContract(), we connected to that blockchain and asked it how many transactions our address had performed (and in passing verified that this connection worked.) We subsequently used that number as the “nonce” for our new transaction, which, to vastly oversimplify, ensures that each transaction is unique.</p><p>We then used the “solc” library to compile the Solidity code we previously wrote — yes, that’s right, we ran a Solidity compiler inside of Javascript — and built a dictionary with all the values required for our new Ethereum transaction. This included the address of our Ethereum account, which we previously set as an environment variable, and the compiled bytecode as its payload. We also set the gasLimit and gasPrice to reasonably generous values, to ensure that we could pay for this transaction out of the money in our account, and finally sent the assembled dictionary off to the “sendRaw” method.</p><p>“sendRaw,” in turn, converted the dictionary into a transaction object from the “ethereumjs-tx” library, signed it using the private key we created and set as an environment variable above, serialized it, and used our web3 client to actually submit the transaction to the blockchain via a JSON-RPC call over the Internet. Once accepted and mined / paid for, our MyPrize contract will be there on the blockchain forevermore, its code eternally available for anyone to call.</p><p>You can doublecheck the transaction’s status by heading over to the Etherscan service for Rinkeby (<a href="https://rinkeby.etherscan.io/">https://rinkeby.etherscan.io/</a>) and entering into its search box the transaction hash that was logged to your console. You can also use your Ethereum address to get a list of all transactions you’ve submitted. Don’t panic if this transaction doesn’t show up immediately — sometime it can take a minute or so. But it shouldn’t take longer than that.</p><p>You can get a wealth of in-depth data about your transactions from Etherscan. I encourage you to go through it in some detail. Much may not make sense at first, but, again, you’ll wrap your head around it all eventually.</p><p><strong><em>6. Node, Meet Blockchain; Blockchain, Meet Node</em></strong></p><p>Now it’s time to move from the command line to the browser. A caveat: as mentioned, I’m generally a Ruby/Rails or Python/Django or Go server guy, so what follows is probably not especially elegant or idiomatic Node code. It should get the job done though.</p><p>The principle is simple: get Node to use web3 to connect to our blockchain smart contract, so that anybody in the world can interact with it via our web service, without them having to install a browser plugin or run an Ethereum node. In fact we won’t even run a node on our server, though you would if you wanted to scale; instead we’ll just keep talking to Rinkeby via Infura.</p><p>We’re mostly just going to build a JSON API, suitable for use by a smartphone app or a modern Javascript framework like React or Angular. We will, however, also create a very simple HTML page for browser consumption, which (for now) just lets users check on the state of our ten prizes.</p><p>We’re also going to use Express and Helmet to make Node easier / better, so make sure you run</p><pre>npm install express</pre><pre>npm install helmet</pre><p>before pointing your text editor to “index1.js” within the “node” directory and dumping the following code into it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dabaef9de9d80c79416175580ca4e48f/href">https://medium.com/media/dabaef9de9d80c79416175580ca4e48f/href</a></iframe><p>This time, however, you cannot run the code out of the box. You have to fill in the “contractAddress” and “abi” values at the top first. The contract address is straightforward enough: head to Etherscan, go to the details of the transaction you submitted, and you should see a line to the effect of</p><pre>[Contract “0x8268205e3e22ccf75615b997ef91e77eeed181aa” Created]</pre><p>That’s your contract address — ie replace the current line with something like</p><pre>const contractAddress=&quot;0x8268205e3e22ccf75615b997ef91e77eeed181aa&quot;;</pre><p>Note that it’s a string, i.e. must be between quotes. You could also set and use it as an environment variable, as before, but note that the Ethereum <em>contract</em> address is different from your Ethereum <em>account</em> address.</p><p>You should now just need to run</p><pre>node index1.js</pre><p>and point your browser to localhost:8801. A very <em>very</em> simple HTML form should appear. Hit “submit” and you should get some raw JSON back, such as —</p><pre>{&quot;res&quot;:{&quot;0&quot;:&quot;1&quot;,&quot;1&quot;:&quot;0xC7679A8C55817EFA649961C6d9DF7596e4bd8C51&quot;,&quot;2&quot;:&quot;0x00000000000000000000&quot;,&quot;3&quot;:&quot;&quot;,&quot;4&quot;:&quot;0&quot;,&quot;id&quot;:&quot;1&quot;,&quot;owner&quot;:&quot;0xC7679A8C55817EFA649961C6d9DF7596e4bd8C51&quot;,&quot;geohash&quot;:&quot;0x00000000000000000000&quot;,&quot;metadata&quot;:&quot;&quot;,&quot;placedAt&quot;:&quot;0&quot;}}</pre><p>Not super-interesting, yet — but what’s important is that this is real live data coming from your Solidity smart contract, running on the Rinkeby blockchain, to the browser, via your Node service! Give yourself a pat on the back for making this happen; you’ve earned it.</p><p><strong><em>A Quick Aside</em></strong></p><p>In order to call the contract, your Node code needs to know its interface, aka its ABI, which is hardcoded above. In theory, you should be able to recompile your Solidity contract from source and use its .interface field, rather than hardcoding it. In theory. In practice that seems to not actually work.</p><p>So, if you change the MyPrize contract, do the following: Re-open deploy.js. Uncomment the “showABI()” line. Save the file. Then run “node deploy.js” again. The new contract will be deployed, to a new address, and the ABI will be logged to the console. Now, copy-and-paste that ABI back to index1.js; update the contract address (don’t use the transaction hash!) in that same file; and you’re good to go.</p><p><strong><em>7. End-To-End It, Baby</em></strong></p><p>OK, let’s put it all together; let’s run a Node web service that you can use to place prizes, to claim prizes, and, what’s more, let’s add in a Postgres database that you can use to track prize data and transaction information.</p><p>Why a database? Because a blockchain is hugely inefficient with respect to both ease-of-development and performance. The idea is to use the blockchain as a source of master truth for the data which needs to be decentralized (such as ownership and location data) and a database as a scalable read cache, and/or for data that doesn’t need to be in the blockchain.</p><p>Granted, this raises unpleasant issues like “when do you invalidate the cache / update the database?” but such is the price we pay. (One answer which springs to mind: refresh the database every time you write a value to the blockchain, and also via a daily cron job.)</p><p>Enough musing; let’s code. But first, for the below to work you’ll need to</p><pre>npm install pg</pre><pre>npm install express-enforces-ssl</pre><p>— and, more to the point, get PostgreSQL running locally on your machine, and create a database “webthereum” within it. Explaining Postgres is way out of the scope of this tutorial, I’m afraid, so this section assumes a basic degree of comfort with databases in general and Postgres specifically.</p><p>Once you’ve done that, here is a new and substantially larger file, index.js, for your node directory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcbaebb9a76a8212f2f939ab2178094a/href">https://medium.com/media/dcbaebb9a76a8212f2f939ab2178094a/href</a></iframe><p>Save that. Replace the “contractAddress = undefined” with your real address, as before. Then fire up</p><pre>node index.js</pre><p>and point your browser at localhost:8801, and you should see a slightly more full-featured (if extremely spartan and ugly) web page — one that lets you not just view prize data, but also place or claim prizes. You can also view the data stored in the local database, and check the details of what the database thinks is the most recent blockchain transaction relating to that prize.</p><p>Note however that this is highly asynchronous. When you “place” or “claim” a prize, you’re really just submitting a transaction to the blockchain; it takes a minute or so, an eternity in web time, for that transaction to be accepted or rejected. (It might be rejected because e.g. there isn’t enough ether in the sender’s account to pay for it.) In a real web app this delay must be handled by the server code, and communicated to the user, in an elegant manner.</p><p>Otherwise there isn’t a lot of conceptually new code here. As with the previous section, you use the Node service as an intermediary between the blockchain and the browser. As with deploying the contract in the first place, to write data to the blockchain you build Ethereum transactions and sign them. The big difference here is that we aren’t restricted to a single address stored in an environment variable; we let <em>anyone</em> with a blockchain address and private key perform these transactions.</p><p>On the one hand this approach arguably combines the decentralized power of blockchain apps with the accessibility and scalability of web applications. On the other, though —</p><p><strong><em>Security and the Browser &lt;&gt; Server &lt;&gt; Blockchain Pipeline</em></strong></p><p>Private keys are called that for a reason. Ethereum addresses can hold very, very significant amounts of money, and their private keys should be guarded accordingly. This means that as a general rule you should not ever be pasting them into a browser. You face potential client-side plugin attacks, Man-in-the-Middle attacks, cross-site scripting attacks, server-side hack attacks, etc etc etc.</p><p>It’s fine for this tutorial which uses fake money, of course. (Though even this tutorial makes a conscious point of enforcing HTTPS if the server is not running locally, using a password HTML field for the private key, etc.) But if you want to build a real blockchain-powered web service, you’re either going to have to be smarter about security than this, or you’ll have to outsource being smarter to your users, e.g. by telling them to only use throwaway, low-value Ethereum addresses for your service.</p><p>That last may sound callous and insecure, and it is, but in the long run maybe part of the correct security solution is for users to have “saving” Ethereum addresses, with real money, which they keep very secure, from which they occasionally transfer money to “spending” addresses which they actually use for application transactions.</p><p>Note: <em>part of</em>. Even then you shouldn’t be pasting private keys into web forms. Perhaps private keys should be decomposed into two halves, unique to a given service, when a user registers: the server maintains one half, the user enters the other half (which the server never stores) to validate each transaction, and if either gets hacked, well, each half is useless without the other. Perhaps some centralized (or even decentralized) private-key manager such as 1Password or LastPass will arise to solve this problem in general.</p><p>Regardless, the thing to remember is that the considerable value of ether currency, combined with the irrevocable nature of Ethereum transactions, means that if you’re building a <em>real</em> Ethereum web app, you have to take private-key security very seriously. You have been warned.</p><p><strong><em>That’s all, folks!</em></strong></p><p>Thus endeth this tutorial. Your thoughts and feedback are (probably) welcome, so feel free to email me or @ me on Twitter; details below. Thanks in advance —</p><p>Jon Evans</p><p><em>jon@happyfuncorp.com</em></p><p><em>twitter.com/rezendi</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c83f35f531" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>