SlideShare a Scribd company logo
Overview
Scripts in Bitcoin
Bitcoin Scripts using Node.JS
Kobi Gurkan
2016-06-11
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Outline
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Topic
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Chief Scientist at Ownership - anti-counterfeiting, supply
chain and certification
Founded Shield128 - Blockchain security company
Founded EPOK
Math geek
Blockchain and security enthusiast
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Topic
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Understand the Bitcoin scripting language
Review Bitcoin scripts, standard and non-standard
Learn to interpret, craft and test Bitcoin scripts using
Node.JS
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Topic
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
What is Bitcoin?
Created in 2008 by Satoshi Nakamoto
Uses elliptic curves - asymmetric encryption,
public/private keys
P2P
Proof of work - solves Byzantine Generals Problem in
decentralized way!
For the first time, a decentralized system of money
Ledger of transactions
Everyone sees every transaction from the creation of the
network - end of 2008
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
What is Bitcoin?
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
What is Bitcoin?
What is a Bitcoin transaction? Transfer of value from A
to B. Or that’s what they want you to believe. . .
But first, let’s review some Bitcoin constructs.
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Keys
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Keys
Bitcoin private key - random 256-bit number
Public key - derived from private key directly
Address
Version = 1 byte of 0 (zero); on the test network, this is
1 byte of 111
Key hash = Version concatenated with
RIPEMD-160(SHA-256(public key))
Checksum = 1st 4 bytes of SHA-256(SHA-256(Key
hash))
Bitcoin Address = Base58Encode(Key hash
concatenated with Checksum)
You prove ownership of an address by signing using your
private key - ECDSA
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Transactions
Source: http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.htmlKobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Transactions
Every transaction has inputs and outputs
Output - credit 100 bitcoins to someone under conditions
- scriptPubKey
Input - spend 100 bitcoins from a previous output by
fulfilling the condition - scriptSig
That is, every input spends a previous output
Where are the initial Bitcoins to spend from? As we saw
in the diagram, mining
Because the ledger is public - i.e. you can see the move
of Bitcoins from the miner up to the moment you
received them
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Source: http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Topic
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
So in reality, you can do much more.
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
117 Opcodes (some disabled, some placeholders)
1 byte
Simple language
No loops (=> no infinite loops)
Not turing-complete, intentionally
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Me
Goals
Bitcoin
Scripting language
Stack starts empty
scriptSig is evaluated
scriptPubKey is evaluated
If top of stack is non-zero then the transaction is valid
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Topic
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
5 OP_SUB
2
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
5 2 OP_SUB
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
5 OP_SUB
2
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
3
This is actually valid, because the top of the stack is
non-zero.
We could also check the result to verify that we know
math.
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
5 OP_SUB
2 3
OP_EQUAL
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
5 2 OP_SUB
3
OP_EQUAL
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
5 OP_SUB
2 3
OP_EQUAL
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
3 3
OP_EQUAL
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
3 OP_EQUAL
3
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Stack scriptSig scriptPubKey
1 (True)
Transaction valid too, and we indeed know to subtract!
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
This was easy, but might be hard to do by hand for more
complex scripts.
Luckily, BitPay did an awesome job by developing bitcore.
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
> npm install bitcore
> node
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
var bitcore = require(’bitcore’);
var interpreter = bitcore.Script.Interpreter();
interpreter.set({script: ’OP_2 OP_1 OP_SUB’})
interpreter.evaluate()
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Let’s see an interactive step session!
https://asciinema.org/a/bkznx7286g09jleitqdu641xm
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
OK, but on to more honesty. The transaction was not
valid.
There are only a few standard scripts.
But don’t despair - arbitrary scripts are still allowed. I’ll
explain later.
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Topic
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
P2PK
scriptSig scriptPubKey
<sig> <pubkey>
OP_CHECKSIG
https:
//asciinema.org/a/82dplmciis241l3slxdee5h09
Not widely used anymore because public key is. . . public.
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
P2PKH
scriptSig scriptPubKey
<sig> OP_DUP
<pubkey> OP_HASH160
OP_EQUALVERIFY
OP_CHECKSIG
Most used transaction - credit 100 Bitcoins to a hash of a
public key.
Prove you own the address by signing the entire
transaction - OP_CHECKSIG.
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Multisig
scriptSig scriptPubKey
OP_0 <m>
<A sig> <A pubkey>
[B sig] <B pubkey>
[C sig...] <C pubkey. . . >
<n>
OP_CHECKMULTISIG
Divide ownership of Bitcoins
Not widely used. Multisig is implemented as script
hash. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Null Data
scriptSig scriptPubKey
OP_RETURN
<0 to 80 bytes of data>
https:
//asciinema.org/a/0llb5rtakanx3to35lvcdul39
Can put arbitrary data on Blockchain!
https://twitter.com/op_return_ack/status/
705968000688455684
Open Assets (colored coins) - https:
//github.com/OpenAssets/open-assets-protocol/
blob/master/specification.mediawiki
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
P2SH
scriptSig scriptPubKey
<additional opcodes. . . > OP_HASH160
<redeemScript> <Hash160(redeemScript)>
OP_EQUAL
Supports arbitrary scripts!
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
P2SH-based 2-of-3 Multisig
scriptSig redeemScript scriptPubKey
OP_0 <OP_2> OP_HASH160
<A sig> <A pubkey> <PubKeyHash>
[C sig] <B pubkey> <Hash160(redeemScript)>
<redeemScript> <C pubkey. . . > OP_EQUAL
<OP_3>
OP_CHECKMULTISIG
Widely used form of Multisig
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Topic
1 Overview
Me
Goals
Bitcoin
Scripting language
2 Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Transaction Puzzle
scriptSig scriptPubKey
<data> OP_SHA256
<given_hash>
OP_EQUAL
https://live.blockcypher.com/btc/tx/
9969603dca74d14d29d1d5f56b94c7872551607f8c2d6837ab9715c60721
https://asciinema.org/a/aj92zdz3m61kjkqfg0nghxiq7
Or as P2SH:
https://asciinema.org/a/5sumtxdthpgza5f8z8s1t8jzb
But this is insecure because anyone can see the solution, since
everything is public. . .
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Secure Transaction Puzzle
scriptSig scriptPubKey
<sig> OP_SHA256
<pubkey> <given_hash>
<data> OP_EQUALVERIFY
OP_DUP
OP_HASH160
<pubkeyHash>
OP_EQUALVERIFY
OP_CHECKSIG
This is non-standard, of course. But, we can put this script as a
P2SH script.
https://asciinema.org/a/bvk8nockh7dq715qp8avhiw8h
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Zero Knowledge Contingent Payment
scriptSig scriptPubKey
OP_SHA256
<Y>
OP_EQUAL
OP_IF
<Seller pubkey>
OP_ELSE
<block_height + 100>
OP_CHECKLOCKTIMEVERIFY
OP_DROP
<Buyer pubkey>
OP_ENDIF
OP_CHECKSIG
https://bitcoincore.org/en/2016/02/26/zero-knowledge-contingent-payments-announcement/
Deposit - https://live.blockcypher.com/btc/tx/
8e5df5f792ac4e98cca87f10aba7947337684a5a0a7333ab897fb9c9d616ba9e
Spend - https://live.blockcypher.com/btc/tx/
200554139d1e3fe6e499f6ffb0b6e01e706eb8c897293a7f6a26d25e39623fae/
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
OP_CHECKLOCKTIMEVERIFY
scriptSig scriptPubKey
OP_IF
<now + 3 months>
OP_CHECKLOCKTIMEVERIFY
OP_DROP
<Lenny’s pubkey>
OP_CHECKSIGVERIFY
1
OP_ELSE
2
OP_ENDIF
<Alice’s pubkey>
<Bob’s pubkey>
2
OP_CHECKMULTISIG
Allows to lock Bitcoins until a specific date!
https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
At any time:
scriptSig scriptPubKey
0
<Alice’s signature>
<Bob’s signature>
0
After 3 months:
scriptSig scriptPubKey
0
<Alice/Bob’s signature>
<Lenny’s signature>
1
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Puzzle time!
var bitcore = require(’bitcore’);
var request = require(’request’);
var utxo = {
’txId’ : ’0c5d827a24b0822abbff73f1adbfd2be7efaf4e368b4baac7e280865eb13f497’,
’outputIndex’ : 1,
’script’ : ’a91431f2ae5b333c56a4f01df6209382ec8f892e4f3687’,
’satoshis’ : 1000000
};
//you can see it here:
//https://live.blockcypher.com/btc/tx/0c5d827a24b0822abbff73f1adbfd2be7efaf4e368b4baac7e280865eb13f497
var address = ’YOUR_ADDRESS’;
var tx = new bitcore.Transaction().from(utxo).to(address, 900000);
var redeemScript = bitcore.Script.fromASM(’
OP_SHA256 
1bc273366856a5fb0bfd0611f57b7d4ae8e709dbe30fa207729f74716fd4e877
OP_EQUAL’);
//this is your task! what is the total number of Bitcoins that will
//ever be created, in hex format (don’t forget the leading zero!)?
var solutionScript = ’’;
var scriptSig = bitcore.Script.fromASM(solutionScript + ’ ’ + redeemScript);
tx.inputs[0].setScript(scriptSig);
var rawTx = tx.toString(’hex’);
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
var pushTx = {
tx: rawTx
};
//for simplicity, we’ll use the awesome BlockCypher API.
//You could also submit this raw transaction using Bitcoin Core.
request({
url: ’https://api.blockcypher.com/v1/btc/main/txs/push’,
method: ’POST’,
json: true,
body: pushTx
}, function (err, response, body) {
if (err) {
return console.log(’Error: ’, err);
}
console.log(body);
});
Kobi Gurkan Bitcoin Scripts using Node.JS
Overview
Scripts in Bitcoin
Simple example
Standard Scripts
Special. . .
Questions?
kobigurk@gmail.com
kobigurk.com
@kobigurk
We are hiring!
Kobi Gurkan Bitcoin Scripts using Node.JS

