def test_from_hex(self):
        block_hex = read_test_data("genesis_block.txt")
        block = Block.from_hex(block_hex)
        self.assertEqual(1, block.n_transactions)
        block_hash = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1" \
                     "b60a8ce26f"
        self.assertEqual(block_hash, block.hash)
        self.assertEqual(486604799, block.header.bits)
        merkle_root = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127" \
                      "b7afdeda33b"
        self.assertEqual(merkle_root, block.header.merkle_root)
        self.assertEqual(2083236893, block.header.nonce)
        self.assertEqual(1, block.header.version)
        self.assertEqual(1, block.header.difficulty)
        self.assertEqual(285, block.size)
        self.assertEqual(datetime.utcfromtimestamp(1231006505),
                         block.header.timestamp)
        self.assertEqual("0" * 64, block.header.previous_block_hash)

        for tx in block.transactions:
            self.assertEqual(1, tx.version)
            tx_hash = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127" \
                      "b7afdeda33b"
            self.assertEqual(tx_hash, tx.hash)
            self.assertEqual(204, tx.size)
            self.assertEqual(0, tx.locktime)
            self.assertEqual(0xffffffff, tx.inputs[0].transaction_index)
            self.assertEqual(0xffffffff, tx.inputs[0].sequence_number)
            self.assertTrue("ffff001d" in tx.inputs[0].script.value)
            self.assertEqual("0" * 64, tx.inputs[0].transaction_hash)
            self.assertEqual(50 * 100000000, tx.outputs[0].value)
Пример #2
0
    async def handle(self):
        global current_block_count

        msg = await self.zmqSubSocket.recv_multipart()
        topic = msg[0]
        body = msg[1]
        sequence = "Unknown"

        if len(msg[-1]) == 4:
            msgSequence = struct.unpack('<I', msg[-1])[-1]
            sequence = str(msgSequence)

        if topic == b"rawblock":
            try:
                block = Block(body, current_block_count + 1)
                current_block_count += 1

                documents = []
                d_txids = []
                for tx in block.transactions:
                    res = tna.extract(block, tx)
                    #print(json.dumps(res, indent=4))
                    documents.append(res)
                    d_txids.append(tx.hash)

                print("height={} inserted={}".format(
                    block.height,
                    len(db.confirmed.insert_many(documents).inserted_ids)))
                db.meta.update({'column': 'last_block'},
                               {"height": block.height},
                               upsert=True)

                for txid in d_txids:
                    db.unconfirmed.delete_one({'tx': {'h': txid}})
            except AssertionError:
                print('error: rawblock')

        elif topic == b"rawtx":
            try:
                tx = Transaction.from_hex(body)
                res = tna.extract(None, tx)
                db.unconfirmed.insert_one(res)
                print('inserted tx: {}'.format(tx.hash))
            except:
                print('error: rawtx')
        # schedule to receive next message
        asyncio.ensure_future(self.handle())
Пример #3
0
    def test_from_hex(self):
        block_str = "0100000000000000000000000000000000000000000000000000000" \
                    "000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81b" \
                    "c3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c01010" \
                    "0000001000000000000000000000000000000000000000000000000" \
                    "0000000000000000ffffffff4d04ffff001d0104455468652054696" \
                    "d65732030332f4a616e2f32303039204368616e63656c6c6f72206f" \
                    "6e206272696e6b206f66207365636f6e64206261696c6f757420666" \
                    "f722062616e6b73ffffffff0100f2052a01000000434104678afdb0" \
                    "fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb" \
                    "649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c70" \
                    "2b6bf11d5fac00000000"
        block_hex = a2b_hex(block_str)
        block = Block.from_hex(block_hex)
        self.assertEqual(1, block.n_transactions)
        block_hash = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1" \
                     "b60a8ce26f"
        self.assertEqual(block_hash, block.hash)
        self.assertEqual(486604799, block.header.bits)
        merkle_root = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127" \
                      "b7afdeda33b"
        self.assertEqual(merkle_root, block.header.merkle_root)
        self.assertEqual(2083236893, block.header.nonce)
        self.assertEqual(1, block.header.version)
        self.assertEqual(1, block.header.difficulty)
        self.assertEqual(285, block.size)
        self.assertEqual(datetime.utcfromtimestamp(1231006505),
                         block.header.timestamp)
        self.assertEqual("0" * 64, block.header.previous_block_hash)

        for tx in block.transactions:
            self.assertEqual(1, tx.version)
            tx_hash = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127" \
                      "b7afdeda33b"
            self.assertEqual(tx_hash, tx.hash)
            self.assertEqual(204, tx.size)
            self.assertEqual(0, tx.locktime)
            self.assertEqual(0xffffffff, tx.inputs[0].transaction_index)
            self.assertEqual(0xffffffff, tx.inputs[0].sequence_number)
            self.assertTrue("ffff001d" in tx.inputs[0].script.value)
            self.assertEqual("0" * 64, tx.inputs[0].transaction_hash)
            self.assertEqual(50 * 100000000, tx.outputs[0].value)
Пример #4
0
# reload(sys)
# sys.setdefaultencoding('utf8')
path_list = os.listdir("./")
filelist = []
for filename in path_list:
    if os.path.splitext(filename)[1] == ".dat":
        filelist.append(filename)
# print(filelist)

# for i in filelist:
#     print(i)
#     blk = get_blocks(i)

