Bitcoin Scriptで遊ぶ〜Bitcoin Contractの可能性を探る〜

D3e027f453bb96ac1398cc98620f2920?s=47 Shu Kobuchi
February 21, 2019

Bitcoin Scriptで遊ぶ〜Bitcoin Contractの可能性を探る〜

2019年2月21日bitbank LT Night #4 ~Blockchain~で発表させていただいた資料です。
https://bitbank.connpass.com/event/115449/

D3e027f453bb96ac1398cc98620f2920?s=128

Shu Kobuchi

February 21, 2019
Tweet

Transcript

  1. #JUDPJO4DSJQUͰ༡Ϳ ʙ#JUDPJO$POUSBDUͷՄೳੑΛ୳Δʙ ೥݄೔ CJUCBOL-5/JHIUʙ#MPDLDIBJOʙ ϑϨηοπגࣜձࣾɹখᔹप 4IV,PCVDIJ

  2. খᔹपɹ4IV,PCVDIJɹ!TIV@LPCTIVLPCVDIJ ෱Ԭݝ൧௩ࢢੜ·Εɹฌݿݝ੢ٶࢢɾๅ௩ࢢҭͪ ਆށେֶւࣄՊֶ෦ଔۀ ಸྑઌ୺Պֶٕज़େֶӃେֶ৘ใՊֶݚڀՊम࢜ྃ ৽ଔͰՈిϝʔΧʔͰιϑτ΢ΣΞ։ൃ ͦͷޙ4*FSͰͷडୗ։ൃ౳ େࡕͷϕϯνϟʔͰ#JUDPJO8FC8BMMFUࢼ࡞඼։ൃ ʙגࣜձࣾΩϡʔϒγεςϜ w ʙେࡕʹͯɺڭҭܥେن໛جװγεςϜͷ։ൃͱอक

    w ʙɹ౦ژసۈɺٕज़ઓུࣨʢϒϩοΫνΣʔϯͳͲʣ ʙϒϩοΫνΣʔϯେֶߍΤϯδχΞϒϩϯζίʔεୈظ ʙϑϨηοπגࣜձࣾͷγχΞιϑτ΢ΣΞΤϯδχΞ w Ծ૝௨՟ࣄۀऀ޲͚΢ΥϨοτ։ൃΛ୲౰ ࣗݾ঺հ !2
  3. #JUDPJO4DSJQUʹ͍ͭͯ

  4. #JUDPJO4DSJQU -*'0 -BTU*O'JSTU0VU ͷ୯७ͳεΫϦϓτ 'PSUIͷΑ͏ͳٯϙʔϥϯυه๏ #JUDPJO5SBOTBDUJPOʹ͸ඞؚͣ·ΕΔ #JUDPJO$PSF CJUDPJOE ΍ϥΠϒϥϦΛ࢖͏ͱɺ#JUDPJO4DSJQUΛҙࣝ͠ͳ ͯ͘΋ɺϓϩάϥϜ͸ॻ͚Δ

    6OMPDLJOH4DSJQUͱ-PDLJOH4DSJQUΛಉ࣌ʹ࣮ߦ ਖ਼࣮͘͠ߦͰ͖Ε͹6OMPDLJOH4DSJQUͰ-PDLJOH4DSJQUΛղআͯ͠ɺτϥϯ βΫγϣϯͷ༗ޮੑΛࣔͤΔ !4 6OMPDLJOH4DSJQU -PDLJOH4DSJQU
  5. ୯७ͳྫ 6OMPDLJOH4DSJQUɺ-PDLJOH4DSJQUͷॱͰ࣮ߦ͢Δ ελοΫʹσʔλͱ01ίʔυΛೖΕ͍ͯ͘ 01ίʔυΛελοΫʹೖΕͨͱ͖͸໋ྩʹԠͯ͡Լͷσʔλͭ·ͨ͸ෳ਺ ʹରͯ͠ԋࢉ͢Δ !5 -PDLJOH4DSJQU 6OMPDLJOH4DSJQU  01@"%%01@&26"-Λ࣮ߦ

        01@"%%  Ճࢉ        ༗ޮ 01@&26"-  01@"%%01@&26"- ಉ஋͔ ౳͍͠
  6. CJUDPJOSC CJUDPJOSCͰ͸ɺTUSJOHܕͰ#JUDPJO4DSJQUΛ࣮ߦՄೳ IUUQTHJUIVCDPNDIBJOUPQFCJUDPJOSC CJUDPJOSVCZͷޙܧϥΠϒϥϦ DIBJOUPQF$50҆౔ໜږࢯ͕։ൃ .BDͷ৔߹
 CSFXJOTUBMMMFWFMEC
 HFNJOTUBMMCJUDPJOSC
 !6 $

    irb irb(main):001:0> require 'bitcoin' => true irb(main):002:0> Bitcoin::Script.from_string("2 3 OP_ADD 5 OP_EQUAL").run => true irb(main):003:0> Bitcoin::Script.from_string("2 3 OP_ADD 6 OP_EQUAL").run => false
  7. #JUDPJOΛֶͿ্ͰಡΜͰ͓͖͍ͨॻ੶ ࢁ࡚ॏҰ࿠ɾ҆౔ໜږɾాதढ़ଠ࿠ஶ  ʰϒϩοΫνΣʔϯɾϓϩά ϥϛϯάԾ૝௨՟ೖ໳ʱߨஊࣾ ॻ੶Ͱ͸ɺCJUDPJOSVCZΛ࢖͍ͬͯͯɺҧ͏෦෼΋͋Δ͕ɺCJUDPJOSCΛ࢖͏ ্Ͱ΋ࢀߟʹ !7

  8. 11, 1BZUP1VCMJD,FZ TJH͸ൿີ伴Λ༻͍ͯੜ੒͞ΕΔॺ໊ɺ1VC,͸ެ։伴 !8 -PDLJOH4DSJQU 6OMPDLJOH4DSJQU TJH1VC,01@$)&$,4*(Λ࣮ߦ TJH 1VC, TJH

    1VC, 536& ༗ޮ $)&$,4*( TJH 1VC,01@$)&$,4*( ॺ໊Λݕূ
  9. 11,) 1BZUP1VCMJD,FZ)BTIʢ11,ͷൃల൛ʣ ެ։伴ϋογϡΛ༻͍Δ͜ͱͰΞυϨεΛ୹͘දݱͰ͖Δ TDSJQU͸ʢʣ಺Ͱͷݺͼํ΋͋Δ !9 -PDLJOH4DSJQU TDSJQU1VC,FZ 6OMPDLJOH4DSJQU TDSJQU4JH TJH

    TJH1VC,01@%6101@)"4)1VC,)BTI01@&26"-7&3*': 01@$)&$,4*(Λ࣮ߦ TJH 536& 1VC, TJH 1VC, TJH 1VC, 1VC, 1VC, 1VC,)BTI TJH 1VC, 1VC,)BTI 1VC,)BTI TJH 1VC, 1VC,)BTI 1VC,)BTI TJH 1VC, 01@%61 )"4) ෳ੡ 01@&26"- ౳͍͠ TJH1VC, 01@%6101@)"4)1VC,)BTI 01@&26"-7&3*':01@$)&$,4*( ಉ஋͔ 4)"ͷޙ 3*1&.% ෳ੡
  10. 11,)ʢଓ͖ʣ ࢒Γ͸11,ͱಉ͡ !10 -PDLJOH4DSJQU 6OMPDLJOH4DSJQU TJH 1VC, TJH 1VC, 

    536& $)&$,4*( TJH1VC, 01@%6101@)"4)1VC,)BTI 01@&26"-7&3*':01@$)&$,4*( ॺ໊Λݕূ
  11. ঝೝऀ਺ γϯάϧγά ҰਓͷखͰࢿۚΛૹۚͰ͖Δ Ϛϧνγά ๏ਓͷࢿ࢈Λෳ਺ਓͰ؅ཧ͢Δ৔߹ͳͲ PG͕Ұൠత w ଞʹPGͳͲ PGͷΑ͏ͳ৔߹ͩͱɺ伴͕̍ຊͰ΋ฆࣦ͢ΔͱࢿۚΛಈ͔ͤͳ͘ͳΔ !11

    ॺ໊ऀ" ॺ໊ऀ# ॺ໊ऀ$ ൿີ伴# ൿີ伴" ൿີ伴$ ๏ਓͷ ࣗݾࢿ࢈ ๏ਓͷ ސ٬ࢿ࢈ PG Ϛϧνγά PG Ϛϧνγά
  12. PGͷ৔߹  ະॺ໊59࡞੒  ॺ໊ऀ"͕ͭ໨ͷॺ໊  ॺ໊ऀ#͕ͭ໨ͷॺ໊  ॺ໊ࡁ59Λ
 ϒϩʔυΩϟετ

    Ϛϧνγάͷॺ໊ͰϒϩʔυΩϟετ !12 ະॺ໊59 PG ॺ໊ࡁ59 ΋͏Ұਓ͸ॺ໊ෆཁ ॺ໊ऀ$ ॺ໊ऀ" ॺ໊ऀ# ॺ໊ऀ$ͷ ॺ໊ࡁ59 ॺ໊ ॺ໊ ॺ໊ऀ#ͷ ॺ໊ࡁ59 ൿີ伴" ൿີ伴$ ผਓ͕ ૢ࡞ ॺ໊͕ ἧ͏ ϒϩʔυ Ωϟετ ൿີ伴#
  13. Ϛϧνγά NPGOʢOਓதNਓͷॺ໊ͰϩοΫղআͰ͖ΔʣϚϧνγά Α͋͘ΔPGͷྫ !13 -PDLJOH4DSJQU TDSJQU1VC,FZ 6OMPDLJOH4DSJQU TDSJQU4JH 01@TJH"4JH#1VC,"1VC,#1VC,$01@$)&$,.6-5*4*( )"4)

    01@TJH"TJH# 1VC,"1VC,#1VC,$ 01@$)&$,.6-5*4*( 536&  TJHOBUVSF#  1VCMJD,FZ$  $)&$,.6-5*4*( TJHOBUVSF" 1VCMJD,FZ# 1VCMJD,FZ" 
  14. #JUDPJO4DSJQUͷϋοΧιϯ

  15. #JUDPJO4DSJQUͷϋοΧιϯ 5PLZP#JUDPJO)BDLBUIPOʹࢀՃ IUUQTOFVUSJOPDPOOQBTTDPNFWFOU #JUDPJO4DSJQUΛ༻͍ͨϋοΧιϯ จࣈྻͷϋογϡ஋Λ༻͍ͨΫΠζήʔϜΛ࣮૷ IUUQTEPDTHPPHMFDPNQSFTFOUBUJPOER6:6#'R7/GDB D6:'I%%&%HFZPCD,F.S-NEB6 !15

  16. ΤεΫϩʔ

  17. ΤεΫϩʔͱ͸ 8JLJQFEJBʹΑΔͱɺ ΤεΫϩʔʢFTDSPXʣͱ͸ɺ঎औҾͷࡍʹ৴པͷஔ͚ΔୈࡾऀΛ஥հͤͯ͞ औҾͷ҆શΛ୲อ͢Δୈࡾऀ༬ୗͰ͋Δɻ IUUQTKBXJLJQFEJBPSHXJLJ &"&#&"'&"%&#$ !17 #MPDLDIBJO#J[ औҾͷ҆શΛอো͢ΔʮΤεΫϩʔʯ IUUQTHBJBYCMPDLDIBJODPNFTDSPX

    ϒϩοΫνΣʔϯ͸෺ཧతͳ෺ͷड ͚౉͠·Ͱอোͯ͘͠Εͳ͍ͷͰɺ ΤεΫϩʔͷΑ͏ͳ࢓૊ΈΛ࢖͏
  18. ΤεΫϩʔΛϒϩοΫνΣʔϯͰ !18 #MPDLDIBJO#J[ औҾͷ҆શΛอো͢ΔʮΤεΫϩʔʯ IUUQTHBJBYCMPDLDIBJODPNFTDSPX ֤͕ࣗѱҙΛ͍࣋ͬͯͨ৔߹ ങ͍ख
 ɾങ͍खͷૹ͕ۚ͞Εͳ͚Ε͹औҾΛ ։࢝͠ͳ͍ ചΓख


    ɾങ͍ख΋ΤεΫϩʔ΋ॺ໊ͤͣɺച Γख΁ͷૹۚΛ્ࢭ ΤεΫϩʔΤʔδΣϯτ
 ɾങ͍खͱചΓखͰॺ໊ͯ͠ૹۚͰ͖ɺ औҾ੒ཱ ˞ೋਓҎ্͕ѱҙ͕͋Ε͹࢓૊Έ͕੒ཱ ͠ͳ͍
  19. ΤεΫϩʔΛ#JUDPJO4DSJQUͰ Ϛϧνγάʹૹ͕ۚͨ͠ɺ໊ͷॺ໊͕ಘΒΕͳ͔ͬͨͱ͖ʹങ͍खʹࢿۚΛ ໭͢࢓૊Έ΋ඞཁ Լهͷྫ͸೔ؒखΛ͚ͭΒΕͳ͔ͬͨΒങ͍खʹ໭͢ !19 IF 2 <Alice's pubkey> <Bob's

    pubkey> <Escrow's pubkey> 3 CHECKMULTISIG ELSE "30d" CHECKSEQUENCEVERIFY DROP <Alice's pubkey> CHECKSIG ENDIF IUUQTHJUIVCDPNCJUDPJOCJQTCMPCNBTUFSCJQNFEJBXJLJ
  20. อݥ

  21. อݥΛ#JUDPJO4DSJQUͰ ੜ໋อݥͷΑ͏ͳ૬ޓอݥͰ͸ɺ฼਺ͷେ͖͍ϚϧνγάͱͳΓ೉ͦ͠͏ NPGOͰO͕େ͖͘ɺN΋େ͖͍ͱॺ໊͕ἧΘͳ͍ N͕খ͍͞ͱෆਖ਼͕ى͖΍͍͢ ˠ#JUDPJOʹݶΒͣϒϩοΫνΣʔϯͰ૬ޓอݥ͸೉͍͠ʁ ଛอͷ৔߹ͩͱΤεΫϩʔͷϩδοΫΛෳࡶԽͤ͞Δ͜ͱͰ࣮ݱͰ͖Δʁ ཤྺͷॻ͖ࠐΈ౳Λߟ͑Δͱ#JUDPJOͰ͸ݫ͍͠ʁ !21 IUUQTHJUIVCDPNCJUDPJOCJQTCMPCNBTUFSCJQNFEJBXJLJ IF

    2 <Alice's pubkey> <Bob's pubkey> <Escrow's pubkey> 3 CHECKMULTISIG ELSE "30d" CHECKSEQUENCEVERIFY DROP <Alice's pubkey> CHECKSIG ENDIF
  22. )5-$ೖ໳

  23. )BTIFE5JNF-PDL$POUSBDU தؒऀΛܦ༝͢ΔϚϧνϗοϓૹۚΛ࣮ݱ νϟωϧͷ։ઃ૬ख͕૿͑ΔͱɺPGϚϧνγάѼʹ͢ΔσϙδοτͰ ϩοΫ͞ΕΔ#JUDPJO͕૿͑Δ νϟωϧະ։ઃͷ૬खͱ΋νϟωϧ։ઃࡁΈͷୈࡾऀΛܦ༝ܾͯ͠ࡁ !23 σϙδοτͷ #JUDPJO͕ଟ͍ σϙδοτͷ #JUDPJOΛ཈͑ΒΕΔ

  24. தؒऀΛ৴༻ͤͣૹۚͰ͖Δ࢓૊Έ͕ඞཁ "MJDF͸$BSPMͱ௚઀νϟωϧΛష͍ͬͯͳ͍͕ɺ#PCΛ௨ͯ͠ૹ͕ۚͰ͖ͦ͏ "MJDF͕#PCʹ$BSPM΁#5$ૹۚ͢ΔΑ͏ʹݴͬͯ΋ɺ#PC͕౪ΉՄೳੑ ͸൱ఆͰ͖ͳ͍ #PC͕৴༻Ͱ͖ͳͯ͘΋ૹۚͰ͖Δ࢓૊Έ͕ඞཁ !24 "MJDF #PC $BSPM

  25. ৄ͘͠͸%SZKBࢯͷࢿྉΛ 5IBEEFVT%SZKB -JHIUOJOH/FUXPSL8IJUFQBQFSஶऀͷҰਓʣ
 IUUQTCDKQBSDIJWFTFBTPONBUFSJBMT @-JHIUOJOH@/FUXPSLQEG !25

  26. )5-$ͷ؆қతͳྫ ᶃ4FDSFU3Λ࡞੒͠ɺ4FDSFU)BTI3Λ"MJDFʹ࿈ܞ ᶄ)5-$59"Ͱɺ"MJDF͔Β#PC΁ͷࢧ෷͍Λอূ ᶅ)5-$59#ͱᶆ)5-$59$Ͱ#PC͔Β$BSPM΁ͷૹۚ ᶄ)5-$59"ͱᶇ)5-$59#Ͱ"MJDF͔Β#PC΁ͷૹۚ !26 "MJDF #PC $BSPM 4FDSFU3

    ᶃ)BTI3 ᶄ)5-$59" 4FDSFU3ͱ#PC`T1SJW,FZͰೖखՄ ·ͨ͸ɺ̎िؒܦաޙ"MJDF`T1SJW,FZ ͰೖखՄ ᶅ)5-$59# 4FDSFU3ͱ$BSPM`T1SJW,FZͰೖखՄ ·ͨ͸ɺ̎िؒܦաޙ#PC`T1SJW,FZ ͰೖखՄ ᶆ)5-$59$ 4FDSFU3ͱ$BSPM`T1SJW,FZͰೖख ᶇ)5-$59# 4FDSFU3ͱ#PC`T1SJW,FZͰೖख 4FDSFU3
  27. ࢝఺ɾऴ఺ͷ৴པ͕ͳͯ͘΋࢖͑ΔΑ͏ʹ #JUDPJOͱ4FDSFU3ͷަ׵͚ͩͰ͸ෆे෼ !27 ҆౔ໜږࢯϒϩάʮ%FWFMPQNFOUXJUIQMFBTVSFʯΑΓ IUUQTUFDINFEJBUIJOLIBUFOBCMPHDPNFOUSZ

  28. )5-$͸#*1ͰൃҊ #JUDPJO*NQSPWFNFOU1SPSPTBMʢϏοτίΠϯվળఏҊʣ
 #*1Ͱ)5-$͕ൃҊ͞Εͨ
 IUUQTHJUIVCDPNCJUDPJOCJQTCMPCNBTUFSCJQNFEJBXJLJ !28 HASH160 DUP <R-HASH> EQUAL IF

    "24h" CHECKSEQUENCEVERIFY 2DROP <Alice's pubkey> ELSE <Commit-Revocation-Hash> EQUAL NOTIF "2015/10/20 10:33" CHECKLOCKTIMEVERIFY DROP ENDIF <Bob's pubkey> ENDIF CHECKSIG HASH160 DUP <R-HASH> EQUAL SWAP <Commit-Revocation-Hash> EQUAL ADD IF <Alice's pubkey> ELSE "2015/10/20 10:33" CHECKLOCKTIMEVERIFY "24h" CHECKSEQUENCEVERIFY 2DROP <Bob's pubkey> ENDIF CHECKSIG "MJDF #PC $BSPM
  29. )5-$͸ΤεΫϩʔత -JHIUOJOH/FUXPSL͚ͩͰͳ͘ɺΦϯνΣʔϯʗΦϑνΣʔϯ໰ΘͣΤεΫϩʔ औҾʹԠ༻Ͱ͖Δ෯޿͘࢖͑Δߟ͑ํ &UIFSFVN4NBSU$POUSBDUͰ΋)5-$Λ࢖͑ͦ͏ #5$⁶&5)ͷΑ͏ͳҟछνΣʔϯؒऔҾͰ͋Δ"UPNJD4XBQʢΞτϛοΫ εϫοϓʣʹ΋)5-$͕࢖ΘΕ͍ͯΔ
 IUUQT[PPNCMDDPNBUPNJDTXBQ -JHIUOJOH/FUXPSLͷνϟωϧؒʢ"MJDF⁶#PCͷνϟωϧͱ#PC⁶$BSPMͷ νϟωϧʣऔҾ΋"UPNJD4XBQͰ͋Δ
 IUUQTUFDINFEJBUIJOLIBUFOBCMPHDPNFOUSZ

    !29
  30. ͓஌Βͤ

  31. ೔ຊ҉߸௨՟ϢʔβձɹIUUQTDSZQUPDVSSFODZDPOOQBTTDPN ॳ৺ऀʙதڃऀ޲͚ͷʮϏοτίΠϯͱ͔ษڧձʯʢ݄̍ճɿฏ೔໷ʣ w ࣍ճ͸ Ր ʙ'J/$5FDIOPMPHJFTʢ༗ָொʣ தڃऀʙ্ڃऀ޲͚ͷʮ҉߸௨՟ಡॻձʯʢ݄̍ճɿฏ೔໷ʣ w ࣍ճ͸ ݄

    ʙ)BTI)VCʢຊڷࡾஸ໨ʣͰDPOOQBTTެ։४උத DSBTIBDBEFNZ *5ܥಈը഑৴ ʹͯϏοτίΠϯͱ͔ษڧձͷΞʔΧΠϒ഑৴த
 IUUQTDSBTIBDBEFNZDPNNVOJUZDSZQUPDVSSFODZ :PV5VCFνϟϯωϧʢಡॻձʗษڧձʣIUUQTHPPHM"2&W+ ϩάϛʔ5FDI ษڧձॻ͖ى͜͠ IUUQTHPPHMK,"-3 ίϛϡχςΟ঺հ !31
  32. 04$౦ژ ΦʔϓϯιʔεΧϯϑΝϨϯε5PLZP4QSJOH IUUQTXXXPTQOKQPTDTQSJOH  ۚ ʙ ౔ !໌੕େֶ ࠷دӺɿଟຎϞϊϨʔϧʮதԝେֶɾ໌੕େֶʯӺ ೔ຊ҉߸௨՟Ϣʔβձ͸྆೔ϒʔεग़ల

     ౔ ʙηϛφʔʮ&UIFSFVNίϯτϥΫτೖ໳ʯ IUUQTXXXPTQOKQPTDTQSJOHNPEVMFTFHVJEFFWFOUQIQ FJE !32