Module: Solana::Ruby::Kit::Transactions
- Extended by:
- T::Sig
- Defined in:
- lib/solana/ruby/kit/transactions/compiler.rb,
lib/solana/ruby/kit/transactions/transaction.rb
Defined Under Namespace
Classes: FullySignedTransaction, Transaction
Constant Summary collapse
- TransactionMessageBytes =
The wire-encoded bytes of a compiled transaction message. Mirrors TypeScript’s
TransactionMessageBytes(branded Uint8Array). T.type_alias { String }
- SignaturesMap =
An ordered map of signer addresses to their Ed25519 signatures (or nil when the address has been reserved for signing but not yet signed). Mirrors TypeScript’s ‘SignaturesMap = OrderedMap<Address, SignatureBytes | null>`.
T.type_alias { T::Hash[String, T.nilable(String)] }
Class Method Summary collapse
- .assert_fully_signed_transaction!(transaction) ⇒ Object
- .compile_transaction_message(message) ⇒ Object
- .fully_signed_transaction?(transaction) ⇒ Boolean
- .get_signature_from_transaction(transaction) ⇒ Object
- .partially_sign_transaction(signing_keys, transaction) ⇒ Object
- .sign_transaction(signing_keys, transaction) ⇒ Object
- .wire_encode_transaction(transaction) ⇒ Object
Class Method Details
.assert_fully_signed_transaction!(transaction) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/solana/ruby/kit/transactions/transaction.rb', line 60 def assert_fully_signed_transaction!(transaction) missing = transaction.signatures.filter_map { |addr, sig| addr if sig.nil? } return if missing.empty? Kernel.raise SolanaError.new( :SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING, addresses: missing ) end |
.compile_transaction_message(message) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/solana/ruby/kit/transactions/compiler.rb', line 39 def () Kernel.raise SolanaError.new(:SOLANA_ERROR__TRANSACTION__FEE_PAYER_MISSING) if .fee_payer.nil? fee_payer = T.must(.fee_payer) constraint = .lifetime_constraint Kernel.raise SolanaError.new(:SOLANA_ERROR__TRANSACTION__EXPECTED_BLOCKHASH_LIFETIME) unless constraint.is_a?(TransactionMessages::BlockhashLifetimeConstraint) blockhash_str = constraint.blockhash # ── 1. Collect accounts and merge roles ──────────────────────────────── # Insertion-ordered hash: address_str → merged AccountRole integer. account_roles = T.let({}, T::Hash[String, Integer]) # Fee payer is always the first writable signer. account_roles[fee_payer.value] = Instructions::AccountRole::WRITABLE_SIGNER .instructions.each do |ix| # The instruction's program address is a readonly non-signer participant. prog = ix.program_address.value account_roles[prog] ||= Instructions::AccountRole::READONLY (ix.accounts || []).each do || addr = .address.value existing = account_roles[addr] || Instructions::AccountRole::READONLY account_roles[addr] = Instructions::AccountRole.merge(existing, .role) end end # ── 2. Partition into the four groups and sort within each ───────────── fp = fee_payer.value writable_signers = T.let([], T::Array[String]) readonly_signers = T.let([], T::Array[String]) writable_non_signers = T.let([], T::Array[String]) readonly_non_signers = T.let([], T::Array[String]) account_roles.each do |addr, role| next if addr == fp # fee payer is handled separately is_signer = Instructions::AccountRole.signer_role?(role) is_writable = Instructions::AccountRole.writable_role?(role) if is_signer && is_writable writable_signers << addr elsif is_signer readonly_signers << addr elsif is_writable writable_non_signers << addr else readonly_non_signers << addr end end writable_signers.sort! readonly_signers.sort! writable_non_signers.sort! readonly_non_signers.sort! # Fee payer first, then writable signers, readonly signers, non-signers. ordered = [fp] + writable_signers + readonly_signers + writable_non_signers + readonly_non_signers # ── 3. Build index lookup ────────────────────────────────────────────── account_index = T.let({}, T::Hash[String, Integer]) ordered.each_with_index { |addr, i| account_index[addr] = i } # ── 4. Message header (3 bytes) ──────────────────────────────────────── num_required_sigs = 1 + writable_signers.size + readonly_signers.size num_readonly_signed = readonly_signers.size num_readonly_unsigned = readonly_non_signers.size header = [num_required_sigs, num_readonly_signed, num_readonly_unsigned].pack('CCC').b # ── 5. Account addresses section ─────────────────────────────────────── accounts_section = encode_compact_u16(ordered.size) ordered.each do |addr_str| accounts_section = accounts_section + Addresses.decode_address(Addresses::Address.new(addr_str)) end # ── 6. Recent blockhash (32 bytes) ───────────────────────────────────── blockhash_bytes = Addresses.decode_address(Addresses::Address.new(blockhash_str)) # ── 7. Instructions section ──────────────────────────────────────────── ixs_section = encode_compact_u16(.instructions.size) .instructions.each do |ix| prog_idx = T.must(account_index[ix.program_address.value]) ix_accounts = ix.accounts || [] ix_indices = ix_accounts.map { |m| T.must(account_index[m.address.value]) } data_bytes = (ix.data || '').b ixs_section = ixs_section + [prog_idx].pack('C').b + encode_compact_u16(ix_indices.size) + ix_indices.pack('C*').b + encode_compact_u16(data_bytes.bytesize) + data_bytes end = (header + accounts_section + blockhash_bytes + ixs_section).b # ── 8. Signatures map (one nil slot per required signer) ─────────────── signer_addresses = [fp] + writable_signers + readonly_signers signatures = T.let({}, T::Hash[String, T.nilable(String)]) signer_addresses.each { |addr| signatures[addr] = nil } Transaction.new(message_bytes: , signatures: signatures) end |
.fully_signed_transaction?(transaction) ⇒ Boolean
53 54 55 |
# File 'lib/solana/ruby/kit/transactions/transaction.rb', line 53 def fully_signed_transaction?(transaction) transaction.signatures.values.all? { |sig| !sig.nil? } end |
.get_signature_from_transaction(transaction) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/solana/ruby/kit/transactions/transaction.rb', line 43 def get_signature_from_transaction(transaction) sig_bytes = transaction.signatures.values.first Kernel.raise SolanaError.new(:SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING) unless sig_bytes Keys.encode_signature(Keys::SignatureBytes.new(sig_bytes)) end |
.partially_sign_transaction(signing_keys, transaction) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/solana/ruby/kit/transactions/transaction.rb', line 82 def partially_sign_transaction(signing_keys, transaction) new_signatures = transaction.signatures.dup unexpected = T.let([], T::Array[String]) signing_keys.each do |signing_key| verify_key = signing_key.verify_key addr_str = Addresses.encode_address(verify_key.to_bytes) unless new_signatures.key?(addr_str) unexpected << addr_str next end sig_bytes = Keys.sign_bytes(signing_key, transaction.) existing = new_signatures[addr_str] next if existing && existing == sig_bytes.value new_signatures[addr_str] = sig_bytes.value end if unexpected.any? Kernel.raise SolanaError.new( :SOLANA_ERROR__TRANSACTION__ADDRESSES_CANNOT_SIGN_TRANSACTION, expected_addresses: transaction.signatures.keys, unexpected_addresses: unexpected ) end Transaction.new( message_bytes: transaction., signatures: new_signatures ) end |
.sign_transaction(signing_keys, transaction) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/solana/ruby/kit/transactions/transaction.rb', line 125 def sign_transaction(signing_keys, transaction) signed = partially_sign_transaction(signing_keys, transaction) assert_fully_signed_transaction!(signed) FullySignedTransaction.new( message_bytes: signed., signatures: signed.signatures ) end |
.wire_encode_transaction(transaction) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/solana/ruby/kit/transactions/compiler.rb', line 159 def wire_encode_transaction(transaction) sigs = transaction.signatures header = encode_compact_u16(sigs.size) sig_bytes = sigs.values.map { |s| (s || ("\x00" * 64)).b }.join (header + sig_bytes + transaction.).b end |