Class: CryptoconditionsRuby::Types::Ed25519Fulfillment

Inherits:
Fulfillment
  • Object
show all
Defined in:
lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb

Constant Summary collapse

TYPE_ID =
4
FEATURE_BITMASK =
0x20
PUBKEY_LENGTH =
32
SIGNATURE_LENGTH =
64
FULFILLMENT_LENGTH =
PUBKEY_LENGTH + SIGNATURE_LENGTH

Constants inherited from Fulfillment

Fulfillment::REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Fulfillment

#bitmask, #condition, #condition_binary, #condition_uri, from_binary, from_dict, from_uri, #serialize_binary, #serialize_payload, #serialize_uri, #type_id

Methods included from Crypto::Helpers

#base64_add_padding, #base64_remove_padding, #ed25519_generate_key_pair

Constructor Details

#initialize(public_key = nil) ⇒ Ed25519Fulfillment

Returns a new instance of Ed25519Fulfillment.



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

def initialize(public_key = nil)
  if public_key
    public_key = Crypto::Ed25519VerifyingKey.new(public_key) if public_key.is_a?(String)
    raise TypeError unless public_key.is_a?(Crypto::Ed25519VerifyingKey)
  end
  @public_key = public_key
  @signature = nil
end

Instance Attribute Details

#public_keyObject

Returns the value of attribute public_key.



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

def public_key
  @public_key
end

#signature=(value) ⇒ Object

Sets the attribute signature

Parameters:

  • value

    the value to set the attribute signature to.



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

def signature=(value)
  @signature = value
end

Instance Method Details

#calculate_max_fulfillment_lengthObject



53
54
55
# File 'lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb', line 53

def calculate_max_fulfillment_length
  Ed25519Fulfillment::FULFILLMENT_LENGTH
end

#generate_hashObject

Raises:

  • (StandardError)


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

def generate_hash
  raise StandardError, 'Requires a public publicKey' unless public_key
  public_key.to_s
end

#parse_dict(data) ⇒ Object



67
68
69
70
# File 'lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb', line 67

def parse_dict(data)
  self.public_key = Crypto::Ed25519VerifyingKey.new(data['public_key'])
  self.signature = (Utils::Base58.decode(data['signature']) if data['signature'])
end

#parse_payload(reader, *_args) ⇒ Object



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

def parse_payload(reader, *_args)
  self.public_key = Crypto::Ed25519VerifyingKey.new(
    Utils::Base58.encode(reader.read_octet_string(Ed25519Fulfillment::PUBKEY_LENGTH))
  )
  self.signature = reader.read_octet_string(Ed25519Fulfillment::SIGNATURE_LENGTH)
end

#sign(message, private_key) ⇒ Object



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

def sign(message, private_key)
  sk = private_key
  vk = sk.get_verifying_key

  self.public_key = vk

  self.signature = sk.sign(message, 'bytes')
end

#to_dictObject



57
58
59
60
61
62
63
64
65
# File 'lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb', line 57

def to_dict
  {
    'type' => 'fulfillment',
    'type_id' => TYPE_ID,
    'bitmask' => bitmask,
    'public_key' => Utils::Base58.encode(public_key.to_s),
    'signature' => (Utils::Base58.encode(signature) if signature)
  }
end

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



72
73
74
75
76
# File 'lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb', line 72

def validate(message: nil, **_kwargs)
  return false unless message && signature

  public_key.verify(signature, message, 'bytes')
end

#write_common_header(writer) ⇒ Object



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

def write_common_header(writer)
  writer.write_var_octet_string(public_key)
end

#write_payload(writer) ⇒ Object



46
47
48
49
50
51
# File 'lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb', line 46

def write_payload(writer)
  writer.tap do |w|
    w.write_octet_string(public_key.to_s, Ed25519Fulfillment::PUBKEY_LENGTH)
    w.write_octet_string(signature, Ed25519Fulfillment::SIGNATURE_LENGTH)
  end
end