Exemplo n.º 1
0
    def test_testnet(self):
        # bitcoin core derivation style
        mprv = b'tprv8ZgxMBicQKsPe3g3HwF9xxTLiyc5tNyEtjhBBAk29YA3MTQUqULrmg7aj9qTKNfieuu2HryQ6tGVHse9x7ANFGs3f4HgypMc5nSSoxwf7TK'

        # m/0'/0'/51'
        addr1 = b'mfXYCCsvWPgeCv8ZYGqcubpNLYy5nYHbbj'
        indexes = [0x80000000, 0x80000000, 0x80000000 + 51]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)))
        self.assertEqual(addr, addr1)
        path = "m/0'/0'/51'"
        addr = address_from_xpub(bip32_xpub_from_xprv(bip32_derive(mprv,
                                                                   path)))
        self.assertEqual(addr, addr1)

        # m/0'/1'/150'
        addr2 = b'mfaUnRFxVvf55uD1P3zWXpprN1EJcKcGrb'
        indexes = [0x80000000, 0x80000000 + 1, 0x80000000 + 150]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)))
        self.assertEqual(addr, addr2)
        path = "m/0'/1'/150'"
        addr = address_from_xpub(bip32_xpub_from_xprv(bip32_derive(mprv,
                                                                   path)))
        self.assertEqual(addr, addr2)
Exemplo n.º 2
0
    def test_slip32(self):
        """SLIP32 test vector

        https://github.com/satoshilabs/slips/blob/master/slip-0132.md
        """

        mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
        passphrase = ""

        path = "m/44'/0'/0'"
        prv = b"xprv9xpXFhFpqdQK3TmytPBqXtGSwS3DLjojFhTGht8gwAAii8py5X6pxeBnQ6ehJiyJ6nDjWGJfZ95WxByFXVkDxHXrqu53WCRGypk2ttuqncb"
        pub = b"xpub6BosfCnifzxcFwrSzQiqu2DBVTshkCXacvNsWGYJVVhhawA7d4R5WSWGFNbi8Aw6ZRc1brxMyWMzG3DSSSSoekkudhUd9yLb6qx39T9nMdj"
        address = b"1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA"
        rxprv = bip39.rootxprv_from_mnemonic(mnemonic, passphrase,
                                             bip32.MAIN_xprv)
        mprv = bip32.derive(rxprv, path)
        self.assertEqual(prv, mprv)
        mpub = bip32.xpub_from_xprv(mprv)
        self.assertEqual(pub, mpub)
        pub = bip32.derive(mpub, "./0/0")
        addr = bip32.address_from_xpub(pub)
        self.assertEqual(address, addr)

        path = "m/49'/0'/0'"
        prv = b"yprvAHwhK6RbpuS3dgCYHM5jc2ZvEKd7Bi61u9FVhYMpgMSuZS613T1xxQeKTffhrHY79hZ5PsskBjcc6C2V7DrnsMsNaGDaWev3GLRQRgV7hxF"
        pub = b"ypub6Ww3ibxVfGzLrAH1PNcjyAWenMTbbAosGNB6VvmSEgytSER9azLDWCxoJwW7Ke7icmizBMXrzBx9979FfaHxHcrArf3zbeJJJUZPf663zsP"
        address = b"37VucYSaXLCAsxYyAPfbSi9eh4iEcbShgf"
        rxprv = bip39.rootxprv_from_mnemonic(mnemonic, passphrase,
                                             bip32.MAIN_yprv)
        mprv = bip32.derive(rxprv, path)
        self.assertEqual(prv, mprv)
        mpub = bip32.xpub_from_xprv(mprv)
        self.assertEqual(pub, mpub)
        pub = bip32.derive(mpub, "./0/0")
        addr = bip32.address_from_xpub(pub)
        self.assertEqual(address, addr)
        addr = bip32.p2wpkh_p2sh_address_from_xpub(pub)
        self.assertEqual(address, addr)

        path = "m/84'/0'/0'"
        prv = b"zprvAdG4iTXWBoARxkkzNpNh8r6Qag3irQB8PzEMkAFeTRXxHpbF9z4QgEvBRmfvqWvGp42t42nvgGpNgYSJA9iefm1yYNZKEm7z6qUWCroSQnE"
        pub = b"zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs"
        address = b"bc1qcr8te4kr609gcawutmrza0j4xv80jy8z306fyu"
        rxprv = bip39.rootxprv_from_mnemonic(mnemonic, passphrase,
                                             bip32.MAIN_zprv)
        mprv = bip32.derive(rxprv, path)
        self.assertEqual(prv, mprv)
        mpub = bip32.xpub_from_xprv(mprv)
        self.assertEqual(pub, mpub)
        pub = bip32.derive(mpub, "./0/0")
        addr = bip32.address_from_xpub(pub)
        self.assertEqual(address, addr)
