Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
235 stars 125 forks source link

failed to parse Vkeywitness because its structure changed in babbage era #554

Closed hoangong closed 1 year ago

hoangong commented 1 year ago

This happens since version 1.35.4 where the default tx build is babbage-era

This is Vkeywitness from alonzo era:

./cardano-cli text-view decode-cbor --in-file txw.signed

82  # list(2)
   00  # int(0)
   82  # list(2)
      58 20 ad 19 59 d6 0a 5d 83 bf ff 8f d8 5a dc a4
      6a 8e 70 94 cb 57 d9 44 c1 20 4f 56 56 a9 0f 79
      a9 12  # bytes(32)
      58 40 d6 b9 ac 77 24 4c 78 b7 83 ce a7 f5 a6 a3
      2d d3 93 22 52 de e9 86 6a 8f 2f ea 8c 18 bb 0a
      89 09 43 e4 80 0f 57 40 73 a4 ea 80 21 65 47 b0
      53 f4 dd eb 68 6a bd 78 12 3a 6e ee e4 9e 36 55
      70 01  # bytes(64)

and this is from babbage era

./cardano-cli text-view decode-cbor --in-file txw.signed

82  # list(2)
   58 20 c0 c9 2e de cf d4 93 b3 6e 02 53 fe 8a da
   5f 3a 2a 05 67 31 8d 7f 9d c7 9c 40 0f 53 86 6e
   eb 94  # bytes(32)
   58 40 90 f4 b2 38 ff e5 10 19 9e 42 1e af 03 57
   5c a8 2a 03 b5 5f 8b 1f b8 e3 3f 80 b3 70 3e 51
   f3 8c b0 b7 f8 a6 68 d7 9c 32 8a d2 f9 8e 65 60
   0e f6 6b 4b 70 e1 66 40 24 75 07 67 3d 0d 5d 34
   a2 02  # bytes(64)

I could manually add two bytes 8200 prefix to the babbage era version to make it works. Is it something serializer should take into account?

lisicky commented 1 year ago

Hi @hoangong ! Could you provide more information about what you do and what you expect ? I looked at alonzo cddl and babbage cddl and didn't see any difference for vkeywitness

hoangong commented 1 year ago

Hi @lisicky From what I see, cardano-cli 1.35.4 generated tx witness differently (no 8200 prefix).

You can use these command to generate txw.signed file and compare the output.

./cardano-cli transaction build-raw \
--tx-in ... \
--tx-out ....\
--fee 300000 \
--out-file tx.raw \
--certificate-file pool-registration.cert \
--certificate-file stake.cert \
--certificate-file delegation.cert

./cardano-cli transaction witness \
--tx-body-file tx.raw \
--signing-key-file cold.skey \
--testnet-magic 1 \
--out-file txw.signed

The output from 1.35.4 has error when parse Vkeywitness. This is the code:

const txWitness = "825820c0c92edecfd493b36e0253fe8ada5f3a2a0567318d7f9dc79c400f53866eeb94584090f4b238ffe510199e421eaf03575ca82a03b55f8b1fb8e33f80b3703e51f38cb0b7f8a668d79c328ad2f98e65600ef66b4b70e16640247507673d0d5d34a202"; // this is cbor from txw.signed from cli 1.35.4
const witness = cbor.encode(cbor.decodeAllSync(txWitness)[0][1]);
const encodedWitness = ada.Vkeywitness.from_bytes(witness); // this line throw error "Deserialization failed in Vkeywitness because: Invalid cbor: not the right type, expected `Array' byte received `Bytes'."

If I put "8200" prefix to txWitness, it works fine. what I expect is ada.Vkeywitness.from_bytes can read txWitness that is generated from cardano-cli 1.35.4

lisicky commented 1 year ago

@hoangong Could you tell me what you are doing by that line ? Does it work before ?

const witness = cbor.encode(cbor.decodeAllSync(txWitness)[0][1]);

I have checked deserialization with your cbor and it works. You can use from_hex like in an example below.

Vkeywitness::from_hex("825820c0c92edecfd493b36e0253fe8ada5f3a2a0567318d7f9dc79c400f53866eeb94584090f4b238ffe510199e421eaf03575ca82a03b55f8b1fb8e33f80b3703e51f38cb0b7f8a668d79c328ad2f98e65600ef66b4b70e16640247507673d0d5d34a202");
hoangong commented 1 year ago

@lisicky Thank for quick reply. from_hex works with output from cli 1.35.4

Vkeywitness::from_hex("825820c0c92edecfd493b36e0253fe8ada5f3a2a0567318d7f9dc79c400f53866eeb94584090f4b238ffe510199e421eaf03575ca82a03b55f8b1fb8e33f80b3703e51f38cb0b7f8a668d79c328ad2f98e65600ef66b4b70e16640247507673d0d5d34a202");

but not with output from cli 1.35.3

Vkeywitness::from_hex("8200825820c0c92edecfd493b36e0253fe8ada5f3a2a0567318d7f9dc79c400f53866eeb94584090f4b238ffe510199e421eaf03575ca82a03b55f8b1fb8e33f80b3703e51f38cb0b7f8a668d79c328ad2f98e65600ef66b4b70e16640247507673d0d5d34a202"); // Deserialization failed in Vkeywitness.vkey because: Invalid cbor: not the right type, expected `Bytes' byte received `UnsignedInteger'.
lisicky commented 1 year ago

@hoangong Seems cli 1.35.3 gives cbor in not from cddl format. CSL supports cbor in format that is described in the Alonzo and babbage cddl.