Principal size for stable structure

You were right @LightningLad91, the remaining issue I had was related to the Candid encoding. It adds the ”DIDL” but also serialize values and types (from what I understand from it source code).

pub fn serialize<W: io::Write>(&mut self, mut writer: W) -> Result<()> {
        writer.write_all(b"DIDL")?;
        self.type_ser.serialize()?;
        writer.write_all(self.type_ser.get_result())?;
        writer.write_all(self.value_ser.get_result())?;
        Ok(())
    }

So in addition to defining a proper size for the principal, I also replaced Candid serialization for a custom manual serialization as they do in Internet Idenditiy: https://github.com/dfinity/internet-identity/blob/0641ea641f64a2b51b3647ad1140840c2118b5d6/src/archive/src/main.rs#L212

When it comes to the Principal I ultimately actually serialized it to a bytes length of 30 (29 bytes + one for the length). See this answer for the reason why and how: Convert principal to Vec 29 bytes length - #3 by levi

1 Like