Class: SolanaRuby::Transaction

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

Constant Summary collapse

SIGNATURE_LENGTH =
64
PACKET_DATA_SIZE =
1280 - 40 - 8
DEFAULT_SIGNATURE =
Array.new(64, 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recent_blockhash: nil, signatures: [], instructions: [], fee_payer: nil) ⇒ Transaction

Returns a new instance of Transaction.



12
13
14
15
16
17
# File 'lib/solana_ruby/transaction.rb', line 12

def initialize(recent_blockhash: nil, signatures: [], instructions: [], fee_payer: nil)
  @recent_blockhash = recent_blockhash
  @signatures = signatures
  @instructions = instructions
  @fee_payer = fee_payer
end

Instance Attribute Details

#fee_payerObject

Returns the value of attribute fee_payer.



10
11
12
# File 'lib/solana_ruby/transaction.rb', line 10

def fee_payer
  @fee_payer
end

#instructionsObject

Returns the value of attribute instructions.



10
11
12
# File 'lib/solana_ruby/transaction.rb', line 10

def instructions
  @instructions
end

#messageObject

Returns the value of attribute message.



10
11
12
# File 'lib/solana_ruby/transaction.rb', line 10

def message
  @message
end

#recent_blockhashObject

Returns the value of attribute recent_blockhash.



10
11
12
# File 'lib/solana_ruby/transaction.rb', line 10

def recent_blockhash
  @recent_blockhash
end

#signaturesObject

Returns the value of attribute signatures.



10
11
12
# File 'lib/solana_ruby/transaction.rb', line 10

def signatures
  @signatures
end

Class Method Details

.from(base64_string) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/solana_ruby/transaction.rb', line 33

def self.from(base64_string)
  bytes = Base64.decode64(base64_string).bytes
  signature_count = Utils.decode_length(bytes)
  signatures = signature_count.times.map do
    signature_bytes = bytes.slice!(0, SIGNATURE_LENGTH)
    Utils.bytes_to_base58(signature_bytes)
  end
  msg = Message.from(bytes)
  self.populate(msg, signatures)
end

Instance Method Details

#add(item) ⇒ Object



71
72
73
# File 'lib/solana_ruby/transaction.rb', line 71

def add(item)
  instructions.push(item)
end

#add_instruction(instruction) ⇒ Object



19
20
21
# File 'lib/solana_ruby/transaction.rb', line 19

def add_instruction(instruction)
  @instructions << instruction
end

#serializeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/solana_ruby/transaction.rb', line 44

def serialize
  sign_data = serialize_message

  signature_count = Utils.encode_length(signatures.length)
  raise 'invalid length!' if signatures.length > 256
  
  wire_transaction = signature_count

  signatures.each do |signature|
    if signature
      signature_bytes = signature[:signature]
      raise 'signature is empty' unless (signature_bytes)
      raise 'signature has invalid length' unless (signature_bytes.length == 64)
      wire_transaction += signature_bytes
      raise "Transaction too large: #{wire_transaction.length} > #{PACKET_DATA_SIZE}" unless wire_transaction.length <= PACKET_DATA_SIZE
      wire_transaction
    end
  end

  wire_transaction += sign_data
  wire_transaction
end

#set_fee_payer(pubkey) ⇒ Object



23
24
25
26
# File 'lib/solana_ruby/transaction.rb', line 23

def set_fee_payer(pubkey)
  puts "Setting fee payer: #{pubkey.inspect}"  # Debugging output
  @fee_payer = pubkey  # Store as-is since Base58 gem can handle encoding/decoding
end

#set_recent_blockhash(blockhash) ⇒ Object



28
29
30
31
# File 'lib/solana_ruby/transaction.rb', line 28

def set_recent_blockhash(blockhash)
  # raise "Invalid Base58 blockhash" unless Base58.valid?(blockhash)
  @recent_blockhash = blockhash  # Store as-is for similar reasons
end

#sign(keypairs) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/solana_ruby/transaction.rb', line 75

def sign(keypairs)
  raise 'No signers' unless keypairs.any?

  keys = keypairs.uniq { |kp| kp[:public_key] }
  @signatures = keys.map do |key|
    {
      signature: nil,
      public_key: key[:public_key]
    }
  end

  message = compile_message
  partial_sign(message, keys)
  true
end

#to_base64Object



67
68
69
# File 'lib/solana_ruby/transaction.rb', line 67

def to_base64
  Base64.strict_encode64(serialize.pack('C*'))
end