zold 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -1
  3. data/bin/zold +21 -81
  4. data/fixtures/keys/1.pub +1 -0
  5. data/fixtures/keys/2 +51 -0
  6. data/fixtures/keys/2.pub +1 -0
  7. data/fixtures/scripts/print-helps.sh +15 -0
  8. data/fixtures/scripts/push-and-pull.sh +7 -3
  9. data/lib/zold/commands/clean.rb +24 -7
  10. data/lib/zold/commands/create.rb +16 -4
  11. data/lib/zold/commands/diff.rb +32 -9
  12. data/lib/zold/commands/fetch.rb +36 -9
  13. data/lib/zold/commands/invoice.rb +64 -0
  14. data/lib/zold/commands/list.rb +1 -1
  15. data/lib/zold/commands/merge.rb +31 -10
  16. data/lib/zold/commands/node.rb +6 -2
  17. data/lib/zold/commands/pay.rb +40 -17
  18. data/lib/zold/commands/propagate.rb +34 -14
  19. data/lib/zold/commands/push.rb +27 -9
  20. data/lib/zold/commands/remote.rb +63 -34
  21. data/lib/zold/commands/show.rb +34 -9
  22. data/lib/zold/copies.rb +4 -0
  23. data/lib/zold/http.rb +1 -1
  24. data/lib/zold/key.rb +17 -11
  25. data/lib/zold/node/farm.rb +8 -0
  26. data/lib/zold/node/front.rb +13 -3
  27. data/lib/zold/patch.rb +8 -8
  28. data/lib/zold/prefixes.rb +53 -0
  29. data/lib/zold/remotes.rb +6 -0
  30. data/lib/zold/score.rb +4 -4
  31. data/lib/zold/signature.rb +9 -9
  32. data/lib/zold/txn.rb +111 -0
  33. data/lib/zold/version.rb +1 -1
  34. data/lib/zold/wallet.rb +32 -59
  35. data/lib/zold/wallets.rb +2 -2
  36. data/test/commands/test_clean.rb +5 -4
  37. data/test/commands/test_create.rb +3 -3
  38. data/test/commands/test_diff.rb +16 -15
  39. data/test/commands/test_fetch.rb +13 -11
  40. data/test/commands/test_invoice.rb +46 -0
  41. data/test/commands/test_list.rb +4 -4
  42. data/test/commands/test_merge.rb +21 -22
  43. data/test/commands/test_node.rb +9 -9
  44. data/test/commands/test_pay.rb +12 -12
  45. data/test/commands/test_remote.rb +11 -11
  46. data/test/commands/test_show.rb +9 -7
  47. data/test/node/fake_node.rb +3 -3
  48. data/test/node/test_farm.rb +1 -1
  49. data/test/node/test_front.rb +6 -6
  50. data/test/test_amount.rb +1 -1
  51. data/test/test_copies.rb +1 -1
  52. data/test/test_http.rb +1 -1
  53. data/test/test_id.rb +1 -1
  54. data/test/test_key.rb +19 -1
  55. data/test/test_patch.rb +11 -11
  56. data/test/test_prefixes.rb +46 -0
  57. data/test/test_remotes.rb +1 -1
  58. data/test/test_score.rb +1 -1
  59. data/test/test_signature.rb +9 -11
  60. data/test/test_wallet.rb +22 -21
  61. data/test/test_wallets.rb +4 -4
  62. metadata +12 -1
data/lib/zold/score.rb CHANGED
@@ -29,10 +29,10 @@ require_relative 'remotes'
29
29
  module Zold
30
30
  # Score
31
31
  class Score
32
- DEFAULT_STRENGTH = 8
33
- attr_reader :time, :host, :port
32
+ STRENGTH = 7
33
+ attr_reader :time, :host, :port, :strength
34
34
  # time: UTC ISO 8601 string
35
- def initialize(time, host, port, suffixes = [], strength: DEFAULT_STRENGTH)
35
+ def initialize(time, host, port, suffixes = [], strength: STRENGTH)
36
36
  raise 'Time must be of type Time' unless time.is_a?(Time)
37
37
  raise 'Port must be of type Integer' unless port.is_a?(Integer)
38
38
  @time = time
