def serialize(self): result = self.magic result += self.command + b'\x00' * (12 - len(self.command)) result += int_to_little_endian(len(self.payload), 4) result += hash256(self.payload)[:4] result += self.payload return result
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 hash(self): s = self.serialize() sha = hash256(s) return sha[::-1]
def check_pow(self): sha = hash256(self.serialize()) proof = little_endian_to_int(sha) return proof < self.target()
def op_hash256(stack): if len(stack) < 1: return False element = stack.pop() stack.append(hash256(element)) return True
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__':