Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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
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
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
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())
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
+ 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 \
+ trans.txOut[0]._pkScript \
+ trans._lockTime
return header
header = prep_header(dat.blocks[0].trans[0])
transHash = codecs.encode(hash_SHA256_twice(header)[::-1], "hex")
print(transHash)