@@ -44,7 +44,7 @@ module Zold
44
44
 
45
45
  ZERO = Score.new(Time.now, 'localhost', Remotes::PORT)
46
46
 
47
- def self.parse(text, strength: DEFAULT_STRENGTH)
47
+ def self.parse(text, strength: STRENGTH)
48
48
  _, time, host, port, suffixes = text.split(' ', 5)
49
49
  Score.new(
50
50
  Time.parse(time), host, port.to_i,
@@ -19,9 +19,9 @@
19
19
  # SOFTWARE.
20
20
 
21
21
  require 'time'
22
- require_relative 'key.rb'
23
- require_relative 'id.rb'
24
- require_relative 'amount.rb'
22
+ require_relative 'key'
23
+ require_relative 'id'
24
+ require_relative 'amount'
25
25
 
26
26
  # The signature of a transaction.
27
27
  #
@@ -31,18 +31,18 @@ require_relative 'amount.rb'
31
31
  module Zold
32
32
  # A signature
33
33
  class Signature
34
- def sign(pvt, txn)
35
- pvt.sign(block(txn))
34
+ def sign(pvt, t)
35
+ pvt.sign(block(t))
36
36
  end
37
37
 
38
- def valid?(pub, txn)
39
- pub.verify(txn[:sign], block(txn))
38
+ def valid?(pub, t)
39
+ pub.verify(t.sign, block(t))
40
40
  end
41
41
 
42
42
  private
43
43
 
44
- def block(txn)
45
- "#{txn[:id]};#{txn[:amount].to_i};#{txn[:bnf]};#{txn[:details]}"
44
+ def block(t)
45
+ [t.id, t.amount.to_i, t.prefix, t.bnf, t.details].join(';')
46
46
  end
47
47
  end
48
48
  end
data/lib/zold/txn.rb ADDED
@@ -0,0 +1,111 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'time'
22
+ require_relative 'id'
23
+ require_relative 'amount'
24
+ require_relative 'signature'
25
+
26
+ # The transaction.
27
+ #
28
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
30
+ # License:: MIT
31
+ module Zold
32
+ # A single transaction
33
+ class Txn
34
+ attr_reader :id, :date, :amount, :prefix, :bnf, :details, :sign
35
+ attr_writer :sign, :amount, :bnf
36
+ def initialize(id, date, amount, prefix, bnf, details)
37
+ raise "ID of transaction can't be negative: #{id}" if id < 1
38
+ @id = id
39
+ raise 'Time have to be of type Time' unless date.is_a?(Time)
40
+ raise "Time can't be in the future: #{date}" if date > Time.now
41
+ @date = date
42
+ raise 'The amount has to be of type Amount' unless amount.is_a?(Amount)
43
+ raise 'The amount can\'t be zero' if amount.zero?
44
+ @amount = amount
45
+ raise 'The bnf has to be of type Id' unless bnf.is_a?(Id)
46
+ @bnf = bnf
47
+ raise "Prefix is too short: \"#{prefix}\"" if prefix.length < 8
48
+ raise "Prefix is too long: \"#{prefix}\"" if prefix.length > 32
49
+ @prefix = prefix
50
+ raise 'Details can\'t be empty' if details.empty?
51
+ raise "Details are too long: \"#{details}\"" if details.length > 128
52
+ @details = details
53
+ end
54
+
55
+ def ==(other)
56
+ id == other.id && bnf == other.bnf
57
+ end
58
+
59
+ def to_s
60
+ [
61
+ @id,
62
+ @date.utc.iso8601,
63
+ @amount.to_i,
64
+ @prefix,
65
+ @bnf,
66
+ @details,
67
+ @sign
68
+ ].join(';')
69
+ end
70
+
71
+ def inverse(bnf)
72
+ t = clone
73
+ t.amount = amount.mul(-1)
74
+ t.bnf = bnf
75
+ t
76
+ end
77
+
78
+ def signed(pvt)
79
+ t = clone
80
+ t.sign = Signature.new.sign(pvt, self)
81
+ t
82
+ end
83
+
84
+ def self.parse(line, idx)
85
+ regex = Regexp.new(
86
+ [
87
+ '([0-9]+)',
88
+ '([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)',
89
+ '(-?[0-9]+)',
90
+ '([A-Za-z0-9]{8,32})',
91
+ '([a-f0-9]{16})',
92
+ '([a-zA-Z0-9 -.]{1,128})',
93
+ '([A-Za-z0-9+/]+={0,3})?'
94
+ ].join(';')
95
+ )
96
+ clean = line.strip
97
+ raise "Invalid line ##{idx}: #{line.inspect}" unless regex.match(clean)
98
+ parts = clean.split(';')
99
+ txn = Txn.new(
100
+ parts[0].to_i,
101
+ Time.parse(parts[1]),
102
+ Amount.new(coins: parts[2].to_i),
103
+ parts[3],
104
+ Id.new(parts[4]),
105
+ parts[5]
106
+ )
107
+ txn.sign = parts[6]
108
+ txn
109
+ end
110
+ end
111
+ end
data/lib/zold/version.rb CHANGED
@@ -23,5 +23,5 @@
23
23
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
24
24
  # License:: MIT
25
25
  module Zold
26
- VERSION = '0.2'.freeze
26
+ VERSION = '0.3'.freeze
27
27
  end
data/lib/zold/wallet.rb CHANGED
@@ -19,10 +19,11 @@
19
19
  # SOFTWARE.
20
20
 
21
21
  require 'time'
22
- require_relative 'key.rb'
23
- require_relative 'id.rb'
24
- require_relative 'amount.rb'
25
- require_relative 'signature.rb'
22
+ require_relative 'key'
23
+ require_relative 'id'
24
+ require_relative 'txn'
25
+ require_relative 'amount'
26
+ require_relative 'signature'
26
27
 
27
28
  # The wallet.
28
29
  #
@@ -70,29 +71,36 @@ module Zold
70
71
  end
71
72
 
72
73
  def balance
73
- txns.inject(Amount::ZERO) { |sum, t| sum + t[:amount] }
74
- end
75
-
76
- def sub(amount, target, pvtkey, details = '-')
77
- txn = {
78
- id: max + 1,
79
- date: Time.now,
80
- amount: amount.mul(-1),
81
- bnf: target,
82
- details: details
83
- }
84
- txn[:sign] = Signature.new.sign(pvtkey, txn)
85
- File.write(@file, (lines << to_line(txn)).join)
86
- txn[:amount] = amount
74
+ txns.inject(Amount::ZERO) { |sum, t| sum + t.amount }
75
+ end
76
+
77
+ def sub(amount, invoice, pvt, details = '-')
78
+ raise 'The amount has to be of type Amount' unless amount.is_a?(Amount)
79
+ raise "The amount can't be negative: #{amount}" if amount.negative?
80
+ raise 'The pvt has to be of type Key' unless pvt.is_a?(Key)
81
+ prefix, target = invoice.split('@')
82
+ txn = Txn.new(
83
+ max + 1,
84
+ Time.now,
85
+ amount.mul(-1),
86
+ prefix,
87
+ Id.new(target),
88
+ details
89
+ )
90
+ txn = txn.signed(pvt)
91
+ add(txn)
87
92
  txn
88
93
  end
89
94
 
90
95
  def add(txn)
91
- open(@file, 'a') { |f| f.print to_line(txn) }
96
+ raise 'The txn has to be of type Txn' unless txn.is_a?(Txn)
97
+ open(@file, 'a') { |f| f.print "#{txn}\n" }
92
98
  end
93
99
 
94
100
  def has?(id, bnf)
95
- !txns.find { |t| t[:id] == id && t[:bnf] == bnf }.nil?
101
+ raise 'The txn ID has to be of type Integer' unless id.is_a?(Integer)
102
+ raise 'The bnf has to be of type Id' unless bnf.is_a?(Id)
103
+ !txns.find { |t| t.id == id && t.bnf == bnf }.nil?
96
104
  end
97
105
 
98
106
  def key
@@ -101,15 +109,15 @@ module Zold
101
109
 
102
110
  def income
103
111
  txns.each do |t|
104
- yield t unless t[:amount].negative?
112
+ yield t unless t.amount.negative?
105
113
  end
106
114
  end
107
115
 
108
116
  def txns
109
117
  lines.drop(3)
110
118
  .each_with_index
111
- .map { |t, i| fields(t, i + 4) }
112
- .sort_by { |a| a[:date] }
119
+ .map { |line, i| Txn.parse(line, i + 4) }
120
+ .sort_by(&:date)
113
121
  end
114
122
 
115
123
  private
@@ -119,45 +127,10 @@ module Zold
119
127
  if all.empty?
120
128
  0
121
129
  else
122
- all.select { |t| t[:amount].negative? }.max_by { |t| t[:id] }[:id]
130
+ all.select { |t| t.amount.negative? }.max_by(&:id).id
123
131
  end
124
132
  end
125
133
 
126
- def to_line(txn)
127
- [
128
- txn[:id],
129
- txn[:date].utc.iso8601,
130
- txn[:amount].to_i,
131
- txn[:bnf],
132
- txn[:details],
133
- txn[:sign]
134
- ].join(';') + "\n"
135
- end
136
-
137
- def fields(line, idx)
138
- regex = Regexp.new(
139
- [
140
- '([0-9]+)',
141
- '([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)',
142
- '(-?[0-9]+)',
143
- '([a-f0-9]{16})',
144
- '([a-zA-Z0-9 -.]{1,128})',
145
- '([A-Za-z0-9+/]+={0,3})?'
146
- ].join(';')
147
- )
148
- clean = line.strip
149
- raise "Invalid line ##{idx}: #{line.inspect}" unless regex.match(clean)
150
- parts = clean.split(';')
151
- {
152
- id: parts[0].to_i,
153
- date: Time.parse(parts[1]),
154
- amount: Amount.new(coins: parts[2].to_i),
155
- bnf: Id.new(parts[3]),
156
- details: parts[4],
157
- sign: parts[5]
158
- }
159
- end
160
-
161
134
  def lines
162
135
  raise "File '#{@file}' is absent" unless File.exist?(@file)
163
136
  File.readlines(@file)
data/lib/zold/wallets.rb CHANGED
@@ -18,8 +18,8 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
19
  # SOFTWARE.
20
20
 
21
- require_relative 'id.rb'
22
- require_relative 'wallet.rb'
21
+ require_relative 'id'
22
+ require_relative 'wallet'
23
23
 
24
24
  # The local collection of wallets.
25
25
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -21,8 +21,8 @@
21
21
  require 'minitest/autorun'
22
22
  require 'tmpdir'
23
23
  require 'time'
24
- require_relative '../../lib/zold/copies.rb'
25
- require_relative '../../lib/zold/commands/clean.rb'
24
+ require_relative '../../lib/zold/copies'
25
+ require_relative '../../lib/zold/commands/clean'
26
26
 
27
27
  # CLEAN test.
28
28
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -31,10 +31,11 @@ require_relative '../../lib/zold/commands/clean.rb'
31
31
  class TestClean < Minitest::Test
32
32
  def test_cleans_copies
33
33
  Dir.mktmpdir 'test' do |dir|
34
- copies = Zold::Copies.new(File.join(dir, 'copies'))
34
+ id = Zold::Id.new
35
+ copies = Zold::Copies.new(File.join(dir, "copies/#{id}"))
35
36
  copies.add('a1', 'host-1', 80, 1, Time.now - 26 * 60)
36
37
  copies.add('a2', 'host-2', 80, 2, Time.now - 26 * 60)
37
- Zold::Clean.new(copies: copies).run
38
+ Zold::Clean.new(copies: copies.root).run([id.to_s])
38
39
  assert(copies.all.empty?)
39
40
  end
40
41
  end
@@ -20,9 +20,9 @@
20
20
 
21
21
  require 'minitest/autorun'
22
22
  require 'tmpdir'
23
- require_relative '../../lib/zold/wallets.rb'
24
- require_relative '../../lib/zold/key.rb'
25
- require_relative '../../lib/zold/commands/create.rb'
23
+ require_relative '../../lib/zold/wallets'
24
+ require_relative '../../lib/zold/key'
25
+ require_relative '../../lib/zold/commands/create'
26
26
 
27
27
  # CREATE test.
28
28
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -23,12 +23,13 @@ require 'tmpdir'
23
23
  require 'json'
24
24
  require 'time'
25
25
  require 'webmock/minitest'
26
- require_relative '../../lib/zold/wallet.rb'
27
- require_relative '../../lib/zold/id.rb'
28
- require_relative '../../lib/zold/copies.rb'
29
- require_relative '../../lib/zold/key.rb'
30
- require_relative '../../lib/zold/commands/pay.rb'
31
- require_relative '../../lib/zold/commands/diff.rb'
26
+ require_relative '../../lib/zold/wallets'
27
+ require_relative '../../lib/zold/wallet'
28
+ require_relative '../../lib/zold/id'
29
+ require_relative '../../lib/zold/copies'
30
+ require_relative '../../lib/zold/key'
31
+ require_relative '../../lib/zold/commands/pay'
32
+ require_relative '../../lib/zold/commands/diff'
32
33
 
33
34
  # DIFF test.
34
35
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -38,24 +39,24 @@ class TestDiff < Minitest::Test
38
39
  def test_diff_with_copies
39
40
  Dir.mktmpdir 'test' do |dir|
40
41
  id = Zold::Id.new
41
- file = File.join(dir, id.to_s)
42
- wallet = Zold::Wallet.new(file)
42
+ wallet = Zold::Wallet.new(File.join(dir, id.to_s))
43
43
  wallet.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
44
44
  first = Zold::Wallet.new(File.join(dir, 'copy-1'))
45
45
  File.write(first.path, File.read(wallet.path))
46
46
  second = Zold::Wallet.new(File.join(dir, 'copy-2'))
47
47
  File.write(second.path, File.read(wallet.path))
48
48
  Zold::Pay.new(
49
- payer: first,
50
- receiver: second,
51
- amount: Zold::Amount.new(zld: 14.95),
49
+ wallets: Zold::Wallets.new(dir),
52
50
  pvtkey: Zold::Key.new(file: 'fixtures/id_rsa')
53
- ).run(['--force'])
54
- copies = Zold::Copies.new(File.join(dir, 'copies'))
51
+ ).run([id.to_s, second.id.to_s, '14.95', '--force'])
52
+ copies = Zold::Copies.new(File.join(dir, "copies/#{id}"))
55
53
  copies.add(File.read(first.path), 'host-1', 80, 5)