blk = get_blocks('./../blk00000.dat')
for raw_block in blk:
    b = Block(raw_block)
    bheader = BlockHeader(raw_block)
    # print(block.hash)
    b2 = get_block(b.hash)

    a = {
        "hash": b.hash,
        "basic": {
            "prev_block": bheader.previous_block_hash,
            "mrkl_root": bheader.merkle_root,
            "height": b2.height,
            "time": str(bheader.timestamp),
            "n_tx": b.n_transactions,
            "main_chain": True,
            "fee": b2.fee,
            "nonce": bheader.nonce,
Пример #5
0
import os
from blockchain_parser.block import Block
from blockchain_parser.transaction import double_sha256, format_hash, Transaction

with open('block.dat', 'rb') as f:
    data = f.read()

block = Block(data)
nonce = block.header.nonce

print("FLAG{%i}" % block.header.nonce)

transactions = block.transactions

print("FLAG{%i}" % sum(map(lambda t: t.locktime, transactions)))
Пример #6
0
import os
import json
import base64
import argparse
import binascii
from dotenv import load_dotenv
from blockchain_parser.block import Block
from blockchain_parser.transaction import Transaction
import bitcoin.rpc
import tna

load_dotenv()

btc_conf_file = os.path.expanduser(os.getenv("BTC_CONF_FILE"))

parser = argparse.ArgumentParser(description="render a tx with tna")
parser.add_argument("--txid", type=str, required=True, help="txid to look up")
args = parser.parse_args()

rpc = bitcoin.rpc.RawProxy(btc_conf_file=btc_conf_file)

raw_tx_json = rpc.getrawtransaction(args.txid, 1)

raw_block_json = rpc.getblock(raw_tx_json['blockhash'])
raw_block = binascii.unhexlify(rpc.getblock(raw_tx_json['blockhash'], False))
block = Block(raw_block, raw_block_json['height'])

tx = Transaction.from_hex(binascii.unhexlify(raw_tx_json['hex']))
res = tna.extract(block, tx)
print(json.dumps(res, sort_keys=True, indent=4))
Пример #7
0
if not args.dry:
    print('deleted {} txs from gte block {}'.format(
        util.delete_txs_gte(db, args.start_block),
        args.start_block
    ))

for height in range(args.start_block, args.end_block):
    try:
        block_hash = rpc.getblockhash(height)
    except bitcoin.rpc.JSONRPCError as e:
        print('cannot get block hash, breaking')
        break

    raw_block  = binascii.unhexlify(rpc.getblock(block_hash, False)) # set to 0 to get raw hex data

    block = Block(raw_block, height=height)

    documents = []
    for tx in block.transactions:
        res = tna.extract(block, tx)
        if args.verbose:
            print(json.dumps(res, indent=4))
        documents.append(res)

    if args.dry:
        inserted = 0
    else:
        inserted = len(db.confirmed.insert_many(documents).inserted_ids)
        util.meta_update_last_block_height(db, height)

    print("{}%\theight={} inserted={}".format(
Пример #8
0
    def load(cls, block_index, seq, id):
        #print(len(rawHex))
        block = Block.from_hex(rawHex)

        print(block.header.timestamp)
        print(block.hash)
        print(block.height)

        with open(dir_path + '/files/header' + str(id) + '.txt',
                  'a') as header_file:
            with open(dir_path + '/files/tx' + str(id) + '.txt',
                      'a') as tx_file:
                with open(dir_path + '/files/input' + str(id) + '.txt',
                          'a') as inp_file:
                    with open(dir_path + '/files/output' + str(id) + '.txt',
                              'a') as outp_file:
                        with open(dir_path + '/files/addr' + str(id) + '.txt',
                                  'a') as addr_file:

                            print("{},{},{},{},{},{},{},{},{},{},{},{}".format(
                                seq.header_seq, block.header.version,
                                block.header.previous_block_hash,
                                block.header.merkle_root,
                                block.header.timestamp, block.header.bits,
                                block.header.nonce, block.n_transactions,
                                block.hash, block.height if block.height
                                is not None else '', block_index, block.size),
                                  file=header_file)

                            for tx in block.transactions:
                                print("{},{},{},{},{},{},{},{}".format(
                                    seq.header_seq, seq.tx_seq, tx.hash,
                                    tx.version, tx.n_inputs, tx.n_outputs,
                                    tx.locktime,
                                    "Y" if tx.is_coinbase() else "N"),
                                      file=tx_file)

                                i = 0
                                for inp in tx.inputs:
                                    print("{},{},{},{},{},{},{},{},{}".format(
                                        seq.header_seq, seq.tx_seq,
                                        seq.inp_seq, i, inp.transaction_hash,
                                        inp.transaction_index,
                                        inp._script_length,
                                        inp.sequence_number,
                                        b2a_hex(
                                            inp.script.hex).decode('utf8')),
                                          file=inp_file)

                                    i += 1
                                    seq.inp_seq += 1

                                i = 0
                                for outp in tx.outputs:
                                    print("{},{},{},{},{},{},{},{},{}".format(
                                        seq.header_seq, seq.tx_seq,
                                        seq.outp_seq, tx.hash, i, outp.value,
                                        outp._script_length, outp.type,
                                        b2a_hex(
                                            outp.script.hex).decode('utf8')),
                                          file=outp_file)

                                    j = 0
                                    for addr in outp.addresses:
                                        print("{},{},{},{},{},{},{},{},{},{}".
                                              format(
                                                  seq.header_seq, seq.tx_seq,
                                                  seq.outp_seq, seq.addr_seq,
                                                  tx.hash, i, j,
                                                  b2a_hex(addr.public_key
                                                          ).decode('utf8')
                                                  if addr.public_key
                                                  is not None else '',
                                                  addr.address, addr.type),
                                              file=addr_file)
                                        j += 1
                                        seq.addr_seq += 1
                                    i += 1
                                    seq.outp_seq += 1
                                seq.tx_seq += 1
                            seq.header_seq += 1