Class: SolanaRuby::Message

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

Constant Summary collapse

PUBKEY_LENGTH =
32

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header:, account_keys:, recent_blockhash:, instructions:) ⇒ Message

Returns a new instance of Message.



7
8
9
10
11
12
# File 'lib/solana_ruby/message.rb', line 7

def initialize(header:, account_keys:, recent_blockhash:, instructions:)
  @header = header
  @account_keys = 
  @recent_blockhash = recent_blockhash
  @instructions = instructions
end

Instance Attribute Details

#account_keysObject (readonly)

Returns the value of attribute account_keys.



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

def 
  @account_keys
end

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



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

def instructions
  @instructions
end

#recent_blockhashObject (readonly)

Returns the value of attribute recent_blockhash.



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

def recent_blockhash
  @recent_blockhash
end

Class Method Details

.from(bytes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/solana_ruby/message.rb', line 14

def self.from(bytes)
  bytes = bytes.dup
  num_required_signatures = bytes.shift
  num_readonly_signed_accounts = bytes.shift
  num_readonly_unsigned_accounts = bytes.shift
   = Utils.decode_length(bytes)

   = .times.map do
     = bytes.slice!(0, PUBKEY_LENGTH)
    Utils.bytes_to_base58()
  end

  recent_blockhash_bytes = bytes.slice!(0, PUBKEY_LENGTH)
  recent_blockhash = Utils.bytes_to_base58(recent_blockhash_bytes)

  instruction_count = Utils.decode_length(bytes)
  instructions = instruction_count.times.map do
    program_id_index = bytes.shift
     = Utils.decode_length(bytes)

    accounts = bytes.slice!(0, )

    data_length = Utils.decode_length(bytes)
    data_bytes = bytes.slice!(0, data_length)
    {program_id_index: program_id_index, accounts: accounts, data: data_bytes}
  end
  self.new({
    header: {
      num_required_signatures: num_required_signatures,
      num_readonly_signed_accounts:num_readonly_signed_accounts,
      num_readonly_unsigned_accounts:num_readonly_unsigned_accounts,
    },
    account_keys: ,
    recent_blockhash: recent_blockhash,
    instructions: instructions
  })
end

Instance Method Details

#is_account_writable(index) ⇒ Object



102
103
104
105
# File 'lib/solana_ruby/message.rb', line 102

def (index)
  index < header[:num_required_signatures] - header[:num_readonly_signed_accounts] ||
  (index >= header[:num_required_signatures] && index <  .length - header[:num_readonly_unsigned_accounts])
end

#serializeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/solana_ruby/message.rb', line 52

def serialize
  num_keys = .length
  key_count = Utils.encode_length(num_keys)

  layout = SolanaRuby::DataTypes::Layout.new({
    num_required_signatures: :blob1,
    num_readonly_signed_accounts: :blob1,
    num_readonly_unsigned_accounts: :blob1,
    key_count: SolanaRuby::DataTypes::Blob.new(key_count.length),
    keys: SolanaRuby::DataTypes::Sequence.new(num_keys, SolanaRuby::DataTypes::Blob.new(32)),
    recent_blockhash: SolanaRuby::DataTypes::Blob.new(32)
  })

  sign_data = layout.serialize({
    num_required_signatures: header[:num_required_signatures],
    num_readonly_signed_accounts: header[:num_readonly_signed_accounts],
    num_readonly_unsigned_accounts: header[:num_readonly_unsigned_accounts],
    key_count: key_count,
    keys: .map{|x| Utils.base58_to_bytes(x)},
    recent_blockhash: Utils.base58_to_bytes(recent_blockhash)
  })

  instruction_count = Utils.encode_length(@instructions.length)
  sign_data += instruction_count

  data = @instructions.map do |instruction|
    instruction_layout = SolanaRuby::DataTypes::Layout.new({
      program_id_index: :uint8,
      key_indices_count: SolanaRuby::DataTypes::Blob.new(key_count.length),
      key_indices: SolanaRuby::DataTypes::Sequence.new(num_keys, SolanaRuby::DataTypes::Blob.new(8)),
      data_length: SolanaRuby::DataTypes::Blob.new(key_count.length),
      data: SolanaRuby::DataTypes::Sequence.new(num_keys, SolanaRuby::DataTypes::UnsignedInt.new(8)),
    })

    key_indices_count = Utils.encode_length(instruction[:accounts].length)
    data_count = Utils.encode_length(instruction[:data].length)

    instruction_layout.serialize({
      program_id_index: instruction[:program_id_index],
      key_indices_count: key_indices_count,
      key_indices: instruction[:accounts],
      data_length: data_count,
      data: instruction[:data]
    })
  end.flatten

  sign_data += data
  sign_data
end