vanitygen-wrapper 0.0.1

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: 413a2d4a3858258fe5778cc45e10b2719e56d662
4
+ data.tar.gz: 6db5b7f020f2a48d2f5486444a154d1e323cbc77
5
+ SHA512:
6
+ metadata.gz: a7715ee966b9a2821844df51b9df2eb85a5c063b6d79363e6759a167580215ee21635e3b018fb91c7f5c1e1bea79eafba0faf251971ac499c7eb7707b3e07f4c
7
+ data.tar.gz: 32dd98358eabe88a4e847c1426389b684f2f9234da83bf8d452c5245b908e1ac6694e367a6b3b550023e9de37c9ee2c60fc2e916dbf666bf10e0751a23025572
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vanitygen.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Benjamin Feng
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,68 @@
1
+ # vanitygen-wrapper
2
+
3
+ Thin ruby wrapper around vanitygen executable. Sibling project of <https://github.com/bitvain/vanitygen-ruby>.
4
+
5
+ ## Installation
6
+
7
+ Download and install <https://github.com/samr7/vanitygen>
8
+
9
+ Make sure `vanitygen` is available in your `$PATH`.
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'vanitygen-wrapper', require 'vanitygen'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install vanitygen-wrapper
24
+
25
+ ## Usage
26
+
27
+ Verify pattern validity:
28
+
29
+ ```ruby
30
+ >> Vanitygen.valid?('1AB')
31
+ => true
32
+ >> Vanitygen.valid?('2AB')
33
+ => false
34
+ ```
35
+
36
+ Check pattern difficulty:
37
+
38
+ ```ruby
39
+ >> Vanitygen.difficulty('1AB')
40
+ => 1330
41
+ >> Vanitygen.difficulty('1AB', case_insensitive: true)
42
+ => 654
43
+ ```
44
+
45
+ Generate single addresses:
46
+
47
+ ```ruby
48
+ >> Vanitygen.generate('1AB')
49
+ => {:pattern=>"1AB", :address=>"1ABz3svEbWHyj5penc6LLX6xvDfzZcsZu9", :private_key=>"5KRtsDfuiMf549QU1X6mNcTuYxd2V4XsjQBD8pgUMEPFGFADMzb"}
50
+ ```
51
+
52
+ Continuously generate addresses:
53
+
54
+ ```ruby
55
+ >> Vanitygen.continuous('1AB') { |data| puts data }
56
+ {:pattern=>"1AB", :address=>"1ABsD3pMDJbmpQx941faFn5Tg7aeVccW9c", :private_key=>"5KAjmVJAoBgNNNtVqCWYofNH6N8erSBGd7omsLCzSWg9DHZJd15"}
57
+ {:pattern=>"1AB", :address=>"1ABzPupWHxiWRBhAPYmcxbBQoonE1CgF7u", :private_key=>"5KRB9rV78DdTp6RWD7K1mA7iNgRGTXDuA7aGvC4xJLPg4YLx5j2"}
58
+ {:pattern=>"1AB", :address=>"1ABc8fVqFW2SXBfmQh5B6u1cg9SEPF2xMP", :private_key=>"5JAVeQBXT2ZL5p6oLgK4QAiDVdC8J9ytLHT999TxzSwvHnkgu3T"}
59
+ [...]
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/bitvain/vanitygen-wrapper/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/vanitygen.rb ADDED
@@ -0,0 +1,160 @@
1
+ require 'mkfifo'
2
+ require 'open3'
3
+ require 'tempfile'
4
+
5
+ module Vanitygen
6
+ autoload :VERSION, 'vanitygen/version'
7
+
8
+ class << self
9
+ NETWORKS = {
10
+ bitcoin: nil,
11
+ testnet3: '-T',
12
+ }
13
+
14
+ def network
15
+ @network ||= :bitcoin
16
+ end
17
+
18
+ def network=(network)
19
+ network = network.to_sym
20
+ raise "network #{network} not supported" unless NETWORKS.has_key?(network)
21
+ @network = network
22
+ end
23
+
24
+ def valid?(pattern, options={})
25
+ flags = flags_from({simulate: true,
26
+ patterns: [pattern]
27
+ }.merge(options))
28
+ pid = Process.spawn('vanitygen', *flags, out: '/dev/null', err: '/dev/null')
29
+ pid, status = Process.wait2(pid)
30
+ status == 0
31
+ end
32
+
33
+ def difficulty(pattern, options={})
34
+ flags = flags_from({simulate: true,
35
+ patterns: [pattern]
36
+ }.merge(options))
37
+ msg = ''
38
+ Open3.popen3('vanitygen', *flags) do |stdin, stdout, stderr, wait_thr|
39
+ stdin.close
40
+ stdout.close
41
+ while !stderr.eof?
42
+ msg << stderr.read
43
+ end
44
+ stderr.close
45
+ raise "vanitygen status (#{wait_thr.value}) err: #{msg}" if wait_thr.value != 0
46
+ end
47
+ msg.split.last.to_i
48
+ end
49
+
50
+ def generate(pattern, options={})
51
+ flags = flags_from({patterns: [pattern]
52
+ }.merge(options))
53
+
54
+ msg = ''
55
+ Open3.popen3('vanitygen', *flags) do |stdin, stdout, stderr, wait_thr|
56
+ stdin.close
57
+ while !stdout.eof?
58
+ msg << stdout.read
59
+ end
60
+ stdout.close
61
+
62
+ error = stderr.read
63
+ stderr.close
64
+ raise "vanitygen status (#{wait_thr.value}) err: #{error}" if wait_thr.value != 0
65
+ end
66
+
67
+ parse(msg)[0]
68
+ end
69
+
70
+ def continuous(patterns, options={}, &block)
71
+ raise LocalJumpError if block.nil?
72
+
73
+ patterns_file = Tempfile.new('vanitygen-patterns')
74
+ patterns.each do |pattern|
75
+ patterns_file.puts pattern
76
+ end
77
+ patterns_file.flush
78
+
79
+ tmp_pipe = "/tmp/vanitygen-pipe-#{rand(1000000)}"
80
+ File.mkfifo(tmp_pipe)
81
+
82
+ flags = flags_from({continuous: true,
83
+ patterns_file: patterns_file.path,
84
+ output_file: tmp_pipe,
85
+ patterns: patterns,
86
+ }.merge(options))
87
+
88
+ thread = Thread.new do
89
+ # FIXME: ignore EOF instead of reopening
90
+ loop do
91
+ File.open(tmp_pipe, 'r') do |file|
92
+ while !file.eof? and (msg = file.read)
93
+ parse(msg).each do |data|
94
+ block.call(data)
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ pid_vanitygen = Process.spawn('vanitygen', *flags, out: '/dev/null', err: '/dev/null')
102
+ Process.wait(pid_vanitygen)
103
+ ensure
104
+ thread && thread.kill
105
+ if pid_vanitygen
106
+ begin
107
+ Process.kill('TERM', pid_vanitygen)
108
+ Process.detach(pid_vanitygen) # if no detach, vanitygen will zombify
109
+ rescue Errno::ESRCH
110
+ # vanitygen died by itself. Ignore and continue cleanup.
111
+ end
112
+ end
113
+
114
+ tmp_pipe && File.exist?(tmp_pipe) && File.delete(tmp_pipe)
115
+ if patterns_file
116
+ patterns_file.close
117
+ patterns_file.unlink
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ def flags_from(options)
124
+ [].tap do |flags|
125
+ patterns =
126
+ if options[:patterns].nil?
127
+ nil
128
+ elsif options[:patterns].any? { |p| p.is_a?(Regexp) }
129
+ if options[:patterns].all? { |p| p.is_a?(Regexp) }
130
+ flags << '-r'
131
+ options[:patterns].map(&:source)
132
+ else
133
+ raise TypeError
134
+ end
135
+ else
136
+ options[:patterns].map(&:to_s)
137
+ end
138
+
139
+ flags << NETWORKS[network] if NETWORKS[network]
140
+ flags << '-n' if options[:simulate]
141
+ flags << '-k' if options[:continuous]
142
+ flags << '-i' if options[:case_insensitive]
143
+ flags << '-f' << options[:patterns_file] if options[:patterns_file]
144
+ flags << '-o' << options[:output_file] if options[:output_file]
145
+ flags.concat(patterns) if patterns && !options[:patterns_file]
146
+ end
147
+ end
148
+
149
+ def parse(msg)
150
+ lines = msg.split("\n").grep(/(Pattern|Address|Privkey)/)
151
+ lines.each_slice(3).map do |snippet|
152
+ {
153
+ pattern: snippet[0].split.last,
154
+ address: snippet[1].split.last,
155
+ private_key: snippet[2].split.last,
156
+ }
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,3 @@
1
+ module Vanitygen
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,150 @@
1
+ require 'vanitygen'
2
+
3
+ require 'bitcoin'
4
+ require 'devnull'
5
+ require 'timeout'
6
+
7
+ describe Vanitygen do
8
+ let(:pattern_string_a) { '1A' }
9
+ let(:pattern_string_b) { '1B' }
10
+ let(:pattern_string_ab) { '1AB' }
11
+ let(:pattern_regex_ab) { /[aA][bB]/ }
12
+ let(:pattern_any) { '1' }
13
+
14
+ describe '.generate' do
15
+ context 'string' do
16
+ subject { Vanitygen.generate(pattern_string_a) }
17
+
18
+ it 'has valid address' do
19
+ expect(subject[:address]).to satisfy { |addr| Bitcoin.valid_address?(addr) }
20
+ end
21
+
22
+ it 'has address starting with pattern' do
23
+ expect(subject[:address]).to start_with(pattern_string_a)
24
+ end
25
+
26
+ it 'has correct private_key to unlock pattern' do
27
+ bkey = Bitcoin::Key.from_base58(subject[:private_key])
28
+ expect(subject[:address]).to eq(bkey.addr)
29
+ end
30
+ end
31
+
32
+ context 'regex' do
33
+ subject { Vanitygen.generate(pattern_regex_ab) }
34
+
35
+ it 'has valid address' do
36
+ expect(subject[:address]).to satisfy { |addr| Bitcoin.valid_address?(addr) }
37
+ end
38
+
39
+ it 'has address matching pattern' do
40
+ expect(subject[:address]).to match(pattern_regex_ab)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '.continuous' do
46
+ it 'requires a block' do
47
+ expect{Vanitygen.continuous([pattern_any])}.to raise_error(LocalJumpError)
48
+ end
49
+
50
+ it 'requires same type' do
51
+ noop = proc{}
52
+ expect{Vanitygen.continuous([pattern_regex_ab, pattern_string_a], &noop)}.to raise_error(TypeError)
53
+ end
54
+
55
+ context 'threaded with capture block' do
56
+ let(:captured) { [] }
57
+
58
+ def capture(attr=nil)
59
+ if attr.nil?
60
+ proc { |data| captured << data }
61
+ else
62
+ proc { |data| captured << data[attr] }
63
+ end
64
+ end
65
+
66
+ def continuous_with_timeout(patterns, options={}, &block)
67
+ duration = options.delete(:timeout) || 0.1
68
+ Timeout.timeout duration do
69
+ Vanitygen.continuous(patterns, options, &block)
70
+ end
71
+
72
+ raise 'Expected timeout but did not happen'
73
+ rescue Timeout::Error => e
74
+ # expected
75
+ end
76
+
77
+ context 'base test' do
78
+ before do
79
+ continuous_with_timeout([pattern_any], &capture(:address))
80
+ end
81
+
82
+ it 'runs a lot' do
83
+ expect(captured.count).to be > 10
84
+ end
85
+
86
+ it 'returns valid addresses' do
87
+ expect(captured).to all(satisfy { |addr| Bitcoin.valid_address?(addr) })
88
+ end
89
+ end
90
+
91
+ context 'with string' do
92
+ it 'starts with matching pattern' do
93
+ continuous_with_timeout([pattern_string_a], &capture(:address))
94
+ expect(captured.size).to be > 1
95
+ expect(captured).to all(start_with(pattern_string_a))
96
+ end
97
+
98
+ it 'matches with case insensitivity' do
99
+ continuous_with_timeout([pattern_string_ab], case_insensitive: true, &capture(:address))
100
+ prefixes = captured.map { |addr| addr[0..2] }
101
+ expect(prefixes.uniq.size).to be > 1
102
+ end
103
+
104
+ it 'matches multiple patterns' do
105
+ continuous_with_timeout([pattern_string_a, pattern_string_b], &capture(:address))
106
+ # should be
107
+ # expect(captured).to any(addr.start_with?(pattern_string_a))
108
+ # expect(captured).to any(addr.start_with?(pattern_string_b))
109
+ expect(captured).to satisfy { |a| a.any? { |addr| addr.start_with?(pattern_string_a) } }
110
+ expect(captured).to satisfy { |a| a.any? { |addr| addr.start_with?(pattern_string_b) } }
111
+ end
112
+ end
113
+
114
+ context 'with regex' do
115
+ it 'matches the regex' do
116
+ continuous_with_timeout([pattern_regex_ab], &capture(:address))
117
+ expect(captured.size).to be > 1
118
+ expect(captured).to all(match(pattern_regex_ab))
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ describe '.difficulty' do
125
+ it 'returns difficulty in Numeric' do
126
+ expect(Vanitygen.difficulty(pattern_string_a)).to be_a Numeric
127
+ end
128
+ end
129
+
130
+ describe '.valid?' do
131
+ it 'is true for starting with 1' do
132
+ expect(Vanitygen.valid?('1abc')).to be(true)
133
+ end
134
+
135
+ it 'is false for starting with something else' do
136
+ expect(Vanitygen.valid?('abc')).to be(false)
137
+ end
138
+
139
+ it 'is false for really long strings' do
140
+ expect(Vanitygen.valid?('1abcdefghijklmnopqrstuvwxyz')).to be(false)
141
+ end
142
+
143
+ it 'is false for illegal characters' do
144
+ expect(Vanitygen.valid?('10')).to be(false)
145
+ expect(Vanitygen.valid?('1O')).to be(false)
146
+ expect(Vanitygen.valid?('1I')).to be(false)
147
+ expect(Vanitygen.valid?('1l')).to be(false)
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vanitygen/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'vanitygen-wrapper'
8
+ spec.version = Vanitygen::VERSION
9
+ spec.authors = ['Benjamin Feng']
10
+ spec.email = ['contact@fengb.info']
11
+ spec.summary = %q{Thin ruby wrapper around vanitygen executable}
12
+ spec.description = %q{Thin ruby wrapper around vanitygen executable. Sibling project of <https://github.com/bitvain/vanitygen-ruby>.}
13
+ spec.homepage = 'https://github.com/bitvain/vanitygen-wrapper'
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_runtime_dependency 'mkfifo', '~> 0.0.1'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'bitcoin-ruby', '~> 0.0.6'
25
+ spec.add_development_dependency 'ffi', '~> 1.9'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vanitygen-wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Feng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mkfifo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bitcoin-ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: ffi
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Thin ruby wrapper around vanitygen executable. Sibling project of <https://github.com/bitvain/vanitygen-ruby>.
98
+ email:
99
+ - contact@fengb.info
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/vanitygen.rb
110
+ - lib/vanitygen/version.rb
111
+ - spec/vanitygen_spec.rb
112
+ - vanitygen-wrapper.gemspec
113
+ homepage: https://github.com/bitvain/vanitygen-wrapper
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.2.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Thin ruby wrapper around vanitygen executable
137
+ test_files:
138
+ - spec/vanitygen_spec.rb