コード例 #1
0
 def _checksum(self):
     if self._type.__eq__(UnlockHashType.NIL):
         return bytes(jsarr.new_array(UnlockHash._CHECKSUM_SIZE))
     e = RivineBinaryEncoder()
     e.add_int8(self._type.value)
     e.add(self._hash)
     return jscrypto.blake2b(e.data)
コード例 #2
0
    def from_str(cls, obj):
        if not isinstance(obj, str):
            raise TypeError(
                "UnlockHash is expected to be a str, not {}".format(type(obj)))
        obj = jsstr.strip(obj)
        if len(obj) != UnlockHash._TOTAL_SIZE_HEX:
            raise ValueError(
                "UnlockHash is expexcted to be of length {} when stringified, not of length {}, invalid: {} ({})"
                .format(UnlockHash._TOTAL_SIZE_HEX, len(obj), obj, type(obj)))

        t = UnlockHashType(
            int(jsarr.slice_array(obj, 0, UnlockHash._TYPE_SIZE_HEX)))
        h = Hash(
            value=obj[UnlockHash._TYPE_SIZE_HEX:UnlockHash._TYPE_SIZE_HEX +
                      UnlockHash._HASH_SIZE_HEX])
        uh = cls(uhtype=t, uhhash=h)

        if t.__eq__(UnlockHashType.NIL):
            expectedNH = jshex.bytes_to_hex(
                bytes(jsarr.new_array(UnlockHash._HASH_SIZE)))
            nh = jshex.bytes_to_hex(h.value)
            if nh != expectedNH:
                raise ValueError("unexpected nil hash {}".format(nh))
        else:
            expected_checksum = jshex.bytes_to_hex(
                jsarr.slice_array(uh._checksum(), 0,
                                  UnlockHash._CHECKSUM_SIZE))
            checksum = jsarr.slice_array(
                obj,
                UnlockHash._TOTAL_SIZE_HEX - UnlockHash._CHECKSUM_SIZE_HEX)
            if expected_checksum != checksum:
                raise ValueError("unexpected checksum {}, expected {}".format(
                    checksum, expected_checksum))

        return uh
コード例 #3
0
def to_bytes(x, nbytes, order=None):
    if nbytes == 0:
        return bytes(jsarr.new_array(0))
    if nbytes == 1:
        return jsbin.from_int8(x, order)
    if nbytes == 2:
        return jsbin.from_int16(x, order)
    if nbytes == 3:
        return jsbin.from_int24(x, order)
    if nbytes == 4:
        return jsbin.from_int32(x, order)
    if nbytes == 5:
        return jsbin.from_int40(x, order)
    if nbytes == 6:
        return jsbin.from_int48(x, order)
    if nbytes == 7:
        return jsbin.from_int56(x, order)
    if nbytes == 8:
        return jsbin.from_int64(x, order)
    raise ValueError("unsupported nbytes: {}".format(nbytes))
コード例 #4
0
 def value(self, value):
     # normalize the value
     if isinstance(value, BinaryData):
         value = value.value
     elif value == None:
         value = bytes(jsarray.new_array(0))
     elif isinstance(value, str):
         value = self._from_str(value)
     elif isinstance(value, bytearray):
         value = bytes(value)
     elif not isinstance(value, bytes) and not jsarray.is_uint8_array(value):
         raise TypeError(
             "binary data can only be set to a BinaryData, str, bytes or bytearray, not {}".format(type(value)))
     # if fixed size, check this now
     lvalue = len(value)
     if self._fixed_size != None and lvalue != 0 and lvalue != self._fixed_size:
         raise ValueError(
             "binary data was expected to be of fixed size {}, length {} is not allowed".format(
                 self._fixed_size, len(value)))
     # all good, assign the bytearray value
     self._value = value
コード例 #5
0
 def _pad_specifier(specifier):
     _SPECIFIER_SIZE = 16
     value = jsstr.to_utf8(specifier)
     return jsarray.concat(value,
                           jsarray.new_array(_SPECIFIER_SIZE - len(value)))
コード例 #6
0
 def _custom_data_getter(self):
     return bytes(jsarr.new_array(0))