def from_raw(cls, raw, block_hash=None, height=None, parse_transactions=False, limit=0, network=DEFAULT_NETWORK): """ Create Block object from raw serialized block in bytes. Get genesis block: >>> from bitcoinlib.services.services import Service >>> srv = Service() >>> b = srv.getblock(0) >>> b.block_hash.hex() '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' :param raw: Raw serialize block :type raw: bytes :param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified. :type block_hash: bytes :param height: Specify height if known. Will be derived from coinbase transaction if not provided. :type height: int :param parse_transactions: Indicate if transactions in raw block need to be parsed and converted to Transaction objects. Default is False :type parse_transactions: bool :param limit: Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True :type limit: int :param network: Name of network :type network: str :return Block: """ block_hash_calc = double_sha256(raw[:80])[::-1] if not block_hash: block_hash = block_hash_calc elif block_hash != block_hash_calc: raise ValueError( "Provided block hash does not correspond to calculated block hash %s" % to_hexstring(block_hash_calc)) version = raw[0:4][::-1] prev_block = raw[4:36][::-1] merkle_root = raw[36:68][::-1] time = raw[68:72][::-1] bits = raw[72:76][::-1] nonce = raw[76:80][::-1] tx_count, size = varbyteint_to_int(raw[80:89]) txs_data = raw[80 + size:] # Parse coinbase transaction so we can extract extra information transactions = [ transaction_deserialize(txs_data, network=network, check_size=False) ] txs_data = txs_data[transactions[0].size:] while parse_transactions and txs_data: if limit != 0 and len(transactions) >= limit: break t = transaction_deserialize(txs_data, network=network, check_size=False) transactions.append(t) # t.rawtx = b'' # traw = t.raw_hex() # tblock = txs_data[:t.size].hex() # if traw != tblock: # print(t.txid) # print(traw) # print(tblock) txs_data = txs_data[t.size:] # TODO: verify transactions, need input value from previous txs # if verify and not t.verify(): # raise ValueError("Could not verify transaction %s in block %s" % (t.txid, block_hash)) if parse_transactions and limit == 0 and tx_count != len(transactions): raise ValueError( "Number of found transactions %d is not equal to expected number %d" % (len(transactions), tx_count)) block = cls(block_hash, version, prev_block, merkle_root, time, bits, nonce, transactions, height, network=network) block.txs_data = txs_data block.tx_count = tx_count return block
def test_varbyteint_to_int_3(self): self.assertEqual(18440744073009551600, varbyteint_to_int(b'\xff\xf0\xd8\x9f\xf9\x07\xaf\xea\xff')[0])
def test_varbyteint_to_int_1(self): self.assertEqual(100, varbyteint_to_int(b'd')[0])
def test_varbyteint_to_int_2(self): self.assertEqual(254, varbyteint_to_int(b'\xfe\xfe\x00')[0])
'981ee0630b32b956b2e6dc3e1c028e6d09db5a72103d2c6d31cabe4025c25879010465f501194b352823c553660d303adfa' \ '9a26ad3c53ae' print(to_hexstring(hash160(to_bytes(redeemscript)))) # # Other type conversions and normalizations # der_signature = '3045022100f952ff1b290c54d8b9fd35573b50f1af235632c595bb2f10b34127fb82f66d18022068b59150f825a81032c' \ '22ce2db091d6fd47294c9e2144fa0291949402e3003ce' print("\n=== Convert DER encoded signature ===") print(convert_der_sig(to_bytes(der_signature))) print("\n=== Varbyte Int conversions ===") print("Number 1000 as Varbyte Integer (hexstring): %s" % to_hexstring(int_to_varbyteint(1000))) print("Converted back (3 is size in bytes: 1 size byte + integer in bytes): ", varbyteint_to_int(to_bytes('fde803'))) # Normalize data print("\n=== Normalizations ===") data = [ u"guion cruz envío papel otoño percha hazaña salir joya gorra íntimo actriz", u'\u2167', u'\uFDFA', "あじわう ちしき たわむれる おくさま しゃそう うんこう ひてい みほん たいほ てのひら りこう わかれる かいすいよく こもん ねもと", '12345', ] for dt in data: print("\nInput data", dt) print("Normalized unicode string (normalize_string): ", normalize_string(dt)) print("Normalized variable (normalize_var): ", normalize_var(dt))