Пример #1
0
 def check_bip143_tx(self, tx_u_hex, tx_s_hex, txs_out_value_scripthex_pair,
                     tx_in_count, tx_out_count, version, lock_time):
     tx_u = Tx.from_hex(tx_u_hex)
     tx_s = Tx.from_hex(tx_s_hex)
     txs_out = [
         TxOut(int(coin_value * 1e8), h2b(script_hex))
         for coin_value, script_hex in txs_out_value_scripthex_pair
     ]
     for tx in (tx_u, tx_s):
         self.assertEqual(len(tx.txs_in), tx_in_count)
         self.assertEqual(len(tx.txs_out), tx_out_count)
         self.assertEqual(tx.version, version)
         self.assertEqual(tx.lock_time, lock_time)
         tx.set_unspents(txs_out)
     self.check_unsigned(tx_u)
     self.check_signed(tx_s)
     tx_hex = tx_u.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     tx_hex = tx_s.as_hex()
     self.assertEqual(tx_hex, tx_s_hex)
     tx_u_prime = self.unsigned_copy(tx_s)
     tx_hex = tx_u_prime.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_s_hex))), tx_s.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.id())
     return tx_u, tx_s
Пример #2
0
 def check_bip143_tx(
         self, tx_u_hex, tx_s_hex, txs_out_value_scripthex_pair, tx_in_count, tx_out_count, version, lock_time):
     tx_u = Tx.from_hex(tx_u_hex)
     tx_s = Tx.from_hex(tx_s_hex)
     txs_out = [
         TxOut(int(coin_value * 1e8), h2b(script_hex)) for coin_value, script_hex in txs_out_value_scripthex_pair
     ]
     for tx in (tx_u, tx_s):
         self.assertEqual(len(tx.txs_in), tx_in_count)
         self.assertEqual(len(tx.txs_out), tx_out_count)
         self.assertEqual(tx.version, version)
         self.assertEqual(tx.lock_time, lock_time)
         tx.set_unspents(txs_out)
     self.check_unsigned(tx_u)
     self.check_signed(tx_s)
     tx_hex = tx_u.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     tx_hex = tx_s.as_hex()
     self.assertEqual(tx_hex, tx_s_hex)
     tx_u_prime = self.unsigned_copy(tx_s)
     tx_hex = tx_u_prime.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_s_hex))), tx_s.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.id())
     return tx_u, tx_s
Пример #3
0
 def _calculate_hash(self):
     s = io.BytesIO()
     if self.height < self.FORK_BLOCK:
         self.stream_header_legacy(s)
     else:
         self.stream_header(s)
     return double_sha256(s.getvalue())
Пример #4
0
def _recurse(level_widths, level_index, node_index, hashes, flags, flag_index,
             tx_acc):
    idx, r = divmod(flag_index, 8)
    mask = (1 << r)
    flag_index += 1
    if flags[idx] & mask == 0:
        h = hashes.pop()
        return h, flag_index

    if level_index == len(level_widths) - 1:
        h = hashes.pop()
        tx_acc.append(h)
        return h, flag_index

    # traverse the left
    left_hash, flag_index = _recurse(level_widths, level_index + 1,
                                     node_index * 2, hashes, flags, flag_index,
                                     tx_acc)

    # is there a right?
    if node_index * 2 + 1 < level_widths[level_index + 1]:
        right_hash, flag_index = _recurse(level_widths, level_index + 1,
                                          node_index * 2 + 1, hashes, flags,
                                          flag_index, tx_acc)

        if left_hash == right_hash:
            raise ValueError(
                "merkle hash has same left and right value at node %d" %
                node_index)
    else:
        right_hash = left_hash

    return double_sha256(left_hash + right_hash), flag_index
Пример #5
0
def _recurse(level_widths, level_index, node_index, hashes, flags, flag_index, tx_acc):
    idx, r = divmod(flag_index, 8)
    mask = (1 << r)
    flag_index += 1
    if flags[idx] & mask == 0:
        h = hashes.pop()
        return h, flag_index

    if level_index == len(level_widths) - 1:
        h = hashes.pop()
        tx_acc.append(h)
        return h, flag_index

    # traverse the left
    left_hash, flag_index = _recurse(
        level_widths, level_index+1, node_index*2, hashes, flags, flag_index, tx_acc)

    # is there a right?
    if node_index*2+1 < level_widths[level_index+1]:
        right_hash, flag_index = _recurse(
            level_widths, level_index+1, node_index*2+1, hashes, flags, flag_index, tx_acc)

        if left_hash == right_hash:
            raise ValueError("merkle hash has same left and right value at node %d" % node_index)
    else:
        right_hash = left_hash

    return double_sha256(left_hash + right_hash), flag_index
Пример #6
0
 def subkey(self, path):
     """
     path:
         of the form "K" where K is an integer index, or "K/N" where N is usually
         a 0 (deposit address) or 1 (change address)
     """
     t = path.split("/")
     if len(t) == 2:
         n, for_change = t
     else:
         n, = t
         for_change = 0
     b = (str(n) + ':' + str(for_change) +
          ':').encode("utf8") + self.master_public_key()
     offset = from_bytes_32(double_sha256(b))
     if self.secret_exponent():
         return self.__class__(
             generator=self._generator,
             master_private_key=((self.master_private_key() + offset) %
                                 self._generator.order()))
     p1 = offset * self._generator
     x, y = self.public_pair()
     p2 = self._generator.Point(x, y)
     p = p1 + p2
     return self.__class__(public_pair=p, generator=self._generator)
