~woutf


#3 Serializing an named tuple with numeric keys 3 months ago

Ticket created by ~woutf on ~arestifo/crystal-cbor

I'm implementing signature verification for a message signed by a Cardano wallet. The signature is the following:

sig = "845869a301270458205d9bd15d2b241d42ae767c653e4aaa2ea9e3b2d2da66f0e09d13b9691d12f0336761646472657373583901e4740b2690791de5ebad00ea555d8c6c31318ea980999eb7b13a24b955c29e1abea4593c52660b90b6fa20b3e5a42b105666522a3f14af29a166686173686564f44c48656c6c6f20576f726c64215840a0d0eefb44d92b70e5ac68b4c4f9467d090ca77e68d929301485baade8c32215530b2f4f3b59a28dc3331adead6c03672970270e78f478311c136d80cdb98e0d"

I'm deserializing it with the following data type:

alias Sig = Tuple(Bytes, NamedTuple(hashed: Bool), Bytes, Bytes)

This woks as expected:

data = Sig.from_cbor(sig.hexbytes)
puts CBOR::Diagnostic.to_s(data[0])
# => {1: -8, 4: h'5d9bd15d2b241d42ae767c653e4aaa2ea9e3b2d2da66f0e09d13b9691d12f033', "address": h'01e4740b2690791de5ebad00ea555d8c6c31318ea980999eb7b13a24b955c29e1abea4593c52660b90b6fa20b3e5a42b105666522a3f14af29'}
puts String.new(data[2])
# => Hello World!

The problem is with the headers (data[0]). I can't figure our how to deserialize them. I tried with the following object:

struct Headers
  include CBOR::Serializable

  @[CBOR::Field(key: "1")]
  @one : Int32
  @[CBOR::Field(key: "4")]
  @four : Bytes
  @address : Bytes
end

puts Headers.from_cbor(data[0])

But that raises an error on the keys:

Token::MapT -> Unexpected token CBOR::Token::IntT expected StringT or BytesT (CBOR::ParseError)
         from lib/cbor/src/cbor/serializable.cr:156:7 in 'initialize:__decoder_for_cbor_serializable'
...