Ubuntu Missing add-apt-repository command

I was trying to install the latest version of bitcoin-core from bitcoin maintainers and I needed to add a Personal Package Archive (PPA) to the Software Sources. But when I tried to do this, I got an error:

sudo: add-apt-repository: command not found

Solution: Install the software-properties-common Package

To get the add-apt-repository command, install the software-properties-common package. For my Ubuntu version (precise), I also had to install the python-software-properties package:

sudo apt-get install software-properties-common python-software-properties

Then you need to add the PPA, update your sources, and upgrade your packages. In may case, I wanted to install bitcoind, so I did the following:


sudo apt-add-repository ppa:bitcoin/bitcoin
sudo apt-get update
sudo apt-get install bitcoind bitcoin-qt
Ubuntu Missing add-apt-repository command

Hierarchical Deterministic Wallets (BIP0032/BIP0044) in Ruby

In this post I want to share my experiences playing HD wallet implementation with Ruby. There are many alternative posts about HD wallets, but I couldn’t find one for Ruby Lang. And I’ve decided to post my own blog.

Hierarchical deterministic wallets are a fascinating concept with lots of potential uses. One interesting application is the ability to have a public/private wallet key pair for generating Bitcoin addresses in a deterministic way. This permits, for example, a payment service to let its server generate a fresh address for each order, without giving  server access to corresponding private keys. A lot of bitcoin addresses can be generated by a public wallet key. However, the corresponding private keys for spending bitcoins can only be generated using a private wallet key.

These wallets are “hierarchical” because they contain keys derived in a tree structure, parent key can generate a sequence of children keys and each children key can also generate its own sequence of children keys and on…

Mastering Bitcoin Book
https://github.com/aantonop/bitcoinbook/blob/develop/images/msbt_0409.png
HD wallet creation from a seed

As we mentioned above, HD wallets are created from a single root seed, which is a random number. Every key in the HD wallet is derived from this root seed. If you have a backup of this root seed, you can re-create the entire wallet. The root seed is mostly represented by a “mnemonic word sequence” to make it easier for people to store.

First of all we need to create master key pair from a secret passphrase. These keys represent the root address of the hierarchy.

Important: Never store bitcoins in wallets created from non-random passphrase. Use a long random master secret instead. I suggest you to use a dice for generating secure random password. (Link).

For creating HD Wallet in Ruby, we will use the money-tree, a Ruby implementation of HD Wallets. This library contains all necessary methods for creating HD wallets with Ruby. First of all we need to install money-tree gem.

require 'money-tree'
require 'digest'

# Getting seed EC-HEX 64 hexadecimal digits
seed = Digest::SHA256.digest('some big long brainwallet password').unpack("H*")[0]

# import the seed for creating a new master node.
@master = MoneyTree::Master.new seed_hex: seed

p @master.to_serialized_address(:private)
# "xprv9s21ZrQH143K4RVGkC5MZGMSYPi4Pg6YW6CsC9T3n2Y1fr346srmSDikE63TGszcjmp8ev4F1tdyNV85EJAwKWwMbGdqcM1cJwqhHkwRjJm"


# generate the m/0
@node = @master.node_for_path "m/0"

# generate the m/0 extended public key
p @node.to_serialized_address
# "xpub686awEsziWCdMKK9XEiRsEawYjQetEK6wH6HdJRVFW4vnC6jruLy8CtfasHX7B713AfzC4EYCfxtS3qKiDdjc8dFuUDo9McekZENh9tArPz"

# generate the m/0 extended private key
p @node.to_serialized_address :private
# "xprv9u7EXjM6t8eL8qEgRDBRW6eCzhaAUmbFa4Agpv1shAXwuPmbKN2iaQaBjZW5UanymAgpfu9PFLQ5ihhGvQfXuQzgBs3tviHseb2vTWVdzNc"


# show the private key of m/0 as a WIF
p @node.private_key.to_wif
# "Kz4uohnL2Htjpex9hHJGdgXyW36eLN2QpHphznEyzjm7yZWqgezn"


# show the bitcoin address of M/0
p @node.to_address
# "14gYoPhcUdCouz4SGGs4Z1uhKPo7XJoUFg"

