zold 0.0.6 → 0.0.7

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: e31ddf85f51ef24b3ca8bb69bab3142e78499d01
4
- data.tar.gz: 2ad719e700e8a691bd56a9fad9558baaafcfe49f
3
+ metadata.gz: 7a7e5d899910c588ba9fff4987a7341ecb245dad
4
+ data.tar.gz: d1c940ba9d95d8d57e588fb1a6675f9117e86a4b
5
5
  SHA512:
6
- metadata.gz: 0fef444d304c65a3af45b222fe6def9c74601e622a31e6dd772484d0ce1b1748068c84fc420448157186b32635000b79f0ea87f99735b79a78f6f09192d7baf1
7
- data.tar.gz: 01e70165f45a89d4aa336410374d15d8da8ad560152f23d3dc8907547669cae09fe25928933d7197ada4fadb9596f47706e3486063e0e42131265d6a176e03f6
6
+ metadata.gz: ca614449c84d77fe0b5bce135e33168955bf4f71ee62172a6d01e88cb72890ee04e0128ec86f8f98590d943c5eb325f0db1daa26f2d795002f04b31aae7e7600
7
+ data.tar.gz: 12cbff8839076e9314eee7805f3c75d09f3f22ed64516a1ad964d2018ef637cc0ebd249495eca8b86c48530d031704d3d8f299c03b201e91496371e9c2e7ef7d
@@ -36,23 +36,19 @@ module Zold
36
36
  end
37
37
 
38
38
  def run
39
- clean = true
40
39
  @wallet.income do |t|
41
40
  bnf = Pull.new(
42
41
  wallet: @wallets.find(Id.new(t[:beneficiary])),
43
42
  log: @log
44
43
  ).run
45
- clean = bnf.check(t[:id], t[:amount], @wallet.id)
46
- next if clean
47
- @log.error("Txn ##{t[:id]} for #{t[:amount]} is absent at #{bnf.id}")
48
- break
44
+ bnf.check(t[:id], t[:amount], @wallet.id)
49
45
  end
50
- if clean
51
- @log.info("The #{@wallet} is clean")
52
- else
53
- @log.error("The #{@wallet} is compromised")
46
+ balance = @wallet.balance
47
+ if balance.negative? && !@wallet.root?
48
+ raise "Negative balance of #{@wallet} is not allowed: #{balance}"
54
49
  end
55
- clean
50
+ @log.info("The #{@wallet} is clean")
51
+ true
56
52
  end
57
53
  end
58
54
  end
data/lib/zold/id.rb CHANGED
@@ -29,6 +29,12 @@ module Zold
29
29
  @id = id.nil? ? rand(2**32..2**64 - 1) : Integer("0x#{id}", 16)
30
30
  end
31
31
 
32
+ ROOT = Id.new('0000000000000000')
33
+
34
+ def==(other)
35
+ to_s == other.to_s
36
+ end
37
+
32
38
  def to_s
33
39
  format('%016x', @id)
34
40
  end
data/lib/zold/version.rb CHANGED
@@ -23,5 +23,5 @@
23
23
  # Copyright:: Copyright (c) 2018 Zerocracy, Inc.
24
24
  # License:: MIT
25
25
  module Zold
26
- VERSION = '0.0.6'.freeze
26
+ VERSION = '0.0.7'.freeze
27
27
  end
data/lib/zold/wallet.rb CHANGED
@@ -72,8 +72,12 @@ module Zold
72
72
  ver
73
73
  end
74
74
 
75
+ def root?
76
+ id == Id::ROOT
77
+ end
78
+
75
79
  def id
76
- load.xpath('/wallet/id/text()').to_s
80
+ Id.new(load.xpath('/wallet/id/text()').to_s)
77
81
  end
78
82
 
79
83
  def balance
@@ -114,13 +118,18 @@ module Zold
114
118
  def check(id, amount, beneficiary)
115
119
  xml = load
116
120
  txn = xml.xpath("/wallet/ledger/txn[@id='#{id}']")[0]
117
- Amount.new(coins: txn.xpath('amount/text()')[0].to_s.to_i).mul(-1) ==
118
- amount &&
119
- txn.xpath('beneficiary/text()')[0].to_s == beneficiary &&
120
- Key.new(text: xml.xpath('/wallet/pkey/text()')[0].to_s).verify(
121
- txn.xpath('sign/text()')[0].to_s,
122
- "#{id} #{amount.to_i} #{beneficiary}"
123
- )
121
+ xamount = Amount.new(
122
+ coins: txn.xpath('amount/text()')[0].to_s.to_i
123
+ ).mul(-1)
124
+ raise "#{xamount} != #{amount}" if xamount != amount
125
+ xbeneficiary = Id.new(txn.xpath('beneficiary/text()')[0].to_s)
126
+ raise "#{xbeneficiary} != #{beneficiary}" if xbeneficiary != beneficiary
127
+ data = "#{id} #{amount.to_i} #{beneficiary}"
128
+ valid = Key.new(text: xml.xpath('/wallet/pkey/text()')[0].to_s).verify(
129
+ txn.xpath('sign/text()')[0].to_s, data
130
+ )
131
+ raise "Signature is not confirming this data: '#{data}'" unless valid
132
+ true
124
133
  end
125
134
 
126
135
  def income
@@ -51,7 +51,7 @@ class FrontTest < Minitest::Test
51
51
 
52
52
  def test_pushes_a_wallet
53
53
  Dir.mktmpdir 'test' do |dir|
54
- id = Zold::Id.new
54
+ id = Zold::Id::ROOT
55
55
  file = File.join(dir, "#{id}.xml")
56
56
  wallet = Zold::Wallet.new(file)
57
57
  wallet.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
data/test/test_id.rb CHANGED
@@ -47,4 +47,8 @@ class TestId < Minitest::Test
47
47
  id = Zold::Id.new(hex)
48
48
  assert(id.to_s == hex, "#{id} is not equal to #{hex}")
49
49
  end
50
+
51
+ def test_compares_two_ids
52
+ assert Zold::Id.new(Zold::Id::ROOT.to_s) == Zold::Id.new('0000000000000000')
53
+ end
50
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko