Class: Zold::Wallet
- Inherits:
-
Object
- Object
- Zold::Wallet
- 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'
- EXTENSION =
The extension of the wallet files
'.z'
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add(txn) ⇒ Object
-
#age ⇒ Object
Age of wallet in hours.
- #balance ⇒ Object
- #digest ⇒ Object
- #exists? ⇒ Boolean
- #has?(id, bnf) ⇒ Boolean
- #id ⇒ Object
- #income ⇒ Object
- #init(id, pubkey, overwrite: false, network: 'test') ⇒ Object
-
#initialize(file) ⇒ Wallet
constructor
A new instance of Wallet.
- #key ⇒ Object
- #mtime ⇒ Object
- #network ⇒ Object
- #path ⇒ Object
- #prefix?(prefix) ⇒ Boolean
- #protocol ⇒ Object
- #refurbish ⇒ Object
- #root? ⇒ Boolean
- #sub(amount, invoice, pvt, details = '-', time: Time.now) ⇒ Object
- #to_s ⇒ Object
- #txns ⇒ Object
Constructor Details
Instance Method Details
#==(other) ⇒ Object
59 60 61 |
# File 'lib/zold/wallet.rb', line 59 def ==(other) to_s == other.to_s end |
#add(txn) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/zold/wallet.rb', line 126 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 "Wallet #{id} can't pay itself: #{txn}" if txn.bnf == 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 |
#age ⇒ Object
Age of wallet in hours
164 165 166 167 |
# File 'lib/zold/wallet.rb', line 164 def age list = txns list.empty? ? 0 : (Time.now - list.min_by(&:date).date) / (60 * 60) end |
#balance ⇒ Object
101 102 103 |
# File 'lib/zold/wallet.rb', line 101 def balance txns.inject(Amount::ZERO) { |sum, t| sum + t.amount } end |
#digest ⇒ Object
159 160 161 |
# File 'lib/zold/wallet.rb', line 159 def digest OpenSSL::Digest::SHA256.new(File.read(@file)).hexdigest end |
#exists? ⇒ Boolean
79 80 81 |
# File 'lib/zold/wallet.rb', line 79 def exists? File.exist?(@file) end |
#has?(id, bnf) ⇒ Boolean
135 136 137 138 139 |
# File 'lib/zold/wallet.rb', line 135 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 |
#income ⇒ Object
149 150 151 152 153 |
# File 'lib/zold/wallet.rb', line 149 def income txns.each do |t| yield t unless t.amount.negative? end end |
#init(id, pubkey, overwrite: false, network: 'test') ⇒ Object
87 88 89 90 91 |
# File 'lib/zold/wallet.rb', line 87 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}\n#{PROTOCOL}\n#{id}\n#{pubkey.to_pub}\n\n") end |
#key ⇒ Object
145 146 147 |
# File 'lib/zold/wallet.rb', line 145 def key Key.new(text: lines[3].strip) end |
#mtime ⇒ Object
155 156 157 |
# File 'lib/zold/wallet.rb', line 155 def mtime File.mtime(@file) end |
#network ⇒ Object
67 68 69 70 71 |
# File 'lib/zold/wallet.rb', line 67 def network n = lines[0].strip raise "Invalid network name '#{n}'" unless n =~ /^[a-z]{4,16}$/ n end |
#path ⇒ Object
83 84 85 |
# File 'lib/zold/wallet.rb', line 83 def path @file end |
#prefix?(prefix) ⇒ Boolean
141 142 143 |
# File 'lib/zold/wallet.rb', line 141 def prefix?(prefix) key.to_pub.include?(prefix) end |
#protocol ⇒ Object
73 74 75 76 77 |
# File 'lib/zold/wallet.rb', line 73 def protocol v = lines[1].strip raise "Invalid protocol version name '#{v}'" unless v =~ /^[0-9]+$/ v.to_i end |
#refurbish ⇒ Object
176 177 178 179 180 |
# File 'lib/zold/wallet.rb', line 176 def refurbish AtomicFile.new(@file).write( "#{network}\n#{protocol}\n#{id}\n#{key.to_pub}\n\n#{txns.map { |t| t.to_s + "\n" }.join}" ) end |
#sub(amount, invoice, pvt, details = '-', time: Time.now) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/zold/wallet.rb', line 105 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 right private key for this wallet' unless Signature.new.valid?(key, id, txn) add(txn) txn end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/zold/wallet.rb', line 63 def to_s id.to_s end |