56
54
  copies.add(File.read(second.path), 'host-2', 80, 5)
57
- diff = Zold::Diff.new(wallet: wallet, copies: copies).run
58
- assert(diff.include?('+1;'))
55
+ diff = Zold::Diff.new(
56
+ wallets: Zold::Wallets.new(dir),
57
+ copies: copies.root
58
+ ).run([id.to_s])
59
+ assert(diff.include?('-1;'))
59
60
  end
60
61
  end
61
62
  end
@@ -23,13 +23,13 @@ require 'tmpdir'
23
23
  require 'json'
24
24
  require 'time'
25
25
  require 'webmock/minitest'
26
- require_relative '../../lib/zold/wallet.rb'
27
- require_relative '../../lib/zold/remotes.rb'
28
- require_relative '../../lib/zold/id.rb'
29
- require_relative '../../lib/zold/copies.rb'
30
- require_relative '../../lib/zold/key.rb'
31
- require_relative '../../lib/zold/score.rb'
32
- require_relative '../../lib/zold/commands/fetch.rb'
26
+ require_relative '../../lib/zold/wallet'
27
+ require_relative '../../lib/zold/remotes'
28
+ require_relative '../../lib/zold/id'
29
+ require_relative '../../lib/zold/copies'
30
+ require_relative '../../lib/zold/key'
31
+ require_relative '../../lib/zold/score'
32
+ require_relative '../../lib/zold/commands/fetch'
33
33
 