More Related Content

PDF
New c sharp4_features_part_vi
PDF
3. inteligencia artificial ramas
PDF
A Global Network for Deep Learning: the Case of Uruguay
PDF
Dispositivos y técnicas
PDF
Control remoto IR
PDF
Hard disk encoder
PDF
Electrónica 5 – electrónica digital
PDF
Electrónica 1 – fundamentos 1
New c sharp4_features_part_vi
3. inteligencia artificial ramas
A Global Network for Deep Learning: the Case of Uruguay
Dispositivos y técnicas
Control remoto IR
Hard disk encoder
Electrónica 5 – electrónica digital
Electrónica 1 – fundamentos 1

Viewers also liked (15)

PDF
Microcontroladores 3 – conversor A/D; DSP
PDF
Usando el Attiny85
PDF
Electrónica 3 – componentes activos y semiconductores
PDF
Audio con Arduino
PDF
Microcontroladores 6 – interrupciones
PDF
Microcontroladores 1 – arquitectura
PDF
Óhmetro con Arduino
PDF
Dimmer de 230V AC con Arduino
PDF
Electrónica 4 – fuentes y baterías
PDF
Fuente conmutada con Arduino
PDF
Microcontroladores 5 – comunicación (SPI & I2C)
PDF
Microcontroladores 2 – GPIO y PWM
PDF
Microcontroladores 4 – comunicación (uart)
PDF
Electrónica 2 – fundamentos 2; componentes pasivos
PDF
Capitulo 1 Ciencia y Tecnologia. 4o Congreso Internacional Multidisciplinario...
Microcontroladores 3 – conversor A/D; DSP
Usando el Attiny85
Electrónica 3 – componentes activos y semiconductores
Audio con Arduino
Microcontroladores 6 – interrupciones
Microcontroladores 1 – arquitectura
Óhmetro con Arduino
Dimmer de 230V AC con Arduino
Electrónica 4 – fuentes y baterías
Fuente conmutada con Arduino
Microcontroladores 5 – comunicación (SPI & I2C)
Microcontroladores 2 – GPIO y PWM
Microcontroladores 4 – comunicación (uart)
Electrónica 2 – fundamentos 2; componentes pasivos
Capitulo 1 Ciencia y Tecnologia. 4o Congreso Internacional Multidisciplinario...
Ad

