'Is the RLP protocol supposed to decode the same values that were encoded?
I'm using a Ruby RLP library to encode one value and then decode it. However, I'm getting different values for encoding and decoding:
require 'rlp'
class Transaction
include RLP::Sedes::Serializable
set_serializable_fields(
to: RLP::Sedes::Binary.fixed_length(20, allow_empty: true)
)
def initialize(*args)
fields = parse_field_args(args)
fields[:to] = [fields[:to]].pack('H*')
serializable_initialize(fields)
end
def encoded
RLP.encode(self)
end
def self.decode(data_in)
deserialize(RLP.decode(data_in))
end
end
recipient = "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
tx = Transaction.new(recipient)
puts "Should decode to #{recipient}"
puts "Actually decodes to #{Transaction.decode(tx.encoded).to.unpack('H*').first}"
Running it, the decoded value is actually 431e51ced80a7685c93b, instead of the inputed value. This doesn't even seem to be related to what was encoded.
Is the library at fault here? I'm using this library: https://github.com/cryptape/ruby-rlp
Solution 1:[1]
Yes, it is always the same and you can go back and forth.
require "eth"
recipient = "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
# => "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
encoded = Eth::Rlp.encode "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
# => "\xA86ba381ce15b19c7e44b8603ad7e698059c09a39b"
decoded = Eth::Rlp.decode encoded
# => "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
decoded === recipient
# => true
If you are trying to build a transaction serializer in Ruby, take a look at the eth gem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | q9f |