34
34
  # FETCH test.
35
35
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -42,7 +42,6 @@ class TestFetch < Minitest::Test
42
42
  file = File.join(dir, id.to_s)
43
43
  wallet = Zold::Wallet.new(file)
44
44
  wallet.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
45
- copies = Zold::Copies.new(File.join(dir, 'copies'))
46
45
  remotes = Zold::Remotes.new(File.join(dir, 'remotes.csv'))
47
46
  remotes.clean
48
47
  stub_request(:get, "http://fake-1/wallet/#{id}").to_return(
@@ -57,7 +56,10 @@ class TestFetch < Minitest::Test
57
56
  )
58
57
  remotes.add('fake-1', 80)
59
58
  remotes.add('fake-2', 80)
60
- Zold::Fetch.new(id: id, copies: copies, remotes: remotes).run
59
+ copies = Zold::Copies.new(File.join(dir, "copies/#{id}"))
60
+ Zold::Fetch.new(copies: copies.root, remotes: remotes).run(
61
+ ['--ignore-score-weakness', id.to_s]
62
+ )
61
63
  assert_equal(copies.all[0][:name], '1')
62
64
  assert_equal(copies.all[0][:score], 0)
63
65
  end
@@ -66,7 +68,6 @@ class TestFetch < Minitest::Test
66
68
  def test_fetches_empty_wallet
