Пример #1
0
 def hex(self):
     res_buff = []
     self._sort()
     for key, val in self.value.items():
         res_buff += utils.flatten_sha256(key).hex()
         res_buff += utils.flatten_sha256(val).hex()
     #return (Script() << ''.join(res_buff)).to_hex()
     return ''.join(res_buff)
Пример #2
0
 def key_index(self, key):
     key = utils.primitives_to_scrypt_types(key)
     key_hash = utils.flatten_sha256(key)
     assert type(key) == self.type_key
     self._sort()
     for i, key_other in enumerate(self.value.keys()):
         key_hash_other = utils.flatten_sha256(key_other)
         if key_hash == key_hash_other:
             return i
     return None
Пример #3
0
 def delete(self, key):
     key_hash = utils.flatten_sha256(key)
     to_del = None
     for key_other in self.value:
         key_hash_other = utils.flatten_sha256(key_other)
         if key_hash == key_hash_other:
             to_del = key_other
             break
     if to_del:
         self.value.remove(to_del)
     else:
         raise KeyError('Key not present in this HashedMap.')
Пример #4
0
    def add(self, key):
        key = utils.primitives_to_scrypt_types(key)
        assert type(key) == self.type_val

        # TODO: See set() of HashedMap.
        key_hash = utils.flatten_sha256(key)
        for key_other in self.value:
            key_hash_other = utils.flatten_sha256(key_other)
            if key_hash == key_hash_other:
                self.value.add(key_other)
                return
        self.value.add(key)
Пример #5
0
    def set(self, key, val):
        key = utils.primitives_to_scrypt_types(key)
        val = utils.primitives_to_scrypt_types(val)
        assert type(key) == self.type_key
        assert type(val) == self.type_val

        # TODO: Instead of looping, find other way to directly change
        #       value of existing entry.
        #       This currently doesn't work because it checks if it's the same object itself,
        #       and not only the value.
        key_hash = utils.flatten_sha256(key)
        for key_other in self.value.keys():
            key_hash_other = utils.flatten_sha256(key_other)
            if key_hash == key_hash_other:
                self.value[key_other] = val
                return
        self.value[key] = val
Пример #6
0
    def keys_sorted(self):
        # Sort by keys hashes - ASC
        res = []
        keys_and_keyhashes = []
        for key in self.value:
            key_hash = utils.flatten_sha256(key)
            keys_and_keyhashes.append((key_hash, key))

        keys_and_keyhashes.sort(key=lambda x:x[0][::-1])
        for key_hash, key in keys_and_keyhashes[::-1]:
            res.append(key)
        
        return res
Пример #7
0
    def _sort(self):
        # Sort by keys hashes - ASC
        new_dict = dict()
        keys_and_keyhashes = []
        for key in self.value.keys():
            key_hash = utils.flatten_sha256(key)
            keys_and_keyhashes.append((key_hash, key))

        keys_and_keyhashes.sort(key=lambda x:x[0][::-1])
        for key_hash, key in keys_and_keyhashes[::-1]:
            new_dict[key] = self.value[key]

        self.value = new_dict
Пример #8
0
 def hex(self):
     res_buff = []
     for key in self.keys_sorted():
         res_buff += utils.flatten_sha256(key).hex()
     return ''.join(res_buff)