struct Node { Sha256 hash; int pos; } library MerklePath { static const int INVALID_NODE = 0; static const int LEFT_NODE = 1; static const int RIGHT_NODE = 2; static function calcMerkleRoot(Sha256 leaf, Node[32] merkleProof, static const int depth) : Sha256 { Sha256 root = leaf; loop (depth) : i { Node node = merkleProof[i]; if(node.pos != 0) { root = node.pos == 1 ? Sha256(hash256(node.hash + root)) : Sha256(hash256(root + node.hash)); } } return root; } static function isCoinbase(Node[32] merkleproof, static const int depth) : bool { bool res = true; loop (depth) : i { Node node = merkleproof[i]; if(node.pos != 0) { res = res && node.pos == 2; } } return res; } }