Beispiel #1
0
 def test_utils(self):
     s_spaces = " 0C 28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D  "
     b = bytes_from_octets(s_spaces)
     s = b.hex()  # lower case, no spaces
     self.assertNotEqual(s, s_spaces)
     self.assertEqual(hash160(s_spaces), hash160(bytes_from_octets(s)))
     self.assertEqual(hash256(s_spaces), hash256(bytes_from_octets(s)))
Beispiel #2
0
def test_hash160_hash256() -> None:
    test_vectors = (plain_prv_keys + net_unaware_compressed_pub_keys +
                    net_unaware_uncompressed_pub_keys)
    for hexstring in test_vectors:
        b = bytes_from_octets(hexstring)
        s = b.hex()  # lower case, no spaces
        assert hash160(hexstring) == hash160(s)
        assert hash256(hexstring) == hash256(s)
Beispiel #3
0
    def test_utils(self):
        b = bytes_from_octets(int_with_whitespaces)
        s = b.hex()  # lower case, no spaces
        self.assertNotEqual(int_with_whitespaces, s)
        self.assertEqual(hash160(int_with_whitespaces), hash160(s))
        self.assertEqual(hash256(int_with_whitespaces), hash256(s))

        i = secrets.randbits(256)
        self.assertEqual(i, int_from_integer(i))
        self.assertEqual(i, int_from_integer(i.to_bytes(32, "big")))
        self.assertEqual(i, int_from_integer(hex(i)))
Beispiel #4
0
def generate_merkle_root(transactions):
    if len(transactions) == 0:
        return hash256(b"")
    hashes = [bytes.fromhex(transaction.txid) for transaction in transactions]
    hashes_buffer = []
    while len(hashes) != 1:
        if len(hashes) % 2 != 0:
            hashes.append(hashes[-1])
        for x in range(len(hashes) // 2):
            hashes_buffer.append(hash256(hashes[2 * x] + hashes[2 * x + 1]))
        hashes = hashes_buffer[:]
        hashes_buffer = []
    return hashes[0].hex()
Beispiel #5
0
def legacy(script_: Octets, tx: Tx, vin_i: int, hash_type: int) -> bytes:
    script_ = bytes_from_octets(script_)

    new_tx = deepcopy(tx)
    for txin in new_tx.vin:
        txin.script_sig = b""
    # TODO: delete sig from script_ (even if non standard)
    new_tx.vin[vin_i].script_sig = script_
    if hash_type & 0x1F == NONE:
        new_tx.vout = []
        for i, txin in enumerate(new_tx.vin):
            if i != vin_i:
                txin.sequence = 0

    if hash_type & 0x1F == SINGLE:
        # sign_hash single bug
        if vin_i >= len(new_tx.vout):
            return (256**31).to_bytes(32, byteorder="big", signed=False)
        new_tx.vout = new_tx.vout[:vin_i + 1]
        for txout in new_tx.vout[:-1]:
            txout.script_pub_key = ScriptPubKey(b"")
            txout.value = 0xFFFFFFFFFFFFFFFF
        for i, txin in enumerate(new_tx.vin):
            if i != vin_i:
                txin.sequence = 0

    if hash_type & 0x80:
        new_tx.vin = [new_tx.vin[vin_i]]

    preimage = new_tx.serialize(include_witness=False, check_validity=False)
    preimage += hash_type.to_bytes(4, byteorder="little", signed=False)

    return hash256(preimage)
Beispiel #6
0
    def hash(self) -> bytes:
        """Return the transaction hash.

        It differs from tx_id for witness transactions.
        """
        serialized_ = self.serialize(include_witness=True,
                                     check_validity=False)
        hash256_ = hash256(serialized_)
        return hash256_[::-1]
Beispiel #7
0
def segwit_v0(script_: Octets, tx: Tx, vin_i: int, hash_type: int,
              amount: int) -> bytes:
    script_ = bytes_from_octets(script_)

    hash_prev_outs = b"\x00" * 32
    if not hash_type & ANYONECANPAY:
        hash_prev_outs = b"".join([vin.prev_out.serialize() for vin in tx.vin])
        hash_prev_outs = hash256(hash_prev_outs)

    hash_seqs = b"\x00" * 32
    if (not (hash_type & ANYONECANPAY) and (hash_type & 0x1F) != SINGLE
            and (hash_type & 0x1F) != NONE):
        hash_seqs = b"".join([
            vin.sequence.to_bytes(4, byteorder="little", signed=False)
            for vin in tx.vin
        ])
        hash_seqs = hash256(hash_seqs)

    hash_outputs = b"\x00" * 32
    if hash_type & 0x1F not in (SINGLE, NONE):
        hash_outputs = b"".join([vout.serialize() for vout in tx.vout])
        hash_outputs = hash256(hash_outputs)
    elif (hash_type & 0x1F) == SINGLE and vin_i < len(tx.vout):
        hash_outputs = hash256(tx.vout[vin_i].serialize())

    preimage = b"".join([
        tx.version.to_bytes(4, byteorder="little", signed=False),
        hash_prev_outs,
        hash_seqs,
        tx.vin[vin_i].prev_out.serialize(),
        var_bytes.serialize(script_),
        amount.to_bytes(8, byteorder="little", signed=False),  # value
        tx.vin[vin_i].sequence.to_bytes(4, byteorder="little", signed=False),
        hash_outputs,
        tx.lock_time.to_bytes(4, byteorder="little", signed=False),
        hash_type.to_bytes(4, byteorder="little", signed=False),
    ])
    return hash256(preimage)
Beispiel #8
0
 def test_utils(self):
     s = "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"
     self.assertEqual(hash160(s), hash160(bytes.fromhex(s)))
     self.assertEqual(hash256(s), hash256(bytes.fromhex(s)))
Beispiel #9
0
def _hash256(variable, data, memory):
    memory[variable] = hash256(memory[data[0]])
def test_hash160_hash256() -> None:
    test_vectors = (plain_prv_keys + net_unaware_compressed_pub_keys +
                    net_unaware_uncompressed_pub_keys)
    for hexstring in test_vectors:
        hash160(hexstring)
        hash256(hexstring)
Beispiel #11
0
 def id(self) -> bytes:
     "Return the transaction id."
     serialized_ = self.serialize(include_witness=False,
                                  check_validity=False)
     hash256_ = hash256(serialized_)
     return hash256_[::-1]