Class: Zold::Txn
- Inherits:
-
Object
- Object
- Zold::Txn
- Defined in:
- lib/zold/txn.rb
Overview
A single transaction
Constant Summary collapse
- RE_DETAILS =
Regular expression for details
'[a-zA-Z0-9 @\!\?\*_\-\.:,\']+'- RE_PREFIX =
Regular expression for prefix
'[a-zA-Z0-9]+'
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#bnf ⇒ Object
Returns the value of attribute bnf.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#sign ⇒ Object
Returns the value of attribute sign.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(id, date, amount, prefix, bnf, details) ⇒ Txn
constructor
A new instance of Txn.
- #inverse(bnf) ⇒ Object
-
#signed(pvt, id) ⇒ Object
- Sign the transaction and add RSA signature to it
pvt - The private RSA key of the paying wallet
id -
Paying wallet ID.
- The private RSA key of the paying wallet
- Sign the transaction and add RSA signature to it
- #to_s ⇒ Object
- #to_text ⇒ Object
Constructor Details
#initialize(id, date, amount, prefix, bnf, details) ⇒ Txn
Returns a new instance of Txn.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/zold/txn.rb', line 45 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}\" (#{Txn::RE_PREFIX})" unless prefix =~ Regexp.new("^#{Txn::RE_PREFIX}$") @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 "Wrong details \"#{details}\" (#{Txn::RE_DETAILS})" unless details =~ Regexp.new("^#{Txn::RE_DETAILS}$") @details = details end |
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
43 44 45 |
# File 'lib/zold/txn.rb', line 43 def amount @amount end |
#bnf ⇒ Object
Returns the value of attribute bnf.
43 44 45 |
# File 'lib/zold/txn.rb', line 43 def bnf @bnf end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
43 44 45 |
# File 'lib/zold/txn.rb', line 43 def date @date end |
#details ⇒ Object (readonly)
Returns the value of attribute details.
43 44 45 |
# File 'lib/zold/txn.rb', line 43 def details @details end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
43 44 45 |
# File 'lib/zold/txn.rb', line 43 def id @id end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
43 44 45 |
# File 'lib/zold/txn.rb', line 43 def prefix @prefix end |
#sign ⇒ Object
Returns the value of attribute sign.
43 44 45 |
# File 'lib/zold/txn.rb', line 43 def sign @sign end |
Class Method Details
.parse(line, idx = 0) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/zold/txn.rb', line 118 def self.parse(line, idx = 0) regex = Regexp.new( '^' + [ '(?<id>[0-9a-f]{4})', '(?<date>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)', '(?<amount>[0-9a-f]{16})', "(?<prefix>#{Txn::RE_PREFIX})", '(?<bnf>[0-9a-f]{16})', "(?<details>#{Txn::RE_DETAILS})", '(?<sign>[A-Za-z0-9+/]+={0,3})?' ].join(';') + '$' ) clean = line.strip parts = regex.match(clean) raise "Invalid line ##{idx}: #{line.inspect} #{regex}" unless parts txn = Txn.new( Hexnum.parse(parts[:id]).to_i, Time.parse(parts[:date]), Amount.new(coins: Hexnum.parse(parts[:amount]).to_i), parts[:prefix], Id.new(parts[:bnf]), parts[:details] ) txn.sign = parts[:sign] txn end |
Instance Method Details
#<=>(other) ⇒ Object
78 79 80 81 |
# File 'lib/zold/txn.rb', line 78 def <=>(other) raise 'Can only compare with Txn' unless other.is_a?(Txn) [date, amount * -1, id, bnf] <=> [other.date, other.amount * -1, other.id, other.bnf] end |
#==(other) ⇒ Object
72 73 74 75 76 |
# File 'lib/zold/txn.rb', line 72 def ==(other) id == other.id && date == other.date && amount == other.amount && prefix == other.prefix && bnf == other.bnf && details == other.details && sign == other.sign end |
#inverse(bnf) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/zold/txn.rb', line 100 def inverse(bnf) raise 'You can\'t reverse a positive transaction' unless amount.negative? t = clone t.amount = amount * -1 t.bnf = bnf t.sign = '' t end |
#signed(pvt, id) ⇒ Object
Sign the transaction and add RSA signature to it
pvt-
The private RSA key of the paying wallet
id-
Paying wallet ID
112 113 114 115 116 |
# File 'lib/zold/txn.rb', line 112 def signed(pvt, id) t = clone t.sign = Signature.new.sign(pvt, id, self) t end |
#to_s ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/zold/txn.rb', line 83 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_text ⇒ Object
95 96 97 98 |
# File 'lib/zold/txn.rb', line 95 def to_text start = @amount.negative? ? "##{@id}" : '-' "#{start} #{@date.utc.iso8601} #{@amount} #{@bnf} #{@details}" end |