Class: Zold::Wallet
- Inherits:
-
Object
- Object
- Zold::Wallet
- Defined in:
- lib/zold/wallet.rb
Overview
A single wallet
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add(txn) ⇒ Object
- #balance ⇒ 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
- #network ⇒ Object
- #path ⇒ Object
- #root? ⇒ Boolean
- #sub(amount, invoice, pvt, details = '-') ⇒ Object
- #to_s ⇒ Object
- #txns ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(file) ⇒ Wallet
Returns a new instance of Wallet.
41 42 43 |
# File 'lib/zold/wallet.rb', line 41 def initialize(file) @file = file end |
Instance Method Details
#==(other) ⇒ Object
45 46 47 |
# File 'lib/zold/wallet.rb', line 45 def ==(other) to_s == other.to_s end |
#add(txn) ⇒ Object
109 110 111 112 |
# File 'lib/zold/wallet.rb', line 109 def add(txn) raise 'The txn has to be of type Txn' unless txn.is_a?(Txn) open(@file, 'a') { |f| f.print "#{txn}\n" } end |
#balance ⇒ Object
87 88 89 |
# File 'lib/zold/wallet.rb', line 87 def balance txns.inject(Amount::ZERO) { |sum, t| sum + t.amount } end |
#exists? ⇒ Boolean
65 66 67 |
# File 'lib/zold/wallet.rb', line 65 def exists? File.exist?(@file) end |
#has?(id, bnf) ⇒ Boolean
114 115 116 117 118 |
# File 'lib/zold/wallet.rb', line 114 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
124 125 126 127 128 |
# File 'lib/zold/wallet.rb', line 124 def income txns.each do |t| yield t unless t.amount.negative? end end |
#init(id, pubkey, overwrite: false, network: 'test') ⇒ Object
73 74 75 76 77 |
# File 'lib/zold/wallet.rb', line 73 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}/ File.write(@file, "#{network}\n#{VERSION}\n#{id}\n#{pubkey.to_pub}\n\n") end |
#key ⇒ Object
120 121 122 |
# File 'lib/zold/wallet.rb', line 120 def key Key.new(text: lines[3].strip) end |
#network ⇒ Object
53 54 55 56 57 |
# File 'lib/zold/wallet.rb', line 53 def network n = lines[0].strip raise "Invalid network name '#{n}'" unless n =~ /[a-z]{4,16}/ n end |
#path ⇒ Object
69 70 71 |
# File 'lib/zold/wallet.rb', line 69 def path @file end |
#sub(amount, invoice, pvt, details = '-') ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/zold/wallet.rb', line 91 def sub(amount, invoice, pvt, details = '-') 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('@') txn = Txn.new( max + 1, Time.now, amount * -1, prefix, Id.new(target), details ) txn = txn.signed(pvt, self) add(txn) txn end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/zold/wallet.rb', line 49 def to_s id.to_s end |
#txns ⇒ Object
130 131 132 133 134 135 |
# File 'lib/zold/wallet.rb', line 130 def txns lines.drop(5) .each_with_index .map { |line, i| Txn.parse(line, i + 6) } .sort_by(&:date) end |
#version ⇒ Object
59 60 61 62 63 |
# File 'lib/zold/wallet.rb', line 59 def version v = lines[1].strip raise "Invalid version name '#{v}'" unless v =~ /[0-9]+(\.[0-9]+){1,2}/ v end |