def test_parse(self): hex_merkle_block = '00000020df3b053dc46f162a9b00c7f0d5124e2676d47bbe7c5d0793a500000000000000ef445fef2ed495c275892206ca533e7411907971013ab83e3b47bd0d692d14d4dc7c835b67d8001ac157e670bf0d00000aba412a0d1480e370173072c9562becffe87aa661c1e4a6dbc305d38ec5dc088a7cf92e6458aca7b32edae818f9c2c98c37e06bf72ae0ce80649a38655ee1e27d34d9421d940b16732f24b94023e9d572a7f9ab8023434a4feb532d2adfc8c2c2158785d1bd04eb99df2e86c54bc13e139862897217400def5d72c280222c4cbaee7261831e1550dbb8fa82853e9fe506fc5fda3f7b919d8fe74b6282f92763cef8e625f977af7c8619c32a369b832bc2d051ecd9c73c51e76370ceabd4f25097c256597fa898d404ed53425de608ac6bfe426f6e2bb457f1c554866eb69dcb8d6bf6f880e9a59b3cd053e6c7060eeacaacf4dac6697dac20e4bd3f38a2ea2543d1ab7953e3430790a9f81e1c67f5b58c825acf46bd02848384eebe9af917274cdfbb1a28a5d58a23a17977def0de10d644258d9c54f886d47d293a411cb6226103b55635' mb = MerkleBlock.parse(BytesIO(bytes.fromhex(hex_merkle_block))) version = 0x20000000 self.assertEqual(mb.version, version) merkle_root_hex = 'ef445fef2ed495c275892206ca533e7411907971013ab83e3b47bd0d692d14d4' merkle_root = bytes.fromhex(merkle_root_hex)[::-1] self.assertEqual(mb.merkle_root, merkle_root) prev_block_hex = 'df3b053dc46f162a9b00c7f0d5124e2676d47bbe7c5d0793a500000000000000' prev_block = bytes.fromhex(prev_block_hex)[::-1] self.assertEqual(mb.prev_block, prev_block) timestamp = little_endian_to_int(bytes.fromhex('dc7c835b')) self.assertEqual(mb.timestamp, timestamp) bits = bytes.fromhex('67d8001a') self.assertEqual(mb.bits, bits) nonce = bytes.fromhex('c157e670') self.assertEqual(mb.nonce, nonce) total = little_endian_to_int(bytes.fromhex('bf0d0000')) self.assertEqual(mb.total, total) hex_hashes = [ 'ba412a0d1480e370173072c9562becffe87aa661c1e4a6dbc305d38ec5dc088a', '7cf92e6458aca7b32edae818f9c2c98c37e06bf72ae0ce80649a38655ee1e27d', '34d9421d940b16732f24b94023e9d572a7f9ab8023434a4feb532d2adfc8c2c2', '158785d1bd04eb99df2e86c54bc13e139862897217400def5d72c280222c4cba', 'ee7261831e1550dbb8fa82853e9fe506fc5fda3f7b919d8fe74b6282f92763ce', 'f8e625f977af7c8619c32a369b832bc2d051ecd9c73c51e76370ceabd4f25097', 'c256597fa898d404ed53425de608ac6bfe426f6e2bb457f1c554866eb69dcb8d', '6bf6f880e9a59b3cd053e6c7060eeacaacf4dac6697dac20e4bd3f38a2ea2543', 'd1ab7953e3430790a9f81e1c67f5b58c825acf46bd02848384eebe9af917274c', 'dfbb1a28a5d58a23a17977def0de10d644258d9c54f886d47d293a411cb62261', ] hashes = [bytes.fromhex(h)[::-1] for h in hex_hashes] self.assertEqual(mb.hashes, hashes) flags = bytes.fromhex('b55635') self.assertEqual(mb.flags, flags)
def parse(cls, s): '''Takes a byte stream and parses a merkle block. Returns a Merkle Block object''' # version - 4 bytes, Little-Endian integer version = little_endian_to_int(s.read(4)) # prev_block - 32 bytes, Little-Endian (use [::-1]) prev_block = s.read(32)[::-1] # merkle_root - 32 bytes, Little-Endian (use [::-1]) merkle_root = s.read(32)[::-1] # timestamp - 4 bytes, Little-Endian integer timestamp = little_endian_to_int(s.read(4)) # bits - 4 bytes bits = s.read(4) # nonce - 4 bytes nonce = s.read(4) # total transactions in block - 4 bytes, Little-Endian integer total = little_endian_to_int(s.read(4)) # number of transaction hashes - varint num_hashes = read_varint(s) # each transaction is 32 bytes, Little-Endian hashes = [] for _ in range(num_hashes): hashes.append(s.read(32)[::-1]) # length of flags field - varint flags_length = read_varint(s) # read the flags field flags = s.read(flags_length) # initialize class return cls(version, prev_block, merkle_root, timestamp, bits, nonce, total, hashes, flags)
def parse_header(cls, s): '''Takes a byte stream and parses a block. Returns a Block object''' # s.read(n) will read n bytes from the stream # version - 4 bytes, little endian, interpret as int version = little_endian_to_int(s.read(4)) # prev_block - 32 bytes, little endian (use [::-1] to reverse) prev_block = s.read(32)[::-1] # merkle_root - 32 bytes, little endian (use [::-1] to reverse) merkle_root = s.read(32)[::-1] # timestamp - 4 bytes, little endian, interpret as int timestamp = little_endian_to_int(s.read(4)) # bits - 4 bytes bits = s.read(4) # nonce - 4 bytes nonce = s.read(4) # initialize class return cls(version, prev_block, merkle_root, timestamp, bits, nonce)
def parse(cls, s): # get the length of the entire field length = read_varint(s) # initialize the cmds array cmds = [] # initialize the number of bytes we've read to 0 count = 0 # loop until we've read length bytes while count < length: # get the current byte current = s.read(1) # increment the bytes we've read count += 1 # convert the current byte to an integer current_byte = current[0] # if the current byte is between 1 and 75 inclusive if current_byte >= 1 and current_byte <= 75: # we have an cmd set n to be the current byte n = current_byte # add the next n bytes as an cmd cmds.append(s.read(n)) # increase the count by n count += n elif current_byte == 76: # op_pushdata1 data_length = little_endian_to_int(s.read(1)) cmds.append(s.read(data_length)) count += data_length + 1 elif current_byte == 77: # op_pushdata2 data_length = little_endian_to_int(s.read(2)) cmds.append(s.read(data_length)) count += data_length + 2 else: # we have an opcode. set the current byte to op_code op_code = current_byte # add the op_code to the list of cmds cmds.append(op_code) if count != length: raise SyntaxError('parsing script failed') return cls(cmds)
def parse(cls, s, testnet = False): magic = s.read(4) if magic == b'': raise IOError('Connection reset!') if testnet: expected_magic = TESTNET_NETWORK_MAGIC else: expected_magic = NETWORK_MAGIC if magic != expected_magic: raise SyntaxError('magic is not right {} vs {}'.format(magic.hex(), expected_magic.hex())) command = s.read(12) command = command.strip(b'\x00') payload_length = little_endian_to_int(s.read(4)) checksum = s.read(4) payload = s.read(payload_length) calculated_checksum = hash256(payload)[:4] if calculated_checksum != checksum: raise IOError('checksum does not match') return cls(command, payload, testnet = testnet)
from helper.helper import hash160, decode_base58, little_endian_to_int, hash256, int_to_little_endian from Scripts.Script import p2pkh_script, p2sh_script, atomic_swap_output_script from Transaction.Tx import Tx, TxIn, TxOut import requests import json from crypto.Signature import PrivateKey from Transaction.Tx import TxFetcher from AtomicSwap.bob import bob_addr_btc import time from AtomicSwap.util import broadcast_tx alice_secret_atomic_swap = "thisIsASecretPassword123".encode('utf-8') len_alice_secret = len(alice_secret_atomic_swap) print("Alice's secret is: ", alice_secret_atomic_swap) alice_secret = little_endian_to_int(hash256(b'Alice')) alice_private_key = PrivateKey(secret=alice_secret) alice_public_key = alice_private_key.point.sec() alice_addr_btc = 'n4mNGWL1ycLk45xgvEEn8V2uKbnpa7wr93' fee = int(0.00005 * 100000000) pay_amount = int(0.001 * 100000000) lock_time = 3600 + int(time.time()) alice_locktime = lock_time bob_locktime = lock_time def hash_of_secret(): return hash160(alice_secret_atomic_swap)
def check_pow(self): sha = hash256(self.serialize()) proof = little_endian_to_int(sha) return proof < self.target()
def bits_to_target(bits): exponent = bits[-1] coefficient = little_endian_to_int(bits[:-1]) return coefficient * 256 ** (exponent - 3)
from helper.helper import little_endian_to_int, hash256, decode_base58 from crypto.Signature import PrivateKey secret_Abel = little_endian_to_int(hash256(b'*****@*****.**')) private_key_Abel = PrivateKey(secret=secret_Abel) # private_key: cUz2mXMT2jyqdwXuFeZgy71tvF96vnvAmH8KvtsMdzhY2AtrJUJv addr_Abel = private_key_Abel.point.address(testnet=True) # my_addr = 'n4kJxHoZC9uTjfYAmeXSnHP9ss6YqiQja8' h160_Abel = decode_base58(addr_Abel) print("Abel's private key is: ", private_key_Abel) print("Abel's address is: ", addr_Abel) secret_Alice = little_endian_to_int(hash256(b'Alice')) private_key_Alice = PrivateKey(secret=secret_Alice) # private_key: cUz2mXMT2jyqdwXuFeZgy71tvF96vnvAmH8KvtsMdzhY2AtrJUJv addr_Alice = private_key_Alice.point.address(testnet=True) # my_addr = 'n4kJxHoZC9uTjfYAmeXSnHP9ss6YqiQja8' h160_Alice = decode_base58(addr_Alice) print("Alice's private key is: ", private_key_Alice) print("Alice's address is: ", addr_Alice) secret_Bob = little_endian_to_int(hash256(b'Bob')) private_key_Bob = PrivateKey(secret=secret_Bob) # private_key: cUz2mXMT2jyqdwXuFeZgy71tvF96vnvAmH8KvtsMdzhY2AtrJUJv addr_Bob = private_key_Bob.point.address(testnet=True) # my_addr = 'n4kJxHoZC9uTjfYAmeXSnHP9ss6YqiQja8' h160_Bob = decode_base58(addr_Bob) print("Bob's private key is: ", private_key_Bob) print("Bob's address is: ", addr_Bob)
from helper.helper import hash160, decode_base58, little_endian_to_int, hash256 from Scripts.Script import p2pkh_script from crypto.Signature import PrivateKey from Transaction.Tx import Tx, TxIn, TxOut from AtomicSwap.util import broadcast_tx bob_secret = little_endian_to_int(hash256(b'Bob')) bob_private_key = PrivateKey(secret=bob_secret) alice_public_key = bob_private_key.point.sec() bob_addr_btc = 'mtP5yN9q6bFdiWqwcDJsiobPBVVFdwQTvR' fee = int(0.00005 * 100000000) pay_amount = int(0.001 * 100000000) alice_secret = "thisIsASecretPassword123".encode('utf-8') len_alice_secret_atomic_swap = len(alice_secret) def bob_redeem_tx(amount_to_send, alice_tx_id, alice_secret): txout_h160 = decode_base58(bob_addr_btc) # hash160 of the public key txout_script = p2pkh_script(txout_h160) tx_out = TxOut(amount_to_send, txout_script) tx_in = TxIn(bytes.fromhex(alice_tx_id), 1) tx_obj = Tx(1, [tx_in], [tx_out], 0, testnet=True) tx_obj.sign_input_atomic_swap_true_branch(alice_secret, 0, bob_private_key) if tx_obj.verify(): print('Bob redeem from swap tx (BTC) created successfully!') return tx_obj if __name__ == '__main__':