(node_factory, bitcoind, chainparams)
| 288 | |
| 289 | |
| 290 | def test_txprepare(node_factory, bitcoind, chainparams): |
| 291 | amount = 1000000 |
| 292 | l1 = node_factory.get_node(random_hsm=True) |
| 293 | addr = chainparams['example_addr'] |
| 294 | |
| 295 | # Add some funds to withdraw later: both bech32 and p2sh |
| 296 | for i in range(5): |
| 297 | bitcoind.rpc.sendtoaddress(l1.rpc.newaddr()['bech32'], |
| 298 | amount / 10**8) |
| 299 | bitcoind.rpc.sendtoaddress(l1.rpc.newaddr('p2sh-segwit')['p2sh-segwit'], |
| 300 | amount / 10**8) |
| 301 | |
| 302 | bitcoind.generate_block(1) |
| 303 | wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 10) |
| 304 | |
| 305 | prep = l1.rpc.txprepare(outputs=[{addr: Millisatoshi(amount * 3 * 1000)}]) |
| 306 | decode = bitcoind.rpc.decoderawtransaction(prep['unsigned_tx']) |
| 307 | assert decode['txid'] == prep['txid'] |
| 308 | # 4 inputs, 2 outputs (3 if we have a fee output). |
| 309 | assert len(decode['vin']) == 4 |
| 310 | assert len(decode['vout']) == 2 if not chainparams['feeoutput'] else 3 |
| 311 | |
| 312 | # One output will be correct. |
| 313 | outnum = [i for i, o in enumerate(decode['vout']) if o['value'] == Decimal(amount * 3) / 10**8][0] |
| 314 | |
| 315 | for i, o in enumerate(decode['vout']): |
| 316 | if i == outnum: |
| 317 | assert o['scriptPubKey']['type'] == 'witness_v0_keyhash' |
| 318 | assert scriptpubkey_addr(o['scriptPubKey']) == addr |
| 319 | else: |
| 320 | assert o['scriptPubKey']['type'] in ['witness_v0_keyhash', 'fee'] |
| 321 | |
| 322 | # Now prepare one with no change. |
| 323 | prep2 = l1.rpc.txprepare([{addr: 'all'}]) |
| 324 | decode = bitcoind.rpc.decoderawtransaction(prep2['unsigned_tx']) |
| 325 | assert decode['txid'] == prep2['txid'] |
| 326 | # 6 inputs, 1 outputs. |
| 327 | assert len(decode['vin']) == 6 |
| 328 | assert len(decode['vout']) == 1 if not chainparams['feeoutput'] else 2 |
| 329 | |
| 330 | # Some fees will be paid. |
| 331 | assert decode['vout'][0]['value'] < Decimal(amount * 6) / 10**8 |
| 332 | assert decode['vout'][0]['value'] > Decimal(amount * 6) / 10**8 - Decimal(0.0002) |
| 333 | assert decode['vout'][0]['scriptPubKey']['type'] == 'witness_v0_keyhash' |
| 334 | assert scriptpubkey_addr(decode['vout'][0]['scriptPubKey']) == addr |
| 335 | |
| 336 | # If I cancel the first one, I can get those first 4 outputs. |
| 337 | discard = l1.rpc.txdiscard(prep['txid']) |
| 338 | assert discard['txid'] == prep['txid'] |
| 339 | assert discard['unsigned_tx'] == prep['unsigned_tx'] |
| 340 | |
| 341 | prep3 = l1.rpc.txprepare([{addr: 'all'}]) |
| 342 | decode = bitcoind.rpc.decoderawtransaction(prep3['unsigned_tx']) |
| 343 | assert decode['txid'] == prep3['txid'] |
| 344 | # 4 inputs, 1 outputs. |
| 345 | assert len(decode['vin']) == 4 |
| 346 | assert len(decode['vout']) == 1 if not chainparams['feeoutput'] else 2 |
| 347 |
nothing calls this directly
no test coverage detected