Class: Exonum::MessageT

Inherits:
Object
  • Object
show all
Defined in:
lib/exonum/types/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol_version, message_id, service_id, body) ⇒ MessageT

Returns a new instance of MessageT.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/exonum/types/message.rb', line 10

def initialize protocol_version, message_id, service_id, body
  self.protocol_version = protocol_version
  self.message_id = message_id
  self.service_id = service_id
  self.head = StructT.new([
    { name: 'network_id', type: UInt8T },
    { name: 'protocol_version', type: UInt8T },
    { name: 'message_id', type: UInt16T },
    { name: 'service_id', type: UInt16T },
    { name: 'payload', type: UInt32T }
  ])
  self.body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/exonum/types/message.rb', line 7

def body
  @body
end

#headObject

Returns the value of attribute head.



6
7
8
# File 'lib/exonum/types/message.rb', line 6

def head
  @head
end

#message_idObject

Returns the value of attribute message_id.



4
5
6
# File 'lib/exonum/types/message.rb', line 4

def message_id
  @message_id
end

#protocol_versionObject

Returns the value of attribute protocol_version.



3
4
5
# File 'lib/exonum/types/message.rb', line 3

def protocol_version
  @protocol_version
end

#service_idObject

Returns the value of attribute service_id.



5
6
7
# File 'lib/exonum/types/message.rb', line 5

def service_id
  @service_id
end

#signatureObject

Returns the value of attribute signature.



8
9
10
# File 'lib/exonum/types/message.rb', line 8

def signature
  @signature
end

Instance Method Details

#serialize(data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/exonum/types/message.rb', line 24

def serialize data
  buffer = SparseArray.new
  head_data = {
    network_id: 0,
    protocol_version: self.protocol_version,
    message_id: self.message_id,
    service_id: self.service_id,
    payload: 0 # placeholder, real value will be inserted later
  }
  head.serialize head_data, buffer, 0
  body.serialize data, buffer, head.size, 0, true
  UInt32T.serialize buffer.length + SignatureT.size, buffer, 6
  buffer.serialize
end

#sign(secret, data) ⇒ Object



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

def sign secret, data
  raise "Expecting 64 bytes key in hex" unless secret.is_a?(String) and secret.length == 128
  key = Ed25519::SigningKey.new [secret[0..63]].pack 'H*'
  buffer = serialize(data).pack('c*')
  key.sign(buffer).unpack('H*').first
end