Similar to Bitcoin Scripts using Node.JS, Kobi Gurkan (12)

PPTX
Introduction to Bitcoin's Scripting Language
PPTX
Bitcoin Technology Presentation No.1.pptx
PDF
Bitcoin protocol for developers at techfest
PPTX
Script
PDF
Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)
PPTX
Presentation topalidis giorgos
PPTX
Presentation_Topalidis_Giorgos
PDF
Bitcoin’s blockchain - from hashes to Escrow and beyond
PDF
Bitcoin, the Blockchain, and Open Source
PPTX
Bitcoin developer guide
PDF
Bitcoin protocol for developerBitcoin Protocol for Developers
PPTX
以比特幣為例的區塊鏈技術介紹 ( Intro to Blockchain using Bitcoin as an example)
Introduction to Bitcoin's Scripting Language
Bitcoin Technology Presentation No.1.pptx
Bitcoin protocol for developers at techfest
Script
Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)
Presentation topalidis giorgos
Presentation_Topalidis_Giorgos
Bitcoin’s blockchain - from hashes to Escrow and beyond
Bitcoin, the Blockchain, and Open Source
Bitcoin developer guide
Bitcoin protocol for developerBitcoin Protocol for Developers
以比特幣為例的區塊鏈技術介紹 ( Intro to Blockchain using Bitcoin as an example)
Ad

