class Nem::Model::Message
@attr [String] value @attr [Integer] type @attr [String] public_key
@attr [String] private_key
Constants
- TYPE_ENCRYPTED
- TYPE_PLAIN
Attributes
private_key[RW]
public_key[RW]
type[R]
value[R]
Public Class Methods
new(value = '', type: :plain, private_key: nil, public_key: nil)
click to toggle source
# 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
new_from_message(hash)
click to toggle source
# 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
Public Instance Methods
==(other)
click to toggle source
@return [Boolean]
# File lib/nem/model/message.rb, line 82 def ==(other) @value == other.value end
bytesize()
click to toggle source
@return [Integer]
# File lib/nem/model/message.rb, line 57 def bytesize payload.bytesize end
decrypt!()
click to toggle source
# 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!()
click to toggle source
# 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?()
click to toggle source
@return [Boolean]
# File lib/nem/model/message.rb, line 47 def encrypted? @type == TYPE_ENCRYPTED end
hex?()
click to toggle source
@return [Boolean]
# File lib/nem/model/message.rb, line 67 def hex? !!(value =~ /\Afe\h+\Z/) end
payload()
click to toggle source
@return [String]
# File lib/nem/model/message.rb, line 87 def payload (hex? || encrypted?) ? value : value.unpack('H*').first end
plain?()
click to toggle source
@return [Boolean]
# File lib/nem/model/message.rb, line 52 def plain? @type == TYPE_PLAIN end
to_hash()
click to toggle source
@return [Hash]
# File lib/nem/model/message.rb, line 72 def to_hash { payload: payload, type: type } end
to_s()
click to toggle source
@return [String]
# File lib/nem/model/message.rb, line 77 def to_s @value.to_s end
valid?()
click to toggle source
@return [Boolean]
# File lib/nem/model/message.rb, line 62 def valid? bytesize <= 1024 end
Private Instance Methods
fix_private_key(key)
click to toggle source
# File lib/nem/model/message.rb, line 93 def fix_private_key(key) "#{'0' * 64}#{key.sub(/^00/i, '')}"[-64, 64] end