def hash_coin_list(coin_list: List[Coin]) -> bytes32: coin_list.sort(key=lambda x: x.name_str, reverse=True) buffer = bytearray() for coin in coin_list: buffer.extend(coin.name()) return std_hash(buffer)
def load_clvm(filename): filehash = "" with open(filename, 'rb') as standard_file: buf = standard_file.read() filehash = std_hash(buf) standard_file.close() hex_filename = filename + "." + str(filehash) + ".hex" with open(hex_filename, 'rb') as hex_file: buf = hex_file.read() hex_file.close() return Program.from_bytes(bytes.fromhex(buf.decode('utf8')))
def _tree_hash(self, precalculated: Set[bytes32]) -> bytes32: """ Hash values in `precalculated` are presumed to have been hashed already. """ if self.listp(): left = self.to(self.first())._tree_hash(precalculated) right = self.to(self.rest())._tree_hash(precalculated) s = b"\2" + left + right else: atom = self.as_atom() if atom in precalculated: return bytes32(atom) s = b"\1" + atom return bytes32(std_hash(s))
def _tree_hash(node: SExp, precalculated: Set[bytes32]) -> bytes32: """ Hash values in `precalculated` are presumed to have been hashed already. """ if node.listp(): left = _tree_hash(node.first(), precalculated) right = _tree_hash(node.rest(), precalculated) s = b"\2" + left + right else: atom = node.as_atom() if atom in precalculated: return bytes32(atom) s = b"\1" + atom return bytes32(std_hash(s))
def get_name_puzzle_conditions(block_program: SerializedProgram, safe_mode: bool): # TODO: allow generator mod to take something (future) # TODO: write more tests block_program_args = SerializedProgram.from_bytes(b"\x80") try: if safe_mode: cost, result = GENERATOR_MOD.run_safe_with_cost(block_program, block_program_args) else: cost, result = GENERATOR_MOD.run_with_cost(block_program, block_program_args) npc_list = [] opcodes = set(item.value for item in ConditionOpcode) for res in result.as_iter(): conditions_list = [] name = std_hash( bytes( res.first().first().as_atom() + res.first().rest().first().as_atom() + res.first().rest().rest().first().as_atom() ) ) puzzle_hash = bytes32(res.first().rest().first().as_atom()) for cond in res.rest().first().as_iter(): if cond.first().as_atom() in opcodes: opcode = ConditionOpcode(cond.first().as_atom()) elif not safe_mode: opcode = ConditionOpcode.UNKNOWN else: return "Unknown operator in safe mode.", None, None if len(list(cond.as_iter())) > 1: cond_var_list = [] for cond_1 in cond.rest().as_iter(): cond_var_list.append(cond_1.as_atom()) cvl = ConditionVarPair(opcode, cond_var_list) else: cvl = ConditionVarPair(opcode, []) conditions_list.append(cvl) conditions_dict = conditions_by_opcode(conditions_list) if conditions_dict is None: conditions_dict = {} npc_list.append(NPC(name, puzzle_hash, [(a, b) for a, b in conditions_dict.items()])) return None, npc_list, uint64(cost) except Exception: tb = traceback.format_exc() return tb, None, None
def calculate_synthetic_offset(public_key: G1Element, hidden_puzzle_hash: bytes32) -> int: blob = std_hash(bytes(public_key) + hidden_puzzle_hash) offset = int_from_bytes(blob) offset %= GROUP_ORDER return offset
def create_farmer_coin(block_index: uint32, puzzle_hash: bytes32, reward: uint64): block_index_as_hash = std_hash(std_hash(block_index.to_bytes(4, "big"))) return Coin(block_index_as_hash, puzzle_hash, reward)
def name(self) -> bytes32: return std_hash(bytes(self.parent_coin_info + self.message))
def get_hash(self) -> bytes32: return bytes32(std_hash(bytes(self)))
def secret_exponent_for_index(index: int) -> int: blob = index.to_bytes(32, "big") hashed_blob = BasicSchemeMPL.key_gen(std_hash(b"foo" + blob)) r = int.from_bytes(hashed_blob, "big") return r