-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsend_to_p2wpkh_transaction.py
71 lines (55 loc) · 2.88 KB
/
send_to_p2wpkh_transaction.py
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
# Copyright (C) 2018-2020 The python-litecoin-utils developers
#
# This file is part of python-litecoin-utils
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-litecoin-utils, including this file, may be copied,
# modified, propagated, or distributed except according to the terms contained
# in the LICENSE file.
from litecoinutils.setup import setup
from litecoinutils.utils import to_satoshis
from litecoinutils.transactions import Transaction, TxInput, TxOutput
from litecoinutils.keys import P2wpkhAddress, P2pkhAddress, PrivateKey
from litecoinutils.script import Script
def main():
# always remember to setup the network
setup('testnet')
# send 2 P2PKH inputs to 1 P2WPKH output
# create transaction inputs from tx ids of UTXOs (contained 0.002 tBTC)
txin = TxInput('eddfaa3d5a1c9a2a2961638aa4e28871b09ed9620f9077482248f368d46d8205', 1)
txin2 = TxInput('cf4b2987c06b9dd2ba6770af31a4942a4ea3e7194c0d64e8699e9fda03f50551', 1)
# create transaction output using P2WPKH scriptPubKey (locking script)
addr = P2wpkhAddress('tltc1qedur7y052upuzd7wzh60d2f86szgpuspmml8ce')
txout = TxOutput(to_satoshis(0.0019), addr.to_script_pub_key())
#txout = TxOutput(to_satoshis(0.0019), Script([0, addr.to_hash()]) )
# create transaction from inputs/outputs -- default locktime is used
# note that this is not a segwit transaction since we don't spend segwit
tx = Transaction([txin, txin2], [txout]) #, has_segwit=True)
# print raw transaction
print("\nRaw unsigned transaction:\n" + tx.serialize())
# use the private keys corresponding to the address that contains the
# UTXOs we are trying to spend to sign the input
sk = PrivateKey('cTALNpTpRbbxTCJ2A5Vq88UxT44w1PE2cYqiB3n4hRvzyCev1Wwo')
sk2 = PrivateKey('cVf3kGh6552jU2rLaKwXTKq5APHPoZqCP4GQzQirWGHFoHQ9rEVt')
# note that we pass the scriptPubkey as one of the inputs of sign_input
# because it is used to replace the scriptSig of the UTXO we are trying to
# spend when creating the transaction digest
from_addr = P2pkhAddress('n4bkvTyU1dVdzsrhWBqBw8fEMbHjJvtmJR')
sig = sk.sign_input( tx, 0, Script(['OP_DUP', 'OP_HASH160',
from_addr.to_hash160(), 'OP_EQUALVERIFY',
'OP_CHECKSIG']) )
from_addr2 = P2pkhAddress('mmYNBho9BWQB2dSniP1NJvnPoj5EVWw89w')
sig2 = sk2.sign_input( tx, 1, from_addr2.to_script_pub_key() )
# get public key as hex
pk = sk.get_public_key().to_hex()
pk2 = sk2.get_public_key().to_hex()
# set the scriptSig (unlocking script)
txin.script_sig = Script([sig, pk])
txin2.script_sig = Script([sig2, pk2])
signed_tx = tx.serialize()
# print raw signed transaction ready to be broadcasted
print("\nRaw signed transaction:\n" + signed_tx)
if __name__ == "__main__":
main()