I'm trying to use the tokenType()
function in Midnight's compact language to calculate the TokenType of a new coin. Is there a helper function I can use in the Midnight JS library to replicate Compact's built in pad()
function?
I'm struggling with creating a Uint8Array that matches the Bytes<32> Compact type I created with the pad function in my contract. Any suggestions on how to do this?
To replicate Compact’s pad() function in Midnight JS, you can manually pad a Uint8Array to the desired byte length like so:
function padToBytes32(input: Uint8Array): Uint8Array {
if (input.length > 32) throw new Error("Input too long for Bytes<32>");
const padded = new Uint8Array(32);
padded.set(input, 32 - input.length); // right-align like Compact's pad()
return padded;
}
This matches Compact’s behavior where values are padded on the left (i.e., right-aligned) with zeros to match the required byte size.