Class: Nem::Model::Message

Inherits:
Object
  • Object
show all
Includes:
Nem::Mixin::Assignable
Defined in:
lib/nem/model/message.rb

Constant Summary collapse

TYPE_PLAIN =
1
TYPE_ENCRYPTED =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = '', type: :plain, private_key: nil, public_key: nil) ⇒ Message

Returns a new instance of Message.



23
24
25
26
27
28
# File 'lib/nem/model/message.rb', line 23

def initialize(value = '', type: :plain, private_key: nil, public_key: nil)
  @value = value
  @type = (type == :encrypted) ? TYPE_ENCRYPTED : TYPE_PLAIN
  @private_key = private_key
  @public_key = public_key
end

Instance Attribute Details

#private_keyString

Returns the current value of private_key.

Returns:

  • (String)

    the current value of private_key



7
8
9
# File 'lib/nem/model/message.rb', line 7

def private_key
  @private_key
end

#public_keyString

Returns the current value of public_key.

Returns:

  • (String)

    the current value of public_key



7
8
9
# File 'lib/nem/model/message.rb', line 7

def public_key
  @public_key
end

#typeInteger

Returns the current value of type.

Returns:

  • (Integer)

    the current value of type



7
8
9
# File 'lib/nem/model/message.rb', line 7

def type
  @type
end

#valueString

Returns the current value of value.

Returns:

  • (String)

    the current value of value



7
8
9
# File 'lib/nem/model/message.rb', line 7

def value
  @value
end

Class Method Details

.new_from_message(hash) ⇒ Object



16
17
18
19
20
21
# File 'lib/nem/model/message.rb', line 16

def self.new_from_message(hash)
  new(
    hash[:payload],
    type: (hash[:type] == TYPE_ENCRYPTED) ? :encrypted : :plain
  )
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/nem/model/message.rb', line 82

def ==(other)
  @value == other.value
end

#bytesizeInteger

Returns:

  • (Integer)


57
58
59
# File 'lib/nem/model/message.rb', line 57

def bytesize
  payload.bytesize
end

#decrypt!Object



38
39
40
41
42
43
44
# File 'lib/nem/model/message.rb', line 38

def decrypt!
  bin_sk = fix_private_key(@private_key).scan(/../).map(&:hex).reverse.pack('C*')
  bin_pk = (public_key || @public_key).scan(/../).map(&:hex).pack('C*')
  @value = Nem::Util::Ed25519.decrypt(bin_sk, bin_pk, payload)
  @type = TYPE_PLAIN
  self
end

#encrypt!Object



30
31
32
33
34
35
36
# File 'lib/nem/model/message.rb', line 30

def encrypt!
  bin_sk = fix_private_key(@private_key).scan(/../).map(&:hex).reverse.pack('C*')
  bin_pk = (public_key || @public_key).scan(/../).map(&:hex).pack('C*')
  @value = Nem::Util::Ed25519.encrypt(bin_sk, bin_pk, value)
  @type = TYPE_ENCRYPTED
  self
end

#encrypted?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/nem/model/message.rb', line 47

def encrypted?
  @type == TYPE_ENCRYPTED
end

#hex?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/nem/model/message.rb', line 67

def hex?
  !!(value =~ /\Afe\h+\Z/)
end

#payloadString

Returns:

  • (String)


87
88
89
# File 'lib/nem/model/message.rb', line 87

def payload
  (hex? || encrypted?) ? value : value.unpack('H*').first
end

#plain?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/nem/model/message.rb', line 52

def plain?
  @type == TYPE_PLAIN
end

#to_hashHash

Returns:

  • (Hash)


72
73
74
# File 'lib/nem/model/message.rb', line 72

def to_hash
  { payload: payload, type: type }
end

#to_sString

Returns:

  • (String)


77
78
79
# File 'lib/nem/model/message.rb', line 77

def to_s
  @value.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/nem/model/message.rb', line 62

def valid?
  bytesize <= 1024
end