Sie sind auf Seite 1von 22

Working with

Bitcoin in Ruby

Jonathan Salis

Jonathan Salis

The Challenge

Integrate a Bitcoin payment system

Without a 3rd party


Payment detection
- Address issuing
-

Security
- Price locking
-

Gaming prevention
- protect against Bitcoin price
fluctuations

Bonus Quests

Easy to integrate

Configurable

Easy to use
- Extensible
-

bitcoin_payable

Payment Detection
Address Issuing
Security

The Bitcoin Blockchain


is a public ledger
Index it and read it
yourself
Leverage existing APIs
blockchain.info
helloblock.io
(formerly)

Payment Detection
Address Issuing
Security

Address
|
|
|
Blockchain API
|
|
|
Payment information

Payment Detection
Address Issuing
Security

New address for each


payment
BIP32
Generate a Master
Public Key (MPK)
Ruby gem money-tree
to generate
deterministic addresses
using BIP32

Payment Detection
Address Issuing
Security

BIP32
No private keys on the
server
No private keys to steal
What can go wrong?
Hack the server and
change the MPK with
another one

Honouring Prices

All sales done in local currency


-

$100

0.11 BTC
- Update amount due in BTC every
-

30 minutes
- 0.10 BTC
-

Why?

Honouring Prices

Consider 0.11 = $100 CAD


-

Payment made for 0.01 BTC

When a transaction is read, store


current exchange rate.

0.01 BTC == $8.77

$100 - $8.77 = $91.23

New price to honour

No gaming price fluctuations

bitcoin_payments

bitcoin_payable
bp_transactions

currency_conversions

process_prices
process_payments

Order
Integration
bitcoin_payments

Donation

bitcoin_payments

MORE CODE!

def Order < ActiveRecord::Base


has_bitcoin_payments
end
@order = Order.find(1)
bp = @order.bitcoin_payments
bp.address
bp.currency_conversion
bp.transactions

def Order < ActiveRecord::Base


has_bitcoin_payments
def create_payment(amount_in_cents)
self.bitcoin_payments.create!({
reason: 'sale',
price: amount_in_cents
})
end
end
order = Order.create
order.create_payment(100)

def Order < ActiveRecord::Base


has_bitcoin_payments
def create_payment(amount_in_cents)
self.bitcoin_payments.create!({
reason: 'sale',
price: amount_in_cents
})
end
def bitcoin_payment_paid
self.ship!
end
end

Rake Tasks

bitcoin_payable:process_prices

api.bitcoinaverage.com

24 hour weighted average

updates all pending payments =>


30 mins

Rake Tasks

bitcoin_payable:process_payments

connect to API

save new transaction information


for all pending payments

save currency conversion, BTC


received in satoshis

capital gains

Configuration
BitcoinPayable
.config
.currency = :cad
BitcoinPayable
.config
.node_path = "m/0/"
BitcoinPayable
.config
.master_public_key = ""
BitcoinPayable
.config
.adapter = "blockchain_info"

Use it!

donate.cryptoconsortium.org

Thanks

Das könnte Ihnen auch gefallen