construct_ic_system_state_tree_root_hash function

Uint8List construct_ic_system_state_tree_root_hash(
  1. List tree
)

Constructs the root hash of a HashTree in a certificate returned by the network. Useful when verifying a canister's certified_data.

internetcomputer.org/docs/current/references/ic-interface-spec/#certificate

Implementation

Uint8List construct_ic_system_state_tree_root_hash(List tree) {
    List<int> v;
    if (tree[0] == 0) {
        assert(tree.length==1);
        v = sha256.convert(createdomainseparatorbytes("ic-hashtree-empty")).bytes;
    }
    if (tree[0] == 1) {
        assert(tree.length==3);
        v = sha256.convert(createdomainseparatorbytes("ic-hashtree-fork") + construct_ic_system_state_tree_root_hash(tree[1]) + construct_ic_system_state_tree_root_hash(tree[2])).bytes;
    }
    else if (tree[0] == 2) {
        assert(tree.length==3);
        v = sha256.convert(createdomainseparatorbytes("ic-hashtree-labeled") + tree[1] + construct_ic_system_state_tree_root_hash(tree[2])).bytes;
    }
    else if (tree[0] == 3) {
        assert(tree.length==2);
        v = sha256.convert(createdomainseparatorbytes("ic-hashtree-leaf") + tree[1]).bytes;
    }
    else if (tree[0] == 4) {
        assert(tree.length==2);
        v = tree[1];
    }
    else {
        throw Exception(':system-state-tree is in the wrong-format.');
    }
    return Uint8List.fromList(v);
}