Module: Solana::Ruby::Kit::WalletStandard
- Extended by:
- T::Sig
- Defined in:
- lib/solana/ruby/kit/wallet_standard.rb
Overview
Server-side Ruby port of the @solana/wallet-standard signTransaction interface.
When a browser wallet (Phantom, Backpack, Solflare, …) calls
wallet.features['solana:signTransaction'].signTransaction(transaction)
the signed transaction is returned as serialised wire bytes. Rails receives those bytes (typically base64-encoded in a JSON request body) and uses this module to:
1. Decode the wire bytes back into a +Transactions::Transaction+ struct.
2. Verify every present Ed25519 signature against the message bytes.
3. Confirm that a specific wallet address (or all required addresses) signed.
4. Optionally broadcast the verified transaction via +Rpc+.
Solana addresses ARE Ed25519 public keys (32-byte base58), so signature verification requires no external key lookup — the verify key is derived directly from the signer’s address string.
Typical Rails controller usage:
wire_bytes = Base64.strict_decode64(params[:signed_transaction])
tx = Solana::Ruby::Kit::WalletStandard.verify_signed_transaction!(wire_bytes)
Solana::Ruby::Kit::Transactions.assert_fully_signed_transaction!(tx)
# tx is now safe to inspect or forward to the RPC node
Mirrors @solana/wallet-standard (anza-xyz/kit) and @wallet-standard/features.
Constant Summary collapse
- SIGN_TRANSACTION =
The wallet can sign transactions without broadcasting them.
T.let('solana:signTransaction', String)
- SIGN_AND_SEND_TRANSACTION =
The wallet can sign and broadcast in a single round-trip.
T.let('solana:signAndSendTransaction', String)
- SIGN_MESSAGE =
The wallet can sign arbitrary off-chain messages.
T.let('solana:signMessage', String)
- CONNECT =
Base Wallet Standard lifecycle features.
T.let('standard:connect', String)
- DISCONNECT =
T.let('standard:disconnect', String)
- EVENTS =
T.let('standard:events', String)
Class Method Summary collapse
- .decode_wire_transaction(wire_bytes) ⇒ Object
- .signed_by?(transaction, address) ⇒ Boolean
- .verify_signed_transaction!(wire_bytes) ⇒ Object
- .verify_transaction_signatures!(transaction) ⇒ Object
Class Method Details
.decode_wire_transaction(wire_bytes) ⇒ 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 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 146 147 148 149 150 151 152 153 154 |
# File 'lib/solana/ruby/kit/wallet_standard.rb', line 82 def decode_wire_transaction(wire_bytes) bytes = coerce_to_binary(wire_bytes) offset = 0 # 1. Signature count (compact-u16) sig_count, offset = decode_compact_u16(bytes, offset) if sig_count > 19 Kernel.raise SolanaError.new( SolanaError::WALLET_STANDARD__INVALID_WIRE_FORMAT, reason: "signature count #{sig_count} exceeds the maximum of 19" ) end # 2. Signature slots — 64 bytes each; all-zero means the slot is unfilled. zero64 = ("\x00" * 64).b raw_sigs = T.let([], T::Array[T.nilable(String)]) sig_count.times do slot = bytes[offset, 64] if slot.nil? || slot.bytesize != 64 Kernel.raise SolanaError.new( SolanaError::WALLET_STANDARD__INVALID_WIRE_FORMAT, reason: 'truncated signature slot' ) end raw_sigs << (slot.b == zero64 ? nil : slot.b) offset += 64 end # 3. Everything after the signatures section is the message. msg_slice = bytes[offset..] if msg_slice.nil? || msg_slice.empty? Kernel.raise SolanaError.new( SolanaError::WALLET_STANDARD__INVALID_WIRE_FORMAT, reason: 'message section is missing' ) end = T.must(msg_slice).b # 4. Determine version and locate the message header. # Legacy transactions: first byte = num_required_signatures (0..127) # Versioned (v0) trans.: first byte = 0x80 | version (≥ 128); skip it. msg_pos = 0 first_byte = .getbyte(msg_pos).to_i msg_pos += 1 if (first_byte & 0x80) != 0 num_required_sigs = .getbyte(msg_pos).to_i msg_pos += 1 msg_pos += 2 # skip num_readonly_signed + num_readonly_unsigned # 5. Parse the account list to recover signer addresses. num_accounts, msg_pos = decode_compact_u16(, msg_pos) if msg_pos + (num_accounts * 32) > .bytesize Kernel.raise SolanaError.new( SolanaError::WALLET_STANDARD__INVALID_WIRE_FORMAT, reason: 'account table truncated' ) end account_addrs = T.let([], T::Array[String]) num_accounts.times do account_addrs << Addresses.encode_address([msg_pos, 32].b) msg_pos += 32 end # 6. Build the signatures map. # The first +num_required_sigs+ accounts in the account table are the # signers; their raw_sigs slots are positionally aligned. signer_addrs = account_addrs[0, num_required_sigs] signatures = T.let({}, T::Hash[String, T.nilable(String)]) signer_addrs.each_with_index { |addr, i| signatures[addr] = raw_sigs[i] } Transactions::Transaction.new(message_bytes: , signatures: signatures) end |
.signed_by?(transaction, address) ⇒ Boolean
205 206 207 208 209 210 211 212 |
# File 'lib/solana/ruby/kit/wallet_standard.rb', line 205 def signed_by?(transaction, address) sig_raw = transaction.signatures[address.value] return false if sig_raw.nil? verify_key = RbNaCl::VerifyKey.new(Addresses.decode_address(address)) sig_bytes = Keys::SignatureBytes.new(sig_raw) Keys.verify_signature(verify_key, sig_bytes, transaction.) end |
.verify_signed_transaction!(wire_bytes) ⇒ Object
195 196 197 198 199 |
# File 'lib/solana/ruby/kit/wallet_standard.rb', line 195 def verify_signed_transaction!(wire_bytes) tx = decode_wire_transaction(wire_bytes) verify_transaction_signatures!(tx) tx end |
.verify_transaction_signatures!(transaction) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/solana/ruby/kit/wallet_standard.rb', line 169 def verify_transaction_signatures!(transaction) transaction.signatures.each do |addr_str, sig_raw| next if sig_raw.nil? verify_key = RbNaCl::VerifyKey.new(Addresses.decode_address(Addresses::Address.new(addr_str))) sig_bytes = Keys::SignatureBytes.new(sig_raw) unless Keys.verify_signature(verify_key, sig_bytes, transaction.) Kernel.raise SolanaError.new( SolanaError::WALLET_STANDARD__SIGNATURE_VERIFICATION_FAILED, address: addr_str ) end end end |