|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2015-2017 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test transaction signing using the signrawtransaction* RPCs.""" |
| 6 | + |
| 7 | +from test_framework.test_framework import BitcoinTestFramework |
| 8 | +import decimal |
| 9 | + |
| 10 | +class RpcCreateMultiSigTest(BitcoinTestFramework): |
| 11 | + def set_test_params(self): |
| 12 | + self.setup_clean_chain = True |
| 13 | + self.num_nodes = 3 |
| 14 | + |
| 15 | + def get_keys(self): |
| 16 | + node0,node1,node2 = self.nodes |
| 17 | + self.add = [node1.getnewaddress() for _ in range(self.nkeys)] |
| 18 | + self.pub = [node1.getaddressinfo(a)["pubkey"] for a in self.add] |
| 19 | + self.priv = [node1.dumpprivkey(a) for a in self.add] |
| 20 | + self.final = node2.getnewaddress() |
| 21 | + |
| 22 | + def run_test(self): |
| 23 | + node0,node1,node2 = self.nodes |
| 24 | + |
| 25 | + # 50 BTC each, rest will be 25 BTC each |
| 26 | + node0.generate(149) |
| 27 | + self.sync_all() |
| 28 | + |
| 29 | + self.moved = 0 |
| 30 | + for self.nkeys in [3,5]: |
| 31 | + for self.nsigs in [2,3]: |
| 32 | + for self.output_type in ["bech32", "p2sh-segwit", "legacy"]: |
| 33 | + self.get_keys() |
| 34 | + self.do_multisig() |
| 35 | + |
| 36 | + self.checkbalances() |
| 37 | + |
| 38 | + def checkbalances(self): |
| 39 | + node0,node1,node2 = self.nodes |
| 40 | + node0.generate(100) |
| 41 | + self.sync_all() |
| 42 | + |
| 43 | + bal0 = node0.getbalance() |
| 44 | + bal1 = node1.getbalance() |
| 45 | + bal2 = node2.getbalance() |
| 46 | + |
| 47 | + height = node0.getblockchaininfo()["blocks"] |
| 48 | + assert 150 < height < 350 |
| 49 | + total = 149*50 + (height-149-100)*25 |
| 50 | + assert bal1 == 0 |
| 51 | + assert bal2 == self.moved |
| 52 | + assert bal0+bal1+bal2 == total |
| 53 | + |
| 54 | + def do_multisig(self): |
| 55 | + node0,node1,node2 = self.nodes |
| 56 | + |
| 57 | + msig = node2.createmultisig(self.nsigs, self.pub, self.output_type) |
| 58 | + madd = msig["address"] |
| 59 | + mredeem = msig["redeemScript"] |
| 60 | + if self.output_type == 'bech32': |
| 61 | + assert madd[0:4] == "bcrt" # actually a bech32 address |
| 62 | + |
| 63 | + # compare against addmultisigaddress |
| 64 | + msigw = node1.addmultisigaddress(self.nsigs, self.pub, None, self.output_type) |
| 65 | + maddw = msigw["address"] |
| 66 | + mredeemw = msigw["redeemScript"] |
| 67 | + # addmultisigiaddress and createmultisig work the same |
| 68 | + assert maddw == madd |
| 69 | + assert mredeemw == mredeem |
| 70 | + |
| 71 | + txid = node0.sendtoaddress(madd, 40) |
| 72 | + |
| 73 | + tx = node0.getrawtransaction(txid, True) |
| 74 | + vout = [v["n"] for v in tx["vout"] if madd in v["scriptPubKey"].get("addresses",[])] |
| 75 | + assert len(vout) == 1 |
| 76 | + vout = vout[0] |
| 77 | + scriptPubKey = tx["vout"][vout]["scriptPubKey"]["hex"] |
| 78 | + value = tx["vout"][vout]["value"] |
| 79 | + prevtxs = [{"txid": txid, "vout": vout, "scriptPubKey": scriptPubKey, "redeemScript": mredeem, "amount": value}] |
| 80 | + |
| 81 | + node0.generate(1) |
| 82 | + |
| 83 | + outval = value - decimal.Decimal("0.00001000") |
| 84 | + rawtx = node2.createrawtransaction([{"txid": txid, "vout": vout}], [{self.final: outval}]) |
| 85 | + |
| 86 | + rawtx2 = node2.signrawtransactionwithkey(rawtx, self.priv[0:self.nsigs-1], prevtxs) |
| 87 | + rawtx3 = node2.signrawtransactionwithkey(rawtx2["hex"], [self.priv[-1]], prevtxs) |
| 88 | + |
| 89 | + self.moved += outval |
| 90 | + tx = node0.sendrawtransaction(rawtx3["hex"], True) |
| 91 | + blk = node0.generate(1)[0] |
| 92 | + assert tx in node0.getblock(blk)["tx"] |
| 93 | + |
| 94 | + txinfo = node0.getrawtransaction(tx, True, blk) |
| 95 | + self.log.info("n/m=%d/%d %s size=%d vsize=%d weight=%d" % (self.nsigs, self.nkeys, self.output_type, txinfo["size"], txinfo["vsize"], txinfo["weight"])) |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + RpcCreateMultiSigTest().main() |
0 commit comments