67
69
  Dir.mktmpdir 'test' do |dir|
68
70
  id = Zold::Id.new
69
- copies = Zold::Copies.new(File.join(dir, 'copies'))
70
71
  remotes = Zold::Remotes.new(File.join(dir, 'remotes.csv'))
71
72
  remotes.clean
72
73
  stub_request(:get, "http://fake-1/wallet/#{id}").to_return(
@@ -77,7 +78,8 @@ class TestFetch < Minitest::Test
77
78
  }.to_json
78
79
  )
79
80
  remotes.add('fake-1', 80)
80
- Zold::Fetch.new(id: id, copies: copies, remotes: remotes).run
81
+ copies = Zold::Copies.new(File.join(dir, "copies/#{id}"))
82
+ Zold::Fetch.new(copies: copies.root, remotes: remotes).run([id.to_s])
81
83
  assert_equal(copies.all[0][:name], '1')
82
84
  assert_equal(copies.all[0][:score], 0)
83
85
  end
@@ -0,0 +1,46 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require_relative '../../lib/zold/wallets'
24
+ require_relative '../../lib/zold/amount'
25
+ require_relative '../../lib/zold/key'
26
+ require_relative '../../lib/zold/id'
27
+ require_relative '../../lib/zold/commands/invoice'
28
+
29
+ # INVOICE test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
32
+ # License:: MIT
33
+ class TestInvoice < Minitest::Test
34
+ def test_generates_invoice
35
+ Dir.mktmpdir 'test' do |dir|
36
+ id = Zold::Id.new
37
+ wallets = Zold::Wallets.new(dir)
38
+ source = wallets.find(id)
39
+ source.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
40
+ invoice = Zold::Invoice.new(wallets: wallets).run(
41
+ [id.to_s, '--length=16']
42
+ )
43
+ assert_equal(33, invoice.length)
44
+ end
45
+ end
46
+ end
@@ -20,10 +20,10 @@
20
20
 