Exemplo n.º 3
0
    def test_wif_address_from_xkey(self):
        seed = b"00" * 32  # better be random
        rxprv = bip32.rootxprv_from_seed(seed)
        path = "m/0h/0h/12"
        xprv = bip32.derive(rxprv, path)
        wif = bip32.wif_from_xprv(xprv)
        self.assertEqual(
            wif, b'KyLk7s6Z1FtgYEVp3bPckPVnXvLUWNCcVL6wNt3gaT96EmzTKZwP')
        address = p2pkh_address_from_wif(wif)
        xpub = bip32.xpub_from_xprv(xprv)
        address2 = bip32.address_from_xpub(xpub)
        self.assertEqual(address, address2)

        self.assertRaises(ValueError, bip32.wif_from_xprv, xpub)
Exemplo n.º 4
0
    def test_vectors(self):
        filename = "electrum_test_vectors.json"
        path_to_filename = path.join(path.dirname(__file__), "./data/",
                                     filename)
        with open(path_to_filename, 'r') as f:
            test_vectors = json.load(f)
        f.closed

        lang = "en"
        for test_vector in test_vectors:
            mnemonic = test_vector[0]
            passphrase = test_vector[1]
            mxprv = test_vector[2]
            mxpub = test_vector[3]
            address = test_vector[4]  # "./0/0"

            if mnemonic != "":
                mxprv2 = electrum.masterxprv_from_mnemonic(
                    mnemonic, passphrase)
                self.assertEqual(mxprv2.decode(), mxprv)

                eversion = electrum.version_from_mnemonic(mnemonic)
                entr = int(electrum.entropy_from_mnemonic(mnemonic, lang), 2)
                mnem = electrum.mnemonic_from_entropy(eversion, entr, lang)
                self.assertEqual(mnem, mnemonic)

            if mxprv != "":
                mxpub2 = bip32.xpub_from_xprv(mxprv)
                self.assertEqual(mxpub2.decode(), mxpub)

            xpub = bip32.derive(mxpub, "./0/0")
            address2 = bip32.address_from_xpub(xpub).decode()
            self.assertEqual(address2, address)

        # version 2fa_segwit
        mnemonic = "slender flight session office noodle hand couple option office wait uniform morning"
        self.assertEqual("2fa_segwit",
                         electrum.version_from_mnemonic(mnemonic))

        # version 2fa
        mnemonic = "history recycle company awful donor fold beef nominee hard bleak bracket six"
        self.assertEqual("2fa", electrum.version_from_mnemonic(mnemonic))
Exemplo n.º 5
0
    def test_altnet(self):
        # non-bitcoin address version
        addr_version = 0x46.to_bytes(1, 'big')

        mprv = b'xprv9s21ZrQH143K2oxHiQ5f7D7WYgXD9h6HAXDBuMoozDGGiYHWsq7TLBj2yvGuHTLSPCaFmUyN1v3fJRiY2A4YuNSrqQMPVLZKt76goL6LP7L'

        # m/0'/0'/5'
        receive = b'VUqyLGVdUADWEqDqL2DeUBAcbPQwZfWDDY'
        indexes = [0x80000000, 0x80000000, 0x80000005]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)), addr_version)
        self.assertEqual(addr, receive)
        path = "m/0'/0'/5'"
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, path)), addr_version)
        self.assertEqual(addr, receive)

        # m/0'/1'/1'
        change = b'VMg6DpX7SQUsoECdpXJ8Bv6R7p11PfwHwy'
        indexes = [0x80000000, 0x80000001, 0x80000001]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)), addr_version)
        self.assertEqual(addr, change)
        path = "m/0'/1'/1'"
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, path)), addr_version)
        self.assertEqual(addr, change)

        xkey_version = PRIVATE[0]
        seed = "5b56c417303faa3fcba7e57400e120a0ca83ec5a4fc9ffba757fbe63fbd77a89a1a3be4c67196f57c39a88b76373733891bfaba16ed27a813ceed498804c0570"
        seed = bytes.fromhex(seed)
        mprv = bip32_master_prvkey_from_seed(seed, xkey_version)
        self.assertEqual(
            mprv,
            b'xprv9s21ZrQH143K3t4UZrNgeA3w861fwjYLaGwmPtQyPMmzshV2owVpfBSd2Q7YsHZ9j6i6ddYjb5PLtUdMZn8LhvuCVhGcQntq5rn7JVMqnie'
        )

        indexes = [0x80000000, 0, 0]  # receive
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)), addr_version)
        self.assertEqual(addr, b'VTpEhLjvGYE16pLcNrMY53gQB9bbhn581W')

        indexes = [0x80000000, 1, 0]  # change
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)), addr_version)
        self.assertEqual(addr, b'VRtaZvAe4s29aB3vuXyq7GYEpahsQet2B1')
