Пример #1
0
def test_vectors():
    fname = "electrum_test_vectors.json"
    filename = path.join(path.dirname(__file__), "test_data", fname)
    with open(filename, "r") as f:
        test_vectors = json.load(f)

    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 = bip32.mxprv_from_electrum_mnemonic(mnemonic, passphrase)
            assert mxprv2 == mxprv.encode()

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

        if mxprv != "":
            mxpub2 = bip32.xpub_from_xprv(mxprv)
            assert mxpub2 == mxpub.encode()

        xpub = bip32.derive(mxpub, "./0/0")
        address2 = slip32.address_from_xpub(xpub).decode("ascii")
        assert address2 == address
Пример #2
0
def test_mnemonic():
    lang = "en"

    entropy = 0x110AAAA03974D093EDA670121023CD0772
    eversion = "standard"
    # FIXME: is the following mnemonic obtained in Electrum
    # from the above entropy?
    mnemonic = "ability awful fetch liberty company spatial panda hat then canal ball crouch bunker"
    mnemonic2 = electrum.mnemonic_from_entropy(entropy, eversion, lang)
    assert mnemonic == mnemonic2

    entr = int(electrum.entropy_from_mnemonic(mnemonic, lang), 2)
    assert entr - entropy < 0xFFF

    passphrase = ""
    xprv = b"xprv9s21ZrQH143K2tn5j4pmrLXkS6dkbuX6mFhJfCxAwN6ofRo5ddCrLRWogKEs1AptPmLgrthKxU2csfBgkoKECWtj1XMRicRsoWawukaRQft"
    xprv2 = bip32.mxprv_from_electrum_mnemonic(mnemonic, passphrase)
    assert xprv2 == xprv

    eversion = "std"
    with pytest.raises(ValueError,
                       match="unknown electrum mnemonic version: "):
        electrum.mnemonic_from_entropy(entropy, eversion, lang)

    unkn_ver = "ability awful fetch liberty company spatial panda hat then canal ball cross video"
    with pytest.raises(ValueError,
                       match="unknown electrum mnemonic version: "):
        electrum.entropy_from_mnemonic(unkn_ver, lang)

    with pytest.raises(ValueError,
                       match="unknown electrum mnemonic version: "):
        bip32.mxprv_from_electrum_mnemonic(unkn_ver, passphrase)

    for eversion in ("2fa", "2fa_segwit"):
        mnemonic = electrum.mnemonic_from_entropy(entropy, eversion, lang)
        with pytest.raises(ValueError,
                           match="unmanaged electrum mnemonic version: "):
            bip32.mxprv_from_electrum_mnemonic(mnemonic, passphrase)

    mnemonic = "slender flight session office noodle hand couple option office wait uniform morning"
    assert "2fa_segwit" == electrum.version_from_mnemonic(mnemonic)[0]

    mnemonic = (
        "history recycle company awful donor fold beef nominee hard bleak bracket six"
    )
    assert "2fa" == electrum.version_from_mnemonic(mnemonic)[0]