21
21
  require 'minitest/autorun'
22
22
  require 'tmpdir'
23
- require_relative '../../lib/zold/wallet.rb'
24
- require_relative '../../lib/zold/key.rb'
25
- require_relative '../../lib/zold/id.rb'
26
- require_relative '../../lib/zold/commands/list.rb'
23
+ require_relative '../../lib/zold/wallet'
24
+ require_relative '../../lib/zold/key'
25
+ require_relative '../../lib/zold/id'
26
+ require_relative '../../lib/zold/commands/list'
27
27
 
28
28
  # LIST test.
29
29
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -23,14 +23,14 @@ require 'tmpdir'
23
23
  require 'json'
24
24
  require 'time'
25
25
  require 'webmock/minitest'
26
- require_relative '../../lib/zold/wallet.rb'
27
- require_relative '../../lib/zold/id.rb'
28
- require_relative '../../lib/zold/copies.rb'
29
- require_relative '../../lib/zold/key.rb'
30
- require_relative '../../lib/zold/score.rb'
31
- require_relative '../../lib/zold/patch.rb'
32
- require_relative '../../lib/zold/commands/merge.rb'
33
- require_relative '../../lib/zold/commands/pay.rb'
26
+ require_relative '../../lib/zold/wallet'
27
+ require_relative '../../lib/zold/id'
28
+ require_relative '../../lib/zold/copies'
29
+ require_relative '../../lib/zold/key'
30
+ require_relative '../../lib/zold/score'
31
+ require_relative '../../lib/zold/patch'
32
+ require_relative '../../lib/zold/commands/merge'
33
+ require_relative '../../lib/zold/commands/pay'
34
34
 
