What's the correct Motoko syntax for this this pseudocode of a sha256_to_derivation_path() function?

public func sha256_to_derivation_path(sha256_hash_value : [Nat8]) : async [Nat8] {

    // Use the hash value to generate the derivation path
    let derivation_path = [
      44 + int.from_bytes(sha256_hash_value[: 4], byteorder = 'big') % 45,
      int.from_bytes(sha256_hash_value[4 : 8], byteorder = 'big') % 235,
      int.from_bytes(sha256_hash_value[8 : 12], byteorder = 'big') % 65535,
      int.from_bytes(sha256_hash_value[12 : 16], byteorder = 'big') % 65535,
      int.from_bytes(sha256_hash_value[16 : 20], byteorder = 'big') % 65535,
    ] 
    return derivation_path;
  }

I made a library to help out with number/byte conversions

Try something like this:

import NatX "mo:xtended-numbers/NatX";
import IntX "mo:xtended-numbers/IntX";
...
    public func sha256_to_derivation_path(sha256_hash_value : [Nat8]) : ?[Nat8] {
        do ? {
            // Use the hash value to generate the derivation path
            let iter = Iter.fromArray(sha256_hash_value);
            let v1 = decodeToByte(44 + IntX.decodeInt32(iter, #msb)! % 45);
            let v2 = decodeToByte(IntX.decodeInt32(iter, #msb)! % 235);
            let v3 = decodeToByte(IntX.decodeInt32(iter, #msb)! % 65535);
            let v4 = decodeToByte(IntX.decodeInt32(iter, #msb)! % 65535);
            let v5 = decodeToByte(IntX.decodeInt32(iter, #msb)! % 65535);
            return ?[v1, v2, v3, v4, v5];
        };
    };

    private func decodeToByte(v : Int32) : Nat8 {
        let buffer = Buffer.Buffer<Nat8>(1);
        IntX.encodeInt32(buffer, v, #msb);
        Buffer.toArray(buffer)[0];
    };
2 Likes

I followed the instructions to install moc and then the package but on deploy I got

Failed to run ‘moc’.:

I tried both install and add, as suggested here: Internet Computer Content Validation Bootstrap

but no luck.

Do you know what may be wrong?

@blabagastered I’ll need more info.
What is the error message?
How are you installing moc?
Are you using dfx for a cashier or just moc for a library?

I solved the problem a different way but I’m getting another problem with mops.

I wonder if it has to do with moc. I have not explicitly installed moc. I don’t know if another command has installed it.

This is the full log: icrc1/src/ICRC1/lib.mo:16.1-16.40: import error [M0010], package "StableTrieMap" not defined

dfx downloads a moc version, but doesn’t add it to $PATH