vanitygen-wrapper 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7acb4fcb9c91d2780d7fd860a8fc77fc029e059b
4
- data.tar.gz: 946aad92bb08fac89a0129d2303c30625c61e832
3
+ metadata.gz: 9b071859b8782b0501328ca2022a20c7c99d8d0e
4
+ data.tar.gz: d9ee8a99564dbf34747c0ce35e1e5a212dbfd9dd
5
5
  SHA512:
6
- metadata.gz: 7fe174885c90d8653dbfca10fa42cc8fa55b01f22e19222f88aff97e8d1d9ea2ca0283a67fcef7cee7288fd0b3b6d770e753eb97cf958d4133efd3d50ef05e27
7
- data.tar.gz: 861a9d35b6be688a67e4d4426c998b068578287b20fb7962fa312dabf48b31ac1f07142c97fcd80e7bb633648e679db73b1f0bc73af65d72c7e5927a97c26c02
6
+ metadata.gz: 77a4a440eb85dad93951f302d87e6f4c40406fda5f1f74184b0302d968711fd5a8e0207c294b44a3787d6b3812bf76c85765ac2f036c501a98654c1da160d9c4
7
+ data.tar.gz: d896df076502f7658860066f3fb06dff393ae33d811a5adde0e8e102b0be8d31ea8f79d0a945fbbf5e862cf225ca2752f7db808dab84fb92837fbc52504f8e0e
@@ -1,4 +1,5 @@
1
1
  require 'mkfifo'
2
+ require 'fileutils'
2
3
  require 'open3'
3
4
  require 'tempfile'
4
5
 
@@ -13,6 +14,25 @@ module Vanitygen
13
14
  litecoin: '-L',
14
15
  }
15
16
 
17
+ def work_dir
18
+ return @work_dir unless block_given?
19
+
20
+ if @work_dir
21
+ Dir.chdir @work_dir do
22
+ yield
23
+ end
24
+ else
25
+ yield
26
+ end
27
+ end
28
+
29
+ def work_dir=(dir)
30
+ @work_dir = dir
31
+ if dir
32
+ FileUtils.mkdir_p dir
33
+ end
34
+ end
35
+
16
36
  attr_accessor :executable
17
37
  def executable
18
38
  @executable ||= 'vanitygen'
@@ -29,46 +49,49 @@ module Vanitygen
29
49
  end
30
50
 
31
51
  def valid?(pattern, options={})
32
- flags = flags_from({simulate: true,
33
- patterns: [pattern]
34
- }.merge(options))
35
- pid = Process.spawn(executable, *flags, out: '/dev/null', err: '/dev/null')
36
- pid, status = Process.wait2(pid)
37
- status == 0
52
+ flags = flags_from(options, simulate: true, patterns: [pattern])
53
+
54
+ work_dir do
55
+ pid = Process.spawn(executable, *flags, out: '/dev/null', err: '/dev/null')
56
+ pid, status = Process.wait2(pid)
57
+ status == 0
58
+ end
38
59
  end
39
60
 
40
61
  def difficulty(pattern, options={})
41
- flags = flags_from({simulate: true,
42
- patterns: [pattern]
43
- }.merge(options))
62
+ flags = flags_from(options, simulate: true, patterns: [pattern])
63
+
44
64
  msg = ''
45
- Open3.popen3(executable, *flags) do |stdin, stdout, stderr, wait_thr|
46
- stdin.close
47
- stdout.close
48
- while !stderr.eof?
49
- msg << stderr.read
65
+ work_dir do
66
+ Open3.popen3(executable, *flags) do |stdin, stdout, stderr, wait_thr|
67
+ stdin.close
68
+ stdout.close
69
+ while !stderr.eof?
70
+ msg << stderr.read
71
+ end
72
+ stderr.close
73
+ raise "vanitygen status (#{wait_thr.value}) err: #{msg}" if wait_thr.value != 0
50
74
  end
