Ejemplo n.º 1
0
    def fingerprint(self) -> bytes:
        """
        Gets current node fingerprint.

        :return: first four bytes of SHA256(RIPEMD160(public key))
        """
        return hash160(self.public_key.sec())[:4]
Ejemplo n.º 2
0
    def test_repr(self):
        pub_key = "020bf81062796b773dcb4055dfab420ef92fa95ebd92426a994a464dec5b321004"
        pub_key_bytes = bytes.fromhex(pub_key)
        h160_pub_key = hash160(pub_key_bytes)

        script = p2wpkh_script(h160=h160_pub_key)
        self.assertEqual(str(script),
                         "OP_0 6c743a71b8899dcd30882f5affa712e130339866")

        script = p2pkh_script(h160=h160_pub_key)
        self.assertEqual(
            str(script),
            "OP_DUP OP_HASH160 6c743a71b8899dcd30882f5affa712e130339866 OP_EQUALVERIFY OP_CHECKSIG"
        )

        # still hashing pub key instead of script -> here it does not matter
        script = p2wsh_script(h256=sha256(pub_key_bytes))
        self.assertEqual(
            str(script),
            "OP_0 8634ffba92b015a6c13840d787a17a4a8c0e6c39aeeb56e3fd3d68d97ee2fbd8"
        )

        script = p2sh_script(h160=h160_pub_key)
        self.assertEqual(
            str(script),
            "OP_HASH160 6c743a71b8899dcd30882f5affa712e130339866 OP_EQUAL")
Ejemplo n.º 3
0
    def h160(self, compressed: bool = True) -> bytes:
        """
        SHA256 followed by RIPEMD160 of public key.

        :param compressed: whether to use compressed format (default=True)
        :return: SHA256(RIPEMD160(public key))
        """
        return hash160(self.sec(compressed=compressed))
Ejemplo n.º 4
0
    def p2sh_p2wpkh_address(self, node: Prv_or_PubKeyNode) -> str:
        """
        Generates p2wpkh wrapped in p2sh address from node.

        :param node: key node
        :return: p2sh-p2wpkh address
        """
        return h160_to_p2sh_address(h160=hash160(
            p2wpkh_script(h160=node.public_key.h160()).raw_serialize()),
                                    testnet=self.testnet)
Ejemplo n.º 5
0
    def p2sh_p2wsh_address(self, node: Prv_or_PubKeyNode) -> str:
        """
        Generates p2wsh wrapped in p2sh address from node.

        :param node: key node
        :return: p2sh-p2wsh address
        """
        # [OP_1, sec, OP_1, OP_CHECKMULTISIG]
        witness_script = Script([0x51, node.public_key.sec(), 0x51, 0xae])
        sha256_witness_script = sha256(witness_script.raw_serialize())
        redeem_script = p2wsh_script(
            h256=sha256_witness_script).raw_serialize()
        return h160_to_p2sh_address(h160=hash160(redeem_script),
                                    testnet=self.testnet)