35
35
  # MERGE test.
36
36
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -48,15 +48,16 @@ class TestMerge < Minitest::Test
48
48
  second = Zold::Wallet.new(File.join(dir, 'copy-2'))
49
49
  File.write(second.path, File.read(wallet.path))
50
50
  Zold::Pay.new(
51
- payer: first,
52
- receiver: second,
53
- amount: Zold::Amount.new(zld: 14.95),
51
+ wallets: Zold::Wallets.new(dir),
54
52
  pvtkey: Zold::Key.new(file: 'fixtures/id_rsa')
55
- ).run(['--force'])
56
- copies = Zold::Copies.new(File.join(dir, 'copies'))
53
+ ).run([id.to_s, second.id.to_s, '14.95', '--force'])
54
+ copies = Zold::Copies.new(File.join(dir, "copies/#{id}"))
57
55
  copies.add(File.read(first.path), 'host-1', 80, 5)
58
56
  copies.add(File.read(second.path), 'host-2', 80, 5)
59
- Zold::Merge.new(wallet: wallet, copies: copies).run
57
+ Zold::Merge.new(
58
+ wallets: Zold::Wallets.new(dir),
59
+ copies: copies.root
60
+ ).run([id.to_s])
60
61
  end
61
62
  end
62
63
 
@@ -71,18 +72,16 @@ class TestMerge < Minitest::Test
71
72
  second = Zold::Wallet.new(File.join(dir, 'copy-2'))
72
73
  File.write(second.path, File.read(wallet.path))
73
74
  Zold::Pay.new(
74
- payer: first,
75
- receiver: second,
76
- amount: Zold::Amount.new(zld: 14.95),
75
+ wallets: Zold::Wallets.new(dir),
77
76
  pvtkey: Zold::Key.new(file: 'fixtures/id_rsa')
78
- ).run(['--force'])
79
- copies = Zold::Copies.new(File.join(dir, 'copies'))
77
+ ).run([id.to_s, second.id.to_s, '14.95', '--force'])
78
+ copies = Zold::Copies.new(File.join(dir, "copies/#{id}"))
80
79
  copies.add(File.read(first.path), 'host-1', 80, 5)
81
80
  copies.add(File.read(second.path), 'host-2', 80, 5)
82
81
  Zold::Merge.new(
83
- wallet: Zold::Wallet.new(File.join(dir, Zold::Id.new.to_s)),
84
- copies: copies
85
- ).run
82
+ wallets: Zold::Wallets.new(dir),
83
+ copies: copies.root
84
+ ).run([id.to_s])
86
85
  end
87
86
  end
88
87
  end
@@ -21,15 +21,15 @@
21
21
  require 'minitest/autorun'
22
22
  require 'tmpdir'
23
23
  require 'webmock/minitest'
24
- require_relative '../../lib/zold/wallet.rb'
25
- require_relative '../../lib/zold/remotes.rb'
26
- require_relative '../../lib/zold/id.rb'
27
- require_relative '../../lib/zold/copies.rb'
28
- require_relative '../../lib/zold/key.rb'
29
- require_relative '../../lib/zold/commands/node.rb'
30
- require_relative '../../lib/zold/commands/fetch.rb'
31
- require_relative '../../lib/zold/commands/push.rb'
32
- require_relative '../node/fake_node.rb'
24
+ require_relative '../../lib/zold/wallet'
25
+ require_relative '../../lib/zold/remotes'
26
+ require_relative '../../lib/zold/id'
27
+ require_relative '../../lib/zold/copies'
28
+ require_relative '../../lib/zold/key'
29
+ require_relative '../../lib/zold/commands/node'
30
+ require_relative '../../lib/zold/commands/fetch'
31
+ require_relative '../../lib/zold/commands/push'
32
+ require_relative '../node/fake_node'
33
33
 
34
34
  # NODE test.
35
35
  # Author:: Yegor Bugayenko (yegor256@gmail.com)