Пример #3
0
    def test_mnemonic(self):
        lang = "en"

        entropy = 0x110AAAA03974D093EDA670121023CD0772
        eversion = "standard"
        # FIXME: is the following mnemonic obtained in Electrum
        # from the above entropy?
        mnemonic = ("ability awful fetch liberty company spatial "
                    "panda hat then canal ball crouch bunker")
        mnemonic2 = electrum.mnemonic_from_entropy(entropy, eversion, lang)
        self.assertEqual(mnemonic, mnemonic2)

        entr = int(electrum.entropy_from_mnemonic(mnemonic, lang), 2)
        self.assertLess(entr - entropy, 0xFFF)

        # mnemonic version not in electrum allowed mnemonic versions
        eversion = "std"
        self.assertRaises(ValueError, electrum.mnemonic_from_entropy, entropy,
                          eversion, lang)
        # electrum.mnemonic_from_entropy(entropy, eversion, lang)

        # unknown electrum mnemonic version (00c)
        unknown_version = ("ability awful fetch liberty company spatial "
                           "panda hat then canal ball cross video")
        self.assertRaises(ValueError, electrum.entropy_from_mnemonic,
                          unknown_version, lang)
        # electrum.entropy_from_mnemonic(unknown_version, lang)

        passphrase = ""

        # unknown electrum mnemonic version (00c)
        self.assertRaises(ValueError, bip32.mxprv_from_electrum_mnemonic,
                          unknown_version, passphrase)
        # bip32.mxprv_from_electrum_mnemonic(mnemonic, passphrase)

        xprv = ("xprv9s21ZrQH143K2tn5j4pmrLXkS6dkbuX6mFhJfCxAwN6ofRo5ddCrLRWo"
                "gKEs1AptPmLgrthKxU2csfBgkoKECWtj1XMRicRsoWawukaRQft")
        xprv2 = bip32.mxprv_from_electrum_mnemonic(mnemonic, passphrase)
        self.assertEqual(xprv2, xprv.encode())

        eversion = "2fa"
        mnemonic = electrum.mnemonic_from_entropy(entropy, eversion, lang)
        # 2fa mnemonic version is not managed yet
        self.assertRaises(ValueError, bip32.mxprv_from_electrum_mnemonic,
                          mnemonic, passphrase)
        # bip32.mxprv_from_electrum_mnemonic(mnemonic, passphrase)

        eversion = "2fa_segwit"
        mnemonic = electrum.mnemonic_from_entropy(entropy, eversion, lang)
        # 2fa_segwit mnemonic version is not managed yet
        self.assertRaises(ValueError, bip32.mxprv_from_electrum_mnemonic,
                          mnemonic, passphrase)
Пример #4
0
    def test_vectors(self):
        fname = "electrum_test_vectors.json"
        filename = path.join(path.dirname(__file__), "test_data", fname)
        with open(filename, "r") as f:
            test_vectors = json.load(f)

        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 = bip32.mxprv_from_electrum_mnemonic(
                    mnemonic, passphrase)
                self.assertEqual(mxprv2, mxprv.encode())

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

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

            xpub = bip32.derive(mxpub, "./0/0")
            address2 = slip32.address_from_xpub(xpub).decode("ascii")
            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)[0])

        # version 2fa
        mnemonic = ("history recycle company awful donor   fold "
                    "beef    nominee hard    bleak bracket six")
        self.assertEqual("2fa", electrum.version_from_mnemonic(mnemonic)[0])
Пример #5
0
def test_vectors() -> None:
    fname = "electrum_test_vectors.json"
    filename = path.join(path.dirname(__file__), "test_data", fname)
    with open(filename, "r") as f:
        test_vectors = json.load(f)

    lang = "en"
    for mnemonic, passphrase, rmxprv, rmxpub, address in test_vectors:
        if mnemonic != "":
            mxprv2 = bip32.mxprv_from_electrum_mnemonic(mnemonic, passphrase)
            assert mxprv2 == rmxprv.encode()

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

        assert rmxpub.encode() == bip32.xpub_from_xprv(rmxprv)

        xprv = bip32.derive(rmxprv, "m/0h/0")
        address2 = slip132.address_from_xkey(xprv).decode("ascii")
        assert address2 == address
Пример #6
0
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

import secrets

from btclib import bip32, bip39, electrum

entropy = secrets.randbits(256)

bip39_mnemonic = bip39.mnemonic_from_entropy(entropy)
print()
print(bip39_mnemonic)
rxprv = bip32.mxprv_from_bip39_mnemonic(bip39_mnemonic)
rxpub = bip32.xpub_from_xprv(rxprv)
# warning: first level should always be hardened
# or any (depth=1) child private key would compromise rxprv
path = "m/0h"
xprv = bip32.derive(rxprv, path)
print(path + f" : {xprv!r}")

electrum_mnemonic = electrum.mnemonic_from_entropy(entropy)
print()
print(electrum_mnemonic)
mxprv = bip32.mxprv_from_electrum_mnemonic(electrum_mnemonic)
mxpub = bip32.xpub_from_xprv(mxprv)
# warning: first level should always be hardened
# or any (depth=1) child private key would compromise mxprv
path = "m/0h"
xprv = bip32.derive(mxprv, path)
print(path + f" : {xprv!r}")