Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ They are all equivalent ways of saying the same thing, but the first two are jus

To add just a little more confusion to the mix, some wallets will use negative `i` values to also denote private derivation. Any `i` value that is negative will be processed using private derivation by this library. e.g. `"m/-1/1"`. (NOTE: known issue, see below)

## Change Network

MoneyTree will operate in mainnet by default. To activate Bitcoin Testnet simply run

```ruby
MoneyTree.configure do |config|
config.network = :testnet3
end
```


## Contributing

Expand Down
21 changes: 20 additions & 1 deletion lib/money-tree.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "openssl_extensions"
require 'money-tree/configuration'
require "money-tree/version"
require "money-tree/support"
require "money-tree/networks"
Expand All @@ -8,5 +9,23 @@
require "money-tree/node"

module MoneyTree

class << self
attr_accessor :configuration
end

def self.network
configuration.network
end

def self.configuration
@configuration ||= Configuration.new
end

def self.reset
@configuration = Configuration.new
end

def self.configure
yield(configuration)
end
end
7 changes: 4 additions & 3 deletions lib/money-tree/address.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module MoneyTree
class Address
attr_reader :private_key, :public_key

def initialize(opts = {})
private_key = opts.delete(:private_key)
@private_key = MoneyTree::PrivateKey.new({ key: private_key }.merge(opts))
@public_key = MoneyTree::PublicKey.new(@private_key, opts)
end

def to_s(network: :bitcoin)

def to_s(network: nil)
network ||= MoneyTree.network
public_key.to_s(network: network)
end

Expand Down
9 changes: 9 additions & 0 deletions lib/money-tree/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module MoneyTree
class Configuration
attr_accessor :network

def initialize(args={})
@network = args.delete(:network) || :bitcoin
end
end
end
9 changes: 6 additions & 3 deletions lib/money-tree/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def to_hex
int_to_hex @ec_key.private_key, 64
end

def to_wif(compressed: true, network: :bitcoin)
def to_wif(compressed: true, network: nil)
network ||= MoneyTree.network
source = NETWORKS[network][:privkey_version] + to_hex
source += NETWORKS[network][:privkey_compression_flag] if compressed
hash = sha256(source)
Expand Down Expand Up @@ -157,7 +158,8 @@ def to_base64
encode_base64(to_hex)
end

def to_s(network: :bitcoin)
def to_s(network: nil)
network ||= MoneyTree.network
to_wif(network: network)
end

Expand Down Expand Up @@ -246,7 +248,8 @@ def to_ripemd160
ripemd160 hash
end

def to_address(network: :bitcoin)
def to_address(network: nil)
network ||= MoneyTree.network
hash = to_ripemd160
address = NETWORKS[network][:address_version] + hash
to_serialized_base58 address
Expand Down
49 changes: 49 additions & 0 deletions lib/money-tree/networks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,56 @@ module MoneyTree
compressed_wif_chars: %w(c),
uncompressed_wif_chars: %w(9),
protocol_version: 70001
},
litecoin: {
address_version: '30',
p2sh_version: '32',
p2sh_char: '3',##TODO
privkey_version: 'b0',
privkey_compression_flag: '01',##TODO
extended_privkey_version: "019d9cfe",
extended_pubkey_version: "019da462",
compressed_wif_chars: %w(K L),##TODO
uncompressed_wif_chars: %w(5),##TODO
protocol_version: 70002
},
litecoin_testnet: {
address_version: '6f',
p2sh_version: '3a',
p2sh_char: '2',##TODO
privkey_version: 'ef',
privkey_compression_flag: '01',##TODO
extended_privkey_version: "0436ef7d",
extended_pubkey_version: "0436f6e1",
compressed_wif_chars: %w(c),##TODO
uncompressed_wif_chars: %w(9),##TODO
protocol_version: 70002
},
dash: {
address_version: '4c', # this makes the Y
p2sh_version: '10',
p2sh_char: '2',
privkey_version: 'cc',
privkey_compression_flag: '01',
extended_privkey_version: "04358394", #default BTC
extended_pubkey_version: "043587cf", #default BTC
compressed_wif_chars: %w(c),
uncompressed_wif_chars: %w(9),
protocol_version: 70014
},
dash_testnet: {
address_version: '8c', # this makes the X
p2sh_version: '13',
p2sh_char: '2',
privkey_version: 'ef',
privkey_compression_flag: '01',
extended_privkey_version: "04358394", #default BTC
extended_pubkey_version: "043587cf", #default BTC
compressed_wif_chars: %w(c),
uncompressed_wif_chars: %w(9),
protocol_version: 70014
}

)
hsh[:testnet3] = hsh[:bitcoin_testnet]
hsh
Expand Down
12 changes: 8 additions & 4 deletions lib/money-tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ def right_from_hash(hash)
bytes_to_int hash.bytes.to_a[32..-1]
end

def to_serialized_hex(type = :public, network: :bitcoin)
def to_serialized_hex(type = :public, network: nil)
raise PrivatePublicMismatch if type.to_sym == :private && private_key.nil?
network ||= MoneyTree.network
version_key = type.to_sym == :private ? :extended_privkey_version : :extended_pubkey_version
hex = NETWORKS[network][version_key] # version (4 bytes)
hex += depth_hex(depth) # depth (1 byte)
Expand All @@ -115,12 +116,14 @@ def to_serialized_hex(type = :public, network: :bitcoin)
hex += type.to_sym == :private ? "00#{private_key.to_hex}" : public_key.compressed.to_hex
end

def to_bip32(type = :public, network: :bitcoin)
def to_bip32(type = :public, network: nil)
raise PrivatePublicMismatch if type.to_sym == :private && private_key.nil?
network ||= MoneyTree.network
to_serialized_base58 to_serialized_hex(type, network: network)
end

def to_serialized_address(type = :public, network: :bitcoin)
def to_serialized_address(type = :public, network: nil)
network ||= MoneyTree.network
puts 'Node.to_serialized_address is DEPRECATED. Please use .to_bip32.'
to_bip32(type, network: network)
end
Expand All @@ -142,7 +145,8 @@ def parent_fingerprint
end
end

def to_address(compressed=true, network: :bitcoin)
def to_address(compressed=true, network: nil)
network ||= MoneyTree.network
address = NETWORKS[network][:address_version] + to_identifier(compressed)
to_serialized_base58 address
end
Expand Down
2 changes: 1 addition & 1 deletion lib/money-tree/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MoneyTree
VERSION = "0.10.0"
VERSION = "0.10.3"
end