Class: Zold::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/patch.rb

Overview

A patch

Instance Method Summary collapse

Instance Method Details

#join(wallet) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zold/patch.rb', line 38

def join(wallet)
  if @id.nil?
    @id = wallet.id
    @key = wallet.key
    @txns = wallet.txns
    @network = wallet.network
  end
  if wallet.network != @network
    raise "The wallet is from a different network '#{wallet.network}', ours is '#{@network}'"
  end
  raise 'Public key mismatch' if wallet.key != @key
  raise "Wallet ID mismatch: #{@id} != #{wallet.id}" if wallet.id != @id
  negative = @txns.select { |t| t.amount.negative? }
  max = negative.empty? ? 0 : negative.max_by(&:id).id
  wallet.txns.each do |txn|
    next if @txns.find { |t| t == txn }
    next if
      txn.amount.negative? && !@txns.empty? &&
      (txn.id <= max ||
      @txns.find { |t| t.id == txn.id } ||
      @txns.map(&:amount).inject(&:+) < txn.amount)
    if !txn.amount.negative? && !txn.sign.empty?
      raise "RSA signature is redundant at ##{txn.id} of #{wallet.id}: #{txn.to_text}"
    end
    if txn.amount.negative? && !Signature.new.valid?(@key, wallet.id, txn)
      raise "Invalid RSA signature at transaction ##{txn.id} of #{wallet.id}: #{txn.to_text}"
    end
    @txns << txn
  end
end

#save(file, overwrite: false) ⇒ Object

Returns TRUE if the file was actually modified



70
71
72
73
74
75
76
77
78
79
# File 'lib/zold/patch.rb', line 70

def save(file, overwrite: false)
  raise 'You have to join at least one wallet in' if @id.nil?
  before = ''
  before = AtomicFile.new(file).read if File.exist?(file)
  wallet = Wallet.new(file)
  wallet.init(@id, @key, overwrite: overwrite, network: @network)
  @txns.each { |t| wallet.add(t) }
  after = AtomicFile.new(file).read
  before != after
end

#to_sObject



33
34
35
36
# File 'lib/zold/patch.rb', line 33

def to_s
  return 'empty' if @id.nil?
  "#{@txns.count} txns"
end