Пример #7
0
 def _calculate_hash(self):
     s = io.BytesIO()
     if self.height < self.FORK_BLOCK:
         self.stream_header_legacy(s)
     else:
         self.stream_header(s)
     return double_sha256(s.getvalue())
Пример #8
0
def b2a_hashed_base58(data):
    """
    A "hashed_base58" structure is a base58 integer (which looks like a string)
    with four bytes of hash data at the end. Bitcoin does this in several places,
    including Bitcoin addresses.

    This function turns data (of type "bytes") into its hashed_base58 equivalent.
    """
    return b2a_base58(data + double_sha256(data)[:4])
Пример #9
0
def a2b_hashed_base58(s):
    """
    If the passed string is hashed_base58, return the binary data.
    Otherwise raises an EncodingError.
    """
    data = a2b_base58(s)
    data, the_hash = data[:-4], data[-4:]
    if double_sha256(data)[:4] == the_hash:
        return data
    raise EncodingError("hashed base58 has bad checksum %s" % s)
Пример #10
0
    def hash(self, hash_type=None):
        """Return the binary hash for this :class:`Tx` object.

        :param hash_type: (optional) if set, generates a hash specific to a particular type of signature.

        :return: 32 byte long binary blob corresponding to the hash
        """
        s = io.BytesIO()
        self.stream(s, include_witness_data=False)
        if hash_type is not None:
            stream_struct("L", s, hash_type)
        return double_sha256(s.getvalue())
Пример #11
0
    def blanked_hash(self):
        """
        Return the hash for this Tx object with solution scripts blanked.
        This hash is useful for determining if two Txs might be equivalent modulo
        malleability. (That is, even if tx1 is morphed into tx2 using the malleability
        weakness, they will still have the same blanked hash.)

        :return: 32 byte long binary blob corresponding to the blanked hash
        """
        s = io.BytesIO()
        self.stream(s, blank_solutions=True)
        return double_sha256(s.getvalue())
Пример #12
0
    def blanked_hash(self):
        """
        Return the hash for this Tx object with solution scripts blanked.
        This hash is useful for determining if two Txs might be equivalent modulo
        malleability. (That is, even if tx1 is morphed into tx2 using the malleability
        weakness, they will still have the same blanked hash.)

        :return: 32 byte long binary blob corresponding to the blanked hash
        """
        s = io.BytesIO()
        self.stream(s, blank_solutions=True)
        return double_sha256(s.getvalue())
Пример #13
0
    def hash(self, hash_type=None):
        """Return the binary hash for this :class:`Tx` object.

        :param hash_type: (optional) if set, generates a hash specific to a particular type of signature.

        :return: 32 byte long binary blob corresponding to the hash
        """
        s = io.BytesIO()
        self.stream(s, include_witness_data=False)
        if hash_type is not None:
            stream_struct("L", s, hash_type)
        return double_sha256(s.getvalue())
Пример #14
0
 def subkey(self, path):
     """
     path:
         of the form "K" where K is an integer index, or "K/N" where N is usually
         a 0 (deposit address) or 1 (change address)
     """
     t = path.split("/")
     if len(t) == 2:
         n, for_change = t
     else:
         n, = t
         for_change = 0
     b = (str(n) + ':' + str(for_change) + ':').encode("utf8") + self.master_public_key()
     offset = from_bytes_32(double_sha256(b))
     if self.secret_exponent():
         return self.__class__(
             master_private_key=((self.master_private_key() + offset) % self._generator.order())
         )
     p1 = offset * self._generator
     x, y = self.public_pair()
     p2 = self._generator.Point(x, y)
     p = p1 + p2
     return self.__class__(public_pair=p)
Пример #15
0
    def w_hash(self):
        """Return the segwit-specific binary hash for this :class:`Tx` object.

        :return: 32 byte long binary blob corresponding to the hash
        """
        return double_sha256(self.as_bin())
Пример #16
0
 def do_test(blob, expected_hash):
     self.assertEqual(double_sha256(blob), expected_hash)
Пример #17
0
 def _signature_for_hash_type_segwit(self, script, tx_in_idx, hash_type):
     hash_type |= self.FORKID_BTG << 8
     return from_bytes_32(double_sha256(self._segwit_signature_preimage(script, tx_in_idx, hash_type)))
Пример #18
0
 def _signature_for_hash_type_segwit(self, script, tx_in_idx, hash_type):
     hash_type |= self.FORKID_BTG << 8
     return from_bytes_32(
         double_sha256(
             self._segwit_signature_preimage(script, tx_in_idx, hash_type)))
Пример #19
0
    def w_hash(self):
        """Return the segwit-specific binary hash for this :class:`Tx` object.

        :return: 32 byte long binary blob corresponding to the hash
        """
        return double_sha256(self.as_bin())
Пример #20
0
 def do_test(blob, expected_hash):
     self.assertEqual(double_sha256(blob), expected_hash)
Пример #21
0
def b58_double_sha256(s):
    data = parse_b58(s)
    if data:
        data, the_hash = data[:-4], data[-4:]
        if double_sha256(data)[:4] == the_hash:
            return data