for now this is my implementation:
stable var tip_cert = MerkleTree.empty();
func updateTipCert() = CertifiedData.set(MerkleTree.treeHash(tip_cert));
system func postupgrade() = updateTipCert();
// whenever there's a new block
// ...
tip_cert := MerkleTree.empty();
tip_cert := MerkleTree.put(tip_cert, [Text.encodeUtf8(Constants.LAST_BLOCK_INDEX)], block_id);
tip_cert := MerkleTree.put(tip_cert, [Text.encodeUtf8(Constants.LAST_BLOCK_HASH)], block_hash);
updateTipCert();
// getting the tip cert
public shared query func icrc3_get_tip_certificate() : async ?Types.DataCertificate {
let certificate = switch (CertifiedData.getCertificate()) {
case (?found) found;
case _ return null;
};
let index_witness = MerkleTree.reveal(tip_cert, [Text.encodeUtf8(Constants.LAST_BLOCK_INDEX)]);
let hash_witness = MerkleTree.reveal(tip_cert, [Text.encodeUtf8(Constants.LAST_BLOCK_HASH)]);
let merge = MerkleTree.merge(index_witness, hash_witness);
let witness = MerkleTree.encodeWitness(merge); // CBOR encoded
return ?{ certificate; hash_tree = witness };
};
(did i miss anything important that i should add to my implementation? if so, pls let me know)
As for the hash_tree
, since it’s CBOR encoded, I decoded it and it returned me this;
not sure how to make sense from that. I want to get the
last_block_id
and last_block_hash
to validate all blocks, but I dont know how to start.
and as for certificate
, how do I verify it? or this is too low-level for me?
in case you’re wondering about system func postupgrade() = updateTipCert();
, it’s something i picked up from @nomeata 's Certified Asset tutorial: