コード例 #1
0
ファイル: test_electrum.py プロジェクト: eehlers/btclib
    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)
コード例 #2
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(eversion, entropy, 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, eversion,
                          entropy, lang)
        #electrum.mnemonic_from_entropy(eversion, entropy, 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, electrum.masterxprv_from_mnemonic,
                          unknown_version, passphrase)
        #electrum.masterxprv_from_mnemonic(mnemonic, passphrase)

        xprv = "xprv9s21ZrQH143K2tn5j4pmrLXkS6dkbuX6mFhJfCxAwN6ofRo5ddCrLRWogKEs1AptPmLgrthKxU2csfBgkoKECWtj1XMRicRsoWawukaRQft"
        xprv2 = electrum.masterxprv_from_mnemonic(mnemonic, passphrase)
        self.assertEqual(xprv2.decode(), xprv)

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

        eversion = '2fa_segwit'
        mnemonic = electrum.mnemonic_from_entropy(eversion, entropy, lang)
        # 2fa_segwit mnemonic version is not managed yet
        self.assertRaises(ValueError, electrum.masterxprv_from_mnemonic,
                          mnemonic, passphrase)
コード例 #3
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
コード例 #4
0
    def test_mnemonic(self):
        lang = "en"

        entropy = 0x110aaaa03974d093eda670121023cd0772
        eversion = 'standard'
        mnemonic = electrum.mnemonic_from_entropy(entropy, lang, eversion)
        entr = int(electrum.entropy_from_mnemonic(mnemonic, lang), 2)
        self.assertLess(entr - entropy, 0xfff)

        passphrase = ''

        xversion = b'\x04\x88\xAD\xE4'
        rootxprv = electrum.rootxprv_from_mnemonic(mnemonic, passphrase,
                                                   xversion)
        # TODO: compare with the rootxprv generated by electrum
        rootxprv2 = electrum.rootxprv_from_entropy(entropy, passphrase, lang,
                                                   xversion)
        self.assertEqual(rootxprv2, rootxprv)

        # unmanaged electrum mnemonic version
        mnemonic = "ability awful fetch liberty company spatial panda \
                    hat then canal ball cross video"

        self.assertRaises(ValueError, electrum.rootxprv_from_mnemonic,
                          mnemonic, passphrase, xversion)
        #electrum_rootxprv_from_mnemonic(mnemonic, passphrase, xversion)

        # mnemonic version not in electrum allowed mnemonic versions
        eversion = 'std'
        self.assertRaises(ValueError, electrum.mnemonic_from_entropy, entropy,
                          lang, eversion)
コード例 #5
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]
コード例 #6
0
ファイル: test_electrum.py プロジェクト: eehlers/btclib
    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])
コード例 #7
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))
コード例 #8
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
コード例 #9
0
    def test_vectors(self):
        filename = "electrum_test_vectors.json"
        path_to_filename = os.path.join(os.path.dirname(__file__), "./data/",
                                        filename)
        with open(path_to_filename, 'r') as f:
            test_vectors = json.load(f)
        f.closed

        for test_vector in test_vectors:
            version = test_vector[0]
            mnemonic = test_vector[1]
            passphrase = test_vector[2]
            xpub = test_vector[3]

            xversion = bip32.PRV_VERSION[0]  #FIXME: version / xversion
            rootxprv = electrum.rootxprv_from_mnemonic(mnemonic, passphrase,
                                                       xversion)
            xpub2 = bip32.xpub_from_xprv(rootxprv).decode()
            self.assertEqual(xpub2, xpub)

            lang = "en"
            entr = int(electrum.entropy_from_mnemonic(mnemonic, lang), 2)
            mnem = electrum.mnemonic_from_entropy(entr, lang, version)
            self.assertEqual(mnem, mnemonic)
コード例 #10
0
ファイル: mnemonic.py プロジェクト: pagani-irene/bbt
# 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}")