Exemplo n.º 6
0
    def test_mainnet(self):
        # bitcoin core derivation style
        mprv = b'xprv9s21ZrQH143K2ZP8tyNiUtgoezZosUkw9hhir2JFzDhcUWKz8qFYk3cxdgSFoCMzt8E2Ubi1nXw71TLhwgCfzqFHfM5Snv4zboSebePRmLS'

        # m/0'/0'/463'
        addr1 = b'1DyfBWxhVLmrJ7keyiHeMbt7N3UdeGU4G5'
        indexes = [0x80000000, 0x80000000, 0x80000000 + 463]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)))
        self.assertEqual(addr, addr1)
        path = "m/0'/0'/463'"
        addr = address_from_xpub(bip32_xpub_from_xprv(bip32_derive(mprv,
                                                                   path)))
        self.assertEqual(addr, addr1)

        # m/0'/0'/267'
        addr2 = b'11x2mn59Qy43DjisZWQGRResjyQmgthki'
        indexes = [0x80000000, 0x80000000, 0x80000000 + 267]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(mprv, indexes)))
        self.assertEqual(addr, addr2)
        path = "m/0'/0'/267'"
        addr = address_from_xpub(bip32_xpub_from_xprv(bip32_derive(mprv,
                                                                   path)))
        self.assertEqual(addr, addr2)

        xkey_version = PRIVATE[0]
        seed = "bfc4cbaad0ff131aa97fa30a48d09ae7df914bcc083af1e07793cd0a7c61a03f65d622848209ad3366a419f4718a80ec9037df107d8d12c19b83202de00a40ad"
        seed = bytes.fromhex(seed)
        xprv = bip32_master_prvkey_from_seed(seed, xkey_version)
        xpub = b'xpub661MyMwAqRbcFMYjmw8C6dJV97a4oLss6hb3v9wTQn2X48msQB61RCaLGtNhzgPCWPaJu7SvuB9EBSFCL43kTaFJC3owdaMka85uS154cEh'
        self.assertEqual(bip32_xpub_from_xprv(xprv), xpub)

        indexes = [0, 0]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(xprv, indexes)))
        self.assertEqual(addr, b'1FcfDbWwGs1PmyhMVpCAhoTfMnmSuptH6g')

        indexes = [0, 1]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(xprv, indexes)))
        self.assertEqual(addr, b'1K5GjYkZnPFvMDTGaQHTrVnd8wjmrtfR5x')

        indexes = [0, 2]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(xprv, indexes)))
        self.assertEqual(addr, b'1PQYX2uN7NYFd7Hq22ECMzfDcKhtrHmkfi')

        indexes = [1, 0]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(xprv, indexes)))
        self.assertEqual(addr, b'1BvSYpojWoWUeaMLnzbkK55v42DbizCoyq')

        indexes = [1, 1]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(xprv, indexes)))
        self.assertEqual(addr, b'1NXB59hF4QzYpFrB7o6usLBjbk2D3ZqxAL')

        indexes = [1, 2]
        addr = address_from_xpub(
            bip32_xpub_from_xprv(bip32_derive(xprv, indexes)))
        self.assertEqual(addr, b'16NLYkKtvYhW1Jp86tbocku3gxWcvitY1w')
Exemplo n.º 7
0
mnemonic = config['mnemonic']
master_x_private_key = bip39.mprv_from_mnemonic(mnemonic=mnemonic, passphrase='', xversion=bip32.PRV[0])
derivation_path = "m/44'/0'/0'"
satoshi = int(config['satoshi'])
fee = 296
wif_list = []
outputs = []
print('Derive address from mnemonic, save the corresponding WIF to file split.wif')
print('The mnemonic is: ' + mnemonic)
print("The derivation path is: " + derivation_path)
for i in range(0, split_addresses_count):
    xprv = bip32.derive(master_x_private_key, derivation_path + "/0/" + str(i))
    wif = wif_from_xprv(xprv.decode('ascii'))
    xpub = bip32.xpub_from_xprv(xprv)
    address = bip32.address_from_xpub(xpub).decode('ascii')
    outputs.append((address, satoshi + fee, 'satoshi'))
    wif_list.append(wif)

with open('split.wif', 'w', encoding='utf-8') as file:
    for line in wif_list:
        file.writelines(line + '\n')

money = bitsv.Key(config['wif'].strip(' '))
print("Load balance from your WIF, got {} satoshi".format(money.get_balance()))

split_tx = money.send(outputs=outputs)

with open('split.tx.hash', 'w', encoding='utf-8') as file:
    file.writelines(split_tx + '\n')