2018-12-18 12:00:46 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
|
2018-09-13 15:34:23 -07:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-09-19 13:41:02 -07:00
|
|
|
from test_framework.util import assert_equal, assert_true, initialize_chain_clean, start_node
|
2018-09-21 17:16:44 -07:00
|
|
|
from test_framework.authproxy import JSONRPCException
|
2018-09-13 15:34:23 -07:00
|
|
|
|
|
|
|
class SignOfflineTest (BitcoinTestFramework):
|
|
|
|
# Setup Methods
|
|
|
|
def setup_chain(self):
|
|
|
|
print "Initializing test directory " + self.options.tmpdir
|
|
|
|
initialize_chain_clean(self.options.tmpdir, 2)
|
|
|
|
|
|
|
|
def setup_network(self):
|
2019-07-19 05:10:13 -07:00
|
|
|
self.nodes = [ start_node(0, self.options.tmpdir, ["-nuparams=2bb40e60:10"]) ]
|
2018-09-13 15:34:23 -07:00
|
|
|
self.is_network_split = False
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Tests
|
|
|
|
def run_test(self):
|
|
|
|
print "Mining blocks..."
|
|
|
|
self.nodes[0].generate(101)
|
|
|
|
|
2019-07-19 05:10:13 -07:00
|
|
|
offline_node = start_node(1, self.options.tmpdir, ["-maxconnections=0", "-nuparams=2bb40e60:10"])
|
2018-09-13 15:34:23 -07:00
|
|
|
self.nodes.append(offline_node)
|
|
|
|
|
|
|
|
assert_equal(0, len(offline_node.getpeerinfo())) # make sure node 1 has no peers
|
|
|
|
|
|
|
|
taddr = self.nodes[0].getnewaddress()
|
|
|
|
|
|
|
|
tx = self.nodes[0].listunspent()[0]
|
|
|
|
txid = tx['txid']
|
|
|
|
scriptpubkey = tx['scriptPubKey']
|
2018-03-10 02:19:44 -08:00
|
|
|
privkeys = [self.nodes[0].dumpprivkey(tx['address'])]
|
2018-09-13 15:34:23 -07:00
|
|
|
|
|
|
|
create_inputs = [{'txid': txid, 'vout': 0}]
|
|
|
|
sign_inputs = [{'txid': txid, 'vout': 0, 'scriptPubKey': scriptpubkey, 'amount': 10}]
|
|
|
|
|
|
|
|
create_hex = self.nodes[0].createrawtransaction(create_inputs, {taddr: 9.9999})
|
|
|
|
|
2018-09-21 17:16:44 -07:00
|
|
|
# An offline regtest node does not rely on the approx release height of the software
|
|
|
|
# to determine the consensus rules to be used for signing.
|
|
|
|
try:
|
|
|
|
signed_tx = offline_node.signrawtransaction(create_hex, sign_inputs, privkeys)
|
|
|
|
self.nodes[0].sendrawtransaction(signed_tx['hex'])
|
|
|
|
assert(False)
|
|
|
|
except JSONRPCException:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Passing in the consensus branch id resolves the issue for offline regtest nodes.
|
2019-07-19 05:10:13 -07:00
|
|
|
signed_tx = offline_node.signrawtransaction(create_hex, sign_inputs, privkeys, "ALL", "2bb40e60")
|
2018-09-13 15:34:23 -07:00
|
|
|
|
|
|
|
# If we return the transaction hash, then we have have not thrown an error (success)
|
|
|
|
online_tx_hash = self.nodes[0].sendrawtransaction(signed_tx['hex'])
|
|
|
|
assert_true(len(online_tx_hash) > 0)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
SignOfflineTest().main()
|