예제 #1
0
    def get_P2PKH(self):
        """
        PK = public key in hex
        """
        # Get the parsed script
        script = self.parsed_pkScript
        pk = script[script.index("PUSH_BYTES") + 2]

        # Add version
        pk = b"\00" + pk
        if self.verb >= 6:
            print "{0}pk + ver: {1}".format(" " * 6, pk.encode("hex"))

        # Hash
        h = hash_SHA256_twice(pk)
        if self.verb >= 6:
            print "{0}hash: {1}".format(" " * 6, h.encode("hex"))
        # Add first 4 bytes of second hash to pk (already hex)
        pk = pk + h[0:4]
        if self.verb >= 6:
            print "{0}pk + checksum: {1}".format(" " * 6, pk.encode("hex"))

        # Convert to base 58 (bin -> base58)
        b58 = base58.b58encode(pk)
        if self.verb >= 6:
            print "{0}b58: {1}".format(" " * 6, b58)

        return b58
예제 #2
0
파일: block.py 프로젝트: yaojunWang/PyBC
    def P2PKH(pk: hex,
              debug: bool=False) -> str:
        """
        pk = public key in hex
        """
        # Add version
        pk = b"\00" + pk
        if debug:
            print("{0}pk + ver: {1}".format(" "*6, codecs.encode(pk, "hex")))

        # Hash
        h = hash_SHA256_twice(pk)
        if debug:
            print("{0}hash: {1}".format(" "*6, codecs.encode(h, "hex")))
        # Add first 4 bytes of second hash to pk (already hex)
        pk = pk + h[0:4]
        if debug:
            print("{0}pk + checksum: {1}".format(
                            " "*6, codecs.encode(pk, "hex")))

        # Convert to base 58 (bin -> base58)
        b58 = base58.b58encode(pk)
        if debug:
            print("{0}b58: {1}".format(" "*6, b58))

        return b58
예제 #3
0
    def test_hash_SHA256_twice_s1(self):
        """Test hashing with SHA256 twice."""
        inp = b'\xf3\x17\x9d\x8c\xd7\xbd\x03'

        exp = b"\xb8xn\xdc\x07\xa2\x19\x1e\xd8\xa5\x18\xa3"\
            b"\x8cc\xdf\xda\xf7\xde\xb3n\x91\x00\xfc)\x90P<\xbdzE\xff\xf6"

        self.assertEqual(hash_SHA256_twice(inp), exp)
예제 #4
0
파일: common.py 프로젝트: yaojunWang/PyBC
    def _hash(self) -> bytes:
        """
        Get prepapred header, return hash

        Here self.prep_header() will have been overloaded by
        Trans.prep_header() or Block.prep_header()
        """
        return hash_SHA256_twice(self.prep_header())
예제 #5
0
    def get_PK2Addr(self):
        """
        PK = public key in hex

        Work in bytes throughout (encode back to hex for any prints)
        """
        # Get the parsed script
        script = self.parsed_pkScript
        pk = script[script.index("PUSH_BYTES") + 2]

        # Decode input to binary
        pk = pk.decode("hex")
        if self.verb >= 6:
            print "{0}pk: {1}".format(" " * 6, pk.encode("hex"))

        # Hash SHA256
        h = hash_SHA256_ripemd160(pk)
        if self.verb >= 6:
            print "{0}SHA256: h1: {1}".format(" " * 6, h.encode("hex"))

        # Add version
        h = b"\00" + h
        if self.verb >= 6:
            print "{0}version + h1: {1}".format(" " * 6, h.encode("hex"))

        # Hash SHA256
        h2 = hash_SHA256_twice(h)
        if self.verb >= 6:
            print "{0}h2: {1}".format(" " * 6, h2.encode("hex"))

        # Get checksum
        cs = h2[0:4]
        if self.verb >= 6:
            print "{0}checksum: {1}".format(" " * 6, cs.encode("hex"))
            print "{0}h2 + cs: {1}".format(" " * 5, (h2 + cs).encode("hex"))

        # Add checksum and convert to base58
        b58 = base58.b58encode(h + cs)
        if self.verb >= 6:
            print "{0}b58: {1}".format(" " * 6, b58)

        return b58
예제 #6
0
파일: block.py 프로젝트: yaojunWang/PyBC
    def PK2Addr(pk: hex,
                debug: bool=False) -> str:
        """
        pk = public key in hex
        """
        # Decode input to binary
        pk = codecs.decode(pk, "hex")
        if debug:
            print("{0}pk: {1}".format(" "*6, codecs.encode(pk, "hex")))

        # Hash SHA256
        h = hash_SHA256_ripemd160(pk)
        if debug:
            print("{0}SHA256: h1: {1}".format(" "*6, codecs.encode(h, "hex")))

        # Add version
        h = b"\00" + h
        if debug:
            print("{0}version + h1: {1}".format(
                            " "*6, codecs.encode(h, "hex")))

        # Hash SHA256
        h2 = hash_SHA256_twice(h)
        if debug:
            print("{0}h2: {1}".format(" "*6, codecs.encode(h2, "hex")))

        # Get checksum
        cs = h2[0:4]
        if debug:
            print("{0}checksum: {1}".format(" "*6, codecs.encode(cs, "hex")))
            print("{0}h2 + cs: {1}".format(" "*6,
                                           codecs.encode(h2 + cs, "hex")))

        # Add checksum and convert to base58
        b58 = base58.b58encode(h + cs)
        if debug:
            print("{0}b58: {1}".format(" "*6, b58))

        return b58
예제 #7
0
        + trans.txIn[0]._scriptSig \
        + trans.txIn[0]._sequence \
        + trans._nOutputs \
        + trans.txOut[0]._value \
        + trans.txOut[0]._pkScriptLen \
        + trans.txOut[0]._pkScript \
        + trans._lockTime

print("\n")
print(codecs.encode(header, "hex"))


# %% Hash with SHA256 twice
# Also reverse

print(codecs.encode(hash_SHA256_twice(header)[::-1], "hex"))


# %% Function version

def prep_header(trans):
    header = trans._version \
            + trans._nInputs \
            + trans.txIn[0]._prevOutput \
            + trans.txIn[0]._prevIndex \
            + trans.txIn[0]._scriptLength \
            + trans.txIn[0]._scriptSig \
            + trans.txIn[0]._sequence \
            + trans._nOutputs \
            + trans.txOut[0]._value \
            + trans.txOut[0]._pkScriptLen \
예제 #8
0
 def hash(self):
     """
     Get prepared header, hash twice with SHA256, reverse, convert to hex
     """
     header = self.prep_header()
     return hash_SHA256_twice(header)[::-1].encode("hex")
예제 #9
0
 def hash(self):
     return hash_SHA256_twice(self.prep_header())[::-1].encode("hex")