Class: Zold::Wallet

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

Overview

A single wallet

Constant Summary collapse

MAIN_NETWORK =

The name of the main production network. All other networks must have different names.

'zold'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Wallet

Returns a new instance of Wallet.



47
48
49
# File 'lib/zold/wallet.rb', line 47

def initialize(file)
  @file = file
end

Instance Method Details

#==(other) ⇒ Object



51
52
53
# File 'lib/zold/wallet.rb', line 51

def ==(other)
  to_s == other.to_s
end

#add(txn) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/zold/wallet.rb', line 118

def add(txn)
  raise 'The txn has to be of type Txn' unless txn.is_a?(Txn)
  dup = txns.find { |t| t.bnf == txn.bnf && t.id == txn.id }
  raise "The transaction with the same ID and BNF already exists: #{dup}" unless dup.nil?
  raise "The tax payment already exists: #{txn}" if Tax.new(self).exists?(txn)
  File.open(@file, 'a') { |f| f.print "#{txn}\n" }
end

#ageObject

Age of wallet in hours



143
144
145
146
# File 'lib/zold/wallet.rb', line 143

def age
  list = txns
  list.empty? ? 0 : (Time.now - list.min_by(&:date).date) / 60
end

#balanceObject



93
94
95
# File 'lib/zold/wallet.rb', line 93

def balance
  txns.inject(Amount::ZERO) { |sum, t| sum + t.amount }
end

#exists?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/zold/wallet.rb', line 71

def exists?
  File.exist?(@file)
end

#has?(id, bnf) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/zold/wallet.rb', line 126

def has?(id, bnf)
  raise 'The txn ID has to be of type Integer' unless id.is_a?(Integer)
  raise 'The bnf has to be of type Id' unless bnf.is_a?(Id)
  !txns.find { |t| t.id == id && t.bnf == bnf }.nil?
end

#idObject



89
90
91
# File 'lib/zold/wallet.rb', line 89

def id
  Id.new(lines[2].strip)
end

#incomeObject



136
137
138
139
140
# File 'lib/zold/wallet.rb', line 136

def income
  txns.each do |t|
    yield t unless t.amount.negative?
  end
end

#init(id, pubkey, overwrite: false, network: 'test') ⇒ Object



79
80
81
82
83
# File 'lib/zold/wallet.rb', line 79

def init(id, pubkey, overwrite: false, network: 'test')
  raise "File '#{@file}' already exists" if File.exist?(@file) && !overwrite
  raise "Invalid network name '#{network}'" unless network =~ /^[a-z]{4,16}$/
  AtomicFile.new(@file).write("#{network}\n1\n#{id}\n#{pubkey.to_pub}\n\n")
end

#keyObject



132
133
134
# File 'lib/zold/wallet.rb', line 132

def key
  Key.new(text: lines[3].strip)
end

#networkObject



59
60
61
62
63
# File 'lib/zold/wallet.rb', line 59

def network
  n = lines[0].strip
  raise "Invalid network name '#{n}'" unless n =~ /^[a-z]{4,16}$/
  n
end

#pathObject



75
76
77
# File 'lib/zold/wallet.rb', line 75

def path
  @file
end

#root?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/zold/wallet.rb', line 85

def root?
  id == Id::ROOT
end

#sub(amount, invoice, pvt, details = '-', time: Time.now) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/zold/wallet.rb', line 97

def sub(amount, invoice, pvt, details = '-', time: Time.now)
  raise 'The amount has to be of type Amount' unless amount.is_a?(Amount)
  raise "The amount can't be negative: #{amount}" if amount.negative?
  raise 'The pvt has to be of type Key' unless pvt.is_a?(Key)
  prefix, target = invoice.split('@')
  tid = max + 1
  raise 'Too many transactions already, can\'t add more' if max > 0xffff
  txn = Txn.new(
    tid,
    time,
    amount * -1,
    prefix,
    Id.new(target),
    details
  )
  txn = txn.signed(pvt, id)
  raise 'This is not the private right key for this wallet' unless Signature.new.valid?(key, id, txn)
  add(txn)
  txn
end

#to_sObject



55
56
57
# File 'lib/zold/wallet.rb', line 55

def to_s
  id.to_s
end

#txnsObject



148
149
150
151
152
153
# File 'lib/zold/wallet.rb', line 148

def txns
  lines.drop(5)
    .each_with_index
    .map { |line, i| Txn.parse(line, i + 6) }
    .sort_by { |t| [t.date, t.amount * -1] }
end

#versionObject



65
66
67
68
69
# File 'lib/zold/wallet.rb', line 65

def version
  v = lines[1].strip
  raise "Invalid version name '#{v}'" unless v =~ /^[0-9]+$/
  v
end