Skip to content
Advertisement

Borsh JS and Borsh Rust slightly different serialized output

I am trying to send borsh serialized data from JS to rust application. However, when serialising data in javascript AND rust (to compare the outputs), I am getting 4 extra bytes in rust serialised output. Here is the code:

Borsh JS code

JavaScript

Borsh Rust Code

JavaScript

Comparing outputs of both

  1. Borsh JS
  2. Borsh Rust
JavaScript

There are extra 4 bytes (1, 0, 0, 0) in rust serialised output. I believe this is happening because of Vec<u32> used for votes field (it works fine for u32). However I am unable to understand why this is happening.

Any sort of help/insights are appreciated.

Thank you!

Advertisement

Answer

A Vec<u32> has to encode the length of the data since a Vec represents a variable size. Conversely, you’ve designed your schema in JS as ['u32', 1] which is an array of length 1, so it does not need to encode a length since it is a fixed size.

To resolve the difference, either set your schema to a variable size array: ['u32']. Or change your type in Rust to be a fixed-size array: votes: [u32; 1].

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement