Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel, the Blockchain, and You!

Laravel, the Blockchain, and You!

You've heard the buzz, but is blockchain a technology worth exploring? In this talk, I compare/contrast storing data on the blockchain with traditional MySQL databases, talk about some real-world companies using blockchain technology in production, and share some resources for adding blockchain to your web apps.

Samantha Geitz

March 06, 2019
Tweet

Other Decks in Programming

Transcript

  1. Laravel,
    the Blockchain,
    and YOU

    View full-size slide

  2. Things you know about blockchain technology (if you're
    anything like me 6 months ago)
    ▸ It's a buzzword that people are arguing about on Twitter/Reddit
    ▸ It has coins? Or something...?
    ▸ One of those coins is Bitcoin and I really wish I'd invested in it like 6
    years ago when I first heard about it
    ▸ I have no idea if I can use it with Laravel apps

    View full-size slide

  3. (Spoiler alert: you can use
    it with Laravel apps)

    View full-size slide

  4. But should you?

    View full-size slide

  5. Some people:

    View full-size slide

  6. But then there's
    this:

    View full-size slide

  7. So should I use a blockchain?
    Probably not
    (unless you're trying to solve a very specific problem.)

    View full-size slide

  8. Stuff I'm going to talk at you about:
    1) What is a blockchain/cryptocurrency?
    2) How is it better/worse than a MySQL database?
    3) When should you consider a blockchain for your app?
    4) How do you get started with Laravel/Vue?

    View full-size slide

  9. But before we begin...
    Visit https://chat.laracon.net/c/speaker-questions
    Find the thread called "[Samantha Geitz] Blockchain Game"
    Have it open in the background — you'll need it in 15 minutes or so!
    I will name the winner the "Elitest of the Laravel Elites" on Twitter

    View full-size slide

  10. Let's talk about MySQL
    ▸ Single database, sitting behind a server, acting as the source of
    truth for all clients connecting to an app
    ▸ To write data, you either add a new value or update an existing one
    ▸ Anyone who has access to database can change data
    ▸ If database is deleted or server goes down, data is inaccessible
    ▸ Works kinda like an Excel spreadsheet

    View full-size slide

  11. How is blockchain different?
    ▸ Blockchains are decentralized — a network is made up of various
    "nodes" (computers) with a copy of the entire database
    ▸ Apps can read data and add new records but not edit data
    ▸ To update data, new "blocks" of data are added to the chain to show
    how existing data should change

    View full-size slide

  12. ▸ Works more like a ledger with records written one after another in
    permanent ink, or a git repository with sequential commits
    ▸ If you try to edit/change/erase data in your blockchain, it will be
    different than everyone else's copy of the data history
    ▸ All nodes on network work together to ensure a consensus on data,
    which is highly secure

    View full-size slide

  13. OK, what's the catch?
    ▸ Every time anything happens, every computer on the network needs
    to come to an agreement that it took place. Meaning:
    ▸ It's slooooooooooow
    ▸ It's comparatively very expensive
    ▸ "Roughly, a good heuristic to use is that you will not be able to do
    anything that you cannot do on a smartphone from 1999.”

    View full-size slide

  14. So why would you use it?
    3 real-world examples
    (And one fake Laravel app)

    View full-size slide

  15. Problems being solved:
    ▸ With traditional currency, banks and/or governments have control
    over people's money
    ▸ Nothing to stop governments releasing an unlimited supply of money
    ▸ Credit card fraud / forged currency
    ▸ Risk of transactions being disputed/reversed

    View full-size slide

  16. How does it work?
    ▸ You want to send me a Bitcoin because you loved my talk
    ▸ Two ways to get Bitcoin: buying them on an exchange or "mining"
    them (which we will talk about later)
    ▸ Add your computer as a "node" to the network by downloading the
    Bitcoin software, including a copy of the entire history (150GB)
    ▸ This software is known as a "wallet"

    View full-size slide

  17. Is that a wallet in your pocket or are you just
    happy to see me
    ▸ Wallets do not technically store coins. They contain:
    ▸ Private key: large, secret, randomly-generated string unique to you.
    How you identify yourself — never share this with anyone!
    ▸ Public key: code created via algorithm from private key — how
    others can find you on the network

    View full-size slide

  18. How does it work?
    ▸ You enter my public key in your wallet and indicate you want to send
    me 1 bitcoin (thanks!!!)
    ▸ Your wallet tells the network that you want to send me a coin and
    "signs" the transaction info with your private key, creating a hash
    ▸ Transactions can be validated with your public key and the hash
    without ever revealing your private key

    View full-size slide

  19. Hashing in Laravel
    ▸ Your user signs up and enters "foobar" as a password
    ▸ You save it with Hash::make($password)
    ▸ It is stored in your DB as
    $2y$10$hEEF0lv4spxnvw5O4XyLZ.QjCE1tCu8HjM
    pWhmCS89J0EcSW0XELu
    ▸ Can check 'foobar' against hash; cannot decrypt hash into 'foobar'

    View full-size slide

  20. Adding data to blockchain
    ▸ Need a way to incentivize people to maintain the integrity of the
    network (which takes a lot of computing power)
    ▸ Solution: mining!
    ▸ Mining nodes validate "blocks" (groups of transactions) and add
    them to the blockchain
    ▸ Reward: bitcoins (currently, about $47,500 worth)

    View full-size slide

  21. Ready for that game I mentioned earlier?
    This is your 2-minute warning!

    View full-size slide

  22. A quick guide to mining
    ▸ There is a small cost to putting a transaction on the network
    ▸ This is basically your bidding to put their transaction in the miner's
    solved block
    ▸ The network sends these groups of transactions as a hash created
    with a nonce (short for number used once)

    View full-size slide

  23. You are all miners
    I am thinking of a number between 1 and 200
    The first person to guess it in the chat wins
    You have unlimited guesses. Go! Go! Go!

    View full-size slide

  24. A quick guide to mining
    ▸ Mining nodes guess random numbers as quickly as possible to figure
    out the correct 256-bit number
    ▸ The first miner to guess it is rewarded 12.5 bitcoins, plus the
    transaction fees. Everyone else loses.
    ▸ Verified transactions are added onto the blockchain as a new block
    ▸ More powerful hardware = better odds of guessing nonce

    View full-size slide

  25. What if you try to spend a coin twice?
    ▸ Network has a globally-accepted order of transactions
    ▸ If two transactions contradict each other, the one that ends up being
    second will be rejected and cannot become part of a block

    View full-size slide

  26. OK, but why "mining?"
    ▸ Bitcoin protocol = max of 21 million bitcoins will ever exit
    ▸ (This is expected to happen around 2140)
    ▸ New bitcoins are "dug up" as the network is maintained
    ▸ Currently, 12.5 BTC reward for successful mining
    ▸ This amount halves every 4 years or so (eventually will basically be
    only transaction fees)

    View full-size slide

  27. Bitcoin lets you send
    "money," but what about
    other data?

    View full-size slide

  28. Bitcoin vs. Ethereum
    ▸ Ethereum is another blockchain technology that allows you to easily
    build decentralized apps (Dapps)
    ▸ Has "smart contracts" that can store data / business logic and talk
    to each other
    ▸ Built in JavaScript

    View full-size slide

  29. Ethereum mining
    ▸ Similar to Bitcoin mining, except you mine "ether"
    ▸ Ether is "gas," or computational power used to add data to the
    blockchain
    ▸ When you send a transaction, you set max gas you are willing to pay
    ▸ Gas left over is refunded
    ▸ Possible to get partial credit for mining, unlike Bitcoin

    View full-size slide

  30. Microsoft
    Xbox

    View full-size slide

  31. Problems being solved:
    ▸ It is very difficult to figure out Xbox game publisher royalties
    ▸ Microsoft accountants have to deal with printed documentation and
    manual Excel spreadsheets to reconcile/recalculate royalty
    payments
    ▸ Different publishers / locales have different requirements
    ▸ It generally takes about 45 days for publishers to get their money

    View full-size slide

  32. Solution
    ▸ Smart contracts on a distributed blockchain
    ▸ Stakeholders can see how contract is set up and calculated
    ▸ Publishers get paid (and actionable data) almost immediately
    ▸ New system reduces transaction costs for publishers by 99% and
    cuts Microsoft's operating costs in half

    View full-size slide

  33. Problems being solved:
    ▸ I have a hotel room I want to rent out
    ▸ A travel agency, Expedia, Hotels.com, the hotel chain, and the
    receptionist at the front desk are all using different systems to book
    ▸ An estimated 5% of bookings are disputed
    ▸ This is a nightmare on both the accounts receivable and accounts
    payable ends

    View full-size slide

  34. Solution
    ▸ Webjet created Rezchain, an Ethereum blockchain to share booking
    data between all partners
    ▸ Not reliant on any one partner's infrastructure
    ▸ Disputes are fewer, and happen very near the time of the original
    transaction, not at the hotel or when losses are discovered

    View full-size slide

  35. Possible good uses for blockchain tech
    ▸ Voting app
    ▸ DRM management of media/games
    ▸ Any app where the verifying the identity of participants is
    important: financial, legal, doctor writing a prescription, etc.

    View full-size slide

  36. Bad use cases for blockchain tech
    ▸ Most things
    ▸ Data is in constant flux
    ▸ You want/need the ability to manually control the current state of
    data (remove/correct records)
    ▸ Speed/performance (need data in ms) is a priority

    View full-size slide

  37. Laravel Elites
    (A hypothetical v2)

    View full-size slide

  38. laravelelites.com
    ▸ The new de facto standard for hiring Laravel devs!
    ▸ Now with Github OAuth2 login via Socialite
    ▸ A few prominent people in the community can make people "Laravel
    Elite" via a certification process
    ▸ They have a secret API key and can add data to the database

    View full-size slide

  39. But what if it was on the blockchain?
    ▸ If multiple people were running copies of the blockchain, they could
    see that I edited his info
    ▸ I'd have to stick to a good old-fashioned Twitter fight

    View full-size slide

  40. Developing blockchain
    applications: stuff to know

    View full-size slide

  41. web3.js
    ▸ Main JavaScript library for interacting with Ethereum
    ▸ There are a few web3 PHP implementations as well; if you try them,
    let me know if they're good!

    View full-size slide

  42. Truffle Suite
    https://truffleframework.com

    View full-size slide

  43. Truffle Framework
    ▸ Open-source Ethereum development framework
    ▸ Manage and test your smart contracts
    ▸ Various "boxes" for quickstart with Vue, React, etc.

    View full-size slide

  44. Ganache
    ▸ Sets up a personal blockchain for local development
    ▸ Desktop app (via Electron/JS) or CLI tool
    ▸ Has built-in block explorer to see blocks/transactions

    View full-size slide

  45. Drizzle
    ▸ Collection of libraries built on top of Redux to synchronize contract
    and transaction data
    ▸ Not a lot of Vue tooling around Drizzle yet (although there are
    several Vue Truffle boxes)
    ▸ #teamreact

    View full-size slide

  46. MetaMask
    ▸ Web extension you can install via Chrome
    ▸ Manage your Ethereum private keys via web browser
    ▸ Serves as a web wallet that lets you visit blockchain apps without
    running a full Ethereum node

    View full-size slide

  47. Solidity
    ▸ Object-oriented, statically-typed language for implementing smart
    contracts
    ▸ Truffle lets you "migrate" smart contracts, compiling them to JSON
    and storing them on the blockchain

    View full-size slide

  48. Solidity
    pragma solidity >=0.4.0 <0.6.0;
    contract SimpleStorage {
    uint storedData;
    function set(uint x) public {
    storedData = x;
    }
    function get() public view returns (uint) {
    return storedData;
    }
    }

    View full-size slide

  49. CoinPress
    ▸ (My shameless self-promotion slide)
    ▸ Side project I created with @PolewskiEric
    ▸ Create your own production Ethereum blockchain in seconds
    ▸ Free node hosting
    ▸ In active development; more coming soon!

    View full-size slide

  50. CoinPress
    https://coinpress.cc

    View full-size slide

  51. Before we look at code...
    If you enjoyed my talk, follow:
    @SamanthaGeitz
    @PolewskiEric
    @CoinpressCC

    View full-size slide

  52. Coming up next: @stauffermatt

    View full-size slide

  53. Let's look at some code!

    View full-size slide