|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test transaction signing using the signrawtransaction* RPCs."""
|
6 | 6 |
|
7 |
| -from test_framework.address import check_script, script_to_p2sh |
| 7 | +from test_framework.address import check_script, script_to_p2sh, script_to_p2wsh |
8 | 8 | from test_framework.key import ECKey
|
9 | 9 | from test_framework.test_framework import BitcoinTestFramework
|
10 | 10 | from test_framework.util import assert_equal, assert_raises_rpc_error, find_vout_for_address, hex_str_to_bytes
|
11 |
| -from test_framework.messages import sha256 |
12 |
| -from test_framework.script import CScript, OP_0, OP_CHECKSIG |
| 11 | +from test_framework.messages import sha256, CTransaction, CTxInWitness |
| 12 | +from test_framework.script import CScript, OP_0, OP_CHECKSIG, OP_CHECKSEQUENCEVERIFY, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_TRUE |
13 | 13 | from test_framework.script_util import key_to_p2pkh_script, script_to_p2sh_p2wsh_script, script_to_p2wsh_script
|
14 | 14 | from test_framework.wallet_util import bytes_to_wif
|
15 | 15 |
|
16 |
| -from decimal import Decimal |
| 16 | +from decimal import Decimal, getcontext |
| 17 | +from io import BytesIO |
17 | 18 |
|
18 | 19 | class SignRawTransactionsTest(BitcoinTestFramework):
|
19 | 20 | def set_test_params(self):
|
@@ -238,13 +239,87 @@ def OP_1NEGATE_test(self):
|
238 | 239 | txn = self.nodes[0].signrawtransactionwithwallet(hex_str, prev_txs)
|
239 | 240 | assert txn["complete"]
|
240 | 241 |
|
| 242 | + def test_signing_with_csv(self): |
| 243 | + self.log.info("Test signing a transaction containing a fully signed CSV input") |
| 244 | + self.nodes[0].walletpassphrase("password", 9999) |
| 245 | + getcontext().prec = 8 |
| 246 | + |
| 247 | + # Make sure CSV is active |
| 248 | + self.nodes[0].generate(500) |
| 249 | + |
| 250 | + # Create a P2WSH script with CSV |
| 251 | + script = CScript([1, OP_CHECKSEQUENCEVERIFY, OP_DROP]) |
| 252 | + address = script_to_p2wsh(script) |
| 253 | + |
| 254 | + # Fund that address and make the spend |
| 255 | + txid = self.nodes[0].sendtoaddress(address, 1) |
| 256 | + vout = find_vout_for_address(self.nodes[0], txid, address) |
| 257 | + self.nodes[0].generate(1) |
| 258 | + utxo = self.nodes[0].listunspent()[0] |
| 259 | + amt = Decimal(1) + utxo["amount"] - Decimal(0.00001) |
| 260 | + tx = self.nodes[0].createrawtransaction( |
| 261 | + [{"txid": txid, "vout": vout, "sequence": 1},{"txid": utxo["txid"], "vout": utxo["vout"]}], |
| 262 | + [{self.nodes[0].getnewaddress(): amt}], |
| 263 | + self.nodes[0].getblockcount() |
| 264 | + ) |
| 265 | + |
| 266 | + # Set the witness script |
| 267 | + ctx = CTransaction() |
| 268 | + ctx.deserialize(BytesIO(hex_str_to_bytes(tx))) |
| 269 | + ctx.wit.vtxinwit.append(CTxInWitness()) |
| 270 | + ctx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), script] |
| 271 | + tx = ctx.serialize_with_witness().hex() |
| 272 | + |
| 273 | + # Sign and send the transaction |
| 274 | + signed = self.nodes[0].signrawtransactionwithwallet(tx) |
| 275 | + assert_equal(signed["complete"], True) |
| 276 | + self.nodes[0].sendrawtransaction(signed["hex"]) |
| 277 | + |
| 278 | + def test_signing_with_cltv(self): |
| 279 | + self.log.info("Test signing a transaction containing a fully signed CLTV input") |
| 280 | + self.nodes[0].walletpassphrase("password", 9999) |
| 281 | + getcontext().prec = 8 |
| 282 | + |
| 283 | + # Make sure CSV is active |
| 284 | + self.nodes[0].generate(1500) |
| 285 | + |
| 286 | + # Create a P2WSH script with CLTV |
| 287 | + script = CScript([1000, OP_CHECKLOCKTIMEVERIFY, OP_DROP]) |
| 288 | + address = script_to_p2wsh(script) |
| 289 | + |
| 290 | + # Fund that address and make the spend |
| 291 | + txid = self.nodes[0].sendtoaddress(address, 1) |
| 292 | + vout = find_vout_for_address(self.nodes[0], txid, address) |
| 293 | + self.nodes[0].generate(1) |
| 294 | + utxo = self.nodes[0].listunspent()[0] |
| 295 | + amt = Decimal(1) + utxo["amount"] - Decimal(0.00001) |
| 296 | + tx = self.nodes[0].createrawtransaction( |
| 297 | + [{"txid": txid, "vout": vout},{"txid": utxo["txid"], "vout": utxo["vout"]}], |
| 298 | + [{self.nodes[0].getnewaddress(): amt}], |
| 299 | + self.nodes[0].getblockcount() |
| 300 | + ) |
| 301 | + |
| 302 | + # Set the witness script |
| 303 | + ctx = CTransaction() |
| 304 | + ctx.deserialize(BytesIO(hex_str_to_bytes(tx))) |
| 305 | + ctx.wit.vtxinwit.append(CTxInWitness()) |
| 306 | + ctx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), script] |
| 307 | + tx = ctx.serialize_with_witness().hex() |
| 308 | + |
| 309 | + # Sign and send the transaction |
| 310 | + signed = self.nodes[0].signrawtransactionwithwallet(tx) |
| 311 | + assert_equal(signed["complete"], True) |
| 312 | + self.nodes[0].sendrawtransaction(signed["hex"]) |
| 313 | + |
241 | 314 | def run_test(self):
|
242 | 315 | self.successful_signing_test()
|
243 | 316 | self.script_verification_error_test()
|
244 | 317 | self.witness_script_test()
|
245 | 318 | self.OP_1NEGATE_test()
|
246 | 319 | self.test_with_lock_outputs()
|
247 | 320 | self.test_fully_signed_tx()
|
| 321 | + self.test_signing_with_csv() |
| 322 | + self.test_signing_with_cltv() |
248 | 323 |
|
249 | 324 |
|
250 | 325 | if __name__ == '__main__':
|
|
0 commit comments