51
- stderr.close
52
- raise "vanitygen status (#{wait_thr.value}) err: #{msg}" if wait_thr.value != 0
53
75
  end
54
76
  msg.split.last.to_i
55
77
  end
56
78
 
57
79
  def generate(pattern, options={})
58
- flags = flags_from({patterns: [pattern]
59
- }.merge(options))
80
+ flags = flags_from(options, patterns: [pattern])
60
81
 
61
82
  msg = ''
62
- Open3.popen3(executable, *flags) do |stdin, stdout, stderr, wait_thr|
63
- stdin.close
64
- while !stdout.eof?
65
- msg << stdout.read
66
- end
67
- stdout.close
83
+ work_dir do
84
+ Open3.popen3(executable, *flags) do |stdin, stdout, stderr, wait_thr|
85
+ stdin.close
86
+ while !stdout.eof?
87
+ msg << stdout.read
88
+ end
89
+ stdout.close
68
90
 
69
- error = stderr.read
70
- stderr.close
71
- raise "vanitygen status (#{wait_thr.value}) err: #{error}" if wait_thr.value != 0
91
+ error = stderr.read
92
+ stderr.close
93
+ raise "vanitygen status (#{wait_thr.value}) err: #{error}" if wait_thr.value != 0
94
+ end
72
95
  end
73
96
 
74
97
  parse(msg)[0]
@@ -77,27 +100,30 @@ module Vanitygen
77
100
  def continuous(patterns, options={}, &block)
78
101
  raise LocalJumpError if block.nil?
79
102
 
80
- patterns_file = Tempfile.new('vanitygen-patterns')
103
+ patterns_file = Tempfile.new('vanitygen-patterns-', work_dir)
81
104
  patterns.each do |pattern|
82
105
  patterns_file.puts pattern
83
106
  end
84
107
  patterns_file.flush
85
108
 
86
- tmp_pipe = "/tmp/vanitygen-pipe-#{rand(1000000)}"
109
+ tmp_pipe = File.join(work_dir || Dir.tmpdir, Time.now.strftime("vanitygen-pipe-%Y%m%d-#{rand(1000000)}"))
87
110
  File.mkfifo(tmp_pipe)
88
111
 
89
- flags = flags_from({continuous: true,
90
- patterns_file: patterns_file.path,
91
- output_file: tmp_pipe,
92
- patterns: patterns,
93
- }.merge(options))
94
-
95
- # Unfortunately, vanitygen spams stdout with progress
96
- pid_vanitygen = Process.spawn(executable, *flags, out: '/dev/null', err: '/dev/null')
97
- while child_alive?(pid_vanitygen)
98
- File.open(tmp_pipe, 'r') do |file|
99
- while !file.eof? and (msg = file.read)
100
- parse(msg).each(&block)
112
+ flags = flags_from(options, continuous: true,
113
+ patterns_file: patterns_file.path,
114
+ output_file: tmp_pipe,
115
+ patterns: patterns)
116
+
117
+ pid_vanitygen = nil
118
+ work_dir do
119
+ # Unfortunately, vanitygen spams stdout with progress
120
+ pid_vanitygen = Process.spawn(executable, *flags, out: '/dev/null', err: '/dev/null')
121
+
122
+ while child_alive?(pid_vanitygen)
123
+ File.open(tmp_pipe, 'r') do |file|
124
+ while !file.eof? and (msg = file.read)
125
+ parse(msg).each(&block)
126
+ end
101
127
  end
102
128
  end
103
129
  end
@@ -129,7 +155,9 @@ module Vanitygen
129
155
  false
130
156
  end
131
157
 
132
- def flags_from(options)
158
+ def flags_from(options, default)
159
+ options = default.merge(options)
160
+
133
161
  [].tap do |flags|
134
162
  patterns =
135
163
  if options[:patterns].nil?
@@ -1,3 +1,3 @@
1
1
  module Vanitygen
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vanitygen-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Feng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-22 00:00:00.000000000 Z
11
+ date: 2014-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mkfifo