Class: Zold::Txn

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

Overview

A single transaction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, date, amount, prefix, bnf, details) ⇒ Txn

Returns a new instance of Txn.



37
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
# File 'lib/zold/txn.rb', line 37

def initialize(id, date, amount, prefix, bnf, details)
  raise 'The ID can\'t be NIL' if id.nil?
  raise "ID of transaction can't be negative: #{id}" if id < 1
  @id = id
  raise 'The time can\'t be NIL' if date.nil?
  raise 'Time have to be of type Time' unless date.is_a?(Time)
  raise "Time can't be in the future: #{date}" if date > Time.now
  @date = date
  raise 'The amount can\'t be NIL' if amount.nil?
  raise 'The amount has to be of type Amount' unless amount.is_a?(Amount)
  raise 'The amount can\'t be zero' if amount.zero?
  @amount = amount
  raise 'The bnf can\'t be NIL' if bnf.nil?
  raise 'The bnf has to be of type Id' unless bnf.is_a?(Id)
  @bnf = bnf
  raise 'Prefix can\'t be NIL' if prefix.nil?
  raise "Prefix is too short: \"#{prefix}\"" if prefix.length < 8
  raise "Prefix is too long: \"#{prefix}\"" if prefix.length > 32
  raise "Prefix is wrong: \"#{prefix}\"" unless prefix =~ /^[a-zA-Z0-9]+$/
  @prefix = prefix
  raise 'Details can\'t be NIL' if details.nil?
  raise 'Details can\'t be empty' if details.empty?
  raise "Details are too long: \"#{details}\"" if details.length > 512
  raise "Details are wrong: \"#{details}\"" unless details =~ /^[a-zA-Z0-9 -\.,]+$/
  @details = details
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



35
36
37
# File 'lib/zold/txn.rb', line 35

def amount
  @amount
end

#bnfObject

Returns the value of attribute bnf.



35
36
37
# File 'lib/zold/txn.rb', line 35

def bnf
  @bnf
end

#dateObject (readonly)

Returns the value of attribute date.



35
36
37
# File 'lib/zold/txn.rb', line 35

def date
  @date
end

#detailsObject (readonly)

Returns the value of attribute details.



35
36
37
# File 'lib/zold/txn.rb', line 35

def details
  @details
end

#idObject (readonly)

Returns the value of attribute id.



35
36
37
# File 'lib/zold/txn.rb', line 35

def id
  @id
end

#prefixObject (readonly)

Returns the value of attribute prefix.



35
36
37
# File 'lib/zold/txn.rb', line 35

def prefix
  @prefix
end

#signObject

Returns the value of attribute sign.



35
36
37
# File 'lib/zold/txn.rb', line 35

def sign
  @sign
end

Class Method Details

.parse(line, idx = 0) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/zold/txn.rb', line 98

def self.parse(line, idx = 0)
  regex = Regexp.new(
    '^' + [
      '([0-9a-f]{4})',
      '([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)',
      '([0-9a-f]{16})',
      '([A-Za-z0-9]{8,32})',
      '([0-9a-f]{16})',
      '([a-zA-Z0-9 -\.,]{1,512})',
      '([A-Za-z0-9+/]+={0,3})?'
    ].join(';') + '$'
  )
  clean = line.strip
  raise "Invalid line ##{idx}: #{line.inspect}" unless regex.match(clean)
  parts = clean.split(';')
  txn = Txn.new(
    Hexnum.parse(parts[0]).to_i,
    Time.parse(parts[1]),
    Amount.new(coins: Hexnum.parse(parts[2]).to_i),
    parts[3],
    Id.new(parts[4]),
    parts[5]
  )
  txn.sign = parts[6]
  txn
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
# File 'lib/zold/txn.rb', line 64

def ==(other)
  id == other.id && bnf == other.bnf
end

#inverse(bnf) ⇒ Object



85
86
87
88
89
90
# File 'lib/zold/txn.rb', line 85

def inverse(bnf)
  t = clone
  t.amount = amount * -1
  t.bnf = bnf
  t
end

#signed(pvt, id) ⇒ Object



92
93
94
95
96
# File 'lib/zold/txn.rb', line 92

def signed(pvt, id)
  t = clone
  t.sign = Signature.new.sign(pvt, id, self)
  t
end

#to_sObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zold/txn.rb', line 68

def to_s
  [
    Hexnum.new(@id, 4).to_s,
    @date.utc.iso8601,
    Hexnum.new(@amount.to_i, 16),
    @prefix,
    @bnf,
    @details,
    @sign
  ].join(';')
end

#to_textObject



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

def to_text
  start = @amount.negative? ? "##{@id}" : '-'
  "#{start} #{@date.utc.iso8601} #{@amount} #{@bnf} #{@details}"
end