-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbob.py
More file actions
83 lines (67 loc) · 3.67 KB
/
bob.py
File metadata and controls
83 lines (67 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from bitcoin.core import Hash160, b2lx, CMutableTransaction
from bitcoin.core.script import CScript, SignatureHash, SIGHASH_ALL
from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from Q1 import P2PKH_scriptPubKey, P2PKH_scriptSig
from Q4 import coinExchangeScript, coinExchangeScriptSig1, coinExchangeScriptSig2
from lib.config import (alice_public_key_BTC, alice_address_BTC,
bob_secret_key_BTC, bob_public_key_BTC, bob_address_BTC,
alice_public_key_BCY, alice_address_BCY,
bob_secret_key_BCY, bob_public_key_BCY, bob_address_BCY)
from lib.utils import create_txin, create_txout, create_OP_CHECKSIG_signature, create_signed_transaction, broadcast_transaction
#############################################################################
# Bob wants to exchange his BTC in BCY Testnet with Alice's BTC in Testnet3 #
#############################################################################
def bob_swap_tx(txid_to_spend, utxo_index, amount_to_send, hash_of_secret):
txout_script = coinExchangeScript(bob_public_key_BCY, alice_public_key_BCY, hash_of_secret)
txout = create_txout(amount_to_send, txout_script)
txin_scriptPubKey = P2PKH_scriptPubKey(bob_address_BCY)
txin = create_txin(txid_to_spend, utxo_index)
txin_scriptSig = P2PKH_scriptSig(txin, txout, txin_scriptPubKey,
bob_secret_key_BCY, bob_public_key_BCY)
tx = create_signed_transaction(txin, txout, txin_scriptPubKey,
txin_scriptSig)
print('Bob swap tx (BCY) created successfully!')
return tx, txout_script
def return_coins_tx(amount_to_send, last_tx, lock_time):
txin = create_txin(b2lx(last_tx.GetTxid()), 0)
txout = create_txout(amount_to_send, P2PKH_scriptPubKey(bob_address_BCY))
tx = CMutableTransaction([txin], [txout], nLockTime=lock_time)
return tx
def complete_return_tx(return_coins_tx, txin_scriptPubKey, alice_signature_BCY):
bob_signature_BCY = sign_BCY(return_coins_tx, txin_scriptPubKey)
txin = return_coins_tx.vin[0]
txin.scriptSig = CScript(coinExchangeScriptSig2(bob_signature_BCY, alice_signature_BCY))
VerifyScript(txin.scriptSig, CScript(txin_scriptPubKey),
return_coins_tx, 0, (SCRIPT_VERIFY_P2SH,))
print('Bob return coins (BCY) tx created successfully!')
return return_coins_tx
def redeem_swap(amount_to_send, alice_swap_tx, txin_scriptPubKey, alice_secret_x):
txout_script = P2PKH_scriptPubKey(bob_address_BTC)
txout = create_txout(amount_to_send, txout_script)
txin = create_txin(b2lx(alice_swap_tx.GetTxid()), 0)
tx = CMutableTransaction([txin], [txout])
bob_signature_BTC = sign_BTC(tx, txin_scriptPubKey)
txin_scriptSig = coinExchangeScriptSig1(bob_signature_BTC, alice_secret_x)
txin.scriptSig = CScript(txin_scriptSig)
VerifyScript(txin.scriptSig, CScript(txin_scriptPubKey),
tx, 0, (SCRIPT_VERIFY_P2SH,))
print('Bob redeem from swap tx (BTC) created successfully!')
return tx
def sign_BTC(tx, txin_scriptPubKey):
sighash = SignatureHash(CScript(txin_scriptPubKey), tx,
0, SIGHASH_ALL)
sig = bob_secret_key_BTC.sign(sighash) + bytes([SIGHASH_ALL])
return sig
def sign_BCY(tx, txin_scriptPubKey):
sighash = SignatureHash(CScript(txin_scriptPubKey), tx,
0, SIGHASH_ALL)
sig = bob_secret_key_BCY.sign(sighash) + bytes([SIGHASH_ALL])
return sig
def broadcast_BTC(tx):
response = broadcast_transaction(tx, 'btc-test3')
print(response.status_code, response.reason)
print(response.text)
def broadcast_BCY(tx):
response = broadcast_transaction(tx, 'bcy-test')
print(response.status_code, response.reason)
print(response.text)