SlideShare a Scribd company logo
1 of 15
Download to read offline
How to send a bitcoin
In Ruby...
Sjors Provoost
sjors@purpledunes.com
Twitter: @provoostUtrecht.rb
June 3, 2013
31-05-13 20:05
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
maandag 3 juni 13
Wallets 31-0itcoin Paper Wallet
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
Recipient:
1KHxSzFpdm337XtBeyfbvbS9LZC1BfDu8K
Paper wallet:
Paper wallet in a safe place...
Transfer
maandag 3 juni 13
Inputs
It’s not about balance, it’s about inputs
We received 0.03 btc in a single transaction
So we have one input worth 0.03 btc
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
maandag 3 juni 13
Outputs
1KHxSzFpdm337XtBeyfbvbS9LZC1BfDu8K1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
You must spend the entire input
Return remainder as change
Leave something for the miners
0.01 btc
0.02 btc
0.019
maandag 3 juni 13
Public & private keys
BitcoinPaperWallet
Pagi
https://bitcoinpaperwallet.com/bitcoinpaperwallet/generate-wallet.html
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
256 bit Elliptic Curve Private Key
Base58 encode
Public key
SHA-256 & RIPEMD-160
Add checksum & Base58 encode
Address
... 890 ... KkLlMmNnOoPp...
maandag 3 juni 13
Simplified Transaction
Transaction identifier
Input is earlier output
Those outputs had
the sender as their
destination
The first output is us,
the second is change
{
"time":1370023412,
"hash":"9d9c703d2d4....",
"inputs":[
{"prev_out":{
"transaction" : "...."
"n":1,
"value":6114105,
"addr":"14DCzMesaa...",
}}],
"out":[
{"n":0,
"value":3000000,
"addr":"1NvYRmC...",
{"n":1,
"value":3014105,
"addr":"14DCzMe...",
}],
}
Bitcoin Paper Wallet
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
5
maandag 3 juni 13
Code!
Get current balance from Blockchain.info
@sender = "1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR"
url = "https://blockchain.info/address/#{ @sender }?format=json"
res = JSON.parse(open(url).read)
@balance = BigDecimal.new(res["final_balance"]) / SATOSHI_PER_BITCOIN
Gather inputs (remember, it’s not about balance)
url = "https://blockchain.info/unspent?active=#{@sender}&format=json"
res["unspent_outputs"].each do |output|
@inputs << {
previousTx: [output["tx_hash"]].pack("H*").reverse.unpack("H*")
[0], index: output["tx_output_n"],
scriptSig: nil # We'll sign it later
}
amount = BigDecimal.new(output["value"]) / SATOSHI_PER_BITCOIN
input_total += amount
break if input_total >= @amount + @transaction_fee
end
maandag 3 juni 13
Outputs
Receipient and change
Script says: “he who can sign another
transaction with this public key, may spend”
Low level stuff like the length of the pub. key
@outputs = [
{
value: @amount,
scriptPubKey: "OP_DUP OP_HASH160 " +
(recipient_pubkey_hex.size / 2).to_s(16) +
" " + recipient_pubkey_hex +
" OP_EQUALVERIFY OP_CHECKSIG "
},{
value: @change,
scriptPubKey: "OP_DUP OP_HASH160 " ....
maandag 3 juni 13
Serialize{:version=>1,
:in_counter=>1,
:inputs=>
[{:previousTx=>
"9d9c703d2d47c23e80441eac3f6060d021edea8434e9729a56fb378bf54ebf45",
:index=>0,
:scriptLength=>25,
:scriptSig=>
"OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG ",
:sequence_no=>"ffffffff"}],
:out_counter=>2,
:outputs=>
[{:value=>#<BigDecimal:7fd51c32bf60,'0.1E-1',9(18)>,
:scriptPubKey=>
"OP_DUP OP_HASH160 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 OP_EQUALVERIFY OP_CHECKSIG "},
{:value=>#<BigDecimal:7fd51c26de98,'0.2E-1',9(27)>,
:scriptPubKey=>
"OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG "}],
:lock_time=>0,
:hash_code_type=>"01000000"}
01000000
01
45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000
19
76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac ffffffff
02
40420f0000000000
19
76 a9 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 88 ac
80841e0000000000
19
76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac
00000000
01000000
OP_HASH160 Ripemd160(Sha256(...)) 14
Verify signature of
whoever spends this output
Our address (hex)
maandag 3 juni 13
Sign
Chicken-egg problem
01000000
01
45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000
19
76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac ffffffff
02
....
Sha256 hash
6a9a0ce373eaa03b1da3cd476abfed1d69a142db0a912fe0443fc8cfb4189abe
Our signature needed to spend this
Sign and insert into transaction
01000000
01
45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000
8b
48
3045022002338998a410c7da841e83a33bdf78c5322896b605d5d9718638658d22120389022100ea077a2ecbb4b0d862d38426ae6
d71dc947eaa649e93dfb63e5023182c59222f
01 41
047aeeaaffd99f46af7ba63e5fcd42acd2e421eb54c1af3e042311dd2da8a7be97cae06ff4d12a6ad71d4698be307dbd0ed275253
146601328b0d8645bab16cbe8
ffffffff
02
....
Signature
maandag 3 juni 13
Demo
git clone http://gist.github.com/5574485.git
ruby bitcoin-pay.rb 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
Download and run with parameters
your address
your private key
Optionally change the addressee, me :-)
Copy result and paste into https://
blockchain.info/pushtx
maandag 3 juni 13
If demo doesn’t work...
(1/2)
About to send 0.01 bitcoins from 1NvYRm... to 1KHxSz...
Fetching the current balance for NvYRm from blockchain.info...
Current balance of sender: 0.03 BTC
Using 0.03 from output 0 of transaction 45bf4e...
Spend 0.01 and return 0.02 as change.
Public key matches private key, so we can sign the transaction...
Readable version of the transaction (numbers in strings are hex, otherwise decimal)
{:version=>1,
:in_counter=>1,
:inputs=>
[{:previousTx=>
"9d9c703d2d47c23e80441eac3f6060d021edea8434e9729a56fb378bf54ebf45",
:index=>0,
:scriptLength=>25,
:scriptSig=>
"OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG ",
:sequence_no=>"ffffffff"}],
:out_counter=>2,
:outputs=>
[{:value=>#<BigDecimal:7fd51c32bf60,'0.1E-1',9(18)>,
:scriptPubKey=>
"OP_DUP OP_HASH160 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 OP_EQUALVERIFY OP_CHECKSIG "},
{:value=>#<BigDecimal:7fd51c26de98,'0.2E-1',9(27)>,
:scriptPubKey=>
"OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG "}],
:lock_time=>0,
:hash_code_type=>"01000000"}
maandag 3 juni 13
If demo doesn’t work...
(2/2)
Hex unsigned transaction:
01000000
01
45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000
19
76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac ffffffff
02
40420f0000000000
19
76 a9 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 88 ac
80841e0000000000
19
76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac
00000000
01000000
Hash that we're going to sign: 6a9a0ce373eaa03b1da3cd476abfed1d69a142db0a912fe0443fc8cfb4189abe
Hex signed transaction: (258 bytes)
010000000145bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d000000008b48304502201e9f2
c93ed4a3f57db4b4f79c1b57c616a468f85e1b89fa633de1b3a32570b67022100a5884cdbd43ca1db025f680a30d30a17cc
b264a293706ad0d5736cad173c30bb0141047aeeaaffd99f46af7ba63e5fcd42acd2e421eb54c1af3e042311dd2da8a7be9
7cae06ff4d12a6ad71d4698be307dbd0ed275253146601328b0d8645bab16cbe8ffffffff0240420f00000000001976a914
c8a73488183dd49f63a11dea0a3b242ae70942d288ac80841e00000000001976a914f07aebdb79282ef81eaaba45a35bb82
e340fb97c88ac00000000
Copy paste the transaction and transmit it at https://blockchain.info/pushtx
maandag 3 juni 13
Questions?
Bitcoin Paper Wallet
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8
Sjors Provoost
sjors@purpledunes.com
Twitter: @provoost
Shameless plug
Get notified about this
transaction in your
iPhone’s Passbook:
bitcoin-passbook.com
maandag 3 juni 13
Sources
Ruby script to transfer Bitcoin: https://gist.github.com/Sjors/5574485
Paper wallet: https://bitcoinpaperwallet.com
General Bitcoin info: http://bitcoin.it
Elliptic curve image: http://www.hpl.hp.com/research/info_theory/images/curveplot.gif
maandag 3 juni 13

More Related Content

Recently uploaded

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Recently uploaded (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Bitcoin transaction in ruby

  • 1. How to send a bitcoin In Ruby... Sjors Provoost sjors@purpledunes.com Twitter: @provoostUtrecht.rb June 3, 2013 31-05-13 20:05 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 maandag 3 juni 13
  • 2. Wallets 31-0itcoin Paper Wallet 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 Recipient: 1KHxSzFpdm337XtBeyfbvbS9LZC1BfDu8K Paper wallet: Paper wallet in a safe place... Transfer maandag 3 juni 13
  • 3. Inputs It’s not about balance, it’s about inputs We received 0.03 btc in a single transaction So we have one input worth 0.03 btc 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR maandag 3 juni 13
  • 4. Outputs 1KHxSzFpdm337XtBeyfbvbS9LZC1BfDu8K1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR You must spend the entire input Return remainder as change Leave something for the miners 0.01 btc 0.02 btc 0.019 maandag 3 juni 13
  • 5. Public & private keys BitcoinPaperWallet Pagi https://bitcoinpaperwallet.com/bitcoinpaperwallet/generate-wallet.html 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 256 bit Elliptic Curve Private Key Base58 encode Public key SHA-256 & RIPEMD-160 Add checksum & Base58 encode Address ... 890 ... KkLlMmNnOoPp... maandag 3 juni 13
  • 6. Simplified Transaction Transaction identifier Input is earlier output Those outputs had the sender as their destination The first output is us, the second is change { "time":1370023412, "hash":"9d9c703d2d4....", "inputs":[ {"prev_out":{ "transaction" : "...." "n":1, "value":6114105, "addr":"14DCzMesaa...", }}], "out":[ {"n":0, "value":3000000, "addr":"1NvYRmC...", {"n":1, "value":3014105, "addr":"14DCzMe...", }], } Bitcoin Paper Wallet 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 5 maandag 3 juni 13
  • 7. Code! Get current balance from Blockchain.info @sender = "1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR" url = "https://blockchain.info/address/#{ @sender }?format=json" res = JSON.parse(open(url).read) @balance = BigDecimal.new(res["final_balance"]) / SATOSHI_PER_BITCOIN Gather inputs (remember, it’s not about balance) url = "https://blockchain.info/unspent?active=#{@sender}&format=json" res["unspent_outputs"].each do |output| @inputs << { previousTx: [output["tx_hash"]].pack("H*").reverse.unpack("H*") [0], index: output["tx_output_n"], scriptSig: nil # We'll sign it later } amount = BigDecimal.new(output["value"]) / SATOSHI_PER_BITCOIN input_total += amount break if input_total >= @amount + @transaction_fee end maandag 3 juni 13
  • 8. Outputs Receipient and change Script says: “he who can sign another transaction with this public key, may spend” Low level stuff like the length of the pub. key @outputs = [ { value: @amount, scriptPubKey: "OP_DUP OP_HASH160 " + (recipient_pubkey_hex.size / 2).to_s(16) + " " + recipient_pubkey_hex + " OP_EQUALVERIFY OP_CHECKSIG " },{ value: @change, scriptPubKey: "OP_DUP OP_HASH160 " .... maandag 3 juni 13
  • 9. Serialize{:version=>1, :in_counter=>1, :inputs=> [{:previousTx=> "9d9c703d2d47c23e80441eac3f6060d021edea8434e9729a56fb378bf54ebf45", :index=>0, :scriptLength=>25, :scriptSig=> "OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG ", :sequence_no=>"ffffffff"}], :out_counter=>2, :outputs=> [{:value=>#<BigDecimal:7fd51c32bf60,'0.1E-1',9(18)>, :scriptPubKey=> "OP_DUP OP_HASH160 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 OP_EQUALVERIFY OP_CHECKSIG "}, {:value=>#<BigDecimal:7fd51c26de98,'0.2E-1',9(27)>, :scriptPubKey=> "OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG "}], :lock_time=>0, :hash_code_type=>"01000000"} 01000000 01 45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000 19 76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac ffffffff 02 40420f0000000000 19 76 a9 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 88 ac 80841e0000000000 19 76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac 00000000 01000000 OP_HASH160 Ripemd160(Sha256(...)) 14 Verify signature of whoever spends this output Our address (hex) maandag 3 juni 13
  • 10. Sign Chicken-egg problem 01000000 01 45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000 19 76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac ffffffff 02 .... Sha256 hash 6a9a0ce373eaa03b1da3cd476abfed1d69a142db0a912fe0443fc8cfb4189abe Our signature needed to spend this Sign and insert into transaction 01000000 01 45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000 8b 48 3045022002338998a410c7da841e83a33bdf78c5322896b605d5d9718638658d22120389022100ea077a2ecbb4b0d862d38426ae6 d71dc947eaa649e93dfb63e5023182c59222f 01 41 047aeeaaffd99f46af7ba63e5fcd42acd2e421eb54c1af3e042311dd2da8a7be97cae06ff4d12a6ad71d4698be307dbd0ed275253 146601328b0d8645bab16cbe8 ffffffff 02 .... Signature maandag 3 juni 13
  • 11. Demo git clone http://gist.github.com/5574485.git ruby bitcoin-pay.rb 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 Download and run with parameters your address your private key Optionally change the addressee, me :-) Copy result and paste into https:// blockchain.info/pushtx maandag 3 juni 13
  • 12. If demo doesn’t work... (1/2) About to send 0.01 bitcoins from 1NvYRm... to 1KHxSz... Fetching the current balance for NvYRm from blockchain.info... Current balance of sender: 0.03 BTC Using 0.03 from output 0 of transaction 45bf4e... Spend 0.01 and return 0.02 as change. Public key matches private key, so we can sign the transaction... Readable version of the transaction (numbers in strings are hex, otherwise decimal) {:version=>1, :in_counter=>1, :inputs=> [{:previousTx=> "9d9c703d2d47c23e80441eac3f6060d021edea8434e9729a56fb378bf54ebf45", :index=>0, :scriptLength=>25, :scriptSig=> "OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG ", :sequence_no=>"ffffffff"}], :out_counter=>2, :outputs=> [{:value=>#<BigDecimal:7fd51c32bf60,'0.1E-1',9(18)>, :scriptPubKey=> "OP_DUP OP_HASH160 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 OP_EQUALVERIFY OP_CHECKSIG "}, {:value=>#<BigDecimal:7fd51c26de98,'0.2E-1',9(27)>, :scriptPubKey=> "OP_DUP OP_HASH160 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c OP_EQUALVERIFY OP_CHECKSIG "}], :lock_time=>0, :hash_code_type=>"01000000"} maandag 3 juni 13
  • 13. If demo doesn’t work... (2/2) Hex unsigned transaction: 01000000 01 45bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d 00000000 19 76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac ffffffff 02 40420f0000000000 19 76 a9 14 c8a73488183dd49f63a11dea0a3b242ae70942d2 88 ac 80841e0000000000 19 76 a9 14 f07aebdb79282ef81eaaba45a35bb82e340fb97c 88 ac 00000000 01000000 Hash that we're going to sign: 6a9a0ce373eaa03b1da3cd476abfed1d69a142db0a912fe0443fc8cfb4189abe Hex signed transaction: (258 bytes) 010000000145bf4ef58b37fb569a72e93484eaed21d060603fac1e44803ec2472d3d709c9d000000008b48304502201e9f2 c93ed4a3f57db4b4f79c1b57c616a468f85e1b89fa633de1b3a32570b67022100a5884cdbd43ca1db025f680a30d30a17cc b264a293706ad0d5736cad173c30bb0141047aeeaaffd99f46af7ba63e5fcd42acd2e421eb54c1af3e042311dd2da8a7be9 7cae06ff4d12a6ad71d4698be307dbd0ed275253146601328b0d8645bab16cbe8ffffffff0240420f00000000001976a914 c8a73488183dd49f63a11dea0a3b242ae70942d288ac80841e00000000001976a914f07aebdb79282ef81eaaba45a35bb82 e340fb97c88ac00000000 Copy paste the transaction and transmit it at https://blockchain.info/pushtx maandag 3 juni 13
  • 14. Questions? Bitcoin Paper Wallet 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 1NvYRmCyQdSUf7iRBiYdFzHpYfgoP5vRbR 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 5HvjNurYseJevFhKRUyYzXFucgrekXPJT9BRXzngqzp6Ghcj3H8 Sjors Provoost sjors@purpledunes.com Twitter: @provoost Shameless plug Get notified about this transaction in your iPhone’s Passbook: bitcoin-passbook.com maandag 3 juni 13
  • 15. Sources Ruby script to transfer Bitcoin: https://gist.github.com/Sjors/5574485 Paper wallet: https://bitcoinpaperwallet.com General Bitcoin info: http://bitcoin.it Elliptic curve image: http://www.hpl.hp.com/research/info_theory/images/curveplot.gif maandag 3 juni 13