|
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 script_to_p2sh |
7 | 8 | from test_framework.test_framework import BitcoinTestFramework |
8 | | -from test_framework.util import ( |
9 | | - assert_equal, |
10 | | - assert_raises_rpc_error, |
11 | | -) |
| 9 | +from test_framework.util import assert_equal, assert_raises_rpc_error, find_vout_for_address |
| 10 | +from test_framework.script import CScript, OP_CHECKSEQUENCEVERIFY, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_TRUE |
12 | 11 |
|
| 12 | +from decimal import Decimal, getcontext |
13 | 13 |
|
14 | 14 | class SignRawTransactionsTest(BitcoinTestFramework): |
15 | 15 | def set_test_params(self): |
@@ -153,12 +153,78 @@ def OP_1NEGATE_test(self): |
153 | 153 | txn = self.nodes[0].signrawtransactionwithwallet(hex_str, prev_txs) |
154 | 154 | assert txn["complete"] |
155 | 155 |
|
| 156 | + def test_signing_with_csv(self): |
| 157 | + self.log.info("Test signing a transaction containing a fully signed CSV input") |
| 158 | + self.nodes[0].walletpassphrase("password", 9999) |
| 159 | + getcontext().prec = 8 |
| 160 | + |
| 161 | + # Make sure CSV is active |
| 162 | + self.generate(self.nodes[0], 500) |
| 163 | + |
| 164 | + # Create a P2SH script with CSV |
| 165 | + script = CScript([1, OP_CHECKSEQUENCEVERIFY, OP_DROP, OP_TRUE]) |
| 166 | + address = script_to_p2sh(script) |
| 167 | + |
| 168 | + # Fund that address and make the spend |
| 169 | + txid = self.nodes[0].sendtoaddress(address, 1) |
| 170 | + vout = find_vout_for_address(self.nodes[0], txid, address) |
| 171 | + self.generate(self.nodes[0], 1) |
| 172 | + utxo = self.nodes[0].listunspent()[0] |
| 173 | + amt = Decimal(1) + utxo["amount"] - Decimal(0.00001) |
| 174 | + tx = self.nodes[0].createrawtransaction( |
| 175 | + [{"txid": txid, "vout": vout, "sequence": 1},{"txid": utxo["txid"], "vout": utxo["vout"]}], |
| 176 | + [{self.nodes[0].getnewaddress(): amt}], |
| 177 | + self.nodes[0].getblockcount() |
| 178 | + ) |
| 179 | + |
| 180 | + # Sign and send the transaction |
| 181 | + self.nodes[0].importaddress(script.hex(), p2sh=True) |
| 182 | + signed = self.nodes[0].signrawtransactionwithwallet(tx) |
| 183 | + assert_equal(signed["complete"], True) |
| 184 | + self.nodes[0].sendrawtransaction(signed["hex"]) |
| 185 | + |
| 186 | + def test_signing_with_cltv(self): |
| 187 | + self.log.info("Test signing a transaction containing a fully signed CLTV input") |
| 188 | + self.nodes[0].walletpassphrase("password", 9999) |
| 189 | + getcontext().prec = 8 |
| 190 | + |
| 191 | + # Make sure CSV is active |
| 192 | + self.generate(self.nodes[0], 1500) |
| 193 | + |
| 194 | + # Create a P2SH script with CLTV |
| 195 | + script = CScript([1000, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_TRUE]) |
| 196 | + address = script_to_p2sh(script) |
| 197 | + |
| 198 | + # Fund that address and make the spend |
| 199 | + txid = self.nodes[0].sendtoaddress(address, 1) |
| 200 | + vout = find_vout_for_address(self.nodes[0], txid, address) |
| 201 | + self.generate(self.nodes[0], 1) |
| 202 | + utxo = self.nodes[0].listunspent()[0] |
| 203 | + amt = Decimal(1) + utxo["amount"] - Decimal(0.00001) |
| 204 | + tx = self.nodes[0].createrawtransaction( |
| 205 | + [{"txid": txid, "vout": vout},{"txid": utxo["txid"], "vout": utxo["vout"]}], |
| 206 | + [{self.nodes[0].getnewaddress(): amt}], |
| 207 | + self.nodes[0].getblockcount() |
| 208 | + ) |
| 209 | + |
| 210 | + # Sign and send the transaction |
| 211 | + self.nodes[0].importaddress(script.hex(), p2sh=True) |
| 212 | + signed = self.nodes[0].signrawtransactionwithwallet(tx) |
| 213 | + assert_equal(signed["complete"], True) |
| 214 | + self.nodes[0].sendrawtransaction(signed["hex"]) |
| 215 | + |
156 | 216 | def run_test(self): |
157 | 217 | self.successful_signing_test() |
158 | 218 | self.script_verification_error_test() |
159 | 219 | self.OP_1NEGATE_test() |
160 | 220 | self.test_with_lock_outputs() |
161 | 221 | self.test_fully_signed_tx() |
| 222 | + # Descriptor wallets enforce stricter separation between wallets with and without private |
| 223 | + # key while these tests require spending P2SH and non-P2SH inputs, which cannot reside in |
| 224 | + # the same wallet, skip. |
| 225 | + if not self.options.descriptors: |
| 226 | + self.test_signing_with_csv() |
| 227 | + self.test_signing_with_cltv() |
162 | 228 |
|
163 | 229 |
|
164 | 230 | if __name__ == '__main__': |
|
0 commit comments