Ruby on RailsでBitcoin App開発超入門

Ruby on RailsでBitcoin App開発超入門

Gotanda.rb#42でBitcoin×RubyのLTをさせていただきました。
https://gotanda-rb.connpass.com/event/174652/

D3e027f453bb96ac1398cc98620f2920?s=128

Shu Kobuchi

May 29, 2020
Tweet

Transcript

  1. 3VCZPO3BJMTͰ #JUDPJO"QQ։ൃ௒ೖ໳ ೥݄೔ ۚ !(PUBOEBSC খᔹप 4IV,PCVDIJ

  2. ࣗݾ঺հ খᔹपɹ4IV,PCVDIJɹ!TIV@LPCTIVLPCVDIJ ·Ͱͣͬͱؔ੢ ʙલ৬ͷ4*FSͰେࡕ͔Β౦ژసۈ େ࡚  ʙϑϨηοπ จژ۠ຊڷ  w

    ҉߸ࢿ࢈औҾۀऀ޲͚΢ΥϨοτϕϯμʔ ඼઒۠೥ͪΐͬͱࡏॅɺίϩφͰϑϧϦϞʔτʹɹ 2
  3. ίϛϡχςΟ঺հ ೔ຊ҉߸௨՟ϢʔβձɹIUUQTDSZQUPDVSSFODZDPOOQBTTDPN ʮϏοτίΠϯͱ͔ษڧձʯʢ݄̍ճɿฏ೔໷ʣ w  ۚ ʙɹDMJHIUOJOH -JHIUOJOH/FUXPSL ϋϯζΦϯ ʮ҉߸௨՟ಡॻձʯʢ݄̍ճɿฏ೔໷ʣ

    w ࣍ճௐ੔த ΞʔΧΠϒಈը΋ଟ਺͋Γ·͢ 3
  4. ϏοτίΠϯͱ͔ ϒϩοΫνΣʔϯ ษڧͯ͠Έ͍ͨΜ͚ͩͲͶ

  5. ϏοτίΠϯͱ͔ ϒϩοΫνΣʔϯ ษڧͯ͠Έ͍ͨΜ͚ͩͲͶ Α͘ฉ͘ηϦϑͰ͢

  6. ϏοτίΠϯΛֶͿʹ͸ 5FTUOFUରԠ΢ΥϨοτΛೖΕͯɺ5FTUOFUίΠϯΛૹۚͯ͠༡ΜͰΈΔ -JOVYଟগͳΓͱ΋৮ΕΔͳΒ
 #JUDPJO$PSF CJUDPJOE ΛΠϯετʔ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BDUJPO

    GDCBFECDCCFCDBCEGFFFG DF⒎EE 9
  10. CJUDPJODMJͱCJUDPJO31$ϝιουͷରԠ CJUDPJODMJHFUOFXBEESFTT
 UCRDB[E[GSYZHVSWFEQSHE[XXGQ[YFKSB CJUDPJODMJTFOEUPBEESFTT UCRDB[E[GSYZHVSWFEQSHE[XXGQ[YFKSB
 GDCBFECDCCFCDBCEGFFFG DF⒎EE CJUDPJODMJHFUSBXUSBOTBDUJPO GDCBFECDCCFCDBCEGFFFG DF⒎EE

    10 bitcoinRPC('getnewaddress',[]) bitcoinRPC('sendtoaddress',[address, amount]) bitcoinRPC('getrawtransaction',[txid])
  11. DPOUSPMMFS SBJMTOFXCJUDPJO@BQQ BQQDPOUSPMMFSTCJUDPJO@BQQ@DPOUSPMMFSSC 11 require 'bitcoin' require 'net/http' require 'json'

    Bitcoin.chain_params = :regtest RPCUSER="hoge" RPCPASSWORD="hoge" HOST="localhost" PORT=18443 class BitcoinAppController < ApplicationController def index @blockchaininfo = bitcoinRPC('getblockchaininfo',[]) render template: ‘bitcoin_app/index' end private def bitcoinRPC(method,param) http = Net::HTTP.new(HOST, PORT) request = Net::HTTP::Post.new('/') request.basic_auth(RPCUSER,RPCPASSWORD) request.content_type = 'application/json' request.body = {method: method, params: param, id: 'jsonrpc'}.to_json JSON.parse(http.request(request).body)["result"] end end end bitcoinRPC('getblockchaininfo',[])
  12. WJFXͱSPVUFT BQQWJFXTCJUDPJO@BQQJOEFYIUNMFSC 12 DPOpHSPVUFTSC <td><%= @blockchaininfo %></td> Rails.application.routes.draw do get

    '/', to: 'bitcoin_app#index' get '/index', to: 'bitcoin_app#index' end SBJMTT
  13. ։ൃྫ ϒϩοΫϋογϡɺϒϩοΫߴɺτϥϯβΫγϣϯ*%ɺΞυϨεͱ͍͏छྨ Λݕࡧ૭ͭͰ͞͹͍͍ͯΔ ܧଓ։ൃதɿIUUQTHJUIVCDPNTIVLPCSBJMTCJUDPJO 13

  14. ࢀߟࢿྉ

  15. ϒϩοΫνΣʔϯΤΫεϓϩʔϥ ༷ʑͳ։ൃݩ͔ΒϒϩοΫνΣʔϯΤΫεϓϩʔϥ͕ఏڙ͞Ε͍ͯΔ IUUQTCMPDLTUSFBNJOGP IUUQTHJUIVCDPNCMPDLTUSFBNFTQMPSB w #MPDLTUSFBNFTQMPSB͸044ʢ3VCZͰ͸ͳ͍ʣ 15

  16. ϒϩοΫҰཡ ϒϩοΫνΣʔϯΤΫεϓϩʔϥͷτοϓ͸ϒϩοΫҰཡͰ͋Δ͜ͱ͕ଟ͍ τοϓͷϒϩοΫҰཡ͕JOEFYʹ͢Δ ϧʔςΟϯά΋ઃఆ CJUDPJODMJHFUCMPDLDIBJOJOGPͰ࠷৽ϒϩοΫߴΛऔಘ ࠷৽ϒϩοΫߴ͔Β߱ॱʹ CJUDPJODMJHFUCMPDLIBTICMPDLIFJHIUͰϒϩοΫϋογϡΛಘͯɺ CJUDPJODMJHFUCMPDLCMPDLIBTIͰϒϩοΫͷத਎ΛಘΔ ϒϩοΫͷ਺͕ଟ͘ͳͬͨΒɺϖʔδωʔγϣϯΛೖΕΔ 16

  17. ϒϩοΫͷ৘ใ ؆୯ HFUCMPDLCMPDLIBTIͷΑ͏ͳײ͡ͰΫΤϦʹϒϩοΫϋογϡ CJUDPJODMJHFUCMPDLCMPDLIBTI Ϩεϙϯεͷ+TPOΛ7JFXʹૹΓ͜ΜͰඞཁͳσʔλΛදࣔ HFUCMPDLͷ͍͍ͱ͜Ζ͸ঝೝ਺΍࣍ͷϒϩοΫϋογϡ΋ؚΊͯڭ͑ͯ͘ΕΔ ΋ͪΖΜલͷϒϩοΫϋογϡ΋ 17

  18. ΞϦε͕Ϙϒʹ#5$ΛૹΔ Ҏલड͚औֹ͍ͬͯͨΛ߹ࢉͯ͠ɺ૬खʹૹΓɺ͓௼Γ͸ࣗ෼΁ 59தʹهࡌͷͳֹ͍ ͜͜Ͱ͸#5$ ͸ޙʹϚΠφʔ΁ τϥϯβΫγϣϯ5SBOTBDUJPO59 18 ΞϦε Ϙϒ 5SBOTBDUJPO

    59 */165 065165 ΞϦε Ωϟϩϧ ΞϦεˠϘϒ #5$ ΩϟϩϧˠΞϦε #5$ ΞϦεˠΞϦε ͓௼Γ  #5$ #5$ σϏοτˠΞϦε #5$ #5$ σϏοτ ܭ#5$ ܭ#5$ ϚΠφʔ ωοτϫʔΫख਺ྉ #5$ ˞59ʹهࡌ͸ͳ͍ ൿີ伴Λࠩ͠ࠐΜͰ ϩοΫղআɿిࢠॺ໊ ΞϦεͷ ൿີ伴 ΞϦεͷ ൿີ伴 ΞϦεͷ ެ։伴 Ϙϒͷ ެ։伴 ΞϦεͷ ެ։伴 ΞϦεͷ ެ։伴
  19. τϥϯβΫγϣϯ৘ใ ໘౗ HFUUYUYJEͷΑ͏ͳײ͡ͰUYJEΛΫΤϦʹ CJUDPJODMJHFUSBXUSBOTBDUJPOUYJE SFTQPOTFSBXUSBOTBDUJPO CJUDPJODMJEFDPEFSBXUSBOTBDUJPOSBXUSBOTBDUJPO SFTQPOTFͷதʹ͸PVUQVUͷΞυϨεɾૹֹۚ͸ؚ·ΕΔ͕ɺJOQVUͷΞυ Ϩεɾֹ΍ख਺ྉ͸ؚ·Εͳ͍ w CJUDPJODMJHFUUSBOTBDUJPOUYJEͩͱڭ͑ͯ͘ΕΔ͕ɺ#JUDPJO$PSF

    ࣗ਎ͷXBMMFU಺ͷUYͷΈ JOQVU෦ WJO ͷUYJE WPVU͔ΒHFUSBXUSBOTBDUJPO  EFDPEFSBXUSBOTBDUJPOͰJOQVUͷΞυϨεͱֹΛٻΊΔ ख਺ྉʹJOQVU෦ͷ߹ܭֹʔPVUQVU෦ͷ߹ܭֹ 19
  20. τϥϯβΫγϣϯͷঝೝ਺ɺ࢖༻ࡁ EFDPEFSBXUSBOTBDUJPOSBXUSBOTBDUJPOͷ3FTQPODFʹ͸ϒϩοΫঝೝ਺ ͸ؚ·Ε͍ͯͳ͍ CJUDPJODMJHFUCMPDLDIBJOJOGPͰ࠷৽ͷϒϩοΫߴΛऔಘ ࠷৽ͷϒϩοΫߴ͔Β߱ॱʹHFUCMPDLIBTI HFUCMPDLͰϒϩοΫ಺ͷUYJEΛ ݟ͍͖ͯɺର৅ͷUYΛؚΉϒϩοΫ͔Βঝೝ਺ΛಘΔ CJUDPJODMJHFUUYPVUUYJEWPVU͕ଘࡏ͢Ε͹ະ࢖༻ɺͳ͚Ε͹࢖༻ࡁ 20

  21. औΓѻ͍஫ҙͷ59  $PJOCBTF59ͷ*OQVU WJOʹUYJE͕ͳ͍ͳͲͯ͠൑ผ  (FOFTJT#MPDLͷ59 CJUDPJODMJHFUSBXUSBOTBDUJPOUYJEͰऔಘͰ͖ͳ͍ ϋʔυίʔσΟϯά͞Εͨ59 59*% BFFCBBCGBBDCDGGFDDBCCBGE

    FEBC ͜ͷ59*%͸.BJOOFU 5FTUOFU 3FHUFTU 4JHOFUڞ௨ 21
  22. ΞυϨε৘ใ HFUBEESFTTBEESFTTͷΑ͏ͳײ͡ͰBEESFTTΛΫΤϦʹ ͓޷ΈͰCJUDPJODMJWBMJEBUFBEESFTTBEESFTTͰ༗ޮͳΞυϨε͔Ͳ͏͔ CJUDPJODMJHFUSBXNFNQPPMͰNFNQPPMͷUYJEΛऔಘ CJUDPJODMJHFUCMPDLDIBJOJOGPͰ࠷৽ͷϒϩοΫߴΛऔಘ ࠷৽ͷϒϩοΫߴ͔Β߱ॱʹHFUCMPDLIBTI HFUCMPDLͰϒϩοΫ಺ͷUYJEΛ औಘ HFUSBXUSBOTBDUJPOUYJE EFDPEFSBXUSBOTBDUJPOSBXUSBOTBDUJPOͰ

    WPVUʹΞυϨε͕͋Δ͔ΛݟΔ WJOͷUYJE΋HFUSBXUSBOTBDUJPOUYJE EFDPEFSBXUSBOTBDUJPO SBXUSBOTBDUJPOͰWPVUʹΞυϨε͕͋Δ͔ΛݟΔ ΞυϨεʹؔ܎͋Δ59ͷ৘ใΛ࣌ܥྻͷ߱ॱʹฒ΂Δ NFNQPPMͷ59͕Ұ൪্ ঝೝ਺ͳͲ͸τϥϯβΫγϣϯҰཡΛࢀߟʹ 22
  23. ΞυϨε৘ใ CJUDPJODMJHFUUYPVUUYJEWPVU͕ଘࡏ͢Ε͹ະ࢖༻ɺͳ͚Ε͹࢖༻ࡁ ະ࢖༻ͷ6590Λ͔͖ूΊͯ࢒ߴܭࢉ͕Մೳ $PJOCBTF59͸ঝೝ͠ͳ͍ͱ࢖͑ͳ͍ ΞυϨεҰཡը໘ͰΞυϨεʹؔ܎͢Δશ59ͷঝೝ਺·ͰٻΊ͍ͯͨΒॲཧ ͕ऴΘΒͳ͍ʢۤসʣ3FHUFTUͰ΋ ͦΖͦΖ%#ͷग़൪ 23

  24. ݕࡧ ϒϩοΫϋογϡɺϒϩοΫߴɺ59*%ɺΞυϨεͰͷݕࡧϑΟʔϧυ QPTU  ύϥϝʔλͷαΠζ͕ͷ৔߹ ϒϩοΫϋογϡ͔59*% BFFCBBCGBBDCDGGFDDBCC BGEFEBCͷ৔߹͸ɺ(FOFTJTϒϩοΫͷ59*% HFUSBXUSBOTBDUJPOUYJEͰϨεϙϯε͕͋Ε͹59 HFUCMPDLIBTICMPDLIBTIͰϨεϙϯε͕͋Ε͹ϒϩοΫ

    ύϥϝʔλͷαΠζ͕ɺ .BJOOFUɾ4JHOFUCFDI ɾ 5FTUOFUɾ 3FHUFTU CFDI ͷ͍ͣΕ͔ͷ৔߹͸ΞυϨε ύϥϝʔλ͕਺஋ͷ৔߹ɺϒϩοΫߴ HFUCMPDLIBTIQBSBNTͰϨεϙϯε͕͋Ε͹ɺϒϩοΫ ͦΕͧΕɺϒϩοΫ৘ใɺτϥϯβΫγϣϯ৘ใɺΞυϨε৘ใͷؔ਺ʹ SFEJSFDU ͍ͣΕͰ΋ͳ͍৔߹OPUGPVOEը໘ 24