ruby_bitcoin_wallet 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e2d6a2c758bce319fa6931a0c371e336334400c
4
+ data.tar.gz: 9608df94578c432dfb695fd5becd7c366461fc4a
5
+ SHA512:
6
+ metadata.gz: 0f33c72dd01c07aa5f1e3c225b430487a6fc96115ffedd8f6ae0d33f65e6105c2b09f2cd68f3717d93d9e1b964e472f824ac7ee74e78351f0417e977d4aeea1e
7
+ data.tar.gz: 1c62781bff479568928eee45d638164e9262269c256c0f53537c5255ec444435cc95344d9498b97ae0af942be616f057053fc8dae6620a929ed7133aa50a57c6
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_bitcoin_wallet.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Nicolas Tonnelier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RubyBitcoinWallet
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby_bitcoin_wallet'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby_bitcoin_wallet
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/ruby_bitcoin_wallet/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
4
+
@@ -0,0 +1,133 @@
1
+ require "ruby_bitcoin_wallet/version"
2
+
3
+ require 'openssl'
4
+ require 'ruby_bitcoin_wallet/util'
5
+ require 'ruby_bitcoin_wallet/open_ssl'
6
+
7
+ module RubyBitcoinWallet
8
+
9
+ COIN = 100_000_000
10
+ CENT = 1_000_000
11
+
12
+ NETWORKS = {
13
+ bitcoin: {
14
+ project: :bitcoin,
15
+ magic_head: "\xF9\xBE\xB4\xD9",
16
+ address_version: "00",
17
+ p2sh_version: "05",
18
+ privkey_version: "80",
19
+ default_port: 8333,
20
+ protocol_version: 70001,
21
+ coinbase_maturity: 100,
22
+ reward_base: 50 * COIN,
23
+ reward_halving: 210_000,
24
+ retarget_interval: 2016,
25
+ retarget_time: 1209600, # 2 weeks
26
+ target_spacing: 600, # block interval
27
+ max_money: 21_000_000 * COIN,
28
+ min_tx_fee: 10_000,
29
+ min_relay_tx_fee: 10_000,
30
+ free_tx_bytes: 1_000,
31
+ dust: CENT,
32
+ per_dust_fee: false,
33
+ dns_seeds: [
34
+ "seed.bitcoin.sipa.be",
35
+ "dnsseed.bluematt.me",
36
+ "dnsseed.bitcoin.dashjr.org",
37
+ "bitseed.xf2.org",
38
+ "dnsseed.webbtc.com",
39
+ ],
40
+ genesis_hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
41
+ proof_of_work_limit: 0x1d00ffff,
42
+ alert_pubkeys: ["04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284"],
43
+ known_nodes: [
44
+ 'relay.eligius.st',
45
+ 'mining.bitcoin.cz',
46
+ 'blockchain.info',
47
+ 'blockexplorer.com',
48
+ 'webbtc.com',
49
+ ],
50
+ checkpoints: {
51
+ 11111 => "0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d",
52
+ 33333 => "000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6",
53
+ 74000 => "0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20",
54
+ 105000 => "00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97",
55
+ 134444 => "00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe",
56
+ 168000 => "000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763",
57
+ 193000 => "000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317",
58
+ 210000 => "000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e",
59
+ 216116 => "00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e",
60
+ 225430 => "00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932",
61
+ 290000 => "0000000000000000fa0b2badd05db0178623ebf8dd081fe7eb874c26e27d0b3b",
62
+ 300000 => "000000000000000082ccf8f1557c5d40b21edabb18d2d691cfbf87118bac7254",
63
+ 305000 => "0000000000000000142bb90561e1a907d500bf534a6727a63a92af5b6abc6160",
64
+ }
65
+ }
66
+ }
67
+
68
+ def bitcoin_elliptic_curve
69
+ ::OpenSSL::PKey::EC.new("secp256k1")
70
+ end
71
+
72
+ def generate_key
73
+ key = bitcoin_elliptic_curve.generate_key
74
+ inspect_key( key )
75
+ end
76
+
77
+ def inspect_key(key)
78
+ [ key.private_key_hex, key.public_key_hex ]
79
+ end
80
+
81
+ def pubkey_to_address(pubkey, type = :pubkey_hash)
82
+ hash160_to_address( hash160(pubkey) )
83
+ end
84
+
85
+ def hash160_to_address(hex)
86
+ encode_address hex, address_version
87
+ end
88
+
89
+ def encode_address(hex, version)
90
+ hex = version + hex
91
+ encode_base58(hex + checksum(hex))
92
+ end
93
+
94
+ def network
95
+ @network_options ||= NETWORKS[:bitcoin].dup
96
+ end
97
+
98
+ def encode_base58(hex)
99
+ leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2
100
+ ("1"*leading_zero_bytes) + Util.int_to_base58( hex.to_i(16) )
101
+ end
102
+
103
+ def checksum(hex)
104
+ b = [hex].pack("H*") # unpack hex
105
+ Digest::SHA256.hexdigest( Digest::SHA256.digest(b) )[0...8]
106
+ end
107
+
108
+ def hash160(hex)
109
+ bytes = [hex].pack("H*")
110
+ Digest::RMD160.hexdigest Digest::SHA256.digest(bytes)
111
+ end
112
+
113
+ def network=(name)
114
+ raise "Network descriptor '#{name}' not found." unless NETWORKS[name.to_sym]
115
+ @network_options = nil # clear cached parameters
116
+ @network = name.to_sym
117
+ @network_project = network[:project] rescue nil
118
+ Dogecoin.load if dogecoin? || dogecoin_testnet?
119
+ Namecoin.load if namecoin? && defined?(Namecoin)
120
+ @network
121
+ end
122
+
123
+ def valid_address?(address)
124
+ hex = Util.decode_base58(address) rescue nil
125
+ return false unless hex && hex.bytesize == 50
126
+ return false unless [address_version, p2sh_version].include?(hex[0...2])
127
+ Util.base58_checksum?(address)
128
+ end
129
+
130
+ def address_version; NETWORKS[:bitcoin][:address_version]; end
131
+ def p2sh_version; NETWORKS[:bitcoin][:p2sh_version]; end
132
+
133
+ end
@@ -0,0 +1,22 @@
1
+ require 'openssl'
2
+
3
+ module ::OpenSSL
4
+ class BN
5
+ def self.from_hex(hex); new(hex, 16); end
6
+ def to_hex; to_i.to_s(16); end
7
+ def to_mpi; to_s(0).unpack("C*"); end
8
+ end
9
+ class PKey::EC
10
+ def private_key_hex; private_key.to_hex.rjust(64, '0'); end
11
+ def public_key_hex; public_key.to_hex.rjust(130, '0'); end
12
+ def pubkey_compressed?; public_key.group.point_conversion_form == :compressed; end
13
+ end
14
+ class PKey::EC::Point
15
+ def self.from_hex(group, hex)
16
+ new(group, BN.from_hex(hex))
17
+ end
18
+ def to_hex; to_bn.to_hex; end
19
+ def self.bn2mpi(hex) BN.from_hex(hex).to_mpi; end
20
+ def ec_add(point); self.class.new(group, OpenSSL::BN.from_hex(OpenSSL_EC.ec_add(self, point))); end
21
+ end
22
+ end
@@ -0,0 +1,60 @@
1
+ require 'digest/sha2'
2
+ require 'digest/rmd160'
3
+
4
+ module Util
5
+
6
+ COIN = 100_000_000
7
+ CENT = 1_000_000
8
+
9
+ NETWORKS = {
10
+ bitcoin: {
11
+ project: :bitcoin,
12
+ address_version: "00",
13
+ p2sh_version: "05"
14
+ }
15
+ }
16
+
17
+ def self.int_to_base58(int_val, leading_zero_bytes=0)
18
+ alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
19
+ base58_val, base = '', alpha.size
20
+ while int_val > 0
21
+ int_val, remainder = int_val.divmod(base)
22
+ base58_val = alpha[remainder] + base58_val
23
+ end
24
+ base58_val
25
+ end
26
+
27
+ def self.base58_to_int(base58_val)
28
+ alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
29
+ int_val, base = 0, alpha.size
30
+ base58_val.reverse.each_char.with_index do |char,index|
31
+ raise ArgumentError, 'Value not a valid Base58 String.' unless char_index = alpha.index(char)
32
+ int_val += char_index*(base**index)
33
+ end
34
+ int_val
35
+ end
36
+
37
+ def self.decode_base58(base58_val)
38
+ s = base58_to_int(base58_val).to_s(16); s = (s.bytesize.odd? ? '0'+s : s)
39
+ s = '' if s == '00'
40
+ leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
41
+ s = ("00"*leading_zero_bytes) + s if leading_zero_bytes > 0
42
+ s
43
+ end
44
+
45
+ def self.base58_checksum?(base58)
46
+ hex = decode_base58(base58) rescue nil
47
+ return false unless hex
48
+ checksum( hex[0...42] ) == hex[-8..-1]
49
+ end
50
+
51
+ def self.checksum(hex)
52
+ b = [hex].pack("H*") # unpack hex
53
+ Digest::SHA256.hexdigest( Digest::SHA256.digest(b) )[0...8]
54
+ end
55
+
56
+ def network
57
+ @network_options ||= NETWORKS[:bitcoin].dup
58
+ end
59
+
60
+ end
@@ -0,0 +1,3 @@
1
+ module RubyBitcoinWallet
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_bitcoin_wallet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_bitcoin_wallet"
8
+ spec.version = RubyBitcoinWallet::VERSION
9
+ spec.authors = ["Nicolas Tonnelier"]
10
+ spec.email = ["na.tonnelier@gmail.com"]
11
+ spec.summary = %q{Ruby methods to interact with Bitcoin blockchain.}
12
+ spec.description = %q{Main features to allow interaction with Bitcoin blockchain.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe RubyBitcoinWallet do
4
+ subject { RubyBitcoinWallet.new }
5
+
6
+ describe '#process' do
7
+ let(:input) { 'My input.' }
8
+ let(:output) { subject.process(input) }
9
+
10
+ it 'creates private and public keys' do
11
+ # expect(output.downcase).to eq output
12
+ end
13
+
14
+ it 'creates address' do
15
+ # expect(output).to match /so grandmom./i
16
+ # expect(output).to match /such sweater./i
17
+ # expect(output).to match /very christmas./i
18
+ end
19
+
20
+ it 'returns error message' do
21
+ # expect(output).to end_with 'wow.'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ require 'ruby_bitcoin_wallet'
2
+
3
+ # Requires supporting ruby files with custom matchers and macros, etc, in
4
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
5
+ # run as spec files by default. This means that files in spec/support that end
6
+ # in _spec.rb will both be required and run as specs, causing the specs to be
7
+ # run twice. It is recommended that you do not name files matching this glob to
8
+ # end with _spec.rb. You can configure this pattern with with the --pattern
9
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
10
+ Dir[File.expand_path('spec/support/**/*.rb')].each(&method(:require)) #{ |f| require f }
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_bitcoin_wallet
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Tonnelier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Main features to allow interaction with Bitcoin blockchain.
56
+ email:
57
+ - na.tonnelier@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/ruby_bitcoin_wallet.rb
68
+ - lib/ruby_bitcoin_wallet/open_ssl.rb
69
+ - lib/ruby_bitcoin_wallet/util.rb
70
+ - lib/ruby_bitcoin_wallet/version.rb
71
+ - ruby_bitcoin_wallet.gemspec
72
+ - spec/ruby_bitcoin_wallet_spec.rb
73
+ - spec/spec_helper.rb
74
+ - tasks/rspec.rake
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Ruby methods to interact with Bitcoin blockchain.
99
+ test_files:
100
+ - spec/ruby_bitcoin_wallet_spec.rb
101
+ - spec/spec_helper.rb