我使用Ruby库对一个值进行编码,然后对其进行解码。但是,对于编码和解码,我得到了不同的值:
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}"运行它时,解码的值实际上是431e51ced80a7685c93b,而不是输入的值。这似乎与编码无关。
图书馆出问题了吗?我正在使用这个库:https://github.com/cryptape/ruby-rlp
发布于 2022-05-19 16:26:45
是的,总是一样的,你可以来回走。
require "eth"
recipient = "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
# => "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
encoded = Eth::Rlp.encode "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
# => "\xA86ba381ce15b19c7e44b8603ad7e698059c09a39b"
decoded = Eth::Rlp.decode encoded
# => "6ba381ce15b19c7e44b8603ad7e698059c09a39b"
decoded === recipient
# => true如果您试图在Ruby中构建事务序列化程序,请查看eth gem。
https://stackoverflow.com/questions/43453172
复制相似问题