# generate m/0/12'/4
@node = @master.node_for_path "m/0/12'/4"
p @node.to_serialized_address :private
# "xprv9yoEkvVPX76LhydEXQ9z6dcb94PxdyiBX5zdbhYLFPgncb6HM5ozcg8fp3S4U6qjaoBCRqYJ1SVDA1gKMJCKaNkCow7H9muy8nV2S23ebzD" 

Extended public key

As we mention above, a lot of bitcoin addresses can be generated by a public wallet key. However, the corresponding private keys for spending bitcoins can only be generated using a private wallet key.

With money-tree, either using a capital “M” instead of a lowercase “m”, or by appending “.pub” to the end of the path, we will receive a node that is stripped of its private key. This gives us an extended public key functionality (Link).

@node = @master.node_for_path("M/0/3") # or "m/0/3.pub" or "M/0/3.pub"...these are equivalent

@node.to_serialized_address
=> "xpub6CnbAS2HMUedvThhdRgzTmZKh6ET3SS2tJvEQ5wwojDmVPRRtd8FAUT9fMiWdHeZXUjeVZBgJtiJbkQn5JjNxgrtcSE3BYdQHgRptbe8YjM"

@node.to_serialized_address(:private)
-> raises MoneyTree::Node::PrivatePublicMismatch error

Now the “extended public key” can be installed on a web server and can generate bitcoin addresses as much as we want. The web server will not have any private keys that would be vulnerable to theft. Without HD wallets, the only way to do this is generating a plenty of addresses on a secure server and then load them on the web server.

p @master.node_for_path("M/0/0").to_address
p @master.node_for_path("M/0/1").to_address
p @master.node_for_path("M/0/2").to_address
p @master.node_for_path("M/0/3").to_address

"1QG7ve9GRTV77AJnimcNbu1VUYCm1zGtQQ"
"1Pq5QV6XyFaXtdJiwSeXrnKxsrS6NaTCrD"
"1AhG6KuTZFnDB7pXpGWRYg4ffSzDig36mW"
"1HQxyjNvz4UV732Hdw7o7bevnrgTYZbfPK"

BIP0044 Multi-Account Hierarchy for Deterministic Wallets

This proposal defines a logical hierarchy for HD wallets.

BIP0044 specifies the structure as consisting of five predefined tree levels:

m / 44' / coin_type' / account' / change / address_index

I suggest you to use BIP44 with HD wallets to avoid complexity.

REFERENCES:

Hierarchical Deterministic Wallets (BIP0032/BIP0044) in Ruby

Migrating Postgresql 9.3 to 9.4 with homebrew

I am a big PostgreSQL user and fan. I also use Homebrew to manage 3rd party software packages on my Mac. PostgreSQL 9.4 was just released a couple days ago with some really cool features — a binary-format JSON datatype for speed and flexibility (indexes on JSON keys? Of course.), and some really good performance improvements. Read the release blog post and release notes for more information.

However, if you’ve used PostgreSQL before, you know that upgrading can be a little difficult. Here’s what you have to do to upgrade your Homebrew-installed PostgreSQL 9.3 to 9.4. Keep in mind, these steps are for a standard Homebrew installation — as long as you haven’t configured custom data directory paths, it should work.

  1. Turn PostgreSQL off first, just in case:
    $ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    
  2. Update PostgreSQL itself:
    $ brew update && brew upgrade postgresql
    
  3. Make a new, pristine 9.4 database:
    $ initdb /usr/local/var/postgres9.4 -E utf8
    
  4. Migrate the data to the new 9.4 database:
    $ pg_upgrade \
      -d /usr/local/var/postgres \
      -D /usr/local/var/postgres9.4 \
      -b /usr/local/Cellar/postgresql/9.3.5_1/bin/ \
      -B /usr/local/Cellar/postgresql/9.4.0/bin/ \
      -v
    
  5. Move 9.4 data directory back to where PostgreSQL expects it to be:
    $ mv /usr/local/var/postgres /usr/local/var/postgres9.3
    $ mv /usr/local/var/postgres9.4 /usr/local/var/postgres
    
  6. Start PostgreSQL back up!
    $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    

Note: If you’re using the pg gem for Rails, you should recompile:

$ gem uninstall pg
$ gem install pg
Migrating Postgresql 9.3 to 9.4 with homebrew