More from WithTheBest (20)

PDF
Riccardo Vittoria
PPTX
Recreating history in virtual reality
PDF
Engaging and sharing your VR experience
PDF
How to survive the early days of VR as an Indie Studio
PDF
Mixed reality 101
PDF
Unlocking Human Potential with Immersive Technology
PPTX
Building your own video devices
PPTX
Maximizing performance of 3 d user generated assets in unity
PPTX
Wizdish rovr
PPTX
Haptics & amp; null space vr
PPTX
How we use vr to break the laws of physics
PPTX
The Virtual Self
PPTX
You dont have to be mad to do VR and AR ... but it helps
PDF
Omnivirt overview
PDF
VR Interactions - Jason Jerald
PDF
Japheth Funding your startup - dating the devil
PDF
Transported vr the virtual reality platform for real estate
PDF
Measuring Behavior in VR - Rob Merki Cognitive VR
PDF
Global demand for Mixed Realty (VR/AR) content is about to explode.
PDF
VR, a new technology over 40,000 years old
Riccardo Vittoria
Recreating history in virtual reality
Engaging and sharing your VR experience
How to survive the early days of VR as an Indie Studio
Mixed reality 101
Unlocking Human Potential with Immersive Technology
Building your own video devices
Maximizing performance of 3 d user generated assets in unity
Wizdish rovr
Haptics & amp; null space vr
How we use vr to break the laws of physics
The Virtual Self
You dont have to be mad to do VR and AR ... but it helps
Omnivirt overview
VR Interactions - Jason Jerald
Japheth Funding your startup - dating the devil
Transported vr the virtual reality platform for real estate
Measuring Behavior in VR - Rob Merki Cognitive VR
Global demand for Mixed Realty (VR/AR) content is about to explode.
VR, a new technology over 40,000 years old

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Empathic Computing: Creating Shared Understanding
PPTX
A Presentation on Artificial Intelligence
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Electronic commerce courselecture one. Pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Empathic Computing: Creating Shared Understanding
A Presentation on Artificial Intelligence
Spectral efficient network and resource selection model in 5G networks
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Electronic commerce courselecture one. Pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Bitcoin Scripts using Node.JS, Kobi Gurkan

  • 1. Overview Scripts in Bitcoin Bitcoin Scripts using Node.JS Kobi Gurkan 2016-06-11 Kobi Gurkan Bitcoin Scripts using Node.JS
  • 2. Overview Scripts in Bitcoin Outline 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 3. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Topic 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 4. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Chief Scientist at Ownership - anti-counterfeiting, supply chain and certification Founded Shield128 - Blockchain security company Founded EPOK Math geek Blockchain and security enthusiast Kobi Gurkan Bitcoin Scripts using Node.JS
  • 5. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Kobi Gurkan Bitcoin Scripts using Node.JS
  • 6. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Topic 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 7. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Understand the Bitcoin scripting language Review Bitcoin scripts, standard and non-standard Learn to interpret, craft and test Bitcoin scripts using Node.JS Kobi Gurkan Bitcoin Scripts using Node.JS
  • 8. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Topic 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 9. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Kobi Gurkan Bitcoin Scripts using Node.JS
  • 10. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language What is Bitcoin? Created in 2008 by Satoshi Nakamoto Uses elliptic curves - asymmetric encryption, public/private keys P2P Proof of work - solves Byzantine Generals Problem in decentralized way! For the first time, a decentralized system of money Ledger of transactions Everyone sees every transaction from the creation of the network - end of 2008 Kobi Gurkan Bitcoin Scripts using Node.JS
  • 11. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language What is Bitcoin? Kobi Gurkan Bitcoin Scripts using Node.JS
  • 12. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language What is Bitcoin? What is a Bitcoin transaction? Transfer of value from A to B. Or that’s what they want you to believe. . . But first, let’s review some Bitcoin constructs. Kobi Gurkan Bitcoin Scripts using Node.JS
  • 13. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Keys Kobi Gurkan Bitcoin Scripts using Node.JS
  • 14. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Keys Bitcoin private key - random 256-bit number Public key - derived from private key directly Address Version = 1 byte of 0 (zero); on the test network, this is 1 byte of 111 Key hash = Version concatenated with RIPEMD-160(SHA-256(public key)) Checksum = 1st 4 bytes of SHA-256(SHA-256(Key hash)) Bitcoin Address = Base58Encode(Key hash concatenated with Checksum) You prove ownership of an address by signing using your private key - ECDSA Kobi Gurkan Bitcoin Scripts using Node.JS
  • 15. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Transactions Source: http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.htmlKobi Gurkan Bitcoin Scripts using Node.JS
  • 16. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Transactions Every transaction has inputs and outputs Output - credit 100 bitcoins to someone under conditions - scriptPubKey Input - spend 100 bitcoins from a previous output by fulfilling the condition - scriptSig That is, every input spends a previous output Where are the initial Bitcoins to spend from? As we saw in the diagram, mining Because the ledger is public - i.e. you can see the move of Bitcoins from the miner up to the moment you received them Kobi Gurkan Bitcoin Scripts using Node.JS
  • 17. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Source: http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html Kobi Gurkan Bitcoin Scripts using Node.JS
  • 18. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Kobi Gurkan Bitcoin Scripts using Node.JS
  • 19. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Topic 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 20. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language So in reality, you can do much more. Kobi Gurkan Bitcoin Scripts using Node.JS
  • 21. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language 117 Opcodes (some disabled, some placeholders) 1 byte Simple language No loops (=> no infinite loops) Not turing-complete, intentionally Kobi Gurkan Bitcoin Scripts using Node.JS
  • 22. Overview Scripts in Bitcoin Me Goals Bitcoin Scripting language Stack starts empty scriptSig is evaluated scriptPubKey is evaluated If top of stack is non-zero then the transaction is valid Kobi Gurkan Bitcoin Scripts using Node.JS
  • 23. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Topic 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 24. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 5 OP_SUB 2 Kobi Gurkan Bitcoin Scripts using Node.JS
  • 25. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 5 2 OP_SUB Kobi Gurkan Bitcoin Scripts using Node.JS
  • 26. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 5 OP_SUB 2 Kobi Gurkan Bitcoin Scripts using Node.JS
  • 27. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 3 This is actually valid, because the top of the stack is non-zero. We could also check the result to verify that we know math. Kobi Gurkan Bitcoin Scripts using Node.JS
  • 28. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 5 OP_SUB 2 3 OP_EQUAL Kobi Gurkan Bitcoin Scripts using Node.JS
  • 29. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 5 2 OP_SUB 3 OP_EQUAL Kobi Gurkan Bitcoin Scripts using Node.JS
  • 30. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 5 OP_SUB 2 3 OP_EQUAL Kobi Gurkan Bitcoin Scripts using Node.JS
  • 31. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 3 3 OP_EQUAL Kobi Gurkan Bitcoin Scripts using Node.JS
  • 32. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 3 OP_EQUAL 3 Kobi Gurkan Bitcoin Scripts using Node.JS
  • 33. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Stack scriptSig scriptPubKey 1 (True) Transaction valid too, and we indeed know to subtract! Kobi Gurkan Bitcoin Scripts using Node.JS
  • 34. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . This was easy, but might be hard to do by hand for more complex scripts. Luckily, BitPay did an awesome job by developing bitcore. Kobi Gurkan Bitcoin Scripts using Node.JS
  • 35. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . > npm install bitcore > node Kobi Gurkan Bitcoin Scripts using Node.JS
  • 36. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . var bitcore = require(’bitcore’); var interpreter = bitcore.Script.Interpreter(); interpreter.set({script: ’OP_2 OP_1 OP_SUB’}) interpreter.evaluate() Kobi Gurkan Bitcoin Scripts using Node.JS
  • 37. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Let’s see an interactive step session! https://asciinema.org/a/bkznx7286g09jleitqdu641xm Kobi Gurkan Bitcoin Scripts using Node.JS
  • 38. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . OK, but on to more honesty. The transaction was not valid. There are only a few standard scripts. But don’t despair - arbitrary scripts are still allowed. I’ll explain later. Kobi Gurkan Bitcoin Scripts using Node.JS
  • 39. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Topic 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 40. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . P2PK scriptSig scriptPubKey <sig> <pubkey> OP_CHECKSIG https: //asciinema.org/a/82dplmciis241l3slxdee5h09 Not widely used anymore because public key is. . . public. Kobi Gurkan Bitcoin Scripts using Node.JS
  • 41. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . P2PKH scriptSig scriptPubKey <sig> OP_DUP <pubkey> OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG Most used transaction - credit 100 Bitcoins to a hash of a public key. Prove you own the address by signing the entire transaction - OP_CHECKSIG. Kobi Gurkan Bitcoin Scripts using Node.JS
  • 42. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Multisig scriptSig scriptPubKey OP_0 <m> <A sig> <A pubkey> [B sig] <B pubkey> [C sig...] <C pubkey. . . > <n> OP_CHECKMULTISIG Divide ownership of Bitcoins Not widely used. Multisig is implemented as script hash. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 43. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Null Data scriptSig scriptPubKey OP_RETURN <0 to 80 bytes of data> https: //asciinema.org/a/0llb5rtakanx3to35lvcdul39 Can put arbitrary data on Blockchain! https://twitter.com/op_return_ack/status/ 705968000688455684 Open Assets (colored coins) - https: //github.com/OpenAssets/open-assets-protocol/ blob/master/specification.mediawiki Kobi Gurkan Bitcoin Scripts using Node.JS
  • 44. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . P2SH scriptSig scriptPubKey <additional opcodes. . . > OP_HASH160 <redeemScript> <Hash160(redeemScript)> OP_EQUAL Supports arbitrary scripts! Kobi Gurkan Bitcoin Scripts using Node.JS
  • 45. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . P2SH-based 2-of-3 Multisig scriptSig redeemScript scriptPubKey OP_0 <OP_2> OP_HASH160 <A sig> <A pubkey> <PubKeyHash> [C sig] <B pubkey> <Hash160(redeemScript)> <redeemScript> <C pubkey. . . > OP_EQUAL <OP_3> OP_CHECKMULTISIG Widely used form of Multisig Kobi Gurkan Bitcoin Scripts using Node.JS
  • 46. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Topic 1 Overview Me Goals Bitcoin Scripting language 2 Scripts in Bitcoin Simple example Standard Scripts Special. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 47. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Transaction Puzzle scriptSig scriptPubKey <data> OP_SHA256 <given_hash> OP_EQUAL https://live.blockcypher.com/btc/tx/ 9969603dca74d14d29d1d5f56b94c7872551607f8c2d6837ab9715c60721 https://asciinema.org/a/aj92zdz3m61kjkqfg0nghxiq7 Or as P2SH: https://asciinema.org/a/5sumtxdthpgza5f8z8s1t8jzb But this is insecure because anyone can see the solution, since everything is public. . . Kobi Gurkan Bitcoin Scripts using Node.JS
  • 48. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Secure Transaction Puzzle scriptSig scriptPubKey <sig> OP_SHA256 <pubkey> <given_hash> <data> OP_EQUALVERIFY OP_DUP OP_HASH160 <pubkeyHash> OP_EQUALVERIFY OP_CHECKSIG This is non-standard, of course. But, we can put this script as a P2SH script. https://asciinema.org/a/bvk8nockh7dq715qp8avhiw8h Kobi Gurkan Bitcoin Scripts using Node.JS
  • 49. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Zero Knowledge Contingent Payment scriptSig scriptPubKey OP_SHA256 <Y> OP_EQUAL OP_IF <Seller pubkey> OP_ELSE <block_height + 100> OP_CHECKLOCKTIMEVERIFY OP_DROP <Buyer pubkey> OP_ENDIF OP_CHECKSIG https://bitcoincore.org/en/2016/02/26/zero-knowledge-contingent-payments-announcement/ Deposit - https://live.blockcypher.com/btc/tx/ 8e5df5f792ac4e98cca87f10aba7947337684a5a0a7333ab897fb9c9d616ba9e Spend - https://live.blockcypher.com/btc/tx/ 200554139d1e3fe6e499f6ffb0b6e01e706eb8c897293a7f6a26d25e39623fae/ Kobi Gurkan Bitcoin Scripts using Node.JS
  • 50. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . OP_CHECKLOCKTIMEVERIFY scriptSig scriptPubKey OP_IF <now + 3 months> OP_CHECKLOCKTIMEVERIFY OP_DROP <Lenny’s pubkey> OP_CHECKSIGVERIFY 1 OP_ELSE 2 OP_ENDIF <Alice’s pubkey> <Bob’s pubkey> 2 OP_CHECKMULTISIG Allows to lock Bitcoins until a specific date! https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki Kobi Gurkan Bitcoin Scripts using Node.JS
  • 51. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . At any time: scriptSig scriptPubKey 0 <Alice’s signature> <Bob’s signature> 0 After 3 months: scriptSig scriptPubKey 0 <Alice/Bob’s signature> <Lenny’s signature> 1 Kobi Gurkan Bitcoin Scripts using Node.JS
  • 52. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Puzzle time! var bitcore = require(’bitcore’); var request = require(’request’); var utxo = { ’txId’ : ’0c5d827a24b0822abbff73f1adbfd2be7efaf4e368b4baac7e280865eb13f497’, ’outputIndex’ : 1, ’script’ : ’a91431f2ae5b333c56a4f01df6209382ec8f892e4f3687’, ’satoshis’ : 1000000 }; //you can see it here: //https://live.blockcypher.com/btc/tx/0c5d827a24b0822abbff73f1adbfd2be7efaf4e368b4baac7e280865eb13f497 var address = ’YOUR_ADDRESS’; var tx = new bitcore.Transaction().from(utxo).to(address, 900000); var redeemScript = bitcore.Script.fromASM(’ OP_SHA256 1bc273366856a5fb0bfd0611f57b7d4ae8e709dbe30fa207729f74716fd4e877 OP_EQUAL’); //this is your task! what is the total number of Bitcoins that will //ever be created, in hex format (don’t forget the leading zero!)? var solutionScript = ’’; var scriptSig = bitcore.Script.fromASM(solutionScript + ’ ’ + redeemScript); tx.inputs[0].setScript(scriptSig); var rawTx = tx.toString(’hex’); Kobi Gurkan Bitcoin Scripts using Node.JS
  • 53. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . var pushTx = { tx: rawTx }; //for simplicity, we’ll use the awesome BlockCypher API. //You could also submit this raw transaction using Bitcoin Core. request({ url: ’https://api.blockcypher.com/v1/btc/main/txs/push’, method: ’POST’, json: true, body: pushTx }, function (err, response, body) { if (err) { return console.log(’Error: ’, err); } console.log(body); }); Kobi Gurkan Bitcoin Scripts using Node.JS
  • 54. Overview Scripts in Bitcoin Simple example Standard Scripts Special. . . Questions? kobigurk@gmail.com kobigurk.com @kobigurk We are hiring! Kobi Gurkan Bitcoin Scripts using Node.JS