Пример #1
0
 def test_python_bech32(self, addr):
     hrp = addr[:4]
     assert_equal(hrp, "bcrt")
     (witver, witprog) = decode(hrp, addr)
     assert_equal(encode(hrp, witver, witprog), addr)
Пример #2
0
    def run_test(self):
        wallet_unenc_dump = os.path.join(self.nodes[0].datadir,
                                         "wallet.unencrypted.dump")
        wallet_enc_dump = os.path.join(self.nodes[0].datadir,
                                       "wallet.encrypted.dump")

        # generate 20 addresses to compare against the dump
        # - 10 legacy P2PKH
        # - 10 bech32
        test_addr_count = 10
        addrs = []
        for address_type in ['legacy', 'bech32']:
            for i in range(0, test_addr_count):
                addr = self.nodes[0].getnewaddress(address_type=address_type)
                vaddr = self.nodes[0].getaddressinfo(
                    addr)  # required to get hd keypath
                addrs.append(vaddr)

        # Test scripts dump by adding a 1-of-1 multisig address
        multisig_addr = self.nodes[0].addmultisigaddress(
            1, [addrs[1]["address"]])["address"]
        p2wsh_addr = self.nodes[0].addwitnessaddress(multisig_addr)

        # Construct the P2WPK address from our generated P2WSH address
        addr_info = self.nodes[0].getaddressinfo(p2wsh_addr)
        p2wpk_addr = segwit_addr.encode(
            p2wsh_addr.split('1')[0], addr_info['witness_version'],
            ripemd160(bytes.fromhex(addr_info['witness_program'])))

        # Refill the keypool. getnewaddress() refills the keypool *before* taking a key from
        # the keypool, so the final call to getnewaddress leaves the keypool with one key below
        # its capacity
        self.nodes[0].keypoolrefill()

        # dump unencrypted wallet
        result = self.nodes[0].dumpwallet(wallet_unenc_dump)
        assert_equal(result['filename'], wallet_unenc_dump)

        found_legacy_addr, found_p2sh_segwit_addr, found_bech32_addr, found_script_addr, found_witscript_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
            read_dump(wallet_unenc_dump, addrs, [multisig_addr], [p2wpk_addr], None)
        assert_equal(found_legacy_addr,
                     test_addr_count)  # all keys must be in the dump
        assert_equal(found_p2sh_segwit_addr, 0)  # p2sh-segwit has been removed
        assert_equal(found_bech32_addr,
                     test_addr_count)  # all keys must be in the dump
        assert_equal(found_script_addr, 1)  # all scripts must be in the dump
        assert_equal(found_witscript_addr,
                     1)  # all witscripts must be in the dump
        assert_equal(found_addr_chg, 0)  # 0 blocks where mined
        assert_equal(found_addr_rsv, 90 * 2)  # 90 keys plus 100% internal keys

        # encrypt wallet, restart, unlock and dump
        self.nodes[0].encryptwallet('test')
        self.nodes[0].walletpassphrase('test', 10)
        # Should be a no-op:
        self.nodes[0].keypoolrefill()
        self.nodes[0].dumpwallet(wallet_enc_dump)

        found_legacy_addr, found_p2sh_segwit_addr, found_bech32_addr, found_script_addr, found_witscript_addr, found_addr_chg, found_addr_rsv, _ = \
            read_dump(wallet_enc_dump, addrs, [multisig_addr], [p2wpk_addr], hd_master_addr_unenc)
        assert_equal(found_legacy_addr,
                     test_addr_count)  # all keys must be in the dump
        assert_equal(found_p2sh_segwit_addr, 0)  # p2sh-segwit has been removed
        assert_equal(found_bech32_addr,
                     test_addr_count)  # all keys must be in the dump
        assert_equal(found_script_addr, 1)
        assert_equal(found_witscript_addr, 1)
        assert_equal(found_addr_chg,
                     90 * 2)  # old reserve keys are marked as change now
        assert_equal(found_addr_rsv, 90 * 2)

        # Overwriting should fail
        assert_raises_rpc_error(
            -8, "already exists",
            lambda: self.nodes[0].dumpwallet(wallet_enc_dump))

        # Restart node with new wallet, and test importwallet
        self.stop_node(0)
        self.start_node(0, ['-wallet=w2'])

        # Make sure the address is not IsMine before import
        result = self.nodes[0].getaddressinfo(multisig_addr)
        assert not result['ismine']

        self.nodes[0].importwallet(wallet_unenc_dump)

        # Now check IsMine is true
        result = self.nodes[0].getaddressinfo(multisig_addr)
        assert result['ismine']