Class: Nem::Model::Message
- Inherits:
-
Object
- Object
- Nem::Model::Message
- Includes:
- Nem::Mixin::Assignable
- Defined in:
- lib/nem/model/message.rb
Constant Summary collapse
- TYPE_PLAIN =
1- TYPE_ENCRYPTED =
2
Instance Attribute Summary collapse
-
#private_key ⇒ String
The current value of private_key.
-
#public_key ⇒ String
The current value of public_key.
-
#type ⇒ Integer
The current value of type.
-
#value ⇒ String
The current value of value.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #bytesize ⇒ Integer
- #decrypt! ⇒ Object
- #encrypt! ⇒ Object
- #encrypted? ⇒ Boolean
- #hex? ⇒ Boolean
-
#initialize(value = '', type: :plain, private_key: nil, public_key: nil) ⇒ Message
constructor
A new instance of Message.
- #payload ⇒ String
- #plain? ⇒ Boolean
- #to_hash ⇒ Hash
- #to_s ⇒ String
- #valid? ⇒ Boolean
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_key ⇒ String
Returns the current value of private_key.
7 8 9 |
# File 'lib/nem/model/message.rb', line 7 def private_key @private_key end |
#public_key ⇒ String
Returns the current value of public_key.
7 8 9 |
# File 'lib/nem/model/message.rb', line 7 def public_key @public_key end |
#type ⇒ Integer
Returns the current value of type.
7 8 9 |
# File 'lib/nem/model/message.rb', line 7 def type @type end |
#value ⇒ String
Returns 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.(hash) new( hash[:payload], type: (hash[:type] == TYPE_ENCRYPTED) ? :encrypted : :plain ) end |
Instance Method Details
#==(other) ⇒ Boolean
80 81 82 |
# File 'lib/nem/model/message.rb', line 80 def ==(other) @value == other.value end |
#bytesize ⇒ Integer
55 56 57 |
# File 'lib/nem/model/message.rb', line 55 def bytesize payload.bytesize end |
#decrypt! ⇒ Object
37 38 39 40 41 42 |
# File 'lib/nem/model/message.rb', line 37 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 end |
#encrypt! ⇒ Object
30 31 32 33 34 35 |
# 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 end |
#encrypted? ⇒ Boolean
45 46 47 |
# File 'lib/nem/model/message.rb', line 45 def encrypted? @type == TYPE_ENCRYPTED end |
#hex? ⇒ Boolean
65 66 67 |
# File 'lib/nem/model/message.rb', line 65 def hex? !!(value =~ /\Afe\h+\Z/) end |
#payload ⇒ String
85 86 87 |
# File 'lib/nem/model/message.rb', line 85 def payload (hex? || encrypted?) ? value : value.unpack('H*').first end |
#plain? ⇒ Boolean
50 51 52 |
# File 'lib/nem/model/message.rb', line 50 def plain? @type == TYPE_PLAIN end |
#to_hash ⇒ Hash
70 71 72 |
# File 'lib/nem/model/message.rb', line 70 def to_hash { payload: payload, type: type } end |
#to_s ⇒ String
75 76 77 |
# File 'lib/nem/model/message.rb', line 75 def to_s @value.to_s end |
#valid? ⇒ Boolean
60 61 62 |
# File 'lib/nem/model/message.rb', line 60 def valid? bytesize <= 1024 end |