Esempio n. 1
0
def check_block():
    p = RawProxy()
    var = get_blockhash()
    if var is None:
        return
    header = p.getblockheader(var)
    header_hex = (to_little_endian(header['versionHex']) +
                  to_little_endian(header['previousblockhash']) +
                  to_little_endian(header['merkleroot']) +
                  to_little_endian(format(int(header['time']), 'x')) +
                  to_little_endian(header['bits']) +
                  to_little_endian(format(int(header['nonce']), 'x')))

    header_bin = binascii.unhexlify(header_hex)
    block_hask = hashlib.sha256(
        hashlib.sha256(header_bin).digest()).hexdigest()
    block_hask_le = to_little_endian(block_hask)
    if var == block_hask_le:
        print('Bloko hashas yra teisingas')
    else:
        print('Bloko hashas yra neteisingas')
    globals.control = 0
Esempio n. 2
0
def swap(x):
    ba = bytearray.fromhex(x)
    ba.reverse()
    return ''.join(format(n, '02x') for n in ba)


def convert_int32(x):
    return format(swap32(x), '#010x')[2:]


p = RawProxy()

blockid = str(raw_input("Enter block ID: "))

header = p.getblockheader(blockid)

version = convert_int32(header['version'])
previousBlock = swap(header['previousblockhash'])
merkleRoot = swap(header['merkleroot'])
time = convert_int32(header['time'])
bits = swap(header['bits'])
nonce = convert_int32(header['nonce'])

header_hex = version + previousBlock + merkleRoot + time + bits + nonce
header_bin = header_hex.decode('hex')
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()

final_hash = swap(hash.encode('hex_codec'))

print(final_hash)
Esempio n. 3
0
from bitcoin.rpc import RawProxy
import hashlib
import sys
import struct

p = RawProxy()

height = sys.argv[1]
block_hash = p.getblockhash(int(height))
block_header = p.getblockheader(block_hash)

def little_endian(value, is_number = False):
        if is_number == True:
                value = struct.pack("<I", value).encode('hex')
                return value
        else:
                binary = value.decode('hex')
                value = binary[::-1].encode('hex_codec')
                return value

header_hex = str(little_endian(block_header['versionHex']) + little_endian(block_header['previousblockhash']) +
              little_endian(block_header['merkleroot']) + str(little_endian(block_header['time'], is_number=True)) +
              little_endian(str(block_header['bits'])) + str(little_endian(block_header['nonce'], is_number=True)))

header_bin = header_hex.decode('hex')
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
calculated_hash = hash[::-1].encode('hex_codec')

print(block_hash)
print(calculated_hash)
if block_hash == calculated_hash:
from bitcoin.rpc import RawProxy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Bitcoin Core에 접속한다.
p = RawProxy()

# 블록체인의 블록 개수을 읽어온다
n = p.getblockcount()

# 최근 1000개 블록의 헤더를 읽어서 생성 시간을 조회한다.
header = []
for i in range(n - 999, n + 1):
    bHash = p.getblockhash(i)
    hdr = p.getblockheader(bHash)
    height = hdr['height']
    btime = hdr['time']
    bhash = hdr['hash']
    header.append([height, btime, bhash])

df = pd.DataFrame(header, columns=['Height', 'Time', 'Hash'])
sdf = df.sort_values('Time')
sdf = sdf.reset_index()
print(df.to_string())
print('총 %d 개 블록 헤더를 읽어왔습니다.' % len(df))

# 블록 생성 소요 시간 분포 관찰
mtime = sdf['Time'].diff().values
mtime = mtime[np.logical_not(np.isnan(mtime))]
print("평균 Mining 시간 = %d (초)" % np.mean(mtime))
Esempio n. 5
0
from binascii import unhexlify


def Endian(string):
    a = []
    for j in range(0, len(str(string)), 2):
        a.append(str(string[j:j + 2]))
    a.reverse()
    return "".join(a)


p = RawProxy()

blockheight = int(input())
blockhash = p.getblockhash(blockheight)
block = p.getblockheader(blockhash)

version = Endian(block['versionHex'])
previousHash = Endian(block['previousblockhash'])
merkleRoot = Endian(block['merkleroot'])
time = Endian(hex(block['time'])[2:])
difficultyBits = Endian(block['bits'])
nonce = Endian(hex(block['nonce'])[2:])

header_bin = str(version + previousHash + merkleRoot + time + difficultyBits +
                 nonce)

a = header_bin.decode('hex')

hash = hashlib.sha256(hashlib.sha256(a).digest()).hexdigest()
Esempio n. 6
0
import hashlib
import binascii
from bitcoin.rpc import RawProxy

p = RawProxy()

hash = raw_input("Write block hash:")

header = p.getblockheader(hash)

version = binascii.hexlify(binascii.unhexlify(str(header['versionHex']))[::-1])
prevBlockHash = binascii.hexlify(
    binascii.unhexlify(str(header['previousblockhash']))[::-1])
merkleRoot = binascii.hexlify(
    binascii.unhexlify(str(header['merkleroot']))[::-1])
time = binascii.hexlify(binascii.unhexlify(hex(header['time'])[2:])[::-1])
bits = binascii.hexlify(binascii.unhexlify(str(header['bits']))[::-1])
nonce = binascii.hexlify(binascii.unhexlify(hex(header['nonce'])[2:])[::-1])

headerHex = version + prevBlockHash + merkleRoot + time + bits + nonce

headerTemp = binascii.unhexlify(headerHex)

hash = hashlib.sha256(hashlib.sha256(headerTemp).digest()).digest()
hash = binascii.hexlify(hash)

finalResult = binascii.hexlify(binascii.unhexlify(hash)[::-1])

print(finalResult)
print(header['hash'])