Class: CryptoconditionsRuby::Types::TimeoutFulfillment

Inherits:
PreimageSha256Fulfillment show all
Defined in:
lib/cryptoconditions_ruby/types/timeout_fulfillment.rb

Constant Summary collapse

TYPE_ID =
99
FEATURE_BITMASK =
0x09
REGEX =
TIMESTAMP_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PreimageSha256Fulfillment

#bitmask, #parse_payload, #write_hash_payload, #write_payload

Methods inherited from BaseSha256Fulfillment

#generate_hash, #write_hash_payload

Methods inherited from Fulfillment

#bitmask, #calculate_max_fulfillment_length, #condition, #condition_binary, #condition_uri, from_binary, from_dict, from_uri, #generate_hash, #parse_payload, #serialize_binary, #serialize_payload, #serialize_uri, #type_id, #write_payload

Methods included from Crypto::Helpers

#base64_add_padding, #base64_remove_padding, #ed25519_generate_key_pair

Constructor Details

#initialize(expire_time = nil) ⇒ TimeoutFulfillment

Returns a new instance of TimeoutFulfillment.



14
15
16
17
18
19
# File 'lib/cryptoconditions_ruby/types/timeout_fulfillment.rb', line 14

def initialize(expire_time = nil)
  if expire_time.is_a?(String) && !expire_time.match(REGEX)
    raise TypeError, "Expire time must be conform UTC unix time, was: #{expire_time}"
  end
  super if expire_time
end

Class Method Details

.timestamp(time) ⇒ Object



10
11
12
# File 'lib/cryptoconditions_ruby/types/timeout_fulfillment.rb', line 10

def self.timestamp(time)
  format('%6f', time.to_f)
end

Instance Method Details

#expire_timeObject



21
22
23
# File 'lib/cryptoconditions_ruby/types/timeout_fulfillment.rb', line 21

def expire_time
  preimage
end

#parse_dict(data) ⇒ Object



34
35
36
# File 'lib/cryptoconditions_ruby/types/timeout_fulfillment.rb', line 34

def parse_dict(data)
  self.preimage = data['expire_time']
end

#to_dictObject



25
26
27
28
29
30
31
32
# File 'lib/cryptoconditions_ruby/types/timeout_fulfillment.rb', line 25

def to_dict
  {
    'type' => 'fulfillment',
    'type_id' => TYPE_ID,
    'bitmask' => bitmask,
    'expire_time' => expire_time
  }
end

#validate(message: nil, now: nil, **_kwargs) ⇒ Object



38
39
40
41
42
43
# File 'lib/cryptoconditions_ruby/types/timeout_fulfillment.rb', line 38

def validate(message: nil, now: nil, **_kwargs)
  unless now || now.match(REGEX)
    raise TypeError, "message must be of unix time format, was: #{message}"
  end
  now.to_f <= expire_time.to_f
end